init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:12:37 2018 -0700
Revision:
9:d58e77ebd769
add mbed-os library

Who changed what in which revision?

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