Skip to content

Commit 3427aba

Browse files
Don't explode if given paths outside the root dir
1 parent 8128fa7 commit 3427aba

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

tools/cmake/mbed_generate_configuration.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ if(NOT MBED_NEED_TO_RECONFIGURE)
4343
file(TIMESTAMP ${CMAKE_CURRENT_BINARY_DIR}/mbed_config.cmake MBED_CONFIG_CMAKE_TIMESTAMP "%s" UTC)
4444

4545
foreach(CONFIG_JSON ${MBED_CONFIG_JSON_SOURCE_FILES})
46-
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/${CONFIG_JSON})
46+
get_filename_component(CONFIG_JSON_ABSPATH ${CONFIG_JSON} ABSOLUTE)
47+
48+
if(NOT EXISTS ${CONFIG_JSON_ABSPATH})
4749
message(STATUS "Mbed: ${CONFIG_JSON} deleted or renamed, regenerating configs...")
4850
set(MBED_NEED_TO_RECONFIGURE TRUE)
4951
break()
5052
endif()
5153

52-
file(TIMESTAMP ${CMAKE_SOURCE_DIR}/${CONFIG_JSON} CONFIG_JSON_TIMESTAMP "%s" UTC)
54+
file(TIMESTAMP ${CONFIG_JSON_ABSPATH} CONFIG_JSON_TIMESTAMP "%s" UTC)
5355
if(${CONFIG_JSON_TIMESTAMP} GREATER ${MBED_CONFIG_CMAKE_TIMESTAMP})
5456
message(STATUS "Mbed: ${CONFIG_JSON} modified, regenerating configs...")
5557
set(MBED_NEED_TO_RECONFIGURE TRUE)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ def assemble_config(target_attributes: dict, program: MbedProgram) -> Config:
5252
config.json_sources.append(program.files.custom_targets_json)
5353

5454
# Make all JSON sources relative paths to the program root
55-
config.json_sources = [json_source.relative_to(program.root) for json_source in config.json_sources]
55+
def make_relative_if_possible(path: Path):
56+
# Sadly, Pathlib did not gain a better way to do this until newer python versions.
57+
try:
58+
return path.relative_to(program.root)
59+
except ValueError:
60+
return path
61+
62+
config.json_sources = [make_relative_if_possible(program.root) for json_source in config.json_sources]
5663

5764
return config
5865

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Config(UserDict):
2424

2525
# List of JSON files used to create this config. Dumped to CMake at the end of configuration
2626
# so that it can regenerate configuration if the JSONs change.
27-
# All paths will be relative to the Mbed program root directory.
27+
# All paths will be relative to the Mbed program root directory, or absolute if outside said directory.
2828
json_sources: List[pathlib.Path] = []
2929

3030
def __setitem__(self, key: Hashable, item: Any) -> None:

0 commit comments

Comments
 (0)