blob: 0872d5e522befea88676da631593daaa419e4d30 [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
12 SDK=`xcrun --sdk iphoneos --show-sdk-path`
13 GCC_BIN=`xcrun --sdk iphoneos -f gcc`
14 GCC_BASE="$GCC_BIN -Os -Wimplicit -isysroot $SDK"
15 if (( $# == 0 )); then
16 # build for all iDevices
17 GCC="$GCC_BASE -arch armv7 -arch armv7s -arch arm64"
18 else
19 GCC="$GCC_BASE -arch $1"
20 fi
21 ${MAKE} CC="$GCC"
22}
23
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080024function build {
Nguyen Anh Quynhaf873932014-01-28 11:14:55 +080025 ${MAKE} clean
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080026
27 if [ ${CC}x != x ]; then
28 ${MAKE} CC=$CC
29 else
30 ${MAKE}
31 fi
32}
33
34function install {
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080035 # Mac OSX needs to find the right directory for pkgconfig
36 if [ "$(uname)" == "Darwin" ]; then
37 # find the directory automatically, so we can support both Macport & Brew
38 PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
Nguyen Anh Quynhac673ae2014-04-14 23:58:28 +080039 # set PKGCFGDIR only in non-Brew environment & pkg-config is available
40 if [ "$HOMEBREW_CAPSTONE" != "1" ] && [ ${PKGCFGDIR}x != x ]; then
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080041 if [ ${CC}x != x ]; then
42 ${MAKE} CC=$CC PKGCFGDIR=$PKGCFGDIR install
43 else
44 ${MAKE} PKGCFGDIR=$PKGCFGDIR install
45 fi
46 else
47 if [ ${CC}x != x ]; then
48 ${MAKE} CC=$CC install
49 else
50 ${MAKE} install
51 fi
52 fi
Nguyen Anh Quynh360bf1e2014-04-14 13:36:46 +080053 else # not OSX
54 if test -d /usr/lib64; then
55 if [ ${CC}x != x ]; then
56 ${MAKE} LIBDIRARCH=lib64 CC=$CC install
57 else
58 ${MAKE} LIBDIRARCH=lib64 install
59 fi
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080060 else
Nguyen Anh Quynh360bf1e2014-04-14 13:36:46 +080061 if [ ${CC}x != x ]; then
62 ${MAKE} CC=$CC install
63 else
64 ${MAKE} install
65 fi
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080066 fi
67 fi
68}
69
70function uninstall {
71 # Mac OSX needs to find the right directory for pkgconfig
72 if [ "$(uname)" == "Darwin" ]; then
73 # find the directory automatically, so we can support both Macport & Brew
74 PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
75 if [ ${PKGCFGDIR}x != x ]; then
76 ${MAKE} PKGCFGDIR=$PKGCFGDIR uninstall
77 else
78 ${MAKE} uninstall
79 fi
Nguyen Anh Quynh360bf1e2014-04-14 13:36:46 +080080 else # not OSX
81 if test -d /usr/lib64; then
82 ${MAKE} LIBDIRARCH=lib64 uninstall
83 else
84 ${MAKE} uninstall
85 fi
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080086 fi
87}
88
Nguyen Anh Quynhaf873932014-01-28 11:14:55 +080089MAKE=make
Nguyen Anh Quynhd3b30712014-01-17 21:25:18 +080090if [ "$(uname)" == "SunOS" ]; then
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080091 export MAKE=gmake
92 export INSTALL_BIN=ginstall
93 export CC=gcc
Nguyen Anh Quynhd3b30712014-01-17 21:25:18 +080094fi
95
Nguyen Anh Quynhf63db272014-01-17 22:12:35 +080096if [[ "$(uname)" == *BSD* ]]; then
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +080097 export MAKE=gmake
98 export PREFIX=/usr/local
Nguyen Anh Quynhf63db272014-01-17 22:12:35 +080099fi
100
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800101case "$1" in
102 "" ) build;;
103 "default" ) build;;
104 "install" ) install;;
Nguyen Anh Quynhaf0e9372014-04-12 23:10:46 +0800105 "uninstall" ) uninstall;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800106 "nix32" ) CFLAGS=-m32 LDFLAGS=-m32 build;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800107 "cross-win32" ) CROSS=i686-w64-mingw32- build;;
108 "cross-win64" ) CROSS=x86_64-w64-mingw32- build;;
109 "cygwin-mingw32" ) CROSS=i686-pc-mingw32- build;;
110 "cygwin-mingw64" ) CROSS=x86_64-w64-mingw32- build;;
Nguyen Anh Quynhf08ed972014-04-07 22:13:56 +0800111 "cross-android" ) CROSS=arm-linux-androideabi- build;;
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +0800112 "clang" ) CC=clang build;;
113 "gcc" ) CC=gcc build;;
Nguyen Anh Quynh50ebb682014-03-31 21:15:29 +0800114 "ios" ) build_iOS;;
115 "ios_armv7" ) build_iOS armv7;;
116 "ios_armv7s" ) build_iOS armv7s;;
117 "ios_arm64" ) build_iOS arm64;;
Nguyen Anh Quynhf08ed972014-04-07 22:13:56 +0800118 * ) 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 +0800119esac