Skip to content

Commit 2a9207b

Browse files
hugueskambaevedon
authored andcommitted
Address comments on workaround for Mbed OS 2 CI build after Public (#11114)
* Modify compilation API to provide a list of paths to exclude from the build. * `_exclude_files_from_build` becomes a static method * Replace ternary expression with simple `if/else` statement * Make unit test case for dirs exclusion independent of system files
1 parent 3b23edb commit 2a9207b

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

tools/build_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ def build_mbed_libs(target, toolchain_name, clean=False, macros=None,
10751075
# Build Things
10761076
notify.info("Building library %s (%s, %s)" %
10771077
('MBED', target.name, toolchain_name))
1078-
objects = toolchain.compile_sources(
1078+
objects = toolchain.compile_legacy_sources(
10791079
mbed_resources, incdirs, exclude_paths
10801080
)
10811081
separate_objects = []

tools/test/build_api/build_api_test.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from mock import patch, MagicMock
2121
from tools.build_api import prepare_toolchain, build_project, build_library
2222
from tools.regions import merge_region_list
23-
from tools.resources import Resources
24-
from tools.toolchains import TOOLCHAINS
23+
from tools.resources import Resources, FileRef
24+
from tools.toolchains import TOOLCHAINS, mbedToolchain
2525
from tools.notifier.mock import MockNotifier
2626
from tools.config import Region, Config, ConfigException
2727
from tools.utils import ToolException
@@ -84,6 +84,53 @@ def test_always_complete_build(self, *_):
8484
assert any('percent' in msg and msg['percent'] == 100.0
8585
for msg in notify.messages if msg)
8686

87+
@patch('tools.toolchains.arm.ARM_STD.parse_dependencies',
88+
return_value=["foo"])
89+
@patch('tools.toolchains.mbedToolchain.need_update',
90+
side_effect=[i % 2 for i in range(3000)])
91+
@patch('os.mkdir')
92+
@patch('tools.toolchains.mbedToolchain.dump_build_profile')
93+
@patch('tools.utils.run_cmd', return_value=(b'', b'', 0))
94+
def test_compile_legacy_sources_always_complete_build(self, *_):
95+
"""Test that compile_legacy_sources() completes."""
96+
notify = MockNotifier()
97+
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
98+
self.toolchain_name, notify=notify)
99+
100+
res = Resources(MockNotifier()).scan_with_toolchain(
101+
self.src_paths, toolchain)
102+
103+
toolchain.RESPONSE_FILES=False
104+
toolchain.config_processed = True
105+
toolchain.config_file = "junk"
106+
toolchain.compile_legacy_sources(res)
107+
108+
assert any('percent' in msg and msg['percent'] == 100.0
109+
for msg in notify.messages if msg)
110+
111+
def test_dirs_exclusion_from_file_to_compile(self):
112+
"""Test that dirs can be excluded from the build."""
113+
files_to_compile = [
114+
FileRef(
115+
name="platform/TARGET_CORTEX_M/TOOLCHAIN_ARM/except.S",
116+
path="./platform/TARGET_CORTEX_M/TOOLCHAIN_ARM/except.S",
117+
),
118+
FileRef(
119+
name="rtos/TARGET_CORTEX/rtx5/RTX/Source/TOOLCHAIN_ARM/TARGET_RTOS_M4_M7/targets_irq_cm4f.S",
120+
path="./rtos/TARGET_CORTEX/rtx5/RTX/Source/TOOLCHAIN_ARM/TARGET_RTOS_M4_M7/targets_irq_cm4f.S",
121+
),
122+
]
123+
exclude_dirs = ["platform/", "drivers/", "targets/"]
124+
expected_compilation_queue = [
125+
FileRef(
126+
name="rtos/TARGET_CORTEX/rtx5/RTX/Source/TOOLCHAIN_ARM/TARGET_RTOS_M4_M7/targets_irq_cm4f.S",
127+
path="./rtos/TARGET_CORTEX/rtx5/RTX/Source/TOOLCHAIN_ARM/TARGET_RTOS_M4_M7/targets_irq_cm4f.S",
128+
)
129+
]
130+
compilation_queue = mbedToolchain._exclude_files_from_build(
131+
files_to_compile, exclude_dirs
132+
)
133+
self.assertEqual(compilation_queue, expected_compilation_queue)
87134

88135
@patch('tools.build_api.Config')
89136
def test_prepare_toolchain_app_config(self, mock_config_init):

tools/toolchains/mbed_toolchain.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -395,29 +395,49 @@ def get_arch_file(self, objects):
395395
cmd_list = (c.replace("\\", "/") for c in objects if c)
396396
return self.make_option_file(list(cmd_list), ".archive_files.txt")
397397

398+
def compile_legacy_sources(
399+
self, resources, inc_dirs=None, exclude_dirs=None
400+
):
401+
"""Compile source files with option to exclude some directories.
402+
403+
This method only exists to not break API compatibility and provide a
404+
way to exclude directories for Mbed OS 2 builds.
405+
"""
406+
return self._compile_sources(
407+
resources, inc_dirs=inc_dirs, exclude_dirs=exclude_dirs
408+
)
409+
398410
# THIS METHOD IS BEING CALLED BY THE MBED ONLINE BUILD SYSTEM
399411
# ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
400-
def compile_sources(self, resources, inc_dirs=None, exclude_paths=None):
412+
def compile_sources(self, resources, inc_dirs=None):
413+
"""Compile source files."""
414+
return self._compile_sources(resources, inc_dirs=inc_dirs)
415+
416+
@staticmethod
417+
def _exclude_files_from_build(files_to_compile, exclude_dirs):
418+
"""Remove files from dirs to be excluded for the build."""
419+
return [
420+
file_to_compile
421+
for file_to_compile in files_to_compile
422+
if all(
423+
exclude_dir not in file_to_compile.path
424+
for exclude_dir in exclude_dirs
425+
)
426+
]
427+
428+
def _compile_sources(self, resources, inc_dirs=None, exclude_dirs=None):
401429
# Web IDE progress bar for project build
402430
files_to_compile = (
403431
resources.get_file_refs(FileType.ASM_SRC) +
404432
resources.get_file_refs(FileType.C_SRC) +
405433
resources.get_file_refs(FileType.CPP_SRC)
406434
)
407-
# Remove files from paths to be excluded from the build and create
408-
# a compilation queue.
409-
compile_queue = (
410-
files_to_compile
411-
if not exclude_paths
412-
else [
413-
file_to_compile
414-
for exclude_path in exclude_paths
415-
for file_to_compile in files_to_compile
416-
if exclude_path not in file_to_compile.path
417-
]
418-
)
435+
if exclude_dirs:
436+
compilation_queue = self._exclude_files_from_build(files_to_compile, exclude_dirs)
437+
else:
438+
compilation_queue = files_to_compile
419439

420-
self.to_be_compiled = len(compile_queue)
440+
self.to_be_compiled = len(compilation_queue)
421441
self.compiled = 0
422442

423443
self.notify.cc_verbose("Macros: " + ' '.join([
@@ -447,8 +467,8 @@ def compile_sources(self, resources, inc_dirs=None, exclude_paths=None):
447467
self.dump_build_profile()
448468

449469
# Sort compile queue for consistency
450-
compile_queue.sort()
451-
for source in compile_queue:
470+
compilation_queue.sort()
471+
for source in compilation_queue:
452472
object = self.relative_object_path(self.build_dir, source)
453473

454474
# Queue mode (multiprocessing)

0 commit comments

Comments
 (0)