mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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