Skip to content

Commit 9a0d686

Browse files
LDong-ArmPatater
authored andcommitted
configure/build: Support user-specified application configuration
Add an option `--app-config` to `configure` and `compile`, to allow users to specify an application configuration file. If unspecified, mbed-tools will use `mbed_app.json` if the latter exists. Tests are provided. Fixes #291
1 parent f925e2c commit 9a0d686

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

news/291.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add an option `--app-config` to `configure` and `build` commands to allow users to specify an application configuration file.

src/mbed_tools/cli/build.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
@click.option(
4141
"--custom-targets-json", type=click.Path(), default=None, help="Path to custom_targets.json.",
4242
)
43+
@click.option(
44+
"--app-config", type=click.Path(), default=None, help="Path to application configuration file.",
45+
)
4346
@click.option(
4447
"-f", "--flash", is_flag=True, default=False, help="Flash the binary onto a device",
4548
)
@@ -63,6 +66,7 @@ def build(
6366
baudrate: int,
6467
mbed_os_path: str,
6568
custom_targets_json: str,
69+
app_config: str,
6670
) -> None:
6771
"""Configure and build an Mbed project using CMake and Ninja.
6872
@@ -76,6 +80,7 @@ def build(
7680
custom_targets_json: Path to custom_targets.json.
7781
toolchain: The toolchain to use for the build.
7882
mbed_target: The name of the Mbed target to build for.
83+
app_config: the path to the application configuration file
7984
clean: Perform a clean build.
8085
flash: Flash the binary onto a device.
8186
sterm: Open a serial terminal to the connected target.
@@ -95,6 +100,8 @@ def build(
95100
click.echo("Configuring project and generating build system...")
96101
if custom_targets_json is not None:
97102
program.files.custom_targets_json = pathlib.Path(custom_targets_json)
103+
if app_config is not None:
104+
program.files.app_config_file = pathlib.Path(app_config)
98105
config, _ = generate_config(mbed_target.upper(), toolchain, program)
99106
generate_build_system(program.root, build_tree, profile)
100107

src/mbed_tools/cli/configure.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@
3636
@click.option(
3737
"--mbed-os-path", type=click.Path(), default=None, help="Path to local Mbed OS directory.",
3838
)
39+
@click.option(
40+
"--app-config", type=click.Path(), default=None, help="Path to application configuration file.",
41+
)
3942
def configure(
40-
toolchain: str, mbed_target: str, program_path: str, mbed_os_path: str, output_dir: str, custom_targets_json: str
43+
toolchain: str,
44+
mbed_target: str,
45+
program_path: str,
46+
mbed_os_path: str,
47+
output_dir: str,
48+
custom_targets_json: str,
49+
app_config: str
4150
) -> None:
4251
"""Exports a mbed_config.cmake file to build directory in the program root.
4352
@@ -55,6 +64,7 @@ def configure(
5564
program_path: the path to the local Mbed program
5665
mbed_os_path: the path to the local Mbed OS directory
5766
output_dir: the path to the output directory
67+
app_config: the path to the application configuration file
5868
"""
5969
cmake_build_subdir = pathlib.Path(mbed_target.upper(), "develop", toolchain.upper())
6070
if mbed_os_path is None:
@@ -65,6 +75,8 @@ def configure(
6575
program.files.custom_targets_json = pathlib.Path(custom_targets_json)
6676
if output_dir is not None:
6777
program.files.cmake_build_dir = pathlib.Path(output_dir)
78+
if app_config is not None:
79+
program.files.app_config_file = pathlib.Path(app_config)
6880

6981
mbed_target = mbed_target.upper()
7082
_, output_path = generate_config(mbed_target, toolchain, program)

tests/cli/test_build.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ def test_custom_targets_location_used_when_passed(
171171
generate_config.assert_called_once_with(target.upper(), toolchain.upper(), program)
172172
self.assertEqual(program.files.custom_targets_json, custom_targets_json_path)
173173

174+
def test_app_config_used_when_passed(
175+
self, generate_config, mbed_program, build_project, generate_build_system
176+
):
177+
program = mbed_program.from_existing()
178+
with mock_project_directory(program, mbed_config_exists=True, build_tree_exists=True):
179+
toolchain = "gcc_arm"
180+
target = "k64f"
181+
app_config_path = pathlib.Path("alternative_config.json")
182+
183+
runner = CliRunner()
184+
runner.invoke(build, ["-t", toolchain, "-m", target, "--app-config", app_config_path])
185+
186+
generate_config.assert_called_once_with(target.upper(), toolchain.upper(), program)
187+
self.assertEqual(program.files.app_config_file, app_config_path)
188+
174189
def test_build_folder_removed_when_clean_flag_passed(
175190
self, generate_config, mbed_program, build_project, generate_build_system
176191
):

tests/cli/test_configure.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ def test_custom_output_directory_used_when_passed(self, program, generate_config
4747

4848
generate_config.assert_called_once_with("K64F", "GCC_ARM", program)
4949
self.assertEqual(program.files.cmake_build_dir, output_dir)
50+
51+
@mock.patch("mbed_tools.cli.configure.generate_config")
52+
@mock.patch("mbed_tools.cli.configure.MbedProgram")
53+
def test_app_config_used_when_passed(self, program, generate_config):
54+
program = program.from_existing()
55+
app_config_path = pathlib.Path("alternative_config.json")
56+
CliRunner().invoke(
57+
configure, ["-t", "gcc_arm", "-m", "k64f", "--app-config", app_config_path]
58+
)
59+
60+
generate_config.assert_called_once_with("K64F", "GCC_ARM", program)
61+
self.assertEqual(program.files.app_config_file, app_config_path)

0 commit comments

Comments
 (0)