Skip to content

Commit 692d905

Browse files
authored
Merge pull request #4725 from theotherjimmy/extra_targets_exporters
Parse and use custom targets in exporters
2 parents c295187 + e6bdc32 commit 692d905

File tree

10 files changed

+71
-45
lines changed

10 files changed

+71
-45
lines changed

tools/export/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def mcu_ide_matrix(verbose_html=False):
103103
row = [target] # First column is platform name
104104
for ide in supported_ides:
105105
text = "-"
106-
if target in EXPORTERS[ide].TARGETS:
106+
if EXPORTERS[ide].is_target_supported(target):
107107
if verbose_html:
108108
text = "✓"
109109
else:

tools/export/cmsis/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ def cpu_cmsis(cpu):
103103
class CMSIS(Exporter):
104104
NAME = 'cmsis'
105105
TOOLCHAIN = 'ARM'
106-
TARGETS = [target for target, obj in TARGET_MAP.iteritems()
107-
if "ARM" in obj.supported_toolchains]
106+
107+
@classmethod
108+
def is_target_supported(cls, target_name):
109+
target = TARGET_MAP[target_name]
110+
return cls.TOOLCHAIN in target.supported_toolchains
108111

109112
def make_key(self, src):
110113
"""turn a source file into its group name"""

tools/export/embitz/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717
from os.path import splitext, basename
1818
from tools.targets import TARGET_MAP
19-
from tools.export.exporters import Exporter, filter_supported
19+
from tools.export.exporters import Exporter, apply_supported_whitelist
2020

2121

2222
POST_BINARY_WHITELIST = set([
@@ -30,9 +30,6 @@ class EmBitz(Exporter):
3030
NAME = 'EmBitz'
3131
TOOLCHAIN = 'GCC_ARM'
3232

33-
34-
TARGETS = filter_supported("GCC_ARM", POST_BINARY_WHITELIST)
35-
3633
MBED_CONFIG_HEADER_SUPPORTED = True
3734

3835
FILE_TYPES = {
@@ -42,6 +39,11 @@ class EmBitz(Exporter):
4239
'cpp_sources': 'cpp'
4340
}
4441

42+
@classmethod
43+
def is_target_supported(cls, target_name):
44+
target = TARGET_MAP[target_name]
45+
return apply_supported_whitelist(
46+
cls.TOOLCHAIN, POST_BINARY_WHITELIST, target)
4547

4648
@staticmethod
4749
def _remove_symbols(sym_list):

tools/export/exporters.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Exporter(object):
3636
TEMPLATE_DIR = dirname(__file__)
3737
DOT_IN_RELATIVE_PATH = False
3838
NAME = None
39-
TARGETS = None
39+
TARGETS = set()
4040
TOOLCHAIN = None
4141

4242

@@ -178,19 +178,33 @@ def generate(self):
178178
"""Generate an IDE/tool specific project file"""
179179
raise NotImplemented("Implement a generate function in Exporter child class")
180180

181+
@classmethod
182+
def is_target_supported(cls, target_name):
183+
"""Query support for a particular target
181184
182-
def filter_supported(compiler, whitelist):
185+
NOTE: override this method if your exporter does not provide a static list of targets
186+
187+
Positional Arguments:
188+
target_name - the name of the target.
189+
"""
190+
target = TARGET_MAP[target_name]
191+
return bool(set(target.resolution_order_names).intersection(set(cls.TARGETS))) \
192+
and cls.TOOLCHAIN in target.supported_toolchains
193+
194+
195+
@classmethod
196+
def all_supported_targets(cls):
197+
return [t for t in TARGET_MAP.keys() if cls.is_target_supported(t)]
198+
199+
200+
def apply_supported_whitelist(compiler, whitelist, target):
183201
"""Generate a list of supported targets for a given compiler and post-binary hook
184202
white-list."""
185-
def supported_p(obj):
186-
"""Internal inner function used for filtering"""
187-
if compiler not in obj.supported_toolchains:
188-
return False
189-
if not hasattr(obj, "post_binary_hook"):
190-
return True
191-
if obj.post_binary_hook['function'] in whitelist:
192-
return True
193-
else:
194-
return False
195-
return list(target for target, obj in TARGET_MAP.iteritems()
196-
if supported_p(obj))
203+
if compiler not in target.supported_toolchains:
204+
return False
205+
if not hasattr(target, "post_binary_hook"):
206+
return True
207+
if target.post_binary_hook['function'] in whitelist:
208+
return True
209+
else:
210+
return False

tools/export/gnuarmeclipse/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from random import randint
3434
from json import load
3535

36-
from tools.export.exporters import Exporter, filter_supported
36+
from tools.export.exporters import Exporter, apply_supported_whitelist
3737
from tools.options import list_profiles
3838
from tools.targets import TARGET_MAP
3939
from tools.utils import NotSupportedException
@@ -69,7 +69,11 @@ class GNUARMEclipse(Exporter):
6969
NAME = 'GNU ARM Eclipse'
7070
TOOLCHAIN = 'GCC_ARM'
7171

72-
TARGETS = filter_supported("GCC_ARM", POST_BINARY_WHITELIST)
72+
@classmethod
73+
def is_target_supported(cls, target_name):
74+
target = TARGET_MAP[target_name]
75+
return apply_supported_whitelist(
76+
cls.TOOLCHAIN, POST_BINARY_WHITELIST, target)
7377

7478
# override
7579
@property

tools/export/iar/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def _supported(mcu, iar_targets):
3030
with open(_iar_defs, 'r') as f:
3131
_GUI_OPTIONS = json.load(f)
3232

33-
_IAR_TARGETS = [target for target, obj in TARGET_MAP.iteritems() if
34-
_supported(obj, _GUI_OPTIONS.keys())]
35-
3633

3734
class IAR(Exporter):
3835
NAME = 'iar'
3936
TOOLCHAIN = 'IAR'
4037

41-
TARGETS = _IAR_TARGETS
38+
@classmethod
39+
def is_target_supported(cls, target_name):
40+
target = TARGET_MAP[target_name]
41+
return _supported(target, _GUI_OPTIONS.keys())
4242

4343

4444
def iar_groups(self, grouped_src):

tools/export/makefile/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from subprocess import check_output, CalledProcessError, Popen, PIPE
2222
import shutil
2323
from jinja2.exceptions import TemplateNotFound
24-
from tools.export.exporters import Exporter, filter_supported
24+
from tools.export.exporters import Exporter, apply_supported_whitelist
2525
from tools.utils import NotSupportedException
2626
from tools.targets import TARGET_MAP
2727

@@ -42,6 +42,12 @@ class Makefile(Exporter):
4242
"LPC4088Code.binary_hook"
4343
])
4444

45+
@classmethod
46+
def is_target_supported(cls, target_name):
47+
target = TARGET_MAP[target_name]
48+
return apply_supported_whitelist(
49+
cls.TOOLCHAIN, cls.POST_BINARY_WHITELIST, target)
50+
4551
def generate(self):
4652
"""Generate the makefile
4753
@@ -186,7 +192,6 @@ def build(project_name, log_name="build_log.txt", cleanup=True):
186192

187193
class GccArm(Makefile):
188194
"""GCC ARM specific makefile target"""
189-
TARGETS = filter_supported("GCC_ARM", Makefile.POST_BINARY_WHITELIST)
190195
NAME = 'Make-GCC-ARM'
191196
TEMPLATE = 'make-gcc-arm'
192197
TOOLCHAIN = "GCC_ARM"
@@ -204,7 +209,6 @@ def prepare_sys_lib(libname):
204209

205210
class Armc5(Makefile):
206211
"""ARM Compiler 5 specific makefile target"""
207-
TARGETS = filter_supported("ARM", Makefile.POST_BINARY_WHITELIST)
208212
NAME = 'Make-ARMc5'
209213
TEMPLATE = 'make-armc5'
210214
TOOLCHAIN = "ARM"
@@ -222,7 +226,6 @@ def prepare_sys_lib(libname):
222226

223227
class IAR(Makefile):
224228
"""IAR specific makefile target"""
225-
TARGETS = filter_supported("IAR", Makefile.POST_BINARY_WHITELIST)
226229
NAME = 'Make-IAR'
227230
TEMPLATE = 'make-iar'
228231
TOOLCHAIN = "IAR"

tools/export/qtcreator/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
"""
1717
from os.path import splitext, basename
1818
from tools.targets import TARGET_MAP
19-
from tools.export.exporters import Exporter, filter_supported
19+
from tools.export.exporters import Exporter
2020
from tools.export.makefile import GccArm
2121

2222
class QtCreator(GccArm):
2323
NAME = 'QtCreator'
24-
TOOLCHAIN = 'GCC_ARM'
25-
26-
TARGETS = filter_supported("GCC_ARM", set())
2724

2825
MBED_CONFIG_HEADER_SUPPORTED = True
2926

tools/export/uvision/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from tools.arm_pack_manager import Cache
1111
from tools.targets import TARGET_MAP
12-
from tools.export.exporters import Exporter, filter_supported
12+
from tools.export.exporters import Exporter, apply_supported_whitelist
1313
from tools.export.cmsis import DeviceCMSIS
1414

1515
cache_d = False
@@ -129,8 +129,13 @@ class Uvision(Exporter):
129129
"MTSCode.combine_bins_mts_dragonfly",
130130
"NCS36510TargetCode.ncs36510_addfib"
131131
])
132-
TARGETS = [tgt for tgt in filter_supported("ARM", POST_BINARY_WHITELIST)
133-
if DeviceCMSIS.check_supported(tgt)]
132+
133+
@classmethod
134+
def is_target_supported(cls, target_name):
135+
target = TARGET_MAP[target_name]
136+
return apply_supported_whitelist(
137+
cls.TOOLCHAIN, cls.POST_BINARY_WHITELIST, target) and\
138+
DeviceCMSIS.check_supported(target_name)
134139

135140
#File associations within .uvprojx file
136141
file_types = {'.cpp': 8, '.c': 1, '.s': 2,

tools/project.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def main():
107107

108108
parser.add_argument("-m", "--mcu",
109109
metavar="MCU",
110-
type=argparse_force_uppercase_type(targetnames, "MCU"),
110+
type=str.upper,
111111
help="generate project for the given MCU ({})".format(
112112
', '.join(targetnames)))
113113

@@ -235,19 +235,17 @@ def main():
235235
if exists(EXPORT_DIR):
236236
rmtree(EXPORT_DIR)
237237

238-
for mcu in options.mcu:
239-
zip_proj = not bool(options.source_dir)
238+
zip_proj = not bool(options.source_dir)
240239

241240
if (options.program is None) and (not options.source_dir):
242241
args_error(parser, "one of -p, -n, or --source is required")
243-
# Export to selected toolchain
244242
exporter, toolchain_name = get_exporter_toolchain(options.ide)
245-
if options.mcu not in exporter.TARGETS:
246-
args_error(parser, "%s not supported by %s"%(options.mcu,options.ide))
243+
mcu = extract_mcus(parser, options)[0]
244+
if not exporter.is_target_supported(mcu):
245+
args_error(parser, "%s not supported by %s"%(mcu,options.ide))
247246
profile = extract_profile(parser, options, toolchain_name, fallback="debug")
248247
if options.clean:
249248
rmtree(BUILD_DIR)
250-
mcu = extract_mcus(parser, options)[0]
251249
export(mcu, options.ide, build=options.build,
252250
src=options.source_dir, macros=options.macros,
253251
project_id=options.program, zip_proj=zip_proj,

0 commit comments

Comments
 (0)