the other jimmy / mbed-sdk-tools

Fork of mbed-sdk-tools by mbed official

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, MBED_TARGETS_PATH
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_profile_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, list_profiles
00024 
00025 def setup_project (ide, target, program=None, source_dir=None, build=None, export_path=None):
00026     """Generate a name, if not provided, and find dependencies
00027 
00028     Positional arguments:
00029     ide - IDE or project structure that will soon be exported to
00030     target - MCU that the project will build for
00031 
00032     Keyword arguments:
00033     program - the index of a test program
00034     source_dir - the directory, or directories that contain all of the sources
00035     build - a directory that will contain the result of the export
00036     """
00037     # Some libraries have extra macros (called by exporter symbols) to we need
00038     # to pass them to maintain compilation macros integrity between compiled
00039     # library and header files we might use with it
00040     if source_dir:
00041         # --source is used to generate IDE files to toolchain directly
00042         # in the source tree and doesn't generate zip file
00043         project_dir = export_path or source_dir[0]
00044         if program:
00045             project_name = TESTS[program]
00046         else:
00047             project_name = basename(normpath(realpath(source_dir[0])))
00048         src_paths = source_dir
00049         lib_paths = None
00050     else:
00051         test = Test(program)
00052         if not build:
00053             # Substitute the mbed library builds with their sources
00054             if MBED_LIBRARIES in test.dependencies:
00055                 test.dependencies.remove(MBED_LIBRARIES)
00056                 test.dependencies.append(MBED_HAL)
00057                 test.dependencies.append(MBED_TARGETS_PATH)
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", dest="profile", action="append",
00175                         type=argparse_profile_filestring_type,
00176                         help="Build profile to use. Can be either path to json" \
00177                         "file or one of the default one ({})".format(", ".join(list_profiles())),
00178                         default=[])
00179 
00180     parser.add_argument("--update-packs",
00181                         dest="update_packs",
00182                         action="store_true",
00183                         default=False)
00184 
00185     options = parser.parse_args()
00186 
00187     # Print available tests in order and exit
00188     if options.list_tests is True:
00189         print '\n'.join([str(test) for test in  sorted(TEST_MAP.values())])
00190         sys.exit()
00191 
00192     # Only prints matrix of supported IDEs
00193     if options.supported_ides:
00194         print_large_string(mcu_ide_matrix())
00195         exit(0)
00196 
00197     # Only prints matrix of supported IDEs
00198     if options.supported_ides_html:
00199         html = mcu_ide_matrix(verbose_html=True)
00200         try:
00201             with open("./export/README.md", "w") as readme:
00202                 readme.write("Exporter IDE/Platform Support\n")
00203                 readme.write("-----------------------------------\n")
00204                 readme.write("\n")
00205                 readme.write(html)
00206         except IOError as exc:
00207             print "I/O error({0}): {1}".format(exc.errno, exc.strerror)
00208         except:
00209             print "Unexpected error:", sys.exc_info()[0]
00210             raise
00211         exit(0)
00212 
00213     if options.update_packs:
00214         from tools.arm_pack_manager import Cache
00215         cache = Cache(True, True)
00216         cache.cache_descriptors()
00217 
00218     # Clean Export Directory
00219     if options.clean:
00220         if exists(EXPORT_DIR):
00221             rmtree(EXPORT_DIR)
00222 
00223     for mcu in options.mcu:
00224         zip_proj = not bool(options.source_dir)
00225 
00226     # Target
00227     if not options.mcu:
00228         args_error(parser, "argument -m/--mcu is required")
00229 
00230     # Toolchain
00231     if not options.ide:
00232         args_error(parser, "argument -i is required")
00233 
00234     if (options.program is None) and (not options.source_dir):
00235         args_error(parser, "one of -p, -n, or --source is required")
00236         # Export to selected toolchain
00237     exporter, toolchain_name = get_exporter_toolchain(options.ide)
00238     if options.mcu not in exporter.TARGETS:
00239         args_error(parser, "%s not supported by %s"%(options.mcu,options.ide))
00240     profile = extract_profile(parser, options, toolchain_name)
00241     export(options.mcu, options.ide, build=options.build,
00242            src=options.source_dir, macros=options.macros,
00243            project_id=options.program, clean=options.clean,
00244            zip_proj=zip_proj, build_profile=profile)
00245 
00246 
00247 if __name__ == "__main__":
00248     main()