nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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