Skip to content

Commit b2ced9f

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. The test cases for bin and hex image flashing have been updated accordingly.
1 parent 3ad5b82 commit b2ced9f

File tree

5 files changed

+63
-23
lines changed

5 files changed

+63
-23
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ def generate_config(target_name: str, toolchain: str, program: MbedProgram) -> p
4242
return cmake_config_file_path
4343

4444

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

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: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,53 +200,83 @@ def test_build_folder_removed_when_clean_flag_passed(
200200
self.assertFalse(program.files.cmake_build_dir.exists())
201201

202202
@mock.patch("mbed_tools.cli.build.flash_binary")
203+
@mock.patch("mbed_tools.cli.build.is_image_hex")
203204
@mock.patch("mbed_tools.cli.build.find_all_connected_devices")
204-
def test_build_flash_option(
205-
self, mock_find_devices, flash_binary, generate_config, mbed_program, build_project, generate_build_system
205+
def test_build_flash_option_bin_target(
206+
self,
207+
mock_find_devices,
208+
is_image_hex,
209+
flash_binary,
210+
generate_config,
211+
mbed_program,
212+
build_project,
213+
generate_build_system,
206214
):
207215
mock_find_devices.return_value = [mock.MagicMock()]
216+
is_image_hex.return_value = False
208217
runner = CliRunner()
209218
runner.invoke(build, ["--flash", *DEFAULT_BUILD_ARGS])
210-
flash_binary.assert_called_once()
219+
call_args = flash_binary.call_args
220+
args, kwargs = call_args
221+
flash_binary.assert_called_once_with(args[0], args[1], args[2], args[3], False)
211222

212223
@mock.patch("mbed_tools.cli.build.flash_binary")
224+
@mock.patch("mbed_tools.cli.build.is_image_hex")
213225
@mock.patch("mbed_tools.cli.build.find_all_connected_devices")
214-
def test_build_flash_and_hex_file_options(
215-
self, mock_find_devices, flash_binary, generate_config, mbed_program, build_project, generate_build_system
226+
def test_build_flash_options_hex_target(
227+
self,
228+
mock_find_devices,
229+
is_image_hex,
230+
flash_binary,
231+
generate_config,
232+
mbed_program,
233+
build_project,
234+
generate_build_system,
216235
):
217236
mock_find_devices.return_value = [mock.MagicMock()]
237+
is_image_hex.return_value = True
218238
runner = CliRunner()
219-
runner.invoke(build, ["--flash", "--hex-file", *DEFAULT_BUILD_ARGS])
239+
runner.invoke(build, ["--flash", *DEFAULT_BUILD_ARGS])
220240
call_args = flash_binary.call_args
221241
args, kwargs = call_args
222242
flash_binary.assert_called_once_with(args[0], args[1], args[2], args[3], True)
223243

224244
@mock.patch("mbed_tools.cli.build.flash_binary")
245+
@mock.patch("mbed_tools.cli.build.is_image_hex")
225246
@mock.patch("mbed_tools.cli.build.find_all_connected_devices")
226247
def test_build_flash_both_two_devices(
227-
self, mock_find_devices, flash_binary, generate_config, mbed_program, build_project, generate_build_system
248+
self,
249+
mock_find_devices,
250+
is_image_hex,
251+
flash_binary,
252+
generate_config,
253+
mbed_program,
254+
build_project,
255+
generate_build_system,
228256
):
229257
mock_find_devices.return_value = [mock.MagicMock(), mock.MagicMock()]
230258
runner = CliRunner()
231259
runner.invoke(build, ["--flash", *DEFAULT_BUILD_ARGS])
232260
self.assertEqual(flash_binary.call_count, 2)
233261

234262
@mock.patch("mbed_tools.cli.build.flash_binary")
263+
@mock.patch("mbed_tools.cli.build.is_image_hex")
235264
@mock.patch("mbed_tools.cli.build.find_connected_device")
236265
def test_build_flash_only_identifier_device(
237-
self, mock_find_device, flash_binary, generate_config, mbed_program, build_project, generate_build_system
266+
self,
267+
mock_find_device,
268+
is_image_hex,
269+
flash_binary,
270+
generate_config,
271+
mbed_program,
272+
build_project,
273+
generate_build_system,
238274
):
239275
mock_find_device.return_value = mock.MagicMock()
240276
runner = CliRunner()
241277
runner.invoke(build, ["--flash", "-m", "K64F[1]", "-t", "GCC_ARM"])
242278
self.assertEqual(flash_binary.call_count, 1)
243279

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-
250280
@mock.patch("mbed_tools.cli.build.terminal")
251281
@mock.patch("mbed_tools.cli.build.find_connected_device")
252282
def test_sterm_is_started_when_flag_passed(

0 commit comments

Comments
 (0)