Skip to content

Commit 4c701a3

Browse files
committed
build: Automatically determine image format to flash
The default image format is bin, but targets can override it with `"OUTPUT_EXT": "hex"` in `targets.json`. This is already taken care of in the CMake configuration generated by mbed-tools, but user still need to pass `--hex-file` if they want to run `mbed-tools compile` with `--flash` to flash a hex image. This commit makes the image selection automatic based on `OUTPUT_EXT`. Note: `--hex-file` is now redundant and thus removed. It does not make sense to have it anymore without also offering `--bin-file` for completeness.
1 parent 2e82946 commit 4c701a3

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

news/20210602164800.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The `--flash` option of `mbed-tools compile` now automatically selects bin or hex image. The option `--hex-file` is removed.

src/mbed_tools/build/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
- Export of build instructions to third party command line tools and IDEs.
1212
"""
1313
from mbed_tools.build.build import build_project, generate_build_system
14-
from mbed_tools.build.config import generate_config
14+
from mbed_tools.build.config import generate_config, is_image_hex
1515
from mbed_tools.build.flash import flash_binary

src/mbed_tools/build/config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ def generate_config(target_name: str, toolchain: str, program: MbedProgram) -> p
4141
write_file(cmake_config_file_path, cmake_file_contents)
4242
return cmake_config_file_path
4343

44+
def is_image_hex(target_name: str, program: MbedProgram) -> bool:
45+
"""Check if a target's image format is hex instead of bin.
46+
47+
Args:
48+
target_name: Name of the target to check image format for.
49+
program: The MbedProgram to extract targets data from.
50+
51+
Return:
52+
True if the target's "OUTPUT_EXT" is "hex" in targets.json.
53+
"""
54+
targets_data = _load_raw_targets_data(program)
55+
target_build_attributes = get_target_by_name(target_name, targets_data)
56+
return "OUTPUT_EXT" in target_build_attributes and target_build_attributes["OUTPUT_EXT"] == "hex"
4457

4558
def _load_raw_targets_data(program: MbedProgram) -> Any:
4659
targets_data = decode_json_file(program.mbed_os.targets_json_file)

src/mbed_tools/cli/build.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import click
1313

14-
from mbed_tools.build import build_project, generate_build_system, generate_config, flash_binary
14+
from mbed_tools.build import build_project, generate_build_system, generate_config, is_image_hex, flash_binary
1515
from mbed_tools.devices import find_connected_device, find_all_connected_devices
1616
from mbed_tools.project import MbedProgram
1717
from mbed_tools.sterm import terminal
@@ -42,9 +42,6 @@
4242
@click.option(
4343
"-f", "--flash", is_flag=True, default=False, help="Flash the binary onto a device",
4444
)
45-
@click.option(
46-
"--hex-file", is_flag=True, default=False, help="Use hex file, this option should be used with '-f/--flash' option",
47-
)
4845
@click.option(
4946
"-s", "--sterm", is_flag=True, default=False, help="Launch a serial terminal to the device.",
5047
)
@@ -61,7 +58,6 @@ def build(
6158
mbed_target: str = "",
6259
clean: bool = False,
6360
flash: bool = False,
64-
hex_file: bool = False,
6561
sterm: bool = False,
6662
baudrate: int = 9600,
6763
mbed_os_path: str = None,
@@ -81,7 +77,6 @@ def build(
8177
mbed_target: The name of the Mbed target to build for.
8278
clean: Perform a clean build.
8379
flash: Flash the binary onto a device.
84-
hex_file: Use hex file, this option should be used with '-f/--flash' option.
8580
sterm: Open a serial terminal to the connected target.
8681
baudrate: Change the serial baud rate (ignored unless --sterm is also given).
8782
"""
@@ -114,10 +109,9 @@ def build(
114109

115110
if flash:
116111
for dev in devices:
112+
hex_file = is_image_hex(mbed_target.upper(), program)
117113
flashed_path = flash_binary(dev.mount_points[0].resolve(), program.root, build_tree, mbed_target, hex_file)
118114
click.echo(f"Copied {str(flashed_path.resolve())} to {len(devices)} device(s).")
119-
elif hex_file:
120-
click.echo("'--hex-file' option should be used with '-f/--flash' option")
121115

122116
if sterm:
123117
dev = devices[0]

tests/cli/test_build.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_build_flash_and_hex_file_options(
216216
):
217217
mock_find_devices.return_value = [mock.MagicMock()]
218218
runner = CliRunner()
219-
runner.invoke(build, ["--flash", "--hex-file", *DEFAULT_BUILD_ARGS])
219+
runner.invoke(build, ["--flash", *DEFAULT_BUILD_ARGS])
220220
call_args = flash_binary.call_args
221221
args, kwargs = call_args
222222
flash_binary.assert_called_once_with(args[0], args[1], args[2], args[3], True)
@@ -241,12 +241,6 @@ def test_build_flash_only_identifier_device(
241241
runner.invoke(build, ["--flash", "-m", "K64F[1]", "-t", "GCC_ARM"])
242242
self.assertEqual(flash_binary.call_count, 1)
243243

244-
def test_build_only_hex_file_option(self, generate_config, mbed_program, build_project, generate_build_system):
245-
runner = CliRunner()
246-
result = runner.invoke(build, ["--hex-file", *DEFAULT_BUILD_ARGS])
247-
248-
self.assertRegex(result.output, "-f/--flash")
249-
250244
@mock.patch("mbed_tools.cli.build.terminal")
251245
@mock.patch("mbed_tools.cli.build.find_connected_device")
252246
def test_sterm_is_started_when_flag_passed(

0 commit comments

Comments
 (0)