Skip to content

Commit 7e9d658

Browse files
author
Jamie Smith
authored
Allow Mbed config files to be json5 in addition to json (#194)
* Allow Mbed config files to be json5 in addition to json * Also handle custom_targets.json * Add missing "install python packages" step
1 parent 7c61bab commit 7e9d658

File tree

12 files changed

+83
-27
lines changed

12 files changed

+83
-27
lines changed

.github/workflows/basic_checks.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ jobs:
214214
name: Checkout repo
215215
uses: actions/checkout@v3
216216

217+
218+
- name: Install Python packages
219+
run: |
220+
python3 -m pip install -r tools/requirements.txt
221+
217222
-
218223
name: cmake build
219224
run: |

drivers/mbed_lib.json renamed to drivers/mbed_lib.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"name": "drivers",
33
"config": {
4+
// Note: These are called "uart-serial" because that was the old name
5+
// of BufferedSerial before it was officially merged
46
"uart-serial-txbuf-size": {
57
"help": "Default TX buffer size for a BufferedSerial instance (unit Bytes))",
68
"value": 256

targets/targets.json renamed to targets/targets.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5126,6 +5126,8 @@
51265126
"mbed_ram_start": "0x20000000",
51275127
"mbed_ram_size": "0x10000"
51285128
},
5129+
// Note: "MIMXRT105X" target is for the MIMXRT1051, MIMXRT1061=2, MIMXRT1061, and MIMXRT1062
5130+
// See here for details: https://github.com/mbed-ce/mbed-os/wiki/MCU-Info-Page:-MIMXRT105x-and-106x
51295131
"MIMXRT105X": {
51305132
"core": "Cortex-M7FD",
51315133
"supported_toolchains": [

tools/python/mbed_tools/build/_internal/config/assemble_build_config.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ def assemble_config(target_attributes: dict, search_paths: Iterable[Path], mbed_
3232
search_paths: Iterable of paths to search for mbed_lib.json files.
3333
mbed_app_file: The path to mbed_app.json. This can be None.
3434
"""
35-
mbed_lib_files = list(
36-
set(
37-
itertools.chain.from_iterable(
38-
find_files("mbed_lib.json", path.absolute().resolve()) for path in search_paths
39-
)
40-
)
41-
)
42-
return _assemble_config_from_sources(target_attributes, mbed_lib_files, mbed_app_file)
35+
mbed_lib_files: Set[Path] = set()
36+
37+
for path in search_paths:
38+
mbed_lib_files.update(find_files("mbed_lib.json", path.absolute().resolve()))
39+
mbed_lib_files.update(find_files("mbed_lib.json5", path.absolute().resolve()))
40+
41+
return _assemble_config_from_sources(target_attributes, list(mbed_lib_files), mbed_app_file)
4342

4443

4544
def _assemble_config_from_sources(

tools/python/mbed_tools/lib/json_helpers.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55
"""Helpers for json related functions."""
66
import json
7+
import json5
78
import logging
89

910
from pathlib import Path
@@ -14,9 +15,19 @@
1415

1516
def decode_json_file(path: Path) -> Any:
1617
"""Return the contents of json file."""
17-
try:
18-
logger.debug(f"Loading JSON file {path}")
19-
return json.loads(path.read_text())
20-
except json.JSONDecodeError:
21-
logger.error(f"Failed to decode JSON data in the file located at '{path}'")
22-
raise
18+
if path.suffix == '.json':
19+
try:
20+
logger.debug(f"Loading JSON file {path}")
21+
return json.loads(path.read_text())
22+
except json.JSONDecodeError:
23+
logger.error(f"Failed to decode JSON data in the file located at '{path}'")
24+
raise
25+
elif path.suffix == '.json5':
26+
try:
27+
logger.debug(f"Loading JSON file {path}")
28+
return json5.loads(path.read_text())
29+
except ValueError:
30+
logger.error(f"Failed to decode JSON data in the file located at '{path}'")
31+
raise
32+
else:
33+
raise ValueError(f"Unknown JSON file extension {path.suffix}")

tools/python/mbed_tools/project/_internal/project_data.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919
logger = logging.getLogger(__name__)
2020

2121
# Mbed program file names and constants.
22-
APP_CONFIG_FILE_NAME = "mbed_app.json"
22+
APP_CONFIG_FILE_NAME_JSON = "mbed_app.json"
23+
APP_CONFIG_FILE_NAME_JSON5 = "mbed_app.json5"
2324
BUILD_DIR = "cmake_build"
2425
CMAKELISTS_FILE_NAME = "CMakeLists.txt"
2526
MAIN_CPP_FILE_NAME = "main.cpp"
2627
MBED_OS_REFERENCE_FILE_NAME = "mbed-os.lib"
2728
MBED_OS_DIR_NAME = "mbed-os"
28-
TARGETS_JSON_FILE_PATH = Path("targets", "targets.json")
29+
TARGETS_JSON_FILE_PATH = Path("targets", "targets.json5")
2930
CUSTOM_TARGETS_JSON_FILE_NAME = "custom_targets.json"
31+
CUSTOM_TARGETS_JSON5_FILE_NAME = "custom_targets.json5"
3032

3133
# Information written to mbed-os.lib
3234
MBED_OS_REFERENCE_URL = "https://github.com/ARMmbed/mbed-os"
@@ -71,7 +73,7 @@ def from_new(cls, root_path: Path) -> "MbedProgramFiles":
7173
Raises:
7274
ValueError: A program .mbed or mbed-os.lib file already exists at this path.
7375
"""
74-
app_config = root_path / APP_CONFIG_FILE_NAME
76+
app_config = root_path / APP_CONFIG_FILE_NAME_JSON5
7577
mbed_os_ref = root_path / MBED_OS_REFERENCE_FILE_NAME
7678
cmakelists_file = root_path / CMAKELISTS_FILE_NAME
7779
main_cpp = root_path / MAIN_CPP_FILE_NAME
@@ -103,13 +105,21 @@ def from_existing(cls, root_path: Path, build_subdir: Path) -> "MbedProgramFiles
103105
root_path: The path containing the MbedProgramFiles.
104106
build_subdir: The subdirectory of BUILD_DIR to use for CMake build.
105107
"""
106-
app_config: Optional[Path]
107-
app_config = root_path / APP_CONFIG_FILE_NAME
108-
if not app_config.exists():
108+
app_config: Optional[Path] = None
109+
if (root_path / APP_CONFIG_FILE_NAME_JSON5).exists():
110+
app_config = root_path / APP_CONFIG_FILE_NAME_JSON5
111+
elif (root_path / APP_CONFIG_FILE_NAME_JSON).exists():
112+
app_config = root_path / APP_CONFIG_FILE_NAME_JSON
113+
else:
109114
logger.info("This program does not contain an mbed_app.json config file.")
110-
app_config = None
111115

112-
custom_targets_json = root_path / CUSTOM_TARGETS_JSON_FILE_NAME
116+
# If there's already a custom_targets.json5, use that.
117+
# Otherwise, assume json.
118+
if (root_path / CUSTOM_TARGETS_JSON5_FILE_NAME).exists():
119+
custom_targets_json = root_path / CUSTOM_TARGETS_JSON5_FILE_NAME
120+
else:
121+
custom_targets_json = root_path / CUSTOM_TARGETS_JSON_FILE_NAME
122+
113123
mbed_os_file = root_path / MBED_OS_REFERENCE_FILE_NAME
114124

115125
cmakelists_file = root_path / CMAKELISTS_FILE_NAME

tools/python/python_tests/mbed_tools/lib/test_json_helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ def test_invalid_json(tmp_path):
1515

1616
with pytest.raises(json.JSONDecodeError):
1717
decode_json_file(lib_json_path)
18+
19+
20+
def test_invalid_json5(tmp_path):
21+
lib_json_path = tmp_path / "mbed_lib.json5"
22+
lib_json_path.write_text("name")
23+
24+
with pytest.raises(ValueError):
25+
decode_json_file(lib_json_path)

tools/python/python_tests/mbed_tools/project/_internal/test_project_data.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
MbedProgramFiles,
1313
MbedOS,
1414
MAIN_CPP_FILE_NAME,
15+
APP_CONFIG_FILE_NAME_JSON
1516
)
1617
from python_tests.mbed_tools.project.factories import make_mbed_lib_reference, make_mbed_program_files, make_mbed_os_files
1718

@@ -59,6 +60,22 @@ def test_from_existing_finds_existing_program_data(self, tmp_path):
5960
assert program.mbed_os_ref.exists()
6061
assert program.cmakelists_file.exists()
6162

63+
def test_from_existing_finds_existing_program_data_app_json(self, tmp_path):
64+
"""
65+
Same as test_from_existing_finds_existing_program_data() except the app config
66+
is json instead of json5
67+
"""
68+
69+
root = pathlib.Path(tmp_path, "foo")
70+
make_mbed_program_files(root, APP_CONFIG_FILE_NAME_JSON)
71+
72+
program = MbedProgramFiles.from_existing(root, pathlib.Path("K64F", "develop", "GCC_ARM"))
73+
74+
assert program.app_config_file.exists()
75+
assert program.mbed_os_ref.exists()
76+
assert program.cmakelists_file.exists()
77+
78+
6279

6380
class TestMbedLibReference:
6481
def test_is_resolved_returns_true_if_source_code_dir_exists(self, tmp_path):
@@ -95,7 +112,7 @@ def test_from_existing_finds_existing_mbed_os_data(self, tmp_path):
95112

96113
mbed_os = MbedOS.from_existing(root_path)
97114

98-
assert mbed_os.targets_json_file == root_path / "targets" / "targets.json"
115+
assert mbed_os.targets_json_file == root_path / "targets" / "targets.json5"
99116

100117
def test_raises_if_files_missing(self, tmp_path):
101118
root_path = pathlib.Path(tmp_path, "my-version-of-mbed-os")

tools/python/python_tests/mbed_tools/project/factories.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from mbed_tools.project._internal.libraries import MbedLibReference
66
from mbed_tools.project._internal.project_data import (
77
CMAKELISTS_FILE_NAME,
8-
APP_CONFIG_FILE_NAME,
8+
APP_CONFIG_FILE_NAME_JSON5,
99
MBED_OS_REFERENCE_FILE_NAME,
1010
)
1111

1212

13-
def make_mbed_program_files(root, config_file_name=APP_CONFIG_FILE_NAME):
13+
def make_mbed_program_files(root, config_file_name=APP_CONFIG_FILE_NAME_JSON5):
1414
if not root.exists():
1515
root.mkdir()
1616

@@ -42,4 +42,4 @@ def make_mbed_os_files(root):
4242

4343
targets_dir = root / "targets"
4444
targets_dir.mkdir()
45-
(targets_dir / "targets.json").touch()
45+
(targets_dir / "targets.json5").touch()

tools/python/python_tests/mbed_tools/regression/test_configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_generate_config_called_with_correct_arguments(self):
5151
pathlib.Path(tmpDirPath / "mbed_app.json").write_text(mbed_app_json)
5252
pathlib.Path(tmpDirPath / "mbed-os").mkdir()
5353
pathlib.Path(tmpDirPath / "mbed-os" / "targets").mkdir()
54-
pathlib.Path(tmpDirPath / "mbed-os" / "targets" / "targets.json").write_text(target_json)
54+
pathlib.Path(tmpDirPath / "mbed-os" / "targets" / "targets.json5").write_text(target_json)
5555

5656
result = CliRunner().invoke(
5757
configure, ["-m", "Target", "-t", "gcc_arm", "-p", tmpDir], catch_exceptions=False

0 commit comments

Comments
 (0)