3 # setup_toolchain.sh: Sets toolchain environment variables used by other scripts.
5 # Fail-fast if anything in the script fails.
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"
14 if [ $(type -t exportVar) != 'function' ]; then
15 echo "ERROR: The exportVar function is expected to be defined"
19 if [ $(type -t absPath) != 'function' ]; then
20 echo "ERROR: The absPath function is expected to be defined"
24 if [ -z "$SCRIPT_DIR" ]; then
25 echo "ERROR: The SCRIPT_DIR variable is expected to be defined"
29 function default_toolchain() {
30 NDK_REV=${NDK_REV-10c}
31 ANDROID_ARCH=${ANDROID_ARCH-arm}
33 if [[ $ANDROID_ARCH == *64* ]]; then
34 API_LEVEL=21 # Official Android 5.0 (Lollipop) system images
36 API_LEVEL=14 # Official Android 4.0 system images
39 TOOLCHAIN_DIR=${SCRIPT_DIR}/../toolchains
40 if [ $(uname) == "Darwin" ]; then
41 verbose "Using Mac toolchain."
42 TOOLCHAIN_TYPE=ndk-r$NDK_REV-$ANDROID_ARCH-darwin_v$API_LEVEL
44 verbose "Using Linux toolchain."
45 TOOLCHAIN_TYPE=ndk-r$NDK_REV-$ANDROID_ARCH-linux_v$API_LEVEL
47 exportVar ANDROID_TOOLCHAIN "${TOOLCHAIN_DIR}/${TOOLCHAIN_TYPE}/bin"
49 # Hack for NDK_REV == 10c to ensure that clang is present as it was
50 # added to the tarball after it was initially distributed.
51 if [ ! -x ${ANDROID_TOOLCHAIN}/clang ]; then
52 rm -rf ${TOOLCHAIN_DIR}/${TOOLCHAIN_TYPE}
55 # if the toolchain doesn't exist on your machine then we need to fetch it
56 if [ ! -d "$ANDROID_TOOLCHAIN" ]; then
57 mkdir -p $TOOLCHAIN_DIR
58 # enter the toolchain directory then download, unpack, and remove the tarball
60 TARBALL=ndk-r$NDK_REV-v$API_LEVEL.tgz
62 ${SCRIPT_DIR}/download_toolchains.py \
63 http://chromium-skia-gm.commondatastorage.googleapis.com/android-toolchains/$TARBALL \
64 $TOOLCHAIN_DIR/$TARBALL
65 tar -xzf $TARBALL $TOOLCHAIN_TYPE
70 verbose "Targeting NDK API $API_LEVEL (NDK Revision $NDK_REV)"
73 #check to see if the toolchain has been defined and if not setup the default toolchain
74 if [ -z "$ANDROID_TOOLCHAIN" ]; then
76 if [ ! -d "$ANDROID_TOOLCHAIN" ]; then
77 echo "ERROR: unable to download/setup the required toolchain (${ANDROID_TOOLCHAIN})"
82 GCC=$(command ls $ANDROID_TOOLCHAIN/*-gcc | head -n1)
83 if [ -z "$GCC" ]; then
84 echo "ERROR: Could not find Android cross-compiler in: $ANDROID_TOOLCHAIN"
88 # Remove the '-gcc' at the end to get the full toolchain prefix
89 ANDROID_TOOLCHAIN_PREFIX=${GCC%%-gcc}
91 CCACHE=${ANDROID_MAKE_CCACHE-$(which ccache || true)}
93 if [ -z $USE_CLANG ]; then
94 exportVar CC "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-gcc"
95 exportVar CXX "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-g++"
96 exportVar LINK "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-gcc"
98 # temporarily disable ccache as it is generating errors
100 exportVar CC "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-clang"
101 exportVar CXX "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-clang++"
102 exportVar LINK "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-clang"
105 exportVar AR "$ANDROID_TOOLCHAIN_PREFIX-ar"
106 exportVar RANLIB "$ANDROID_TOOLCHAIN_PREFIX-ranlib"
107 exportVar OBJCOPY "$ANDROID_TOOLCHAIN_PREFIX-objcopy"
108 exportVar STRIP "$ANDROID_TOOLCHAIN_PREFIX-strip"
110 # Create symlinks for nm & readelf and add them to the path so that the ninja
111 # build uses them instead of attempting to use the one on the system.
112 # This is required to build using ninja on a Mac.
113 if [ $(uname) == "Darwin" ]; then
114 ln -sf $ANDROID_TOOLCHAIN_PREFIX-nm $ANDROID_TOOLCHAIN/nm
115 ln -sf $ANDROID_TOOLCHAIN_PREFIX-readelf $ANDROID_TOOLCHAIN/readelf
116 ln -sf $ANDROID_TOOLCHAIN_PREFIX-as $ANDROID_TOOLCHAIN/as
119 exportVar PATH $ANDROID_TOOLCHAIN:$PATH