joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers project.py Source File

project.py

00001 """ The CLI entry point for exporting projects from the mbed tools to any of the
00002 supported IDEs or project structures.
00003 """
00004 import sys
00005 from os.path import join, abspath, dirname, exists, basename
00006 ROOT = abspath(join(dirname(__file__), ".."))
00007 sys.path.insert(0, ROOT)
00008 
00009 from shutil import move, rmtree
00010 from argparse import ArgumentParser
00011 from os.path import normpath, realpath
00012 
00013 from tools.paths import EXPORT_DIR, MBED_BASE, MBED_LIBRARIES
00014 from tools.export import EXPORTERS, mcu_ide_matrix
00015 from tools.tests import TESTS, TEST_MAP
00016 from tools.tests import test_known, test_name_known, Test
00017 from tools.targets import TARGET_NAMES
00018 from tools.utils import argparse_filestring_type, argparse_many, args_error
00019 from tools.utils import argparse_force_lowercase_type
00020 from tools.utils import argparse_force_uppercase_type
00021 from tools.project_api import export_project
00022 
00023 
00024 def setup_project (ide, target, program=None, source_dir=None, build=None):
00025     """Generate a name, if not provided, and find dependencies
00026 
00027     Positional arguments:
00028     ide - IDE or project structure that will soon be exported to
00029     target - MCU that the project will build for
00030 
00031     Keyword arguments:
00032     program - the index of a test program
00033     source_dir - the directory, or directories that contain all of the sources
00034     build - a directory that will contain the result of the export
00035     """
00036     # Some libraries have extra macros (called by exporter symbols) to we need
00037     # to pass them to maintain compilation macros integrity between compiled
00038     # library and header files we might use with it
00039     if source_dir:
00040         # --source is used to generate IDE files to toolchain directly
00041         # in the source tree and doesn't generate zip file
00042         project_dir = source_dir[0]
00043         if program:
00044             project_name = TESTS[program]
00045         else:
00046             project_name = basename(normpath(realpath(source_dir[0])))
00047         src_paths = source_dir
00048         lib_paths = None
00049     else:
00050         test = Test(program)
00051         if not build:
00052             # Substitute the mbed library builds with their sources
00053             if MBED_LIBRARIES in test.dependencies:
00054                 test.dependencies.remove(MBED_LIBRARIES)
00055                 test.dependencies.append(MBED_BASE)
00056 
00057         src_paths = [test.source_dir]
00058         lib_paths = test.dependencies
00059         project_name = "_".join([test.id, ide, target])
00060         project_dir = join(EXPORT_DIR, project_name)
00061 
00062     return project_dir, project_name, src_paths, lib_paths
00063 
00064 
00065 def export (target, ide, build=None, src=None, macros=None, project_id=None,
00066            clean=False, zip_proj=False, options=None):
00067     """Do an export of a project.
00068 
00069     Positional arguments:
00070     target - MCU that the project will compile for
00071     ide - the IDE or project structure to export to
00072 
00073     Keyword arguments:
00074     build - to use the compiled mbed libraries or not
00075     src - directory or directories that contain the source to export
00076     macros - extra macros to add to the project
00077     project_id - the name of the project
00078     clean - start from a clean state before exporting
00079     zip_proj - create a zip file or not
00080     """
00081     project_dir, name, src, lib = setup_project(ide, target, program=project_id,
00082                                                 source_dir=src, build=build)
00083 
00084     zip_name = name+".zip" if zip_proj else None
00085 
00086     export_project(src, project_dir, target, ide, clean=clean, name=name,
00087                    macros=macros, libraries_paths=lib, zip_proj=zip_name, options=options)
00088 
00089 
00090 def main ():
00091     """Entry point"""
00092     # Parse Options
00093     parser = ArgumentParser()
00094 
00095     targetnames = TARGET_NAMES
00096     targetnames.sort()
00097     toolchainlist = EXPORTERS.keys()
00098     toolchainlist.sort()
00099 
00100     parser.add_argument("-m", "--mcu",
00101                         metavar="MCU",
00102                         default='LPC1768',
00103                         type=argparse_force_uppercase_type(targetnames, "MCU"),
00104                         help="generate project for the given MCU ({})".format(
00105                             ', '.join(targetnames)))
00106 
00107     parser.add_argument("-i",
00108                         dest="ide",
00109                         default='uvision',
00110                         type=argparse_force_lowercase_type(
00111                             toolchainlist, "toolchain"),
00112                         help="The target IDE: %s"% str(toolchainlist))
00113 
00114     parser.add_argument("-c", "--clean",
00115                         action="store_true",
00116                         default=False,
00117                         help="clean the export directory")
00118 
00119     group = parser.add_mutually_exclusive_group(required=False)
00120     group.add_argument(
00121         "-p",
00122         type=test_known,
00123         dest="program",
00124         help="The index of the desired test program: [0-%s]"% (len(TESTS)-1))
00125 
00126     group.add_argument("-n",
00127                        type=test_name_known,
00128                        dest="program",
00129                        help="The name of the desired test program")
00130 
00131     parser.add_argument("-b",
00132                       dest="build",
00133                       default=False,
00134                       action="store_true",
00135                       help="use the mbed library build, instead of the sources")
00136 
00137     group.add_argument("-L", "--list-tests",
00138                        action="store_true",
00139                        dest="list_tests",
00140                        default=False,
00141                        help="list available programs in order and exit")
00142 
00143     group.add_argument("-S", "--list-matrix",
00144                        action="store_true",
00145                        dest="supported_ides",
00146                        default=False,
00147                        help="displays supported matrix of MCUs and IDEs")
00148 
00149     parser.add_argument("-E",
00150                         action="store_true",
00151                         dest="supported_ides_html",
00152                         default=False,
00153                         help="writes tools/export/README.md")
00154 
00155     parser.add_argument("--source",
00156                         action="append",
00157                         type=argparse_filestring_type,
00158                         dest="source_dir",
00159                         default=[],
00160                         help="The source (input) directory")
00161 
00162     parser.add_argument("-D",
00163                         action="append",
00164                         dest="macros",
00165                         help="Add a macro definition")
00166 
00167     parser.add_argument("-o",
00168                         type=argparse_many(str),
00169                         dest="opts",
00170                         default=["debug-info"],
00171                         help="Toolchain options")
00172 
00173     options = parser.parse_args()
00174 
00175     # Print available tests in order and exit
00176     if options.list_tests is True:
00177         print '\n'.join([str(test) for test in  sorted(TEST_MAP.values())])
00178         sys.exit()
00179 
00180     # Only prints matrix of supported IDEs
00181     if options.supported_ides:
00182         print mcu_ide_matrix()
00183         exit(0)
00184 
00185     # Only prints matrix of supported IDEs
00186     if options.supported_ides_html:
00187         html = mcu_ide_matrix(verbose_html=True)
00188         try:
00189             with open("./export/README.md", "w") as readme:
00190                 readme.write("Exporter IDE/Platform Support\n")
00191                 readme.write("-----------------------------------\n")
00192                 readme.write("\n")
00193                 readme.write(html)
00194         except IOError as exc:
00195             print "I/O error({0}): {1}".format(exc.errno, exc.strerror)
00196         except:
00197             print "Unexpected error:", sys.exc_info()[0]
00198             raise
00199         exit(0)
00200 
00201     # Clean Export Directory
00202     if options.clean:
00203         if exists(EXPORT_DIR):
00204             rmtree(EXPORT_DIR)
00205 
00206     for mcu in options.mcu:
00207         zip_proj = not bool(options.source_dir)
00208 
00209     # Target
00210     if not options.mcu:
00211         args_error(parser, "argument -m/--mcu is required")
00212 
00213     # Toolchain
00214     if not options.ide:
00215         args_error(parser, "argument -i is required")
00216 
00217     if (options.program is None) and (not options.source_dir):
00218         args_error(parser, "one of -p, -n, or --source is required")
00219         # Export to selected toolchain
00220     export(options.mcu, options.ide, build=options.build,
00221            src=options.source_dir, macros=options.macros,
00222            project_id=options.program, clean=options.clean,
00223            zip_proj=zip_proj, options=options.opts)
00224 
00225 
00226 if __name__ == "__main__":
00227     main()