Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2013 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 #  This shell script is used to rebuild the on-device llvm and mclinker
     18 #  binaries for the Android NDK.
     19 #
     20 
     21 # include common function and variable definitions
     22 . `dirname $0`/prebuilt-common.sh
     23 
     24 PROGRAM_PARAMETERS="<src-dir> <ndk-dir>"
     25 
     26 PROGRAM_DESCRIPTION=\
     27 "Rebuild LLVM-related on-device binaries for the Android NDK.
     28 
     29 Where <src-dir> is the location of toolchain sources, <ndk-dir> is
     30 the top-level NDK installation path."
     31 
     32 RELEASE=`date +%Y%m%d`
     33 ABIS="$PREBUILT_ABIS"
     34 BUILDTOOLS=$ANDROID_NDK_ROOT/build/tools
     35 
     36 BUILD_OUT=/tmp/ndk-$USER/build/toolchain
     37 OPTION_BUILD_OUT=
     38 register_var_option "--build-out=<path>" OPTION_BUILD_OUT "Set temporary build directory"
     39 
     40 OUT_DIR="$BUILD_OUT/../prefix"
     41 OPTION_OUT_DIR=
     42 register_var_option "--out-dir=<path>" OPTION_OUT_DIR "On-device toolchain will be put at <path>"
     43 
     44 OPTION_ABIS=
     45 register_var_option "--abis=<armeabi,armeabi-v7a,x86,mips,armeabi-v7a-hard>" OPTION_ABIS "Default: $ABIS"
     46 
     47 OPTION_GCC_VERSION=
     48 register_var_option "--gcc-version=<version>" OPTION_GCC_VERSION "Specify GCC toolchain version [Default: $DEFAULT_GCC_VERSION]"
     49 
     50 STL=stlport
     51 register_var_option "--stl=<name>" STL "Specify C++ STL"
     52 
     53 SHARED=
     54 do_shared_option () { SHARED=yes; }
     55 register_option "--shared" do_shared_option "Build libLLVM*.so shared by on-device llvm toolchain, and link lib*stl_shared.so"
     56 
     57 register_jobs_option
     58 
     59 extract_parameters "$@"
     60 
     61 prepare_canadian_toolchain /tmp/ndk-$USER/build
     62 
     63 fix_option ABIS "$OPTION_ABIS" "on-device architecture"
     64 ABIS=($commas_to_spaces $ABIS)
     65 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
     66 setup_default_log_file $BUILD_OUT/config.log
     67 fix_option OUT_DIR "$OPTION_OUT_DIR" "On-device toolchain will be put at <path>"
     68 TOOLCHAIN_BUILD_PREFIX="$OUT_DIR"
     69 
     70 set_parameters ()
     71 {
     72     SRC_DIR="$1"
     73     NDK_DIR="$2"
     74     TOOLCHAIN="llvm-$DEFAULT_LLVM_VERSION"
     75 
     76     # Check source directory
     77     #
     78     if [ -z "$SRC_DIR" ] ; then
     79         echo "ERROR: Missing source directory parameter. See --help for details."
     80         exit 1
     81     fi
     82 
     83     if [ ! -d "$SRC_DIR/$TOOLCHAIN/llvm" ] ; then
     84         echo "ERROR: Source directory does not contain llvm sources: $SRC_DIR/$TOOLCHAIN/llvm"
     85         exit 1
     86     fi
     87 
     88     SRC_DIR=`cd $SRC_DIR; pwd`
     89     log "Using source directory: $SRC_DIR"
     90 
     91     # Check NDK installation directory
     92     #
     93     if [ -z "$NDK_DIR" ] ; then
     94         echo "ERROR: Missing NDK directory parameter. See --help for details."
     95         exit 1
     96     fi
     97 
     98     if [ ! -d "$NDK_DIR" ] ; then
     99         mkdir -p $NDK_DIR
    100         if [ $? != 0 ] ; then
    101             echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
    102             exit 1
    103         fi
    104     fi
    105     NDK_DIR=`cd $NDK_DIR; pwd`
    106     log "Using NDK directory: $NDK_DIR"
    107 
    108     # Check toolchain name
    109     #
    110     if [ -z "$TOOLCHAIN" ] ; then
    111         echo "ERROR: Missing toolchain name parameter. See --help for details."
    112         exit 1
    113     fi
    114 }
    115 
    116 set_parameters $PARAMETERS
    117 
    118 prepare_target_build
    119 prepare_abi_configure_build
    120 set_toolchain_ndk $NDK_DIR $TOOLCHAIN
    121 
    122 rm -rf $BUILD_OUT
    123 mkdir -p $BUILD_OUT
    124 
    125 MAKE_FLAGS=
    126 if [ "$VERBOSE" = "yes" ]; then
    127     MAKE_FLAGS="VERBOSE=1"
    128 fi
    129 
    130 dump "Copy     : mclinker source"
    131 MCLINKER_SRC_DIR=$BUILD_OUT/mclinker
    132 mkdir -p $MCLINKER_SRC_DIR
    133 fail_panic "Couldn't create mclinker source directory: $MCLINKER_SRC_DIR"
    134 
    135 run copy_directory "$SRC_DIR/mclinker" "$MCLINKER_SRC_DIR"
    136 fail_panic "Couldn't copy mclinker source: $MCLINKER_SRC_DIR"
    137 
    138 cd $MCLINKER_SRC_DIR && run ./autogen.sh
    139 fail_panic "Couldn't run autogen.sh in $MCLINKER_SRC_DIR"
    140 
    141 # Remove aosp stuff away from PATH to prevent configure error.
    142 aosp="${ANDROID_NDK_ROOT%/}"
    143 aosp="${aosp%/ndk}"
    144 aosp_regex="${aosp}/[^:]*:"
    145 PATH="`echo $PATH | sed -e \"s#$aosp_regex##g\"`"
    146 export PATH
    147 
    148 for abi in $ABIS; do
    149   dump "Rebuild for ABI $abi with $STL"
    150 
    151   # configure the toolchain
    152   dump "Configure: $TOOLCHAIN toolchain build"
    153   LLVM_BUILD_OUT=$BUILD_OUT/llvm/$abi
    154   run mkdir -p $LLVM_BUILD_OUT && cd $LLVM_BUILD_OUT
    155   fail_panic "Couldn't cd into llvm build path: $LLVM_BUILD_OUT"
    156 
    157   arch="$(convert_abi_to_arch $abi)"
    158   if [ -n "$OPTION_GCC_VERSION" ]; then
    159     GCCVER=$OPTION_GCC_VERSION
    160   else
    161     GCCVER=$(get_default_gcc_version_for_arch $arch)
    162   fi
    163   toolchain_prefix=`get_default_toolchain_prefix_for_arch $arch`
    164   toolchain_name=`get_toolchain_name_for_arch $arch $GCCVER`
    165   CFLAGS="-fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections"
    166   case $abi in
    167     armeabi)
    168       # No -mthumb, because ./llvm/lib/Target/ARM/ARMJITInfo.cpp now contain inline assembly
    169       # code such as "stmdb sp!,{r0,r1,r2,r3,lr}" which doesn't support thumb1
    170       CFLAGS=$CFLAGS" -march=armv5te -msoft-float"
    171       ;;
    172     armeabi-v7a|armeabi-v7a-hard)
    173       CFLAGS=$CFLAGS" -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
    174       if [ "$arbi" = "armeabi-v7a-hard" ]; then
    175         CFLAGS=$CFLAGS" -mhard-float -D_NDK_MATH_NO_SOFTFP=1 -Wl,--no-warn-mismatch -lm_hard"
    176       fi
    177       ;;
    178     mips)
    179       CFLAGS=$CFLAGS" -fmessage-length=0 -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers"
    180       ;;
    181   esac
    182 
    183   run $BUILDTOOLS/make-standalone-toolchain.sh \
    184     --toolchain=$toolchain_name \
    185     --stl=$STL \
    186     --arch=$arch \
    187     --system=$HOST_TAG \
    188     --platform=android-19 \
    189     --install-dir=$BUILD_OUT/ndk-standalone-$arch
    190   fail_panic "Couldn't make standalone for $arch"
    191 
    192   run mkdir -p $TOOLCHAIN_BUILD_PREFIX/$abi
    193   if [ "$SHARED" = "yes" ]; then
    194     run cp -f $BUILD_OUT/ndk-standalone-$arch/$toolchain_prefix/lib/lib${STL}_shared.so $TOOLCHAIN_BUILD_PREFIX/$abi
    195   fi
    196 
    197   CC=$BUILD_OUT/ndk-standalone-$arch/bin/$toolchain_prefix-gcc
    198   CXX=$BUILD_OUT/ndk-standalone-$arch/bin/$toolchain_prefix-g++
    199   export CC CXX
    200 
    201   EXTRA_LLVM_CONFIG=""
    202   EXTRA_MCLINKER_CONFIG=""
    203   if [ "$SHARED" = "yes" ]; then
    204     EXTRA_LLVM_CONFIG="--enable-shared --with-extra-ld-options=-l${STL}_shared"
    205     EXTRA_MCLINKER_CONFIG="--with-llvm-shared-lib=$LLVM_BUILD_OUT/Release/lib/libLLVM-${DEFAULT_LLVM_VERSION}.so"
    206   fi
    207 
    208   run $SRC_DIR/$TOOLCHAIN/llvm/configure \
    209     --prefix=$TOOLCHAIN_BUILD_PREFIX/$abi \
    210     --host=$toolchain_prefix \
    211     --with-bug-report-url=$DEFAULT_ISSUE_TRACKER_URL \
    212     --enable-targets=$arch \
    213     --enable-optimized \
    214     --enable-shrink-binary-size \
    215     --disable-polly \
    216     --with-clang-srcdir=/dev/null \
    217     --disable-assertions \
    218     --disable-terminfo \
    219     --with-extra-options="$CFLAGS" \
    220     $EXTRA_LLVM_CONFIG
    221   fail_panic "Couldn't configure llvm toolchain for ABI $abi"
    222 
    223   dump "Building : llvm toolchain [this can take a long time]."
    224   unset CC CXX  # Need for BuildTools
    225   export CC CXX
    226   run make -j$NUM_JOBS $MAKE_FLAGS
    227   fail_panic "Couldn't compile llvm toolchain"
    228 
    229   # Copy binaries what we need
    230   run mkdir -p $TOOLCHAIN_BUILD_PREFIX/$abi
    231   if [ "$SHARED" = "yes" ]; then
    232     run cp -f $LLVM_BUILD_OUT/Release/lib/libLLVM-${DEFAULT_LLVM_VERSION}.so $TOOLCHAIN_BUILD_PREFIX/$abi
    233   fi
    234   run cp -f $LLVM_BUILD_OUT/Release/bin/ndk-translate $TOOLCHAIN_BUILD_PREFIX/$abi
    235   run cp -f $LLVM_BUILD_OUT/Release/bin/llc $TOOLCHAIN_BUILD_PREFIX/$abi
    236   run cd $TOOLCHAIN_BUILD_PREFIX/$abi && ln -s ndk-translate le32-none-ndk-translate
    237   run cd $TOOLCHAIN_BUILD_PREFIX/$abi && ln -s ndk-translate le64-none-ndk-translate
    238 
    239   # build mclinker only against default the LLVM version, once
    240   dump "Configure: mclinker against $TOOLCHAIN"
    241   MCLINKER_BUILD_OUT=$MCLINKER_SRC_DIR/build/$abi
    242   run mkdir -p $MCLINKER_BUILD_OUT && cd $MCLINKER_BUILD_OUT
    243   fail_panic "Couldn't cd into mclinker build path: $MCLINKER_BUILD_OUT"
    244 
    245   CC="$BUILD_OUT/ndk-standalone-$arch/bin/$toolchain_prefix-gcc $CFLAGS"
    246   CXX="$BUILD_OUT/ndk-standalone-$arch/bin/$toolchain_prefix-g++ $CFLAGS"
    247   if [ "$SHARED" = "yes" ]; then
    248     CXX="$CXX -l${STL}_shared"
    249   fi
    250   export CC CXX
    251 
    252   run $MCLINKER_SRC_DIR/configure \
    253     --prefix=$TOOLCHAIN_BUILD_PREFIX/$abi \
    254     --with-llvm-config=$LLVM_BUILD_OUT/BuildTools/Release/bin/llvm-config \
    255     --enable-targets=$arch \
    256     --host=$toolchain_prefix \
    257     --enable-shrink-binary-size \
    258     $EXTRA_MCLINKER_CONFIG
    259   fail_panic "Couldn't configure mclinker for ABI $abi"
    260 
    261   CXXFLAGS="$CXXFLAGS -fexceptions"  # optimized/ScriptParser.cc needs it
    262   export CXXFLAGS
    263   dump "Building : mclinker"
    264   cd $MCLINKER_BUILD_OUT
    265   run make -j$NUM_JOBS $MAKE_FLAGS CXXFLAGS="$CXXFLAGS"
    266   fail_panic "Couldn't compile mclinker"
    267 
    268   run mkdir -p $TOOLCHAIN_BUILD_PREFIX/$abi
    269   if [ -f $MCLINKER_BUILD_OUT/tools/lite/ld.lite ]; then
    270     run cp -f $MCLINKER_BUILD_OUT/tools/lite/ld.lite $TOOLCHAIN_BUILD_PREFIX/$abi/ld.mcld
    271   else
    272     run cp -f $MCLINKER_BUILD_OUT/optimized/ld.mcld $TOOLCHAIN_BUILD_PREFIX/$abi
    273   fi
    274   fail_panic "Couldn't copy mclinker"
    275 
    276   # Strip
    277   STRIP=$BUILD_OUT/ndk-standalone-$arch/bin/$toolchain_prefix-strip
    278   find $TOOLCHAIN_BUILD_PREFIX/$abi -maxdepth 1 -type f -exec $STRIP --strip-all {} \;
    279 
    280   # copy SOURCES
    281   run cp "$SRC_DIR/SOURCES" $TOOLCHAIN_BUILD_PREFIX/$abi
    282 done
    283 
    284 TOOLCHAIN_BUILD_PREFIX="`cd $TOOLCHAIN_BUILD_PREFIX; pwd`"
    285 
    286 dump "Done. Output is under $TOOLCHAIN_BUILD_PREFIX"
    287 if [ -z "$OPTION_BUILD_OUT" ] ; then
    288   rm -rf $BUILD_OUT
    289 fi
    290