Skip to content

Commit 001c2d3

Browse files
committed
Revisit Mbed 2 building
It's much cleaner now
1 parent c50d8a2 commit 001c2d3

File tree

3 files changed

+93
-74
lines changed

3 files changed

+93
-74
lines changed

tools/build_api.py

Lines changed: 87 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -871,13 +871,31 @@ def build_lib(lib_id, target, toolchain_name, clean=False, macros=None,
871871
# Let Exception propagate
872872
raise
873873

874-
# We do have unique legacy conventions about how we build and package the mbed
875-
# library
874+
875+
# A number of compiled files need to be copied as objects as the linker
876+
# will not search for weak symbol overrides in archives. These are:
877+
# - mbed_retarget.o: to make sure that the C standard lib symbols get
878+
# overridden
879+
# - mbed_board.o: `mbed_die` is weak
880+
# - mbed_overrides.o: this contains platform overrides of various
881+
# weak SDK functions
882+
# - mbed_main.o: this contains main redirection
883+
# - mbed_sdk_boot.o: this contains the main boot code in
884+
# - PeripheralPins.o: PinMap can be weak
885+
SEPARATE_NAMES = [
886+
'PeripheralPins.o',
887+
'mbed_retarget.o',
888+
'mbed_board.o',
889+
'mbed_overrides.o',
890+
'mbed_main.o',
891+
'mbed_sdk_boot.o',
892+
]
893+
894+
876895
def build_mbed_libs(target, toolchain_name, clean=False, macros=None,
877896
notify=None, jobs=1, report=None, properties=None,
878897
build_profile=None, ignore=None):
879-
""" Function returns True is library was built and false if building was
880-
skipped
898+
""" Build legacy libraries for a target and toolchain pair
881899
882900
Positional arguments:
883901
target - the MCU or board that the project will compile for
@@ -892,61 +910,64 @@ def build_mbed_libs(target, toolchain_name, clean=False, macros=None,
892910
properties - UUUUHHHHH beats me
893911
build_profile - a dict of flags that will be passed to the compiler
894912
ignore - list of paths to add to mbedignore
913+
914+
Return - True if target + toolchain built correctly, False if not supported
895915
"""
896916

897-
if report != None:
917+
if report is not None:
898918
start = time()
899919
id_name = "MBED"
900920
description = "mbed SDK"
901921
vendor_label = target.extra_labels[0]
902922
cur_result = None
903923
prep_report(report, target.name, toolchain_name, id_name)
904-
cur_result = create_result(target.name, toolchain_name, id_name,
905-
description)
906-
907-
if properties != None:
908-
prep_properties(properties, target.name, toolchain_name,
909-
vendor_label)
924+
cur_result = create_result(
925+
target.name, toolchain_name, id_name, description)
926+
if properties is not None:
927+
prep_properties(
928+
properties, target.name, toolchain_name, vendor_label)
910929

911-
# Check toolchain support
912930
if toolchain_name not in target.supported_toolchains:
913931
supported_toolchains_text = ", ".join(target.supported_toolchains)
914-
print('%s target is not yet supported by toolchain %s' %
915-
(target.name, toolchain_name))
916-
print('%s target supports %s toolchain%s' %
917-
(target.name, supported_toolchains_text, 's'
918-
if len(target.supported_toolchains) > 1 else ''))
919-
920-
if report != None:
932+
notify.info('The target {} does not support the toolchain {}'.format(
933+
target.name,
934+
toolchain_name
935+
))
936+
notify.info('{} supports {} toolchain{}'.format(
937+
target.name,
938+
supported_toolchains_text,
939+
's' if len(target.supported_toolchains) > 1 else ''
940+
))
941+
942+
if report is not None:
921943
cur_result["result"] = "SKIP"
922944
add_result_to_report(report, cur_result)
923945

924946
return False
925947

926948
try:
927949
# Source and Build Paths
928-
build_target = join(MBED_LIBRARIES, "TARGET_" + target.name)
929-
build_toolchain = join(MBED_LIBRARIES, mbed2_obj_path(target.name, toolchain_name))
950+
build_toolchain = join(
951+
MBED_LIBRARIES, mbed2_obj_path(target.name, toolchain_name))
930952
mkdir(build_toolchain)
931953

932-
# Toolchain
933-
tmp_path = join(MBED_LIBRARIES, '.temp', mbed2_obj_path(target.name, toolchain_name))
954+
tmp_path = join(
955+
MBED_LIBRARIES,
956+
'.temp',
957+
mbed2_obj_path(target.name, toolchain_name)
958+
)
934959
mkdir(tmp_path)
935960

961+
# Toolchain and config
936962
toolchain = prepare_toolchain(
937963
[""], tmp_path, target, toolchain_name, macros=macros, notify=notify,
938964
build_profile=build_profile, jobs=jobs, clean=clean, ignore=ignore)
939965

940-
# Take into account the library configuration (MBED_CONFIG_FILE)
941966
config = toolchain.config
942967
config.add_config_files([MBED_CONFIG_FILE])
943968
toolchain.set_config_data(toolchain.config.get_config_data())
944969

945-
# mbed
946-
notify.info("Building library %s (%s, %s)" %
947-
('MBED', target.name, toolchain_name))
948-
949-
# Common Headers
970+
# distribute header files
950971
toolchain.copy_files([MBED_HEADER], MBED_LIBRARIES)
951972
library_incdirs = [dirname(MBED_LIBRARIES), MBED_LIBRARIES]
952973

@@ -957,76 +978,70 @@ def build_mbed_libs(target, toolchain_name, clean=False, macros=None,
957978
toolchain.copy_files(resources.headers, dest)
958979
library_incdirs.append(dest)
959980

960-
cmsis_implementation = Resources(notify).scan_with_toolchain([MBED_CMSIS_PATH], toolchain)
961-
toolchain.copy_files(cmsis_implementation.headers, build_target)
962-
toolchain.copy_files(cmsis_implementation.linker_script, build_toolchain)
963-
toolchain.copy_files(cmsis_implementation.bin_files, build_toolchain)
964-
965-
hal_implementation = Resources(notify).scan_with_toolchain([MBED_TARGETS_PATH], toolchain)
966-
toolchain.copy_files(hal_implementation.headers +
967-
hal_implementation.hex_files +
968-
hal_implementation.libraries +
969-
[MBED_CONFIG_FILE],
970-
build_target, resources=hal_implementation)
971-
toolchain.copy_files(hal_implementation.linker_script, build_toolchain)
972-
toolchain.copy_files(hal_implementation.bin_files, build_toolchain)
973-
incdirs = Resources(notify).scan_with_toolchain([build_target], toolchain).inc_dirs
974-
objects = toolchain.compile_sources(cmsis_implementation + hal_implementation,
975-
library_incdirs + incdirs + [tmp_path])
976-
toolchain.copy_files(objects, build_toolchain)
977-
978-
# Common Sources
981+
# collect resources of the libs to compile
982+
cmsis_res = Resources(notify).scan_with_toolchain(
983+
[MBED_CMSIS_PATH], toolchain)
984+
hal_res = Resources(notify).scan_with_toolchain(
985+
[MBED_TARGETS_PATH], toolchain)
979986
mbed_resources = Resources(notify).scan_with_toolchain(
980987
[MBED_DRIVERS, MBED_PLATFORM, MBED_HAL], toolchain)
981988

982-
objects = toolchain.compile_sources(mbed_resources,
983-
library_incdirs + incdirs)
984-
985-
# A number of compiled files need to be copied as objects as opposed to
986-
# way the linker search for symbols in archives. These are:
987-
# - mbed_retarget.o: to make sure that the C standard lib symbols get
988-
# overridden
989-
# - mbed_board.o: mbed_die is weak
990-
# - mbed_overrides.o: this contains platform overrides of various
991-
# weak SDK functions
992-
# - mbed_main.o: this contains main redirection
993-
# - PeripheralPins.o: PinMap can be weak
994-
separate_names, separate_objects = ['PeripheralPins.o', 'mbed_retarget.o', 'mbed_board.o',
995-
'mbed_overrides.o', 'mbed_main.o', 'mbed_sdk_boot.o'], []
989+
incdirs = cmsis_res.inc_dirs + hal_res.inc_dirs + library_incdirs
990+
991+
# Build Things
992+
notify.info("Building library %s (%s, %s)" %
993+
('MBED', target.name, toolchain_name))
994+
objects = toolchain.compile_sources(mbed_resources, incdirs)
995+
separate_objects = []
996996

997997
for obj in objects:
998-
for name in separate_names:
998+
for name in SEPARATE_NAMES:
999999
if obj.endswith(name):
10001000
separate_objects.append(obj)
10011001

10021002
for obj in separate_objects:
10031003
objects.remove(obj)
10041004

10051005
toolchain.build_library(objects, build_toolchain, "mbed")
1006-
1007-
for obj in separate_objects:
1008-
toolchain.copy_files(obj, build_toolchain)
1009-
1010-
if report != None:
1006+
notify.info("Building library %s (%s, %s)" %
1007+
('CMSIS', target.name, toolchain_name))
1008+
cmsis_objects = toolchain.compile_sources(cmsis_res, incdirs + [tmp_path])
1009+
notify.info("Building library %s (%s, %s)" %
1010+
('HAL', target.name, toolchain_name))
1011+
hal_objects = toolchain.compile_sources(hal_res, incdirs + [tmp_path])
1012+
1013+
# Copy everything into the build directory
1014+
to_copy = sum([
1015+
hal_res.headers,
1016+
hal_res.hex_files,
1017+
hal_res.bin_files,
1018+
hal_res.libraries,
1019+
cmsis_res.headers,
1020+
cmsis_res.bin_files,
1021+
[cmsis_res.linker_script, hal_res.linker_script, MBED_CONFIG_FILE],
1022+
cmsis_objects,
1023+
hal_objects,
1024+
separate_objects,
1025+
], [])
1026+
toolchain.copy_files(to_copy, build_toolchain)
1027+
1028+
if report is not None:
10111029
end = time()
10121030
cur_result["elapsed_time"] = end - start
10131031
cur_result["result"] = "OK"
1014-
10151032
add_result_to_report(report, cur_result)
10161033

10171034
return True
10181035

10191036
except Exception as exc:
1020-
if report != None:
1037+
if report is not None:
10211038
end = time()
10221039
cur_result["result"] = "FAIL"
10231040
cur_result["elapsed_time"] = end - start
10241041

10251042
cur_result["output"] += str(exc)
10261043

10271044
add_result_to_report(report, cur_result)
1028-
1029-
# Let Exception propagate
10301045
raise
10311046

10321047

tools/export/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ def export_project(src_paths, export_path, target, ide, libraries_paths=None,
265265
name = basename(normpath(abspath(src_paths[0])))
266266

267267
resources = Resources(notify, collect_ignores=True)
268+
resources.add_toolchain_labels(toolchain)
268269
for loc, path in src_paths.items():
269-
resources.add_toolchain_labels(toolchain)
270270
for p in path:
271271
resources.add_directory(p, into_path=loc)
272272
toolchain.build_dir = export_path

tools/resources/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,11 @@ def lib_refs(self):
321321

322322
@property
323323
def linker_script(self):
324-
return self.get_file_names(FileType.LD_SCRIPT)[-1]
324+
options = self.get_file_names(FileType.LD_SCRIPT)
325+
if options:
326+
return options[-1]
327+
else:
328+
return None
325329

326330
@property
327331
def hex_files(self):

0 commit comments

Comments
 (0)