diff options
Diffstat (limited to 'third_party/libwebrtc/build/config/android/internal_rules.gni')
-rw-r--r-- | third_party/libwebrtc/build/config/android/internal_rules.gni | 4368 |
1 files changed, 4368 insertions, 0 deletions
diff --git a/third_party/libwebrtc/build/config/android/internal_rules.gni b/third_party/libwebrtc/build/config/android/internal_rules.gni new file mode 100644 index 0000000000..3ed306a9e7 --- /dev/null +++ b/third_party/libwebrtc/build/config/android/internal_rules.gni @@ -0,0 +1,4368 @@ +# 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. + +# Do not add any imports to non-//build directories here. +# Some projects (e.g. V8) do not have non-build directories DEPS'ed in. +import("//build/config/android/config.gni") +import("//build/config/android/copy_ex.gni") +import("//build/config/compiler/compiler.gni") +import("//build/config/compute_inputs_for_analyze.gni") +import("//build/config/coverage/coverage.gni") +import("//build/config/python.gni") +import("//build/config/sanitizers/sanitizers.gni") +import("//build/toolchain/goma.gni") +import("//build/toolchain/kythe.gni") +import("//build/util/generate_wrapper.gni") +import("//build_overrides/build.gni") +if (current_toolchain == default_toolchain) { + import("//build/toolchain/concurrent_links.gni") +} +assert(is_android) + +# The following _java_*_types variables capture all the existing target types. +# If a new type is introduced, please add it to one of these categories, +# preferring the more specific resource/library types. +_java_resource_types = [ + "android_assets", + "android_resources", +] + +_java_library_types = [ + "java_library", + "system_java_library", + "android_app_bundle_module", +] + +# These are leaf java target types. They cannot be passed as deps to other +# targets. Thus their naming schemes are not enforced. +_java_leaf_types = [ + "android_apk", + "android_app_bundle", + "dist_aar", + "dist_jar", + "java_annotation_processor", + "java_binary", + "junit_binary", +] + +# All _java_resource_types targets must conform to these patterns. +_java_resource_patterns = [ + "*:*_assets", + "*android*:assets", + "*:*_apk_*resources", + "*android*:resources", + "*:*_resources", + "*:*_grd", + "*:*locale_paks", + "*:*_java_strings", + "*:*strings_java", +] + +# All _java_library_types targets must conform to these patterns. This includes +# all non-leaf targets that use java_library_impl. +_java_library_patterns = [ + "*:*_java", + "*:*_javalib", + "*:*_java_*", # e.g. chrome_java_test_support + "*:java", + "*:junit", + "*:junit_*", + "*:*_junit_*", + "*:*javatests", + "*:*_bundle_module", + + # TODO(agrieve): Rename targets below to match above patterns. + "//android_webview/glue:glue", +] + +# These identify all non-leaf targets that have .build_config.json files. This is the +# set of patterns that other targets can use to filter out java targets. +java_target_patterns = _java_library_patterns + _java_resource_patterns + +_r8_path = "//third_party/r8/lib/r8.jar" +_custom_d8_path = "//third_party/r8/custom_d8.jar" +_desugar_jdk_libs_json = "//third_party/r8/desugar_jdk_libs.json" +_desugar_jdk_libs_jar = "//third_party/android_deps/libs/com_android_tools_desugar_jdk_libs/desugar_jdk_libs-1.1.1.jar" +_desugar_jdk_libs_configuration_jar = "//third_party/android_deps/libs/com_android_tools_desugar_jdk_libs_configuration/desugar_jdk_libs_configuration-1.1.1.jar" +_desugar_runtime_jar = "$root_build_dir/obj/third_party/bazel/desugar/Desugar_runtime.processed.jar" + +_dexdump_path = "$android_sdk_build_tools/dexdump" +_dexlayout_path = "//third_party/android_build_tools/art/dexlayout" +_profman_path = "//third_party/android_build_tools/art/profman" +_art_lib_file_names = [ + "libartbase.so", + "libart-compiler.so", + "libart-dexlayout.so", + "libart-disassembler.so", + "libart-gtest.so", + "libart.so", + "libbacktrace.so", + "libbase.so", + "libcrypto-host.so", + "libc++.so", + "libcutils.so", + "libdexfile.so", + "libexpat-host.so", + "libicui18n-host.so", + "libicuuc-host.so", + "libjavacore.so", + "libjavacrypto.so", + "liblog.so", + "liblz4.so", + "liblzma.so", + "libnativebridge.so", + "libnativehelper.so", + "libnativeloader.so", + "libopenjdkjvm.so", + "libopenjdkjvmti.so", + "libopenjdk.so", + "libprofile.so", + "libsigchain.so", + "libssl-host.so", + "libunwindstack.so", + "libvixl-arm64.so", + "libvixl-arm.so", + "libvixld-arm64.so", + "libvixld-arm.so", + "libz-host.so", + "libziparchive.so", + "slicer.so", +] +_default_art_libs = [] +foreach(lib, _art_lib_file_names) { + _default_art_libs += [ "//third_party/android_build_tools/art/lib/$lib" ] +} + +# Put the bug number in the target name so that false-positives have a hint in +# the error message about why non-existent dependencies are there. +build_config_target_suffix = "__build_config_crbug_908819" + +# Write the target's .build_config.json 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") { + _type = invoker.type + _parent_invoker = invoker.invoker + _target_label = + get_label_info(":${_parent_invoker.target_name}", "label_no_toolchain") + + # Ensure targets match naming patterns so that __assetres, __header, __impl + # targets work properly. Those generated targets allow for effective deps + # filtering. + if (filter_exclude([ _type ], _java_resource_types) == []) { + if (filter_exclude([ _target_label ], _java_resource_patterns) != []) { + assert(false, "Invalid java resource target name: $_target_label") + } + } else if (filter_exclude([ _type ], _java_library_types) == []) { + if (filter_exclude([ _target_label ], _java_library_patterns) != [] || + filter_exclude([ _target_label ], _java_resource_patterns) == []) { + assert(false, "Invalid java library target name: $_target_label") + } + } else if (_type == "group") { + if (filter_exclude([ _target_label ], java_target_patterns) != []) { + assert(false, "Invalid java target name: $_target_label") + } + } else if (filter_exclude([ _type ], _java_leaf_types) != []) { + assert(false, "This java type needs a category: $_type") + } + + if (defined(invoker.public_target_label)) { + _target_label = invoker.public_target_label + } + + action_with_pydeps(target_name) { + forward_variables_from(invoker, + [ + "deps", + "testonly", + ]) + if (!defined(deps)) { + deps = [] + } + if (defined(invoker.android_manifest_dep)) { + deps += [ invoker.android_manifest_dep ] + } + + script = "//build/android/gyp/write_build_config.py" + depfile = "$target_gen_dir/$target_name.d" + inputs = [] + outputs = [ invoker.build_config ] + + _deps_configs = [] + _public_deps_configs = [] + if (defined(invoker.possible_config_deps)) { + foreach(_possible_dep, invoker.possible_config_deps) { + _dep_label = get_label_info(_possible_dep, "label_no_toolchain") + if (filter_exclude([ _dep_label ], java_target_patterns) == []) { + # Put the bug number in the target name so that false-positives + # have a hint in the error message about non-existent dependencies. + deps += [ "$_dep_label$build_config_target_suffix" ] + _dep_gen_dir = get_label_info(_possible_dep, "target_gen_dir") + _dep_name = get_label_info(_possible_dep, "name") + _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json" + + _deps_configs += [ _dep_config ] + if (defined(invoker.possible_config_public_deps)) { + if (filter_include([ _possible_dep ], + invoker.possible_config_public_deps) != []) { + _public_deps_configs += [ _dep_config ] + } + } + } + } + } + _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir) + _rebased_public_deps_configs = + rebase_path(_public_deps_configs, root_build_dir) + + args = [ + "--type=$_type", + "--depfile", + rebase_path(depfile, root_build_dir), + "--deps-configs=$_rebased_deps_configs", + "--public-deps-configs=$_rebased_public_deps_configs", + "--build-config", + rebase_path(invoker.build_config, root_build_dir), + "--gn-target", + _target_label, + ] + + if (defined(invoker.ignore_dependency_public_deps) && + invoker.ignore_dependency_public_deps) { + args += [ "--ignore-dependency-public-deps" ] + } + + if (defined(invoker.aar_path)) { + args += [ + "--aar-path", + rebase_path(invoker.aar_path, root_build_dir), + ] + } + + if (defined(invoker.chromium_code) && !invoker.chromium_code) { + # Default to chromium code if invoker did not pass anything. + args += [ "--non-chromium-code" ] + } + + if (defined(invoker.device_jar_path)) { + args += [ + "--device-jar-path", + rebase_path(invoker.device_jar_path, root_build_dir), + ] + } + if (defined(invoker.host_jar_path)) { + args += [ + "--host-jar-path", + rebase_path(invoker.host_jar_path, root_build_dir), + ] + } + if (defined(invoker.unprocessed_jar_path)) { + args += [ + "--unprocessed-jar-path", + rebase_path(invoker.unprocessed_jar_path, root_build_dir), + ] + } + if (defined(invoker.ijar_path)) { + args += [ + "--interface-jar-path", + rebase_path(invoker.ijar_path, root_build_dir), + ] + } + if (defined(invoker.java_resources_jar)) { + args += [ + "--java-resources-jar-path", + rebase_path(invoker.java_resources_jar, root_build_dir), + ] + } + if (defined(invoker.annotation_processor_deps) && + invoker.annotation_processor_deps != []) { + _processor_configs = [] + foreach(_processor_dep, invoker.annotation_processor_deps) { + _dep_label = get_label_info(_processor_dep, "label_no_toolchain") + _dep_gen_dir = get_label_info(_processor_dep, "target_gen_dir") + _dep_name = get_label_info(_processor_dep, "name") + deps += [ "$_dep_label$build_config_target_suffix" ] + _processor_configs += [ "$_dep_gen_dir/$_dep_name.build_config.json" ] + } + _rebased_processor_configs = + rebase_path(_processor_configs, root_build_dir) + args += [ "--annotation-processor-configs=$_rebased_processor_configs" ] + } + + # Dex path for library targets, or the the intermediate library for apks. + if (defined(invoker.dex_path)) { + args += [ + "--dex-path", + rebase_path(invoker.dex_path, root_build_dir), + ] + } + + # Dex path for the final apk. + if (defined(invoker.final_dex_path)) { + args += [ + "--final-dex-path", + rebase_path(invoker.final_dex_path, root_build_dir), + ] + } + if (defined(invoker.supports_android) && invoker.supports_android) { + args += [ "--supports-android" ] + } + if (defined(invoker.requires_android) && invoker.requires_android) { + args += [ "--requires-android" ] + } + if (defined(invoker.is_prebuilt) && invoker.is_prebuilt) { + args += [ "--is-prebuilt" ] + } + if (defined(invoker.bypass_platform_checks) && + invoker.bypass_platform_checks) { + args += [ "--bypass-platform-checks" ] + } + + if (defined(invoker.apk_under_test)) { + deps += [ "${invoker.apk_under_test}$build_config_target_suffix" ] + apk_under_test_gen_dir = + get_label_info(invoker.apk_under_test, "target_gen_dir") + apk_under_test_name = get_label_info(invoker.apk_under_test, "name") + apk_under_test_config = + "$apk_under_test_gen_dir/$apk_under_test_name.build_config.json" + args += [ + "--tested-apk-config", + rebase_path(apk_under_test_config, root_build_dir), + ] + } + + if (defined(invoker.asset_sources)) { + _rebased_asset_sources = + rebase_path(invoker.asset_sources, root_build_dir) + args += [ "--asset-sources=$_rebased_asset_sources" ] + } + if (defined(invoker.asset_renaming_sources)) { + _rebased_asset_renaming_sources = + rebase_path(invoker.asset_renaming_sources, root_build_dir) + args += [ "--asset-renaming-sources=$_rebased_asset_renaming_sources" ] + + # These are zip paths, so no need to rebase. + args += [ + "--asset-renaming-destinations=${invoker.asset_renaming_destinations}", + ] + } + if (defined(invoker.disable_compression) && invoker.disable_compression) { + args += [ "--disable-asset-compression" ] + } + if (defined(invoker.treat_as_locale_paks) && invoker.treat_as_locale_paks) { + args += [ "--treat-as-locale-paks" ] + } + + if (defined(invoker.merged_android_manifest)) { + args += [ + "--merged-android-manifest", + rebase_path(invoker.merged_android_manifest, root_build_dir), + ] + } + if (defined(invoker.android_manifest)) { + inputs += [ invoker.android_manifest ] + args += [ + "--android-manifest", + rebase_path(invoker.android_manifest, root_build_dir), + ] + } + if (defined(invoker.resources_zip)) { + args += [ + "--resources-zip", + rebase_path(invoker.resources_zip, root_build_dir), + ] + } + + if (defined(invoker.resource_overlay) && invoker.resource_overlay) { + args += [ "--resource-overlay" ] + } + + if (defined(invoker.custom_package)) { + args += [ + "--package-name", + invoker.custom_package, + ] + } + if (defined(invoker.r_text)) { + args += [ + "--r-text-path", + rebase_path(invoker.r_text, root_build_dir), + ] + } + if (defined(invoker.res_size_info_path)) { + args += [ + "--res-size-info", + rebase_path(invoker.res_size_info_path, root_build_dir), + ] + } + if (defined(invoker.res_sources_path)) { + _res_sources_path = rebase_path(invoker.res_sources_path, root_build_dir) + args += [ "--res-sources-path=$_res_sources_path" ] + } + if (defined(invoker.proto_resources_path)) { + _rebased_proto_resources = + rebase_path(invoker.proto_resources_path, root_build_dir) + args += [ "--apk-proto-resources=$_rebased_proto_resources" ] + } + if (defined(invoker.r_text_path)) { + _rebased_rtxt_path = rebase_path(invoker.r_text_path, root_build_dir) + args += [ "--r-text-path=$_rebased_rtxt_path" ] + } + if (defined(invoker.module_pathmap_path)) { + _rebased_pathmap_path = + rebase_path(invoker.module_pathmap_path, root_build_dir) + args += [ "--module-pathmap-path=$_rebased_pathmap_path" ] + } + + if (defined(invoker.shared_libraries_runtime_deps_file)) { + # Don't list shared_libraries_runtime_deps_file as an input in order to + # avoid having to depend on the runtime_deps target. See comment in + # rules.gni for why we do this. + args += [ + "--shared-libraries-runtime-deps", + rebase_path(invoker.shared_libraries_runtime_deps_file, root_build_dir), + ] + } + + if (defined(invoker.base_allowlist_rtxt_path)) { + args += [ + "--base-allowlist-rtxt-path", + rebase_path(invoker.base_allowlist_rtxt_path, root_build_dir), + ] + } + + if (defined(invoker.is_base_module) && invoker.is_base_module) { + args += [ "--is-base-module" ] + } + + if (defined(invoker.loadable_modules)) { + _rebased_loadable_modules = + rebase_path(invoker.loadable_modules, root_build_dir) + args += [ "--loadable-modules=$_rebased_loadable_modules" ] + } + + if (defined(invoker.secondary_abi_shared_libraries_runtime_deps_file)) { + # Don't list secondary_abi_shared_libraries_runtime_deps_file as an + # input in order to avoid having to depend on the runtime_deps target. + # See comment in rules.gni for why we do this. + args += [ + "--secondary-abi-shared-libraries-runtime-deps", + rebase_path(invoker.secondary_abi_shared_libraries_runtime_deps_file, + root_build_dir), + ] + } + + if (defined(invoker.secondary_abi_loadable_modules) && + invoker.secondary_abi_loadable_modules != []) { + _rebased_secondary_abi_loadable_modules = + rebase_path(invoker.secondary_abi_loadable_modules, root_build_dir) + args += [ "--secondary-abi-loadable-modules=$_rebased_secondary_abi_loadable_modules" ] + } + + if (defined(invoker.native_lib_placeholders) && + invoker.native_lib_placeholders != []) { + args += [ "--native-lib-placeholders=${invoker.native_lib_placeholders}" ] + } + + if (defined(invoker.secondary_native_lib_placeholders) && + invoker.secondary_native_lib_placeholders != []) { + args += [ "--secondary-native-lib-placeholders=${invoker.secondary_native_lib_placeholders}" ] + } + + if (defined(invoker.uncompress_shared_libraries) && + invoker.uncompress_shared_libraries) { + args += [ "--uncompress-shared-libraries" ] + } + + if (defined(invoker.library_always_compress)) { + args += [ "--library-always-compress=${invoker.library_always_compress}" ] + } + + if (defined(invoker.library_renames)) { + args += [ "--library-renames=${invoker.library_renames}" ] + } + + if (defined(invoker.apk_path)) { + # TODO(tiborg): Remove APK path from build config and use + # install_artifacts from metadata instead. + _rebased_apk_path = rebase_path(invoker.apk_path, root_build_dir) + args += [ "--apk-path=$_rebased_apk_path" ] + if (defined(invoker.incremental_apk_path)) { + _rebased_incremental_apk_path = + rebase_path(invoker.incremental_apk_path, root_build_dir) + _rebased_incremental_install_json_path = + rebase_path(invoker.incremental_install_json_path, root_build_dir) + args += [ + "--incremental-install-json-path=$_rebased_incremental_install_json_path", + "--incremental-apk-path=$_rebased_incremental_apk_path", + ] + } + } + + if (defined(invoker.java_sources_file)) { + args += [ + "--java-sources-file", + rebase_path(invoker.java_sources_file, root_build_dir), + ] + } + if (defined(invoker.srcjar)) { + args += [ + "--srcjar", + rebase_path(invoker.srcjar, root_build_dir), + ] + } + if (defined(invoker.bundled_srcjars)) { + _rebased_bundled_srcjars = + rebase_path(invoker.bundled_srcjars, root_build_dir) + args += [ "--bundled-srcjars=$_rebased_bundled_srcjars" ] + } + if (defined(invoker.proguard_enabled) && invoker.proguard_enabled) { + args += [ "--proguard-enabled" ] + } + if (defined(invoker.proguard_mapping_path)) { + _rebased_proguard_mapping_path = + rebase_path(invoker.proguard_mapping_path, root_build_dir) + args += [ "--proguard-mapping-path=$_rebased_proguard_mapping_path" ] + } + if (defined(invoker.input_jars_paths)) { + _rebased_input_jars_paths = + rebase_path(invoker.input_jars_paths, root_build_dir) + args += [ "--extra-classpath-jars=$_rebased_input_jars_paths" ] + } + if (defined(invoker.low_classpath_priority) && + invoker.low_classpath_priority) { + args += [ "--low-classpath-priority" ] + } + if (defined(invoker.mergeable_android_manifests)) { + _rebased_mergeable_android_manifests = + rebase_path(invoker.mergeable_android_manifests, root_build_dir) + args += [ + "--mergeable-android-manifests=$_rebased_mergeable_android_manifests", + ] + } + if (defined(invoker.proguard_configs)) { + _rebased_proguard_configs = + rebase_path(invoker.proguard_configs, root_build_dir) + args += [ "--proguard-configs=$_rebased_proguard_configs" ] + } + if (defined(invoker.static_library_dependent_targets)) { + _dependent_configs = [] + foreach(_dep, invoker.static_library_dependent_targets) { + _dep_name = _dep.name + _dep_label = get_label_info(_dep_name, "label_no_toolchain") + deps += [ "$_dep_label$build_config_target_suffix" ] + _dep_gen_dir = get_label_info(_dep_name, "target_gen_dir") + _dep_name = get_label_info(_dep_name, "name") + _config = rebase_path("$_dep_gen_dir/$_dep_name.build_config.json", + root_build_dir) + _dependent_configs += [ _config ] + } + args += [ "--static-library-dependent-configs=$_dependent_configs" ] + } + if (defined(invoker.gradle_treat_as_prebuilt) && + invoker.gradle_treat_as_prebuilt) { + args += [ "--gradle-treat-as-prebuilt" ] + } + if (defined(invoker.main_class)) { + args += [ + "--main-class", + invoker.main_class, + ] + } + if (defined(invoker.base_module_target)) { + _base_label = + get_label_info(invoker.base_module_target, "label_no_toolchain") + _dep_gen_dir = get_label_info(_base_label, "target_gen_dir") + _dep_name = get_label_info(_base_label, "name") + deps += [ "$_base_label$build_config_target_suffix" ] + _base_module_build_config = "$_dep_gen_dir/$_dep_name.build_config.json" + inputs += [ _base_module_build_config ] + args += [ + "--base-module-build-config", + rebase_path(_base_module_build_config, root_build_dir), + ] + } + if (defined(invoker.module_build_configs)) { + inputs += invoker.module_build_configs + _rebased_configs = + rebase_path(invoker.module_build_configs, root_build_dir) + args += [ "--module-build-configs=$_rebased_configs" ] + } + if (defined(invoker.version_name)) { + args += [ + "--version-name", + invoker.version_name, + ] + } + if (defined(invoker.version_code)) { + args += [ + "--version-code", + invoker.version_code, + ] + } + if (defined(invoker.recursive_resource_deps) && + invoker.recursive_resource_deps) { + args += [ "--recursive-resource-deps" ] + } + if (current_toolchain != default_toolchain) { + # This has to be a built-time error rather than a GN assert because many + # packages have a mix of java and non-java targets. For example, the + # following would fail even though nothing depends on :bar(//baz): + # + # shared_library("foo") { + # } + # + # android_library("bar") { + # deps = [ ":foo(//baz)" ] + # assert(current_toolchain == default_toolchain) + # } + _msg = [ + "Tried to build an Android target in a non-default toolchain.", + "target: $_target_label", + "current_toolchain: $current_toolchain", + "default_toolchain: $default_toolchain", + ] + args += [ "--fail=$_msg" ] + } + } +} + +template("generate_android_wrapper") { + generate_wrapper(target_name) { + forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY) + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + generator_script = "//build/android/gyp/generate_android_wrapper.py" + sources = [ + "//build/android/gyp/util/build_utils.py", + "//build/gn_helpers.py", + "//build/util/generate_wrapper.py", + ] + } +} + +template("generate_r_java") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ]) + if (!defined(deps)) { + deps = [] + } + if (defined(invoker.possible_resource_deps)) { + foreach(_dep, invoker.possible_resource_deps) { + _target_label = get_label_info(_dep, "label_no_toolchain") + if (filter_exclude([ _target_label ], _java_library_patterns) == [] && + filter_exclude([ _target_label ], _java_resource_patterns) != []) { + # Depend on the java libraries' transitive __assetres target instead. + # This is required to ensure depending on java_groups works. + deps += [ "${_target_label}__assetres" ] + } else { + deps += [ _dep ] + } + } + } + depfile = "$target_gen_dir/${invoker.target_name}.d" + inputs = [ invoker.build_config ] + outputs = [ invoker.srcjar_path ] + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + script = "//build/android/gyp/create_r_java.py" + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--srcjar-out", + rebase_path(invoker.srcjar_path, root_build_dir), + "--deps-rtxts=@FileArg($_rebased_build_config:deps_info:dependency_r_txt_files)", + "--r-package=${invoker.package}", + ] + } +} + +# Generates a script in the build bin directory which runs the test +# target using the test runner script in build/android/test_runner.py. +template("test_runner_script") { + testonly = true + _test_name = invoker.test_name + _test_type = invoker.test_type + _incremental_apk = defined(invoker.incremental_apk) && invoker.incremental_apk + + _runtime_deps = + !defined(invoker.ignore_all_data_deps) || !invoker.ignore_all_data_deps + + if (_runtime_deps) { + # This runtime_deps file is used at runtime and thus cannot go in + # target_gen_dir. + _target_dir_name = get_label_info(":$target_name", "dir") + _runtime_deps_file = + "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.runtime_deps" + _runtime_deps_target = "${target_name}__write_deps" + group(_runtime_deps_target) { + forward_variables_from(invoker, + [ + "data", + "deps", + "public_deps", + ]) + data_deps = [] + if (defined(invoker.data_deps)) { + data_deps += invoker.data_deps + } + if (defined(invoker.additional_apks)) { + data_deps += invoker.additional_apks + } + write_runtime_deps = _runtime_deps_file + } + } + + if (defined(invoker.apk_under_test)) { + _install_artifacts_json = + "${target_gen_dir}/${target_name}.install_artifacts" + generated_file("${target_name}__install_artifacts") { + deps = [ invoker.apk_under_test ] + output_conversion = "json" + outputs = [ _install_artifacts_json ] + data_keys = [ "install_artifacts" ] + walk_keys = [ "install_artifacts_barrier" ] + rebase = root_build_dir + } + } + + generate_android_wrapper(target_name) { + wrapper_script = "$root_build_dir/bin/run_${_test_name}" + + executable = "//testing/test_env.py" + + if (defined(android_test_runner_script)) { + _runner_script = android_test_runner_script + } else { + _runner_script = "//build/android/test_runner.py" + } + + deps = [] + if (defined(invoker.deps)) { + deps = invoker.deps + } + data_deps = [ + "//build/android:test_runner_py", + "//testing:test_scripts_shared", + ] + if (defined(invoker.data_deps)) { + data_deps += invoker.data_deps + } + data = [] + if (defined(invoker.data)) { + data += invoker.data + } + + executable_args = [ + "@WrappedPath(" + rebase_path(_runner_script, root_build_dir) + ")", + _test_type, + "--output-directory", + "@WrappedPath(.)", + ] + + if (_runtime_deps) { + deps += [ ":$_runtime_deps_target" ] + data += [ _runtime_deps_file ] + _rebased_runtime_deps_file = + rebase_path(_runtime_deps_file, root_build_dir) + executable_args += [ + "--runtime-deps-path", + "@WrappedPath(${_rebased_runtime_deps_file})", + ] + } + + # apk_target is not used for native executable tests + # (e.g. breakpad_unittests). + if (defined(invoker.apk_target)) { + assert(!defined(invoker.executable_dist_dir)) + deps += [ "${invoker.apk_target}$build_config_target_suffix" ] + _apk_build_config = + get_label_info(invoker.apk_target, "target_gen_dir") + "/" + + get_label_info(invoker.apk_target, "name") + ".build_config.json" + _rebased_apk_build_config = rebase_path(_apk_build_config, root_build_dir) + not_needed([ "_rebased_apk_build_config" ]) + } else if (_test_type == "gtest") { + assert( + defined(invoker.executable_dist_dir), + "Must define either apk_target or executable_dist_dir for test_runner_script()") + _rebased_executable_dist_dir = + rebase_path(invoker.executable_dist_dir, root_build_dir) + executable_args += [ + "--executable-dist-dir", + "@WrappedPath(${_rebased_executable_dist_dir})", + ] + } + + _device_test = true + if (_test_type == "gtest") { + assert(defined(invoker.test_suite)) + executable_args += [ + "--suite", + invoker.test_suite, + ] + if (use_clang_coverage) { + # Set a default coverage output directory (can be overridden by user + # passing the same flag). + _rebased_coverage_dir = + rebase_path("$root_out_dir/coverage", root_build_dir) + executable_args += [ + "--coverage-dir", + "@WrappedPath(${_rebased_coverage_dir})", + ] + } + } else if (_test_type == "instrumentation") { + _test_apk = "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:apk_path))" + if (_incremental_apk) { + _test_apk = "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:incremental_apk_path))" + } + _rebased_test_jar = rebase_path(invoker.test_jar, root_build_dir) + executable_args += [ + "--test-apk", + _test_apk, + "--test-jar", + "@WrappedPath(${_rebased_test_jar})", + ] + if (defined(invoker.apk_under_test)) { + if (_incremental_apk) { + deps += [ "${invoker.apk_under_test}$build_config_target_suffix" ] + _apk_under_test_build_config = + get_label_info(invoker.apk_under_test, "target_gen_dir") + "/" + + get_label_info(invoker.apk_under_test, "name") + + ".build_config.json" + _rebased_apk_under_test_build_config = + rebase_path(_apk_under_test_build_config, root_build_dir) + _apk_under_test = "@WrappedPath(@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_apk_path))" + } else { + deps += [ ":${target_name}__install_artifacts" ] + _rebased_install_artifacts_json = + rebase_path(_install_artifacts_json, root_build_dir) + _apk_under_test = + "@WrappedPath(@FileArg($_rebased_install_artifacts_json[]))" + } + executable_args += [ + "--apk-under-test", + _apk_under_test, + ] + } + if (defined(invoker.use_webview_provider)) { + deps += [ "${invoker.use_webview_provider}$build_config_target_suffix" ] + _build_config = + get_label_info(invoker.use_webview_provider, "target_gen_dir") + + "/" + get_label_info(invoker.use_webview_provider, "name") + + ".build_config.json" + _rebased_build_config = rebase_path(_build_config, root_build_dir) + executable_args += [ + "--use-webview-provider", + "@WrappedPath(@FileArg($_rebased_build_config:deps_info:apk_path))", + ] + } + if (defined(invoker.proguard_enabled) && invoker.proguard_enabled && + !_incremental_apk) { + executable_args += [ "--enable-java-deobfuscation" ] + } + if (use_jacoco_coverage) { + # Set a default coverage output directory (can be overridden by user + # passing the same flag). + _rebased_coverage_dir = + rebase_path("$root_out_dir/coverage", root_build_dir) + executable_args += [ + "--coverage-dir", + "@WrappedPath(${_rebased_coverage_dir})", + ] + } + } else if (_test_type == "junit") { + assert(defined(invoker.test_suite)) + _device_test = false + executable_args += [ + "--test-suite", + invoker.test_suite, + ] + + deps += [ ":${invoker.test_suite}$build_config_target_suffix" ] + _junit_binary_build_config = + "${target_gen_dir}/${invoker.test_suite}.build_config.json" + + _rebased_robolectric_runtime_deps_dir = + rebase_path("$root_build_dir/lib.java/third_party/robolectric", + root_build_dir) + _rebased_resource_apk = rebase_path(invoker.resource_apk, root_build_dir) + executable_args += [ + "--resource-apk", + "@WrappedPath(${_rebased_resource_apk})", + "--robolectric-runtime-deps-dir", + "@WrappedPath(${_rebased_robolectric_runtime_deps_dir})", + ] + if (use_jacoco_coverage) { + # Set a default coverage output directory (can be overridden by user + # passing the same flag). + _rebased_coverage_dir = + rebase_path("$root_out_dir/coverage", root_build_dir) + executable_args += [ + "--coverage-dir", + "@WrappedPath(${_rebased_coverage_dir})", + ] + } + } else if (_test_type == "linker") { + executable_args += [ + "--test-apk", + "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:apk_path))", + ] + } else { + assert(false, "Invalid test type: $_test_type.") + } + + if (defined(invoker.additional_apks)) { + foreach(additional_apk, invoker.additional_apks) { + deps += [ "$additional_apk$build_config_target_suffix" ] + _build_config = + get_label_info(additional_apk, "target_gen_dir") + "/" + + get_label_info(additional_apk, "name") + ".build_config.json" + _rebased_build_config = rebase_path(_build_config, root_build_dir) + executable_args += [ + "--additional-apk", + "@WrappedPath(@FileArg($_rebased_build_config:deps_info:apk_path))", + ] + } + } + if (defined(invoker.shard_timeout)) { + executable_args += [ "--shard-timeout=${invoker.shard_timeout}" ] + } + if (_incremental_apk) { + executable_args += [ + "--test-apk-incremental-install-json", + "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:incremental_install_json_path))", + ] + if (defined(invoker.apk_under_test)) { + executable_args += [ + "--apk-under-test-incremental-install-json", + "@WrappedPath(@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_install_json_path))", + ] + } + executable_args += [ "--fast-local-dev" ] + } + if (_device_test && is_asan) { + executable_args += [ "--tool=asan" ] + } + + if (defined(invoker.modules)) { + foreach(module, invoker.modules) { + executable_args += [ + "--module", + module, + ] + } + } + + if (defined(invoker.fake_modules)) { + foreach(fake_module, invoker.fake_modules) { + executable_args += [ + "--fake-module", + fake_module, + ] + } + } + + if (defined(invoker.additional_locales)) { + foreach(locale, invoker.additional_locales) { + executable_args += [ + "--additional-locale", + locale, + ] + } + } + + if (defined(invoker.extra_args)) { + executable_args += invoker.extra_args + } + } +} + +if (enable_java_templates) { + template("android_lint") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + if (!defined(deps)) { + deps = [] + } + + # https://crbug.com/1098752 Fix for bot OOM (https://crbug.com/1098333). + if (defined(java_cmd_pool_size)) { + pool = "//build/config/android:java_cmd_pool($default_toolchain)" + } else { + pool = "//build/toolchain:link_pool($default_toolchain)" + } + + # Lint requires generated sources and generated resources from the build. + # Turbine __header targets depend on all generated sources, and the + # __assetres targets depend on all generated resources. + if (defined(invoker.deps)) { + foreach(_dep, invoker.deps) { + _target_label = get_label_info(_dep, "label_no_toolchain") + if (filter_exclude([ _target_label ], _java_library_patterns) == [] && + filter_exclude([ _target_label ], _java_resource_patterns) != + []) { + deps += [ + "${_target_label}__assetres", + "${_target_label}__header", + ] + } else { + # Keep non-java deps as they may generate files used only by lint. + # e.g. generated suppressions.xml files. + deps += [ _dep ] + } + } + } + + if (defined(invoker.min_sdk_version)) { + _min_sdk_version = invoker.min_sdk_version + } else { + _min_sdk_version = default_min_sdk_version + } + + _lint_binary_path = "$lint_android_sdk_root/cmdline-tools/latest/bin/lint" + _cache_dir = "$root_build_dir/android_lint_cache" + + # Save generated xml files in a consistent location for debugging. + _lint_gen_dir = "$target_gen_dir/$target_name" + _backported_methods = "//third_party/r8/backported_methods.txt" + + script = "//build/android/gyp/lint.py" + depfile = "$target_gen_dir/$target_name.d" + inputs = [ + _lint_binary_path, + _backported_methods, + ] + + args = [ + "--target-name", + get_label_info(":${target_name}", "label_no_toolchain"), + "--depfile", + rebase_path(depfile, root_build_dir), + "--lint-binary-path", + rebase_path(_lint_binary_path, root_build_dir), + "--cache-dir", + rebase_path(_cache_dir, root_build_dir), + "--lint-gen-dir", + rebase_path(_lint_gen_dir, root_build_dir), + "--android-sdk-version=${lint_android_sdk_version}", + "--min-sdk-version=$_min_sdk_version", + "--android-sdk-root", + rebase_path(lint_android_sdk_root, root_build_dir), + "--backported-methods", + rebase_path(_backported_methods, root_build_dir), + ] + + if (defined(invoker.skip_build_server) && invoker.skip_build_server) { + # Nocompile tests need lint to fail through ninja. + args += [ "--skip-build-server" ] + } + + if (defined(invoker.lint_suppressions_file)) { + inputs += [ invoker.lint_suppressions_file ] + + args += [ + "--config-path", + rebase_path(invoker.lint_suppressions_file, root_build_dir), + ] + } + + if (defined(testonly) && testonly) { + # Allows us to ignore unnecessary checks when linting test targets. + args += [ "--testonly" ] + } + + if (defined(invoker.manifest_package)) { + args += [ "--manifest-package=${invoker.manifest_package}" ] + } + + if (treat_warnings_as_errors) { + args += [ "--warnings-as-errors" ] + } + + if (defined(invoker.lint_baseline_file)) { + if (compute_inputs_for_analyze) { + # The baseline file is included in lint.py as a depfile dep. Since + # removing it regenerates the file, it is useful to not have this as + # a gn input during local development. Add it only for bots' analyze. + inputs += [ invoker.lint_baseline_file ] + } + args += [ + # Baseline allows us to turn on lint warnings without fixing all the + # pre-existing issues. This stops the flood of new issues while the + # existing ones are being fixed. + "--baseline", + rebase_path(invoker.lint_baseline_file, root_build_dir), + ] + } + + if (defined(invoker.create_cache) && invoker.create_cache) { + # Putting the stamp file in the cache dir allows us to depend on ninja + # to create the cache dir for us. + _stamp_path = "$_cache_dir/build.lint.stamp" + args += [ "--create-cache" ] + } else { + _stamp_path = "$target_out_dir/$target_name/build.lint.stamp" + deps += [ + "//build/android:prepare_android_lint_cache", + invoker.build_config_dep, + ] + inputs += [ invoker.build_config ] + _rebased_build_config = + rebase_path(invoker.build_config, root_build_dir) + + args += [ + "--manifest-path=@FileArg($_rebased_build_config:deps_info:lint_android_manifest)", + "--extra-manifest-paths=@FileArg($_rebased_build_config:deps_info:lint_extra_android_manifests)", + + # Lint requires all source and all resource files to be passed in the + # same invocation for checks like UnusedResources. + "--java-sources=@FileArg($_rebased_build_config:deps_info:lint_java_sources)", + "--aars=@FileArg($_rebased_build_config:deps_info:lint_aars)", + "--srcjars=@FileArg($_rebased_build_config:deps_info:lint_srcjars)", + "--resource-sources=@FileArg($_rebased_build_config:deps_info:lint_resource_sources)", + "--resource-zips=@FileArg($_rebased_build_config:deps_info:lint_resource_zips)", + + # The full classpath is required for annotation checks like @IntDef. + "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)", + ] + } + + outputs = [ _stamp_path ] + args += [ + "--stamp", + rebase_path(_stamp_path, root_build_dir), + ] + } + } + + template("proguard") { + forward_variables_from(invoker, + TESTONLY_AND_VISIBILITY + [ + "data", + "data_deps", + "public_deps", + ]) + _script = "//build/android/gyp/proguard.py" + _deps = invoker.deps + + _inputs = [ + invoker.build_config, + _r8_path, + ] + if (defined(invoker.inputs)) { + _inputs += invoker.inputs + } + if (defined(invoker.proguard_mapping_path)) { + _mapping_path = invoker.proguard_mapping_path + } else { + _mapping_path = "${invoker.output_path}.mapping" + } + + _enable_jdk_library_desugaring = enable_jdk_library_desugaring + if (defined(invoker.supports_jdk_library_desugaring) && + !invoker.supports_jdk_library_desugaring) { + _enable_jdk_library_desugaring = false + } + + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + _args = [ + "--mapping-output", + rebase_path(_mapping_path, root_build_dir), + "--classpath", + "@FileArg($_rebased_build_config:deps_info:proguard_classpath_jars)", + "--classpath", + "@FileArg($_rebased_build_config:android:sdk_jars)", + "--r8-path", + rebase_path(_r8_path, root_build_dir), + ] + if (treat_warnings_as_errors) { + _args += [ "--warnings-as-errors" ] + } + if (defined(invoker.desugar_jars_paths)) { + _rebased_desugar_jars_paths = + rebase_path(invoker.desugar_jars_paths, root_build_dir) + args += [ "--classpath=${_rebased_desugar_jars_paths}" ] + } + + if ((!defined(invoker.proguard_enable_obfuscation) || + invoker.proguard_enable_obfuscation) && enable_proguard_obfuscation) { + _proguard_sourcefile_suffix = "" + if (defined(invoker.proguard_sourcefile_suffix)) { + _proguard_sourcefile_suffix = "-${invoker.proguard_sourcefile_suffix}" + } + + # This is generally the apk name, and serves to identify the mapping + # file that would be required to deobfuscate a stacktrace. + _mapping_id = get_path_info(_mapping_path, "name") + _args += [ + "--enable-obfuscation", + "--sourcefile", + "chromium-${_mapping_id}${_proguard_sourcefile_suffix}", + ] + } else if (defined(invoker.proguard_sourcefile_suffix)) { + not_needed(invoker, [ "proguard_sourcefile_suffix" ]) + } + + if (defined(invoker.modules)) { + foreach(_feature_module, invoker.modules) { + _rebased_module_build_config = + rebase_path(_feature_module.build_config, root_build_dir) + _args += [ + "--feature-name=${_feature_module.name}", + "--dex-dest=@FileArg($_rebased_module_build_config:final_dex:path)", + "--feature-jars=@FileArg($_rebased_module_build_config:deps_info:device_classpath)", + ] + if (defined(_feature_module.uses_split)) { + _args += [ "--uses-split=${_feature_module.name}:${_feature_module.uses_split}" ] + } + _deps += [ _feature_module.build_config_target ] + } + _stamp = "${target_gen_dir}/${target_name}.r8.stamp" + _outputs = [ _stamp ] + _output_arg = [ + "--stamp", + rebase_path(_stamp, root_build_dir), + ] + } else { + # We don't directly set the output arg on the _args variable since it is + # shared with the expectation target that uses its own stamp file and + # does not take an --output-path. + _output_arg = [ + "--output-path", + rebase_path(invoker.output_path, root_build_dir), + ] + _outputs = [ invoker.output_path ] + } + _outputs += [ _mapping_path ] + + if (defined(invoker.disable_r8_outlining) && invoker.disable_r8_outlining) { + _args += [ "--disable-outlining" ] + } + + if (defined(invoker.enable_proguard_checks) && + !invoker.enable_proguard_checks) { + _args += [ "--disable-checks" ] + } + + if (defined(invoker.is_static_library) && invoker.is_static_library) { + _args += [ + "--extra-mapping-output-paths", + "@FileArg($_rebased_build_config:deps_info:static_library_proguard_mapping_output_paths)", + ] + } + + if (_enable_jdk_library_desugaring) { + _args += [ + "--desugar-jdk-libs-json", + rebase_path(_desugar_jdk_libs_json, root_build_dir), + ] + _inputs += [ _desugar_jdk_libs_json ] + + _args += [ + "--desugar-jdk-libs-jar", + rebase_path(_desugar_jdk_libs_jar, root_build_dir), + "--desugar-jdk-libs-configuration-jar", + rebase_path(_desugar_jdk_libs_configuration_jar, root_build_dir), + ] + _inputs += [ + _desugar_jdk_libs_jar, + _desugar_jdk_libs_configuration_jar, + ] + + _desugared_library_keep_rule_output_path = + "$target_gen_dir/$target_name.desugared_library_keep_rules.flags" + _args += [ + "--desugared-library-keep-rule-output", + rebase_path(_desugared_library_keep_rule_output_path, root_build_dir), + ] + } + _ignore_desugar_missing_deps = + defined(invoker.ignore_desugar_missing_deps) && + invoker.ignore_desugar_missing_deps + if (!_ignore_desugar_missing_deps) { + _args += [ "--show-desugar-default-interface-warnings" ] + } + + if (enable_java_asserts) { + # The default for generating dex file format is + # --force-disable-assertions. + _args += [ "--force-enable-assertions" ] + } + + if (defined(invoker.args)) { + _args += invoker.args + } + + if (defined(invoker.expected_proguard_config)) { + _expectations_target = + "${invoker.top_target_name}_validate_proguard_config" + action_with_pydeps(_expectations_target) { + script = _script + + # Need to depend on all deps so that proguard.txt within .aar files get + # extracted. + deps = _deps + depfile = "${target_gen_dir}/${target_name}.d" + inputs = [ + invoker.build_config, + invoker.expected_proguard_config, + ] + _actual_file = "$target_gen_dir/$target_name.proguard_configs" + _failure_file = + "$expectations_failure_dir/" + + string_replace(invoker.expected_proguard_config, "/", "_") + outputs = [ + _actual_file, + _failure_file, + ] + args = _args + [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--failure-file", + rebase_path(_failure_file, root_build_dir), + "--expected-file", + rebase_path(invoker.expected_proguard_config, root_build_dir), + "--actual-file", + rebase_path(_actual_file, root_build_dir), + "--only-verify-expectations", + ] + if (defined(invoker.expected_proguard_config_base)) { + inputs += [ invoker.expected_proguard_config_base ] + args += [ + "--expected-file-base", + rebase_path(invoker.expected_proguard_config_base, root_build_dir), + ] + } + if (fail_on_android_expectations) { + args += [ "--fail-on-expectations" ] + } + } + _deps += [ ":$_expectations_target" ] + } + action_with_pydeps(target_name) { + script = _script + deps = _deps + inputs = _inputs + outputs = _outputs + depfile = "${target_gen_dir}/${target_name}.d" + args = _args + _output_arg + [ + "--depfile", + rebase_path(depfile, root_build_dir), + ] + + # http://crbug.com/725224. Fix for bots running out of memory. + if (defined(java_cmd_pool_size)) { + pool = "//build/config/android:java_cmd_pool($default_toolchain)" + } else { + pool = "//build/toolchain:link_pool($default_toolchain)" + } + } + } + + # Generates a script in the build bin directory to run a java binary. + # + # Variables + # main_class: The class containing the program entry point. + # build_config: Path to .build_config.json for the jar (contains classpath). + # script_name: Name of the script to generate. + # wrapper_script_args: List of extra arguments to pass to the executable. + # tiered_stop_at_level_one: Whether to pass --tiered-stop-at-level-one + # + template("java_binary_script") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ]) + + _main_class = invoker.main_class + _build_config = invoker.build_config + _script_name = invoker.script_name + + script = "//build/android/gyp/create_java_binary_script.py" + inputs = [ _build_config ] + _java_script = "$root_build_dir/bin/$_script_name" + outputs = [ _java_script ] + _rebased_build_config = rebase_path(_build_config, root_build_dir) + args = [ + "--output", + rebase_path(_java_script, root_build_dir), + "--main-class", + _main_class, + ] + args += [ + "--classpath=@FileArg($_rebased_build_config:deps_info:host_classpath)", + ] + + if (use_jacoco_coverage) { + args += [ + "--classpath", + rebase_path("//third_party/jacoco/lib/jacocoagent.jar", + root_build_dir), + ] + } + if (use_jacoco_coverage || !treat_warnings_as_errors) { + args += [ "--noverify" ] + } + if (defined(invoker.tiered_stop_at_level_one) && + invoker.tiered_stop_at_level_one) { + args += [ "--tiered-stop-at-level-one" ] + } + if (defined(invoker.wrapper_script_args)) { + args += [ "--" ] + invoker.wrapper_script_args + } + } + } + + # Variables + # apply_mapping: The path to the ProGuard mapping file to apply. + # disable_incremental: Disable incremental dexing. + template("dex") { + _min_sdk_version = default_min_sdk_version + if (defined(invoker.min_sdk_version)) { + _min_sdk_version = invoker.min_sdk_version + } + + _proguard_enabled = + defined(invoker.proguard_enabled) && invoker.proguard_enabled + _is_dex_merging = defined(invoker.input_dex_filearg) + _enable_multidex = + !defined(invoker.enable_multidex) || invoker.enable_multidex + _enable_main_dex_list = _enable_multidex && _min_sdk_version < 21 + _enable_desugar = !defined(invoker.enable_desugar) || invoker.enable_desugar + _use_classic_desugar = + defined(invoker.use_classic_desugar) && invoker.use_classic_desugar + _desugar_needs_classpath = _enable_desugar && !_use_classic_desugar + + # It's not safe to dex merge with libraries dex'ed at higher api versions. + assert(!_is_dex_merging || _min_sdk_version >= default_min_sdk_version) + + # For D8's backported method desugaring to work properly, the dex merge step + # must not be set to a higher minSdkVersion than it was for the libraries. + if (_enable_desugar && _is_dex_merging) { + _min_sdk_version = default_min_sdk_version + } + + assert(defined(invoker.output) || + (_proguard_enabled && defined(invoker.modules))) + assert(!_proguard_enabled || !(defined(invoker.input_dex_filearg) || + defined(invoker.input_classes_filearg) || + defined(invoker.input_class_jars)), + "Cannot explicitly set inputs when proguarding a dex.") + + # Dex merging should not also be dexing. + assert(!(_is_dex_merging && defined(invoker.input_classes_filearg))) + assert(!(_is_dex_merging && defined(invoker.input_class_jars))) + + assert(!(defined(invoker.apply_mapping) && !_proguard_enabled), + "apply_mapping can only be specified if proguard is enabled.") + + if (_enable_main_dex_list) { + _main_dex_rules = "//build/android/main_dex_classes.flags" + } + + if (_desugar_needs_classpath || _proguard_enabled) { + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + } + + if (_proguard_enabled) { + _proguard_target_name = target_name + + proguard(_proguard_target_name) { + forward_variables_from(invoker, + TESTONLY_AND_VISIBILITY + [ + "build_config", + "data", + "data_deps", + "deps", + "desugar_jars_paths", + "disable_r8_outlining", + "enable_proguard_checks", + "expected_proguard_config", + "expected_proguard_config_base", + "ignore_desugar_missing_deps", + "is_static_library", + "modules", + "proguard_enable_obfuscation", + "proguard_mapping_path", + "proguard_sourcefile_suffix", + "supports_jdk_library_desugaring", + "top_target_name", + ]) + inputs = [] + if (defined(invoker.inputs)) { + inputs += invoker.inputs + } + if (defined(invoker.proguard_configs)) { + inputs += invoker.proguard_configs + } + + args = [ + "--proguard-configs=@FileArg($_rebased_build_config:deps_info:proguard_all_configs)", + "--min-api=$_min_sdk_version", + ] + if (defined(invoker.has_apk_under_test) && invoker.has_apk_under_test) { + args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:device_classpath_extended)" ] + } else { + args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:device_classpath)" ] + } + if (_use_classic_desugar) { + deps += [ "//third_party/bazel/desugar:desugar_runtime_java" ] + inputs += [ _desugar_runtime_jar ] + args += [ + "--input-paths", + rebase_path(_desugar_runtime_jar, root_build_dir), + ] + } + if (defined(invoker.proguard_args)) { + args += invoker.proguard_args + } + + if (defined(invoker.apply_mapping)) { + _rebased_apply_mapping_path = + rebase_path(invoker.apply_mapping, root_build_dir) + args += [ "--apply-mapping=$_rebased_apply_mapping_path" ] + } + + if (_enable_main_dex_list) { + if (defined(invoker.extra_main_dex_proguard_config)) { + args += [ + "--main-dex-rules-path", + rebase_path(invoker.extra_main_dex_proguard_config, + root_build_dir), + ] + inputs += [ invoker.extra_main_dex_proguard_config ] + } + args += [ + "--main-dex-rules-path", + rebase_path(_main_dex_rules, root_build_dir), + ] + inputs += [ _main_dex_rules ] + } + + if (defined(invoker.output)) { + output_path = invoker.output + } else if (!defined(proguard_mapping_path)) { + proguard_mapping_path = "$target_out_dir/$target_name.mapping" + } + } + } else { # !_proguard_enabled + _is_library = defined(invoker.is_library) && invoker.is_library + _input_class_jars = [] + if (defined(invoker.input_class_jars)) { + _input_class_jars = invoker.input_class_jars + } + _deps = invoker.deps + + if (!_is_library && _use_classic_desugar) { + # It would be more efficient to use the pre-dex'ed copy of the runtime, + # but it's easier to add it in this way. + _deps += [ "//third_party/bazel/desugar:desugar_runtime_java" ] + _input_class_jars += [ _desugar_runtime_jar ] + } + if (_input_class_jars != []) { + _rebased_input_class_jars = + rebase_path(_input_class_jars, root_build_dir) + } + + action_with_pydeps(target_name) { + forward_variables_from(invoker, + TESTONLY_AND_VISIBILITY + [ + "data", + "data_deps", + ]) + script = "//build/android/gyp/dex.py" + deps = _deps + depfile = "$target_gen_dir/$target_name.d" + outputs = [ invoker.output ] + inputs = [ + _r8_path, + _custom_d8_path, + ] + + if (!_is_library) { + # http://crbug.com/725224. Fix for bots running out of memory. + if (defined(java_cmd_pool_size)) { + pool = "//build/config/android:java_cmd_pool($default_toolchain)" + } else { + pool = "//build/toolchain:link_pool($default_toolchain)" + } + } + + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--output", + rebase_path(outputs[0], root_build_dir), + "--min-api=$_min_sdk_version", + "--r8-jar-path", + rebase_path(_r8_path, root_build_dir), + "--custom-d8-jar-path", + rebase_path(_custom_d8_path, root_build_dir), + + # Uncomment when rebuilding custom_d8.jar. + #"--skip-custom-d8", + ] + if (treat_warnings_as_errors) { + args += [ "--warnings-as-errors" ] + } + + if (enable_incremental_d8 && !(defined(invoker.disable_incremental) && + invoker.disable_incremental)) { + # Don't use incremental dexing for ProGuarded inputs as a precaution. + args += [ + "--incremental-dir", + rebase_path("$target_out_dir/$target_name", root_build_dir), + ] + } + + if (_enable_multidex) { + args += [ "--multi-dex" ] + if (_enable_main_dex_list) { + if (defined(invoker.extra_main_dex_proguard_config)) { + args += [ + "--main-dex-rules-path", + rebase_path(invoker.extra_main_dex_proguard_config, + root_build_dir), + ] + inputs += [ invoker.extra_main_dex_proguard_config ] + } + args += [ + "--main-dex-rules-path", + rebase_path(_main_dex_rules, root_build_dir), + ] + inputs += [ _main_dex_rules ] + } + } + if (_is_library) { + args += [ "--library" ] + } + if (defined(invoker.input_dex_filearg)) { + inputs += [ invoker.build_config ] + args += [ "--dex-inputs-filearg=${invoker.input_dex_filearg}" ] + } + if (defined(invoker.input_classes_filearg)) { + inputs += [ invoker.build_config ] + args += [ "--class-inputs-filearg=${invoker.input_classes_filearg}" ] + } + if (_input_class_jars != []) { + inputs += _input_class_jars + args += [ "--class-inputs=${_rebased_input_class_jars}" ] + } + + if (defined(invoker.dexlayout_profile)) { + args += [ + "--dexlayout-profile", + rebase_path(invoker.dexlayout_profile, root_build_dir), + "--dexlayout-path", + rebase_path(_dexlayout_path, root_build_dir), + "--profman-path", + rebase_path(_profman_path, root_build_dir), + "--dexdump-path", + rebase_path(_dexdump_path, root_build_dir), + ] + inputs += [ + _dexlayout_path, + _profman_path, + _dexdump_path, + invoker.dexlayout_profile, + ] + inputs += _default_art_libs + } + + # Never compile intemediates with --release in order to: + # 1) not require recompiles when toggling is_java_debug, + # 2) allow incremental_install=1 to still have local variable + # information even when is_java_debug=false. + if (!is_java_debug && !_is_library) { + args += [ "--release" ] + } + + if (_enable_desugar) { + args += [ "--desugar" ] + + # Passing the flag for dex merging causes invalid dex files to be created. + if (enable_jdk_library_desugaring && !_is_dex_merging) { + inputs += [ _desugar_jdk_libs_json ] + args += [ + "--desugar-jdk-libs-json", + rebase_path(_desugar_jdk_libs_json, root_build_dir), + ] + } + _ignore_desugar_missing_deps = + defined(invoker.ignore_desugar_missing_deps) && + invoker.ignore_desugar_missing_deps + if (!_ignore_desugar_missing_deps && !_use_classic_desugar) { + args += [ "--show-desugar-default-interface-warnings" ] + } + } + if (_desugar_needs_classpath) { + _desugar_dependencies_path = + "$target_gen_dir/$target_name.desugardeps" + args += [ + "--desugar-dependencies", + rebase_path(_desugar_dependencies_path, root_build_dir), + "--bootclasspath=@FileArg($_rebased_build_config:android:sdk_jars)", + + # Pass the full classpath to find new dependencies that are not in + # the .desugardeps file. + "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)", + ] + if (defined(invoker.desugar_jars_paths)) { + _rebased_desugar_jars_paths = + rebase_path(invoker.desugar_jars_paths, root_build_dir) + args += [ "--classpath=${_rebased_desugar_jars_paths}" ] + } + if (defined(invoker.final_ijar_path)) { + # Need to include the input .interface.jar on the classpath in order to make + # jar_excluded_patterns classes visible to desugar. + args += [ + "--classpath", + rebase_path(invoker.final_ijar_path, root_build_dir), + ] + inputs += [ invoker.final_ijar_path ] + } + } else { + not_needed(invoker, [ "desugar_jars_paths" ]) + } + + if (enable_java_asserts) { + # The default for generating dex file format is + # --force-disable-assertions. + args += [ "--force-enable-assertions" ] + } + } + } + } + + # Variables + # output: Path to output ".l8.dex". + # min_sdk_version: The minimum Android SDK version this target supports. + template("dex_jdk_libs") { + action_with_pydeps(target_name) { + script = "//build/android/gyp/dex_jdk_libs.py" + inputs = [ + _r8_path, + _desugar_jdk_libs_json, + _desugar_jdk_libs_jar, + _desugar_jdk_libs_configuration_jar, + ] + outputs = [ invoker.output ] + args = [ + "--r8-path", + rebase_path(_r8_path, root_build_dir), + "--desugar-jdk-libs-json", + rebase_path(_desugar_jdk_libs_json, root_build_dir), + "--desugar-jdk-libs-jar", + rebase_path(_desugar_jdk_libs_jar, root_build_dir), + "--desugar-jdk-libs-configuration-jar", + rebase_path(_desugar_jdk_libs_configuration_jar, root_build_dir), + "--output", + rebase_path(invoker.output, root_build_dir), + "--min-api=${invoker.min_sdk_version}", + ] + if (treat_warnings_as_errors) { + args += [ "--warnings-as-errors" ] + } + } + } + + template("jacoco_instr") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, + TESTONLY_AND_VISIBILITY + [ + "deps", + "public_deps", + ]) + + # The name needs to match the SOURCES_JSON_FILES_SUFFIX in + # generate_coverage_metadata_for_java.py. + _sources_json_file = "$target_out_dir/${target_name}__jacoco_sources.json" + _jacococli_jar = "//third_party/jacoco/lib/jacococli.jar" + + script = "//build/android/gyp/jacoco_instr.py" + inputs = invoker.java_files + [ + _jacococli_jar, + invoker.input_jar_path, + ] + outputs = [ + _sources_json_file, + invoker.output_jar_path, + ] + args = [ + "--input-path", + rebase_path(invoker.input_jar_path, root_build_dir), + "--output-path", + rebase_path(invoker.output_jar_path, root_build_dir), + "--sources-json-file", + rebase_path(_sources_json_file, root_build_dir), + "--java-sources-file", + rebase_path(invoker.java_sources_file, root_build_dir), + "--jacococli-jar", + rebase_path(_jacococli_jar, root_build_dir), + ] + if (coverage_instrumentation_input_file != "") { + args += [ + "--files-to-instrument", + rebase_path(coverage_instrumentation_input_file, root_build_dir), + ] + } + } + } + + template("filter_jar") { + action_with_pydeps(target_name) { + script = "//build/android/gyp/filter_zip.py" + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ]) + inputs = [ invoker.input_jar ] + if (defined(invoker.inputs)) { + inputs += invoker.inputs + } + outputs = [ invoker.output_jar ] + + _jar_excluded_patterns = [] + if (defined(invoker.jar_excluded_patterns)) { + _jar_excluded_patterns = invoker.jar_excluded_patterns + } + _jar_included_patterns = [] + if (defined(invoker.jar_included_patterns)) { + _jar_included_patterns = invoker.jar_included_patterns + } + args = [ + "--input", + rebase_path(invoker.input_jar, root_build_dir), + "--output", + rebase_path(invoker.output_jar, root_build_dir), + "--exclude-globs=${_jar_excluded_patterns}", + "--include-globs=${_jar_included_patterns}", + ] + } + } + + template("process_java_library") { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + + _deps = invoker.jar_deps + _previous_output_jar = invoker.input_jar_path + + if (invoker.is_device_jar && invoker.enable_desugar) { + _desugar_target = "${target_name}_device__desugar" + _desugar_output_jar = "$target_out_dir/$target_name.desugar.jar" + + action_with_pydeps(_desugar_target) { + script = "//build/android/gyp/desugar.py" + deps = _deps + invoker.classpath_deps + depfile = "$target_gen_dir/$target_name.d" + _rebased_build_config = + rebase_path(invoker.build_config, root_build_dir) + _desugar_jar = "//third_party/bazel/desugar/Desugar.jar" + + inputs = [ + invoker.build_config, + _previous_output_jar, + _desugar_jar, + ] + outputs = [ _desugar_output_jar ] + args = [ + "--desugar-jar", + rebase_path(_desugar_jar, root_build_dir), + "--input-jar", + rebase_path(_previous_output_jar, root_build_dir), + "--output-jar", + rebase_path(_desugar_output_jar, root_build_dir), + + # Temporarily using java_full_interface_classpath until classpath validation of targets + # is implemented, see http://crbug.com/885273 + "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)", + "--bootclasspath=@FileArg($_rebased_build_config:android:sdk_interface_jars)", + "--depfile", + rebase_path(depfile, root_build_dir), + ] + if (defined(invoker.desugar_jars_paths)) { + _rebased_desugar_jars_paths = + rebase_path(invoker.desugar_jars_paths, root_build_dir) + args += [ "--classpath=${_rebased_desugar_jars_paths}" ] + } + if (treat_warnings_as_errors) { + args += [ "--warnings-as-errors" ] + } + } + + _deps = [] + _deps = [ ":$_desugar_target" ] + _previous_output_jar = _desugar_output_jar + } + + if (invoker.jacoco_instrument) { + _filter_jar_target_name = "${target_name}__filter_jar" + _filter_jar_output_jar = "$target_out_dir/$target_name.filter.jar" + } else { + _filter_jar_target_name = target_name + _filter_jar_output_jar = invoker.output_jar_path + } + + filter_jar(_filter_jar_target_name) { + forward_variables_from(invoker, + [ + "jar_excluded_patterns", + "jar_included_patterns", + ]) + deps = _deps + input_jar = _previous_output_jar + output_jar = _filter_jar_output_jar + } + + if (invoker.jacoco_instrument) { + # Jacoco must run after desugar (or else desugar sometimes fails). + # It must run after filtering to avoid the same (filtered) class mapping + # to multiple .jar files. + # We run offline code coverage processing here rather than with a + # javaagent as the desired coverage data was not being generated. + # See crbug.com/1097815. + jacoco_instr(target_name) { + deps = [ ":$_filter_jar_target_name" ] + invoker.jar_deps + forward_variables_from(invoker, + [ + "java_files", + "java_sources_file", + ]) + + input_jar_path = _filter_jar_output_jar + output_jar_path = invoker.output_jar_path + } + } + } + + template("bytecode_processor") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + _bytecode_checker_script = "$root_build_dir/bin/helper/bytecode_processor" + script = "//build/android/gyp/bytecode_processor.py" + inputs = [ + invoker.build_config, + invoker.input_jar, + _bytecode_checker_script, + ] + outputs = [ "$target_out_dir/$target_name.bytecode.stamp" ] + deps = + invoker.deps + + [ "//build/android/bytecode:bytecode_processor($default_toolchain)" ] + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + args = [ + "--target-name", + get_label_info(":${target_name}", "label_no_toolchain"), + "--script", + rebase_path(_bytecode_checker_script, root_build_dir), + "--gn-target=${invoker.target_label}", + "--input-jar", + rebase_path(invoker.input_jar, root_build_dir), + "--stamp", + rebase_path(outputs[0], root_build_dir), + "--direct-classpath-jars=@FileArg($_rebased_build_config:javac:classpath)", + "--full-classpath-jars=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)", + "--full-classpath-gn-targets=@FileArg($_rebased_build_config:deps_info:javac_full_classpath_targets)", + ] + if (invoker.requires_android) { + args += [ "--sdk-classpath-jars=@FileArg($_rebased_build_config:android:sdk_jars)" ] + } + if (invoker.is_prebuilt) { + args += [ "--is-prebuilt" ] + } + if (treat_warnings_as_errors) { + args += [ "--warnings-as-errors" ] + } + if (defined(invoker.missing_classes_allowlist)) { + args += [ + "--missing-classes-allowlist=${invoker.missing_classes_allowlist}", + ] + } + } + } + + template("merge_manifests") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ]) + script = "//build/android/gyp/merge_manifest.py" + depfile = "$target_gen_dir/$target_name.d" + + inputs = [ + invoker.build_config, + invoker.input_manifest, + ] + + outputs = [ invoker.output_manifest ] + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--android-sdk-cmdline-tools", + rebase_path("${public_android_sdk_root}/cmdline-tools/latest", + root_build_dir), + "--root-manifest", + rebase_path(invoker.input_manifest, root_build_dir), + "--output", + rebase_path(invoker.output_manifest, root_build_dir), + "--extras", + "@FileArg($_rebased_build_config:extra_android_manifests)", + "--min-sdk-version=${invoker.min_sdk_version}", + "--target-sdk-version=${invoker.target_sdk_version}", + ] + + if (defined(invoker.manifest_package)) { + args += [ "--manifest-package=${invoker.manifest_package}" ] + } + + if (defined(invoker.max_sdk_version)) { + args += [ "--max-sdk-version=${invoker.max_sdk_version}" ] + } + + if (treat_warnings_as_errors) { + args += [ "--warnings-as-errors" ] + } + } + } + + # This template is used to parse a set of resource directories and + # create the R.txt, .srcjar and .resources.zip for it. + # + # Input variables: + # deps: Specifies the input dependencies for this target. + # + # build_config: Path to the .build_config.json file corresponding to the target. + # + # sources: + # List of input resource files. + # + # custom_package: (optional) + # Package name for the generated R.java source file. Optional if + # android_manifest is not provided. + # + # android_manifest: (optional) + # If custom_package is not provided, path to an AndroidManifest.xml file + # that is only used to extract a package name out of it. + # + # r_text_in_path: (optional) + # Path to an input R.txt file to use to generate the R.java file. + # The default is to use 'aapt' to generate the file from the content + # of the resource directories. + # + # Output variables: + # resources_zip: + # Path to a .resources.zip that will simply contain all the + # input resources, collected in a single archive. + # + # r_text_out_path: Path for the generated R.txt file. + # + template("prepare_resources") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, + TESTONLY_AND_VISIBILITY + [ + "deps", + "sources", + ]) + script = "//build/android/gyp/prepare_resources.py" + + depfile = "$target_gen_dir/${invoker.target_name}.d" + outputs = [ + invoker.resources_zip, + invoker.resources_zip + ".info", + invoker.r_text_out_path, + ] + + inputs = [ invoker.res_sources_path ] + + _rebased_res_sources_path = + rebase_path(invoker.res_sources_path, root_build_dir) + + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--res-sources-path=$_rebased_res_sources_path", + "--resource-zip-out", + rebase_path(invoker.resources_zip, root_build_dir), + "--r-text-out", + rebase_path(invoker.r_text_out_path, root_build_dir), + ] + + if (defined(invoker.r_text_in_path)) { + _r_text_in_path = invoker.r_text_in_path + inputs += [ _r_text_in_path ] + args += [ + "--r-text-in", + rebase_path(_r_text_in_path, root_build_dir), + ] + } + + if (defined(invoker.strip_drawables) && invoker.strip_drawables) { + args += [ "--strip-drawables" ] + } + if (defined(invoker.allow_missing_resources) && + invoker.allow_missing_resources) { + args += [ "--allow-missing-resources" ] + } + } + } + + # A template that is used to compile all resources needed by a binary + # (e.g. an android_apk or a junit_binary) into an intermediate .ar_ + # archive. It can also generate an associated .srcjar that contains the + # final R.java sources for all resource packages the binary depends on. + # + # Input variables: + # android_sdk_dep: The sdk dep that these resources should compile against. + # + # deps: Specifies the input dependencies for this target. + # + # build_config: Path to the .build_config.json file corresponding to the target. + # + # build_config_dep: Dep target to generate the .build_config.json file. + # + # android_manifest: Path to root manifest for the binary. + # + # version_code: (optional) + # + # version_name: (optional) + # + # shared_resources: (optional) + # If true, make all variables in each generated R.java file non-final, + # and provide an onResourcesLoaded() method that can be used to reset + # their package index at load time. Useful when the APK corresponds to + # a library that is loaded at runtime, like system_webview_apk or + # monochrome_apk. + # + # app_as_shared_lib: (optional) + # If true, same effect as shared_resources, but also ensures that the + # resources can be used by the APK when it is loaded as a regular + # application as well. Useful for the monochrome_public_apk target + # which is both an application and a shared runtime library that + # implements the system webview feature. + # + # shared_resources_allowlist: (optional) + # Path to an R.txt file. If provided, acts similar to shared_resources + # except that it restricts the list of non-final resource variables + # to the list from the input R.txt file. Overrides shared_resources + # when both are specified. + # + # shared_resources_allowlist_locales: (optional) + # If shared_resources_allowlist is used, provide an optional list of + # Chromium locale names to determine which localized shared string + # resources to put in the final output, even if aapt_locale_allowlist + # is defined to a smaller subset. + # + # aapt_locale_allowlist: (optional) + # Restrict compiled locale-dependent resources to a specific allowlist. + # NOTE: This is a list of Chromium locale names, not Android ones. + # + # r_java_root_package_name: (optional) + # Short package name for this target's root R java file (ex. input of + # "base" would become "gen.base_module" for the root R java package name). + # Optional as defaults to "base". + # + # resource_exclusion_regex: (optional) + # + # resource_exclusion_exceptions: (optional) + # + # resource_values_filter_rules: (optional) + # + # no_xml_namespaces: (optional) + # + # png_to_webp: (optional) + # If true, convert all PNG resources (except 9-patch files) to WebP. + # + # post_process_script: (optional) + # + # package_name: (optional) + # Name of the package for the purpose of creating R class. + # + # package_id: (optional) + # Use a custom package ID in resource IDs. + # + # arsc_package_name: (optional) + # Use this package name in the arsc file rather than the package name + # found in the AndroidManifest.xml. Does not affect the package name + # used in AndroidManifest.xml. + # + # resource_ids_provider_dep: (optional) + # Use resource IDs provided by another APK target when compiling resources + # (via. "aapt2 link --stable-ids") + # + # + # Output variables: + # arsc_output: Path to output .ap_ file (optional). + # + # proto_output: Path to output .proto.ap_ file (optional). + # + # r_text_out_path: (optional): + # Path for the corresponding generated R.txt file. + # + # proguard_file: (optional) + # Path to proguard configuration file for this apk target. + # + # proguard_file_main_dex: (optional) + # + template("compile_resources") { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + + _deps = [ + invoker.android_sdk_dep, + invoker.build_config_dep, + ] + if (defined(invoker.android_manifest_dep)) { + _deps += [ invoker.android_manifest_dep ] + } + foreach(_dep, invoker.deps) { + _target_label = get_label_info(_dep, "label_no_toolchain") + if (filter_exclude([ _target_label ], _java_library_patterns) == [] && + filter_exclude([ _target_label ], _java_resource_patterns) != []) { + # Depend on the java libraries' transitive __assetres target instead. + _deps += [ "${_target_label}__assetres" ] + } else { + _deps += [ _dep ] + } + } + + if (defined(invoker.arsc_output)) { + _arsc_output = invoker.arsc_output + } + _final_srcjar_path = "${target_gen_dir}/${target_name}.srcjar" + + _script = "//build/android/gyp/compile_resources.py" + + _inputs = [ + invoker.build_config, + android_sdk_tools_bundle_aapt2, + ] + + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + + _args = [ + "--include-resources=@FileArg($_rebased_build_config:android:sdk_jars)", + "--aapt2-path", + rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir), + "--dependencies-res-zips=@FileArg($_rebased_build_config:deps_info:dependency_zips)", + "--extra-res-packages=@FileArg($_rebased_build_config:deps_info:extra_package_names)", + "--extra-main-r-text-files=@FileArg($_rebased_build_config:deps_info:extra_main_r_text_files)", + "--min-sdk-version=${invoker.min_sdk_version}", + "--target-sdk-version=${invoker.target_sdk_version}", + "--webp-cache-dir=obj/android-webp-cache", + ] + + _inputs += [ invoker.android_manifest ] + _outputs = [ _final_srcjar_path ] + _args += [ + "--android-manifest", + rebase_path(invoker.android_manifest, root_build_dir), + "--srcjar-out", + rebase_path(_final_srcjar_path, root_build_dir), + ] + if (defined(invoker.no_xml_namespaces) && invoker.no_xml_namespaces) { + _args += [ "--no-xml-namespaces" ] + } + if (defined(invoker.version_code)) { + _args += [ + "--version-code", + invoker.version_code, + ] + } + if (defined(invoker.version_name)) { + _args += [ + "--version-name", + invoker.version_name, + ] + } + if (defined(_arsc_output)) { + _outputs += [ _arsc_output ] + _args += [ + "--arsc-path", + rebase_path(_arsc_output, root_build_dir), + ] + } + if (defined(invoker.proto_output)) { + _outputs += [ invoker.proto_output ] + _args += [ + "--proto-path", + rebase_path(invoker.proto_output, root_build_dir), + ] + } + if (defined(invoker.size_info_path)) { + _outputs += [ invoker.size_info_path ] + _args += [ + "--info-path", + rebase_path(invoker.size_info_path, root_build_dir), + ] + } + + if (defined(invoker.r_java_root_package_name)) { + _args += [ + "--r-java-root-package-name", + invoker.r_java_root_package_name, + ] + } + + # Useful to have android:debuggable in the manifest even for Release + # builds. Just omit it for officai + if (debuggable_apks) { + _args += [ "--debuggable" ] + } + + if (defined(invoker.r_text_out_path)) { + _outputs += [ invoker.r_text_out_path ] + _args += [ + "--r-text-out", + rebase_path(invoker.r_text_out_path, root_build_dir), + ] + } + + if (defined(invoker.rename_manifest_package)) { + _args += [ + "--rename-manifest-package", + invoker.rename_manifest_package, + ] + } + + # Define the flags related to shared resources. + # + # Note the small sanity check to ensure that the package ID of the + # generated resources table is correct. It should be 0x02 for runtime + # shared libraries, and 0x7f otherwise. + + if (defined(invoker.shared_resources) && invoker.shared_resources) { + _args += [ "--shared-resources" ] + } + if (defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib) { + _args += [ "--app-as-shared-lib" ] + } + if (defined(invoker.package_id)) { + _args += [ "--package-id=${invoker.package_id}" ] + } + if (defined(invoker.package_name)) { + _args += [ + "--package-name", + invoker.package_name, + ] + } + if (defined(invoker.arsc_package_name)) { + _args += [ + "--arsc-package-name", + invoker.arsc_package_name, + ] + } + + if (defined(invoker.shared_resources_allowlist)) { + _inputs += [ invoker.shared_resources_allowlist ] + _args += [ + "--shared-resources-allowlist", + rebase_path(invoker.shared_resources_allowlist, root_build_dir), + ] + } + if (defined(invoker.shared_resources_allowlist_locales)) { + _args += [ "--shared-resources-allowlist-locales=" + + "${invoker.shared_resources_allowlist_locales}" ] + } + + if (!defined(testonly) || !testonly || + (defined(invoker.enforce_resource_overlays_in_tests) && + invoker.enforce_resource_overlays_in_tests)) { + _args += [ "--dependencies-res-zip-overlays=@FileArg($_rebased_build_config:deps_info:dependency_zip_overlays)" ] + } else { + _args += [ "--dependencies-res-zip-overlays=@FileArg($_rebased_build_config:deps_info:dependency_zips)" ] + } + + if (defined(invoker.proguard_file)) { + _outputs += [ invoker.proguard_file ] + _args += [ + "--proguard-file", + rebase_path(invoker.proguard_file, root_build_dir), + ] + } + + if (defined(invoker.proguard_file_main_dex)) { + _outputs += [ invoker.proguard_file_main_dex ] + _args += [ + "--proguard-file-main-dex", + rebase_path(invoker.proguard_file_main_dex, root_build_dir), + ] + } + + if (defined(invoker.aapt_locale_allowlist)) { + _args += [ "--locale-allowlist=${invoker.aapt_locale_allowlist}" ] + } + if (defined(invoker.png_to_webp) && invoker.png_to_webp) { + _webp_target = "//third_party/libwebp:cwebp($host_toolchain)" + _webp_binary = get_label_info(_webp_target, "root_out_dir") + "/cwebp" + _deps += [ _webp_target ] + _inputs += [ _webp_binary ] + _args += [ + "--png-to-webp", + "--webp-binary", + rebase_path(_webp_binary, root_build_dir), + ] + } + if (defined(invoker.resource_exclusion_regex)) { + _args += + [ "--resource-exclusion-regex=${invoker.resource_exclusion_regex}" ] + if (defined(invoker.resource_exclusion_exceptions)) { + _args += [ "--resource-exclusion-exceptions=${invoker.resource_exclusion_exceptions}" ] + } + } + if (defined(invoker.resource_values_filter_rules)) { + _args += + [ "--values-filter-rules=${invoker.resource_values_filter_rules}" ] + } + + if (defined(invoker.include_resource)) { + _rebased_include_resources = + rebase_path(invoker.include_resource, root_build_dir) + _args += [ "--include-resources=$_rebased_include_resources" ] + } + + if (defined(invoker._args)) { + _args += invoker._args + } + + if (defined(invoker.emit_ids_out_path)) { + _outputs += [ invoker.emit_ids_out_path ] + _rebased_emit_ids_path = + rebase_path(invoker.emit_ids_out_path, root_out_dir) + _args += [ "--emit-ids-out=$_rebased_emit_ids_path" ] + } + + if (defined(invoker.resource_ids_provider_dep)) { + _compile_res_dep = + "${invoker.resource_ids_provider_dep}__compile_resources" + _gen_dir = get_label_info(_compile_res_dep, "target_gen_dir") + _name = get_label_info(_compile_res_dep, "name") + _resource_ids_path = "$_gen_dir/$_name.resource_ids" + _inputs += [ _resource_ids_path ] + _rebased_ids_path = rebase_path(_resource_ids_path, root_out_dir) + _args += [ "--use-resource-ids-path=$_rebased_ids_path" ] + _deps += [ _compile_res_dep ] + } + + if (defined(invoker.max_sdk_version)) { + _max_sdk_version = invoker.max_sdk_version + _args += [ "--max-sdk-version=$_max_sdk_version" ] + } + + if (defined(invoker.manifest_package)) { + _args += [ "--manifest-package=${invoker.manifest_package}" ] + } + + if (defined(invoker.is_bundle_module) && invoker.is_bundle_module) { + _args += [ "--is-bundle-module" ] + } + + if (defined(invoker.uses_split)) { + assert(invoker.is_bundle_module) + _args += [ "--uses-split=${invoker.uses_split}" ] + } + + if (defined(invoker.expected_android_manifest)) { + _expectations_target = + "${invoker.top_target_name}_validate_android_manifest" + action_with_pydeps(_expectations_target) { + _actual_file = "${invoker.android_manifest}.normalized" + _failure_file = + "$expectations_failure_dir/" + + string_replace(invoker.expected_android_manifest, "/", "_") + inputs = [ + invoker.android_manifest, + invoker.build_config, + invoker.expected_android_manifest, + ] + outputs = [ + _actual_file, + _failure_file, + ] + deps = [ + invoker.android_manifest_dep, + invoker.build_config_dep, + ] + script = _script + args = _args + [ + "--expected-file", + rebase_path(invoker.expected_android_manifest, root_build_dir), + "--actual-file", + rebase_path(_actual_file, root_build_dir), + "--failure-file", + rebase_path(_failure_file, root_build_dir), + "--only-verify-expectations", + ] + if (defined(invoker.expected_android_manifest_base)) { + args += [ + "--expected-file-base", + rebase_path(invoker.expected_android_manifest_base, root_build_dir), + ] + inputs += [ invoker.expected_android_manifest_base ] + } + if (fail_on_android_expectations) { + args += [ "--fail-on-expectations" ] + } + if (defined(invoker.extra_verification_manifest)) { + inputs += [ invoker.extra_verification_manifest ] + args += [ + "--extra-verification-manifest", + rebase_path(invoker.extra_verification_manifest, root_build_dir), + ] + if (defined(invoker.extra_verification_manifest_dep)) { + deps += [ invoker.extra_verification_manifest_dep ] + } + } + } + _deps += [ ":$_expectations_target" ] + } + + action_with_pydeps(target_name) { + script = _script + depfile = "$target_gen_dir/${target_name}.d" + inputs = _inputs + outputs = _outputs + deps = _deps + args = _args + [ + "--depfile", + rebase_path(depfile, root_build_dir), + ] + } + } + + # A template that is used to optimize compiled resources using aapt2 optimize. + # + # proto_input_path: + # Path to input compiled .proto.ap_ file. + # + # short_resource_paths: (optional) + # Rename the paths within a the apk to be randomly generated short + # strings to reduce binary size. + # + # strip_resource_names: (optional) + # Strip resource names from the resources table of the apk. + # + # resources_configs_paths: (optional) + # List of resource configs to use for optimization. + # + # optimized_proto_output: + # Path to output optimized .proto.ap_ file. + # + # resources_path_map_out_path: (optional): + # Path for the generated map between original resource paths and + # shortened resource paths. + template("optimize_resources") { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + action_with_pydeps(target_name) { + forward_variables_from(invoker, [ "deps" ]) + script = "//build/android/gyp/optimize_resources.py" + outputs = [ invoker.optimized_proto_output ] + inputs = [ + android_sdk_tools_bundle_aapt2, + invoker.r_text_path, + invoker.proto_input_path, + ] + args = [ + "--aapt2-path", + rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir), + "--r-text-in", + rebase_path(invoker.r_text_path, root_build_dir), + "--proto-path", + rebase_path(invoker.proto_input_path, root_build_dir), + "--optimized-proto-path", + rebase_path(invoker.optimized_proto_output, root_build_dir), + ] + + if (defined(invoker.resources_config_paths)) { + inputs += invoker.resources_config_paths + _rebased_resource_configs = + rebase_path(invoker.resources_config_paths, root_build_dir) + args += [ "--resources-config-paths=${_rebased_resource_configs}" ] + } + + if (defined(invoker.short_resource_paths) && + invoker.short_resource_paths) { + args += [ "--short-resource-paths" ] + if (defined(invoker.resources_path_map_out_path)) { + outputs += [ invoker.resources_path_map_out_path ] + args += [ + "--resources-path-map-out-path", + rebase_path(invoker.resources_path_map_out_path, root_build_dir), + ] + } + } + + if (defined(invoker.strip_resource_names) && + invoker.strip_resource_names) { + args += [ "--strip-resource-names" ] + } + } + } + + # A template that is used to find unused resources. + template("unused_resources") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ]) + script = "//build/android/gyp/unused_resources.py" + depfile = "$target_gen_dir/${target_name}.d" + _unused_resources_script = "$root_build_dir/bin/helper/unused_resources" + inputs = [ _unused_resources_script ] + outputs = [ invoker.output_config ] + if (!defined(deps)) { + deps = [] + } + deps += [ "//build/android/unused_resources:unused_resources" ] + _rebased_module_build_config = + rebase_path(invoker.build_config, root_build_dir) + args = [ + "--script", + rebase_path(_unused_resources_script, root_build_dir), + "--output-config", + rebase_path(invoker.output_config, root_build_dir), + "--r-text=@FileArg($_rebased_module_build_config:deps_info:r_text_path)", + "--dependencies-res-zips=@FileArg($_rebased_module_build_config:deps_info:dependency_zips)", + "--depfile", + rebase_path(depfile, root_build_dir), + ] + + if (defined(invoker.proguard_mapping_path)) { + inputs += [ invoker.proguard_mapping_path ] + args += [ + "--proguard-mapping", + rebase_path(invoker.proguard_mapping_path, root_build_dir), + ] + } + + foreach(_build_config, invoker.all_module_build_configs) { + inputs += [ _build_config ] + _rebased_build_config = rebase_path(_build_config, root_build_dir) + args += [ + "--dexes=@FileArg($_rebased_build_config:final_dex:path)", + "--android-manifests=@FileArg($_rebased_build_config:deps_info:merged_android_manifest)", + ] + } + } + } + + # Create an .jar.info file by merging several .jar.info files into one. + # + # Variables: + # build_config: Path to APK's build config file. Used to extract the + # list of input .jar files from its dependencies. + # name: Name of the apk or app bundle (e.g. "Foo.apk"). + # res_size_info_path: Path to input .ap_.info file (for apks). + # + template("create_size_info_files") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ]) + script = "//build/android/gyp/create_size_info_files.py" + _jar_info_path = "$root_build_dir/size-info/${invoker.name}.jar.info" + _pak_info_path = "$root_build_dir/size-info/${invoker.name}.pak.info" + _res_info_path = "$root_build_dir/size-info/${invoker.name}.res.info" + outputs = [ + _jar_info_path, + _pak_info_path, + _res_info_path, + ] + depfile = "$target_gen_dir/$target_name.d" + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--jar-info-path", + rebase_path(_jar_info_path, root_build_dir), + "--pak-info-path", + rebase_path(_pak_info_path, root_build_dir), + "--res-info-path", + rebase_path(_res_info_path, root_build_dir), + ] + _is_bundle = defined(invoker.module_build_configs) + if (_is_bundle) { + inputs = invoker.module_build_configs + foreach(_build_config, invoker.module_build_configs) { + _rebased_build_config = rebase_path(_build_config, root_build_dir) + args += [ + "--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)", + "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)", + "--in-res-info-path=@FileArg($_rebased_build_config:deps_info:res_size_info)", + "--assets=@FileArg($_rebased_build_config:assets)", + "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)", + ] + } + } else { + inputs = [ + invoker.build_config, + invoker.res_size_info_path, + ] + _rebased_build_config = + rebase_path(invoker.build_config, root_build_dir) + args += [ + "--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)", + "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)", + "--in-res-info-path", + rebase_path(invoker.res_size_info_path, root_build_dir), + "--assets=@FileArg($_rebased_build_config:assets)", + "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)", + ] + } + } + } + + # Creates a signed and aligned .apk. + # + # Variables + # apk_name: (optional) APK name (without .apk suffix). If provided, will + # be used to generate .info files later used by the supersize tool. + # assets_build_config: Path to android_apk .build_config.json containing merged + # asset information. + # deps: Specifies the dependencies of this target. + # dex_path: Path to classes.dex file to include (optional). + # expected_libs_and_assets: Verify the list of included native libraries + # and assets is consistent with the given expectation file. + # expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff + # with this file as the base. + # jdk_libs_dex: Path to classes.dex for desugar_jdk_libs. + # packaged_resources_path: Path to .ap_ to use. + # output_apk_path: Output path for the generated .apk. + # min_sdk_version: The minimum Android SDK version this target supports. + # native_lib_placeholders: List of placeholder filenames to add to the apk + # (optional). + # secondary_native_lib_placeholders: List of placeholder filenames to add to + # the apk for the secondary ABI (optional). + # loadable_modules: List of native libraries. + # native_libs_filearg: @FileArg() of additionally native libraries. + # secondary_abi_loadable_modules: (optional) List of native libraries for + # secondary ABI. + # secondary_abi_native_libs_filearg: (optional). @FileArg() of additional + # secondary ABI native libs. + # keystore_path: Path to keystore to use for signing. + # keystore_name: Key alias to use. + # keystore_password: Keystore password. + # uncompress_shared_libraries: (optional, default false) Whether to store + # native libraries inside the APK uncompressed and page-aligned. + template("package_apk") { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "public_deps" ]) + _deps = invoker.deps + _native_lib_placeholders = [] + if (defined(invoker.native_lib_placeholders)) { + _native_lib_placeholders = invoker.native_lib_placeholders + } + _secondary_native_lib_placeholders = [] + if (defined(invoker.secondary_native_lib_placeholders)) { + _secondary_native_lib_placeholders = + invoker.secondary_native_lib_placeholders + } + + _script = "//build/android/gyp/apkbuilder.py" + _apksigner = "$android_sdk_build_tools/lib/apksigner.jar" + _zipalign = "$android_sdk_build_tools/zipalign" + + _inputs = [ + invoker.build_config, + invoker.keystore_path, + invoker.packaged_resources_path, + _apksigner, + _zipalign, + ] + + _outputs = [ invoker.output_apk_path ] + _data = [ invoker.output_apk_path ] + + _rebased_compiled_resources_path = + rebase_path(invoker.packaged_resources_path, root_build_dir) + _rebased_packaged_apk_path = + rebase_path(invoker.output_apk_path, root_build_dir) + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + _args = [ + "--resource-apk=$_rebased_compiled_resources_path", + "--output-apk=$_rebased_packaged_apk_path", + "--assets=@FileArg($_rebased_build_config:assets)", + "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)", + "--apksigner-jar", + rebase_path(_apksigner, root_build_dir), + "--zipalign-path", + rebase_path(_zipalign, root_build_dir), + "--key-path", + rebase_path(invoker.keystore_path, root_build_dir), + "--key-name", + invoker.keystore_name, + "--key-passwd", + invoker.keystore_password, + "--min-sdk-version=${invoker.min_sdk_version}", + + # TODO(mlopatkin) We are relying on the fact that build_config is an APK + # build_config. + "--java-resources=@FileArg($_rebased_build_config:java_resources_jars)", + ] + if (is_official_build) { + _args += [ "--best-compression" ] + } + if (defined(invoker.uncompress_dex) && invoker.uncompress_dex) { + _args += [ "--uncompress-dex" ] + } + if (defined(invoker.uncompress_shared_libraries) && + invoker.uncompress_shared_libraries) { + _args += [ "--uncompress-shared-libraries=True" ] + } + if (defined(invoker.library_always_compress)) { + _args += + [ "--library-always-compress=${invoker.library_always_compress}" ] + } + if (defined(invoker.library_renames)) { + _args += [ "--library-renames=${invoker.library_renames}" ] + } + if (defined(invoker.dex_path)) { + _inputs += [ invoker.dex_path ] + _args += [ + "--dex-file", + rebase_path(invoker.dex_path, root_build_dir), + ] + } + if (defined(invoker.jdk_libs_dex)) { + _inputs += [ invoker.jdk_libs_dex ] + _args += [ + "--jdk-libs-dex-file", + rebase_path(invoker.jdk_libs_dex, root_build_dir), + ] + } + if ((defined(invoker.loadable_modules) && invoker.loadable_modules != []) || + defined(invoker.native_libs_filearg) || + _native_lib_placeholders != []) { + _args += [ "--android-abi=$android_app_abi" ] + } + if (defined(android_app_secondary_abi)) { + _args += [ "--secondary-android-abi=$android_app_secondary_abi" ] + } + if (defined(invoker.loadable_modules) && invoker.loadable_modules != []) { + _inputs += invoker.loadable_modules + _rebased_loadable_modules = + rebase_path(invoker.loadable_modules, root_build_dir) + _args += [ "--native-libs=$_rebased_loadable_modules" ] + } + if (defined(invoker.native_libs_filearg)) { + _args += [ "--native-libs=${invoker.native_libs_filearg}" ] + } + if (_native_lib_placeholders != []) { + _args += [ "--native-lib-placeholders=$_native_lib_placeholders" ] + } + + if (defined(invoker.secondary_abi_native_libs_filearg)) { + _args += [ + "--secondary-native-libs=${invoker.secondary_abi_native_libs_filearg}", + ] + } + if (defined(invoker.secondary_abi_loadable_modules)) { + _rebased_secondary_abi_loadable_modules = + rebase_path(invoker.secondary_abi_loadable_modules, root_build_dir) + _args += + [ "--secondary-native-libs=$_rebased_secondary_abi_loadable_modules" ] + } + if (_secondary_native_lib_placeholders != []) { + _args += [ "--secondary-native-lib-placeholders=$_secondary_native_lib_placeholders" ] + } + if (treat_warnings_as_errors) { + _args += [ "--warnings-as-errors" ] + } + + if (defined(invoker.expected_libs_and_assets)) { + _expectations_target = + "${invoker.top_target_name}_validate_libs_and_assets" + action_with_pydeps(_expectations_target) { + _actual_file = "$target_gen_dir/$target_name.libs_and_assets" + _failure_file = + "$expectations_failure_dir/" + + string_replace(invoker.expected_libs_and_assets, "/", "_") + inputs = [ + invoker.build_config, + invoker.expected_libs_and_assets, + ] + deps = [ invoker.build_config_dep ] + outputs = [ + _actual_file, + _failure_file, + ] + script = _script + args = _args + [ + "--expected-file", + rebase_path(invoker.expected_libs_and_assets, root_build_dir), + "--actual-file", + rebase_path(_actual_file, root_build_dir), + "--failure-file", + rebase_path(_failure_file, root_build_dir), + "--only-verify-expectations", + ] + if (defined(invoker.expected_libs_and_assets_base)) { + inputs += [ invoker.expected_libs_and_assets_base ] + args += [ + "--expected-file-base", + rebase_path(invoker.expected_libs_and_assets_base, root_build_dir), + ] + } + if (fail_on_android_expectations) { + args += [ "--fail-on-expectations" ] + } + } + _deps += [ ":$_expectations_target" ] + } + action_with_pydeps(target_name) { + depfile = "$target_gen_dir/$target_name.d" + inputs = _inputs + deps = _deps + data = _data + outputs = _outputs + script = _script + args = _args + [ + "--depfile", + rebase_path(depfile, root_build_dir), + ] + } + } + + # Compile Java source files into a .jar file, potentially using an + # annotation processor, and/or the errorprone compiler. + # + # Note that the only way to specify custom annotation processors is + # by using build_config to point to a file that corresponds to a java-related + # target that includes javac:processor_classes entries (i.e. there is no + # variable here that can be used for this purpose). + # + # Note also the peculiar use of java_files / java_sources_file. The content + # of the java_files list and the java_sources_file file must match exactly. + # This rule uses java_files only to list the inputs to the action that + # calls compile_java.py, but will pass the list of Java source files + # with the '@${java_sources_file}" command-line syntax. Not a problem in + # practice since this is only called from java_library_impl() that sets up + # the variables properly. + # + # Variables: + # main_target_name: Used when extracting srcjars for codesearch. + # java_files: Optional list of Java source file paths. + # srcjar_deps: Optional list of .srcjar dependencies (not file paths). + # The corresponding source files they contain will be compiled too. + # java_sources_file: Optional path to file containing list of Java source + # file paths. This must always be provided if java_files is not empty + # and must match it exactly. + # build_config: Path to the .build_config.json file of the corresponding + # java_library_impl() target. The following entries will be used by this + # template: javac:srcjars, deps_info:javac_full_classpath, + # deps_info:javac_full_interface_classpath, javac:processor_classpath, + # javac:processor_classes + # javac_jar_path: Path to the final output .jar file. + # javac_args: Optional list of extra arguments to pass to javac. + # chromium_code: Whether this corresponds to Chromium-specific sources. + # requires_android: True if these sources can only run on Android. + # additional_jar_files: Optional list of files to copy into the resulting + # .jar file (by default, only .class files are put there). Each entry + # has the 'srcPath:dstPath' format. + # enable_errorprone: If True, use the errorprone compiler to check for + # error-prone constructs in the language. If not provided, whether this is + # enabled depends on chromium_code and the global + # use_errorprone_java_compiler variable. + # use_turbine: If True, compile headers using turbine.py. + # apk_name: Optional APK name. If provided, will tell compile_java.py to also + # generate an .apk.jar.info file under size-info/${apk_name}.apk.jar.info + # processor_args_javac: List of annotation processor arguments, each one + # will be passed to javac as -A<entry>. + # deps: Dependencies for the corresponding target. + # testonly: Usual meaning (should be True for test-only targets) + # + # [1] https://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html + # + template("compile_java") { + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + + _build_config = invoker.build_config + _chromium_code = invoker.chromium_code + + _processor_args = [] + if (defined(invoker.processor_args_javac)) { + _processor_args = invoker.processor_args_javac + } + + _additional_jar_files = [] + if (defined(invoker.additional_jar_files)) { + _additional_jar_files = invoker.additional_jar_files + } + + _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" ] + } + + # generated_jar_path is an output when use_turbine and an input otherwise. + if (!invoker.use_turbine) { + _java_srcjars += [ invoker.generated_jar_path ] + } + + _javac_args = [] + if (defined(invoker.javac_args)) { + _javac_args = invoker.javac_args + } + + action_with_pydeps(target_name) { + if (invoker.use_turbine) { + script = "//build/android/gyp/turbine.py" + } else { + script = "//build/android/gyp/compile_java.py" + } + + if (target_name == "chrome_java__header") { + # Regression test for: https://crbug.com/1154302 + assert_no_deps = [ "//base:base_java__impl" ] + } + + depfile = "$target_gen_dir/$target_name.d" + deps = _srcjar_deps + if (defined(invoker.deps)) { + deps += invoker.deps + } + + outputs = [ invoker.output_jar_path ] + if (!invoker.enable_errorprone && !invoker.use_turbine) { + outputs += [ invoker.output_jar_path + ".info" ] + } + inputs = invoker.java_files + _java_srcjars + [ _build_config ] + if (invoker.java_files != []) { + inputs += [ invoker.java_sources_file ] + } + + _rebased_build_config = rebase_path(_build_config, root_build_dir) + _rebased_output_jar_path = + rebase_path(invoker.output_jar_path, root_build_dir) + _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir) + _rebased_depfile = rebase_path(depfile, root_build_dir) + _rebased_generated_dir = rebase_path( + "$target_gen_dir/${invoker.main_target_name}/generated_java", + root_build_dir) + args = [ + "--depfile=$_rebased_depfile", + "--generated-dir=$_rebased_generated_dir", + "--jar-path=$_rebased_output_jar_path", + "--java-srcjars=$_rebased_java_srcjars", + "--target-name", + get_label_info(":${target_name}", "label_no_toolchain"), + ] + + if (defined(invoker.header_jar_path)) { + inputs += [ invoker.header_jar_path ] + args += [ + "--header-jar", + rebase_path(invoker.header_jar_path, root_build_dir), + ] + _header_jar_classpath = + [ rebase_path(invoker.header_jar_path, root_build_dir) ] + args += [ "--classpath=$_header_jar_classpath" ] + } + + if (invoker.use_turbine) { + # Prefer direct deps for turbine as much as possible. + args += [ "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)" ] + } else { + args += [ "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)" ] + } + + if (invoker.use_turbine) { + args += [ + "--processorpath=@FileArg($_rebased_build_config:javac:processor_classpath)", + "--processors=@FileArg($_rebased_build_config:javac:processor_classes)", + ] + } + + if (invoker.use_turbine) { + _turbine_jar_path = "//third_party/turbine/turbine.jar" + inputs += [ _turbine_jar_path ] + outputs += [ invoker.generated_jar_path ] + args += [ + "--turbine-jar-path", + rebase_path(_turbine_jar_path, root_build_dir), + "--generated-jar-path", + rebase_path(invoker.generated_jar_path, root_build_dir), + ] + } + + # Currently turbine does not support JDK11. + if (invoker.supports_android || invoker.use_turbine) { + args += [ "--java-version=1.8" ] + } + if (use_java_goma) { + args += [ "--gomacc-path=$goma_dir/gomacc" ] + + # Override the default action_pool when goma is enabled. + pool = "//build/config/android:goma_javac_pool" + } + + # Flag enable_kythe_annotations requires + # checkout_android_prebuilts_build_tools=True in .gclient. + if (enable_kythe_annotations && !invoker.enable_errorprone) { + args += [ "--enable-kythe-annotations" ] + } + if (invoker.requires_android) { + args += [ "--bootclasspath=@FileArg($_rebased_build_config:android:sdk_interface_jars)" ] + } + if (_chromium_code) { + args += [ "--chromium-code=1" ] + if (treat_warnings_as_errors) { + args += [ "--warnings-as-errors" ] + } + } + if (defined(invoker.jar_excluded_patterns)) { + args += [ "--jar-info-exclude-globs=${invoker.jar_excluded_patterns}" ] + } + + if (invoker.enable_errorprone) { + # Our custom plugin pulls in the main errorprone dep transitively. + _errorprone_dep = "//tools/android/errorprone_plugin:errorprone_plugin" + deps += [ _errorprone_dep ] + _dep_gen_dir = get_label_info(_errorprone_dep, "target_gen_dir") + _dep_name = get_label_info(_errorprone_dep, "name") + _rebased_errorprone_buildconfig = + rebase_path("$_dep_gen_dir/$_dep_name.build_config.json", + root_build_dir) + args += [ + "--processorpath=@FileArg($_rebased_errorprone_buildconfig:deps_info:host_classpath)", + "--enable-errorprone", + ] + } + if (defined(invoker.skip_build_server) && invoker.skip_build_server) { + # Nocompile tests need lint to fail through ninja. + args += [ "--skip-build-server" ] + } + + foreach(e, _processor_args) { + args += [ "--processor-arg=" + e ] + } + + foreach(file_tuple, _additional_jar_files) { + # Each element is of length two, [ path_to_file, path_to_put_in_jar ] + inputs += [ file_tuple[0] ] + args += + [ "--additional-jar-file=" + + rebase_path(file_tuple[0], root_build_dir) + ":" + file_tuple[1] ] + } + if (invoker.java_files != []) { + args += [ "@" + rebase_path(invoker.java_sources_file, root_build_dir) ] + } + foreach(e, _javac_args) { + args += [ "--javac-arg=" + e ] + } + } + } + + template("java_lib_group") { + forward_variables_from(invoker, [ "testonly" ]) + _group_name = invoker.group_name + not_needed([ "_group_name" ]) + group(target_name) { + if (defined(invoker.deps)) { + deps = [] + foreach(_dep, invoker.deps) { + _target_label = get_label_info(_dep, "label_no_toolchain") + if (filter_exclude([ _target_label ], _java_library_patterns) == [] && + filter_exclude([ _target_label ], _java_resource_patterns) != + []) { + # This is a java library dep, so replace it. + deps += [ "${_target_label}__${_group_name}" ] + } else { + # Transitive java group targets should also include direct deps. + deps += [ _dep ] + } + } + } + } + } + + # Create an interface jar from a normal jar. + # + # Variables + # input_jar: Path to input .jar. + # output_jar: Path to output .ijar. + # + template("generate_interface_jar") { + action_with_pydeps(target_name) { + _ijar_target = "//third_party/ijar:ijar($host_toolchain)" + _ijar_executable = get_label_info(_ijar_target, "root_out_dir") + "/ijar" + forward_variables_from(invoker, + TESTONLY_AND_VISIBILITY + [ + "data", + "data_deps", + "public_deps", + ]) + script = "//build/android/gyp/ijar.py" + deps = [ _ijar_target ] + if (defined(invoker.deps)) { + deps += invoker.deps + } + inputs = [ + invoker.input_jar, + _ijar_executable, + ] + if (defined(invoker.inputs)) { + inputs += invoker.inputs + } + outputs = [ invoker.output_jar ] + args = [ + rebase_path(_ijar_executable, root_build_dir), + rebase_path(invoker.input_jar, root_build_dir), + rebase_path(invoker.output_jar, root_build_dir), + ] + } + } + + # A rule that will handle multiple Java-related targets. + # + # The caller can provide a list of source files with 'java_files' + # and 'srcjar_deps', or a prebuilt .jar file through 'jar_path'. + # + # In the case of a 'java_binary' target type, it can even provide none of + # that (and the rule will just generate its wrapper script). + # + # The template will process the input .jar file (either the prebuilt one, + # or the result of compiling the sources), for example to apply Proguard, + # but also other ranges of bytecode-level rewriting schemes. + # + # Variables: + # type: type of Java target, valid values: 'java_library', 'java_binary', + # 'junit_binary', 'java_annotation_processor', and 'android_apk' + # main_target_name: optional. If provided, overrides target_name when + # creating sub-targets (e.g. "${main_target_name}__dex") and + # some output files (e.g. "${main_target_name}.sources"). Only used + # for 'android_apk' types at the moment, where main_target_name will + # be the name of the main APK target. + # supports_android: Optional. True if target can run on Android. + # requires_android: Optional. True if target can only run on Android. + # java_files: Optional list of Java source file paths for this target. + # javac_args: Optional list of extra arguments to pass to javac. + # errorprone_args: Optional list of extra arguments to pass to. + # srcjar_deps: Optional list of .srcjar targets (not file paths). The Java + # source files they contain will also be compiled for this target. + # java_sources_file: Optional path to a file which will be written with + # the content of java_files. If not provided, the file will be written + # under $target_gen_dir/$main_target_name.sources. Ignored if + # java_files is empty. If not + # jar_path: Optional path to a prebuilt .jar file for this target. + # Mutually exclusive with java_files and srcjar_deps. + # output_name: Optional output name for the final jar path. Used to + # determine the name of the final jar. Default is to use the same + # name as jar_path, if provided, or main_target_name. + # main_class: Main Java class name for 'java_binary', 'junit_binary' and + # 'java_annotation_processor' target types. Should not be set for other + # ones. + # deps: Dependencies for this target. + # public_deps: Dependencies that this target exposes as part of its public API. + # public_deps do not need to be listed in both the 'deps' and 'public_deps' lists. + # testonly: True iff target should only be used for tests. + # chromium_code: Optional. Whether this is Chromium-specific code. If not + # provided, this is determined automatically, based on the location of + # the source files (i.e. anything under third_party/ is not + # Chromium-specific unless it is in a 'chromium' sub-directory). + # jacoco_never_instrument: Optional. If provided, whether to forbid + # instrumentation with the Jacoco coverage processor. If not provided, + # this is controlled by the global use_jacoco_coverage build arg variable + # and only used for non-test Chromium code. + # include_android_sdk: Optional. Whether or not the android SDK dep + # should be added to deps. Defaults to true for non-system libraries + # that support android. + # alternative_android_sdk_dep: Optional. Alternative Android system + # android java target to use. + # annotation_processor_deps: Optional list of dependencies corresponding + # to annotation processors used to compile these sources. + # input_jars_paths: Optional list of additional .jar file paths, which will + # be added to the compile-time classpath when building this target (but + # not to the runtime classpath). + # desugar_jars_paths: Optional list of additional .jar file paths, which will + # be added to the desugar classpath when building this target (but not to + # any other classpath). This is only used to break dependency cycles. + # gradle_treat_as_prebuilt: Cause generate_gradle.py to reference this + # library via its built .jar rather than including its .java sources. + # proguard_enabled: Optional. True to enable ProGuard obfuscation. + # proguard_configs: Optional list of additional proguard config file paths. + # bypass_platform_checks: Optional. If True, platform checks will not + # be performed. They are used to verify that every target with + # requires_android only depends on targets that, at least supports_android. + # Similarly, if a target has !supports_android, then it cannot depend on + # any other target that has requires_android. + # include_java_resources: Optional. If True, include Java (not Android) + # resources into final .jar file. + # jar_excluded_patterns: Optional list of .class file patterns to exclude + # from the final .jar file. + # jar_included_patterns: Optional list of .class file patterns to include + # in the final .jar file. jar_excluded_patterns take precedence over this. + # low_classpath_priority: Indicates that the library should be placed at the + # end of the classpath. The default classpath order has libraries ordered + # before the libraries that they depend on. 'low_classpath_priority' is + # useful when one java_library() overrides another via + # 'jar_excluded_patterns' and the overriding library does not depend on the + # overridee. + # + # For 'android_apk' and 'android_app_bundle_module' targets only: + # + # apk_path: Path to the final APK file. + # android_manifest: Path to AndroidManifest.xml file for the APK. + # android_manifest_dep: Optional. Dependency target that generates + # android_manifest. + # apk_under_test: For 'android_apk' targets used to test other APKs, + # this is the target name of APK being tested. + # incremental_apk_path: Path to the incremental APK. + # incremental_install_json_path: Path to the incremental install json. + # native_lib_placeholders: Optional. List of placeholder filenames to add to + # the APK. + # proguard_mapping_path: Path to .mapping file produced from ProGuard step. + # shared_libraries_runtime_deps_file: Optional. Path to a file listing the + # native shared libraries required at runtime by the APK. + # secondary_abi_shared_libraries_runtime_deps_file: + # secondary_native_lib_placeholders: Optional. List of placeholder filenames + # to add to the APK for the secondary ABI. + # loadable_modules: Optional list of extra native libraries to + # be stored in the APK. + # secondary_abi_loadable_modules: Optional list of native libraries for + # secondary ABI. + # uncompress_shared_libraries: Optional. True to store native shared + # libraries uncompressed and page-aligned. + # proto_resources_path: The path of an zip archive containing the APK's + # resources compiled to the protocol buffer format (instead of regular + # binary xml + resources.arsc). + # r_text_path: The path of the R.txt file generated when compiling the + # resources for this target. + # module_pathmap_path: The path of the pathmap file generated when compiling + # the resources for the bundle module, if path shortening is enabled. + # base_allowlist_rtxt_path: The path of the R.txt file containing the + # list of string resources to keep in the base split APK for any bundle + # that uses this target. + # + # For 'java_binary' and 'junit_binary' targets only. Ignored by others: + # + # wrapper_script_name: Optional name for the generated wrapper script. + # Default is main target name. + # wrapper_script_args: Optional list of extra arguments used by the + # generated wrapper script. + # + template("java_library_impl") { + # TODO(crbug.com/1042017): Remove. + not_needed(invoker, [ "no_build_hooks" ]) + + forward_variables_from(invoker, [ "testonly" ]) + _is_prebuilt = defined(invoker.jar_path) + _is_annotation_processor = invoker.type == "java_annotation_processor" + _is_java_binary = + invoker.type == "java_binary" || invoker.type == "junit_binary" + _supports_android = + defined(invoker.supports_android) && invoker.supports_android + _requires_android = + defined(invoker.requires_android) && invoker.requires_android + + _invoker_deps = [] + if (defined(invoker.deps)) { + _invoker_deps += invoker.deps + } + if (defined(invoker.public_deps)) { + foreach(_public_dep, invoker.public_deps) { + if (filter_include([ _public_dep ], _invoker_deps) != []) { + assert(false, "'public_deps' and 'deps' overlap: $_public_dep") + } + } + _invoker_deps += invoker.public_deps + } + + _main_target_name = target_name + if (defined(invoker.main_target_name)) { + _main_target_name = invoker.main_target_name + } + + if (defined(invoker.resources_package)) { + _resources_package = invoker.resources_package + } + + _java_files = [] + if (defined(invoker.sources)) { + _java_files = invoker.sources + } + _srcjar_deps = [] + if (defined(invoker.srcjar_deps)) { + _srcjar_deps = invoker.srcjar_deps + } + _has_sources = _java_files != [] || _srcjar_deps != [] + + if (_is_prebuilt) { + assert(!_has_sources) + } else { + # Allow java_binary to not specify any sources. This is needed when a prebuilt + # is needed as a library as well as a binary. + assert(_is_annotation_processor || _is_java_binary || _has_sources) + } + + if (_is_java_binary) { + assert(defined(invoker.main_class), + "${invoker.type}() must set main_class") + } else if (_is_annotation_processor) { + assert(defined(invoker.main_class), + "java_annotation_processor() must set main_class") + } else { + assert(!defined(invoker.main_class), + "main_class cannot be used for target of type ${invoker.type}") + } + + if (defined(invoker.chromium_code)) { + _chromium_code = invoker.chromium_code + } else { + # Default based on whether target is in third_party. + _chromium_code = + filter_exclude([ get_label_info(":$_main_target_name", "dir") ], + [ "*\bthird_party\b*" ]) != [] + if (!_chromium_code && !_is_prebuilt && _java_files != []) { + # Unless third_party code has an org.chromium file in it. + _chromium_code = + filter_exclude(_java_files, [ "*\bchromium\b*" ]) != _java_files + } + } + + # Define build_config_deps which will be a list of targets required to + # build the _build_config. + _build_config = "$target_gen_dir/$_main_target_name.build_config.json" + _build_config_target_name = + "${_main_target_name}$build_config_target_suffix" + + # The only target that might have no prebuilt and no sources is a java_binary. + _build_host_jar = false + _build_device_jar = false + if (_is_prebuilt || _has_sources) { + if (defined(invoker.output_name)) { + _output_name = invoker.output_name + } else if (_is_prebuilt) { + _output_name = get_path_info(invoker.jar_path, "name") + } else { + _output_name = _main_target_name + } + + _build_host_jar = _is_java_binary || _is_annotation_processor || + invoker.type == "java_library" + _build_device_jar = + invoker.type != "system_java_library" && _supports_android + + _jacoco_instrument = + use_jacoco_coverage && _chromium_code && _java_files != [] && + _build_device_jar && (!defined(invoker.testonly) || !invoker.testonly) + if (defined(invoker.jacoco_never_instrument)) { + _jacoco_instrument = + !invoker.jacoco_never_instrument && _jacoco_instrument + } + + if (_build_host_jar) { + # Jar files can be needed at runtime (by Robolectric tests or java binaries), + # so do not put them under obj/. + # TODO(agrieve): I suspect it would be better to use dist_jar for java_binary + # rather than archiving unnecessary .jar files within lib.java. + _target_dir_name = get_label_info(":$_main_target_name", "dir") + _host_processed_jar_path = + "$root_out_dir/lib.java$_target_dir_name/$_output_name.jar" + } + if (_build_device_jar) { + _dex_path = "$target_out_dir/$_main_target_name.dex.jar" + _enable_desugar = + !defined(invoker.enable_desugar) || invoker.enable_desugar + _use_classic_desugar = + defined(invoker.use_classic_desugar) && invoker.use_classic_desugar + + # Build speed optimization: Skip "process device" step if the step + # would be just a copy and avoid the copy. + _process_device_jar = + defined(invoker.bytecode_rewriter_target) || _jacoco_instrument || + (_enable_desugar && _use_classic_desugar) || + defined(invoker.jar_excluded_patterns) || + defined(invoker.jar_included_patterns) + if (!_process_device_jar && _is_prebuilt) { + _device_processed_jar_path = invoker.jar_path + } else { + _device_processed_jar_path = + "$target_out_dir/$_output_name.processed.jar" + } + } + + # For static libraries, the javac jar output is created at the intermediate + # path so that it can be processed by another target and moved to the final + # spot that the .build_config.json knows about. Technically this should be done + # for the ijar as well, but this is only used for APK targets where + # the ijar path isn't actually used. + if (_has_sources) { + _final_ijar_path = "$target_out_dir/$_output_name.turbine.jar" + } else { + _final_ijar_path = "$target_out_dir/$_output_name.ijar.jar" + } + + if (_has_sources) { + if (_build_device_jar && !_process_device_jar) { + _javac_jar_path = _device_processed_jar_path + } else { + _javac_jar_path = "$target_out_dir/$_main_target_name.javac.jar" + } + _generated_jar_path = + "$target_gen_dir/$_main_target_name.generated.srcjar" + } + + if (_is_prebuilt) { + _unprocessed_jar_path = invoker.jar_path + } else { + _unprocessed_jar_path = _javac_jar_path + } + } + + if (_is_prebuilt || _has_sources) { + _java_res_deps = [] + _java_header_deps = [] + _java_impl_deps = [] + _non_java_deps = [] + foreach(_dep, _invoker_deps) { + _target_label = get_label_info(_dep, "label_no_toolchain") + if (filter_exclude([ _target_label ], _java_resource_patterns) == []) { + _java_res_deps += [ _dep ] + } else if (filter_exclude([ _target_label ], _java_library_patterns) == + []) { + # This is a java library dep, so it has header and impl targets. + _java_header_deps += [ "${_target_label}__header" ] + _java_impl_deps += [ "${_target_label}__impl" ] + } else { + _non_java_deps += [ _dep ] + } + } + + # Don't need to depend on the apk-under-test to be packaged. + if (defined(invoker.apk_under_test)) { + _java_header_deps += [ "${invoker.apk_under_test}__java__header" ] + _java_impl_deps += [ "${invoker.apk_under_test}__java__impl" ] + } + + # These deps cannot be passed via invoker.deps since bundle_module targets + # have bundle_module.build_config without the __java suffix, so they are + # special and cannot be passed as regular deps to write_build_config. + if (defined(invoker.base_module_target)) { + _java_header_deps += [ "${invoker.base_module_target}__java__header" ] + _java_impl_deps += [ "${invoker.base_module_target}__java__impl" ] + } + + _extra_java_deps = [] + if (_jacoco_instrument) { + _extra_java_deps += [ "//third_party/jacoco:jacocoagent_java" ] + } + + _include_android_sdk = _build_device_jar + if (defined(invoker.include_android_sdk)) { + _include_android_sdk = invoker.include_android_sdk + } + if (_include_android_sdk) { + _sdk_java_dep = "//third_party/android_sdk:android_sdk_java" + if (defined(invoker.alternative_android_sdk_dep)) { + _sdk_java_dep = invoker.alternative_android_sdk_dep + } + + # This is an android_system_java_prebuilt target, so no headers. + _extra_java_deps += [ _sdk_java_dep ] + } + + # Classpath deps is used for header and dex targets, they do not need + # resource deps. + _classpath_deps = _java_header_deps + _non_java_deps + _extra_java_deps + + [ ":$_build_config_target_name" ] + + _full_classpath_deps = + _java_impl_deps + _java_res_deps + _non_java_deps + _extra_java_deps + + [ ":$_build_config_target_name" ] + } + + # Often needed, but too hard to figure out when ahead of time. + not_needed([ + "_classpath_deps", + "_full_classpath_deps", + ]) + + if (_java_files != []) { + _java_sources_file = "$target_gen_dir/$_main_target_name.sources" + if (defined(invoker.java_sources_file)) { + _java_sources_file = invoker.java_sources_file + } + write_file(_java_sources_file, rebase_path(_java_files, root_build_dir)) + } + + write_build_config(_build_config_target_name) { + forward_variables_from(invoker, + [ + "aar_path", + "annotation_processor_deps", + "base_allowlist_rtxt_path", + "gradle_treat_as_prebuilt", + "input_jars_paths", + "low_classpath_priority", + "main_class", + "proguard_configs", + "proguard_enabled", + "proguard_mapping_path", + "public_target_label", + "r_text_path", + "type", + ]) + if (type == "android_apk" || type == "android_app_bundle_module") { + forward_variables_from( + invoker, + [ + "android_manifest", + "android_manifest_dep", + "merged_android_manifest", + "final_dex_path", + "loadable_modules", + "native_lib_placeholders", + "res_size_info_path", + "secondary_abi_loadable_modules", + "secondary_abi_shared_libraries_runtime_deps_file", + "secondary_native_lib_placeholders", + "shared_libraries_runtime_deps_file", + "static_library_dependent_targets", + "uncompress_shared_libraries", + "library_always_compress", + "library_renames", + ]) + } + if (type == "android_apk") { + forward_variables_from(invoker, + [ + "apk_path", + "apk_under_test", + "incremental_apk_path", + "incremental_install_json_path", + ]) + } + if (type == "android_app_bundle_module") { + forward_variables_from(invoker, + [ + "base_module_target", + "is_base_module", + "module_pathmap_path", + "proto_resources_path", + "version_name", + "version_code", + ]) + } + chromium_code = _chromium_code + build_config = _build_config + is_prebuilt = _is_prebuilt + + # Specifically avoid passing in invoker.base_module_target as one of the + # possible_config_deps. + possible_config_deps = _invoker_deps + if (defined(_extra_java_deps)) { + possible_config_deps += _extra_java_deps + } + if (defined(apk_under_test)) { + possible_config_deps += [ apk_under_test ] + } + + if (defined(invoker.public_deps)) { + possible_config_public_deps = invoker.public_deps + } + + supports_android = _supports_android + requires_android = _requires_android + bypass_platform_checks = defined(invoker.bypass_platform_checks) && + invoker.bypass_platform_checks + + if (defined(_resources_package)) { + custom_package = _resources_package + } + if (_is_prebuilt || _has_sources) { + ijar_path = _final_ijar_path + unprocessed_jar_path = _unprocessed_jar_path + } + if (_build_host_jar) { + host_jar_path = _host_processed_jar_path + } + if (_build_device_jar) { + device_jar_path = _device_processed_jar_path + dex_path = _dex_path + } + if (_java_files != []) { + java_sources_file = _java_sources_file + } + + bundled_srcjars = [] + foreach(d, _srcjar_deps) { + _dep_gen_dir = get_label_info(d, "target_gen_dir") + _dep_name = get_label_info(d, "name") + bundled_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ] + } + if (defined(invoker.include_java_resources) && + invoker.include_java_resources) { + java_resources_jar = _unprocessed_jar_path + if (defined(invoker.jar_path)) { + # Use original jar_path because _jar_path points to a library without + # resources. + } else { + java_resources_jar = _device_processed_jar_path + } + } + } + + if (_is_prebuilt || _has_sources) { + _header_target_name = "${target_name}__header" + } + + _public_deps = [] + _analysis_public_deps = [] + _analysis_files = [] + + if (defined(invoker.proguard_configs)) { + assert(_build_host_jar || _build_device_jar) + + # proguard_configs listed on java_library targets need to be marked + # as inputs to at least one action so that "gn analyze" will know + # about them. Although this target doesn't use them, it's a convenient spot + # to list them. + # https://crbug.com/827197 + _analysis_files = invoker.proguard_configs + _analysis_public_deps = + _non_java_deps + _srcjar_deps # For the aapt-generated + # proguard rules. + } + + if (_has_sources) { + if (defined(invoker.enable_errorprone)) { + _enable_errorprone = invoker.enable_errorprone + } else { + _enable_errorprone = + _java_files != [] && _chromium_code && use_errorprone_java_compiler + } + + _type = invoker.type + + _uses_fake_rjava = _type == "java_library" && _requires_android + + if (_uses_fake_rjava && defined(_resources_package)) { + # has _resources at the end so it looks like a resources pattern, since + # it does act like one (and other resources patterns need to depend on + # this before they can read its output R.txt). + _fake_rjava_target = "${target_name}__rjava_resources" + _possible_resource_deps = _invoker_deps + generate_r_java(_fake_rjava_target) { + deps = [ ":$_build_config_target_name" ] + if (defined(_possible_resource_deps)) { + possible_resource_deps = _possible_resource_deps + } + build_config = _build_config + + # Filepath has to be exactly this because compile_java looks for the + # srcjar of srcjar_deps at this location $gen_dir/$target_name.srcjar + srcjar_path = "$target_gen_dir/$target_name.srcjar" + package = _resources_package + } + _srcjar_deps += [ ":$_fake_rjava_target" ] + } + + template("compile_java_helper") { + _enable_errorprone = + defined(invoker.enable_errorprone) && invoker.enable_errorprone + if (_enable_errorprone) { + # Rely on the header jar to provide all .class files so that it is + # safe to omit generated files entirely for errorprone. + _filtered_java_files = + filter_exclude(_java_files, [ "$root_gen_dir*" ]) + } + if (_enable_errorprone && _filtered_java_files == []) { + # Filtering out generated files resulted in no files left. + group(target_name) { + not_needed(invoker, "*") + } + } else { + compile_java(target_name) { + forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY) + output_jar_path = invoker.output_jar_path + enable_errorprone = _enable_errorprone + use_turbine = defined(invoker.use_turbine) && invoker.use_turbine + + main_target_name = _main_target_name + build_config = _build_config + + if (_enable_errorprone) { + java_files = _filtered_java_files + } else { + java_files = _java_files + srcjar_deps = _srcjar_deps + } + + if (java_files != []) { + java_sources_file = _java_sources_file + } + chromium_code = _chromium_code + supports_android = _supports_android + requires_android = _requires_android + if (!defined(deps)) { + deps = [] + } + deps += _classpath_deps + } + } + } + _compile_java_forward_variables = [ + "additional_jar_files", + "apk_name", + "jar_excluded_patterns", + "javac_args", + "processor_args_javac", + "skip_build_server", + ] + _annotation_processor_deps = [] + if (defined(invoker.annotation_processor_deps)) { + _annotation_processor_deps = invoker.annotation_processor_deps + } + + compile_java_helper(_header_target_name) { + forward_variables_from(invoker, _compile_java_forward_variables) + use_turbine = true + output_jar_path = _final_ijar_path + generated_jar_path = _generated_jar_path + deps = _annotation_processor_deps + } + _public_deps += [ ":$_header_target_name" ] + + _compile_java_target = "${_main_target_name}__compile_java" + compile_java_helper(_compile_java_target) { + forward_variables_from(invoker, _compile_java_forward_variables) + output_jar_path = _javac_jar_path + deps = [ ":$_header_target_name" ] + header_jar_path = _final_ijar_path + generated_jar_path = _generated_jar_path + } + if (_enable_errorprone) { + _compile_java_errorprone_target = "${_main_target_name}__errorprone" + compile_java_helper(_compile_java_errorprone_target) { + forward_variables_from(invoker, _compile_java_forward_variables) + enable_errorprone = true + if (defined(invoker.errorprone_args)) { + if (!defined(javac_args)) { + javac_args = [] + } + javac_args += invoker.errorprone_args + } + deps = [ ":$_header_target_name" ] + header_jar_path = _final_ijar_path + generated_jar_path = _generated_jar_path + output_jar_path = "$target_out_dir/$target_name.errorprone.stamp" + } + _analysis_public_deps += [ ":$_compile_java_errorprone_target" ] + } + } # _has_sources + + if (_is_prebuilt || _build_device_jar || _build_host_jar) { + _unprocessed_jar_deps = [] + if (_has_sources) { + _unprocessed_jar_deps += [ ":$_compile_java_target" ] + } + } + + if (defined(invoker.bytecode_rewriter_target)) { + assert(_build_host_jar || _build_device_jar, + "A host or device jar must be created to use bytecode rewriting") + + _rewritten_jar = "$target_out_dir/${target_name}_rewritten.jar" + _rewritten_jar_target_name = "${target_name}__rewritten" + _rewriter_path = root_build_dir + "/bin/helper/" + + get_label_info(invoker.bytecode_rewriter_target, "name") + _rebased_build_config = rebase_path(_build_config, root_build_dir) + action_with_pydeps(_rewritten_jar_target_name) { + script = "//build/android/gyp/bytecode_rewriter.py" + inputs = [ + _rewriter_path, + _build_config, + _unprocessed_jar_path, + ] + outputs = [ _rewritten_jar ] + depfile = "$target_gen_dir/$target_name.d" + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--script", + rebase_path(_rewriter_path, root_build_dir), + "--classpath", + "@FileArg($_rebased_build_config:deps_info:javac_full_classpath)", + "--classpath", + "@FileArg($_rebased_build_config:android:sdk_jars)", + "--input-jar", + rebase_path(_unprocessed_jar_path, root_build_dir), + "--output-jar", + rebase_path(_rewritten_jar, root_build_dir), + ] + deps = _unprocessed_jar_deps + _full_classpath_deps + + [ invoker.bytecode_rewriter_target ] + } + + _unprocessed_jar_deps = [] + _unprocessed_jar_deps = [ ":$_rewritten_jar_target_name" ] + _unprocessed_jar_path = _rewritten_jar + } + + if (_is_prebuilt) { + generate_interface_jar(_header_target_name) { + # Always used the unfiltered .jar to create the interface jar so that + # other targets will resolve filtered classes when depending on + # BuildConfig, NativeLibraries, etc. + input_jar = _unprocessed_jar_path + output_jar = _final_ijar_path + + # Normally ijar does not require any deps, but: + # 1 - Some jars are bytecode rewritten by _unprocessed_jar_deps. + # 2 - Other jars need to be unzipped by _non_java_deps. + # 3 - It is expected that depending on a header target implies depending + # on its transitive header target deps via _java_header_deps. + deps = _unprocessed_jar_deps + _non_java_deps + _java_header_deps + } + _public_deps += [ ":$_header_target_name" ] + } + + if (_build_host_jar || _build_device_jar) { + _enable_bytecode_checks = !defined(invoker.enable_bytecode_checks) || + invoker.enable_bytecode_checks + if (_enable_bytecode_checks) { + _bytecode_checks_target = "${target_name}__validate_classpath" + bytecode_processor(_bytecode_checks_target) { + forward_variables_from(invoker, [ "missing_classes_allowlist" ]) + deps = _unprocessed_jar_deps + _full_classpath_deps + + [ ":$_build_config_target_name" ] + requires_android = _requires_android + target_label = + get_label_info(":${invoker.target_name}", "label_no_toolchain") + input_jar = _unprocessed_jar_path + build_config = _build_config + is_prebuilt = _is_prebuilt + } + _analysis_public_deps += [ ":$_bytecode_checks_target" ] + } + + if (_build_host_jar) { + _process_host_jar_target_name = "${target_name}__process_host" + process_java_library(_process_host_jar_target_name) { + forward_variables_from(invoker, + [ + "jar_excluded_patterns", + "jar_included_patterns", + ]) + input_jar_path = _unprocessed_jar_path + jar_deps = _unprocessed_jar_deps + _full_classpath_deps + is_device_jar = false + output_jar_path = _host_processed_jar_path + jacoco_instrument = _jacoco_instrument + if (_jacoco_instrument) { + java_files = _java_files + java_sources_file = _java_sources_file + } + } + _public_deps += [ ":${_process_host_jar_target_name}" ] + } + + if (_build_device_jar) { + if (_process_device_jar) { + _process_device_jar_target_name = "${target_name}__process_device" + process_java_library(_process_device_jar_target_name) { + forward_variables_from(invoker, + [ + "jar_excluded_patterns", + "jar_included_patterns", + ]) + input_jar_path = _unprocessed_jar_path + jar_deps = _unprocessed_jar_deps + _full_classpath_deps + is_device_jar = true + output_jar_path = _device_processed_jar_path + jacoco_instrument = _jacoco_instrument + if (_jacoco_instrument) { + java_files = _java_files + java_sources_file = _java_sources_file + } + enable_desugar = _enable_desugar && _use_classic_desugar + if (enable_desugar) { + build_config = _build_config + classpath_deps = _classpath_deps + forward_variables_from(invoker, [ "desugar_jars_paths" ]) + } + } + _process_device_jar_deps = [ ":${_process_device_jar_target_name}" ] + _public_deps += _process_device_jar_deps + } else { + assert(_unprocessed_jar_path == _device_processed_jar_path) + _process_device_jar_deps = + _unprocessed_jar_deps + _full_classpath_deps + } + + dex("${target_name}__dex") { + forward_variables_from(invoker, + [ + "desugar_jars_paths", + "proguard_enable_obfuscation", + "use_classic_desugar", + ]) + input_class_jars = [ _device_processed_jar_path ] + enable_desugar = _enable_desugar + ignore_desugar_missing_deps = !_enable_bytecode_checks + + # There's no value in per-class dexing prebuilts since they never + # change just one class at a time. + disable_incremental = _is_prebuilt + output = _dex_path + deps = _process_device_jar_deps + + if (enable_desugar && !_use_classic_desugar) { + # Desugaring with D8 requires full classpath. + build_config = _build_config + final_ijar_path = _final_ijar_path + deps += _classpath_deps + [ ":$_header_target_name" ] + } + + enable_multidex = false + is_library = true + } + _public_deps += [ ":${target_name}__dex" ] + } + } + + if (_is_java_binary) { + # Targets might use the generated script while building, so make it a dep + # rather than a data_dep. + java_binary_script("${target_name}__java_binary_script") { + forward_variables_from(invoker, + [ + "tiered_stop_at_level_one", + "main_class", + "wrapper_script_args", + ]) + build_config = _build_config + script_name = _main_target_name + if (defined(invoker.wrapper_script_name)) { + script_name = invoker.wrapper_script_name + } + deps = [ ":$_build_config_target_name" ] + } + _public_deps += [ ":${target_name}__java_binary_script" ] + } + + # The __impl target contains all non-analysis steps for this template. + # Having this separated out from the main target (which contains analysis + # steps) allows analysis steps for this target to be run concurrently with + # the non-analysis steps of other targets that depend on this one. + group("${target_name}__impl") { + public_deps = _public_deps + } + + java_lib_group("${target_name}__assetres") { + deps = _invoker_deps + group_name = "assetres" + + if (defined(_fake_rjava_target)) { + deps += [ ":$_fake_rjava_target" ] + } + } + + group(target_name) { + forward_variables_from(invoker, + [ + "assert_no_deps", + "data", + "data_deps", + "deps", + "public_deps", + "visibility", + ]) + if (!defined(public_deps)) { + public_deps = [] + } + public_deps += [ ":${target_name}__impl" ] + if (!defined(data)) { + data = [] + } + data += _analysis_files + if (!defined(data_deps)) { + data_deps = [] + } + data_deps += _analysis_public_deps + } + } +} + +# Create a zip archive corresponding to an application bundle module. +# +# Compile all the components of a given android_apk_or_module() target into a +# zip archive suitable to later create an android_app_bundle() target. This +# archive's format is very similar to that on an APK, except for a few +# differences in internal directory layouts, and the fact that resources, as +# well as xml files, are compiled using a protocol-buffer based format (instead +# of the regular binary xml + resources.arsc). +# +# A final application bundle is built from one or more module bundle modules, +# plus some configuration file. +# +# Variables: +# module_zip_path: Output module path. +# build_config: Path to build_config of the android_apk_or_module() target. +# dex_path: If module is proguarded separately from the base module, dex_path +# is the path to its dex file and is passed directly to the creation script. +# Otherwise, dex_path is undefined and we retrieve the module's dex file +# using its build_config. +# expected_libs_and_assets: Verify the list of included native libraries +# and assets is consistent with the given expectation file. +# expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff +# with this file as the base. +# is_multi_abi: If true will add a library placeholder for the missing ABI if +# either the primary or the secondary ABI has no native libraries set. +# module_name: The module's name. +# native_libraries_config: Path to file listing native libraries to be +# packaged into each module. +# proguard_enabled: Optional. True if proguarding is enabled for this +# bundle. Default is to enable this only for release builds. Note that +# this will always perform synchronized proguarding. +template("create_android_app_bundle_module") { + _rebased_build_config = rebase_path(invoker.build_config, root_build_dir) + _rebased_native_libraries_config = + rebase_path(invoker.native_libraries_config, root_build_dir) + _proguard_enabled = + defined(invoker.proguard_enabled) && invoker.proguard_enabled + + forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) + _deps = invoker.deps + _script = "//build/android/gyp/apkbuilder.py" + + # NOTE: Compared to the inputs of the "package_apk" template action, + # this list is much smaller, since finalize_apk is never called + # by apkbuild.py --format=bundle-module. This means not using + # apksigner and zipalign as well, nor the keystore. Other + # dependencies like extra native libraries are all pulled from the + # .build_config.json through @FileArg() references (see below) and + # will be listed in the generated depfile instead. + _inputs = [ + invoker.build_config, + invoker.native_libraries_config, + ] + _outputs = [ invoker.module_zip_path ] + _args = [ + "--format=bundle-module", + "--output-apk", + rebase_path(invoker.module_zip_path, root_build_dir), + "--resource-apk=@FileArg(" + + "$_rebased_build_config:deps_info:proto_resources_path)", + "--assets=@FileArg($_rebased_build_config:assets)", + "--uncompressed-assets=@FileArg(" + + "$_rebased_build_config:uncompressed_assets)", + "--native-libs=@FileArg($_rebased_native_libraries_config" + + ":${invoker.module_name})", + "--native-lib-placeholders=@FileArg($_rebased_build_config" + + ":native:native_library_placeholders)", + "--secondary-native-lib-placeholders=@FileArg($_rebased_build_config" + + ":native:secondary_native_library_placeholders)", + "--android-abi=$android_app_abi", + "--min-sdk-version=${invoker.min_sdk_version}", + "--uncompress-shared-libraries=@FileArg(" + + "$_rebased_build_config:native:uncompress_shared_libraries)", + "--library-always-compress=@FileArg($_rebased_build_config:native:library_always_compress)", + "--library-renames=@FileArg($_rebased_build_config:native:library_renames)", + ] + if (defined(android_app_secondary_abi)) { + _rebased_secondary_abi_native_libraries_config = + rebase_path(invoker.secondary_abi_native_libraries_config, + root_build_dir) + _args += [ + "--secondary-native-libs", + "@FileArg($_rebased_secondary_abi_native_libraries_config" + + ":${invoker.module_name})", + "--secondary-android-abi=$android_app_secondary_abi", + ] + } + if (defined(invoker.is_multi_abi) && invoker.is_multi_abi) { + _args += [ "--is-multi-abi" ] + } + if (defined(invoker.uncompress_dex) && invoker.uncompress_dex) { + _args += [ "--uncompress-dex" ] + } + + # Use either provided dex path or build config path based on type of module. + if (defined(invoker.dex_path)) { + _inputs += [ invoker.dex_path ] + _rebased_dex_path = rebase_path(invoker.dex_path, root_build_dir) + _args += [ "--dex-file=$_rebased_dex_path" ] + } else { + _args += [ "--dex-file=@FileArg($_rebased_build_config:final_dex:path)" ] + } + + # The library is imported via proguard when proguard is enabled. + if (!_proguard_enabled && enable_jdk_library_desugaring && + invoker.module_name == "base") { + _all_jdk_libs = "//build/android:all_jdk_libs" + _deps += [ _all_jdk_libs ] + _jdk_libs_dex = + get_label_info(_all_jdk_libs, "target_out_dir") + "/all_jdk_libs.l8.dex" + _inputs += [ _jdk_libs_dex ] + _args += [ + "--jdk-libs-dex-file", + rebase_path(_jdk_libs_dex, root_build_dir), + ] + } + + if (treat_warnings_as_errors) { + _args += [ "--warnings-as-errors" ] + } + + if (defined(invoker.expected_libs_and_assets)) { + _expectations_target = "${invoker.top_target_name}_validate_libs_and_assets" + action_with_pydeps(_expectations_target) { + _actual_file = "$target_gen_dir/$target_name.libs_and_assets" + _failure_file = "$expectations_failure_dir/" + + string_replace(invoker.expected_libs_and_assets, "/", "_") + inputs = [ + invoker.expected_libs_and_assets, + invoker.build_config, + invoker.native_libraries_config, + ] + deps = [ + invoker.build_config_target, + invoker.native_libraries_config_target, + ] + if (defined(android_app_secondary_abi)) { + inputs += [ invoker.secondary_abi_native_libraries_config ] + deps += [ invoker.secondary_abi_native_libraries_config_target ] + } + outputs = [ + _actual_file, + _failure_file, + ] + script = _script + args = _args + [ + "--expected-file", + rebase_path(invoker.expected_libs_and_assets, root_build_dir), + "--actual-file", + rebase_path(_actual_file, root_build_dir), + "--failure-file", + rebase_path(_failure_file, root_build_dir), + "--only-verify-expectations", + ] + if (defined(invoker.expected_libs_and_assets_base)) { + inputs += [ invoker.expected_libs_and_assets_base ] + args += [ + "--expected-file-base", + rebase_path(invoker.expected_libs_and_assets_base, root_build_dir), + ] + } + if (fail_on_android_expectations) { + args += [ "--fail-on-expectations" ] + } + } + _deps += [ ":$_expectations_target" ] + } + + action_with_pydeps(target_name) { + deps = _deps + inputs = _inputs + outputs = _outputs + script = _script + depfile = "$target_gen_dir/$target_name.d" + args = _args + [ + "--depfile", + rebase_path(depfile, root_build_dir), + ] + } +} + +# Splits input dex file(s) based on given feature jars into seperate dex files +# for each feature. +# +# Variables: +# proguard_mapping: Path to input proguard mapping produced by synchronized +# proguarding. +# input_dex_zip: Path to zipped dex files to split. +# all_modules: Path to list of all modules. Each Module must have +# build_config, name, and build_config_target properties. +# feature_jars_args: Optional list of args to be passed to dexsplitter.py. +# If used should include the jars owned by each feature (in the same order +# as all_modules). Allows invoker to pull the list of jars from a different +# .build_config.json than the module's .build_config. +template("dexsplitter") { + action_with_pydeps(target_name) { + forward_variables_from(invoker, [ "deps" ]) + script = "//build/android/gyp/dexsplitter.py" + _stamp = "${target_gen_dir}/${target_name}.stamp" + outputs = [ _stamp ] + + depfile = "${target_gen_dir}/${target_name}.d" + args = [ + "--stamp", + rebase_path(_stamp, root_build_dir), + "--depfile", + rebase_path(depfile, root_build_dir), + "--r8-path", + rebase_path(_r8_path, root_build_dir), + "--input-dex-zip", + rebase_path(invoker.input_dex_zip, root_build_dir), + "--proguard-mapping-file", + rebase_path(invoker.proguard_mapping, root_build_dir), + ] + + foreach(_feature_module, invoker.all_modules) { + _rebased_module_build_config = + rebase_path(_feature_module.build_config, root_build_dir) + args += [ + "--feature-name", + _feature_module.name, + "--dex-dest=@FileArg($_rebased_module_build_config:final_dex:path)", + ] + if (!defined(invoker.feature_jars_args)) { + args += [ "--feature-jars=@FileArg($_rebased_module_build_config:deps_info:device_classpath)" ] + } + deps += [ _feature_module.build_config_target ] + } + if (defined(invoker.feature_jars_args)) { + args += invoker.feature_jars_args + } + } +} + +# Allots native libraries depended on by feature modules to the module the +# libraries should be packaged into. The packaging module may be different from +# the dependee module in case a library is depended on by multiple modules. In +# that case the library will be allotted to the closest ancestor given a module +# dependency tree (see |parent| below). +# +# Variables: +# modules: List of scopes with the following format: +# name: The module's name. +# parent: The module's parent's name. +# build_config: Path to the module's build config. +# build_config_target: Target creating |build_config|. +# native_libraries_filearg_keys: Keys to be used in +# @FileArg(|build_config|:<keys>) expressions pointing to a list of native +# libraries to consider in |build_config|. +# output: Path to native libraries config. +template("allot_native_libraries") { + action_with_pydeps(target_name) { + script = "//build/android/gyp/allot_native_libraries.py" + args = [ + "--output", + rebase_path(invoker.output, root_build_dir), + ] + outputs = [ invoker.output ] + deps = [] + if (defined(invoker.deps)) { + deps += invoker.deps + } + inputs = [] + foreach(_module, invoker.modules) { + deps += [ _module.build_config_target ] + inputs += [ _module.build_config ] + _rebased_build_config = rebase_path(_module.build_config, root_out_dir) + foreach(_key, invoker.native_libraries_filearg_keys) { + args += [ + "--libraries", + "${_module.name},@FileArg($_rebased_build_config:$_key)", + ] + } + if (defined(_module.parent)) { + args += [ + "--dep", + "${_module.parent}:${_module.name}", + ] + } + } + } +} |