blob: d4fa43f0175193ce0ebf1392193d3d24af0dfd21 [file] [log] [blame]
Nguyen Anh Quynhc3fd5272014-03-31 16:05:25 +08001#!/usr/bin/env bash
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +08002
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08003# Capstone Disassembly Engine
4# By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +08005
6# Note: to cross-compile "nix32" on Linux, package gcc-multilib is required.
7
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +08008
9# build iOS lib for all iDevices, or only specific device
10function build_iOS {
11 ${MAKE} clean
Ole André Vadla Ravnås6583f062014-07-04 23:09:19 +020012 IOS_SDK=`xcrun --sdk iphoneos --show-sdk-path`
13 IOS_CC=`xcrun --sdk iphoneos -f clang`
14 IOS_CFLAGS="-Os -Wimplicit -isysroot $IOS_SDK"
15 IOS_LDFLAGS="-isysroot $IOS_SDK"
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +080016 if (( $# == 0 )); then
17 # build for all iDevices
Ole André Vadla Ravnås6583f062014-07-04 23:09:19 +020018 IOS_ARCHS="armv7 armv7s arm64"
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +080019 else
Ole André Vadla Ravnås6583f062014-07-04 23:09:19 +020020 IOS_ARCHS="$1"
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +080021 fi
Ole André Vadla Ravnås174f9bb2014-07-05 20:29:50 +020022 CC="$IOS_CC" CFLAGS="$IOS_CFLAGS" LDFLAGS="$IOS_LDFLAGS" LIBARCHS="$IOS_ARCHS" ${MAKE}
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +080023}
24
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080025function build {
Ole André Vadla Ravnås39edca12014-07-05 20:31:30 +020026 if [ $(uname -s) = Darwin ]; then
27 export LIBARCHS="i386 x86_64"
28 fi
29
Nguyen Anh Quynhaf873932014-01-28 11:14:55 +080030 ${MAKE} clean
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080031
32 if [ ${CC}x != x ]; then
33 ${MAKE} CC=$CC
34 else
35 ${MAKE}
36 fi
37}
38
39function install {
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080040 # Mac OSX needs to find the right directory for pkgconfig
41 if [ "$(uname)" == "Darwin" ]; then
Nguyen Anh Quynhbcc6e532014-07-07 11:42:25 +080042 # we are going to install into /usr/local, so remove old installs under /usr
43 rm -rf /usr/lib/libcapstone.*
44 rm -rf /usr/include/capstone
45 # install into /usr/local
46 export PREFIX=/usr/local
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080047 # find the directory automatically, so we can support both Macport & Brew
48 PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
Nguyen Anh Quynhac673ae2014-04-14 23:58:28 +080049 # set PKGCFGDIR only in non-Brew environment & pkg-config is available
50 if [ "$HOMEBREW_CAPSTONE" != "1" ] && [ ${PKGCFGDIR}x != x ]; then
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080051 if [ ${CC}x != x ]; then
52 ${MAKE} CC=$CC PKGCFGDIR=$PKGCFGDIR install
53 else
54 ${MAKE} PKGCFGDIR=$PKGCFGDIR install
55 fi
56 else
57 if [ ${CC}x != x ]; then
58 ${MAKE} CC=$CC install
59 else
60 ${MAKE} install
61 fi
62 fi
Nguyen Anh Quynh360bf1e2014-04-14 13:36:46 +080063 else # not OSX
64 if test -d /usr/lib64; then
65 if [ ${CC}x != x ]; then
66 ${MAKE} LIBDIRARCH=lib64 CC=$CC install
67 else
68 ${MAKE} LIBDIRARCH=lib64 install
69 fi
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080070 else
Nguyen Anh Quynh360bf1e2014-04-14 13:36:46 +080071 if [ ${CC}x != x ]; then
72 ${MAKE} CC=$CC install
73 else
74 ${MAKE} install
75 fi
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080076 fi
77 fi
78}
79
80function uninstall {
81 # Mac OSX needs to find the right directory for pkgconfig
82 if [ "$(uname)" == "Darwin" ]; then
83 # find the directory automatically, so we can support both Macport & Brew
84 PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
Nguyen Anh Quynhcf862702014-07-22 21:19:02 +080085 export PREFIX=/usr/local
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080086 if [ ${PKGCFGDIR}x != x ]; then
87 ${MAKE} PKGCFGDIR=$PKGCFGDIR uninstall
88 else
89 ${MAKE} uninstall
90 fi
Nguyen Anh Quynh360bf1e2014-04-14 13:36:46 +080091 else # not OSX
92 if test -d /usr/lib64; then
93 ${MAKE} LIBDIRARCH=lib64 uninstall
94 else
95 ${MAKE} uninstall
96 fi
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080097 fi
98}
99
Nguyen Anh Quynhaf873932014-01-28 11:14:55 +0800100MAKE=make
Nguyen Anh Quynhd3b30712014-01-17 21:25:18 +0800101if [ "$(uname)" == "SunOS" ]; then
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +0800102 export MAKE=gmake
103 export INSTALL_BIN=ginstall
104 export CC=gcc
Nguyen Anh Quynhd3b30712014-01-17 21:25:18 +0800105fi
106
Nguyen Anh Quynhf63db272014-01-17 22:12:35 +0800107if [[ "$(uname)" == *BSD* ]]; then
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +0800108 export MAKE=gmake
109 export PREFIX=/usr/local
Nguyen Anh Quynhf63db272014-01-17 22:12:35 +0800110fi
111
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800112case "$1" in
113 "" ) build;;
114 "default" ) build;;
115 "install" ) install;;
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +0800116 "uninstall" ) uninstall;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800117 "nix32" ) CFLAGS=-m32 LDFLAGS=-m32 build;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800118 "cross-win32" ) CROSS=i686-w64-mingw32- build;;
119 "cross-win64" ) CROSS=x86_64-w64-mingw32- build;;
120 "cygwin-mingw32" ) CROSS=i686-pc-mingw32- build;;
121 "cygwin-mingw64" ) CROSS=x86_64-w64-mingw32- build;;
Nguyen Anh Quynhf08ed972014-04-07 22:13:56 +0800122 "cross-android" ) CROSS=arm-linux-androideabi- build;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800123 "clang" ) CC=clang build;;
124 "gcc" ) CC=gcc build;;
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +0800125 "ios" ) build_iOS;;
126 "ios_armv7" ) build_iOS armv7;;
127 "ios_armv7s" ) build_iOS armv7s;;
128 "ios_arm64" ) build_iOS arm64;;
Nguyen Anh Quynhf08ed972014-04-07 22:13:56 +0800129 * ) echo "Usage: make.sh [nix32|cross-win32|cross-win64|cygwin-mingw32|cygwin-mingw64|ios|ios_armv7|ios_armv7s|ios_arm64|cross-android|clang|gcc|install|uninstall]"; exit 1;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800130esac