Skip to content

Commit 704ffd7

Browse files
authored
Merge pull request #4331 from theotherjimmy/depend-on-profile
Have objects depend on their respective compiler invocations
2 parents ede7762 + 7b04a52 commit 704ffd7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

tools/test/build_api/build_api_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def tearDown(self):
5555
side_effect=[i % 2 for i in range(3000)])
5656
@patch('os.mkdir')
5757
@patch('tools.toolchains.exists', return_value=True)
58+
@patch('tools.toolchains.mbedToolchain.dump_build_profile')
5859
@patch('tools.utils.run_cmd', return_value=("", "", 0))
5960
def test_always_complete_build(self, *_):
6061
with MagicMock() as notify:

tools/toolchains/__init__.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ class mbedToolchain:
252252

253253
MBED_CONFIG_FILE_NAME="mbed_config.h"
254254

255+
PROFILE_FILE_NAME = ".profile"
256+
255257
__metaclass__ = ABCMeta
256258

257259
profile_template = {'common':[], 'c':[], 'cxx':[], 'asm':[], 'ld':[]}
@@ -798,6 +800,7 @@ def compile_sources(self, resources, inc_dirs=None):
798800

799801
# Generate configuration header (this will update self.build_all if needed)
800802
self.get_config_header()
803+
self.dump_build_profile()
801804

802805
# Sort compile queue for consistency
803806
files_to_compile.sort()
@@ -911,13 +914,19 @@ def compile_command(self, source, object, includes):
911914
deps = []
912915
config_file = ([self.config.app_config_location]
913916
if self.config.app_config_location else [])
914-
if len(deps) == 0 or self.need_update(object, deps + config_file):
917+
deps.append(config_file)
918+
if ext == '.cpp' or self.COMPILE_C_AS_CPP:
919+
deps.append(join(self.build_dir, self.PROFILE_FILE_NAME + "-cxx"))
920+
else:
921+
deps.append(join(self.build_dir, self.PROFILE_FILE_NAME + "-c"))
922+
if len(deps) == 0 or self.need_update(object, deps):
915923
if ext == '.cpp' or self.COMPILE_C_AS_CPP:
916924
return self.compile_cpp(source, object, includes)
917925
else:
918926
return self.compile_c(source, object, includes)
919927
elif ext == '.s':
920928
deps = [source]
929+
deps.append(join(self.build_dir, self.PROFILE_FILE_NAME + "-asm"))
921930
if self.need_update(object, deps):
922931
return self.assemble(source, object, includes)
923932
else:
@@ -1012,8 +1021,9 @@ def link_program(self, r, tmp_path, name):
10121021
r.objects = sorted(set(r.objects))
10131022
config_file = ([self.config.app_config_location]
10141023
if self.config.app_config_location else [])
1015-
if self.need_update(elf, r.objects + r.libraries + [r.linker_script] +
1016-
config_file):
1024+
dependencies = r.objects + r.libraries + [r.linker_script, config_file]
1025+
dependencies.append(join(self.build_dir, self.PROFILE_FILE_NAME + "-ld"))
1026+
if self.need_update(elf, dependencies):
10171027
needed_update = True
10181028
self.progress("link", name)
10191029
self.link(elf, r.objects, r.libraries, r.lib_dirs, r.linker_script)
@@ -1161,6 +1171,22 @@ def get_config_header(self):
11611171
self.config_processed = True
11621172
return self.config_file
11631173

1174+
def dump_build_profile(self):
1175+
"""Dump the current build profile and macros into the `.profile` file
1176+
in the build directory"""
1177+
for key in ["cxx", "c", "asm", "ld"]:
1178+
to_dump = (str(self.flags[key]) + str(sorted(self.macros)))
1179+
if key in ["cxx", "c"]:
1180+
to_dump += str(self.flags['common'])
1181+
where = join(self.build_dir, self.PROFILE_FILE_NAME + "-" + key)
1182+
self._overwrite_when_not_equal(where, to_dump)
1183+
1184+
@staticmethod
1185+
def _overwrite_when_not_equal(filename, content):
1186+
if not exists(filename) or content != open(filename).read():
1187+
with open(filename, "wb") as out:
1188+
out.write(content)
1189+
11641190
@staticmethod
11651191
def generic_check_executable(tool_key, executable_name, levels_up,
11661192
nested_dir=None):

0 commit comments

Comments
 (0)