Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Mbed config files to be json5 in addition to json #194

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/basic_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ jobs:
name: Checkout repo
uses: actions/checkout@v3


- name: Install Python packages
run: |
python3 -m pip install -r tools/requirements.txt

-
name: cmake build
run: |
Expand Down
2 changes: 2 additions & 0 deletions drivers/mbed_lib.json → drivers/mbed_lib.json5
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"name": "drivers",
"config": {
// Note: These are called "uart-serial" because that was the old name
// of BufferedSerial before it was officially merged
"uart-serial-txbuf-size": {
"help": "Default TX buffer size for a BufferedSerial instance (unit Bytes))",
"value": 256
Expand Down
2 changes: 2 additions & 0 deletions targets/targets.json → targets/targets.json5
Original file line number Diff line number Diff line change
Expand Up @@ -5126,6 +5126,8 @@
"mbed_ram_start": "0x20000000",
"mbed_ram_size": "0x10000"
},
// Note: "MIMXRT105X" target is for the MIMXRT1051, MIMXRT1061=2, MIMXRT1061, and MIMXRT1062
// See here for details: https://github.com/mbed-ce/mbed-os/wiki/MCU-Info-Page:-MIMXRT105x-and-106x
"MIMXRT105X": {
"core": "Cortex-M7FD",
"supported_toolchains": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ def assemble_config(target_attributes: dict, search_paths: Iterable[Path], mbed_
search_paths: Iterable of paths to search for mbed_lib.json files.
mbed_app_file: The path to mbed_app.json. This can be None.
"""
mbed_lib_files = list(
set(
itertools.chain.from_iterable(
find_files("mbed_lib.json", path.absolute().resolve()) for path in search_paths
)
)
)
return _assemble_config_from_sources(target_attributes, mbed_lib_files, mbed_app_file)
mbed_lib_files: Set[Path] = set()

for path in search_paths:
mbed_lib_files.update(find_files("mbed_lib.json", path.absolute().resolve()))
mbed_lib_files.update(find_files("mbed_lib.json5", path.absolute().resolve()))
multiplemonomials marked this conversation as resolved.
Show resolved Hide resolved

return _assemble_config_from_sources(target_attributes, list(mbed_lib_files), mbed_app_file)


def _assemble_config_from_sources(
Expand Down
23 changes: 17 additions & 6 deletions tools/python/mbed_tools/lib/json_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
"""Helpers for json related functions."""
import json
import json5
import logging

from pathlib import Path
Expand All @@ -14,9 +15,19 @@

def decode_json_file(path: Path) -> Any:
"""Return the contents of json file."""
try:
logger.debug(f"Loading JSON file {path}")
return json.loads(path.read_text())
except json.JSONDecodeError:
logger.error(f"Failed to decode JSON data in the file located at '{path}'")
raise
if path.suffix == '.json':
try:
logger.debug(f"Loading JSON file {path}")
return json.loads(path.read_text())
except json.JSONDecodeError:
logger.error(f"Failed to decode JSON data in the file located at '{path}'")
raise
elif path.suffix == '.json5':
try:
logger.debug(f"Loading JSON file {path}")
return json5.loads(path.read_text())
except ValueError:
logger.error(f"Failed to decode JSON data in the file located at '{path}'")
raise
else:
raise ValueError(f"Unknown JSON file extension {path.suffix}")
26 changes: 18 additions & 8 deletions tools/python/mbed_tools/project/_internal/project_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
logger = logging.getLogger(__name__)

# Mbed program file names and constants.
APP_CONFIG_FILE_NAME = "mbed_app.json"
APP_CONFIG_FILE_NAME_JSON = "mbed_app.json"
APP_CONFIG_FILE_NAME_JSON5 = "mbed_app.json5"
BUILD_DIR = "cmake_build"
CMAKELISTS_FILE_NAME = "CMakeLists.txt"
MAIN_CPP_FILE_NAME = "main.cpp"
MBED_OS_REFERENCE_FILE_NAME = "mbed-os.lib"
MBED_OS_DIR_NAME = "mbed-os"
TARGETS_JSON_FILE_PATH = Path("targets", "targets.json")
TARGETS_JSON_FILE_PATH = Path("targets", "targets.json5")
CUSTOM_TARGETS_JSON_FILE_NAME = "custom_targets.json"
CUSTOM_TARGETS_JSON5_FILE_NAME = "custom_targets.json5"

# Information written to mbed-os.lib
MBED_OS_REFERENCE_URL = "https://github.com/ARMmbed/mbed-os"
Expand Down Expand Up @@ -71,7 +73,7 @@ def from_new(cls, root_path: Path) -> "MbedProgramFiles":
Raises:
ValueError: A program .mbed or mbed-os.lib file already exists at this path.
"""
app_config = root_path / APP_CONFIG_FILE_NAME
app_config = root_path / APP_CONFIG_FILE_NAME_JSON5
mbed_os_ref = root_path / MBED_OS_REFERENCE_FILE_NAME
cmakelists_file = root_path / CMAKELISTS_FILE_NAME
main_cpp = root_path / MAIN_CPP_FILE_NAME
Expand Down Expand Up @@ -103,13 +105,21 @@ def from_existing(cls, root_path: Path, build_subdir: Path) -> "MbedProgramFiles
root_path: The path containing the MbedProgramFiles.
build_subdir: The subdirectory of BUILD_DIR to use for CMake build.
"""
app_config: Optional[Path]
app_config = root_path / APP_CONFIG_FILE_NAME
if not app_config.exists():
app_config: Optional[Path] = None
if (root_path / APP_CONFIG_FILE_NAME_JSON5).exists():
app_config = root_path / APP_CONFIG_FILE_NAME_JSON5
elif (root_path / APP_CONFIG_FILE_NAME_JSON).exists():
app_config = root_path / APP_CONFIG_FILE_NAME_JSON
else:
logger.info("This program does not contain an mbed_app.json config file.")
app_config = None

custom_targets_json = root_path / CUSTOM_TARGETS_JSON_FILE_NAME
# If there's already a custom_targets.json5, use that.
# Otherwise, assume json.
if (root_path / CUSTOM_TARGETS_JSON5_FILE_NAME).exists():
custom_targets_json = root_path / CUSTOM_TARGETS_JSON5_FILE_NAME
else:
custom_targets_json = root_path / CUSTOM_TARGETS_JSON_FILE_NAME

mbed_os_file = root_path / MBED_OS_REFERENCE_FILE_NAME

cmakelists_file = root_path / CMAKELISTS_FILE_NAME
Expand Down
8 changes: 8 additions & 0 deletions tools/python/python_tests/mbed_tools/lib/test_json_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ def test_invalid_json(tmp_path):

with pytest.raises(json.JSONDecodeError):
decode_json_file(lib_json_path)


def test_invalid_json5(tmp_path):
lib_json_path = tmp_path / "mbed_lib.json5"
lib_json_path.write_text("name")

with pytest.raises(ValueError):
decode_json_file(lib_json_path)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MbedProgramFiles,
MbedOS,
MAIN_CPP_FILE_NAME,
APP_CONFIG_FILE_NAME_JSON
)
from python_tests.mbed_tools.project.factories import make_mbed_lib_reference, make_mbed_program_files, make_mbed_os_files

Expand Down Expand Up @@ -59,6 +60,22 @@ def test_from_existing_finds_existing_program_data(self, tmp_path):
assert program.mbed_os_ref.exists()
assert program.cmakelists_file.exists()

def test_from_existing_finds_existing_program_data_app_json(self, tmp_path):
"""
Same as test_from_existing_finds_existing_program_data() except the app config
is json instead of json5
"""

root = pathlib.Path(tmp_path, "foo")
make_mbed_program_files(root, APP_CONFIG_FILE_NAME_JSON)

program = MbedProgramFiles.from_existing(root, pathlib.Path("K64F", "develop", "GCC_ARM"))

assert program.app_config_file.exists()
assert program.mbed_os_ref.exists()
assert program.cmakelists_file.exists()



class TestMbedLibReference:
def test_is_resolved_returns_true_if_source_code_dir_exists(self, tmp_path):
Expand Down Expand Up @@ -95,7 +112,7 @@ def test_from_existing_finds_existing_mbed_os_data(self, tmp_path):

mbed_os = MbedOS.from_existing(root_path)

assert mbed_os.targets_json_file == root_path / "targets" / "targets.json"
assert mbed_os.targets_json_file == root_path / "targets" / "targets.json5"

def test_raises_if_files_missing(self, tmp_path):
root_path = pathlib.Path(tmp_path, "my-version-of-mbed-os")
Expand Down
6 changes: 3 additions & 3 deletions tools/python/python_tests/mbed_tools/project/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from mbed_tools.project._internal.libraries import MbedLibReference
from mbed_tools.project._internal.project_data import (
CMAKELISTS_FILE_NAME,
APP_CONFIG_FILE_NAME,
APP_CONFIG_FILE_NAME_JSON5,
MBED_OS_REFERENCE_FILE_NAME,
)


def make_mbed_program_files(root, config_file_name=APP_CONFIG_FILE_NAME):
def make_mbed_program_files(root, config_file_name=APP_CONFIG_FILE_NAME_JSON5):
if not root.exists():
root.mkdir()

Expand Down Expand Up @@ -42,4 +42,4 @@ def make_mbed_os_files(root):

targets_dir = root / "targets"
targets_dir.mkdir()
(targets_dir / "targets.json").touch()
(targets_dir / "targets.json5").touch()
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_generate_config_called_with_correct_arguments(self):
pathlib.Path(tmpDirPath / "mbed_app.json").write_text(mbed_app_json)
pathlib.Path(tmpDirPath / "mbed-os").mkdir()
pathlib.Path(tmpDirPath / "mbed-os" / "targets").mkdir()
pathlib.Path(tmpDirPath / "mbed-os" / "targets" / "targets.json").write_text(target_json)
pathlib.Path(tmpDirPath / "mbed-os" / "targets" / "targets.json5").write_text(target_json)

result = CliRunner().invoke(
configure, ["-m", "Target", "-t", "gcc_arm", "-p", tmpDir], catch_exceptions=False
Expand Down
1 change: 1 addition & 0 deletions tools/requirements.apt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ python3-lockfile
python3-junit.xml
python3-cryptography
python3-cbor
python3-json5
1 change: 1 addition & 0 deletions tools/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ junit-xml>=1.0,<2.0
lockfile
six>=1.0,<2.0
colorama>=0.3,<0.5
json5

# beautifulsoup only needed for USB device detection on Mac
beautifulsoup4; sys_platform == 'darwin'
Expand Down