mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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