Commit 35fc212e authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

dex.py: Remove obsolete flags, code clean-up.

Change-Id: I20d7316522446b934583fb23ff34f9212b0fb346
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1694090
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarSam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#676101}
parent dde83b31
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import json import argparse
import logging import logging
import optparse
import os import os
import re import re
import shutil import shutil
...@@ -24,76 +23,52 @@ import convert_dex_profile ...@@ -24,76 +23,52 @@ import convert_dex_profile
_FIXED_ZIP_HEADER_LEN = 30 _FIXED_ZIP_HEADER_LEN = 30
def _CheckFilePathEndsWithJar(parser, file_path):
if not file_path.endswith(".jar"):
parser.error("%s does not end in .jar" % file_path)
def _CheckFilePathsEndWithJar(parser, file_paths):
for file_path in file_paths:
_CheckFilePathEndsWithJar(parser, file_path)
def _ParseArgs(args): def _ParseArgs(args):
args = build_utils.ExpandFileArgs(args) args = build_utils.ExpandFileArgs(args)
parser = argparse.ArgumentParser()
parser = optparse.OptionParser()
build_utils.AddDepfileOption(parser) build_utils.AddDepfileOption(parser)
parser.add_argument('--output', required=True, help='Dex output path.')
parser.add_option('--output-directory', parser.add_argument('--input-list', help='GN-list of additional input paths.')
default=os.getcwd(), parser.add_argument(
help='Path to the output build directory.') '--main-dex-list-path',
parser.add_option('--dex-path', help='Dex output path.') help='File containing a list of the classes to include in the main dex.')
parser.add_option('--configuration-name', parser.add_argument(
help='The build CONFIGURATION_NAME.') '--multi-dex',
parser.add_option('--proguard-enabled', action='store_true',
help='"true" if proguard is enabled.') help='Allow multiple dex files within output.')
parser.add_option('--debug-build-proguard-enabled', parser.add_argument('--d8-jar-path', required=True, help='Path to D8 jar.')
help='"true" if proguard is enabled for debug build.') parser.add_argument(
parser.add_option('--proguard-enabled-input-path', '--release',
help=('Path to dex in Release mode when proguard ' action='store_true',
'is enabled.')) help='Run D8 in release mode. Release mode maximises main dex and '
parser.add_option('--inputs', help='A list of additional input paths.') 'deletes non-essential line number information (vs debug which minimizes '
parser.add_option('--excluded-paths', 'main dex and keeps all line number information, and then some.')
help='A list of paths to exclude from the dex file.') parser.add_argument(
parser.add_option('--main-dex-list-path', '--min-api', help='Minimum Android API level compatibility.')
help='A file containing a list of the classes to ' parser.add_argument('inputs', nargs='*', help='Input .jar files.')
'include in the main dex.')
parser.add_option('--multidex-configuration-path', group = parser.add_argument_group('Dexlayout')
help='A JSON file containing multidex build configuration.') group.add_argument(
parser.add_option('--multi-dex', default=False, action='store_true', '--dexlayout-profile',
help='Generate multiple dex files.') help=('Text profile for dexlayout. If present, a dexlayout '
parser.add_option('--d8-jar-path', help='Path to D8 jar.') 'pass will happen'))
parser.add_option('--release', action='store_true', default=False, group.add_argument(
help='Run D8 in release mode. Release mode maximises main ' '--profman-path',
'dex and deletes non-essential line number information ' help=('Path to ART profman binary. There should be a lib/ directory at '
'(vs debug which minimizes main dex and keeps all line ' 'the same path with shared libraries (shared with dexlayout).'))
'number information, and then some.') group.add_argument(
parser.add_option('--min-api', '--dexlayout-path',
help='Minimum Android API level compatibility.') help=('Path to ART dexlayout binary. There should be a lib/ directory at '
'the same path with shared libraries (shared with dexlayout).'))
parser.add_option('--dexlayout-profile', group.add_argument('--dexdump-path', help='Path to dexdump binary.')
help=('Text profile for dexlayout. If present, a dexlayout ' group.add_argument(
'pass will happen'))
parser.add_option('--profman-path',
help=('Path to ART profman binary. There should be a '
'lib/ directory at the same path containing shared '
'libraries (shared with dexlayout).'))
parser.add_option('--dexlayout-path',
help=('Path to ART dexlayout binary. There should be a '
'lib/ directory at the same path containing shared '
'libraries (shared with dexlayout).'))
parser.add_option('--dexdump-path', help='Path to dexdump binary.')
parser.add_option(
'--proguard-mapping-path', '--proguard-mapping-path',
help=('Path to proguard map from obfuscated symbols in the jar to ' help=('Path to proguard map from obfuscated symbols in the jar to '
'unobfuscated symbols present in the code. If not ' 'unobfuscated symbols present in the code. If not present, the jar '
'present, the jar is assumed not to be obfuscated.')) 'is assumed not to be obfuscated.'))
options, paths = parser.parse_args(args)
required_options = ('d8_jar_path',) options = parser.parse_args(args)
build_utils.CheckOptions(options, parser, required=required_options)
if options.dexlayout_profile: if options.dexlayout_profile:
build_utils.CheckOptions( build_utils.CheckOptions(
...@@ -101,27 +76,15 @@ def _ParseArgs(args): ...@@ -101,27 +76,15 @@ def _ParseArgs(args):
parser, parser,
required=('profman_path', 'dexlayout_path', 'dexdump_path')) required=('profman_path', 'dexlayout_path', 'dexdump_path'))
elif options.proguard_mapping_path is not None: elif options.proguard_mapping_path is not None:
raise Exception('Unexpected proguard mapping without dexlayout') parser.error('Unexpected proguard mapping without dexlayout')
if options.multidex_configuration_path:
with open(options.multidex_configuration_path) as multidex_config_file:
multidex_config = json.loads(multidex_config_file.read())
options.multi_dex = multidex_config.get('enabled', False)
if options.main_dex_list_path and not options.multi_dex: if options.main_dex_list_path and not options.multi_dex:
logging.warning('--main-dex-list-path is unused if multidex is not enabled') parser.error('--main-dex-list-path is unused if multidex is not enabled')
if options.inputs: if options.input_list:
options.inputs = build_utils.ParseGnList(options.inputs) options.inputs += build_utils.ParseGnList(options.input_list)
_CheckFilePathsEndWithJar(parser, options.inputs)
if options.excluded_paths:
options.excluded_paths = build_utils.ParseGnList(options.excluded_paths)
if options.proguard_enabled_input_path: return options
_CheckFilePathEndsWithJar(parser, options.proguard_enabled_input_path)
_CheckFilePathsEndWithJar(parser, paths)
return options, paths
def _RunD8(dex_cmd, input_paths, output_path): def _RunD8(dex_cmd, input_paths, output_path):
...@@ -209,7 +172,7 @@ def _LayoutDex(binary_profile, input_dex, dexlayout_path, temp_dir): ...@@ -209,7 +172,7 @@ def _LayoutDex(binary_profile, input_dex, dexlayout_path, temp_dir):
output_files = os.listdir(dexlayout_output_dir) output_files = os.listdir(dexlayout_output_dir)
if not output_files: if not output_files:
raise Exception('dexlayout unexpectedly produced no output') raise Exception('dexlayout unexpectedly produced no output')
return [os.path.join(dexlayout_output_dir, f) for f in output_files] return sorted([os.path.join(dexlayout_output_dir, f) for f in output_files])
def _ZipMultidex(file_dir, dex_files): def _ZipMultidex(file_dir, dex_files):
...@@ -273,7 +236,31 @@ def _ZipAligned(dex_files, output_path): ...@@ -273,7 +236,31 @@ def _ZipAligned(dex_files, output_path):
z.writestr(zip_info, f.read()) z.writestr(zip_info, f.read())
def _PerformDexing(paths, options): def _PerformDexlayout(tmp_dir, tmp_dex_output, options):
if options.proguard_mapping_path is not None:
matching_profile = os.path.join(tmp_dir, 'obfuscated_profile')
convert_dex_profile.ObfuscateProfile(
options.dexlayout_profile, tmp_dex_output,
options.proguard_mapping_path, options.dexdump_path, matching_profile)
else:
logging.warning('No obfuscation for %s', options.dexlayout_profile)
matching_profile = options.dexlayout_profile
binary_profile = _CreateBinaryProfile(matching_profile, tmp_dex_output,
options.profman_path, tmp_dir)
output_files = _LayoutDex(binary_profile, tmp_dex_output,
options.dexlayout_path, tmp_dir)
if len(output_files) > 1:
return _ZipMultidex(tmp_dir, output_files)
if zipfile.is_zipfile(output_files[0]):
return output_files[0]
final_output = os.path.join(tmp_dir, 'dex_classes.zip')
_ZipAligned(output_files, final_output)
return final_output
def _PerformDexing(options):
dex_cmd = ['java', '-jar', options.d8_jar_path, '--no-desugaring'] dex_cmd = ['java', '-jar', options.d8_jar_path, '--no-desugaring']
if options.multi_dex and options.main_dex_list_path: if options.multi_dex and options.main_dex_list_path:
dex_cmd += ['--main-dex-list', options.main_dex_list_path] dex_cmd += ['--main-dex-list', options.main_dex_list_path]
...@@ -285,10 +272,10 @@ def _PerformDexing(paths, options): ...@@ -285,10 +272,10 @@ def _PerformDexing(paths, options):
with build_utils.TempDir() as tmp_dir: with build_utils.TempDir() as tmp_dir:
tmp_dex_dir = os.path.join(tmp_dir, 'tmp_dex_dir') tmp_dex_dir = os.path.join(tmp_dir, 'tmp_dex_dir')
os.mkdir(tmp_dex_dir) os.mkdir(tmp_dex_dir)
_RunD8(dex_cmd, paths, tmp_dex_dir) _RunD8(dex_cmd, options.inputs, tmp_dex_dir)
dex_files = [os.path.join(tmp_dex_dir, f) for f in os.listdir(tmp_dex_dir)] dex_files = [os.path.join(tmp_dex_dir, f) for f in os.listdir(tmp_dex_dir)]
if not options.dex_path.endswith('.dex'): if not options.output.endswith('.dex'):
tmp_dex_output = os.path.join(tmp_dir, 'tmp_dex_output.zip') tmp_dex_output = os.path.join(tmp_dir, 'tmp_dex_output.zip')
_ZipAligned(sorted(dex_files), tmp_dex_output) _ZipAligned(sorted(dex_files), tmp_dex_output)
else: else:
...@@ -298,48 +285,23 @@ def _PerformDexing(paths, options): ...@@ -298,48 +285,23 @@ def _PerformDexing(paths, options):
tmp_dex_output = dex_files[0] tmp_dex_output = dex_files[0]
if options.dexlayout_profile: if options.dexlayout_profile:
if options.proguard_mapping_path is not None: tmp_dex_output = _PerformDexlayout(tmp_dir, tmp_dex_output, options)
matching_profile = os.path.join(tmp_dir, 'obfuscated_profile')
convert_dex_profile.ObfuscateProfile(
options.dexlayout_profile, tmp_dex_output,
options.proguard_mapping_path, options.dexdump_path,
matching_profile)
else:
logging.warning('No obfuscation for %s', options.dexlayout_profile)
matching_profile = options.dexlayout_profile
binary_profile = _CreateBinaryProfile(matching_profile, tmp_dex_output,
options.profman_path, tmp_dir)
output_files = _LayoutDex(binary_profile, tmp_dex_output,
options.dexlayout_path, tmp_dir)
target = None
if len(output_files) > 1:
target = _ZipMultidex(tmp_dir, output_files)
else:
if not zipfile.is_zipfile(output_files[0]):
target = os.path.join(tmp_dir, 'dex_classes.zip')
_ZipAligned(output_files, target)
else:
target = output_files[0]
tmp_dex_output = os.path.join(tmp_dir, target)
# The dex file is complete and can be moved out of tmp_dir. # The dex file is complete and can be moved out of tmp_dir.
shutil.move(tmp_dex_output, options.dex_path) shutil.move(tmp_dex_output, options.output)
def main(args): def main(args):
options, paths = _ParseArgs(args) options = _ParseArgs(args)
if options.inputs:
paths += options.inputs
input_paths = list(paths) input_paths = list(options.inputs)
if options.multi_dex and options.main_dex_list_path: if options.multi_dex and options.main_dex_list_path:
input_paths.append(options.main_dex_list_path) input_paths.append(options.main_dex_list_path)
_PerformDexing(paths, options) _PerformDexing(options)
build_utils.WriteDepfile( build_utils.WriteDepfile(
options.depfile, options.dex_path, input_paths, add_pydeps=False) options.depfile, options.output, input_paths, add_pydeps=False)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -1300,13 +1300,11 @@ if (enable_java_templates) { ...@@ -1300,13 +1300,11 @@ if (enable_java_templates) {
invoker.output, invoker.output,
] ]
_rebased_output = rebase_path(invoker.output, root_build_dir)
args = [ args = [
"--depfile", "--depfile",
rebase_path(depfile, root_build_dir), rebase_path(depfile, root_build_dir),
"--dex-path", "--output",
_rebased_output, rebase_path(outputs[0], root_build_dir),
] ]
if (_proguard_enabled) { if (_proguard_enabled) {
...@@ -1327,7 +1325,7 @@ if (enable_java_templates) { ...@@ -1327,7 +1325,7 @@ if (enable_java_templates) {
if (defined(invoker.input_dex_classpath)) { if (defined(invoker.input_dex_classpath)) {
inputs += [ invoker.build_config ] inputs += [ invoker.build_config ]
args += [ "--inputs=@FileArg(${invoker.input_dex_classpath})" ] args += [ "--input-list=@FileArg(${invoker.input_dex_classpath})" ]
} }
inputs += _dexing_jars inputs += _dexing_jars
......
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