1#!/bin/bash 2# 3# setup_toolchain.sh: Sets toolchain environment variables used by other scripts. 4 5# Fail-fast if anything in the script fails. 6set -e 7 8# check that the preconditions for this script are met 9if [ $(type -t verbose) != 'function' ]; then 10 echo "ERROR: The verbose function is expected to be defined" 11 return 1 12fi 13 14if [ $(type -t exportVar) != 'function' ]; then 15 echo "ERROR: The exportVar function is expected to be defined" 16 return 1 17fi 18 19if [ $(type -t absPath) != 'function' ]; then 20 echo "ERROR: The absPath function is expected to be defined" 21 return 1 22fi 23 24if [ -z "$SCRIPT_DIR" ]; then 25 echo "ERROR: The SCRIPT_DIR variable is expected to be defined" 26 return 1 27fi 28 29function default_toolchain() { 30 API_LEVEL=14 31 NDK_REV=${NDK_REV-8e} 32 ANDROID_ARCH=${ANDROID_ARCH-arm} 33 34 TOOLCHAIN_DIR=${SCRIPT_DIR}/../toolchains 35 if [ $(uname) == "Darwin" ]; then 36 verbose "Using Mac toolchain." 37 TOOLCHAIN_TYPE=ndk-r$NDK_REV-$ANDROID_ARCH-mac_v$API_LEVEL 38 else 39 verbose "Using Linux toolchain." 40 TOOLCHAIN_TYPE=ndk-r$NDK_REV-$ANDROID_ARCH-linux_v$API_LEVEL 41 fi 42 exportVar ANDROID_TOOLCHAIN "${TOOLCHAIN_DIR}/${TOOLCHAIN_TYPE}/bin" 43 44 # if the toolchain doesn't exist on your machine then we need to fetch it 45 if [ ! -d "$ANDROID_TOOLCHAIN" ]; then 46 mkdir -p $TOOLCHAIN_DIR 47 # enter the toolchain directory then download, unpack, and remove the tarball 48 pushd $TOOLCHAIN_DIR 49 TARBALL=ndk-r$NDK_REV-v$API_LEVEL.tgz 50 51 ${SCRIPT_DIR}/download_toolchains.py \ 52 http://chromium-skia-gm.commondatastorage.googleapis.com/android-toolchains/$TARBALL \ 53 $TOOLCHAIN_DIR/$TARBALL 54 tar -xzf $TARBALL $TOOLCHAIN_TYPE 55 rm $TARBALL 56 popd 57 fi 58 59 verbose "Targeting NDK API $API_LEVEL for use on Android 4.0 (NDK Revision $NDK_REV) and above" 60} 61 62#check to see if the toolchain has been defined and if not setup the default toolchain 63if [ -z "$ANDROID_TOOLCHAIN" ]; then 64 default_toolchain 65 if [ ! -d "$ANDROID_TOOLCHAIN" ]; then 66 echo "ERROR: unable to download/setup the required toolchain (${ANDROID_TOOLCHAIN})" 67 return 1; 68 fi 69fi 70 71GCC=$(command ls $ANDROID_TOOLCHAIN/*-gcc | head -n1) 72if [ -z "$GCC" ]; then 73 echo "ERROR: Could not find Android cross-compiler in: $ANDROID_TOOLCHAIN" 74 return 1 75fi 76 77# Remove the '-gcc' at the end to get the full toolchain prefix 78ANDROID_TOOLCHAIN_PREFIX=${GCC%%-gcc} 79 80CCACHE=${ANDROID_MAKE_CCACHE-$(which ccache || true)} 81 82exportVar CC "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-gcc" 83exportVar CXX "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-g++" 84exportVar LINK "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-gcc" 85 86exportVar AR "$ANDROID_TOOLCHAIN_PREFIX-ar" 87exportVar RANLIB "$ANDROID_TOOLCHAIN_PREFIX-ranlib" 88exportVar OBJCOPY "$ANDROID_TOOLCHAIN_PREFIX-objcopy" 89exportVar STRIP "$ANDROID_TOOLCHAIN_PREFIX-strip" 90 91# Create symlinks for nm & readelf and add them to the path so that the ninja 92# build uses them instead of attempting to use the one on the system. 93# This is required to build using ninja on a Mac. 94ln -sf $ANDROID_TOOLCHAIN_PREFIX-nm $ANDROID_TOOLCHAIN/nm 95ln -sf $ANDROID_TOOLCHAIN_PREFIX-readelf $ANDROID_TOOLCHAIN/readelf 96exportVar PATH $ANDROID_TOOLCHAIN:$PATH 97