Skip to content

Commit 1f73607

Browse files
committed
Add support for macros handling to mbed test
Pass macros only to `mbed-os/tools/test.py` and not to mbedgt to enable workflow where `mbed test ... -D SOME_MACRO` will both compile with the macro and also run mbed greentea without `no such option: -D` error. Add macro `MBED_TEST_MODE` to `mbed test` to allow the main application to exclude the main() using the macro. See https://github.com/ARMmbed/pelion-ready-example/blob/master/main.cpp#L18
1 parent 1370900 commit 1f73607

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

mbed/mbed.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,8 +1760,8 @@ def set_defaults(self, target=None, toolchain=None):
17601760
if toolchain and not self.get_cfg('TOOLCHAIN'):
17611761
self.set_cfg('TOOLCHAIN', toolchain)
17621762

1763-
def get_macros(self):
1764-
macros = []
1763+
def get_macros(self, macros=[]):
1764+
# backwards compatibility with old MACROS.txt file
17651765
if os.path.isfile('MACROS.txt'):
17661766
with open('MACROS.txt') as f:
17671767
macros = f.read().splitlines()
@@ -2613,6 +2613,7 @@ def _safe_append_profile_to_build_path(build_path, profile):
26132613
@subcommand('compile',
26142614
dict(name=['-t', '--toolchain'], help='Compile toolchain. Example: ARM, GCC_ARM, IAR'),
26152615
dict(name=['-m', '--target'], help='Compile target MCU. Example: K64F, NUCLEO_F401RE, NRF51822...'),
2616+
dict(name=['-D', '--macro'], action='append', help='Add a macro definition'),
26162617
dict(name=['--profile'], action='append', help='Path of a build profile configuration file (or name of Mbed OS profile). Default: develop'),
26172618
dict(name='--library', dest='compile_library', action='store_true', help='Compile the current program or library as a static library.'),
26182619
dict(name='--config', dest='compile_config', action='store_true', help='Show run-time compile configuration'),
@@ -2627,7 +2628,10 @@ def _safe_append_profile_to_build_path(build_path, profile):
26272628
dict(name='--app-config', dest="app_config", help="Path of an application configuration file. Default is to look for \"mbed_app.json\"."),
26282629
help='Compile code using the mbed build tools',
26292630
description="Compile this program using the mbed build tools.")
2630-
def compile_(toolchain=None, target=None, profile=False, compile_library=False, compile_config=False, config_prefix=None, source=False, build=False, clean=False, flash=False, sterm=False, artifact_name=None, supported=False, app_config=None):
2631+
def compile_(toolchain=None, target=None, macro=False, profile=False,
2632+
compile_library=False, compile_config=False, config_prefix=None,
2633+
source=False, build=False, clean=False, flash=False, sterm=False,
2634+
artifact_name=None, supported=False, app_config=None):
26312635
# Gather remaining arguments
26322636
args = remainder
26332637
# Find the root of the program
@@ -2656,7 +2660,7 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
26562660

26572661
target = program.get_target(target)
26582662
tchain = program.get_toolchain(toolchain)
2659-
macros = program.get_macros()
2663+
macros = program.get_macros(macro)
26602664
profile = program.get_profile(profile)
26612665

26622666
if compile_config:
@@ -2750,6 +2754,7 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
27502754
@subcommand('test',
27512755
dict(name=['-t', '--toolchain'], help='Compile toolchain. Example: ARM, GCC_ARM, IAR'),
27522756
dict(name=['-m', '--target'], help='Compile target MCU. Example: K64F, NUCLEO_F401RE, NRF51822...'),
2757+
dict(name=['-D', '--macro'], action='append', help='Add a macro definition'),
27532758
dict(name='--compile-list', dest='compile_list', action='store_true',
27542759
help='List all tests that can be built'),
27552760
dict(name='--run-list', dest='run_list', action='store_true', help='List all built tests that can be ran'),
@@ -2777,12 +2782,7 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
27772782
help="Run Icetea tests. If used without --greentea flag then run only icetea tests."),
27782783
help='Find, build and run tests',
27792784
description="Find, build, and run tests in a program and libraries")
2780-
def test_(toolchain=None, target=None, compile_list=False, run_list=False,
2781-
compile_only=False, run_only=False, tests_by_name=None, source=False,
2782-
profile=False, build=False, clean=False, test_spec=None,
2783-
app_config=None, test_config=None, coverage=None, make_program=None,
2784-
new=None, generator=None, regex=None, unittests=None,
2785-
build_data=None, greentea=None, icetea=None):
2785+
def test_(toolchain=None, target=None, macro=False, compile_list=False, run_list=False, compile_only=False, run_only=False, tests_by_name=None, source=False, profile=False, build=False, clean=False, test_spec=None, app_config=None, test_config=None, coverage=None, make_program=None, new=None, generator=None, regex=None, unittests=None, build_data=None, greentea=None, icetea=None):
27862786

27872787
# Default behaviour is to run only greentea tests
27882788
if not (greentea or icetea or unittests):
@@ -2808,7 +2808,8 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False,
28082808
orig_path = getcwd()
28092809
orig_target = target
28102810

2811-
macros = program.get_macros()
2811+
macros = program.get_macros(macro)
2812+
macros.append("MBED_TEST_MODE")
28122813
tools_dir = program.get_tools()
28132814
build_and_run_tests = not compile_list and not run_list and not compile_only and not run_only
28142815

0 commit comments

Comments
 (0)