From 0fbdafe016239ae98d62e4446ffa347880c8f8cf Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Wed, 21 Apr 2021 16:33:33 -0700 Subject: [PATCH 1/2] Allow cargo2android.py to use a config file instead of command-line options. The command-line options to c2a are already long and will only get longer as new arguments are added. This allows it to load them from a config file. For simplicity of implementation, the config file supplements the command-line arguments and uses the same format they do, although it ideally will replace them over time. This also adds an option to dump the current set of command-line arguments to a config file and exit. This is intended to ease migration over to the config file: for a given crate, we can call c2a with its command-line arguments plus this new flag to generate the config file and then use just that instead. Test: Dump and load some config files. Change-Id: I63b29dd20bcff5d5832dbd380d7c6eb273547ed0 --- scripts/cargo2android.py | 49 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/scripts/cargo2android.py b/scripts/cargo2android.py index 2da6dcb7e..ecdb4513a 100755 --- a/scripts/cargo2android.py +++ b/scripts/cargo2android.py @@ -51,6 +51,7 @@ from update_crate_tests import TestMapping import argparse import glob +import json import os import os.path import platform @@ -1474,7 +1475,7 @@ class Runner(object): self.find_warning_owners() -def parse_args(): +def get_parser(): """Parse main arguments.""" parser = argparse.ArgumentParser('cargo2android') parser.add_argument( @@ -1595,14 +1596,54 @@ def parse_args(): action='store_true', default=False, help='run cargo with -vv instead of default -v') - return parser.parse_args() + parser.add_argument( + '--dump-config-and-exit', + type=str, + help=('Dump command-line arguments (minus this flag) to a config file and exit. ' + + 'This is intended to help migrate from command line options to config files.')) + parser.add_argument( + '--config', + type=str, + help=('Load command-line options from the given config file. ' + + 'Options in this file will override those passed on the command line.')) + return parser + + +def parse_args(parser): + """Parses command-line options.""" + args = parser.parse_args() + # Use the values specified in a config file if one was found. + if args.config: + with open(args.config, 'r') as f: + config = json.load(f) + args_dict = vars(args) + for arg in config: + args_dict[arg.replace('-', '_')] = config[arg] + return args + + +def dump_config(parser, args): + """Writes the non-default command-line options to the specified file.""" + args_dict = vars(args) + # Filter out the arguments that have their default value. + non_default_args = {} + for arg in args_dict: + if args_dict[arg] != parser.get_default(arg) and arg != 'dump_config_and_exit': + non_default_args[arg.replace('_', '-')] = args_dict[arg] + # Write to the specified file. + with open(args.dump_config_and_exit, 'w') as f: + json.dump(non_default_args, f, indent=2, sort_keys=True) def main(): - args = parse_args() + parser = get_parser() + args = parse_args(parser) if not args.run: # default is dry-run print(DRY_RUN_NOTE) - Runner(args).run_cargo().gen_bp().apply_patch().dump_test_mapping_files() + if args.dump_config_and_exit: + dump_config(parser, args) + else: + Runner(args).run_cargo().gen_bp().apply_patch().dump_test_mapping_files() if __name__ == '__main__': From d9c4de6fe33332a0e87091fee585d278a5aa8460 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Fri, 23 Apr 2021 10:26:40 -0700 Subject: [PATCH 2/2] Let cargo2android.py generate apex_available and min_sdk_version. This commit adds an --apex-available option to cargo2android.py. It takes in multiple strings that are included in the generated block. It also adds --min-sdk-version. Test: Use flags. Change-Id: I319a32fc5e8be11015ba4484b0ab1665ee457326 --- scripts/cargo2android.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/scripts/cargo2android.py b/scripts/cargo2android.py index ecdb4513a..44f7e3e69 100755 --- a/scripts/cargo2android.py +++ b/scripts/cargo2android.py @@ -649,6 +649,13 @@ class Crate(object): self.dump_edition_flags_libs() if self.runner.args.host_first_multilib and self.host_supported and crate_type != 'test': self.write(' compile_multilib: "first",') + if self.runner.args.apex_available and crate_type == 'lib': + self.write(' apex_available: [') + for apex in self.runner.args.apex_available: + self.write(' "%s",' % apex) + self.write(' ],') + if self.runner.args.min_sdk_version and crate_type == 'lib': + self.write(' min_sdk_version: "%s",' % self.runner.args.min_sdk_version) self.write('}') def dump_android_flags(self): @@ -1581,6 +1588,14 @@ def get_parser(): default=False, help=('run cargo build with existing Cargo.lock ' + '(used when some latest dependent crates failed)')) + parser.add_argument( + '--min-sdk-version', + type=str, + help='Minimum SDK version') + parser.add_argument( + '--apex-available', + nargs='*', + help='Mark the main library as apex_available with the given apexes.') parser.add_argument( '--no-test-mapping', action='store_true',