Home | History | Annotate | Download | only in capstone
      1 #!/usr/bin/env bash
      2 
      3 # Capstone Disassembly Engine
      4 # By Nguyen Anh Quynh <aquynh (at] gmail.com>, 2013-2014
      5 
      6 # Note: to cross-compile "nix32" on Linux, package gcc-multilib is required.
      7 
      8 
      9 # build iOS lib for all iDevices, or only specific device
     10 function build_iOS {
     11 	IOS_SDK=`xcrun --sdk iphoneos --show-sdk-path`
     12 	IOS_CC=`xcrun --sdk iphoneos -f clang`
     13 	IOS_CFLAGS="-Os -Wimplicit -isysroot $IOS_SDK"
     14 	IOS_LDFLAGS="-isysroot $IOS_SDK"
     15 	if (( $# == 0 )); then
     16 		# build for all iDevices
     17 		IOS_ARCHS="armv7 armv7s arm64"
     18 	else
     19 		IOS_ARCHS="$1"
     20 	fi
     21 	CC="$IOS_CC" CFLAGS="$IOS_CFLAGS" LDFLAGS="$IOS_LDFLAGS" LIBARCHS="$IOS_ARCHS" ${MAKE}
     22 }
     23 
     24 # build Android lib for only one supported architecture
     25 function build_android {
     26     if [ -z "$NDK" ]; then
     27         echo "ERROR! Please set \$NDK to point at your Android NDK directory."
     28         exit 1
     29     fi
     30     HOSTOS=$(uname -s | tr 'LD' 'ld')
     31     HOSTARCH=$(uname -m)
     32 
     33     TARGARCH="$1"
     34     shift
     35 
     36     case "$TARGARCH" in
     37       arm)
     38           [ -n "$APILEVEL" ] || APILEVEL="android-14"  # default to ICS
     39           [ -n "$GCCVER" ] || GCCVER="4.8"
     40           CROSS=arm-linux-androideabi-
     41           ;;
     42       arm64)
     43           [ -n "$APILEVEL" ] || APILEVEL="android-21"  # first with arm64
     44           [ -n "$GCCVER" ] || GCCVER="4.9"
     45           CROSS=aarch64-linux-android-
     46           ;;
     47 
     48       *)
     49           echo "ERROR! Building for Android on $1 is not currently supported."
     50           exit 1
     51           ;;
     52     esac
     53 
     54     TOOLCHAIN="$NDK/toolchains/$CROSS$GCCVER/prebuilt/$HOSTOS-$HOSTARCH"
     55     PLATFORM="$NDK/platforms/$APILEVEL/arch-$TARGARCH"
     56 
     57     CROSS="$TOOLCHAIN/bin/$CROSS" CFLAGS="--sysroot=$PLATFORM" LDFLAGS="--sysroot=$PLATFORM" ${MAKE} $*
     58 }
     59 
     60 
     61 function build {
     62 	if [ $(uname -s) = Darwin ]; then
     63 		export LIBARCHS="i386 x86_64"
     64 	fi
     65 
     66 	if [ -n "$CC" ]; then
     67 		${MAKE} CC="$CC" $*
     68 	else
     69 		${MAKE} $*
     70 	fi
     71 }
     72 
     73 function install {
     74 	# Mac OSX needs to find the right directory for pkgconfig
     75 	if [ "$(uname)" == "Darwin" ]; then
     76 		# we are going to install into /usr/local, so remove old installs under /usr
     77 		rm -rf /usr/lib/libcapstone.*
     78 		rm -rf /usr/include/capstone
     79 		# install into /usr/local
     80 		export PREFIX=/usr/local
     81 		# find the directory automatically, so we can support both Macport & Brew
     82 		PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
     83 		# set PKGCFGDIR only in non-Brew environment & pkg-config is available
     84 		if [ "$HOMEBREW_CAPSTONE" != "1" ] && [ ${PKGCFGDIR}x != x ]; then
     85 			if [ ${CC}x != x ]; then
     86 				${MAKE} CC=$CC PKGCFGDIR=$PKGCFGDIR install
     87 			else
     88 				${MAKE} PKGCFGDIR=$PKGCFGDIR install
     89 			fi
     90 		else
     91 			if [ ${CC}x != x ]; then
     92 				${MAKE} CC=$CC install
     93 			else
     94 				${MAKE} install
     95 			fi
     96 		fi
     97 	else	# not OSX
     98 		if test -d /usr/lib64; then
     99 			if [ -n "$CC" ]; then
    100 				${MAKE} LIBDIRARCH=lib64 CC="$CC" install
    101 			else
    102 				${MAKE} LIBDIRARCH=lib64 install
    103 			fi
    104 		else
    105 			if [ -n "$CC" ]; then
    106 				${MAKE} CC="$CC" install
    107 			else
    108 				${MAKE} install
    109 			fi
    110 		fi
    111 	fi
    112 }
    113 
    114 function uninstall {
    115 	# Mac OSX needs to find the right directory for pkgconfig
    116 	if [ "$(uname)" == "Darwin" ]; then
    117 		# find the directory automatically, so we can support both Macport & Brew
    118 		PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
    119 		export PREFIX=/usr/local
    120 		if [ ${PKGCFGDIR}x != x ]; then
    121 			${MAKE} PKGCFGDIR=$PKGCFGDIR uninstall
    122 		else
    123 			${MAKE} uninstall
    124 		fi
    125 	else	# not OSX
    126 		if test -d /usr/lib64; then
    127 			${MAKE} LIBDIRARCH=lib64 uninstall
    128 		else
    129 			${MAKE} uninstall
    130 		fi
    131 	fi
    132 }
    133 
    134 MAKE=make
    135 if [ "$(uname)" == "SunOS" ]; then
    136 	export MAKE=gmake
    137 	export INSTALL_BIN=ginstall
    138 	export CC=gcc
    139 fi
    140 
    141 if [[ "$(uname)" == *BSD* ]]; then
    142 	export MAKE=gmake
    143 	export PREFIX=/usr/local
    144 fi
    145 
    146 TARGET="$1"
    147 shift
    148 
    149 case "$TARGET" in
    150   "" ) build $*;;
    151   "default" ) build $*;;
    152   "debug" ) CAPSTONE_USE_SYS_DYN_MEM=yes CAPSTONE_STATIC=yes CFLAGS='-O0 -g -fsanitize=address' LDFLAGS='-fsanitize=address' build $*;;
    153   "install" ) install;;
    154   "uninstall" ) uninstall;;
    155   "nix32" ) CFLAGS=-m32 LDFLAGS=-m32 build $*;;
    156   "cross-win32" ) CROSS=i686-w64-mingw32- build $*;;
    157   "cross-win64" ) CROSS=x86_64-w64-mingw32- build $*;;
    158   "cygwin-mingw32" ) CROSS=i686-pc-mingw32- build $*;;
    159   "cygwin-mingw64" ) CROSS=x86_64-w64-mingw32- build $*;;
    160   "cross-android" ) build_android $*;;
    161   "clang" ) CC=clang build $*;;
    162   "gcc" ) CC=gcc build $*;;
    163   "ios" ) build_iOS $*;;
    164   "ios_armv7" ) build_iOS armv7 $*;;
    165   "ios_armv7s" ) build_iOS armv7s $*;;
    166   "ios_arm64" ) build_iOS arm64 $*;;
    167   "osx-kernel" ) CAPSTONE_USE_SYS_DYN_MEM=yes CAPSTONE_HAS_OSXKERNEL=yes CAPSTONE_ARCHS=x86 CAPSTONE_SHARED=no CAPSTONE_BUILD_CORE_ONLY=yes build $*;;
    168   * ) echo "Usage: make.sh [nix32|cross-win32|cross-win64|cygwin-mingw32|cygwin-mingw64|ios|ios_armv7|ios_armv7s|ios_arm64|cross-android arm|cross-android arm64|clang|gcc|install|uninstall]"; exit 1;;
    169 esac
    170