Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

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