Clone of official tools

Committer:
theotherjimmy
Date:
Wed Oct 25 14:46:50 2017 -0500
Revision:
41:2a77626a4c21
Parent:
40:7d3fa6b99b2b
Child:
43:2a7da56ebd24
Update to track Mbed OS 5.6.3

Who changed what in which revision?

UserRevisionLine numberNew 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
theotherjimmy 40:7d3fa6b99b2b 18 from tools.targets import TARGET_NAMES, set_targets_json_location
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
theotherjimmy 41:2a77626a4c21 23 from tools.utils import NotSupportedException
The Other Jimmy 38:399953da035d 24 from tools.options import extract_profile, list_profiles, extract_mcus
theotherjimmy 40:7d3fa6b99b2b 25 from tools.build_profiles import find_targets_json, find_build_profile, get_toolchain_profile
theotherjimmy 40:7d3fa6b99b2b 26 from tools.toolchains import mbedToolchain
The Other Jimmy 31:8ea194f6145b 27
The Other Jimmy 31:8ea194f6145b 28 def setup_project(ide, target, program=None, source_dir=None, build=None, export_path=None):
The Other Jimmy 31:8ea194f6145b 29 """Generate a name, if not provided, and find dependencies
The Other Jimmy 31:8ea194f6145b 30
The Other Jimmy 31:8ea194f6145b 31 Positional arguments:
The Other Jimmy 31:8ea194f6145b 32 ide - IDE or project structure that will soon be exported to
The Other Jimmy 31:8ea194f6145b 33 target - MCU that the project will build for
The Other Jimmy 31:8ea194f6145b 34
The Other Jimmy 31:8ea194f6145b 35 Keyword arguments:
The Other Jimmy 31:8ea194f6145b 36 program - the index of a test program
The Other Jimmy 31:8ea194f6145b 37 source_dir - the directory, or directories that contain all of the sources
The Other Jimmy 31:8ea194f6145b 38 build - a directory that will contain the result of the export
The Other Jimmy 31:8ea194f6145b 39 """
The Other Jimmy 31:8ea194f6145b 40 # Some libraries have extra macros (called by exporter symbols) to we need
The Other Jimmy 31:8ea194f6145b 41 # to pass them to maintain compilation macros integrity between compiled
The Other Jimmy 31:8ea194f6145b 42 # library and header files we might use with it
The Other Jimmy 31:8ea194f6145b 43 if source_dir:
The Other Jimmy 31:8ea194f6145b 44 # --source is used to generate IDE files to toolchain directly
The Other Jimmy 31:8ea194f6145b 45 # in the source tree and doesn't generate zip file
The Other Jimmy 31:8ea194f6145b 46 project_dir = export_path or source_dir[0]
The Other Jimmy 31:8ea194f6145b 47 if program:
The Other Jimmy 31:8ea194f6145b 48 project_name = TESTS[program]
The Other Jimmy 31:8ea194f6145b 49 else:
The Other Jimmy 31:8ea194f6145b 50 project_name = basename(normpath(realpath(source_dir[0])))
The Other Jimmy 31:8ea194f6145b 51 src_paths = source_dir
The Other Jimmy 31:8ea194f6145b 52 lib_paths = None
The Other Jimmy 31:8ea194f6145b 53 else:
The Other Jimmy 31:8ea194f6145b 54 test = Test(program)
The Other Jimmy 31:8ea194f6145b 55 if not build:
The Other Jimmy 31:8ea194f6145b 56 # Substitute the mbed library builds with their sources
The Other Jimmy 31:8ea194f6145b 57 if MBED_LIBRARIES in test.dependencies:
The Other Jimmy 31:8ea194f6145b 58 test.dependencies.remove(MBED_LIBRARIES)
The Other Jimmy 31:8ea194f6145b 59 test.dependencies.append(MBED_HAL)
The Other Jimmy 31:8ea194f6145b 60 test.dependencies.append(MBED_TARGETS_PATH)
screamer 0:66f3b5499f7f 61
screamer 0:66f3b5499f7f 62
The Other Jimmy 31:8ea194f6145b 63 src_paths = [test.source_dir]
The Other Jimmy 31:8ea194f6145b 64 lib_paths = test.dependencies
The Other Jimmy 31:8ea194f6145b 65 project_name = "_".join([test.id, ide, target])
The Other Jimmy 31:8ea194f6145b 66 project_dir = join(EXPORT_DIR, project_name)
screamer 0:66f3b5499f7f 67
The Other Jimmy 31:8ea194f6145b 68 return project_dir, project_name, src_paths, lib_paths
The Other Jimmy 31:8ea194f6145b 69
The Other Jimmy 31:8ea194f6145b 70
The Other Jimmy 31:8ea194f6145b 71 def export(target, ide, build=None, src=None, macros=None, project_id=None,
The Other Jimmy 36:96847d42f010 72 zip_proj=False, build_profile=None, export_path=None, silent=False,
The Other Jimmy 36:96847d42f010 73 app_config=None):
The Other Jimmy 31:8ea194f6145b 74 """Do an export of a project.
The Other Jimmy 31:8ea194f6145b 75
The Other Jimmy 31:8ea194f6145b 76 Positional arguments:
The Other Jimmy 31:8ea194f6145b 77 target - MCU that the project will compile for
The Other Jimmy 31:8ea194f6145b 78 ide - the IDE or project structure to export to
The Other Jimmy 31:8ea194f6145b 79
The Other Jimmy 31:8ea194f6145b 80 Keyword arguments:
The Other Jimmy 31:8ea194f6145b 81 build - to use the compiled mbed libraries or not
The Other Jimmy 31:8ea194f6145b 82 src - directory or directories that contain the source to export
The Other Jimmy 31:8ea194f6145b 83 macros - extra macros to add to the project
The Other Jimmy 31:8ea194f6145b 84 project_id - the name of the project
The Other Jimmy 31:8ea194f6145b 85 clean - start from a clean state before exporting
The Other Jimmy 31:8ea194f6145b 86 zip_proj - create a zip file or not
The Other Jimmy 31:8ea194f6145b 87
The Other Jimmy 31:8ea194f6145b 88 Returns an object of type Exporter (tools/exports/exporters.py)
The Other Jimmy 31:8ea194f6145b 89 """
The Other Jimmy 31:8ea194f6145b 90 project_dir, name, src, lib = setup_project(ide, target, program=project_id,
The Other Jimmy 31:8ea194f6145b 91 source_dir=src, build=build, export_path=export_path)
The Other Jimmy 31:8ea194f6145b 92
The Other Jimmy 31:8ea194f6145b 93 zip_name = name+".zip" if zip_proj else None
The Other Jimmy 31:8ea194f6145b 94
theotherjimmy 40:7d3fa6b99b2b 95 ###################################
theotherjimmy 40:7d3fa6b99b2b 96 # mbed Classic/2.0/libary support #
theotherjimmy 40:7d3fa6b99b2b 97
theotherjimmy 40:7d3fa6b99b2b 98 # Find build system profile
theotherjimmy 40:7d3fa6b99b2b 99 profile = None
theotherjimmy 40:7d3fa6b99b2b 100 targets_json = None
theotherjimmy 40:7d3fa6b99b2b 101 for path in src:
theotherjimmy 40:7d3fa6b99b2b 102 profile = find_build_profile(path) or profile
theotherjimmy 40:7d3fa6b99b2b 103 if profile:
theotherjimmy 40:7d3fa6b99b2b 104 targets_json = join(dirname(dirname(abspath(__file__))), 'legacy_targets.json')
theotherjimmy 40:7d3fa6b99b2b 105 else:
theotherjimmy 40:7d3fa6b99b2b 106 targets_json = find_targets_json(path) or targets_json
theotherjimmy 40:7d3fa6b99b2b 107
theotherjimmy 40:7d3fa6b99b2b 108 # Apply targets.json to active targets
theotherjimmy 40:7d3fa6b99b2b 109 if targets_json:
theotherjimmy 40:7d3fa6b99b2b 110 if not silent:
theotherjimmy 40:7d3fa6b99b2b 111 print("Using targets from %s" % targets_json)
theotherjimmy 40:7d3fa6b99b2b 112 set_targets_json_location(targets_json)
theotherjimmy 40:7d3fa6b99b2b 113
theotherjimmy 40:7d3fa6b99b2b 114 # Apply profile to toolchains
theotherjimmy 40:7d3fa6b99b2b 115 if profile:
theotherjimmy 40:7d3fa6b99b2b 116 def init_hook(self):
theotherjimmy 40:7d3fa6b99b2b 117 profile_data = get_toolchain_profile(self.name, profile)
theotherjimmy 40:7d3fa6b99b2b 118 if not profile_data:
theotherjimmy 40:7d3fa6b99b2b 119 return
theotherjimmy 40:7d3fa6b99b2b 120 if not silent:
theotherjimmy 40:7d3fa6b99b2b 121 self.info("Using toolchain %s profile %s" % (self.name, profile))
theotherjimmy 40:7d3fa6b99b2b 122
theotherjimmy 40:7d3fa6b99b2b 123 for k,v in profile_data.items():
theotherjimmy 40:7d3fa6b99b2b 124 if self.flags.has_key(k):
theotherjimmy 40:7d3fa6b99b2b 125 self.flags[k] = v
theotherjimmy 40:7d3fa6b99b2b 126 else:
theotherjimmy 40:7d3fa6b99b2b 127 setattr(self, k, v)
theotherjimmy 40:7d3fa6b99b2b 128
theotherjimmy 40:7d3fa6b99b2b 129 mbedToolchain.init = init_hook
theotherjimmy 40:7d3fa6b99b2b 130
theotherjimmy 40:7d3fa6b99b2b 131 # mbed Classic/2.0/libary support #
theotherjimmy 40:7d3fa6b99b2b 132 ###################################
theotherjimmy 40:7d3fa6b99b2b 133
The Other Jimmy 35:da9c89f8be7d 134 return export_project(src, project_dir, target, ide, name=name,
The Other Jimmy 35:da9c89f8be7d 135 macros=macros, libraries_paths=lib, zip_proj=zip_name,
The Other Jimmy 36:96847d42f010 136 build_profile=build_profile, silent=silent,
The Other Jimmy 36:96847d42f010 137 app_config=app_config)
The Other Jimmy 31:8ea194f6145b 138
The Other Jimmy 31:8ea194f6145b 139
The Other Jimmy 31:8ea194f6145b 140 def main():
The Other Jimmy 31:8ea194f6145b 141 """Entry point"""
screamer 0:66f3b5499f7f 142 # Parse Options
screamer 22:9e85236d8716 143 parser = ArgumentParser()
screamer 0:66f3b5499f7f 144
screamer 0:66f3b5499f7f 145 targetnames = TARGET_NAMES
screamer 0:66f3b5499f7f 146 targetnames.sort()
screamer 0:66f3b5499f7f 147 toolchainlist = EXPORTERS.keys()
screamer 0:66f3b5499f7f 148 toolchainlist.sort()
screamer 0:66f3b5499f7f 149
screamer 22:9e85236d8716 150 parser.add_argument("-m", "--mcu",
The Other Jimmy 31:8ea194f6145b 151 metavar="MCU",
theotherjimmy 40:7d3fa6b99b2b 152 type=str.upper,
The Other Jimmy 31:8ea194f6145b 153 help="generate project for the given MCU ({})".format(
The Other Jimmy 31:8ea194f6145b 154 ', '.join(targetnames)))
screamer 0:66f3b5499f7f 155
screamer 22:9e85236d8716 156 parser.add_argument("-i",
The Other Jimmy 31:8ea194f6145b 157 dest="ide",
The Other Jimmy 31:8ea194f6145b 158 type=argparse_force_lowercase_type(
The Other Jimmy 31:8ea194f6145b 159 toolchainlist, "toolchain"),
The Other Jimmy 31:8ea194f6145b 160 help="The target IDE: %s"% str(toolchainlist))
screamer 0:66f3b5499f7f 161
screamer 22:9e85236d8716 162 parser.add_argument("-c", "--clean",
The Other Jimmy 31:8ea194f6145b 163 action="store_true",
The Other Jimmy 31:8ea194f6145b 164 default=False,
The Other Jimmy 31:8ea194f6145b 165 help="clean the export directory")
screamer 0:66f3b5499f7f 166
screamer 22:9e85236d8716 167 group = parser.add_mutually_exclusive_group(required=False)
The Other Jimmy 31:8ea194f6145b 168 group.add_argument(
The Other Jimmy 31:8ea194f6145b 169 "-p",
The Other Jimmy 31:8ea194f6145b 170 type=test_known,
The Other Jimmy 31:8ea194f6145b 171 dest="program",
The Other Jimmy 31:8ea194f6145b 172 help="The index of the desired test program: [0-%s]"% (len(TESTS)-1))
screamer 0:66f3b5499f7f 173
screamer 22:9e85236d8716 174 group.add_argument("-n",
The Other Jimmy 31:8ea194f6145b 175 type=test_name_known,
The Other Jimmy 31:8ea194f6145b 176 dest="program",
The Other Jimmy 31:8ea194f6145b 177 help="The name of the desired test program")
screamer 0:66f3b5499f7f 178
screamer 22:9e85236d8716 179 parser.add_argument("-b",
screamer 0:66f3b5499f7f 180 dest="build",
screamer 0:66f3b5499f7f 181 default=False,
screamer 29:1210849dba19 182 action="store_true",
screamer 0:66f3b5499f7f 183 help="use the mbed library build, instead of the sources")
screamer 0:66f3b5499f7f 184
screamer 22:9e85236d8716 185 group.add_argument("-L", "--list-tests",
The Other Jimmy 31:8ea194f6145b 186 action="store_true",
The Other Jimmy 31:8ea194f6145b 187 dest="list_tests",
The Other Jimmy 31:8ea194f6145b 188 default=False,
The Other Jimmy 31:8ea194f6145b 189 help="list available programs in order and exit")
screamer 0:66f3b5499f7f 190
screamer 22:9e85236d8716 191 group.add_argument("-S", "--list-matrix",
The Other Jimmy 31:8ea194f6145b 192 dest="supported_ides",
The Other Jimmy 31:8ea194f6145b 193 default=False,
The Other Jimmy 36:96847d42f010 194 const="matrix",
The Other Jimmy 36:96847d42f010 195 choices=["matrix", "ides"],
The Other Jimmy 36:96847d42f010 196 nargs="?",
The Other Jimmy 31:8ea194f6145b 197 help="displays supported matrix of MCUs and IDEs")
screamer 0:66f3b5499f7f 198
screamer 22:9e85236d8716 199 parser.add_argument("-E",
The Other Jimmy 31:8ea194f6145b 200 action="store_true",
The Other Jimmy 31:8ea194f6145b 201 dest="supported_ides_html",
The Other Jimmy 31:8ea194f6145b 202 default=False,
The Other Jimmy 31:8ea194f6145b 203 help="writes tools/export/README.md")
screamer 0:66f3b5499f7f 204
screamer 22:9e85236d8716 205 parser.add_argument("--source",
The Other Jimmy 31:8ea194f6145b 206 action="append",
The Other Jimmy 31:8ea194f6145b 207 type=argparse_filestring_type,
The Other Jimmy 31:8ea194f6145b 208 dest="source_dir",
The Other Jimmy 31:8ea194f6145b 209 default=[],
The Other Jimmy 31:8ea194f6145b 210 help="The source (input) directory")
screamer 0:66f3b5499f7f 211
screamer 22:9e85236d8716 212 parser.add_argument("-D",
The Other Jimmy 31:8ea194f6145b 213 action="append",
The Other Jimmy 31:8ea194f6145b 214 dest="macros",
The Other Jimmy 31:8ea194f6145b 215 help="Add a macro definition")
The Other Jimmy 31:8ea194f6145b 216
The Other Jimmy 31:8ea194f6145b 217 parser.add_argument("--profile", dest="profile", action="append",
The Other Jimmy 31:8ea194f6145b 218 type=argparse_profile_filestring_type,
The Other Jimmy 31:8ea194f6145b 219 help="Build profile to use. Can be either path to json" \
The Other Jimmy 31:8ea194f6145b 220 "file or one of the default one ({})".format(", ".join(list_profiles())),
The Other Jimmy 31:8ea194f6145b 221 default=[])
The Other Jimmy 31:8ea194f6145b 222
The Other Jimmy 31:8ea194f6145b 223 parser.add_argument("--update-packs",
The Other Jimmy 31:8ea194f6145b 224 dest="update_packs",
The Other Jimmy 31:8ea194f6145b 225 action="store_true",
The Other Jimmy 31:8ea194f6145b 226 default=False)
The Other Jimmy 36:96847d42f010 227 parser.add_argument("--app-config",
The Other Jimmy 36:96847d42f010 228 dest="app_config",
The Other Jimmy 36:96847d42f010 229 default=None)
screamer 0:66f3b5499f7f 230
screamer 22:9e85236d8716 231 options = parser.parse_args()
screamer 0:66f3b5499f7f 232
screamer 0:66f3b5499f7f 233 # Print available tests in order and exit
screamer 0:66f3b5499f7f 234 if options.list_tests is True:
The Other Jimmy 31:8ea194f6145b 235 print '\n'.join([str(test) for test in sorted(TEST_MAP.values())])
screamer 0:66f3b5499f7f 236 sys.exit()
screamer 0:66f3b5499f7f 237
screamer 0:66f3b5499f7f 238 # Only prints matrix of supported IDEs
screamer 0:66f3b5499f7f 239 if options.supported_ides:
The Other Jimmy 36:96847d42f010 240 if options.supported_ides == "matrix":
The Other Jimmy 36:96847d42f010 241 print_large_string(mcu_ide_matrix())
The Other Jimmy 36:96847d42f010 242 elif options.supported_ides == "ides":
The Other Jimmy 36:96847d42f010 243 print mcu_ide_list()
screamer 0:66f3b5499f7f 244 exit(0)
screamer 0:66f3b5499f7f 245
screamer 0:66f3b5499f7f 246 # Only prints matrix of supported IDEs
screamer 0:66f3b5499f7f 247 if options.supported_ides_html:
screamer 0:66f3b5499f7f 248 html = mcu_ide_matrix(verbose_html=True)
screamer 0:66f3b5499f7f 249 try:
The Other Jimmy 31:8ea194f6145b 250 with open("./export/README.md", "w") as readme:
The Other Jimmy 31:8ea194f6145b 251 readme.write("Exporter IDE/Platform Support\n")
The Other Jimmy 31:8ea194f6145b 252 readme.write("-----------------------------------\n")
The Other Jimmy 31:8ea194f6145b 253 readme.write("\n")
The Other Jimmy 31:8ea194f6145b 254 readme.write(html)
The Other Jimmy 31:8ea194f6145b 255 except IOError as exc:
The Other Jimmy 31:8ea194f6145b 256 print "I/O error({0}): {1}".format(exc.errno, exc.strerror)
screamer 0:66f3b5499f7f 257 except:
screamer 0:66f3b5499f7f 258 print "Unexpected error:", sys.exc_info()[0]
screamer 0:66f3b5499f7f 259 raise
screamer 0:66f3b5499f7f 260 exit(0)
screamer 0:66f3b5499f7f 261
The Other Jimmy 31:8ea194f6145b 262 if options.update_packs:
The Other Jimmy 31:8ea194f6145b 263 from tools.arm_pack_manager import Cache
The Other Jimmy 31:8ea194f6145b 264 cache = Cache(True, True)
The Other Jimmy 31:8ea194f6145b 265 cache.cache_descriptors()
The Other Jimmy 31:8ea194f6145b 266
The Other Jimmy 31:8ea194f6145b 267 # Target
The Other Jimmy 31:8ea194f6145b 268 if not options.mcu:
The Other Jimmy 31:8ea194f6145b 269 args_error(parser, "argument -m/--mcu is required")
The Other Jimmy 31:8ea194f6145b 270
The Other Jimmy 31:8ea194f6145b 271 # Toolchain
The Other Jimmy 31:8ea194f6145b 272 if not options.ide:
The Other Jimmy 31:8ea194f6145b 273 args_error(parser, "argument -i is required")
screamer 0:66f3b5499f7f 274
The Other Jimmy 35:da9c89f8be7d 275 # Clean Export Directory
The Other Jimmy 35:da9c89f8be7d 276 if options.clean:
The Other Jimmy 35:da9c89f8be7d 277 if exists(EXPORT_DIR):
The Other Jimmy 35:da9c89f8be7d 278 rmtree(EXPORT_DIR)
The Other Jimmy 35:da9c89f8be7d 279
theotherjimmy 40:7d3fa6b99b2b 280 zip_proj = not bool(options.source_dir)
The Other Jimmy 35:da9c89f8be7d 281
The Other Jimmy 31:8ea194f6145b 282 if (options.program is None) and (not options.source_dir):
The Other Jimmy 31:8ea194f6145b 283 args_error(parser, "one of -p, -n, or --source is required")
The Other Jimmy 31:8ea194f6145b 284 exporter, toolchain_name = get_exporter_toolchain(options.ide)
theotherjimmy 40:7d3fa6b99b2b 285 mcu = extract_mcus(parser, options)[0]
theotherjimmy 40:7d3fa6b99b2b 286 if not exporter.is_target_supported(mcu):
theotherjimmy 40:7d3fa6b99b2b 287 args_error(parser, "%s not supported by %s"%(mcu,options.ide))
The Other Jimmy 35:da9c89f8be7d 288 profile = extract_profile(parser, options, toolchain_name, fallback="debug")
The Other Jimmy 35:da9c89f8be7d 289 if options.clean:
The Other Jimmy 35:da9c89f8be7d 290 rmtree(BUILD_DIR)
theotherjimmy 41:2a77626a4c21 291 try:
theotherjimmy 41:2a77626a4c21 292 export(mcu, options.ide, build=options.build,
theotherjimmy 41:2a77626a4c21 293 src=options.source_dir, macros=options.macros,
theotherjimmy 41:2a77626a4c21 294 project_id=options.program, zip_proj=zip_proj,
theotherjimmy 41:2a77626a4c21 295 build_profile=profile, app_config=options.app_config)
theotherjimmy 41:2a77626a4c21 296 except NotSupportedException as exc:
theotherjimmy 41:2a77626a4c21 297 print "[ERROR] %s" % str(exc)
The Other Jimmy 31:8ea194f6145b 298
The Other Jimmy 31:8ea194f6145b 299 if __name__ == "__main__":
The Other Jimmy 31:8ea194f6145b 300 main()