Clone of official tools

Committer:
theotherjimmy
Date:
Tue Oct 10 16:56:30 2017 -0500
Revision:
40:7d3fa6b99b2b
Parent:
38:399953da035d
Child:
41:2a77626a4c21
Update to tools release 5.6.1

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