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