Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / platform_tools / android / bin / utils / setup_toolchain.sh
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   NDK_REV=${NDK_REV-10c}
31   ANDROID_ARCH=${ANDROID_ARCH-arm}
32   
33   if [[ $ANDROID_ARCH == *64* ]]; then
34     API_LEVEL=21 # Official Android 5.0 (Lollipop) system images
35   else
36     API_LEVEL=14 # Official Android 4.0 system images
37   fi
38
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
43   else
44     verbose "Using Linux toolchain."
45     TOOLCHAIN_TYPE=ndk-r$NDK_REV-$ANDROID_ARCH-linux_v$API_LEVEL
46   fi
47   exportVar ANDROID_TOOLCHAIN "${TOOLCHAIN_DIR}/${TOOLCHAIN_TYPE}/bin"
48
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}
53   fi
54
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
59     pushd $TOOLCHAIN_DIR
60     TARBALL=ndk-r$NDK_REV-v$API_LEVEL.tgz
61
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
66     rm $TARBALL
67     popd
68   fi
69
70   verbose "Targeting NDK API $API_LEVEL (NDK Revision $NDK_REV)"
71 }
72
73 #check to see if the toolchain has been defined and if not setup the default toolchain
74 if [ -z "$ANDROID_TOOLCHAIN" ]; then
75   default_toolchain
76   if [ ! -d "$ANDROID_TOOLCHAIN" ]; then
77     echo "ERROR: unable to download/setup the required toolchain (${ANDROID_TOOLCHAIN})"
78     return 1;
79   fi
80 fi
81
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"
85   return 1
86 fi
87
88 # Remove the '-gcc' at the end to get the full toolchain prefix
89 ANDROID_TOOLCHAIN_PREFIX=${GCC%%-gcc}
90
91 CCACHE=${ANDROID_MAKE_CCACHE-$(which ccache || true)}
92
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"
97 else
98   # temporarily disable ccache as it is generating errors
99   CCACHE=""
100   exportVar CC "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-clang"
101   exportVar CXX "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-clang++"
102   exportVar LINK "$CCACHE $ANDROID_TOOLCHAIN_PREFIX-clang"
103 fi
104
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"
109
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
117 fi
118
119 exportVar PATH $ANDROID_TOOLCHAIN:$PATH