Commit dee2c037 authored by Eric Stevenson's avatar Eric Stevenson Committed by Commit Bot

Trichrome proguard: Implement majority of .build_config changes.

This CL changes the way static library APK .build configs are
structured. Specifically:
  * JNI sources, Proguard flags, and Proguard classpath jars are
    combined from deps that use the static library
  * The .build_config contains a mapping of .build_config paths ->
    classpath for that target that can be used when dexsplitting

Bug: 901465
Change-Id: Ie83e071b3f6482cab594b3b0e735ad4ef8313bbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1536306
Commit-Queue: Eric Stevenson <estevenson@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#644316}
parent b80d9e96
......@@ -537,8 +537,10 @@ invoking `javac`.
This type corresponds to an Android app bundle (`.aab` file).
--------------- END_MARKDOWN ---------------------------------------------------
TODO(estevenson): Add docs for static library synchronized proguarding.
"""
import collections
import itertools
import json
import optparse
......@@ -903,6 +905,10 @@ def main(argv):
parser.add_option('--incremental-install-json-path',
help="Path to the target's generated incremental install "
"json.")
parser.add_option(
'--static-library-dependent-configs',
help='GN list of .build_configs of targets that use this target as a '
'static library.')
parser.add_option('--tested-apk-config',
help='Path to the build config of the tested apk (for an instrumentation '
......@@ -1008,6 +1014,19 @@ def main(argv):
'java_library', 'android_apk', 'dist_aar', 'dist_jar',
'system_java_library', 'android_app_bundle_module')
is_static_library_dex_provider_target = (
options.static_library_dependent_configs and options.proguard_enabled)
if is_static_library_dex_provider_target and options.type != 'android_apk':
raise Exception(
'--static-library-dependent-configs only supports --type=android_apk')
options.static_library_dependent_configs = build_utils.ParseGnList(
options.static_library_dependent_configs)
static_library_dependent_configs_by_path = {
p: GetDepConfig(p)
for p in options.static_library_dependent_configs
}
deps = _DepsFromPaths(
build_utils.ParseGnList(options.deps_configs), options.type)
processor_deps = _DepsFromPaths(
......@@ -1017,9 +1036,10 @@ def main(argv):
build_utils.ParseGnList(options.classpath_deps_configs or ''),
options.type)
all_inputs = sorted(set(deps.AllConfigPaths() +
processor_deps.AllConfigPaths() +
classpath_deps.AllConfigPaths()))
all_inputs = sorted(
set(deps.AllConfigPaths() + processor_deps.AllConfigPaths() +
classpath_deps.AllConfigPaths() +
list(static_library_dependent_configs_by_path)))
system_library_deps = deps.Direct('system_java_library')
direct_library_deps = deps.Direct('java_library')
......@@ -1085,12 +1105,11 @@ def main(argv):
# opposed to groups and libraries.
if is_apk_or_module_target or options.type in (
'group', 'java_library', 'junit_binary'):
config['jni'] = {}
deps_info['jni'] = {}
all_java_sources = [c['java_sources_file'] for c in all_library_deps
if 'java_sources_file' in c]
if options.java_sources_file:
all_java_sources.append(options.java_sources_file)
config['jni']['all_source'] = all_java_sources
if options.apk_proto_resources:
deps_info['proto_resources_path'] = options.apk_proto_resources
......@@ -1314,6 +1333,52 @@ def main(argv):
c for c in d.get('java_runtime_classpath', [])
if c not in java_full_classpath)
all_configs = build_utils.ParseGnList(options.proguard_configs)
deps_info['proguard_configs'] = list(all_configs)
extra_jars = []
if is_static_library_dex_provider_target:
# Map classpath entries to configs that include them in their classpath.
configs_by_classpath_entry = collections.defaultdict(list)
for config_path, dep_config in (
static_library_dependent_configs_by_path.iteritems()):
all_configs.extend(dep_config['proguard_all_configs'])
extra_jars.extend(dep_config['proguard_classpath_jars'])
all_java_sources.extend(dep_config['jni']['all_source'])
for cp_entry in dep_config['java_runtime_classpath']:
# The APK Java targets for the static library dependent targets will
# have some of the same classes (R.java) due to shared resource
# dependencies. To avoid Proguard failures due to duplicate classes, we
# merge the APK jars into the static library's jar_path as a
# preprocessing build step.
if cp_entry != dep_config['jar_path']:
configs_by_classpath_entry[cp_entry].append(config_path)
for cp_entry in java_full_classpath:
configs_by_classpath_entry[cp_entry].append(options.build_config)
# Map configs to classpath entries that should be included in their final
# dex.
classpath_entries_by_owning_config = collections.defaultdict(list)
for cp_entry, candidate_configs in configs_by_classpath_entry.iteritems():
config_path = (candidate_configs[0]
if len(candidate_configs) == 1 else options.build_config)
classpath_entries_by_owning_config[config_path].append(cp_entry)
java_full_classpath.append(cp_entry)
classpath_entries_by_owning_config[options.build_config].append(
deps_info['jar_path'])
java_full_classpath = sorted(set(java_full_classpath))
deps_info['static_library_dependent_classpath_configs'] = {
path: sorted(set(classpath))
for path, classpath in classpath_entries_by_owning_config.iteritems()
}
if is_apk_or_module_target or options.type in ('group', 'java_library',
'junit_binary'):
deps_info['jni']['all_source'] = sorted(set(all_java_sources))
system_jars = [c['jar_path'] for c in system_library_deps]
system_interface_jars = [c['interface_jar_path'] for c in system_library_deps]
if system_library_deps:
......@@ -1321,14 +1386,8 @@ def main(argv):
config['android']['sdk_interface_jars'] = system_interface_jars
config['android']['sdk_jars'] = system_jars
if options.proguard_configs:
deps_info['proguard_configs'] = (
build_utils.ParseGnList(options.proguard_configs))
if options.type in ('android_apk', 'dist_aar',
'dist_jar', 'android_app_bundle_module', 'android_app_bundle'):
all_configs = deps_info.get('proguard_configs', [])
extra_jars = list()
for c in all_library_deps:
all_configs.extend(
p for p in c.get('proguard_configs', []) if p not in all_configs)
......@@ -1341,13 +1400,13 @@ def main(argv):
for c in deps.Direct('android_app_bundle_module'):
all_configs.extend(
p for p in c.get('proguard_configs', []) if p not in all_configs)
deps_info['proguard_all_configs'] = all_configs
deps_info['proguard_all_configs'] = sorted(set(all_configs))
if options.type == 'android_app_bundle':
for d in deps.Direct('android_app_bundle_module'):
extra_jars.extend(
c for c in d.get('proguard_classpath_jars', [])
if c not in extra_jars)
deps_info['proguard_classpath_jars'] = extra_jars
deps_info['proguard_classpath_jars'] = sorted(set(extra_jars))
if options.type == 'android_app_bundle':
deps_proguard_enabled = []
......
......@@ -456,6 +456,18 @@ template("write_build_config") {
rebase_path(invoker.proguard_configs, root_build_dir)
args += [ "--proguard-configs=$_rebased_proguard_configs" ]
}
if (defined(invoker.static_library_dependent_targets)) {
_dependent_configs = []
foreach(d, invoker.static_library_dependent_targets) {
_target_label = get_label_info(d, "label_no_toolchain")
deps += [ "$_target_label$build_config_target_suffix" ]
_dep_gen_dir = get_label_info(d, "target_gen_dir")
_dep_name = get_label_info(d, "name")
_dependent_configs += [ "$_dep_gen_dir/$_dep_name.build_config" ]
}
_rebased_depdent_configs = rebase_path(_dependent_configs, root_build_dir)
args += [ "--static-library-dependent-configs=$_rebased_depdent_configs" ]
}
if (defined(invoker.gradle_treat_as_prebuilt) &&
invoker.gradle_treat_as_prebuilt) {
args += [ "--gradle-treat-as-prebuilt" ]
......@@ -3318,6 +3330,7 @@ if (enable_java_templates) {
"secondary_abi_shared_libraries_runtime_deps_file",
"secondary_native_lib_placeholders",
"shared_libraries_runtime_deps_file",
"static_library_dependent_targets",
"uncompress_shared_libraries",
])
}
......
......@@ -445,7 +445,7 @@ if (enable_java_templates) {
args = [
# This is a list of .sources files.
"--sources-files=@FileArg($_rebased_build_config:jni:all_source)",
"--sources-files=@FileArg($_rebased_build_config:deps_info:jni:all_source)",
"--srcjar-path",
rebase_path(_srcjar_output, root_build_dir),
"--depfile",
......@@ -2593,6 +2593,7 @@ if (enable_java_templates) {
"no_build_hooks",
"secondary_abi_loadable_modules",
"secondary_native_lib_placeholders",
"static_library_dependent_targets",
])
if (_is_bundle_module) {
type = "android_app_bundle_module"
......@@ -3106,6 +3107,7 @@ if (enable_java_templates) {
"shared_resources_whitelist_locales",
"shared_resources_whitelist_target",
"srcjar_deps",
"static_library_dependent_targets",
"support_zh_hk",
"testonly",
"uncompress_shared_libraries",
......
......@@ -1785,6 +1785,12 @@ if (public_android_sdk) {
apk_name = "TrichromeLibrary"
android_manifest = trichrome_library_android_manifest
android_manifest_dep = ":trichrome_library_android_manifest"
if (trichrome_synchronized_proguard) {
static_library_dependent_targets = [
":trichrome_chrome_apk",
"//android_webview:trichrome_webview_apk",
]
}
}
}
......
......@@ -46,6 +46,7 @@ template("trichrome_library_apk_tmpl") {
"apk_name",
"android_manifest",
"android_manifest_dep",
"static_library_dependent_targets",
])
# TODO(torne): this contains the list of locales amongst other things.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment