Skip to content

Commit 0711f42

Browse files
nordicjmkartben
authored andcommitted
sysbuild: Add support for snippets
Adds support for sysbuild loading snippets, these can be included by using e.g.: cmake ... -DSB_SNIPPET=blah for sysbuild directly or can be used with an application and sysbuild using -DSNIPPET. Snippets for sysbuild can use SB_EXTRA_CONF_FILE in the snippet file to specify an extra Kconfig fragment for sysbuild Signed-off-by: Jamie McCrae <[email protected]>
1 parent 1207ccf commit 0711f42

File tree

8 files changed

+58
-12
lines changed

8 files changed

+58
-12
lines changed

cmake/modules/root.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ zephyr_get(BOARD_ROOT MERGE SYSBUILD GLOBAL)
2828
zephyr_get(SOC_ROOT MERGE SYSBUILD GLOBAL)
2929
zephyr_get(ARCH_ROOT MERGE SYSBUILD GLOBAL)
3030
zephyr_get(SCA_ROOT MERGE SYSBUILD GLOBAL)
31+
zephyr_get(SNIPPET_ROOT MERGE SYSBUILD GLOBAL)
3132

3233
# Convert paths to absolute, relative from APPLICATION_SOURCE_DIR
3334
zephyr_file(APPLICATION_ROOT MODULE_EXT_ROOT)
3435
zephyr_file(APPLICATION_ROOT BOARD_ROOT)
3536
zephyr_file(APPLICATION_ROOT SOC_ROOT)
3637
zephyr_file(APPLICATION_ROOT ARCH_ROOT)
3738
zephyr_file(APPLICATION_ROOT SCA_ROOT)
39+
zephyr_file(APPLICATION_ROOT SNIPPET_ROOT)
3840

3941
if(unittest IN_LIST Zephyr_FIND_COMPONENTS)
4042
# Zephyr used in unittest mode, use dedicated unittest root.

cmake/modules/snippets.cmake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,23 @@ zephyr_check_cache(SNIPPET WATCH)
4545
function(zephyr_process_snippets)
4646
set(snippets_py ${ZEPHYR_BASE}/scripts/snippets.py)
4747
set(snippets_generated ${CMAKE_BINARY_DIR}/zephyr/snippets_generated.cmake)
48+
set_ifndef(SNIPPET_NAMESPACE SNIPPET)
49+
set_ifndef(SNIPPET_PYTHON_EXTRA_ARGS "")
50+
set_ifndef(SNIPPET_APP_DIR "${APPLICATION_SOURCE_DIR}")
4851

4952
# Set SNIPPET_AS_LIST, removing snippets_generated.cmake if we are
5053
# running cmake again and snippets are no longer requested.
51-
if (NOT DEFINED SNIPPET)
54+
if(NOT DEFINED SNIPPET)
5255
set(SNIPPET_AS_LIST "" PARENT_SCOPE)
5356
file(REMOVE ${snippets_generated})
5457
else()
55-
string(REPLACE " " ";" SNIPPET_AS_LIST "${SNIPPET}")
58+
string(REPLACE " " ";" SNIPPET_AS_LIST "${${SNIPPET_NAMESPACE}}")
5659
set(SNIPPET_AS_LIST "${SNIPPET_AS_LIST}" PARENT_SCOPE)
5760
endif()
5861

5962
# Set SNIPPET_ROOT.
60-
list(APPEND SNIPPET_ROOT ${APPLICATION_SOURCE_DIR})
63+
zephyr_get(SNIPPET_ROOT MERGE SYSBUILD GLOBAL)
64+
list(APPEND SNIPPET_ROOT ${SNIPPET_APP_DIR})
6165
list(APPEND SNIPPET_ROOT ${ZEPHYR_BASE})
6266
unset(real_snippet_root)
6367
foreach(snippet_dir ${SNIPPET_ROOT})
@@ -85,6 +89,7 @@ function(zephyr_process_snippets)
8589
${snippet_root_args}
8690
${requested_snippet_args}
8791
--cmake-out ${snippets_generated}
92+
${SNIPPET_PYTHON_EXTRA_ARGS}
8893
OUTPUT_VARIABLE output
8994
ERROR_VARIABLE output
9095
RESULT_VARIABLE ret)

scripts/schemas/snippet-schema.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ schema;append-schema:
1313
type: str
1414
EXTRA_CONF_FILE:
1515
type: str
16+
SB_EXTRA_CONF_FILE:
17+
type: str
1618
DTS_EXTRA_CPPFLAGS:
1719
type: str
1820

scripts/snippets.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class Snippet:
4848
appends: Appends = field(default_factory=_new_append)
4949
board2appends: Dict[str, Appends] = field(default_factory=_new_board2appends)
5050

51-
def process_data(self, pathobj: Path, snippet_data: dict):
51+
def process_data(self, pathobj: Path, snippet_data: dict, sysbuild: bool):
5252
'''Process the data in a snippet.yml file, after it is loaded into a
5353
python object and validated by pykwalify.'''
5454
def append_value(variable, value):
55-
if variable in ('EXTRA_DTC_OVERLAY_FILE', 'EXTRA_CONF_FILE'):
55+
if variable in ('SB_EXTRA_CONF_FILE', 'EXTRA_DTC_OVERLAY_FILE', 'EXTRA_CONF_FILE'):
5656
path = pathobj.parent / value
5757
if not path.is_file():
5858
_err(f'snippet file {pathobj}: {variable}: file not found: {path}')
@@ -62,14 +62,18 @@ def append_value(variable, value):
6262
_err(f'unknown append variable: {variable}')
6363

6464
for variable, value in snippet_data.get('append', {}).items():
65-
self.appends[variable].append(append_value(variable, value))
65+
if (sysbuild is True and variable[0:3] == 'SB_') or \
66+
(sysbuild is False and variable[0:3] != 'SB_'):
67+
self.appends[variable].append(append_value(variable, value))
6668
for board, settings in snippet_data.get('boards', {}).items():
6769
if board.startswith('/') and not board.endswith('/'):
6870
_err(f"snippet file {pathobj}: board {board} starts with '/', so "
6971
"it must end with '/' to use a regular expression")
7072
for variable, value in settings.get('append', {}).items():
71-
self.board2appends[board][variable].append(
72-
append_value(variable, value))
73+
if (sysbuild is True and variable[0:3] == 'SB_') or \
74+
(sysbuild is False and variable[0:3] != 'SB_'):
75+
self.board2appends[board][variable].append(
76+
append_value(variable, value))
7377

7478
class Snippets(UserDict):
7579
'''Type for all the information we have discovered about all snippets.
@@ -212,6 +216,8 @@ def parse_args():
212216
parser.add_argument('--cmake-out', type=Path,
213217
help='''file to write cmake output to; include()
214218
this file after calling this script''')
219+
parser.add_argument('--sysbuild', action="store_true",
220+
help='''set if this is running as sysbuild''')
215221
return parser.parse_args()
216222

217223
def setup_logging():
@@ -234,7 +240,7 @@ def process_snippets(args: argparse.Namespace) -> Snippets:
234240
# Process each path in snippet_root in order, adjusting
235241
# snippets as needed for each one.
236242
for root in args.snippet_root:
237-
process_snippets_in(root, snippets)
243+
process_snippets_in(root, snippets, args.sysbuild)
238244

239245
return snippets
240246

@@ -250,11 +256,11 @@ def find_snippets_in_roots(requested_snippets, snippet_roots) -> Snippets:
250256
# Process each path in snippet_root in order, adjusting
251257
# snippets as needed for each one.
252258
for root in snippet_roots:
253-
process_snippets_in(root, snippets)
259+
process_snippets_in(root, snippets, False)
254260

255261
return snippets
256262

257-
def process_snippets_in(root_dir: Path, snippets: Snippets) -> None:
263+
def process_snippets_in(root_dir: Path, snippets: Snippets, sysbuild: bool) -> None:
258264
'''Process snippet.yml files in *root_dir*,
259265
updating *snippets* as needed.'''
260266

@@ -276,7 +282,7 @@ def process_snippets_in(root_dir: Path, snippets: Snippets) -> None:
276282
name = snippet_data['name']
277283
if name not in snippets:
278284
snippets[name] = Snippet(name=name)
279-
snippets[name].process_data(snippet_yml, snippet_data)
285+
snippets[name].process_data(snippet_yml, snippet_data, sysbuild)
280286
snippets.paths.add(snippet_yml)
281287

282288
def load_snippet_yml(snippet_yml: Path) -> dict:

share/sysbuild/cmake/modules/sysbuild_default.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ include(zephyr_module)
1515
include(boards)
1616
include(shields)
1717
include(hwm_v2)
18+
include(sysbuild_snippets)
1819
include(sysbuild_kconfig)
1920
include(native_simulator_sb_extensions)
2021
include(sysbuild_images)

share/sysbuild/cmake/modules/sysbuild_kconfig.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ set(KCONFIG_NAMESPACE SB_CONFIG)
105105
if(EXISTS ${APP_DIR}/Kconfig.sysbuild)
106106
set(KCONFIG_ROOT ${APP_DIR}/Kconfig.sysbuild)
107107
endif()
108+
109+
# Apply any EXTRA_CONF_FILE variables from snippets
110+
if(TARGET snippets_scope)
111+
get_property(snippets_EXTRA_CONF_FILE TARGET snippets_scope PROPERTY SB_EXTRA_CONF_FILE)
112+
list(APPEND EXTRA_CONF_FILE ${snippets_EXTRA_CONF_FILE})
113+
endif()
114+
108115
include(${ZEPHYR_BASE}/cmake/modules/kconfig.cmake)
109116
set(CONF_FILE)
110117
set(EXTRA_CONF_FILE)
118+
set(SB_EXTRA_CONF_FILE)

share/sysbuild/cmake/modules/sysbuild_root.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ zephyr_get(BOARD_ROOT MERGE)
3131
zephyr_get(SOC_ROOT MERGE)
3232
zephyr_get(ARCH_ROOT MERGE)
3333
zephyr_get(SCA_ROOT MERGE)
34+
zephyr_get(SNIPPET_ROOT MERGE)
3435

3536
# Convert paths to absolute, relative from APP_DIR
3637
zephyr_file(APPLICATION_ROOT MODULE_EXT_ROOT BASE_DIR ${APP_DIR})
3738
zephyr_file(APPLICATION_ROOT BOARD_ROOT BASE_DIR ${APP_DIR})
3839
zephyr_file(APPLICATION_ROOT SOC_ROOT BASE_DIR ${APP_DIR})
3940
zephyr_file(APPLICATION_ROOT ARCH_ROOT BASE_DIR ${APP_DIR})
4041
zephyr_file(APPLICATION_ROOT SCA_ROOT BASE_DIR ${APP_DIR})
42+
zephyr_file(APPLICATION_ROOT SNIPPET_ROOT BASE_DIR ${APP_DIR})
4143

4244
# Sysbuild must ensure any locally defined variables in sysbuild/CMakeLists.txt
4345
# have been added to the cache in order for the settings to propagate to images.
@@ -61,3 +63,7 @@ endif()
6163
if(DEFINED SCA_ROOT)
6264
set(SCA_ROOT ${SCA_ROOT} CACHE PATH "Sysbuild adjusted SCA_ROOT" FORCE)
6365
endif()
66+
67+
if(DEFINED SNIPPET_ROOT)
68+
set(SNIPPET_ROOT ${SNIPPET_ROOT} CACHE PATH "Sysbuild adjusted SNIPPET_ROOT" FORCE)
69+
endif()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2024 Nordic Semiconductor
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
zephyr_get(SB_SNIPPET)
6+
if(NOT SB_SNIPPET AND SNIPPET)
7+
set_ifndef(SB_SNIPPET ${SNIPPET})
8+
endif()
9+
set(SNIPPET_NAMESPACE SB_SNIPPET)
10+
set(SNIPPET_PYTHON_EXTRA_ARGS --sysbuild)
11+
set(SNIPPET_APP_DIR ${APP_DIR})
12+
include(${ZEPHYR_BASE}/cmake/modules/snippets.cmake)
13+
14+
set(SNIPPET_NAMESPACE)
15+
set(SNIPPET_PYTHON_EXTRA_ARGS)
16+
set(SNIPPET_APP_DIR)

0 commit comments

Comments
 (0)