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): ...@@ -29,30 +29,44 @@ def _ParseArgs(args):
return options return options
def main(args): def DexJdkLibJar(r8_path, min_api, desugar_jdk_libs_json, desugar_jdk_libs_jar,
options = _ParseArgs(args) keep_rule_file, output):
# TODO(agrieve): Spews a lot of stderr about missing classes. # TODO(agrieve): Spews a lot of stderr about missing classes.
with build_utils.TempDir() as tmp_dir: with build_utils.TempDir() as tmp_dir:
cmd = [ cmd = [
build_utils.JAVA_PATH, build_utils.JAVA_PATH,
'-jar', '-jar',
options.r8_path, r8_path,
'l8', 'l8',
'--min-api', '--min-api',
options.min_api, min_api,
#'--lib', build_utils.JAVA_HOME, '--lib',
build_utils.JAVA_HOME,
'--desugared-lib', '--desugared-lib',
options.desugar_jdk_libs_json, desugar_jdk_libs_json,
'--output',
tmp_dir,
options.desugar_jdk_libs_jar
] ]
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')): if os.path.exists(os.path.join(tmp_dir, 'classes2.dex')):
raise Exception('Achievement unlocked: desugar_jdk_libs is multidex!') 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__': if __name__ == '__main__':
......
...@@ -12,6 +12,7 @@ import sys ...@@ -12,6 +12,7 @@ import sys
import tempfile import tempfile
import zipfile import zipfile
import dex_jdk_libs
from util import build_utils from util import build_utils
from util import diff_utils from util import diff_utils
...@@ -116,6 +117,8 @@ def _ParseOptions(): ...@@ -116,6 +117,8 @@ def _ParseOptions():
action='append', action='append',
required=True, required=True,
help='GN-list of .jar files to optimize.') 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('--output-path', help='Path to the generated .jar file.')
parser.add_argument( parser.add_argument(
'--proguard-configs', '--proguard-configs',
...@@ -196,6 +199,8 @@ def _ParseOptions(): ...@@ -196,6 +199,8 @@ def _ParseOptions():
parser.add_argument( parser.add_argument(
'--stamp', '--stamp',
help='File to touch upon success. Mutually exclusive with --output-path') 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) options = parser.parse_args(args)
...@@ -265,12 +270,18 @@ class _DexPathContext(object): ...@@ -265,12 +270,18 @@ class _DexPathContext(object):
self.staging_dir = os.path.join(work_dir, name) self.staging_dir = os.path.join(work_dir, name)
os.mkdir(self.staging_dir) 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) found_files = build_utils.FindInDirectory(self.staging_dir)
if not found_files: if not found_files:
raise Exception('Missing dex outputs in {}'.format(self.staging_dir)) raise Exception('Missing dex outputs in {}'.format(self.staging_dir))
if self._final_output_path.endswith('.dex'): 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: if len(found_files) != 1:
raise Exception('Expected exactly 1 dex file output, found: {}'.format( raise Exception('Expected exactly 1 dex file output, found: {}'.format(
'\t'.join(found_files))) '\t'.join(found_files)))
...@@ -330,7 +341,12 @@ def _OptimizeWithR8(options, ...@@ -330,7 +341,12 @@ def _OptimizeWithR8(options,
] ]
if options.desugar_jdk_libs_json: 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: if options.min_api:
cmd += ['--min-api', options.min_api] cmd += ['--min-api', options.min_api]
...@@ -379,7 +395,18 @@ def _OptimizeWithR8(options, ...@@ -379,7 +395,18 @@ def _OptimizeWithR8(options,
'android/docs/java_optimization.md#Debugging-common-failures\n')) 'android/docs/java_optimization.md#Debugging-common-failures\n'))
raise ProguardProcessError(err, debugging_link) 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: for feature in feature_contexts:
feature.CreateOutput() feature.CreateOutput()
......
# Generated by running: # Generated by running:
# build/print_python_deps.py --root build/android/gyp --output build/android/gyp/proguard.pydeps build/android/gyp/proguard.py # build/print_python_deps.py --root build/android/gyp --output build/android/gyp/proguard.pydeps build/android/gyp/proguard.py
../../gn_helpers.py ../../gn_helpers.py
dex_jdk_libs.py
proguard.py proguard.py
util/__init__.py util/__init__.py
util/build_utils.py util/build_utils.py
......
...@@ -236,7 +236,7 @@ if (is_android || is_chromeos) { ...@@ -236,7 +236,7 @@ if (is_android || is_chromeos) {
enable_bazel_desugar = true enable_bazel_desugar = true
# Enables Java library desugaring. # 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 enable_jdk_library_desugaring = false
} }
......
...@@ -1085,6 +1085,19 @@ if (enable_java_templates) { ...@@ -1085,6 +1085,19 @@ if (enable_java_templates) {
rebase_path(_desugar_jdk_libs_json, root_build_dir), rebase_path(_desugar_jdk_libs_json, root_build_dir),
] ]
_inputs += [ _desugar_jdk_libs_json ] _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 _enable_assert = is_java_debug || dcheck_always_on
...@@ -3826,10 +3839,15 @@ if (enable_java_templates) { ...@@ -3826,10 +3839,15 @@ if (enable_java_templates) {
# verify_native_libs_and_assets_target_name: (optional): If set, will verify # verify_native_libs_and_assets_target_name: (optional): If set, will verify
# the list of included native libraries and assets is consistent with an # the list of included native libraries and assets is consistent with an
# expectation file. # 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") { template("create_android_app_bundle_module") {
_rebased_build_config = rebase_path(invoker.build_config, root_build_dir) _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
_rebased_native_libraries_config = _rebased_native_libraries_config =
rebase_path(invoker.native_libraries_config, root_build_dir) rebase_path(invoker.native_libraries_config, root_build_dir)
_proguard_enabled =
defined(invoker.proguard_enabled) && invoker.proguard_enabled
forward_variables_from(invoker, forward_variables_from(invoker,
[ [
...@@ -3900,8 +3918,9 @@ template("create_android_app_bundle_module") { ...@@ -3900,8 +3918,9 @@ template("create_android_app_bundle_module") {
_args += [ "--dex-file=@FileArg($_rebased_build_config:final_dex:path)" ] _args += [ "--dex-file=@FileArg($_rebased_build_config:final_dex:path)" ]
} }
# TODO(https://crbug.com/1056751): Add support for proguarding jdk libs. # The library is imported via proguard when proguard is enabled.
if (enable_jdk_library_desugaring && invoker.module_name == "base") { if (!_proguard_enabled && enable_jdk_library_desugaring &&
invoker.module_name == "base") {
_all_jdk_libs = "//build/android:all_jdk_libs" _all_jdk_libs = "//build/android:all_jdk_libs"
_deps += [ _all_jdk_libs ] _deps += [ _all_jdk_libs ]
_jdk_libs_dex = _jdk_libs_dex =
......
...@@ -3163,8 +3163,7 @@ if (enable_java_templates) { ...@@ -3163,8 +3163,7 @@ if (enable_java_templates) {
deps = _deps + [ ":$_build_config_target" ] deps = _deps + [ ":$_build_config_target" ]
# TODO(https://crbug.com/1056751): Add support for proguarding jdk libs. if (!_proguard_enabled && enable_jdk_library_desugaring) {
if (enable_jdk_library_desugaring) {
_all_jdk_libs = "//build/android:all_jdk_libs" _all_jdk_libs = "//build/android:all_jdk_libs"
deps += [ _all_jdk_libs ] deps += [ _all_jdk_libs ]
jdk_libs_dex = get_label_info(_all_jdk_libs, "target_out_dir") + jdk_libs_dex = get_label_info(_all_jdk_libs, "target_out_dir") +
...@@ -4706,6 +4705,7 @@ if (enable_java_templates) { ...@@ -4706,6 +4705,7 @@ if (enable_java_templates) {
"is_multi_abi", "is_multi_abi",
"min_sdk_version", "min_sdk_version",
"uncompress_dex", "uncompress_dex",
"proguard_enabled",
]) ])
module_name = _module.name module_name = _module.name
build_config = _module_build_config 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