Home | History | Annotate | Download | only in abcc
      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 # Build a abcc package. This exploits build-on-device-toolchain.sh and
     18 # needs SDK help.
     19 #
     20 
     21 PROGDIR=`cd $(dirname $0) && pwd`
     22 NDK_BUILDTOOLS_PATH=$PROGDIR/../../build/tools
     23 . $NDK_BUILDTOOLS_PATH/prebuilt-common.sh
     24 
     25 SDK_DIR=
     26 register_var_option "--sdk-dir=<path>" SDK_DIR "SDK installation directory (Required)"
     27 
     28 SDK_TARGET=
     29 register_var_option "--sdk-target=<str>" SDK_TARGET "SDK target for building APK (Use 'android list target' to check)"
     30 
     31 NDK_DIR=$ANDROID_NDK_ROOT
     32 register_var_option "--ndk-dir=<path>" NDK_DIR "NDK installation directory"
     33 
     34 ABCC_DIR=$PROGDIR
     35 register_var_option "--abcc-dir=<path>" ABCC_DIR "Compiler app directory"
     36 
     37 BUILD_DIR=/tmp/ndk-$USER/build
     38 register_var_option "--build-dir=<path>" BUILD_DIR "Specify temporary build dir"
     39 
     40 OUT_DIR=/tmp/ndk-$USER/out
     41 register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly"
     42 
     43 ABIS=
     44 register_var_option "--abis=<target>" ABIS "List which targets you use (comma for split)"
     45 
     46 DEFAULT_TMP_SRC_DIR=/tmp/ndk-$USER/ndk-toolchain-source-`date +%s`
     47 SRC_DIR=$DEFAULT_TMP_SRC_DIR
     48 register_var_option "--src-dir=<path>" SRC_DIR "Specify an existing toolchain source"
     49 
     50 ONLY_ASSETS=
     51 do_only_assets_option () { ONLY_ASSETS=yes; }
     52 register_option "--only-assets" do_only_assets_option "Build toolchain only under prebuilts/assets/ instead of whole app"
     53 
     54 DEBUG=
     55 do_debug_option () { DEBUG=yes; }
     56 register_option "--no-share-system-uid" do_debug_option "Just for testing. Be careful of device directory permission issue!"
     57 
     58 TESTING=
     59 do_testing_option () { TESTING=yes; }
     60 register_option "--testing" do_testing_option "Package all prebuilts into abcc for testing"
     61 
     62 NO_REBUILD_ASSETS=
     63 do_no_rebuild_assets_option () { NO_REBUILD_ASSETS=yes; }
     64 register_option "--no-rebuild-assets" do_no_rebuild_assets_option "Use existing toolchain prebuilt assets instead of rebuilding them"
     65 
     66 register_jobs_option
     67 
     68 PROGRAM_PARAMETERS=""
     69 PROGRAM_DESCRIPTION=\
     70 "This script can be used to build abcc, which contains all toolchain
     71 we need for on-device compilation. This script also needs SDK with binaries,
     72 like ant, aapt, android, ...etc, since they are necessary to produce APK."
     73 
     74 extract_parameters "$@"
     75 ABIS=$(commas_to_spaces $ABIS)
     76 test -z "$ABIS" && ABIS="$PREBUILT_ABIS"
     77 BUILDTOOLS=$NDK_DIR/build/tools
     78 ABCC_PREBUILT_ASSETS=$ABCC_DIR/prebuilts/assets
     79 ABCC=`basename $ABCC_DIR`
     80 FLAGS=
     81 test "$VERBOSE" = "yes" && FLAGS=$FLAGS" --verbose"
     82 test "$VERBOSE2" = "yes" && FLAGS=$FLAGS" --verbose"
     83 FLAGS="$FLAGS -j$NUM_JOBS"
     84 test "$TESTING" = "yes" && FLAGS=$FLAGS" --testing"
     85 
     86 #
     87 # First: Build toolchain assets
     88 #
     89 
     90 if [ "$NO_REBUILD_ASSETS" = "yes" ]; then
     91   test -z "`ls $ABCC_PREBUILT_ASSETS 2> /dev/null`" && dump "[WARNING] No toolchain assets found!"
     92 else
     93   test "$SRC_DIR" != "$DEFAULT_TMP_SRC_DIR" && check_toolchain_src_dir "$SRC_DIR"
     94   test "$SRC_DIR" = "$DEFAULT_TMP_SRC_DIR" && run $BUILDTOOLS/download-toolchain-sources.sh $SRC_DIR
     95   run rm -rf $ABCC_PREBUILT_ASSETS/*
     96   for ABI in $ABIS; do
     97     run $BUILDTOOLS/build-on-device-toolchain.sh --ndk-dir=$NDK_DIR --build-dir=$BUILD_DIR --out-dir=$ABCC_PREBUILT_ASSETS/$ABI --abi=$ABI --no-sync $FLAGS $SRC_DIR
     98     fail_panic "Could not build device toolchain."
     99   done
    100 fi
    101 
    102 test "$ONLY_ASSETS" = "yes" && exit
    103 
    104 #
    105 # Second: Check SDK
    106 #
    107 
    108 test -z "$SDK_DIR" && dump "--sdk-dir is required." && exit 1
    109 test ! -f "$SDK_DIR/tools/android" && dump "--sdk-dir is not a valid SDK." && exit 1
    110 test `$SDK_DIR/tools/android list target | grep '^id' | wc -l` -eq 0 && "Please download at least one target first." && exit 1
    111 
    112 # Ask users for SDK configuration
    113 if [ `$SDK_DIR/tools/android list target | grep '^id' | wc -l` -ne 1 ] && [ -z "$SDK_TARGET" ]; then
    114   DEFAULT_TARGET="`$SDK_DIR/tools/android list target | grep '^id' | head -n 1 | awk '{print $4}'`"
    115   echo "* Which target do you want? [$DEFAULT_TARGET]"
    116   for line in "`$SDK_DIR/tools/android list target | grep '^id'`"; do
    117     echo "-- `echo $line | awk '{print $4}'`"
    118   done
    119   echo ""
    120   read SDK_TARGET
    121   test -z "$SDK_TARGET" && SDK_TARGET=$DEFAULT_TARGET
    122 elif [ -z "$SDK_TARGET" ]; then
    123   SDK_TARGET=`$SDK_DIR/tools/android list target | grep '^id' | awk '{print $4}'`
    124 fi
    125 dump "SDK target: $SDK_TARGET"
    126 
    127 #
    128 # Third: Build apk
    129 #
    130 
    131 run rm -rf $BUILD_DIR
    132 run mkdir -p $BUILD_DIR $OUT_DIR
    133 
    134 run cd $BUILD_DIR
    135 run cp -a $ABCC_DIR $ABCC
    136 
    137 run cd $BUILD_DIR/$ABCC
    138 run $SDK_DIR/tools/android update project -p . -t "$SDK_TARGET"
    139 if [ $? -ne 0 ]; then
    140   dump "Cannot create build.xml. Abort."
    141   exit 1
    142 fi
    143 
    144 for ABI in $ABIS; do
    145   run rm -rf obj libs
    146   run $NDK_DIR/ndk-build -B APP_ABI=$ABI -C jni
    147   fail_panic "Build ndk-build failed. Abort."
    148   if [ "$DEBUG" = "yes" ]; then
    149     run rm -f AndroidManifest.xml
    150     run cp -a AndroidManifest.xml.debug AndroidManifest.xml
    151     run ant debug -Dasset.dir=prebuilts/assets/$ABI
    152     fail_panic "Build dex failed. Abort."
    153     run cp -a bin/$ABCC-debug.apk $OUT_DIR/$ABCC-$ABI.apk
    154   else  # DEBUG != yes
    155     run ant release -Dasset.dir=prebuilts/assets/$ABI
    156     fail_panic "Build dex failed. Abort."
    157     run cp -a bin/$ABCC-release-unsigned.apk $OUT_DIR/$ABCC-$ABI-unsigned.apk
    158   fi
    159 done
    160 
    161 run cd $OUT_DIR
    162 run rm -rf $BUILD_DIR
    163 
    164 dump "Done. Compiler app is under $OUT_DIR"
    165 test "$DEBUG" != "yes" && dump "[WARNING] APK has not been signed nor aligned!"
    166 exit 0
    167