Clone of official tools
project.py@38:399953da035d, 2017-07-13 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Thu Jul 13 15:26:26 2017 -0500
- Revision:
- 38:399953da035d
- Parent:
- 36:96847d42f010
- Child:
- 40:7d3fa6b99b2b
Update to tools release 5.5.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
The Other Jimmy |
31:8ea194f6145b | 1 | """ The CLI entry point for exporting projects from the mbed tools to any of the |
The Other Jimmy |
31:8ea194f6145b | 2 | supported IDEs or project structures. |
The Other Jimmy |
31:8ea194f6145b | 3 | """ |
screamer | 0:66f3b5499f7f | 4 | import sys |
screamer | 0:66f3b5499f7f | 5 | from os.path import join, abspath, dirname, exists, basename |
screamer | 0:66f3b5499f7f | 6 | ROOT = abspath(join(dirname(__file__), "..")) |
screamer | 0:66f3b5499f7f | 7 | sys.path.insert(0, ROOT) |
screamer | 0:66f3b5499f7f | 8 | |
screamer | 0:66f3b5499f7f | 9 | from shutil import move, rmtree |
screamer | 22:9e85236d8716 | 10 | from argparse import ArgumentParser |
The Other Jimmy |
31:8ea194f6145b | 11 | from os.path import normpath, realpath |
screamer | 0:66f3b5499f7f | 12 | |
The Other Jimmy |
31:8ea194f6145b | 13 | from tools.paths import EXPORT_DIR, MBED_HAL, MBED_LIBRARIES, MBED_TARGETS_PATH |
The Other Jimmy |
35:da9c89f8be7d | 14 | from tools.settings import BUILD_DIR |
The Other Jimmy |
36:96847d42f010 | 15 | from tools.export import EXPORTERS, mcu_ide_matrix, mcu_ide_list, export_project, get_exporter_toolchain |
screamer | 23:fbae331171fa | 16 | from tools.tests import TESTS, TEST_MAP |
The Other Jimmy |
31:8ea194f6145b | 17 | from tools.tests import test_known, test_name_known, Test |
screamer | 0:66f3b5499f7f | 18 | from tools.targets import TARGET_NAMES |
The Other Jimmy |
31:8ea194f6145b | 19 | from tools.utils import argparse_filestring_type, argparse_profile_filestring_type, argparse_many, args_error |
The Other Jimmy |
31:8ea194f6145b | 20 | from tools.utils import argparse_force_lowercase_type |
The Other Jimmy |
31:8ea194f6145b | 21 | from tools.utils import argparse_force_uppercase_type |
The Other Jimmy |
31:8ea194f6145b | 22 | from tools.utils import print_large_string |
The Other Jimmy |
38:399953da035d | 23 | from tools.options import extract_profile, list_profiles, extract_mcus |
The Other Jimmy |
31:8ea194f6145b | 24 | |
The Other Jimmy |
31:8ea194f6145b | 25 | def setup_project(ide, target, program=None, source_dir=None, build=None, export_path=None): |
The Other Jimmy |
31:8ea194f6145b | 26 | """Generate a name, if not provided, and find dependencies |
The Other Jimmy |
31:8ea194f6145b | 27 | |
The Other Jimmy |
31:8ea194f6145b | 28 | Positional arguments: |
The Other Jimmy |
31:8ea194f6145b | 29 | ide - IDE or project structure that will soon be exported to |
The Other Jimmy |
31:8ea194f6145b | 30 | target - MCU that the project will build for |
The Other Jimmy |
31:8ea194f6145b | 31 | |
The Other Jimmy |
31:8ea194f6145b | 32 | Keyword arguments: |
The Other Jimmy |
31:8ea194f6145b | 33 | program - the index of a test program |
The Other Jimmy |
31:8ea194f6145b | 34 | source_dir - the directory, or directories that contain all of the sources |
The Other Jimmy |
31:8ea194f6145b | 35 | build - a directory that will contain the result of the export |
The Other Jimmy |
31:8ea194f6145b | 36 | """ |
The Other Jimmy |
31:8ea194f6145b | 37 | # Some libraries have extra macros (called by exporter symbols) to we need |
The Other Jimmy |
31:8ea194f6145b | 38 | # to pass them to maintain compilation macros integrity between compiled |
The Other Jimmy |
31:8ea194f6145b | 39 | # library and header files we might use with it |
The Other Jimmy |
31:8ea194f6145b | 40 | if source_dir: |
The Other Jimmy |
31:8ea194f6145b | 41 | # --source is used to generate IDE files to toolchain directly |
The Other Jimmy |
31:8ea194f6145b | 42 | # in the source tree and doesn't generate zip file |
The Other Jimmy |
31:8ea194f6145b | 43 | project_dir = export_path or source_dir[0] |
The Other Jimmy |
31:8ea194f6145b | 44 | if program: |
The Other Jimmy |
31:8ea194f6145b | 45 | project_name = TESTS[program] |
The Other Jimmy |
31:8ea194f6145b | 46 | else: |
The Other Jimmy |
31:8ea194f6145b | 47 | project_name = basename(normpath(realpath(source_dir[0]))) |
The Other Jimmy |
31:8ea194f6145b | 48 | src_paths = source_dir |
The Other Jimmy |
31:8ea194f6145b | 49 | lib_paths = None |
The Other Jimmy |
31:8ea194f6145b | 50 | else: |
The Other Jimmy |
31:8ea194f6145b | 51 | test = Test(program) |
The Other Jimmy |
31:8ea194f6145b | 52 | if not build: |
The Other Jimmy |
31:8ea194f6145b | 53 | # Substitute the mbed library builds with their sources |
The Other Jimmy |
31:8ea194f6145b | 54 | if MBED_LIBRARIES in test.dependencies: |
The Other Jimmy |
31:8ea194f6145b | 55 | test.dependencies.remove(MBED_LIBRARIES) |
The Other Jimmy |
31:8ea194f6145b | 56 | test.dependencies.append(MBED_HAL) |
The Other Jimmy |
31:8ea194f6145b | 57 | test.dependencies.append(MBED_TARGETS_PATH) |
screamer | 0:66f3b5499f7f | 58 | |
screamer | 0:66f3b5499f7f | 59 | |
The Other Jimmy |
31:8ea194f6145b | 60 | src_paths = [test.source_dir] |
The Other Jimmy |
31:8ea194f6145b | 61 | lib_paths = test.dependencies |
The Other Jimmy |
31:8ea194f6145b | 62 | project_name = "_".join([test.id, ide, target]) |
The Other Jimmy |
31:8ea194f6145b | 63 | project_dir = join(EXPORT_DIR, project_name) |
screamer | 0:66f3b5499f7f | 64 | |
The Other Jimmy |
31:8ea194f6145b | 65 | return project_dir, project_name, src_paths, lib_paths |
The Other Jimmy |
31:8ea194f6145b | 66 | |
The Other Jimmy |
31:8ea194f6145b | 67 | |
The Other Jimmy |
31:8ea194f6145b | 68 | def export(target, ide, build=None, src=None, macros=None, project_id=None, |
The Other Jimmy |
36:96847d42f010 | 69 | zip_proj=False, build_profile=None, export_path=None, silent=False, |
The Other Jimmy |
36:96847d42f010 | 70 | app_config=None): |
The Other Jimmy |
31:8ea194f6145b | 71 | """Do an export of a project. |
The Other Jimmy |
31:8ea194f6145b | 72 | |
The Other Jimmy |
31:8ea194f6145b | 73 | Positional arguments: |
The Other Jimmy |
31:8ea194f6145b | 74 | target - MCU that the project will compile for |
The Other Jimmy |
31:8ea194f6145b | 75 | ide - the IDE or project structure to export to |
The Other Jimmy |
31:8ea194f6145b | 76 | |
The Other Jimmy |
31:8ea194f6145b | 77 | Keyword arguments: |
The Other Jimmy |
31:8ea194f6145b | 78 | build - to use the compiled mbed libraries or not |
The Other Jimmy |
31:8ea194f6145b | 79 | src - directory or directories that contain the source to export |
The Other Jimmy |
31:8ea194f6145b | 80 | macros - extra macros to add to the project |
The Other Jimmy |
31:8ea194f6145b | 81 | project_id - the name of the project |
The Other Jimmy |
31:8ea194f6145b | 82 | clean - start from a clean state before exporting |
The Other Jimmy |
31:8ea194f6145b | 83 | zip_proj - create a zip file or not |
The Other Jimmy |
31:8ea194f6145b | 84 | |
The Other Jimmy |
31:8ea194f6145b | 85 | Returns an object of type Exporter (tools/exports/exporters.py) |
The Other Jimmy |
31:8ea194f6145b | 86 | """ |
The Other Jimmy |
31:8ea194f6145b | 87 | project_dir, name, src, lib = setup_project(ide, target, program=project_id, |
The Other Jimmy |
31:8ea194f6145b | 88 | source_dir=src, build=build, export_path=export_path) |
The Other Jimmy |
31:8ea194f6145b | 89 | |
The Other Jimmy |
31:8ea194f6145b | 90 | zip_name = name+".zip" if zip_proj else None |
The Other Jimmy |
31:8ea194f6145b | 91 | |
The Other Jimmy |
35:da9c89f8be7d | 92 | return export_project(src, project_dir, target, ide, name=name, |
The Other Jimmy |
35:da9c89f8be7d | 93 | macros=macros, libraries_paths=lib, zip_proj=zip_name, |
The Other Jimmy |
36:96847d42f010 | 94 | build_profile=build_profile, silent=silent, |
The Other Jimmy |
36:96847d42f010 | 95 | app_config=app_config) |
The Other Jimmy |
31:8ea194f6145b | 96 | |
The Other Jimmy |
31:8ea194f6145b | 97 | |
The Other Jimmy |
31:8ea194f6145b | 98 | def main(): |
The Other Jimmy |
31:8ea194f6145b | 99 | """Entry point""" |
screamer | 0:66f3b5499f7f | 100 | # Parse Options |
screamer | 22:9e85236d8716 | 101 | parser = ArgumentParser() |
screamer | 0:66f3b5499f7f | 102 | |
screamer | 0:66f3b5499f7f | 103 | targetnames = TARGET_NAMES |
screamer | 0:66f3b5499f7f | 104 | targetnames.sort() |
screamer | 0:66f3b5499f7f | 105 | toolchainlist = EXPORTERS.keys() |
screamer | 0:66f3b5499f7f | 106 | toolchainlist.sort() |
screamer | 0:66f3b5499f7f | 107 | |
screamer | 22:9e85236d8716 | 108 | parser.add_argument("-m", "--mcu", |
The Other Jimmy |
31:8ea194f6145b | 109 | metavar="MCU", |
The Other Jimmy |
31:8ea194f6145b | 110 | type=argparse_force_uppercase_type(targetnames, "MCU"), |
The Other Jimmy |
31:8ea194f6145b | 111 | help="generate project for the given MCU ({})".format( |
The Other Jimmy |
31:8ea194f6145b | 112 | ', '.join(targetnames))) |
screamer | 0:66f3b5499f7f | 113 | |
screamer | 22:9e85236d8716 | 114 | parser.add_argument("-i", |
The Other Jimmy |
31:8ea194f6145b | 115 | dest="ide", |
The Other Jimmy |
31:8ea194f6145b | 116 | type=argparse_force_lowercase_type( |
The Other Jimmy |
31:8ea194f6145b | 117 | toolchainlist, "toolchain"), |
The Other Jimmy |
31:8ea194f6145b | 118 | help="The target IDE: %s"% str(toolchainlist)) |
screamer | 0:66f3b5499f7f | 119 | |
screamer | 22:9e85236d8716 | 120 | parser.add_argument("-c", "--clean", |
The Other Jimmy |
31:8ea194f6145b | 121 | action="store_true", |
The Other Jimmy |
31:8ea194f6145b | 122 | default=False, |
The Other Jimmy |
31:8ea194f6145b | 123 | help="clean the export directory") |
screamer | 0:66f3b5499f7f | 124 | |
screamer | 22:9e85236d8716 | 125 | group = parser.add_mutually_exclusive_group(required=False) |
The Other Jimmy |
31:8ea194f6145b | 126 | group.add_argument( |
The Other Jimmy |
31:8ea194f6145b | 127 | "-p", |
The Other Jimmy |
31:8ea194f6145b | 128 | type=test_known, |
The Other Jimmy |
31:8ea194f6145b | 129 | dest="program", |
The Other Jimmy |
31:8ea194f6145b | 130 | help="The index of the desired test program: [0-%s]"% (len(TESTS)-1)) |
screamer | 0:66f3b5499f7f | 131 | |
screamer | 22:9e85236d8716 | 132 | group.add_argument("-n", |
The Other Jimmy |
31:8ea194f6145b | 133 | type=test_name_known, |
The Other Jimmy |
31:8ea194f6145b | 134 | dest="program", |
The Other Jimmy |
31:8ea194f6145b | 135 | help="The name of the desired test program") |
screamer | 0:66f3b5499f7f | 136 | |
screamer | 22:9e85236d8716 | 137 | parser.add_argument("-b", |
screamer | 0:66f3b5499f7f | 138 | dest="build", |
screamer | 0:66f3b5499f7f | 139 | default=False, |
screamer | 29:1210849dba19 | 140 | action="store_true", |
screamer | 0:66f3b5499f7f | 141 | help="use the mbed library build, instead of the sources") |
screamer | 0:66f3b5499f7f | 142 | |
screamer | 22:9e85236d8716 | 143 | group.add_argument("-L", "--list-tests", |
The Other Jimmy |
31:8ea194f6145b | 144 | action="store_true", |
The Other Jimmy |
31:8ea194f6145b | 145 | dest="list_tests", |
The Other Jimmy |
31:8ea194f6145b | 146 | default=False, |
The Other Jimmy |
31:8ea194f6145b | 147 | help="list available programs in order and exit") |
screamer | 0:66f3b5499f7f | 148 | |
screamer | 22:9e85236d8716 | 149 | group.add_argument("-S", "--list-matrix", |
The Other Jimmy |
31:8ea194f6145b | 150 | dest="supported_ides", |
The Other Jimmy |
31:8ea194f6145b | 151 | default=False, |
The Other Jimmy |
36:96847d42f010 | 152 | const="matrix", |
The Other Jimmy |
36:96847d42f010 | 153 | choices=["matrix", "ides"], |
The Other Jimmy |
36:96847d42f010 | 154 | nargs="?", |
The Other Jimmy |
31:8ea194f6145b | 155 | help="displays supported matrix of MCUs and IDEs") |
screamer | 0:66f3b5499f7f | 156 | |
screamer | 22:9e85236d8716 | 157 | parser.add_argument("-E", |
The Other Jimmy |
31:8ea194f6145b | 158 | action="store_true", |
The Other Jimmy |
31:8ea194f6145b | 159 | dest="supported_ides_html", |
The Other Jimmy |
31:8ea194f6145b | 160 | default=False, |
The Other Jimmy |
31:8ea194f6145b | 161 | help="writes tools/export/README.md") |
screamer | 0:66f3b5499f7f | 162 | |
screamer | 22:9e85236d8716 | 163 | parser.add_argument("--source", |
The Other Jimmy |
31:8ea194f6145b | 164 | action="append", |
The Other Jimmy |
31:8ea194f6145b | 165 | type=argparse_filestring_type, |
The Other Jimmy |
31:8ea194f6145b | 166 | dest="source_dir", |
The Other Jimmy |
31:8ea194f6145b | 167 | default=[], |
The Other Jimmy |
31:8ea194f6145b | 168 | help="The source (input) directory") |
screamer | 0:66f3b5499f7f | 169 | |
screamer | 22:9e85236d8716 | 170 | parser.add_argument("-D", |
The Other Jimmy |
31:8ea194f6145b | 171 | action="append", |
The Other Jimmy |
31:8ea194f6145b | 172 | dest="macros", |
The Other Jimmy |
31:8ea194f6145b | 173 | help="Add a macro definition") |
The Other Jimmy |
31:8ea194f6145b | 174 | |
The Other Jimmy |
31:8ea194f6145b | 175 | parser.add_argument("--profile", dest="profile", action="append", |
The Other Jimmy |
31:8ea194f6145b | 176 | type=argparse_profile_filestring_type, |
The Other Jimmy |
31:8ea194f6145b | 177 | help="Build profile to use. Can be either path to json" \ |
The Other Jimmy |
31:8ea194f6145b | 178 | "file or one of the default one ({})".format(", ".join(list_profiles())), |
The Other Jimmy |
31:8ea194f6145b | 179 | default=[]) |
The Other Jimmy |
31:8ea194f6145b | 180 | |
The Other Jimmy |
31:8ea194f6145b | 181 | parser.add_argument("--update-packs", |
The Other Jimmy |
31:8ea194f6145b | 182 | dest="update_packs", |
The Other Jimmy |
31:8ea194f6145b | 183 | action="store_true", |
The Other Jimmy |
31:8ea194f6145b | 184 | default=False) |
The Other Jimmy |
36:96847d42f010 | 185 | parser.add_argument("--app-config", |
The Other Jimmy |
36:96847d42f010 | 186 | dest="app_config", |
The Other Jimmy |
36:96847d42f010 | 187 | default=None) |
screamer | 0:66f3b5499f7f | 188 | |
screamer | 22:9e85236d8716 | 189 | options = parser.parse_args() |
screamer | 0:66f3b5499f7f | 190 | |
screamer | 0:66f3b5499f7f | 191 | # Print available tests in order and exit |
screamer | 0:66f3b5499f7f | 192 | if options.list_tests is True: |
The Other Jimmy |
31:8ea194f6145b | 193 | print '\n'.join([str(test) for test in sorted(TEST_MAP.values())]) |
screamer | 0:66f3b5499f7f | 194 | sys.exit() |
screamer | 0:66f3b5499f7f | 195 | |
screamer | 0:66f3b5499f7f | 196 | # Only prints matrix of supported IDEs |
screamer | 0:66f3b5499f7f | 197 | if options.supported_ides: |
The Other Jimmy |
36:96847d42f010 | 198 | if options.supported_ides == "matrix": |
The Other Jimmy |
36:96847d42f010 | 199 | print_large_string(mcu_ide_matrix()) |
The Other Jimmy |
36:96847d42f010 | 200 | elif options.supported_ides == "ides": |
The Other Jimmy |
36:96847d42f010 | 201 | print mcu_ide_list() |
screamer | 0:66f3b5499f7f | 202 | exit(0) |
screamer | 0:66f3b5499f7f | 203 | |
screamer | 0:66f3b5499f7f | 204 | # Only prints matrix of supported IDEs |
screamer | 0:66f3b5499f7f | 205 | if options.supported_ides_html: |
screamer | 0:66f3b5499f7f | 206 | html = mcu_ide_matrix(verbose_html=True) |
screamer | 0:66f3b5499f7f | 207 | try: |
The Other Jimmy |
31:8ea194f6145b | 208 | with open("./export/README.md", "w") as readme: |
The Other Jimmy |
31:8ea194f6145b | 209 | readme.write("Exporter IDE/Platform Support\n") |
The Other Jimmy |
31:8ea194f6145b | 210 | readme.write("-----------------------------------\n") |
The Other Jimmy |
31:8ea194f6145b | 211 | readme.write("\n") |
The Other Jimmy |
31:8ea194f6145b | 212 | readme.write(html) |
The Other Jimmy |
31:8ea194f6145b | 213 | except IOError as exc: |
The Other Jimmy |
31:8ea194f6145b | 214 | print "I/O error({0}): {1}".format(exc.errno, exc.strerror) |
screamer | 0:66f3b5499f7f | 215 | except: |
screamer | 0:66f3b5499f7f | 216 | print "Unexpected error:", sys.exc_info()[0] |
screamer | 0:66f3b5499f7f | 217 | raise |
screamer | 0:66f3b5499f7f | 218 | exit(0) |
screamer | 0:66f3b5499f7f | 219 | |
The Other Jimmy |
31:8ea194f6145b | 220 | if options.update_packs: |
The Other Jimmy |
31:8ea194f6145b | 221 | from tools.arm_pack_manager import Cache |
The Other Jimmy |
31:8ea194f6145b | 222 | cache = Cache(True, True) |
The Other Jimmy |
31:8ea194f6145b | 223 | cache.cache_descriptors() |
The Other Jimmy |
31:8ea194f6145b | 224 | |
The Other Jimmy |
31:8ea194f6145b | 225 | # Target |
The Other Jimmy |
31:8ea194f6145b | 226 | if not options.mcu: |
The Other Jimmy |
31:8ea194f6145b | 227 | args_error(parser, "argument -m/--mcu is required") |
The Other Jimmy |
31:8ea194f6145b | 228 | |
The Other Jimmy |
31:8ea194f6145b | 229 | # Toolchain |
The Other Jimmy |
31:8ea194f6145b | 230 | if not options.ide: |
The Other Jimmy |
31:8ea194f6145b | 231 | args_error(parser, "argument -i is required") |
screamer | 0:66f3b5499f7f | 232 | |
The Other Jimmy |
35:da9c89f8be7d | 233 | # Clean Export Directory |
The Other Jimmy |
35:da9c89f8be7d | 234 | if options.clean: |
The Other Jimmy |
35:da9c89f8be7d | 235 | if exists(EXPORT_DIR): |
The Other Jimmy |
35:da9c89f8be7d | 236 | rmtree(EXPORT_DIR) |
The Other Jimmy |
35:da9c89f8be7d | 237 | |
The Other Jimmy |
35:da9c89f8be7d | 238 | for mcu in options.mcu: |
The Other Jimmy |
35:da9c89f8be7d | 239 | zip_proj = not bool(options.source_dir) |
The Other Jimmy |
35:da9c89f8be7d | 240 | |
The Other Jimmy |
31:8ea194f6145b | 241 | if (options.program is None) and (not options.source_dir): |
The Other Jimmy |
31:8ea194f6145b | 242 | args_error(parser, "one of -p, -n, or --source is required") |
The Other Jimmy |
31:8ea194f6145b | 243 | # Export to selected toolchain |
The Other Jimmy |
31:8ea194f6145b | 244 | exporter, toolchain_name = get_exporter_toolchain(options.ide) |
The Other Jimmy |
31:8ea194f6145b | 245 | if options.mcu not in exporter.TARGETS: |
The Other Jimmy |
31:8ea194f6145b | 246 | args_error(parser, "%s not supported by %s"%(options.mcu,options.ide)) |
The Other Jimmy |
35:da9c89f8be7d | 247 | profile = extract_profile(parser, options, toolchain_name, fallback="debug") |
The Other Jimmy |
35:da9c89f8be7d | 248 | if options.clean: |
The Other Jimmy |
35:da9c89f8be7d | 249 | rmtree(BUILD_DIR) |
The Other Jimmy |
38:399953da035d | 250 | mcu = extract_mcus(parser, options)[0] |
The Other Jimmy |
38:399953da035d | 251 | export(mcu, options.ide, build=options.build, |
The Other Jimmy |
31:8ea194f6145b | 252 | src=options.source_dir, macros=options.macros, |
The Other Jimmy |
35:da9c89f8be7d | 253 | project_id=options.program, zip_proj=zip_proj, |
The Other Jimmy |
36:96847d42f010 | 254 | build_profile=profile, app_config=options.app_config) |
screamer | 0:66f3b5499f7f | 255 | |
The Other Jimmy |
31:8ea194f6145b | 256 | |
The Other Jimmy |
31:8ea194f6145b | 257 | if __name__ == "__main__": |
The Other Jimmy |
31:8ea194f6145b | 258 | main() |