From b56a7a6a5a70e143392ec217a6d9d71f94fcba23 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Tue, 3 Jul 2018 13:56:23 -0500 Subject: [PATCH 1/6] Move exporter alias handling into CLI --- tools/export/__init__.py | 2 -- tools/project.py | 22 ++++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/tools/export/__init__.py b/tools/export/__init__.py index 88347157d9f..f0b46c45ce2 100644 --- a/tools/export/__init__.py +++ b/tools/export/__init__.py @@ -34,8 +34,6 @@ EXPORTERS = { u'uvision5': uvision.Uvision, - u'uvision': uvision.Uvision, - u'gcc_arm': makefile.GccArm, u'make_gcc_arm': makefile.GccArm, u'make_armc5': makefile.Armc5, u'make_armc6': makefile.Armc6, diff --git a/tools/project.py b/tools/project.py index 959bf1aa4fe..e6a5a4ce816 100644 --- a/tools/project.py +++ b/tools/project.py @@ -28,6 +28,19 @@ from tools.options import extract_profile, list_profiles, extract_mcus from tools.notifier.term import TerminalNotifier +EXPORTER_ALIASES = { + u'gcc_arm': u'make_gcc_arm', + u'uvision': u'uvision5', +} + + +def resolve_exporter_alias(ide): + if ide in EXPORTER_ALIASES: + return EXPORTER_ALIASES[ide] + else: + return ide + + def setup_project(ide, target, program=None, source_dir=None, build=None, export_path=None): """Generate a name, if not provided, and find dependencies @@ -109,7 +122,7 @@ def main(): targetnames = TARGET_NAMES targetnames.sort() - toolchainlist = list(EXPORTERS.keys()) + toolchainlist = list(EXPORTERS.keys() + EXPORTER_ALIASES.keys()) toolchainlist.sort() parser.add_argument("-m", "--mcu", @@ -256,10 +269,11 @@ def main(): if (options.program is None) and (not options.source_dir): args_error(parser, "one of -p, -n, or --source is required") - exporter, toolchain_name = get_exporter_toolchain(options.ide) + ide = resolve_exporter_alias(options.ide) + exporter, toolchain_name = get_exporter_toolchain(ide) mcu = extract_mcus(parser, options)[0] if not exporter.is_target_supported(mcu): - args_error(parser, "%s not supported by %s"%(mcu,options.ide)) + args_error(parser, "%s not supported by %s" % (mcu, ide)) profile = extract_profile(parser, options, toolchain_name, fallback="debug") if options.clean: for cls in EXPORTERS.values(): @@ -273,7 +287,7 @@ def main(): except (IOError, OSError): pass try: - export(mcu, options.ide, build=options.build, + export(mcu, ide, build=options.build, src=options.source_dir, macros=options.macros, project_id=options.program, zip_proj=zip_proj, build_profile=profile, app_config=options.app_config, From 71323af2f58ff4850f28065f4fb8ac52a7ab60c5 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Tue, 3 Jul 2018 14:03:28 -0500 Subject: [PATCH 2/6] Reformat part of project.py --- tools/project.py | 305 +++++++++++++++++++++++++++++------------------ 1 file changed, 191 insertions(+), 114 deletions(-) diff --git a/tools/project.py b/tools/project.py index e6a5a4ce816..43c9c383c1b 100644 --- a/tools/project.py +++ b/tools/project.py @@ -16,11 +16,22 @@ from tools.paths import EXPORT_DIR, MBED_HAL, MBED_LIBRARIES, MBED_TARGETS_PATH from tools.settings import BUILD_DIR -from tools.export import EXPORTERS, mcu_ide_matrix, mcu_ide_list, export_project, get_exporter_toolchain +from tools.export import ( + EXPORTERS, + mcu_ide_matrix, + mcu_ide_list, + export_project, + get_exporter_toolchain, +) from tools.tests import TESTS, TEST_MAP from tools.tests import test_known, test_name_known, Test from tools.targets import TARGET_NAMES -from tools.utils import argparse_filestring_type, argparse_profile_filestring_type, argparse_many, args_error +from tools.utils import ( + argparse_filestring_type, + argparse_profile_filestring_type, + argparse_many, + args_error, +) from tools.utils import argparse_force_lowercase_type from tools.utils import argparse_force_uppercase_type from tools.utils import print_large_string @@ -41,7 +52,14 @@ def resolve_exporter_alias(ide): return ide -def setup_project(ide, target, program=None, source_dir=None, build=None, export_path=None): +def setup_project( + ide, + target, + program=None, + source_dir=None, + build=None, + export_path=None +): """Generate a name, if not provided, and find dependencies Positional arguments: @@ -75,7 +93,6 @@ def setup_project(ide, target, program=None, source_dir=None, build=None, export test.dependencies.append(MBED_HAL) test.dependencies.append(MBED_TARGETS_PATH) - src_paths = [test.source_dir] lib_paths = test.dependencies project_name = "_".join([test.id, ide, target]) @@ -104,15 +121,31 @@ def export(target, ide, build=None, src=None, macros=None, project_id=None, Returns an object of type Exporter (tools/exports/exporters.py) """ - project_dir, name, src, lib = setup_project(ide, target, program=project_id, - source_dir=src, build=build, export_path=export_path) + project_dir, name, src, lib = setup_project( + ide, + target, + program=project_id, + source_dir=src, + build=build, + export_path=export_path, + ) zip_name = name+".zip" if zip_proj else None - return export_project(src, project_dir, target, ide, name=name, - macros=macros, libraries_paths=lib, zip_proj=zip_name, - build_profile=build_profile, notify=notify, - app_config=app_config, ignore=ignore) + return export_project( + src, + project_dir, + target, + ide, + name=name, + macros=macros, + libraries_paths=lib, + zip_proj=zip_name, + build_profile=build_profile, + notify=notify, + app_config=app_config, + ignore=ignore + ) def main(): @@ -125,100 +158,139 @@ def main(): toolchainlist = list(EXPORTERS.keys() + EXPORTER_ALIASES.keys()) toolchainlist.sort() - parser.add_argument("-m", "--mcu", - metavar="MCU", - help="generate project for the given MCU ({})".format( - ', '.join(targetnames))) - - parser.add_argument("-i", - dest="ide", - type=argparse_force_lowercase_type( - toolchainlist, "toolchain"), - help="The target IDE: %s"% str(toolchainlist)) - - parser.add_argument("-c", "--clean", - action="store_true", - default=False, - help="clean the export directory") + parser.add_argument( + "-m", "--mcu", + metavar="MCU", + help="generate project for the given MCU ({})".format( + ', '.join(targetnames)) + ) + + parser.add_argument( + "-i", + dest="ide", + type=argparse_force_lowercase_type( + toolchainlist, "toolchain"), + help="The target IDE: %s" % str(toolchainlist) + ) + + parser.add_argument( + "-c", "--clean", + action="store_true", + default=False, + help="clean the export directory" + ) group = parser.add_mutually_exclusive_group(required=False) group.add_argument( "-p", type=test_known, dest="program", - help="The index of the desired test program: [0-%s]"% (len(TESTS)-1)) - - group.add_argument("-n", - type=test_name_known, - dest="program", - help="The name of the desired test program") - - parser.add_argument("-b", - dest="build", - default=False, - action="store_true", - help="use the mbed library build, instead of the sources") - - group.add_argument("-L", "--list-tests", - action="store_true", - dest="list_tests", - default=False, - help="list available programs in order and exit") - - group.add_argument("-S", "--list-matrix", - dest="supported_ides", - default=False, - const="matrix", - choices=["matrix", "ides"], - nargs="?", - help="displays supported matrix of MCUs and IDEs") - - parser.add_argument("-E", - action="store_true", - dest="supported_ides_html", - default=False, - help="writes tools/export/README.md") - - parser.add_argument("--build", - type=argparse_filestring_type, - dest="build_dir", - default=None, - help="Directory for the exported project files") - - parser.add_argument("--source", - action="append", - type=argparse_filestring_type, - dest="source_dir", - default=[], - help="The source (input) directory") - - parser.add_argument("-D", - action="append", - dest="macros", - help="Add a macro definition") - - parser.add_argument("--profile", dest="profile", action="append", - type=argparse_profile_filestring_type, - help="Build profile to use. Can be either path to json" \ - "file or one of the default one ({})".format(", ".join(list_profiles())), - default=[]) - - parser.add_argument("--update-packs", - dest="update_packs", - action="store_true", - default=False) - parser.add_argument("--app-config", - dest="app_config", - default=None) - - parser.add_argument("--ignore", dest="ignore", type=argparse_many(str), - default=None, help="Comma separated list of patterns to add to mbedignore (eg. ./main.cpp)") + help="The index of the desired test program: [0-%s]" % (len(TESTS) - 1) + ) + + group.add_argument( + "-n", + type=test_name_known, + dest="program", + help="The name of the desired test program" + ) + + parser.add_argument( + "-b", + dest="build", + default=False, + action="store_true", + help="use the mbed library build, instead of the sources" + ) + + group.add_argument( + "-L", "--list-tests", + action="store_true", + dest="list_tests", + default=False, + help="list available programs in order and exit" + ) + + group.add_argument( + "-S", "--list-matrix", + dest="supported_ides", + default=False, + const="matrix", + choices=["matrix", "ides"], + nargs="?", + help="displays supported matrix of MCUs and IDEs" + ) + + parser.add_argument( + "-E", + action="store_true", + dest="supported_ides_html", + default=False, + help="writes tools/export/README.md" + ) + + parser.add_argument( + "--build", + type=argparse_filestring_type, + dest="build_dir", + default=None, + help="Directory for the exported project files" + ) + + parser.add_argument( + "--source", + action="append", + type=argparse_filestring_type, + dest="source_dir", + default=[], + help="The source (input) directory" + ) + + parser.add_argument( + "-D", + action="append", + dest="macros", + help="Add a macro definition" + ) + + parser.add_argument( + "--profile", + dest="profile", + action="append", + type=argparse_profile_filestring_type, + help=("Build profile to use. Can be either path to json" + "file or one of the default one ({})".format( + ", ".join(list_profiles()))), + default=[] + ) + + parser.add_argument( + "--update-packs", + dest="update_packs", + action="store_true", + default=False + ) + + parser.add_argument( + "--app-config", + dest="app_config", + default=None + ) + + parser.add_argument( + "--ignore", + dest="ignore", + type=argparse_many(str), + default=None, + help=("Comma separated list of patterns to add to mbedignore " + "(eg. ./main.cpp)") + ) options = parser.parse_args() # Print available tests in order and exit if options.list_tests is True: - print('\n'.join([str(test) for test in sorted(TEST_MAP.values())])) + print('\n'.join(str(test) for test in sorted(TEST_MAP.values()))) sys.exit() # Only prints matrix of supported IDEs @@ -232,17 +304,11 @@ def main(): # Only prints matrix of supported IDEs if options.supported_ides_html: html = mcu_ide_matrix(verbose_html=True) - try: - with open("./export/README.md", "w") as readme: - readme.write("Exporter IDE/Platform Support\n") - readme.write("-----------------------------------\n") - readme.write("\n") - readme.write(html) - except IOError as exc: - print("I/O error({0}): {1}".format(exc.errno, exc.strerror)) - except: - print("Unexpected error:", sys.exc_info()[0]) - raise + with open("./export/README.md", "w") as readme: + readme.write("Exporter IDE/Platform Support\n") + readme.write("-----------------------------------\n") + readme.write("\n") + readme.write(html) exit(0) if options.update_packs: @@ -267,14 +333,17 @@ def main(): notify = TerminalNotifier() - if (options.program is None) and (not options.source_dir): - args_error(parser, "one of -p, -n, or --source is required") ide = resolve_exporter_alias(options.ide) exporter, toolchain_name = get_exporter_toolchain(ide) + profile = extract_profile(parser, options, toolchain_name, fallback="debug") mcu = extract_mcus(parser, options)[0] + if not exporter.is_target_supported(mcu): args_error(parser, "%s not supported by %s" % (mcu, ide)) - profile = extract_profile(parser, options, toolchain_name, fallback="debug") + + if (options.program is None) and (not options.source_dir): + args_error(parser, "one of -p, -n, or --source is required") + if options.clean: for cls in EXPORTERS.values(): try: @@ -287,14 +356,22 @@ def main(): except (IOError, OSError): pass try: - export(mcu, ide, build=options.build, - src=options.source_dir, macros=options.macros, - project_id=options.program, zip_proj=zip_proj, - build_profile=profile, app_config=options.app_config, - export_path=options.build_dir, notify=notify, - ignore=options.ignore) + export( + mcu, + ide, + build=options.build, + src=options.source_dir, + macros=options.macros, + project_id=options.program, + zip_proj=zip_proj, + build_profile=profile, + app_config=options.app_config, + export_path=options.build_dir, + notify=notify, + ignore=options.ignore + ) except NotSupportedException as exc: - print("[ERROR] %s" % str(exc)) + print("[Not Supported] %s" % str(exc)) if __name__ == "__main__": main() From 44030c23d37a15f787a7cc22a820f63223f4b4f4 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Fri, 6 Jul 2018 10:20:37 -0500 Subject: [PATCH 3/6] Simplify main function --- tools/project.py | 154 ++++++++++++++++++++++------------------------- 1 file changed, 72 insertions(+), 82 deletions(-) diff --git a/tools/project.py b/tools/project.py index 43c9c383c1b..9b30a1f819a 100644 --- a/tools/project.py +++ b/tools/project.py @@ -142,15 +142,27 @@ def export(target, ide, build=None, src=None, macros=None, project_id=None, libraries_paths=lib, zip_proj=zip_name, build_profile=build_profile, - notify=notify, + notify=TerminalNotifier(), app_config=app_config, ignore=ignore ) - -def main(): - """Entry point""" - # Parse Options +def clean(source_dir): + if exists(EXPORT_DIR): + rmtree(EXPORT_DIR) + for cls in EXPORTERS.values(): + try: + cls.clean(basename(abspath(source_dir[0]))) + except (NotImplementedError, IOError, OSError): + pass + for f in list(EXPORTERS.values())[0].CLEAN_FILES: + try: + remove(f) + except (IOError, OSError): + pass + + +def get_args(argv): parser = ArgumentParser() targetnames = TARGET_NAMES @@ -221,6 +233,13 @@ def main(): help="displays supported matrix of MCUs and IDEs" ) + group.add_argument( + "--update-packs", + dest="update_packs", + action="store_true", + default=False + ) + parser.add_argument( "-E", action="store_true", @@ -264,13 +283,6 @@ def main(): default=[] ) - parser.add_argument( - "--update-packs", - dest="update_packs", - action="store_true", - default=False - ) - parser.add_argument( "--app-config", dest="app_config", @@ -286,92 +298,70 @@ def main(): "(eg. ./main.cpp)") ) - options = parser.parse_args() + return parser.parse_args(argv), parser + + +def main(): + """Entry point""" + # Parse Options + options, parser = get_args(sys.argv) # Print available tests in order and exit - if options.list_tests is True: + if options.list_tests: print('\n'.join(str(test) for test in sorted(TEST_MAP.values()))) - sys.exit() - - # Only prints matrix of supported IDEs - if options.supported_ides: + elif options.supported_ides: if options.supported_ides == "matrix": print_large_string(mcu_ide_matrix()) elif options.supported_ides == "ides": print(mcu_ide_list()) - exit(0) - - # Only prints matrix of supported IDEs - if options.supported_ides_html: + elif options.supported_ides_html: html = mcu_ide_matrix(verbose_html=True) with open("./export/README.md", "w") as readme: readme.write("Exporter IDE/Platform Support\n") readme.write("-----------------------------------\n") readme.write("\n") readme.write(html) - exit(0) - - if options.update_packs: + elif options.update_packs: from tools.arm_pack_manager import Cache cache = Cache(True, True) cache.cache_everything() - - # Target - if not options.mcu: - args_error(parser, "argument -m/--mcu is required") - - # Toolchain - if not options.ide: - args_error(parser, "argument -i is required") - - # Clean Export Directory - if options.clean: - if exists(EXPORT_DIR): - rmtree(EXPORT_DIR) - - zip_proj = not bool(options.source_dir) - - notify = TerminalNotifier() - - ide = resolve_exporter_alias(options.ide) - exporter, toolchain_name = get_exporter_toolchain(ide) - profile = extract_profile(parser, options, toolchain_name, fallback="debug") - mcu = extract_mcus(parser, options)[0] - - if not exporter.is_target_supported(mcu): - args_error(parser, "%s not supported by %s" % (mcu, ide)) - - if (options.program is None) and (not options.source_dir): - args_error(parser, "one of -p, -n, or --source is required") - - if options.clean: - for cls in EXPORTERS.values(): - try: - cls.clean(basename(abspath(options.source_dir[0]))) - except (NotImplementedError, IOError, OSError): - pass - for f in list(EXPORTERS.values())[0].CLEAN_FILES: - try: - remove(f) - except (IOError, OSError): - pass - try: - export( - mcu, - ide, - build=options.build, - src=options.source_dir, - macros=options.macros, - project_id=options.program, - zip_proj=zip_proj, - build_profile=profile, - app_config=options.app_config, - export_path=options.build_dir, - notify=notify, - ignore=options.ignore - ) - except NotSupportedException as exc: - print("[Not Supported] %s" % str(exc)) + else: + # Check required arguments + if not options.mcu: + args_error(parser, "argument -m/--mcu is required") + if not options.ide: + args_error(parser, "argument -i is required") + if (options.program is None) and (not options.source_dir): + args_error(parser, "one of -p, -n, or --source is required") + + if options.clean: + clean(options.source_dir) + + ide = resolve_exporter_alias(options.ide) + exporter, toolchain_name = get_exporter_toolchain(ide) + profile = extract_profile(parser, options, toolchain_name, fallback="debug") + mcu = extract_mcus(parser, options)[0] + if not exporter.is_target_supported(mcu): + args_error(parser, "%s not supported by %s" % (mcu, ide)) + + try: + export( + mcu, + ide, + build=options.build, + src=options.source_dir, + macros=options.macros, + project_id=options.program, + zip_proj=not bool(options.source_dir), + build_profile=profile, + app_config=options.app_config, + export_path=options.build_dir, + ignore=options.ignore + ) + except NotSupportedException as exc: + args_error(parser, "%s not supported by %s" % (mcu, ide)) + print("[Not Supported] %s" % str(exc)) + exit(0) if __name__ == "__main__": main() From ec0a4933fa375d36048b1694c9a77c9c9894d074 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Fri, 6 Jul 2018 10:22:37 -0500 Subject: [PATCH 4/6] Correct argv usage --- tools/project.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/project.py b/tools/project.py index 9b30a1f819a..e60c55cd04b 100644 --- a/tools/project.py +++ b/tools/project.py @@ -304,7 +304,7 @@ def get_args(argv): def main(): """Entry point""" # Parse Options - options, parser = get_args(sys.argv) + options, parser = get_args(sys.argv[1:]) # Print available tests in order and exit if options.list_tests: From 047cb631e8e2efec1359fc3980454ff675d4c7c5 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Fri, 6 Jul 2018 10:28:13 -0500 Subject: [PATCH 5/6] Correct -E help --- tools/project.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/project.py b/tools/project.py index e60c55cd04b..87dba9d5b49 100644 --- a/tools/project.py +++ b/tools/project.py @@ -245,7 +245,7 @@ def get_args(argv): action="store_true", dest="supported_ides_html", default=False, - help="writes tools/export/README.md" + help="Generate a markdown version of the results of -S in README.md" ) parser.add_argument( @@ -316,7 +316,7 @@ def main(): print(mcu_ide_list()) elif options.supported_ides_html: html = mcu_ide_matrix(verbose_html=True) - with open("./export/README.md", "w") as readme: + with open("README.md", "w") as readme: readme.write("Exporter IDE/Platform Support\n") readme.write("-----------------------------------\n") readme.write("\n") From a9abcf1b157409ea054ceb50995a3eeebf4490ab Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 9 Jul 2018 09:49:55 -0500 Subject: [PATCH 6/6] Add aliases to example exporter options --- tools/test/examples/examples_lib.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/test/examples/examples_lib.py b/tools/test/examples/examples_lib.py index 9b064cadc74..f215d563801 100644 --- a/tools/test/examples/examples_lib.py +++ b/tools/test/examples/examples_lib.py @@ -17,10 +17,12 @@ from tools.build_api import get_mbed_official_release from tools.targets import TARGET_MAP from tools.export import EXPORTERS +from tools.project import EXPORTER_ALIASES from tools.toolchains import TOOLCHAINS SUPPORTED_TOOLCHAINS = list(TOOLCHAINS - set(u'uARM')) -SUPPORTED_IDES = [exp for exp in EXPORTERS.keys() if exp != "cmsis" and exp != "zip"] +SUPPORTED_IDES = [exp for exp in EXPORTERS.keys() + EXPORTER_ALIASES.keys() + if exp != "cmsis" and exp != "zip"] def print_list(lst):