Skip to content

Instantly share code, notes, and snippets.

@RikudouSage
Last active June 19, 2018 12:20
Show Gist options
  • Save RikudouSage/8c75d3c9eb6f9d49e1b932e7089a7946 to your computer and use it in GitHub Desktop.
Save RikudouSage/8c75d3c9eb6f9d49e1b932e7089a7946 to your computer and use it in GitHub Desktop.
How To Compile Magick.NET native for AWS Lambda

How To Compile Magick.NET native for AWS Lambda

So you want to use Magick.NET on AWS Lambda? Well, not as simple as it sounds. Fortunatelly for you, here is a step by step guide to use Magick.NET on AWS Lambda.

Prepare

Create an Amazon Linux instance used for building.

Preferably choose some bigger instance so the build process is quick. (I used c4.xlarge). You can create it as a spot instance to save some coins, after the process is done, you won't need the instance anymore. Choose more storage than the default 8 GB, you can run out of inodes if you have a lot of files (actually happened to me). I used 20 GB and everything was smooth.

After the instance is created, ssh into it and login as root (sudo su -).

For lazy people

You can use this "one-liner" to compile it all, it's basically all of the commands below merged into one command. The downside is that in case of error it gets harder to identify the exact step that failed. If you don't want to use this, skip below and do the step-by-step guide.

yum update -y && yum groupinstall "Development Tools" -y && yum install -y cmake git make autoconf yum-utils libunwind libicu wget nasm && export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/ && git clone https://github.com/dlemstra/Magick.NET.git && cd Magick.NET && git checkout tags/7.2.1.0 && cd ImageMagick/Source && ./Checkout.sh && cd ImageMagick/ImageMagick && git clone https://github.com/libjpeg-turbo/libjpeg-turbo.git jpeg && cd jpeg && git checkout tags/1.5.2 && autoreconf -fiv && ./configure --disable-shared --with-pic --prefix="/usr/local/" && make && make install && cd .. && git clone https://github.com/madler/zlib.git zlib && cd zlib && git checkout tags/v1.2.9 && ./configure --static && make CFLAGS='-fPIC' && make install && cd .. && git clone https://github.com/mm2/Little-CMS.git lcms && cd lcms && git checkout tags/lcms2.8 && ./configure --disable-shared --with-pic && make && make install && cd .. && git clone https://github.com/webmproject/libwebp.git webp && cd webp && git checkout tags/v0.5.1 && ./autogen.sh && ./configure --disable-shared --with-pic && make && make install && cd .. && git clone https://git.code.sf.net/p/libpng/code png && cd png && git checkout tags/v1.6.13beta03 && ./autogen.sh && ./configure --disable-shared --with-pic && make && make install && cd .. && wget https://download.savannah.gnu.org/releases/freetype/freetype-2.7.tar.gz && tar xzf freetype-2.7.tar.gz && mv freetype-2.7 freetype && cd freetype && ./configure --with-pic && make && make install && cd .. && ./configure --with-quantum-depth=8 --enable-hdri=no --with-magick-plus-plus=no --with-jpeg --with-png --with-webp --with-lcms --with-zlib --without-pango --without-x --without-fontconfig --with-freetype --disable-shared --with-pic --enable-delegate-build && make && make install && cd ../../../../Source/Magick.NET.Native && wget https://gist.githubusercontent.com/RikudouSage/8c75d3c9eb6f9d49e1b932e7089a7946/raw/d175a8843d1369477ac4e8d3a739ee8ef6489717/CMakeLists.txt && mkdir build && cd build && cmake .. -DQUANTUM_DEPTH=8 -DHDRI=no && make && mkdir -p ../../../../Magick.NET.Native/{lib,lib64} && cp *.so ../../../../Magick.NET.Native/lib && cd ../../../../Magick.NET.Native && cp /usr/lib64/libfreetype.so.6 ./lib64 && zip -r Magick.NET.Native.zip $(ls)

After this command finishes, you can skip to the Done section.

Install some dependencies etc.

  1. Update packages
    • yum update -y
  2. Install development tools
    • yum groupinstall "Development Tools" -y
    • yum install -y cmake git make autoconf yum-utils libunwind libicu wget nasm
  3. Fix package config resolution (This needs to be done every time you log out and in)
    • export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

Build it all

  1. Clone Magick.NET and cd into it
    • git clone https://github.com/dlemstra/Magick.NET.git
    • cd Magick.NET
  2. Select the Magick.NET version you want and checkout the tag, I'll use 7.2.1.0
    • git checkout tags/7.2.1.0
  3. Clone the coresponding ImageMagick version
    • cd ImageMagick/Source
    • ./Checkout.sh
  4. Build ImageMagick dependencies
    • cd ImageMagick/ImageMagick
    1. Build jpeg
      • git clone https://github.com/libjpeg-turbo/libjpeg-turbo.git jpeg
      • cd jpeg
      • git checkout tags/1.5.2
      • autoreconf -fiv
      • ./configure --disable-shared --with-pic --prefix="/usr/local/"
      • make && make install
      • cd ..
    2. Build zlib
      • git clone https://github.com/madler/zlib.git zlib
      • cd zlib
      • git checkout tags/v1.2.9
      • ./configure --static
      • make CFLAGS='-fPIC' && make install
      • cd ..
    3. Build lcms
      • git clone https://github.com/mm2/Little-CMS.git lcms
      • cd lcms
      • git checkout tags/lcms2.8
      • ./configure --disable-shared --with-pic
      • make && make install
      • cd ..
    4. Build webp
      • git clone https://github.com/webmproject/libwebp.git webp
      • cd webp
      • git checkout tags/v0.5.1
      • ./autogen.sh
      • ./configure --disable-shared --with-pic
      • make && make install
      • cd ..
    5. Build png
      • git clone https://git.code.sf.net/p/libpng/code png
      • cd png
      • git checkout tags/v1.6.13beta03
      • ./autogen.sh
      • ./configure --disable-shared --with-pic
      • make && make install
      • cd ..
    6. Build freetype
      • wget https://download.savannah.gnu.org/releases/freetype/freetype-2.7.tar.gz
      • tar xzf freetype-2.7.tar.gz
      • mv freetype-2.7 freetype
      • cd freetype
      • ./configure --with-pic
      • make && make install
      • cd ..
  5. Build ImageMagick, you can replace quantum-depth and hdri parameters
    • ./configure --with-quantum-depth=8 --enable-hdri=no --with-magick-plus-plus=no --with-jpeg --with-png --with-webp --with-lcms --with-zlib --without-pango --without-x --without-fontconfig --with-freetype --disable-shared --with-pic --enable-delegate-build
    • make && make install
  6. Create Magick.NET wrapper, in the cmake step input the same settings for quantum-depth and hdri as in ImageMagick compilation
    • cd ../../../../Source/Magick.NET.Native
    • nano CMakeLists.txt and paste (CTRL+SHIFT+V) the content of this file.
    • mkdir build && cd build
    • cmake .. -DQUANTUM_DEPTH=8 -DHDRI=no
    • make
  7. Wrap the libraries in a nice package
    • mkdir -p ../../../../Magick.NET.Native/{lib,lib64}
    • cp *.so ../../../../Magick.NET.Native/lib
    • cd ../../../../Magick.NET.Native
    • cp /usr/lib64/libfreetype.so.6 ./lib64
    • zip -r Magick.NET.Native.zip $(ls)

Done

Now you can move the Magick.Net.Native.zip file somewhere you like, for example move it to /home/ec2-user and then copy it to your pc via SFTP.

In your C# project for AWS Lambda extract the zip archive and everything's done. Your project should include two new directories, lib and lib64. You must set the project to include these two directories in publish package.

cmake_minimum_required(VERSION 2.8)
set(QUANTUM_DEPTH 8)
set(HDRI NO)
set(TARGET_NAME Magick.NET-Q${QUANTUM_DEPTH}-x64.Native.dll)
if (${HDRI})
set(HDRI_SUFFIX HDRI)
endif()
find_package(PkgConfig REQUIRED)
pkg_check_modules(IM MagickWand-7.Q${QUANTUM_DEPTH}${HDRI_SUFFIX} REQUIRED)
pkg_check_modules(JPEG libjpeg REQUIRED)
include_directories(. ${IM_INCLUDE_DIRS} ${JPEG_INCLUDE_DIRS})
add_definitions(-DMAGICK_NET_LINUX -DMAGICKCORE_QUANTUM_DEPTH=${QUANTUM_DEPTH} -DMAGICKCORE_HDRI_ENABLE=0)
add_library(${TARGET_NAME} SHARED
./Colors/MagickColor.c
./Colors/MagickColorCollection.c
./Drawables/DrawingWand.c
./Drawables/PointInfoCollection.c
./Exceptions/MagickExceptionHelper.c
./Helpers/Environment.c
./Helpers/MagickMemory.c
./MagickFormatInfo.c
./MagickImage.c
./MagickImageCollection.c
./MagickNET.c
./Matricis/DoubleMatrix.c
./OpenCL/OpenCL.c
./OpenCL/OpenCLDevice.c
./OpenCL/OpenCLKernelProfileRecord.c
./Optimizers/JpegOptimizer.c
./Pixels/PixelCollection.c
./Quantum.c
./ResourceLimits.c
./Settings/DrawingSettings.c
./Settings/MagickSettings.c
./Settings/MontageSettings.c
./Settings/QuantizeSettings.c
./Statistics/ChannelMoments.c
./Statistics/ChannelPerceptualHash.c
./Statistics/ChannelStatistics.c
./Statistics/Moments.c
./Statistics/PerceptualHash.c
./Statistics/Statistics.c
./Stdafx.c
./Types/ConnectedComponent.c
./Types/MagickGeometry.c
./Types/MagickRectangle.c
./Types/OffsetInfo.c
./Types/PointInfo.c
./Types/PrimaryInfo.c
./Types/StringInfo.c
./Types/TypeMetric.c)
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS -Wl,--no-undefined)
target_link_libraries(${TARGET_NAME} ${IM_STATIC_LIBRARIES} pthread)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment