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 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import json
import argparse
import logging
import optparse
import os
import re
import shutil
......@@ -24,76 +23,52 @@ import convert_dex_profile
_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):
args = build_utils.ExpandFileArgs(args)
parser = argparse.ArgumentParser()
parser = optparse.OptionParser()
build_utils.AddDepfileOption(parser)
parser.add_option('--output-directory',
default=os.getcwd(),
help='Path to the output build directory.')
parser.add_option('--dex-path', help='Dex output path.')
parser.add_option('--configuration-name',
help='The build CONFIGURATION_NAME.')
parser.add_option('--proguard-enabled',
help='"true" if proguard is enabled.')
parser.add_option('--debug-build-proguard-enabled',
help='"true" if proguard is enabled for debug build.')
parser.add_option('--proguard-enabled-input-path',
help=('Path to dex in Release mode when proguard '
'is enabled.'))
parser.add_option('--inputs', help='A list of additional input paths.')
parser.add_option('--excluded-paths',
help='A list of paths to exclude from the dex file.')
parser.add_option('--main-dex-list-path',
help='A file containing a list of the classes to '
'include in the main dex.')
parser.add_option('--multidex-configuration-path',
help='A JSON file containing multidex build configuration.')
parser.add_option('--multi-dex', default=False, action='store_true',
help='Generate multiple dex files.')
parser.add_option('--d8-jar-path', help='Path to D8 jar.')
parser.add_option('--release', action='store_true', default=False,
help='Run D8 in release mode. Release mode maximises main '
'dex and deletes non-essential line number information '
'(vs debug which minimizes main dex and keeps all line '
'number information, and then some.')
parser.add_option('--min-api',
help='Minimum Android API level compatibility.')
parser.add_option('--dexlayout-profile',
help=('Text profile for dexlayout. If present, a dexlayout '
'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(
parser.add_argument('--output', required=True, help='Dex output path.')
parser.add_argument('--input-list', help='GN-list of additional input paths.')
parser.add_argument(
'--main-dex-list-path',
help='File containing a list of the classes to include in the main dex.')
parser.add_argument(
'--multi-dex',
action='store_true',
help='Allow multiple dex files within output.')
parser.add_argument('--d8-jar-path', required=True, help='Path to D8 jar.')
parser.add_argument(
'--release',
action='store_true',
help='Run D8 in release mode. Release mode maximises main dex and '
'deletes non-essential line number information (vs debug which minimizes '
'main dex and keeps all line number information, and then some.')
parser.add_argument(
'--min-api', help='Minimum Android API level compatibility.')
parser.add_argument('inputs', nargs='*', help='Input .jar files.')
group = parser.add_argument_group('Dexlayout')
group.add_argument(
'--dexlayout-profile',
help=('Text profile for dexlayout. If present, a dexlayout '
'pass will happen'))
group.add_argument(
'--profman-path',
help=('Path to ART profman binary. There should be a lib/ directory at '
'the same path with shared libraries (shared with dexlayout).'))
group.add_argument(
'--dexlayout-path',
help=('Path to ART dexlayout binary. There should be a lib/ directory at '
'the same path with shared libraries (shared with dexlayout).'))
group.add_argument('--dexdump-path', help='Path to dexdump binary.')
group.add_argument(
'--proguard-mapping-path',
help=('Path to proguard map from obfuscated symbols in the jar to '
'unobfuscated symbols present in the code. If not '
'present, the jar is assumed not to be obfuscated.'))
options, paths = parser.parse_args(args)
'unobfuscated symbols present in the code. If not present, the jar '
'is assumed not to be obfuscated.'))
required_options = ('d8_jar_path',)
build_utils.CheckOptions(options, parser, required=required_options)
options = parser.parse_args(args)
if options.dexlayout_profile:
build_utils.CheckOptions(
......@@ -101,27 +76,15 @@ def _ParseArgs(args):
parser,
required=('profman_path', 'dexlayout_path', 'dexdump_path'))
elif options.proguard_mapping_path is not None:
raise Exception('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)
parser.error('Unexpected proguard mapping without dexlayout')
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:
options.inputs = build_utils.ParseGnList(options.inputs)
_CheckFilePathsEndWithJar(parser, options.inputs)
if options.excluded_paths:
options.excluded_paths = build_utils.ParseGnList(options.excluded_paths)
if options.input_list:
options.inputs += build_utils.ParseGnList(options.input_list)
if options.proguard_enabled_input_path:
_CheckFilePathEndsWithJar(parser, options.proguard_enabled_input_path)
_CheckFilePathsEndWithJar(parser, paths)
return options, paths
return options
def _RunD8(dex_cmd, input_paths, output_path):
......@@ -209,7 +172,7 @@ def _LayoutDex(binary_profile, input_dex, dexlayout_path, temp_dir):
output_files = os.listdir(dexlayout_output_dir)
if not output_files:
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):
......@@ -273,7 +236,31 @@ def _ZipAligned(dex_files, output_path):
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']
if options.multi_dex and options.main_dex_list_path:
dex_cmd += ['--main-dex-list', options.main_dex_list_path]
......@@ -285,10 +272,10 @@ def _PerformDexing(paths, options):
with build_utils.TempDir() as tmp_dir:
tmp_dex_dir = os.path.join(tmp_dir, '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)]
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')
_ZipAligned(sorted(dex_files), tmp_dex_output)
else:
......@@ -298,48 +285,23 @@ def _PerformDexing(paths, options):
tmp_dex_output = dex_files[0]
if options.dexlayout_profile:
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)
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)
tmp_dex_output = _PerformDexlayout(tmp_dir, tmp_dex_output, options)
# 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):
options, paths = _ParseArgs(args)
if options.inputs:
paths += options.inputs
options = _ParseArgs(args)
input_paths = list(paths)
input_paths = list(options.inputs)
if options.multi_dex and options.main_dex_list_path:
input_paths.append(options.main_dex_list_path)
_PerformDexing(paths, options)
_PerformDexing(options)
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__':
......
......@@ -1300,13 +1300,11 @@ if (enable_java_templates) {
invoker.output,
]
_rebased_output = rebase_path(invoker.output, root_build_dir)
args = [
"--depfile",
rebase_path(depfile, root_build_dir),
"--dex-path",
_rebased_output,
"--output",
rebase_path(outputs[0], root_build_dir),
]
if (_proguard_enabled) {
......@@ -1327,7 +1325,7 @@ if (enable_java_templates) {
if (defined(invoker.input_dex_classpath)) {
inputs += [ invoker.build_config ]
args += [ "--inputs=@FileArg(${invoker.input_dex_classpath})" ]
args += [ "--input-list=@FileArg(${invoker.input_dex_classpath})" ]
}
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