- add sources.
[platform/framework/web/crosswalk.git] / src / build / android / envsetup_functions.sh
1 #!/bin/bash
2
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 # Defines functions for envsetup.sh which sets up environment for building
8 # Chromium on Android.  The build can be either use the Android NDK/SDK or
9 # android source tree.  Each has a unique init function which calls functions
10 # prefixed with "common_" that is common for both environment setups.
11
12 ################################################################################
13 # Check to make sure the toolchain exists for the NDK version.
14 ################################################################################
15 common_check_toolchain() {
16   if [[ ! -d "${ANDROID_TOOLCHAIN}" ]]; then
17     echo "Can not find Android toolchain in ${ANDROID_TOOLCHAIN}." >& 2
18     echo "The NDK version might be wrong." >& 2
19     return 1
20   fi
21 }
22
23 ################################################################################
24 # Exports environment variables common to both sdk and non-sdk build (e.g. PATH)
25 # based on CHROME_SRC and ANDROID_TOOLCHAIN, along with DEFINES for GYP_DEFINES.
26 ################################################################################
27 common_vars_defines() {
28   # Set toolchain path according to product architecture.
29   case "${TARGET_ARCH}" in
30     "arm")
31       toolchain_arch="arm-linux-androideabi"
32       ;;
33     "x86")
34       toolchain_arch="x86"
35       ;;
36     "mips")
37       toolchain_arch="mipsel-linux-android"
38       ;;
39     *)
40       echo "TARGET_ARCH: ${TARGET_ARCH} is not supported." >& 2
41       print_usage
42       return 1
43       ;;
44   esac
45
46   toolchain_version="4.6"
47   # We directly set the gcc_version since we know what we use, and it should
48   # be set to xx instead of x.x. Refer the output of compiler_version.py.
49   gcc_version="46"
50   toolchain_target=$(basename \
51     ${ANDROID_NDK_ROOT}/toolchains/${toolchain_arch}-${toolchain_version})
52   toolchain_path="${ANDROID_NDK_ROOT}/toolchains/${toolchain_target}"\
53 "/prebuilt/${toolchain_dir}/bin/"
54
55   # Set only if not already set.
56   # Don't override ANDROID_TOOLCHAIN if set by Android configuration env.
57   export ANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN:-${toolchain_path}}
58
59   common_check_toolchain
60
61   # Add Android SDK/NDK tools to system path.
62   export PATH=$PATH:${ANDROID_NDK_ROOT}
63   export PATH=$PATH:${ANDROID_SDK_ROOT}/tools
64   export PATH=$PATH:${ANDROID_SDK_ROOT}/platform-tools
65   export PATH=$PATH:${ANDROID_SDK_ROOT}/build-tools/\
66 ${ANDROID_SDK_BUILD_TOOLS_VERSION}
67
68   # This must be set before ANDROID_TOOLCHAIN, so that clang could find the
69   # gold linker.
70   # TODO(michaelbai): Remove this path once the gold linker become the default
71   # linker.
72   export PATH=$PATH:${CHROME_SRC}/build/android/${toolchain_arch}-gold
73
74   # Must have tools like arm-linux-androideabi-gcc on the path for ninja
75   export PATH=$PATH:${ANDROID_TOOLCHAIN}
76
77   # Add Chromium Android development scripts to system path.
78   # Must be after CHROME_SRC is set.
79   export PATH=$PATH:${CHROME_SRC}/build/android
80
81   # TODO(beverloo): Remove these once all consumers updated to --strip-binary.
82   export OBJCOPY=$(echo ${ANDROID_TOOLCHAIN}/*-objcopy)
83   export STRIP=$(echo ${ANDROID_TOOLCHAIN}/*-strip)
84
85   # The set of GYP_DEFINES to pass to gyp. Use 'readlink -e' on directories
86   # to canonicalize them (remove double '/', remove trailing '/', etc).
87   DEFINES="OS=android"
88   DEFINES+=" host_os=${host_os}"
89   DEFINES+=" gcc_version=${gcc_version}"
90
91   if [[ -n "$CHROME_ANDROID_OFFICIAL_BUILD" ]]; then
92     DEFINES+=" branding=Chrome"
93     DEFINES+=" buildtype=Official"
94
95     # These defines are used by various chrome build scripts to tag the binary's
96     # version string as 'official' in linux builds (e.g. in
97     # chrome/trunk/src/chrome/tools/build/version.py).
98     export OFFICIAL_BUILD=1
99     export CHROMIUM_BUILD="_google_chrome"
100     export CHROME_BUILD_TYPE="_official"
101   fi
102
103   # The order file specifies the order of symbols in the .text section of the
104   # shared library, libchromeview.so.  The file is an order list of section
105   # names and the library is linked with option
106   # --section-ordering-file=<orderfile>. The order file is updated by profiling
107   # startup after compiling with the order_profiling=1 GYP_DEFINES flag.
108   ORDER_DEFINES="order_text_section=${CHROME_SRC}/orderfiles/orderfile.out"
109
110   # The following defines will affect ARM code generation of both C/C++ compiler
111   # and V8 mksnapshot.
112   case "${TARGET_ARCH}" in
113     "arm")
114       DEFINES+=" ${ORDER_DEFINES}"
115       DEFINES+=" target_arch=arm"
116       ;;
117     "x86")
118     # TODO(tedbo): The ia32 build fails on ffmpeg, so we disable it here.
119       DEFINES+=" use_libffmpeg=0"
120
121       host_arch=$(uname -m | sed -e \
122         's/i.86/ia32/;s/x86_64/x64/;s/amd64/x64/;s/arm.*/arm/;s/i86pc/ia32/')
123       DEFINES+=" host_arch=${host_arch}"
124       DEFINES+=" target_arch=ia32"
125       ;;
126     "mips")
127       DEFINES+=" target_arch=mipsel"
128       DEFINES+=" mips_arch_variant=mips32r1"
129       ;;
130     *)
131       echo "TARGET_ARCH: ${TARGET_ARCH} is not supported." >& 2
132       print_usage
133       return 1
134   esac
135 }
136
137
138 ################################################################################
139 # Exports common GYP variables based on variable DEFINES and CHROME_SRC.
140 ################################################################################
141 common_gyp_vars() {
142   export GYP_DEFINES="${DEFINES}"
143
144   # Set GYP_GENERATORS to ninja if it's currently unset or null.
145   if [ -z "$GYP_GENERATORS" ]; then
146     echo "Defaulting GYP_GENERATORS to ninja."
147     GYP_GENERATORS=ninja
148   elif [ "$GYP_GENERATORS" != "ninja" ]; then
149     echo "Warning: GYP_GENERATORS set to '$GYP_GENERATORS'."
150     echo "Only GYP_GENERATORS=ninja has continuous coverage."
151   fi
152   export GYP_GENERATORS
153
154   # Use our All target as the default
155   export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} default_target=All"
156
157   # We want to use our version of "all" targets.
158   export CHROMIUM_GYP_FILE="${CHROME_SRC}/build/all_android.gyp"
159 }
160
161
162 ################################################################################
163 # Prints out help message on usage.
164 ################################################################################
165 print_usage() {
166   echo "usage: ${0##*/} [--target-arch=value] [--help]" >& 2
167   echo "--target-arch=value     target CPU architecture (arm=default, x86)" >& 2
168   echo "--host-os=value         override host OS detection (linux, mac)" >&2
169   echo "--try-32bit-host        try building a 32-bit host architecture" >&2
170   echo "--help                  this help" >& 2
171 }
172
173 ################################################################################
174 # Process command line options.
175 # --target-arch=  Specifices target CPU architecture. Currently supported
176 #                 architectures are "arm" (default), and "x86".
177 # --help          Prints out help message.
178 ################################################################################
179 process_options() {
180   host_os=$(uname -s | sed -e 's/Linux/linux/;s/Darwin/mac/')
181   try_32bit_host_build=
182   while [[ -n $1 ]]; do
183     case "$1" in
184       --target-arch=*)
185         target_arch="$(echo "$1" | sed 's/^[^=]*=//')"
186         ;;
187       --host-os=*)
188         host_os="$(echo "$1" | sed 's/^[^=]*=//')"
189         ;;
190       --try-32bit-host)
191         try_32bit_host_build=true
192         ;;
193       --help)
194         print_usage
195         return 1
196         ;;
197       *)
198         # Ignore other command line options
199         echo "Unknown option: $1"
200         ;;
201     esac
202     shift
203   done
204
205   # Sets TARGET_ARCH. Defaults to arm if not specified.
206   TARGET_ARCH=${target_arch:-arm}
207 }
208
209 ################################################################################
210 # Initializes environment variables for NDK/SDK build. Only Android NDK Revision
211 # 7 on Linux or Mac is offically supported. To run this script, the system
212 # environment ANDROID_NDK_ROOT must be set to Android NDK's root path.  The
213 # ANDROID_SDK_ROOT only needs to be set to override the default SDK which is in
214 # the tree under $ROOT/src/third_party/android_tools/sdk.
215 # To build Chromium for Android with NDK/SDK follow the steps below:
216 #  > export ANDROID_NDK_ROOT=<android ndk root>
217 #  > export ANDROID_SDK_ROOT=<android sdk root> # to override the default sdk
218 #  > . build/android/envsetup.sh
219 #  > make
220 ################################################################################
221 sdk_build_init() {
222
223   # Allow the caller to override a few environment variables. If any of them is
224   # unset, we default to a sane value that's known to work. This allows for
225   # experimentation with a custom SDK.
226   local sdk_defines=""
227   if [[ -z "${ANDROID_NDK_ROOT}" || ! -d "${ANDROID_NDK_ROOT}" ]]; then
228     export ANDROID_NDK_ROOT="${CHROME_SRC}/third_party/android_tools/ndk/"
229   fi
230   if [[ -z "${ANDROID_SDK_VERSION}" ]]; then
231     export ANDROID_SDK_VERSION=18
232   else
233     sdk_defines+=" android_sdk_version=${ANDROID_SDK_VERSION}"
234   fi
235   local sdk_suffix=platforms/android-${ANDROID_SDK_VERSION}
236   if [[ -z "${ANDROID_SDK_ROOT}" || \
237        ! -d "${ANDROID_SDK_ROOT}/${sdk_suffix}" ]]; then
238     export ANDROID_SDK_ROOT="${CHROME_SRC}/third_party/android_tools/sdk/"
239   else
240     sdk_defines+=" android_sdk_root=${ANDROID_SDK_ROOT}"
241   fi
242   if [[ -z "${ANDROID_SDK_BUILD_TOOLS_VERSION}" ]]; then
243     export ANDROID_SDK_BUILD_TOOLS_VERSION=18.0.1
244   fi
245
246   unset ANDROID_BUILD_TOP
247
248   # Set default target.
249   export TARGET_PRODUCT="${TARGET_PRODUCT:-trygon}"
250
251   # Unset toolchain so that it can be set based on TARGET_PRODUCT.
252   # This makes it easy to switch between architectures.
253   unset ANDROID_TOOLCHAIN
254
255   common_vars_defines
256
257   DEFINES+="${sdk_defines}"
258
259   common_gyp_vars
260
261   if [[ -n "$CHROME_ANDROID_BUILD_WEBVIEW" ]]; then
262     # Can not build WebView with NDK/SDK because it needs the Android build
263     # system and build inside an Android source tree.
264     echo "Can not build WebView with NDK/SDK.  Requires android source tree." \
265         >& 2
266     echo "Try . build/android/envsetup.sh instead." >& 2
267     return 1
268   fi
269
270   # Directory containing build-tools: aapt, aidl, dx
271   export ANDROID_SDK_TOOLS="${ANDROID_SDK_ROOT}/build-tools/\
272 ${ANDROID_SDK_BUILD_TOOLS_VERSION}"
273 }
274
275 ################################################################################
276 # To build WebView, we use the Android build system and build inside an Android
277 # source tree. This method is called from non_sdk_build_init() and adds to the
278 # settings specified there.
279 #############################################################################
280 webview_build_init() {
281   # Use the latest API in the AOSP prebuilts directory (change with AOSP roll).
282   export ANDROID_SDK_VERSION=18
283
284   # For the WebView build we always use the NDK and SDK in the Android tree,
285   # and we don't touch ANDROID_TOOLCHAIN which is already set by Android.
286   export ANDROID_NDK_ROOT=${ANDROID_BUILD_TOP}/prebuilts/ndk/8
287   export ANDROID_SDK_ROOT=${ANDROID_BUILD_TOP}/prebuilts/sdk/\
288 ${ANDROID_SDK_VERSION}
289
290   common_vars_defines
291
292   # We need to supply SDK paths relative to the top of the Android tree to make
293   # sure the generated Android makefiles are portable, as they will be checked
294   # into the Android tree.
295   ANDROID_SDK=$(python -c \
296       "import os.path; print os.path.relpath('${ANDROID_SDK_ROOT}', \
297       '${ANDROID_BUILD_TOP}')")
298   case "${host_os}" in
299     "linux")
300       ANDROID_SDK_TOOLS=$(python -c \
301           "import os.path; \
302           print os.path.relpath('${ANDROID_SDK_ROOT}/../tools/linux', \
303           '${ANDROID_BUILD_TOP}')")
304       ;;
305     "mac")
306       ANDROID_SDK_TOOLS=$(python -c \
307           "import os.path; \
308           print os.path.relpath('${ANDROID_SDK_ROOT}/../tools/darwin', \
309           '${ANDROID_BUILD_TOP}')")
310       ;;
311   esac
312   DEFINES+=" android_webview_build=1"
313   # temporary until all uses of android_build_type are gone (crbug.com/184431)
314   DEFINES+=" android_build_type=1"
315   DEFINES+=" android_src=\$(PWD)"
316   DEFINES+=" android_sdk=\$(PWD)/${ANDROID_SDK}"
317   DEFINES+=" android_sdk_root=\$(PWD)/${ANDROID_SDK}"
318   DEFINES+=" android_sdk_tools=\$(PWD)/${ANDROID_SDK_TOOLS}"
319   DEFINES+=" android_sdk_version=${ANDROID_SDK_VERSION}"
320   DEFINES+=" android_toolchain=${ANDROID_TOOLCHAIN}"
321   if [[ -n "$CHROME_ANDROID_WEBVIEW_ENABLE_DMPROF" ]]; then
322     DEFINES+=" disable_debugallocation=1"
323     DEFINES+=" android_full_debug=1"
324     DEFINES+=" android_use_tcmalloc=1"
325   fi
326   export GYP_DEFINES="${DEFINES}"
327
328   export GYP_GENERATORS="android"
329
330   export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} default_target=All"
331   export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} limit_to_target_all=1"
332   export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} auto_regeneration=0"
333
334   export CHROMIUM_GYP_FILE="${CHROME_SRC}/android_webview/all_webview.gyp"
335 }