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