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.
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.
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
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
31 toolchain_arch="arm-linux-androideabi"
37 toolchain_arch="mipsel-linux-android"
40 echo "TARGET_ARCH: ${TARGET_ARCH} is not supported." >& 2
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.
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/"
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}}
59 common_check_toolchain
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}
68 # This must be set before ANDROID_TOOLCHAIN, so that clang could find the
70 # TODO(michaelbai): Remove this path once the gold linker become the default
72 export PATH=$PATH:${CHROME_SRC}/build/android/${toolchain_arch}-gold
74 # Must have tools like arm-linux-androideabi-gcc on the path for ninja
75 export PATH=$PATH:${ANDROID_TOOLCHAIN}
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
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)
85 # The set of GYP_DEFINES to pass to gyp. Use 'readlink -e' on directories
86 # to canonicalize them (remove double '/', remove trailing '/', etc).
88 DEFINES+=" host_os=${host_os}"
89 DEFINES+=" gcc_version=${gcc_version}"
91 if [[ -n "$CHROME_ANDROID_OFFICIAL_BUILD" ]]; then
92 DEFINES+=" branding=Chrome"
93 DEFINES+=" buildtype=Official"
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"
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"
110 # The following defines will affect ARM code generation of both C/C++ compiler
112 case "${TARGET_ARCH}" in
114 DEFINES+=" ${ORDER_DEFINES}"
115 DEFINES+=" target_arch=arm"
118 # TODO(tedbo): The ia32 build fails on ffmpeg, so we disable it here.
119 DEFINES+=" use_libffmpeg=0"
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"
127 DEFINES+=" target_arch=mipsel"
128 DEFINES+=" mips_arch_variant=mips32r1"
131 echo "TARGET_ARCH: ${TARGET_ARCH} is not supported." >& 2
138 ################################################################################
139 # Exports common GYP variables based on variable DEFINES and CHROME_SRC.
140 ################################################################################
142 export GYP_DEFINES="${DEFINES}"
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."
148 elif [ "$GYP_GENERATORS" != "ninja" ]; then
149 echo "Warning: GYP_GENERATORS set to '$GYP_GENERATORS'."
150 echo "Only GYP_GENERATORS=ninja has continuous coverage."
152 export GYP_GENERATORS
154 # Use our All target as the default
155 export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} default_target=All"
157 # We want to use our version of "all" targets.
158 export CHROMIUM_GYP_FILE="${CHROME_SRC}/build/all_android.gyp"
162 ################################################################################
163 # Prints out help message on usage.
164 ################################################################################
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
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 ################################################################################
180 host_os=$(uname -s | sed -e 's/Linux/linux/;s/Darwin/mac/')
181 try_32bit_host_build=
182 while [[ -n $1 ]]; do
185 target_arch="$(echo "$1" | sed 's/^[^=]*=//')"
188 host_os="$(echo "$1" | sed 's/^[^=]*=//')"
191 try_32bit_host_build=true
198 # Ignore other command line options
199 echo "Unknown option: $1"
205 # Sets TARGET_ARCH. Defaults to arm if not specified.
206 TARGET_ARCH=${target_arch:-arm}
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
220 ################################################################################
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.
227 if [[ -z "${ANDROID_NDK_ROOT}" || ! -d "${ANDROID_NDK_ROOT}" ]]; then
228 export ANDROID_NDK_ROOT="${CHROME_SRC}/third_party/android_tools/ndk/"
230 if [[ -z "${ANDROID_SDK_VERSION}" ]]; then
231 export ANDROID_SDK_VERSION=18
233 sdk_defines+=" android_sdk_version=${ANDROID_SDK_VERSION}"
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/"
240 sdk_defines+=" android_sdk_root=${ANDROID_SDK_ROOT}"
242 if [[ -z "${ANDROID_SDK_BUILD_TOOLS_VERSION}" ]]; then
243 export ANDROID_SDK_BUILD_TOOLS_VERSION=18.0.1
246 unset ANDROID_BUILD_TOP
248 # Set default target.
249 export TARGET_PRODUCT="${TARGET_PRODUCT:-trygon}"
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
257 DEFINES+="${sdk_defines}"
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." \
266 echo "Try . build/android/envsetup.sh instead." >& 2
270 # Directory containing build-tools: aapt, aidl, dx
271 export ANDROID_SDK_TOOLS="${ANDROID_SDK_ROOT}/build-tools/\
272 ${ANDROID_SDK_BUILD_TOOLS_VERSION}"
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
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}
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}')")
300 ANDROID_SDK_TOOLS=$(python -c \
302 print os.path.relpath('${ANDROID_SDK_ROOT}/../tools/linux', \
303 '${ANDROID_BUILD_TOP}')")
306 ANDROID_SDK_TOOLS=$(python -c \
308 print os.path.relpath('${ANDROID_SDK_ROOT}/../tools/darwin', \
309 '${ANDROID_BUILD_TOP}')")
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"
326 export GYP_DEFINES="${DEFINES}"
328 export GYP_GENERATORS="android"
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"
334 export CHROMIUM_GYP_FILE="${CHROME_SRC}/android_webview/all_webview.gyp"