Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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