Skip to content

[tools] Delay validation of mcu's until after loading config #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@

from tools.toolchains import TOOLCHAINS
from tools.toolchains import mbedToolchain
from tools.targets import TARGET_NAMES, TARGET_MAP
from tools.targets import TARGET_NAMES, TARGET_MAP, Target
from tools.options import get_default_options_parser
from tools.build_api import build_library, build_mbed_libs, build_lib
from tools.build_api import mcu_toolchain_matrix
from tools.build_api import static_analysis_scan, static_analysis_scan_lib, static_analysis_scan_library
from tools.build_api import print_build_results
from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT
from utils import argparse_filestring_type
from utils import argparse_force_uppercase_type
from utils import argparse_many
from utils import run_type_after_parse
from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT, CLI_COLOR_MAP
from utils import argparse_filestring_type, argparse_dir_not_parent
from tools.config import Config

if __name__ == '__main__':
start = time()
Expand Down Expand Up @@ -156,13 +160,23 @@

options = parser.parse_args()

if options.source_dir:
config = Config.add_target_config(options.source_dir)
if "custom_targets" in config:
Target.add_py_targets(config["custom_targets"])

# Only prints matrix of supported toolchains
if options.supported_toolchains:
print mcu_toolchain_matrix(platform_filter=options.general_filter_regex)
exit(0)

# Get target list
targets = options.mcu if options.mcu else TARGET_NAMES
if options.mcu:
targets = run_type_after_parse(
argparse_many(argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU")),
parser, options.mcu, "-m")
else:
targets = TARGET_NAMES

# Get toolchains list
toolchains = options.tool if options.tool else TOOLCHAINS
Expand Down
35 changes: 20 additions & 15 deletions tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,30 +190,35 @@ class Config:
"UVISOR", "BLE", "CLIENT", "IPV4", "IPV6"
]

# The initialization arguments for Config are:
# target: the name of the mbed target used for this configuration instance
# top_level_dirs: a list of top level source directories (where mbed_abb_config.json could be found)
# __init__ will look for the application configuration file in top_level_dirs.
# If found once, it'll parse it and check if it has a custom_targets function.
# If it does, it'll update the list of targets if need.
# If found more than once, an exception is raised
# top_level_dirs can be None (in this case, mbed_app_config.json will not be searched)
def __init__(self, target, top_level_dirs = []):
@staticmethod
def add_target_config(top_level_dirs):
app_config_location = None
for s in (top_level_dirs or []):
full_path = os.path.join(s, self.__mbed_app_config_name)
full_path = os.path.join(s, Config.__mbed_app_config_name)
if os.path.isfile(full_path):
if app_config_location is not None:
raise ConfigException("Duplicate '%s' file in '%s' and '%s'" % (self.__mbed_app_config_name, app_config_location, full_path))
raise ConfigException("Duplicate '%s' file in '%s' and '%s'" % (Config.__mbed_app_config_name, app_config_location, full_path))
else:
app_config_location = full_path
self.app_config_data = json_file_to_dict(app_config_location) if app_config_location else {}
app_config_data = json_file_to_dict(app_config_location) if app_config_location else {}
# Check the keys in the application configuration data
unknown_keys = set(self.app_config_data.keys()) - self.__allowed_keys["application"]
unknown_keys = set(app_config_data.keys()) - Config.__allowed_keys["application"]
if unknown_keys:
raise ConfigException("Unknown key(s) '%s' in %s" % (",".join(unknown_keys), self.__mbed_app_config_name))
raise ConfigException("Unknown key(s) '%s' in %s" % (",".join(unknown_keys), Config.__mbed_app_config_name))
# Update the list of targets with the ones defined in the application config, if applicable
Target.add_py_targets(self.app_config_data.get("custom_targets", {}))
return app_config_data


# The initialization arguments for Config are:
# target: the name of the mbed target used for this configuration instance
# top_level_dirs: a list of top level source directories (where mbed_abb_config.json could be found)
# __init__ will look for the application configuration file in top_level_dirs.
# If found once, it'll parse it and check if it has a custom_targets function.
# If it does, it'll update the list of targets if need.
# If found more than once, an exception is raised
# top_level_dirs can be None (in this case, mbed_app_config.json will not be searched)
def __init__(self, target, top_level_dirs = []):
self.app_config_data = Config.add_target_config(top_level_dirs)
self.lib_config_data = {}
# Make sure that each config is processed only once
self.processed_configs = {}
Expand Down
16 changes: 14 additions & 2 deletions tools/get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
from tools.utils import args_error
from tools.options import get_default_options_parser
from tools.build_api import get_config
from tools.targets import TARGET_NAMES
from tools.targets import Target
from config import Config
from utils import argparse_filestring_type
from utils import argparse_force_uppercase_type
from utils import run_type_after_parse
try:
import tools.private_settings as ps
except:
Expand All @@ -46,10 +50,18 @@

options = parser.parse_args()

if options.source_dir:
config = Config.add_target_config(options.source_dir)
if "custom_targets" in config:
Target.add_py_targets(config["custom_targets"])

# Target
if options.mcu is None :
if options.mcu:
target = run_type_after_parse(
argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU"),
parser, options.mcu, "-m")
else:
args_error(parser, "[ERROR] You should specify an MCU")
target = options.mcu[0]

# Toolchain
if options.tool is None:
Expand Down
16 changes: 14 additions & 2 deletions tools/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@
from tools.tests import TEST_MBED_LIB
from tools.tests import test_known, test_name_known
from tools.targets import TARGET_MAP
from tools.targets import TARGET_NAMES
from tools.targets import Target
from tools.options import get_default_options_parser
from tools.build_api import build_project
from tools.build_api import mcu_toolchain_matrix
from utils import argparse_filestring_type
from utils import argparse_many
from utils import argparse_dir_not_parent
from utils import argparse_force_uppercase_type
from utils import run_type_after_parse
from argparse import ArgumentTypeError
from tools.toolchains import mbedToolchain
from tools.settings import CLI_COLOR_MAP
from tools.config import Config

if __name__ == '__main__':
# Parse Options
Expand Down Expand Up @@ -196,6 +201,9 @@

# force program to "0" if a source dir is specified
if options.source_dir is not None:
config = Config.add_target_config(options.source_dir)
if "custom_targets" in config:
Target.add_py_targets(config["custom_targets"])
p = 0
else:
# Program Number or name
Expand All @@ -206,9 +214,13 @@
p = [p]

# Target
if options.mcu is None :
# Get target list
if options.mcu:
mcu = run_type_after_parse(
argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU"),
parser, options.mcu, "-m")
else:
args_error(parser, "[ERROR] You should specify an MCU")
mcu = options.mcu[0]

# Toolchain
if options.tool is None:
Expand Down
3 changes: 1 addition & 2 deletions tools/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def get_default_options_parser(add_clean=True, add_options=True):

parser.add_argument("-m", "--mcu",
help="build for the given MCU (%s)" % ', '.join(targetnames),
metavar="MCU",
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")))
metavar="MCU")

parser.add_argument("-t", "--tool",
help="build using the given TOOLCHAIN (%s)" % ', '.join(toolchainlist),
Expand Down
17 changes: 14 additions & 3 deletions tools/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
from tools.export import export, EXPORTERS, mcu_ide_matrix
from tools.tests import TESTS, TEST_MAP
from tools.tests import test_known, test_name_known
from tools.targets import TARGET_NAMES
from tools.targets import TARGET_NAMES, Target
from tools.libraries import LIBRARIES
from utils import argparse_filestring_type, argparse_many
from utils import argparse_force_lowercase_type, argparse_force_uppercase_type, argparse_dir_not_parent
from utils import run_type_after_parse
from tools.config import Config
from project_api import setup_project, perform_export, print_results, get_lib_symbols


Expand All @@ -31,7 +33,6 @@
parser.add_argument("-m", "--mcu",
metavar="MCU",
default='LPC1768',
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")),
help="generate project for the given MCU (%s)"% ', '.join(targetnames))

parser.add_argument("-i",
Expand Down Expand Up @@ -99,6 +100,11 @@
print '\n'.join(map(str, sorted(TEST_MAP.values())))
sys.exit()

if options.source_dir:
config = Config.add_target_config(options.source_dir)
if "custom_targets" in config:
Target.add_py_targets(config["custom_targets"])

# Only prints matrix of supported IDEs
if options.supported_ides:
print mcu_ide_matrix()
Expand All @@ -120,6 +126,11 @@
raise
exit(0)

if options.mcu:
mcus = run_type_after_parse(
argparse_many(argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU")),
parser, options.mcu, "-m")

# Clean Export Directory
if options.clean:
if exists(EXPORT_DIR):
Expand All @@ -132,7 +143,7 @@
# source_dir = use relative paths, otherwise sources are copied
sources_relative = True if options.source_dir else False

for mcu in options.mcu:
for mcu in mcus:
# Program Number or name
p, src, ide = options.program, options.source_dir, options.ide
try:
Expand Down
19 changes: 18 additions & 1 deletion tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@
from tools.build_api import build_project, build_library
from tools.build_api import print_build_memory_usage_results
from tools.targets import TARGET_MAP
from tools.targets import TARGET_NAMES
from tools.targets import Target
from tools.utils import mkdir, ToolException, NotSupportedException
from tools.test_exporters import ReportExporter, ResultExporterType
from utils import argparse_filestring_type, argparse_lowercase_type, argparse_many
from utils import argparse_dir_not_parent
from utils import argparse_force_uppercase_type
from utils import run_type_after_parse
from utils import args_error
from tools.toolchains import mbedToolchain
from tools.settings import CLI_COLOR_MAP
from tools.config import Config

if __name__ == '__main__':
try:
Expand Down Expand Up @@ -96,6 +102,11 @@

options = parser.parse_args()

if options.source_dir:
config = Config.add_target_config(options.source_dir)
if "custom_targets" in config:
Target.add_py_targets(config["custom_targets"])

# Filter tests by path if specified
if options.paths:
all_paths = options.paths
Expand Down Expand Up @@ -152,7 +163,13 @@
base_source_paths = ['.']


target = options.mcu[0]
# Get target list
if options.mcu:
target = run_type_after_parse(
argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU"),
parser, options.mcu, "-m")
else:
args_error(parser, "[ERROR] You should specify an MCU")

build_report = {}
build_properties = {}
Expand Down
3 changes: 3 additions & 0 deletions tools/test/config_test/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def test_tree(full_name, name):
try:
# Use 'set_targets_json_location' to remove the previous custom targets from the target list
set_targets_json_location(Target._Target__targets_json_location)
config = Config.add_target_config([full_name])
if "custom_targets" in config:
Target.add_py_targets(config["custom_targets"])
cfg, macros, features = get_config(full_name, target, "GCC_ARM")
macros = Config.config_macros_to_macros(macros)
except ConfigException as e:
Expand Down
9 changes: 9 additions & 0 deletions tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ def argparse_filestring_type(string) :
else :
raise argparse.ArgumentTypeError("{0}"" does not exist in the filesystem.".format(string))

def run_type_after_parse(type_fn, parser, args, switch):
try:
if not isinstance(args, list):
return type_fn(args)
else:
return sum([type_fn(arg) for arg in args], [])
except argparse.ArgumentTypeError as e:
parser.error("argument {}: {}".format(switch, str(e)))

# render a list of strings as a in a bunch of columns
def columnate(strings, seperator=", ", chars=80):
col_width = max(len(s) for s in strings)
Expand Down