blob: ae504ea02aee260f0eb6a7511f9fb484ce4524ad [file] [log] [blame]
pancake63414a42015-02-27 17:57:53 +01001#!/bin/sh
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +08002
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08003# Capstone Disassembly Engine
pancake63414a42015-02-27 17:57:53 +01004# By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +08005
6# Note: to cross-compile "nix32" on Linux, package gcc-multilib is required.
7
pancake63414a42015-02-27 17:57:53 +01008MAKE_JOBS=$((${MAKE_JOBS}+0))
9[ ${MAKE_JOBS} -lt 1 ] && \
10 MAKE_JOBS=4
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +080011
Nguyen Anh Quynhfc3b1382015-06-22 12:51:21 +080012# build Android lib for only one supported architecture
radare71fd42d2015-07-28 16:49:00 +020013build_android() {
Nguyen Anh Quynhfc3b1382015-06-22 12:51:21 +080014 if [ -z "$NDK" ]; then
15 echo "ERROR! Please set \$NDK to point at your Android NDK directory."
16 exit 1
17 fi
18 HOSTOS=$(uname -s | tr 'LD' 'ld')
19 HOSTARCH=$(uname -m)
20
21 TARGARCH="$1"
22 shift
23
24 case "$TARGARCH" in
25 arm)
26 [ -n "$APILEVEL" ] || APILEVEL="android-14" # default to ICS
27 [ -n "$GCCVER" ] || GCCVER="4.8"
28 CROSS=arm-linux-androideabi-
29 ;;
30 arm64)
31 [ -n "$APILEVEL" ] || APILEVEL="android-21" # first with arm64
32 [ -n "$GCCVER" ] || GCCVER="4.9"
33 CROSS=aarch64-linux-android-
34 ;;
35
36 *)
37 echo "ERROR! Building for Android on $1 is not currently supported."
38 exit 1
39 ;;
40 esac
41
42 TOOLCHAIN="$NDK/toolchains/$CROSS$GCCVER/prebuilt/$HOSTOS-$HOSTARCH"
43 PLATFORM="$NDK/platforms/$APILEVEL/arch-$TARGARCH"
44
45 ${MAKE} clean
46
47 CROSS="$TOOLCHAIN/bin/$CROSS" CFLAGS="--sysroot=$PLATFORM" LDFLAGS="--sysroot=$PLATFORM" ${MAKE} $*
48}
49
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +080050# build iOS lib for all iDevices, or only specific device
pancake63414a42015-02-27 17:57:53 +010051build_iOS() {
52 ${MAKE} clean
53 IOS_SDK=`xcrun --sdk iphoneos --show-sdk-path`
54 IOS_CC=`xcrun --sdk iphoneos -f clang`
55 IOS_CFLAGS="-Os -Wimplicit -isysroot $IOS_SDK"
56 IOS_LDFLAGS="-isysroot $IOS_SDK"
57 if [ -z "$1" ]; then
58 # build for all iDevices
59 IOS_ARCHS="armv7 armv7s arm64"
60 else
61 IOS_ARCHS="$1"
62 fi
63 CC="$IOS_CC" \
64 CFLAGS="$IOS_CFLAGS" \
65 LDFLAGS="$IOS_LDFLAGS" \
66 LIBARCHS="$IOS_ARCHS" \
67 ${MAKE}
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +080068}
69
pancake63414a42015-02-27 17:57:53 +010070build() {
71 [ "$UNAME" = Darwin ] && LIBARCHS="i386 x86_64"
72 ${MAKE} clean
Nguyen Anh Quynhfc3b1382015-06-22 12:51:21 +080073 ${MAKE} $*
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080074}
75
pancake63414a42015-02-27 17:57:53 +010076install() {
77 # Mac OSX needs to find the right directory for pkgconfig
78 if [ "$UNAME" = Darwin ]; then
79 # we are going to install into /usr/local, so remove old installs under /usr
80 rm -rf /usr/lib/libcapstone.*
81 rm -rf /usr/include/capstone
82 # install into /usr/local
83 PREFIX=/usr/local
pancaked1af4dd2015-03-02 10:55:55 +010084 if [ "${HOMEBREW_CAPSTONE}" != 1 ]; then
85 # find the directory automatically, so we can support both Macport & Brew
86 PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
87 fi
pancake63414a42015-02-27 17:57:53 +010088 ${MAKE} install
89 else # not OSX
90 test -d /usr/lib64 && ${MAKE} LIBDIRARCH=lib64
91 ${MAKE} install
92 fi
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080093}
94
pancake63414a42015-02-27 17:57:53 +010095uninstall() {
96 # Mac OSX needs to find the right directory for pkgconfig
97 if [ "$UNAME" = "Darwin" ]; then
98 # find the directory automatically, so we can support both Macport & Brew
99 PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
100 PREFIX=/usr/local
101 ${MAKE} uninstall
102 else # not OSX
103 test -d /usr/lib64 && LIBDIRARCH=lib64
104 ${MAKE} uninstall
105 fi
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800106}
107
pancake63414a42015-02-27 17:57:53 +0100108if [ "$UNAME" = SunOS ]; then
109 [ -z "${MAKE}" ] && MAKE=gmake
110 INSTALL_BIN=ginstall
111 CC=gcc
Nguyen Anh Quynhd3b30712014-01-17 21:25:18 +0800112fi
113
pancake63414a42015-02-27 17:57:53 +0100114if [ -n "`echo "$UNAME" | grep BSD`" ]; then
115 MAKE=gmake
116 PREFIX=/usr/local
Nguyen Anh Quynhf63db272014-01-17 22:12:35 +0800117fi
118
pancake63414a42015-02-27 17:57:53 +0100119[ -z "${UNAME}" ] && UNAME=$(uname)
120[ -z "${MAKE}" ] && MAKE=make
121[ -n "${MAKE_JOBS}" ] && MAKE="$MAKE -j${MAKE_JOBS}"
radare796f1d42015-03-07 11:40:19 +0100122export CC INSTALL_BIN PREFIX PKGCFGDIR LIBDIRARCH LIBARCHS CFLAGS LDFLAGS
pancake63414a42015-02-27 17:57:53 +0100123
Nguyen Anh Quynhfc3b1382015-06-22 12:51:21 +0800124TARGET="$1"
radare71fd42d2015-07-28 16:49:00 +0200125[ -n "$TARGET" ] && shift
Nguyen Anh Quynhfc3b1382015-06-22 12:51:21 +0800126
127case "$TARGET" in
128 "" ) build $*;;
129 "default" ) build $*;;
130 "debug" ) CAPSTONE_USE_SYS_DYN_MEM=yes CAPSTONE_STATIC=yes CFLAGS='-O0 -g -fsanitize=address' LDFLAGS='-fsanitize=address' build $*;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800131 "install" ) install;;
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +0800132 "uninstall" ) uninstall;;
Nguyen Anh Quynhfc3b1382015-06-22 12:51:21 +0800133 "nix32" ) CFLAGS=-m32 LDFLAGS=-m32 build $*;;
134 "cross-win32" ) CROSS=i686-w64-mingw32- build $*;;
135 "cross-win64" ) CROSS=x86_64-w64-mingw32- build $*;;
136 "cygwin-mingw32" ) CROSS=i686-pc-mingw32- build $*;;
137 "cygwin-mingw64" ) CROSS=x86_64-w64-mingw32- build $*;;
138 "cross-android" ) build_android $*;;
139 "cross-android64" ) CROSS=aarch64-linux-gnu- build $*;; # Linux cross build
140 "clang" ) CC=clang build $*;;
141 "gcc" ) CC=gcc build $*;;
142 "ios" ) build_iOS $*;;
143 "ios_armv7" ) build_iOS armv7 $*;;
144 "ios_armv7s" ) build_iOS armv7s $*;;
145 "ios_arm64" ) build_iOS arm64 $*;;
Pb2769c772015-11-06 14:44:55 +0100146 "osx-kernel" ) CAPSTONE_USE_SYS_DYN_MEM=yes CAPSTONE_HAS_OSXKERNEL=yes CAPSTONE_ARCHS=x86 CAPSTONE_SHARED=no CAPSTONE_BUILD_CORE_ONLY=yes build $*;;
pancake63414a42015-02-27 17:57:53 +0100147 * )
148 echo "Usage: $0 ["`grep '^ "' $0 | cut -d '"' -f 2 | tr "\\n" "|"`"]"
149 exit 1;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800150esac