2016-07-20 19:43:09 +00:00
|
|
|
""" The CLI entry point for exporting projects from the mbed tools to any of the
|
|
|
|
supported IDEs or project structures.
|
|
|
|
"""
|
2018-02-23 15:57:02 +00:00
|
|
|
from __future__ import print_function, absolute_import
|
|
|
|
from builtins import str
|
|
|
|
|
2013-08-16 15:39:30 +00:00
|
|
|
import sys
|
2018-02-21 15:26:14 +00:00
|
|
|
from os.path import (join, abspath, dirname, exists, basename, normpath,
|
2018-02-20 16:44:16 +00:00
|
|
|
realpath, relpath, basename)
|
2018-02-21 15:26:14 +00:00
|
|
|
from os import remove
|
2013-08-16 15:39:30 +00:00
|
|
|
ROOT = abspath(join(dirname(__file__), ".."))
|
2013-12-19 13:02:57 +00:00
|
|
|
sys.path.insert(0, ROOT)
|
2013-08-16 15:39:30 +00:00
|
|
|
|
2014-10-15 21:42:48 +00:00
|
|
|
from shutil import move, rmtree
|
2016-06-24 22:15:01 +00:00
|
|
|
from argparse import ArgumentParser
|
2013-08-16 15:39:30 +00:00
|
|
|
|
2016-11-02 21:14:40 +00:00
|
|
|
from tools.paths import EXPORT_DIR, MBED_HAL, MBED_LIBRARIES, MBED_TARGETS_PATH
|
2016-09-13 15:27:30 +00:00
|
|
|
from tools.settings import BUILD_DIR
|
2018-07-03 19:03:28 +00:00
|
|
|
from tools.export import (
|
|
|
|
EXPORTERS,
|
|
|
|
mcu_ide_matrix,
|
|
|
|
mcu_ide_list,
|
|
|
|
export_project,
|
|
|
|
get_exporter_toolchain,
|
|
|
|
)
|
2016-07-15 16:14:21 +00:00
|
|
|
from tools.tests import TESTS, TEST_MAP
|
2016-07-20 19:43:09 +00:00
|
|
|
from tools.tests import test_known, test_name_known, Test
|
2016-06-09 20:34:53 +00:00
|
|
|
from tools.targets import TARGET_NAMES
|
2018-07-03 19:03:28 +00:00
|
|
|
from tools.utils import (
|
|
|
|
argparse_filestring_type,
|
|
|
|
argparse_profile_filestring_type,
|
|
|
|
argparse_many,
|
|
|
|
args_error,
|
|
|
|
)
|
2016-07-20 19:43:09 +00:00
|
|
|
from tools.utils import argparse_force_lowercase_type
|
|
|
|
from tools.utils import argparse_force_uppercase_type
|
2016-10-31 20:39:34 +00:00
|
|
|
from tools.utils import print_large_string
|
2017-08-23 13:31:07 +00:00
|
|
|
from tools.utils import NotSupportedException
|
2017-04-06 19:40:38 +00:00
|
|
|
from tools.options import extract_profile, list_profiles, extract_mcus
|
2018-04-30 19:42:33 +00:00
|
|
|
from tools.notifier.term import TerminalNotifier
|
2016-07-20 19:43:09 +00:00
|
|
|
|
2018-07-03 18:56:23 +00:00
|
|
|
EXPORTER_ALIASES = {
|
|
|
|
u'gcc_arm': u'make_gcc_arm',
|
|
|
|
u'uvision': u'uvision5',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_exporter_alias(ide):
|
|
|
|
if ide in EXPORTER_ALIASES:
|
|
|
|
return EXPORTER_ALIASES[ide]
|
|
|
|
else:
|
|
|
|
return ide
|
|
|
|
|
|
|
|
|
2018-07-03 19:03:28 +00:00
|
|
|
def setup_project(
|
|
|
|
ide,
|
|
|
|
target,
|
|
|
|
program=None,
|
|
|
|
source_dir=None,
|
|
|
|
build=None,
|
|
|
|
export_path=None
|
|
|
|
):
|
2016-07-20 19:43:09 +00:00
|
|
|
"""Generate a name, if not provided, and find dependencies
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
ide - IDE or project structure that will soon be exported to
|
|
|
|
target - MCU that the project will build for
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
program - the index of a test program
|
|
|
|
source_dir - the directory, or directories that contain all of the sources
|
|
|
|
build - a directory that will contain the result of the export
|
|
|
|
"""
|
|
|
|
# Some libraries have extra macros (called by exporter symbols) to we need
|
|
|
|
# to pass them to maintain compilation macros integrity between compiled
|
|
|
|
# library and header files we might use with it
|
|
|
|
if source_dir:
|
|
|
|
# --source is used to generate IDE files to toolchain directly
|
|
|
|
# in the source tree and doesn't generate zip file
|
2016-09-20 15:12:17 +00:00
|
|
|
project_dir = export_path or source_dir[0]
|
2016-07-20 19:43:09 +00:00
|
|
|
if program:
|
|
|
|
project_name = TESTS[program]
|
|
|
|
else:
|
2016-08-18 19:30:46 +00:00
|
|
|
project_name = basename(normpath(realpath(source_dir[0])))
|
2018-02-20 16:44:16 +00:00
|
|
|
src_paths = {relpath(path, project_dir): [path] for path in source_dir}
|
2016-07-20 19:43:09 +00:00
|
|
|
lib_paths = None
|
|
|
|
else:
|
|
|
|
test = Test(program)
|
|
|
|
if not build:
|
|
|
|
# Substitute the mbed library builds with their sources
|
|
|
|
if MBED_LIBRARIES in test.dependencies:
|
|
|
|
test.dependencies.remove(MBED_LIBRARIES)
|
2016-10-03 18:04:35 +00:00
|
|
|
test.dependencies.append(MBED_HAL)
|
2016-11-02 21:14:40 +00:00
|
|
|
test.dependencies.append(MBED_TARGETS_PATH)
|
2016-10-03 18:04:35 +00:00
|
|
|
|
2016-07-20 19:43:09 +00:00
|
|
|
src_paths = [test.source_dir]
|
|
|
|
lib_paths = test.dependencies
|
|
|
|
project_name = "_".join([test.id, ide, target])
|
|
|
|
project_dir = join(EXPORT_DIR, project_name)
|
|
|
|
|
|
|
|
return project_dir, project_name, src_paths, lib_paths
|
|
|
|
|
|
|
|
|
|
|
|
def export(target, ide, build=None, src=None, macros=None, project_id=None,
|
2018-04-30 19:42:33 +00:00
|
|
|
zip_proj=False, build_profile=None, export_path=None, notify=None,
|
2018-05-11 04:00:01 +00:00
|
|
|
app_config=None, ignore=None):
|
2016-07-20 19:43:09 +00:00
|
|
|
"""Do an export of a project.
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
target - MCU that the project will compile for
|
|
|
|
ide - the IDE or project structure to export to
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
build - to use the compiled mbed libraries or not
|
|
|
|
src - directory or directories that contain the source to export
|
|
|
|
macros - extra macros to add to the project
|
|
|
|
project_id - the name of the project
|
|
|
|
clean - start from a clean state before exporting
|
|
|
|
zip_proj - create a zip file or not
|
2018-05-11 04:00:01 +00:00
|
|
|
ignore - list of paths to add to mbedignore
|
2016-09-20 15:12:17 +00:00
|
|
|
|
|
|
|
Returns an object of type Exporter (tools/exports/exporters.py)
|
2016-07-20 19:43:09 +00:00
|
|
|
"""
|
2018-07-03 19:03:28 +00:00
|
|
|
project_dir, name, src, lib = setup_project(
|
|
|
|
ide,
|
|
|
|
target,
|
|
|
|
program=project_id,
|
|
|
|
source_dir=src,
|
|
|
|
build=build,
|
|
|
|
export_path=export_path,
|
|
|
|
)
|
2016-07-20 19:43:09 +00:00
|
|
|
|
|
|
|
zip_name = name+".zip" if zip_proj else None
|
|
|
|
|
2018-07-03 19:03:28 +00:00
|
|
|
return export_project(
|
|
|
|
src,
|
|
|
|
project_dir,
|
|
|
|
target,
|
|
|
|
ide,
|
|
|
|
name=name,
|
|
|
|
macros=macros,
|
|
|
|
libraries_paths=lib,
|
|
|
|
zip_proj=zip_name,
|
|
|
|
build_profile=build_profile,
|
|
|
|
notify=notify,
|
|
|
|
app_config=app_config,
|
|
|
|
ignore=ignore
|
|
|
|
)
|
2016-07-20 19:43:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Entry point"""
|
2013-08-16 15:39:30 +00:00
|
|
|
# Parse Options
|
2016-06-24 22:15:01 +00:00
|
|
|
parser = ArgumentParser()
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2014-10-14 16:04:32 +00:00
|
|
|
targetnames = TARGET_NAMES
|
|
|
|
targetnames.sort()
|
2018-07-03 18:56:23 +00:00
|
|
|
toolchainlist = list(EXPORTERS.keys() + EXPORTER_ALIASES.keys())
|
2014-10-14 16:04:32 +00:00
|
|
|
toolchainlist.sort()
|
|
|
|
|
2018-07-03 19:03:28 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-m", "--mcu",
|
|
|
|
metavar="MCU",
|
|
|
|
help="generate project for the given MCU ({})".format(
|
|
|
|
', '.join(targetnames))
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-i",
|
|
|
|
dest="ide",
|
|
|
|
type=argparse_force_lowercase_type(
|
|
|
|
toolchainlist, "toolchain"),
|
|
|
|
help="The target IDE: %s" % str(toolchainlist)
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-c", "--clean",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="clean the export directory"
|
|
|
|
)
|
2014-11-11 16:04:59 +00:00
|
|
|
|
2016-06-29 21:18:36 +00:00
|
|
|
group = parser.add_mutually_exclusive_group(required=False)
|
2016-07-20 19:43:09 +00:00
|
|
|
group.add_argument(
|
|
|
|
"-p",
|
|
|
|
type=test_known,
|
|
|
|
dest="program",
|
2018-07-03 19:03:28 +00:00
|
|
|
help="The index of the desired test program: [0-%s]" % (len(TESTS) - 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
group.add_argument(
|
|
|
|
"-n",
|
|
|
|
type=test_name_known,
|
|
|
|
dest="program",
|
|
|
|
help="The name of the desired test program"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-b",
|
|
|
|
dest="build",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help="use the mbed library build, instead of the sources"
|
|
|
|
)
|
|
|
|
|
|
|
|
group.add_argument(
|
|
|
|
"-L", "--list-tests",
|
|
|
|
action="store_true",
|
|
|
|
dest="list_tests",
|
|
|
|
default=False,
|
|
|
|
help="list available programs in order and exit"
|
|
|
|
)
|
|
|
|
|
|
|
|
group.add_argument(
|
|
|
|
"-S", "--list-matrix",
|
|
|
|
dest="supported_ides",
|
|
|
|
default=False,
|
|
|
|
const="matrix",
|
|
|
|
choices=["matrix", "ides"],
|
|
|
|
nargs="?",
|
|
|
|
help="displays supported matrix of MCUs and IDEs"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-E",
|
|
|
|
action="store_true",
|
|
|
|
dest="supported_ides_html",
|
|
|
|
default=False,
|
|
|
|
help="writes tools/export/README.md"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--build",
|
|
|
|
type=argparse_filestring_type,
|
|
|
|
dest="build_dir",
|
|
|
|
default=None,
|
|
|
|
help="Directory for the exported project files"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--source",
|
|
|
|
action="append",
|
|
|
|
type=argparse_filestring_type,
|
|
|
|
dest="source_dir",
|
|
|
|
default=[],
|
|
|
|
help="The source (input) directory"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-D",
|
|
|
|
action="append",
|
|
|
|
dest="macros",
|
|
|
|
help="Add a macro definition"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--profile",
|
|
|
|
dest="profile",
|
|
|
|
action="append",
|
|
|
|
type=argparse_profile_filestring_type,
|
|
|
|
help=("Build profile to use. Can be either path to json"
|
|
|
|
"file or one of the default one ({})".format(
|
|
|
|
", ".join(list_profiles()))),
|
|
|
|
default=[]
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--update-packs",
|
|
|
|
dest="update_packs",
|
|
|
|
action="store_true",
|
|
|
|
default=False
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--app-config",
|
|
|
|
dest="app_config",
|
|
|
|
default=None
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--ignore",
|
|
|
|
dest="ignore",
|
|
|
|
type=argparse_many(str),
|
|
|
|
default=None,
|
|
|
|
help=("Comma separated list of patterns to add to mbedignore "
|
|
|
|
"(eg. ./main.cpp)")
|
|
|
|
)
|
2018-05-11 04:00:01 +00:00
|
|
|
|
2016-06-24 22:15:01 +00:00
|
|
|
options = parser.parse_args()
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2014-10-14 16:04:32 +00:00
|
|
|
# Print available tests in order and exit
|
|
|
|
if options.list_tests is True:
|
2018-07-03 19:03:28 +00:00
|
|
|
print('\n'.join(str(test) for test in sorted(TEST_MAP.values())))
|
2014-10-14 16:04:32 +00:00
|
|
|
sys.exit()
|
2014-10-16 10:45:46 +00:00
|
|
|
|
2014-12-30 18:26:43 +00:00
|
|
|
# Only prints matrix of supported IDEs
|
|
|
|
if options.supported_ides:
|
2017-03-22 19:29:44 +00:00
|
|
|
if options.supported_ides == "matrix":
|
|
|
|
print_large_string(mcu_ide_matrix())
|
|
|
|
elif options.supported_ides == "ides":
|
2018-02-21 15:25:59 +00:00
|
|
|
print(mcu_ide_list())
|
2014-12-30 18:26:43 +00:00
|
|
|
exit(0)
|
|
|
|
|
|
|
|
# Only prints matrix of supported IDEs
|
|
|
|
if options.supported_ides_html:
|
|
|
|
html = mcu_ide_matrix(verbose_html=True)
|
2018-07-03 19:03:28 +00:00
|
|
|
with open("./export/README.md", "w") as readme:
|
|
|
|
readme.write("Exporter IDE/Platform Support\n")
|
|
|
|
readme.write("-----------------------------------\n")
|
|
|
|
readme.write("\n")
|
|
|
|
readme.write(html)
|
2014-12-30 18:26:43 +00:00
|
|
|
exit(0)
|
|
|
|
|
2016-10-18 19:00:11 +00:00
|
|
|
if options.update_packs:
|
|
|
|
from tools.arm_pack_manager import Cache
|
|
|
|
cache = Cache(True, True)
|
2018-02-16 17:20:27 +00:00
|
|
|
cache.cache_everything()
|
2016-10-18 19:00:11 +00:00
|
|
|
|
2016-08-04 21:18:20 +00:00
|
|
|
# Target
|
|
|
|
if not options.mcu:
|
|
|
|
args_error(parser, "argument -m/--mcu is required")
|
|
|
|
|
|
|
|
# Toolchain
|
|
|
|
if not options.ide:
|
|
|
|
args_error(parser, "argument -i is required")
|
|
|
|
|
2017-01-05 19:54:04 +00:00
|
|
|
# Clean Export Directory
|
|
|
|
if options.clean:
|
|
|
|
if exists(EXPORT_DIR):
|
|
|
|
rmtree(EXPORT_DIR)
|
|
|
|
|
2017-07-07 19:45:58 +00:00
|
|
|
zip_proj = not bool(options.source_dir)
|
2017-01-05 19:54:04 +00:00
|
|
|
|
2018-04-30 19:42:33 +00:00
|
|
|
notify = TerminalNotifier()
|
|
|
|
|
2018-07-03 18:56:23 +00:00
|
|
|
ide = resolve_exporter_alias(options.ide)
|
|
|
|
exporter, toolchain_name = get_exporter_toolchain(ide)
|
2018-07-03 19:03:28 +00:00
|
|
|
profile = extract_profile(parser, options, toolchain_name, fallback="debug")
|
2017-07-07 19:45:58 +00:00
|
|
|
mcu = extract_mcus(parser, options)[0]
|
2018-07-03 19:03:28 +00:00
|
|
|
|
2017-07-07 19:45:58 +00:00
|
|
|
if not exporter.is_target_supported(mcu):
|
2018-07-03 18:56:23 +00:00
|
|
|
args_error(parser, "%s not supported by %s" % (mcu, ide))
|
2018-07-03 19:03:28 +00:00
|
|
|
|
|
|
|
if (options.program is None) and (not options.source_dir):
|
|
|
|
args_error(parser, "one of -p, -n, or --source is required")
|
|
|
|
|
2016-09-13 15:27:30 +00:00
|
|
|
if options.clean:
|
2018-02-21 15:26:14 +00:00
|
|
|
for cls in EXPORTERS.values():
|
|
|
|
try:
|
|
|
|
cls.clean(basename(abspath(options.source_dir[0])))
|
|
|
|
except (NotImplementedError, IOError, OSError):
|
|
|
|
pass
|
2018-02-23 15:57:02 +00:00
|
|
|
for f in list(EXPORTERS.values())[0].CLEAN_FILES:
|
2018-02-20 16:44:16 +00:00
|
|
|
try:
|
|
|
|
remove(f)
|
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
2017-08-23 13:31:07 +00:00
|
|
|
try:
|
2018-07-03 19:03:28 +00:00
|
|
|
export(
|
|
|
|
mcu,
|
|
|
|
ide,
|
|
|
|
build=options.build,
|
|
|
|
src=options.source_dir,
|
|
|
|
macros=options.macros,
|
|
|
|
project_id=options.program,
|
|
|
|
zip_proj=zip_proj,
|
|
|
|
build_profile=profile,
|
|
|
|
app_config=options.app_config,
|
|
|
|
export_path=options.build_dir,
|
|
|
|
notify=notify,
|
|
|
|
ignore=options.ignore
|
|
|
|
)
|
2017-08-23 13:31:07 +00:00
|
|
|
except NotSupportedException as exc:
|
2018-07-03 19:03:28 +00:00
|
|
|
print("[Not Supported] %s" % str(exc))
|
2014-11-11 16:04:59 +00:00
|
|
|
|
2016-07-20 19:43:09 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|