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
21PROGDIR=`cd $(dirname $0) && pwd`
22NDK_BUILDTOOLS_PATH=$PROGDIR/../../build/tools
23. $NDK_BUILDTOOLS_PATH/prebuilt-common.sh
24
25SDK_DIR=
26register_var_option "--sdk-dir=<path>" SDK_DIR "SDK installation directory (Required)"
27
28SDK_TARGET=
29register_var_option "--sdk-target=<str>" SDK_TARGET "SDK target for building APK (Use 'android list target' to check)"
30
31NDK_DIR=$ANDROID_NDK_ROOT
32register_var_option "--ndk-dir=<path>" NDK_DIR "NDK installation directory"
33
34ABCC_DIR=$PROGDIR
35register_var_option "--abcc-dir=<path>" ABCC_DIR "Compiler app directory"
36
37BUILD_DIR=/tmp/ndk-$USER/build
38register_var_option "--build-dir=<path>" BUILD_DIR "Specify temporary build dir"
39
40OUT_DIR=/tmp/ndk-$USER/out
41register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly"
42
43ABIS=
44register_var_option "--abis=<target>" ABIS "List which targets you use (comma for split)"
45
46DEFAULT_TMP_SRC_DIR=/tmp/ndk-$USER/ndk-toolchain-source-`date +%s`
47SRC_DIR=$DEFAULT_TMP_SRC_DIR
48register_var_option "--src-dir=<path>" SRC_DIR "Specify an existing toolchain source"
49
50ONLY_ASSETS=
51do_only_assets_option () { ONLY_ASSETS=yes; }
52register_option "--only-assets" do_only_assets_option "Build toolchain only under prebuilts/assets/ instead of whole app"
53
54DEBUG=
55do_debug_option () { DEBUG=yes; }
56register_option "--no-share-system-uid" do_debug_option "Just for testing. Be careful of device directory permission issue!"
57
58TESTING=
59do_testing_option () { TESTING=yes; }
60register_option "--testing" do_testing_option "Package all prebuilts into abcc for testing"
61
62NO_REBUILD_ASSETS=
63do_no_rebuild_assets_option () { NO_REBUILD_ASSETS=yes; }
64register_option "--no-rebuild-assets" do_no_rebuild_assets_option "Use existing toolchain prebuilt assets instead of rebuilding them"
65
66register_jobs_option
67
68PROGRAM_PARAMETERS=""
69PROGRAM_DESCRIPTION=\
70"This script can be used to build abcc, which contains all toolchain
71we need for on-device compilation. This script also needs SDK with binaries,
72like ant, aapt, android, ...etc, since they are necessary to produce APK."
73
74extract_parameters "$@"
75ABIS=$(commas_to_spaces $ABIS)
76test -z "$ABIS" && ABIS="$PREBUILT_ABIS"
77BUILDTOOLS=$NDK_DIR/build/tools
78ABCC_PREBUILT_ASSETS=$ABCC_DIR/prebuilts/assets
79ABCC=`basename $ABCC_DIR`
80FLAGS=
81test "$VERBOSE" = "yes" && FLAGS=$FLAGS" --verbose"
82test "$VERBOSE2" = "yes" && FLAGS=$FLAGS" --verbose"
83FLAGS="$FLAGS -j$NUM_JOBS"
84test "$TESTING" = "yes" && FLAGS=$FLAGS" --testing"
85
86#
87# First: Build toolchain assets
88#
89
90if [ "$NO_REBUILD_ASSETS" = "yes" ]; then
91  test -z "`ls $ABCC_PREBUILT_ASSETS 2> /dev/null`" && dump "[WARNING] No toolchain assets found!"
92else
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
100fi
101
102test "$ONLY_ASSETS" = "yes" && exit
103
104#
105# Second: Check SDK
106#
107
108test -z "$SDK_DIR" && dump "--sdk-dir is required." && exit 1
109test ! -f "$SDK_DIR/tools/android" && dump "--sdk-dir is not a valid SDK." && exit 1
110test `$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
113if [ `$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
122elif [ -z "$SDK_TARGET" ]; then
123  SDK_TARGET=`$SDK_DIR/tools/android list target | grep '^id' | awk '{print $4}'`
124fi
125dump "SDK target: $SDK_TARGET"
126
127#
128# Third: Build apk
129#
130
131run rm -rf $BUILD_DIR
132run mkdir -p $BUILD_DIR $OUT_DIR
133
134run cd $BUILD_DIR
135run cp -a $ABCC_DIR $ABCC
136
137run cd $BUILD_DIR/$ABCC
138run $SDK_DIR/tools/android update project -p . -t "$SDK_TARGET"
139if [ $? -ne 0 ]; then
140  dump "Cannot create build.xml. Abort."
141  exit 1
142fi
143
144for 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
159done
160
161run cd $OUT_DIR
162run rm -rf $BUILD_DIR
163
164dump "Done. Compiler app is under $OUT_DIR"
165test "$DEBUG" != "yes" && dump "[WARNING] APK has not been signed nor aligned!"
166exit 0
167