Anestis Bechtsoudis | cfc39fb | 2015-08-06 10:31:36 +0300 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # honggfuzz libunwind build help script |
| 4 | # ----------------------------------------- |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | if [ -z "$NDK" ]; then |
| 19 | # Search in $PATH |
| 20 | if [[ $(which ndk-build) != "" ]]; then |
| 21 | $NDK=$(dirname $(which ndk-build)) |
| 22 | else |
| 23 | echo "[-] Could not detect Android NDK dir" |
| 24 | exit 1 |
| 25 | fi |
| 26 | fi |
| 27 | |
| 28 | if [ $# -ne 2 ]; then |
| 29 | echo "[-] Invalid arguments" |
| 30 | echo "[!] $0 <LIBUNWIND_DIR> <ARCH>" |
| 31 | echo " ARCH: arm arm64 x86 x86_64" |
| 32 | exit 1 |
| 33 | fi |
| 34 | |
| 35 | readonly LIBUNWIND_DIR=$1 |
| 36 | |
| 37 | # Fetch if not already there |
| 38 | if [ ! -d $LIBUNWIND_DIR ]; then |
| 39 | echo "[!] libunwind not found. Fetching a fresh copy" |
| 40 | git clone git://git.sv.gnu.org/libunwind.git $LIBUNWIND_DIR |
| 41 | fi |
| 42 | |
| 43 | case "$2" in |
| 44 | arm|arm64|x86|x86_64) |
| 45 | readonly ARCH=$2 |
| 46 | if [ ! -d $LIBUNWIND_DIR/$ARCH ] ; then mkdir -p $LIBUNWIND_DIR/$ARCH; fi |
| 47 | ;; |
| 48 | *) |
| 49 | echo "[-] Invalid architecture" |
| 50 | exit 1 |
| 51 | ;; |
| 52 | esac |
| 53 | |
| 54 | # Change workdir to simplify args |
| 55 | cd $LIBUNWIND_DIR |
| 56 | |
| 57 | # Prepare toolchain |
| 58 | case "$ARCH" in |
| 59 | arm) |
| 60 | TOOLCHAIN=arm-linux-androideabi |
| 61 | TOOLCHAIN_S=arm-linux-androideabi-4.9 |
| 62 | ;; |
| 63 | arm64) |
| 64 | TOOLCHAIN=aarch64-linux-android |
| 65 | TOOLCHAIN_S=aarch64-linux-android-4.9 |
| 66 | ;; |
| 67 | x86) |
| 68 | TOOLCHAIN=i686-linux-android |
| 69 | TOOLCHAIN_S=x86-4.9 |
| 70 | ;; |
| 71 | x86_64) |
| 72 | TOOLCHAIN=x86_64-linux-android |
| 73 | TOOLCHAIN_S=x86_64-4.9 |
| 74 | ;; |
| 75 | esac |
| 76 | |
| 77 | # Apply patches required for Android |
| 78 | # TODO: Automate global patching when all archs have been tested |
| 79 | if [ "$ARCH" == "arm64" ]; then |
| 80 | # Missing libc functionality |
| 81 | patch -N --dry-run --silent include/libunwind-aarch64.h < ../patches/aarch64-libunwind.patch &>/dev/null |
| 82 | if [ $? -eq 0 ]; then |
| 83 | patch include/libunwind-aarch64.h < ../patches/aarch64-libunwind.patch |
| 84 | if [ $? -ne 0 ]; then |
| 85 | echo "[-] aarch64-libunwind patch failed" |
| 86 | exit 1 |
| 87 | fi |
| 88 | fi |
| 89 | fi |
| 90 | |
| 91 | if [ "$ARCH" == "x86" ]; then |
| 92 | # Missing syscalls |
| 93 | patch -N --dry-run --silent src/x86/Gos-linux.c < ../patches/x86-libunwind.patch &>/dev/null |
| 94 | if [ $? -eq 0 ]; then |
| 95 | patch src/x86/Gos-linux.c < ../patches/x86-libunwind.patch |
| 96 | if [ $? -ne 0 ]; then |
| 97 | echo "[-] x86-libunwind patch failed" |
| 98 | exit 1 |
| 99 | fi |
| 100 | fi |
| 101 | fi |
| 102 | |
| 103 | # Support both Linux & Darwin |
| 104 | HOST_OS=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 105 | HOST_ARCH=$(uname -m) |
| 106 | |
| 107 | SYSROOT="$NDK/platforms/android-21/arch-$ARCH" |
| 108 | export CC="$NDK/toolchains/$TOOLCHAIN_S/prebuilt/$HOST_OS-$HOST_ARCH/bin/$TOOLCHAIN-gcc --sysroot=$SYSROOT" |
| 109 | export CXX="$NDK/toolchains/$TOOLCHAIN_S/prebuilt/$HOST_OS-$HOST_ARCH/bin/$TOOLCHAIN-g++ --sysroot=$SYSROOT" |
| 110 | export PATH="$NDK/toolchains/$TOOLCHAIN_S/prebuilt/$HOST_OS-$HOST_ARCH/bin":$PATH |
| 111 | |
| 112 | if [ ! -f configure ]; then |
| 113 | autoreconf -i |
| 114 | if [ $? -ne 0 ]; then |
| 115 | echo "[-] autoreconf failed" |
| 116 | exit 1 |
| 117 | fi |
| 118 | # Patch configure |
| 119 | sed -i -e 's/-lgcc_s/-lgcc/g' configure |
| 120 | else |
| 121 | make clean |
| 122 | if [ $? -ne 0 ]; then |
| 123 | echo "[-] Old build detected, although clean failed. Consider manual clean-up." |
| 124 | exit 1 |
| 125 | fi |
| 126 | fi |
| 127 | |
| 128 | ./configure --host=$TOOLCHAIN --disable-coredump |
| 129 | if [ $? -ne 0 ]; then |
| 130 | echo "[-] configure failed" |
| 131 | exit 1 |
| 132 | fi |
| 133 | |
| 134 | make CFLAGS="-static" LDFLAGS="-static" |
| 135 | if [ $? -ne 0 ]; then |
| 136 | echo "[-] Compilation failed" |
| 137 | cd - |
| 138 | exit 1 |
| 139 | else |
| 140 | echo "[*] '$ARCH' libunwind available at '$LIBUNWIND_DIR/$ARCH'" |
| 141 | cp src/.libs/*.a $ARCH |
| 142 | cd - |
| 143 | fi |