Skip to content

Commit 3a15898

Browse files
rwalton-armurutva
authored andcommitted
config: Support custom_targets.json
1 parent 68e0d83 commit 3a15898

File tree

12 files changed

+897
-1
lines changed

12 files changed

+897
-1
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ matrix:
162162
- mbedtools compile -t GCC_ARM -m K64F
163163
- ccache -s
164164

165+
- <<: *mbed-tools-test
166+
name: "Test custom target"
167+
env: NAME=test-import-cmd CACHE_NAME=test-import-cmd
168+
script:
169+
- mbedtools new custom-target-test
170+
- cd custom-target-test
171+
- cp $TRAVIS_BUILD_DIR/travis-ci/test-data/custom_targets.json .
172+
- mkdir TARGET_IMAGINARYBOARD && cp $TRAVIS_BUILD_DIR/travis-ci/test-data/TARGET_IMAGINARYBOARD/* TARGET_IMAGINARYBOARD
173+
- mbedtools compile -t GCC_ARM -m IMAGINARYBOARD
174+
- ccache -s
175+
165176
- <<: *mbed-tools-test
166177
name: "Test deploy command checks out tip of default branch"
167178
env: NAME=test-deploy-cmd CACHE_NAME=test-deploy-cmd

news/20201222090211.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support custom_targets.json.

src/mbed_tools/build/config.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
"""Parses the Mbed configuration system and generates a CMake config script."""
66
import pathlib
77

8+
from typing import Any
9+
810
from mbed_tools.lib.json_helpers import decode_json_file
911
from mbed_tools.project import MbedProgram
1012
from mbed_tools.targets import get_target_by_name
1113
from mbed_tools.build._internal.cmake_file import render_mbed_config_cmake_template
1214
from mbed_tools.build._internal.config.assemble_build_config import assemble_config
1315
from mbed_tools.build._internal.write_files import write_file
16+
from mbed_tools.build.exceptions import MbedBuildError
1417

1518

1619
def generate_config(target_name: str, toolchain: str, program: MbedProgram) -> pathlib.Path:
@@ -24,11 +27,29 @@ def generate_config(target_name: str, toolchain: str, program: MbedProgram) -> p
2427
Returns:
2528
Path to the generated config file.
2629
"""
27-
target_build_attributes = get_target_by_name(target_name, decode_json_file(program.mbed_os.targets_json_file))
30+
targets_data = _load_raw_targets_data(program)
31+
target_build_attributes = get_target_by_name(target_name, targets_data)
2832
config = assemble_config(target_build_attributes, program.root, program.files.app_config_file)
2933
cmake_file_contents = render_mbed_config_cmake_template(
3034
target_name=target_name, config=config, toolchain_name=toolchain,
3135
)
3236
cmake_config_file_path = program.files.cmake_config_file
3337
write_file(cmake_config_file_path, cmake_file_contents)
3438
return cmake_config_file_path
39+
40+
41+
def _load_raw_targets_data(program: MbedProgram) -> Any:
42+
targets_data = decode_json_file(program.mbed_os.targets_json_file)
43+
if program.files.custom_targets_json.exists():
44+
custom_targets_data = decode_json_file(program.files.custom_targets_json)
45+
for custom_target in custom_targets_data:
46+
if custom_target in targets_data:
47+
raise MbedBuildError(
48+
f"Error found in {program.files.custom_targets_json}.\n"
49+
f"A target with the name '{custom_target}' already exists in targets.json. "
50+
"Please give your custom target a unique name so it can be identified."
51+
)
52+
53+
targets_data.update(custom_targets_data)
54+
55+
return targets_data

src/mbed_tools/cli/build.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
@click.option(
3535
"--mbed-os-path", type=click.Path(), default=None, help="Path to local Mbed OS directory.",
3636
)
37+
@click.option(
38+
"--custom-targets-json", type=click.Path(), default=None, help="Path to custom_targets.json.",
39+
)
3740
@click.option(
3841
"-f", "--flash", is_flag=True, default=False, help="Flash the binary onto a device",
3942
)
@@ -60,6 +63,7 @@ def build(
6063
sterm: bool = False,
6164
baudrate: int = 9600,
6265
mbed_os_path: str = None,
66+
custom_targets_json: str = None,
6367
) -> None:
6468
"""Configure and build an Mbed project using CMake and Ninja.
6569
@@ -73,6 +77,7 @@ def build(
7377
program_path: Path to the Mbed project.
7478
mbed_os_path: the path to the local Mbed OS directory
7579
profile: The Mbed build profile (debug, develop or release).
80+
custom_targets_json: Path to custom_targets.json.
7681
toolchain: The toolchain to use for the build.
7782
mbed_target: The name of the Mbed target to build for.
7883
clean: Perform a clean build.
@@ -93,6 +98,9 @@ def build(
9398
if any([not mbed_config_file.exists(), not build_tree.exists(), mbed_target, toolchain]):
9499
click.echo("Configuring project and generating build system...")
95100
_validate_target_and_toolchain_args(mbed_target, toolchain)
101+
if custom_targets_json is not None:
102+
program.files.custom_targets_json = pathlib.Path(custom_targets_json)
103+
96104
generate_config(mbed_target.upper(), toolchain, program)
97105
generate_build_system(program.root, build_tree, profile)
98106

src/mbed_tools/project/_internal/project_data.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
MBED_OS_REFERENCE_FILE_NAME = "mbed-os.lib"
2828
MBED_OS_DIR_NAME = "mbed-os"
2929
TARGETS_JSON_FILE_PATH = Path("targets", "targets.json")
30+
CUSTOM_TARGETS_JSON_FILE_NAME = "custom_targets.json"
3031

3132
# Information written to mbed-os.lib
3233
MBED_OS_REFERENCE_URL = "https://github.com/ARMmbed/mbed-os"
@@ -59,6 +60,7 @@ class MbedProgramFiles:
5960
cmakelists_file: Path
6061
cmake_config_file: Path
6162
cmake_build_dir: Path
63+
custom_targets_json: Path
6264

6365
@classmethod
6466
def from_new(cls, root_path: Path) -> "MbedProgramFiles":
@@ -79,6 +81,7 @@ def from_new(cls, root_path: Path) -> "MbedProgramFiles":
7981
gitignore = root_path / ".gitignore"
8082
cmake_config = root_path / CMAKE_CONFIG_FILE_PATH
8183
cmake_build_dir = root_path / CMAKE_BUILD_DIR
84+
custom_targets_json = root_path / CUSTOM_TARGETS_JSON_FILE_NAME
8285

8386
if mbed_os_ref.exists():
8487
raise ValueError(f"Program already exists at path {root_path}.")
@@ -94,6 +97,7 @@ def from_new(cls, root_path: Path) -> "MbedProgramFiles":
9497
cmakelists_file=cmakelists_file,
9598
cmake_config_file=cmake_config,
9699
cmake_build_dir=cmake_build_dir,
100+
custom_targets_json=custom_targets_json,
97101
)
98102

99103
@classmethod
@@ -109,6 +113,7 @@ def from_existing(cls, root_path: Path) -> "MbedProgramFiles":
109113
logger.info("This program does not contain an mbed_app.json config file.")
110114
app_config = None
111115

116+
custom_targets_json = root_path / CUSTOM_TARGETS_JSON_FILE_NAME
112117
mbed_os_file = root_path / MBED_OS_REFERENCE_FILE_NAME
113118

114119
cmakelists_file = root_path / CMAKELISTS_FILE_NAME
@@ -121,6 +126,7 @@ def from_existing(cls, root_path: Path) -> "MbedProgramFiles":
121126
cmakelists_file=cmakelists_file,
122127
cmake_config_file=root_path / CMAKE_CONFIG_FILE_PATH,
123128
cmake_build_dir=root_path / CMAKE_BUILD_DIR,
129+
custom_targets_json=custom_targets_json,
124130
)
125131

126132

tests/build/test_generate_config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ def test_target_and_toolchain_collected(program):
9090
assert toolchain in config_text
9191

9292

93+
def test_custom_targets_data_found(program):
94+
program.files.custom_targets_json.write_text(json.dumps({"IMAGINARYBOARD": TARGET_DATA}))
95+
target = "IMAGINARYBOARD"
96+
toolchain = "GCC_ARM"
97+
98+
generate_config(target, toolchain, program)
99+
100+
config_text = program.files.cmake_config_file.read_text()
101+
102+
assert target in config_text
103+
104+
105+
def test_raises_error_when_attempting_to_customize_existing_target(program):
106+
program.files.custom_targets_json.write_text(json.dumps({TARGETS[0]: TARGET_DATA}))
107+
target = TARGETS[0]
108+
toolchain = "GCC_ARM"
109+
110+
with pytest.raises(ToolsError):
111+
generate_config(target, toolchain, program)
112+
113+
93114
def test_config_param_from_lib_processed_with_default_name_mangling(program):
94115
create_mbed_lib_json(
95116
program.mbed_os.root / "platform" / "mbed_lib.json",

tests/cli/test_build.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ def test_build_system_regenerated_when_mbed_os_path_passed(
144144
generate_config.assert_called_once_with(target.upper(), toolchain.upper(), program)
145145
generate_build_system.assert_called_once_with(program.root, program.files.cmake_build_dir, "develop")
146146

147+
def test_custom_targets_location_used_when_passed(
148+
self, generate_config, mbed_program, build_project, generate_build_system
149+
):
150+
program = mbed_program.from_existing()
151+
with mock_project_directory(program, mbed_config_exists=True, build_tree_exists=True):
152+
toolchain = "gcc_arm"
153+
target = "k64f"
154+
custom_targets_json_path = pathlib.Path("custom", "custom_targets.json")
155+
156+
runner = CliRunner()
157+
runner.invoke(build, ["-t", toolchain, "-m", target, "--custom-targets-json", custom_targets_json_path])
158+
159+
generate_config.assert_called_once_with(target.upper(), toolchain.upper(), program)
160+
self.assertEqual(program.files.custom_targets_json, custom_targets_json_path)
161+
147162
def test_build_folder_removed_when_clean_flag_passed(
148163
self, generate_config, mbed_program, build_project, generate_build_system
149164
):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_library(IMAGINARYBOARD INTERFACE)
5+
6+
target_sources(IMAGINARYBOARD
7+
INTERFACE
8+
PeripheralPins.c
9+
)
10+
11+
target_include_directories(IMAGINARYBOARD
12+
INTERFACE
13+
.
14+
)
15+
16+
target_link_libraries(IMAGINARYBOARD INTERFACE STM32L475xG)

0 commit comments

Comments
 (0)