Saturday, March 12, 2016

Compiling GraphicsMagick (1.3.23) using Android NDK

Compiling GraphicsMagick (1.3.23) using Android NDK


Before starting read this common guide how to build libpng with Android NDK  for most of 
building process as it will be re-writing of same stuff if I write here as well.

After above guide understood and ensured the NDK and other tools are correctly setup.
Follow these step to build GraphicsMagick 1.3.23.
 

Setup:
  • Create directory GM and extract GraphicsMagick 1.3.23 contents inside it. Go inside extracted GM directory.
  • Create directories "prefix" and "dependencies". Instead of prefix you can give names like bin, binaries etc.
  • Copy lib****.a files in GM/dependencies directory. libjasper.a liblzma.a libtiff.a libfreetype.a libjpeg.a libpng.a libwebp.a. If you don't have these libraries then with help of above libpng, webp, FreeType etc guide you can make them.
  • Create GM/dependencies/include directory and copy APIs i.e. headers from all above libraries inside it.
Below is the script to build GraphicsMagick. It does not build c++ apis.


Android Script

# AndroidBuild.sh START
#!/bin/sh
# http://www.ourinnovativemind.in
# http://www.gamesgreh.in

# use custom build environment
export DEPENDENCIES="`pwd`/dependencies"
export INC_DEPENDENCIES=${DEPENDENCIES}/include
export INC_JPEG=${INC_DEPENDENCIES}/jpeg
export INC_PNG=${INC_DEPENDENCIES}/png
export INC_TIFF=${INC_DEPENDENCIES}/tiff
export INC_WEBP=${INC_DEPENDENCIES}/webp
export INC_TTF=${INC_DEPENDENCIES}/ttf
export INC_JASPER=${INC_DEPENDENCIES}/jasper
export INC_LZMA=${INC_DEPENDENCIES}/lzma


export EXTRA_INCLUDES="-I${INC_DEPENDENCIES} -I${INC_JPEG} -I${INC_PNG} -I${INC_TIFF} -I${INC_WEBP} -I${INC_TTF} -I${INC_JASPER} -I${INC_LZMA}"
export EXTRA_LIBS=$DEPENDENCIES


export CONFIGURATION_OPTIONS="--disable-largefile --disable-openmp --without-magick-plus-plus --disable-installed --without-threads --without-x --without-perl --disable-shared --without-gslib"



# use custom build environment

# installation directory
export PREFIX="`pwd`/prefix"

# Path of NDK. For simplicity make symlinks in home directories
export NDK=/home/bindesh/bin/ndk

# processor specific libraries and includes.
# Suffix for arch-???. arch-arm  arch-arm64  arch-mips  arch-mips64  arch-x86  arch-x86_64
export ARCH=arm

# compiler for processor
export CROSS_COMPILE=arm-linux-androideabi

# Android platform version
export PLATFORM=android-8

# GCC compiler version
export GCC_VERSION=4.8


# Getting toolchain directory inside NDK from variables set above
# Hardcoded for linux running on x86 cpu 64-Bit
export ANDROID_PREFIX="${NDK}/toolchains/$CROSS_COMPILE-$GCC_VERSION/prebuilt/linux-x86_64"

# SYSROOT. usr dir i.e. usr/include, usr/lib
export SYSROOT="${NDK}/platforms/$PLATFORM/arch-$ARCH"
export CROSS_PATH="${ANDROID_PREFIX}/bin/${CROSS_COMPILE}"

# Some compiler and binutils. More might be needed depending on source. 
export CPP=${CROSS_PATH}-cpp
export AR=${CROSS_PATH}-ar
export AS=${CROSS_PATH}-as
export NM=${CROSS_PATH}-nm
export CC=${CROSS_PATH}-gcc
export CXX=${CROSS_PATH}-g++
export LD=${CROSS_PATH}-ld
export RANLIB=${CROSS_PATH}-ranlib

# for C++ extra libs
export CXX_SYSROOT=${NDK}/sources/cxx-stl/gnu-libstdc++/${COMPILER_VERSION}  # standard template library
export CXX_BITS_INCLUDE=${CXX_SYSROOT}/libs/armeabi/include

export PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig


# Flags for compiler
export CFLAGS="${CFLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/usr/include -I${ANDROID_PREFIX}/include ${EXTRA_INCLUDES}"
export CPPFLAGS="${CFLAGS}"
export LDFLAGS="${LDFLAGS} -L${SYSROOT}/usr/lib -L${ANDROID_PREFIX}/lib -L${EXTRA_LIBS}"


./configure --host=${CROSS_COMPILE} --with-sysroot=${SYSROOT} --prefix=${PREFIX} "$@" $CONFIGURATION_OPTIONS
make
make install
# AndroidBuild.sh END


Copy all lines above and paste in a new file AndroidBuild.sh and save. Then change the 
variables valid for your system. Create folder prefix or whatever name you like. 
Change NDK variable and compiler related values as per your system.
This script copied from web can cause error due to removal of newlines for strange reasons.
Ensure # is ahead of comments. 
 Now run sh AndroidBuild.sh. It should end without errors. This will install all built files
in prefix directory. Now we can copy libpng16.a renamed to libpng.a and headers directory.


Links:

Script file
http://txt.do/5ns57 or http://textuploader.com/5ns57