Commit 2d66d8eb authored by Henry Jian's avatar Henry Jian Committed by Commit Bot

Make R8 dump keep rule file and use it on L8

Bug: 1056751
Change-Id: Ifbf0eaed0c6a99725893e798df56298768085474
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2194071
Commit-Queue: Henry Jian <hzjian@google.com>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#771585}
parent d67cf55d
......@@ -29,30 +29,44 @@ def _ParseArgs(args):
return options
def main(args):
options = _ParseArgs(args)
def DexJdkLibJar(r8_path, min_api, desugar_jdk_libs_json, desugar_jdk_libs_jar,
keep_rule_file, output):
# TODO(agrieve): Spews a lot of stderr about missing classes.
with build_utils.TempDir() as tmp_dir:
cmd = [
build_utils.JAVA_PATH,
'-jar',
options.r8_path,
r8_path,
'l8',
'--min-api',
options.min_api,
#'--lib', build_utils.JAVA_HOME,
min_api,
'--lib',
build_utils.JAVA_HOME,
'--desugared-lib',
options.desugar_jdk_libs_json,
'--output',
tmp_dir,
options.desugar_jdk_libs_jar
desugar_jdk_libs_json,
]
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
if keep_rule_file:
cmd += ['--pg-conf', keep_rule_file]
cmd += ['--output', tmp_dir, desugar_jdk_libs_jar]
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
if os.path.exists(os.path.join(tmp_dir, 'classes2.dex')):
raise Exception('Achievement unlocked: desugar_jdk_libs is multidex!')
shutil.move(os.path.join(tmp_dir, 'classes.dex'), options.output)
# classes.dex might not exists if the "desugar_jdk_libs_jar" is not used
# at all.
if os.path.exists(os.path.join(tmp_dir, 'classes.dex')):
shutil.move(os.path.join(tmp_dir, 'classes.dex'), output)
return True
return False
def main(args):
options = _ParseArgs(args)
DexJdkLibJar(options.r8_path, options.min_api, options.desugar_jdk_libs_json,
options.desugar_jdk_libs_jar, None, options.output)
if __name__ == '__main__':
......
......@@ -12,6 +12,7 @@ import sys
import tempfile
import zipfile
import dex_jdk_libs
from util import build_utils
from util import diff_utils
......@@ -116,6 +117,8 @@ def _ParseOptions():
action='append',
required=True,
help='GN-list of .jar files to optimize.')
parser.add_argument('--desugar-jdk-libs-jar',
help='Path to desugar_jdk_libs.jar.')
parser.add_argument('--output-path', help='Path to the generated .jar file.')
parser.add_argument(
'--proguard-configs',
......@@ -196,6 +199,8 @@ def _ParseOptions():
parser.add_argument(
'--stamp',
help='File to touch upon success. Mutually exclusive with --output-path')
parser.add_argument('--desugared-library-keep-rule-output',
help='Path to desugared library keep rule output file.')
options = parser.parse_args(args)
......@@ -265,12 +270,18 @@ class _DexPathContext(object):
self.staging_dir = os.path.join(work_dir, name)
os.mkdir(self.staging_dir)
def CreateOutput(self):
def CreateOutput(self, has_imported_lib=False, keep_rule_output=None):
found_files = build_utils.FindInDirectory(self.staging_dir)
if not found_files:
raise Exception('Missing dex outputs in {}'.format(self.staging_dir))
if self._final_output_path.endswith('.dex'):
if has_imported_lib:
raise Exception(
'Trying to create a single .dex file, but a dependency requires '
'JDK Library Desugaring (which necessitates a second file).'
'Refer to %s to see what desugaring was required' %
keep_rule_output)
if len(found_files) != 1:
raise Exception('Expected exactly 1 dex file output, found: {}'.format(
'\t'.join(found_files)))
......@@ -330,7 +341,12 @@ def _OptimizeWithR8(options,
]
if options.desugar_jdk_libs_json:
cmd += ['--desugared-lib', options.desugar_jdk_libs_json]
cmd += [
'--desugared-lib',
options.desugar_jdk_libs_json,
'--desugared-library-keep-rule-output',
options.desugared_library_keep_rule_output,
]
if options.min_api:
cmd += ['--min-api', options.min_api]
......@@ -379,7 +395,18 @@ def _OptimizeWithR8(options,
'android/docs/java_optimization.md#Debugging-common-failures\n'))
raise ProguardProcessError(err, debugging_link)
base_dex_context.CreateOutput()
base_has_imported_lib = False
if options.desugar_jdk_libs_json:
existing_files = build_utils.FindInDirectory(base_dex_context.staging_dir)
base_has_imported_lib = dex_jdk_libs.DexJdkLibJar(
options.r8_path, options.min_api, options.desugar_jdk_libs_json,
options.desugar_jdk_libs_jar,
options.desugared_library_keep_rule_output,
os.path.join(base_dex_context.staging_dir,
'classes%d.dex' % (len(existing_files) + 1)))
base_dex_context.CreateOutput(base_has_imported_lib,
options.desugared_library_keep_rule_output)
for feature in feature_contexts:
feature.CreateOutput()
......
# Generated by running:
# build/print_python_deps.py --root build/android/gyp --output build/android/gyp/proguard.pydeps build/android/gyp/proguard.py
../../gn_helpers.py
dex_jdk_libs.py
proguard.py
util/__init__.py
util/build_utils.py
......
......@@ -236,7 +236,7 @@ if (is_android || is_chromeos) {
enable_bazel_desugar = true
# Enables Java library desugaring.
# This will cause an extra 1MB classes.dex file to appear in every apk.
# This will cause an extra classes.dex file to appear in every apk.
enable_jdk_library_desugaring = false
}
......
......@@ -1085,6 +1085,19 @@ if (enable_java_templates) {
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),
]
_inputs += [ _desugar_jdk_libs_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),
]
}
_enable_assert = is_java_debug || dcheck_always_on
......@@ -3826,10 +3839,15 @@ if (enable_java_templates) {
# verify_native_libs_and_assets_target_name: (optional): If set, will verify
# the list of included native libraries and assets is consistent with an
# expectation file.
# 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,
[
......@@ -3900,8 +3918,9 @@ template("create_android_app_bundle_module") {
_args += [ "--dex-file=@FileArg($_rebased_build_config:final_dex:path)" ]
}
# TODO(https://crbug.com/1056751): Add support for proguarding jdk libs.
if (enable_jdk_library_desugaring && invoker.module_name == "base") {
# 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 =
......
......@@ -3163,8 +3163,7 @@ if (enable_java_templates) {
deps = _deps + [ ":$_build_config_target" ]
# TODO(https://crbug.com/1056751): Add support for proguarding jdk libs.
if (enable_jdk_library_desugaring) {
if (!_proguard_enabled && enable_jdk_library_desugaring) {
_all_jdk_libs = "//build/android:all_jdk_libs"
deps += [ _all_jdk_libs ]
jdk_libs_dex = get_label_info(_all_jdk_libs, "target_out_dir") +
......@@ -4706,6 +4705,7 @@ if (enable_java_templates) {
"is_multi_abi",
"min_sdk_version",
"uncompress_dex",
"proguard_enabled",
])
module_name = _module.name
build_config = _module_build_config
......
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