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