Home | History | Annotate | Download | only in utils
      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.
      6 set -e
      7 
      8 # check that the preconditions for this script are met
      9 if [ $(type -t verbose) != 'function' ]; then
     10   echo "ERROR: The verbose function is expected to be defined"
     11   return 1
     12 fi
     13 
     14 if [ $(type -t exportVar) != 'function' ]; then
     15   echo "ERROR: The exportVar function is expected to be defined"
     16   return 1
     17 fi
     18 
     19 if [ $(type -t absPath) != 'function' ]; then
     20   echo "ERROR: The absPath function is expected to be defined"
     21   return 1
     22 fi
     23 
     24 if [ -z "$SCRIPT_DIR" ]; then
     25   echo "ERROR: The SCRIPT_DIR variable is expected to be defined"
     26   return 1
     27 fi
     28 
     29 function 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
     63 if [ -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
     69 fi
     70 
     71 GCC=$(command ls $ANDROID_TOOLCHAIN/*-gcc | head -n1)
     72 if [ -z "$GCC" ]; then
     73   echo "ERROR: Could not find Android cross-compiler in: $ANDROID_TOOLCHAIN"
     74   return 1
     75 fi
     76 
     77 # Remove the '-gcc' at the end to get the full toolchain prefix
     78 ANDROID_TOOLCHAIN_PREFIX=${GCC%%-gcc}
     79 
     80 CCACHE=${ANDROID_MAKE_CCACHE-$(which ccache || true)}
     81 
     82 exportVar CC "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-gcc"
     83 exportVar CXX "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-g++"
     84 exportVar LINK "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-gcc"
     85 
     86 exportVar AR "$ANDROID_TOOLCHAIN_PREFIX-ar"
     87 exportVar RANLIB "$ANDROID_TOOLCHAIN_PREFIX-ranlib"
     88 exportVar OBJCOPY "$ANDROID_TOOLCHAIN_PREFIX-objcopy"
     89 exportVar 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.
     94 ln -sf $ANDROID_TOOLCHAIN_PREFIX-nm $ANDROID_TOOLCHAIN/nm
     95 ln -sf $ANDROID_TOOLCHAIN_PREFIX-readelf $ANDROID_TOOLCHAIN/readelf
     96 exportVar PATH $ANDROID_TOOLCHAIN:$PATH
     97