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