+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/config/android/config.gni")
+
+assert(is_android)
+
+
+rebased_android_sdk = rebase_path(android_sdk, root_build_dir)
+rebased_android_sdk_root = rebase_path(android_sdk_root, root_build_dir)
+rebased_android_sdk_build_tools = rebase_path(android_sdk_build_tools, root_build_dir)
+
+
+# Write the target's .build_config file. This is a json file that contains a
+# dictionary of information about how to build this target (things that
+# require knowledge about this target's dependencies and cannot be calculated
+# at gn-time). There is a special syntax to add a value in that dictionary to
+# an action/action_foreachs args:
+# --python-arg=@FileArg($rebased_build_config_path:key0:key1)
+# At runtime, such an arg will be replaced by the value in the build_config.
+# See build/android/gyp/write_build_config.py and
+# build/android/gyp/util/build_utils.py:ExpandFileArgs
+template("write_build_config") {
+ assert(defined(invoker.type))
+ assert(defined(invoker.build_config))
+
+ type = invoker.type
+ build_config = invoker.build_config
+
+ assert(type == "android_apk" || type == "android_library" || type == "android_resources")
+
+ action(target_name) {
+ script = "//build/android/gyp/write_build_config.py"
+ depfile = "$target_gen_dir/$target_name.d"
+
+ deps = []
+ if (defined(invoker.deps)) {
+ deps += invoker.deps
+ }
+
+ outputs = [
+ depfile,
+ build_config
+ ]
+
+ possible_deps_configs = []
+ foreach(d, deps) {
+ dep_gen_dir = get_label_info(d, "target_gen_dir")
+ dep_name = get_label_info(d, "name")
+ possible_deps_configs += [ "$dep_gen_dir/$dep_name.build_config" ]
+ }
+ rebase_possible_deps_configs = rebase_path(possible_deps_configs)
+
+ args = [
+ "--type", type,
+ "--depfile", rebase_path(depfile, root_build_dir),
+ "--possible-deps-configs=$rebase_possible_deps_configs",
+ "--build-config", rebase_path(build_config, root_build_dir),
+ ]
+
+ if (type == "android_library" || type == "android_apk") {
+ args += [
+ "--jar-path", rebase_path(invoker.jar_path, root_build_dir),
+ "--dex-path", rebase_path(invoker.dex_path, root_build_dir),
+ ]
+ }
+
+ if (type == "android_resources" || type == "android_apk") {
+ assert(defined(invoker.resources_zip))
+ args += [
+ "--resources-zip", rebase_path(invoker.resources_zip, root_build_dir),
+ ]
+ }
+
+ if (defined(invoker.srcjar)) {
+ args += [
+ "--srcjar", rebase_path(invoker.srcjar, root_build_dir)
+ ]
+ }
+ }
+}
+
# Creates a zip archive of the inputs.
# If base_dir is provided, the archive paths will be relative to it.
assert(defined(invoker.inputs))
assert(defined(invoker.output))
- rebase_inputs = rebase_path(invoker.inputs)
- rebase_output = rebase_path(invoker.output)
+ rebase_inputs = rebase_path(invoker.inputs, root_build_dir)
+ rebase_output = rebase_path(invoker.output, root_build_dir)
action(target_name) {
script = "//build/android/gn/zip.py"
- source_prereqs = invoker.inputs
- outputs = [invoker.output]
+ depfile = "$target_gen_dir/$target_name.d"
+ inputs = invoker.inputs
+ outputs = [
+ depfile,
+ invoker.output
+ ]
args = [
+ "--depfile", rebase_path(depfile, root_build_dir),
"--inputs=$rebase_inputs",
"--output=$rebase_output",
]
if (defined(invoker.base_dir)) {
args += [
- "--base-dir", rebase_path(invoker.base_dir)
+ "--base-dir", rebase_path(invoker.base_dir, root_build_dir)
]
}
}
}
+template("dex") {
+ assert(defined(invoker.sources))
+ assert(defined(invoker.output))
+ action(target_name) {
+ script = "//build/android/gyp/dex.py"
+ depfile = "$target_gen_dir/$target_name.d"
+ sources = invoker.sources
+ outputs = [depfile, invoker.output]
+ if (defined(invoker.inputs)) {
+ inputs = invoker.inputs
+ }
+
+ rebased_output = rebase_path(invoker.output, root_build_dir)
+
+ args = [
+ "--depfile", rebase_path(depfile, root_build_dir),
+ "--android-sdk-tools", rebased_android_sdk_build_tools,
+ "--dex-path", rebased_output,
+ ]
+
+ if (defined(invoker.no_locals) && invoker.no_locals) {
+ args += [
+ "--no-locals=1"
+ ]
+ }
+
+ if (defined(invoker.args)) {
+ args += invoker.args
+ }
+
+ args += rebase_path(invoker.sources, root_build_dir)
+ }
+}
+
+# Packages resources, assets, dex, and native libraries into an apk. Signs and
+# zipaligns the apk.
+template("create_apk") {
+ _android_manifest = invoker.android_manifest
+ _base_path = invoker.base_path
+ _final_apk_path = invoker.apk_path
+ _resources_zip = invoker.resources_zip
+ _dex_path = invoker.dex_path
+ _keystore_path = invoker.keystore_path
+ _keystore_name = invoker.keystore_name
+ _keystore_password = invoker.keystore_password
+
+ _deps = []
+ if (defined(invoker.deps)) {
+ _deps = invoker.deps
+ }
+
+ _native_libs_dir = "//build/android/empty/res"
+ if (defined(invoker.native_libs_dir)) {
+ _native_libs_dir = invoker.native_libs_dir
+ }
+
+ _asset_location = "//build/android/empty/res"
+ if (defined(invoker.asset_location)) {
+ _asset_location = invoker.asset_location
+ }
+
+ _version_code = "0"
+ _version_name = "Developer Build"
+
+ _base_apk_path = _base_path + ".apk_intermediates"
+
+ _resource_packaged_apk_path = _base_apk_path + ".ap_"
+ _packaged_apk_path = _base_apk_path + ".unfinished.apk"
+
+
+ _configuration_name = "Release"
+ if (is_debug) {
+ _configuration_name = "Debug"
+ }
+
+ action("${target_name}__package_resources") {
+ deps = _deps
+
+ script = "//build/android/gyp/package_resources.py"
+ depfile = "${target_gen_dir}/${target_name}.d"
+ source_prereqs = [
+ _android_manifest,
+ _resources_zip,
+ ]
+ outputs = [depfile, _resource_packaged_apk_path]
+
+ _rebased_resources_zips = [rebase_path(_resources_zip, root_build_dir)]
+ args = [
+ "--depfile", rebase_path(depfile, root_build_dir),
+ "--android-sdk", rebased_android_sdk,
+ "--android-sdk-tools", rebased_android_sdk_build_tools,
+
+ "--configuration-name=$_configuration_name",
+
+ "--android-manifest", rebase_path(_android_manifest, root_build_dir),
+ "--version-code", _version_code,
+ "--version-name", _version_name,
+
+ "--asset-dir", rebase_path(_asset_location, root_build_dir),
+ "--resource-zips=$_rebased_resources_zips",
+
+ "--apk-path", rebase_path(_resource_packaged_apk_path, root_build_dir),
+ ]
+ }
+
+ action("${target_name}__package") {
+ script = "//build/android/gyp/ant.py"
+ _ant_script = "//build/android/ant/apk-package.xml"
+
+ depfile = "$target_gen_dir/$target_name.d"
+
+ source_prereqs = [
+ _dex_path,
+ _resource_packaged_apk_path,
+ _ant_script
+ ]
+
+ outputs = [
+ depfile,
+ _packaged_apk_path,
+ ]
+
+ _rebased_emma_jar = ""
+ _rebased_resource_packaged_apk_path = rebase_path(
+ _resource_packaged_apk_path, root_build_dir)
+ _rebased_packaged_apk_path = rebase_path(_packaged_apk_path, root_build_dir)
+ _rebased_native_libs_dir = rebase_path(_native_libs_dir, root_build_dir)
+ _rebased_dex_path = rebase_path(_dex_path, root_build_dir)
+ args = [
+ "--depfile", rebase_path(depfile, root_build_dir),
+ "--",
+ "-quiet",
+ "-DANDROID_SDK_ROOT=$rebased_android_sdk_root",
+ "-DANDROID_SDK_TOOLS=$rebased_android_sdk_build_tools",
+ "-DRESOURCE_PACKAGED_APK_NAME=$_rebased_resource_packaged_apk_path",
+ "-DCONFIGURATION_NAME=$_configuration_name",
+ "-DNATIVE_LIBS_DIR=$_rebased_native_libs_dir",
+ "-DOUT_DIR=",
+ "-DUNSIGNED_APK_PATH=$_rebased_packaged_apk_path",
+ "-DEMMA_INSTRUMENT=0",
+ "-DEMMA_DEVICE_JAR=$_rebased_emma_jar",
+ "-DDEX_FILE_PATH=$_rebased_dex_path",
+
+ "-Dbasedir=.",
+ "-buildfile", rebase_path(_ant_script, root_build_dir)
+ ]
+ }
+
+ action("${target_name}__finalize") {
+ script = "//build/android/gyp/finalize_apk.py"
+ depfile = "$target_gen_dir/$target_name.d"
+
+ sources = [_packaged_apk_path]
+ source_prereqs = [_keystore_path]
+ outputs = [depfile, _final_apk_path]
+
+ args = [
+ "--depfile", rebase_path(depfile, root_build_dir),
+ "--zipalign-path", rebase_path(zipalign_path, root_build_dir),
+ "--unsigned-apk-path", rebase_path(_packaged_apk_path, root_build_dir),
+ "--final-apk-path", rebase_path(_final_apk_path, root_build_dir),
+ "--key-path", rebase_path(_keystore_path, root_build_dir),
+ "--key-name", _keystore_name,
+ "--key-passwd", _keystore_password,
+ ]
+ }
+
+ group(target_name) {
+ deps = [":${target_name}__finalize"]
+ }
+}
+
+# Compiles and jars a set of java files.
+#
+# Outputs:
+# $jar_path.jar
+# $jar_path.jar.TOC
+#
+# Variables
+# java_files: List of .java files to compile.
+# java_deps: List of java dependencies. These should all have a .jar output
+# at "${target_gen_dir}/${target_name}.jar.
+# chromium_code: If 1, enable extra warnings.
+# srcjar_deps: List of srcjar dependencies. The .java files contained in the
+# dependencies srcjar outputs will be compiled and added to the output jar.
+# jar_path: Use this to explicitly set the output jar path. Defaults to
+# "${target_gen_dir}/${target_name}.jar.
+template("java_library") {
+ assert(defined(invoker.java_files))
+ assert(defined(invoker.build_config))
+ assert(defined(invoker.jar_path))
+
+ java_files = invoker.java_files
+ jar_path = invoker.jar_path
+ jar_toc_path = jar_path + ".TOC"
+
+ build_config = invoker.build_config
+
+ jar_excluded_patterns = []
+ if (defined(invoker.jar_excluded_patterns)) {
+ jar_excluded_patterns += invoker.jar_excluded_patterns
+ }
+
+ chromium_code = false
+ if (defined(invoker.chromium_code)) {
+ chromium_code = chromium_code || invoker.chromium_code
+ }
+
+ srcjar_deps = []
+ if (defined(invoker.srcjar_deps)) {
+ srcjar_deps += invoker.srcjar_deps
+ }
+
+ java_srcjars = []
+ foreach(dep, srcjar_deps) {
+ dep_gen_dir = get_label_info(dep, "target_gen_dir")
+ dep_name = get_label_info(dep, "name")
+ java_srcjars += [ "$dep_gen_dir/$dep_name.srcjar" ]
+ }
+ # Mark srcjar_deps as used.
+ assert(srcjar_deps == [] || srcjar_deps != [])
+
+ rebase_jar_path = rebase_path(jar_path, root_build_dir)
+
+ system_jars = [ "${android_sdk}/android.jar" ]
+ action("${target_name}__javac") {
+ script = "//build/android/gyp/javac.py"
+ depfile = "$target_gen_dir/$target_name.d"
+ outputs = [
+ depfile,
+ jar_path,
+ jar_path + ".md5.stamp"
+ ]
+ sources = java_files + java_srcjars
+ inputs = system_jars + [ build_config ]
+
+ rebase_system_jars = rebase_path(system_jars, root_build_dir)
+ rebase_java_srcjars = rebase_path(java_srcjars, root_build_dir)
+ rebase_build_config = rebase_path(build_config, root_build_dir)
+ rebase_depfile = rebase_path(depfile, root_build_dir)
+ args = [
+ "--depfile=$rebase_depfile",
+ "--classpath=$rebase_system_jars",
+ "--classpath=@FileArg($rebase_build_config:javac:classpath)",
+ "--jar-path=$rebase_jar_path",
+ "--java-srcjars=$rebase_java_srcjars",
+ "--java-srcjars=@FileArg($rebase_build_config:javac:srcjars)",
+ "--jar-excluded-classes=$jar_excluded_patterns",
+ ]
+ if (chromium_code) {
+ args += [ "--chromium-code" ]
+ }
+
+ args += rebase_path(java_files, root_build_dir)
+ }
+
+ # TODO(cjhopman): proguard
+
+ rebase_jar_toc_path = rebase_path(jar_toc_path, root_build_dir)
+ action("${target_name}__jar_toc") {
+ script = "//build/android/gyp/jar_toc.py"
+ depfile = "$target_gen_dir/$target_name.d"
+ outputs = [
+ depfile,
+ jar_toc_path,
+ jar_toc_path + ".md5.stamp"
+ ]
+ inputs = [ jar_path ]
+ args = [
+ "--depfile", rebase_path(depfile, root_build_dir),
+ "--jar-path=${rebase_jar_path}",
+ "--toc-path=${rebase_jar_toc_path}",
+ ]
+ }
+
+ group(target_name) {
+ deps = [
+ ":${target_name}__javac",
+ ":${target_name}__jar_toc",
+ ]
+ }
+}
+
+
+# This adds Android-specific parts to the java_library template.
+#
+# Runs Android lint against the compiled java files.
+# Dexes the output jar for inclusion in an APK.
+template("android_java_library") {
+ assert(defined(invoker.java_files))
+ assert(defined(invoker.build_config))
+ assert(defined(invoker.jar_path))
+ assert(defined(invoker.dex_path))
+
+ _jar_path = invoker.jar_path
+ _dex_path = invoker.dex_path
+
+ java_library("${target_name}__java_library") {
+ jar_path = _jar_path
+ if (defined(invoker.jar_excluded_patterns)) {
+ jar_excluded_patterns = invoker.jar_excluded_patterns
+ }
+ build_config = invoker.build_config
+ java_files = invoker.java_files
+
+ if (defined(invoker.srcjar_deps)) {
+ srcjar_deps = invoker.srcjar_deps
+ }
+ }
+
+ # TODO(cjhopman): lint
+
+
+ dex("${target_name}__dex") {
+ sources = [_jar_path]
+ output = _dex_path
+ }
+
+ group(target_name) {
+ deps = [
+ ":${target_name}__java_library",
+ ":${target_name}__dex",
+ ]
+ }
+}
+
+
+# Runs process_resources.py
+template("process_resources") {
+ zip_path = invoker.zip_path
+ srcjar_path = invoker.srcjar_path
+ build_config = invoker.build_config
+ resource_dirs = invoker.resource_dirs
+ android_manifest = invoker.android_manifest
+
+ non_constant_id = true
+ if (defined(invoker.generate_constant_ids) && invoker.generate_constant_ids) {
+ non_constant_id = false
+ }
+
+ action(target_name) {
+ script = "//build/android/gyp/process_resources.py"
+
+ depfile = "$target_gen_dir/$target_name.d"
+ outputs = [
+ depfile,
+ zip_path,
+ srcjar_path,
+ ]
+
+ sources_build_rel = exec_script(
+ "//build/android/gyp/find.py",
+ rebase_path(resource_dirs, root_build_dir),
+ "list lines"
+ )
+ sources = rebase_path(sources_build_rel, ".", root_build_dir)
+
+ source_prereqs = [
+ build_config,
+ android_manifest,
+ ]
+
+ rebase_resource_dirs = rebase_path(resource_dirs, root_build_dir)
+ rebase_build_config = rebase_path(build_config, root_build_dir)
+ args = [
+ "--depfile", rebase_path(depfile, root_build_dir),
+ "--android-sdk", rebase_path(android_sdk, root_build_dir),
+ "--android-sdk-tools", rebase_path(android_sdk_build_tools, root_build_dir),
+ "--android-manifest", rebase_path(android_manifest, root_build_dir),
+
+ "--resource-dirs=$rebase_resource_dirs",
+ "--srcjar-out", rebase_path(srcjar_path, root_build_dir),
+ "--resource-zip-out", rebase_path(zip_path, root_build_dir),
+
+ "--dependencies-res-zips=@FileArg($rebase_build_config:resources:dependency_zips)",
+ ]
+
+ if (non_constant_id) {
+ args += [ "--non-constant-id" ]
+ }
+
+ if (defined(invoker.custom_package)) {
+ args += [
+ "--custom-package", invoker.custom_package,
+ ]
+ }
+
+ if (defined(invoker.v14_verify_only) && invoker.v14_verify_only) {
+ args += ["--v14-verify-only"]
+ }
+
+ if (defined(invoker.all_resources_zip_path)) {
+ all_resources_zip = invoker.all_resources_zip_path
+ outputs += [ all_resources_zip ]
+ args += [
+ "--all-resources-zip-out", rebase_path(all_resources_zip, root_build_dir)
+ ]
+ }
+
+ if (defined(invoker.args)) {
+ args += invoker.args
+ }
+ }
+}