BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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