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