Rewrite make.sh to make it shorter, faster and more portable

- Remove bashisms (use posix shell)
- Use only two space indentation (no mixed tabs)
- Honor $0 and autogenerate help message
- Correct use of exported and local vars
- Simplify spaguetti (-30LOC)
- Update copyright year
- Quote all vars to avoid command injection
- Cache uname value and allow to override it
- Honor user environment
- Support MAKE_JOBS (by default is 4)
This commit is contained in:
pancake 2015-02-27 17:57:53 +01:00
parent e0352d69d8
commit 63414a490e
2 changed files with 74 additions and 100 deletions

View File

@ -20,10 +20,10 @@ AR ?= ar
RANLIB ?= ranlib RANLIB ?= ranlib
STRIP ?= strip STRIP ?= strip
else else
CC = $(CROSS)gcc CC ?= $(CROSS)gcc
AR = $(CROSS)ar AR ?= $(CROSS)ar
RANLIB = $(CROSS)ranlib RANLIB ?= $(CROSS)ranlib
STRIP = $(CROSS)strip STRIP ?= $(CROSS)strip
endif endif
ifneq (,$(findstring yes,$(CAPSTONE_DIET))) ifneq (,$(findstring yes,$(CAPSTONE_DIET)))
@ -55,7 +55,7 @@ else
BLDIR = $(abspath $(BUILDDIR)) BLDIR = $(abspath $(BUILDDIR))
OBJDIR = $(BLDIR)/obj OBJDIR = $(BLDIR)/obj
endif endif
INCDIR = $(DESTDIR)$(PREFIX)/include INCDIR ?= $(DESTDIR)$(PREFIX)/include
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
@ -63,9 +63,9 @@ LIBDIRARCH ?= lib
# Uncomment the below line to installs x86_64 libs to lib64/ directory. # Uncomment the below line to installs x86_64 libs to lib64/ directory.
# Or better, pass 'LIBDIRARCH=lib64' to 'make install/uninstall' via 'make.sh'. # Or better, pass 'LIBDIRARCH=lib64' to 'make install/uninstall' via 'make.sh'.
#LIBDIRARCH ?= lib64 #LIBDIRARCH ?= lib64
LIBDIR = $(DESTDIR)$(PREFIX)/$(LIBDIRARCH) LIBDIR ?= $(DESTDIR)$(PREFIX)/$(LIBDIRARCH)
LIBDATADIR = $(LIBDIR) LIBDATADIR ?= $(LIBDIR)
ifeq ($(UNAME_S), FreeBSD) ifeq ($(UNAME_S), FreeBSD)
LIBDATADIR = $(DESTDIR)$(PREFIX)/libdata LIBDATADIR = $(DESTDIR)$(PREFIX)/libdata
endif endif

160
make.sh
View File

@ -1,114 +1,86 @@
#!/usr/bin/env bash #!/bin/sh
# Capstone Disassembly Engine # Capstone Disassembly Engine
# By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 # By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015
# Note: to cross-compile "nix32" on Linux, package gcc-multilib is required. # Note: to cross-compile "nix32" on Linux, package gcc-multilib is required.
MAKE_JOBS=$((${MAKE_JOBS}+0))
[ ${MAKE_JOBS} -lt 1 ] && \
MAKE_JOBS=4
# build iOS lib for all iDevices, or only specific device # build iOS lib for all iDevices, or only specific device
function build_iOS { build_iOS() {
${MAKE} clean ${MAKE} clean
IOS_SDK=`xcrun --sdk iphoneos --show-sdk-path` IOS_SDK=`xcrun --sdk iphoneos --show-sdk-path`
IOS_CC=`xcrun --sdk iphoneos -f clang` IOS_CC=`xcrun --sdk iphoneos -f clang`
IOS_CFLAGS="-Os -Wimplicit -isysroot $IOS_SDK" IOS_CFLAGS="-Os -Wimplicit -isysroot $IOS_SDK"
IOS_LDFLAGS="-isysroot $IOS_SDK" IOS_LDFLAGS="-isysroot $IOS_SDK"
if (( $# == 0 )); then if [ -z "$1" ]; then
# build for all iDevices # build for all iDevices
IOS_ARCHS="armv7 armv7s arm64" IOS_ARCHS="armv7 armv7s arm64"
else else
IOS_ARCHS="$1" IOS_ARCHS="$1"
fi fi
CC="$IOS_CC" CFLAGS="$IOS_CFLAGS" LDFLAGS="$IOS_LDFLAGS" LIBARCHS="$IOS_ARCHS" ${MAKE} CC="$IOS_CC" \
CFLAGS="$IOS_CFLAGS" \
LDFLAGS="$IOS_LDFLAGS" \
LIBARCHS="$IOS_ARCHS" \
${MAKE}
} }
function build { build() {
if [ $(uname -s) = Darwin ]; then [ "$UNAME" = Darwin ] && LIBARCHS="i386 x86_64"
export LIBARCHS="i386 x86_64" ${MAKE} clean
fi ${MAKE}
${MAKE} clean
if [ ${CC}x != x ]; then
${MAKE} CC=$CC
else
${MAKE}
fi
} }
function install { install() {
# Mac OSX needs to find the right directory for pkgconfig # Mac OSX needs to find the right directory for pkgconfig
if [ "$(uname)" == "Darwin" ]; then if [ "$UNAME" = Darwin ]; then
# we are going to install into /usr/local, so remove old installs under /usr # we are going to install into /usr/local, so remove old installs under /usr
rm -rf /usr/lib/libcapstone.* rm -rf /usr/lib/libcapstone.*
rm -rf /usr/include/capstone rm -rf /usr/include/capstone
# install into /usr/local # install into /usr/local
export PREFIX=/usr/local PREFIX=/usr/local
# find the directory automatically, so we can support both Macport & Brew # find the directory automatically, so we can support both Macport & Brew
PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)" PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
# set PKGCFGDIR only in non-Brew environment & pkg-config is available ${MAKE} install
if [ "$HOMEBREW_CAPSTONE" != "1" ] && [ ${PKGCFGDIR}x != x ]; then else # not OSX
if [ ${CC}x != x ]; then test -d /usr/lib64 && ${MAKE} LIBDIRARCH=lib64
${MAKE} CC=$CC PKGCFGDIR=$PKGCFGDIR install ${MAKE} install
else fi
${MAKE} PKGCFGDIR=$PKGCFGDIR install
fi
else
if [ ${CC}x != x ]; then
${MAKE} CC=$CC install
else
${MAKE} install
fi
fi
else # not OSX
if test -d /usr/lib64; then
if [ ${CC}x != x ]; then
${MAKE} LIBDIRARCH=lib64 CC=$CC install
else
${MAKE} LIBDIRARCH=lib64 install
fi
else
if [ ${CC}x != x ]; then
${MAKE} CC=$CC install
else
${MAKE} install
fi
fi
fi
} }
function uninstall { uninstall() {
# Mac OSX needs to find the right directory for pkgconfig # Mac OSX needs to find the right directory for pkgconfig
if [ "$(uname)" == "Darwin" ]; then if [ "$UNAME" = "Darwin" ]; then
# find the directory automatically, so we can support both Macport & Brew # find the directory automatically, so we can support both Macport & Brew
PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)" PKGCFGDIR="$(pkg-config --variable pc_path pkg-config | cut -d ':' -f 1)"
export PREFIX=/usr/local PREFIX=/usr/local
if [ ${PKGCFGDIR}x != x ]; then ${MAKE} uninstall
${MAKE} PKGCFGDIR=$PKGCFGDIR uninstall else # not OSX
else test -d /usr/lib64 && LIBDIRARCH=lib64
${MAKE} uninstall ${MAKE} uninstall
fi fi
else # not OSX
if test -d /usr/lib64; then
${MAKE} LIBDIRARCH=lib64 uninstall
else
${MAKE} uninstall
fi
fi
} }
MAKE=make if [ "$UNAME" = SunOS ]; then
if [ "$(uname)" == "SunOS" ]; then [ -z "${MAKE}" ] && MAKE=gmake
export MAKE=gmake INSTALL_BIN=ginstall
export INSTALL_BIN=ginstall CC=gcc
export CC=gcc
fi fi
if [[ "$(uname)" == *BSD* ]]; then if [ -n "`echo "$UNAME" | grep BSD`" ]; then
export MAKE=gmake MAKE=gmake
export PREFIX=/usr/local PREFIX=/usr/local
fi fi
[ -z "${UNAME}" ] && UNAME=$(uname)
[ -z "${MAKE}" ] && MAKE=make
[ -n "${MAKE_JOBS}" ] && MAKE="$MAKE -j${MAKE_JOBS}"
export MAKE CC INSTALL_BIN PREFIX PKGCFGDIR LIBDIRARCH LIBARCHS CFLAGS LDFLAGS
case "$1" in case "$1" in
"" ) build;; "" ) build;;
"default" ) build;; "default" ) build;;
@ -126,5 +98,7 @@ case "$1" in
"ios_armv7" ) build_iOS armv7;; "ios_armv7" ) build_iOS armv7;;
"ios_armv7s" ) build_iOS armv7s;; "ios_armv7s" ) build_iOS armv7s;;
"ios_arm64" ) build_iOS arm64;; "ios_arm64" ) build_iOS arm64;;
* ) 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;; * )
echo "Usage: $0 ["`grep '^ "' $0 | cut -d '"' -f 2 | tr "\\n" "|"`"]"
exit 1;;
esac esac