cmake: support snippets scope for zephyr_get()

Fixes: #57139

Snippets provides the possibility of defining EXTRA_DTC_OVERLAY_FILE and
EXTRA_CONF_FILE in snippets.
Snippets must co-exist with existing infrastructure of
EXTRA_DTC_OVERLAY_FILE and EXTRA_CONF_FILE, and a user specifying a
snippet must be able to specify extra files for adjusting the snippet.

This means that if the following is specified:
`-DSNIPPET=some_snippet -DEXTRA_CONF_FILE=extra.conf`
then `extra.conf` may contain adjustments to the snippet.
Similar to sysbuild. Imagine a sysbuild controlled image uses a default
snippet for building, by ensuring that any extra
`-D<image>_EXTRA_CONF_FILE=extra.conf` arguments takes precedence over
the snippet we allow users to make adjustments if they need.

This commit introduces a snippets scope where snippet scoped variables
can be set with `zephyr_set()` and then `zephyr_get()` will take the
snippet scoped variables into consideration before returning.

Adjust calls to `zephyr_get(EXTRA_DTC_OVERLAY_FILE)` and
`zephyr_get(EXTRA_CONF_FILE)` to use `MERGE` to ensure all scopes are
considered.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2023-05-08 14:01:34 +02:00 committed by Fabio Baltieri
parent a6d9105d51
commit ba48dd8763
4 changed files with 11 additions and 8 deletions

View file

@ -116,5 +116,5 @@ DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"")
# The DTC_OVERLAY_FILE variable is now set to its final value.
zephyr_boilerplate_watch(DTC_OVERLAY_FILE)
zephyr_get(EXTRA_CONF_FILE SYSBUILD LOCAL VAR EXTRA_CONF_FILE OVERLAY_CONFIG)
zephyr_get(EXTRA_DTC_OVERLAY_FILE SYSBUILD LOCAL)
zephyr_get(EXTRA_CONF_FILE SYSBUILD LOCAL VAR EXTRA_CONF_FILE OVERLAY_CONFIG MERGE REVERSE)
zephyr_get(EXTRA_DTC_OVERLAY_FILE SYSBUILD LOCAL MERGE REVERSE)

View file

@ -2568,9 +2568,13 @@ function(zephyr_get variable)
else()
set(sysbuild_${var})
endif()
if(TARGET snippets_scope)
get_property(snippets_${var} TARGET snippets_scope PROPERTY ${var})
endif()
endforeach()
set(scopes "sysbuild;CACHE;ENV;current")
set(scopes "sysbuild;CACHE;snippets;ENV;current")
if(GET_VAR_REVERSE)
list(REVERSE scopes)
endif()

View file

@ -93,10 +93,6 @@ function(zephyr_process_snippets)
endif()
include(${snippets_generated})
# Propagate include()d build system settings to the caller.
set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} PARENT_SCOPE)
set(OVERLAY_CONFIG ${OVERLAY_CONFIG} PARENT_SCOPE)
# Create the 'snippets' target. Each snippet is printed in a
# separate command because build system files are not fond of
# newlines.

View file

@ -133,6 +133,9 @@ set(SNIPPET_NAMES {' '.join(f'"{name}"' for name in snippet_names)})
# The paths to all the snippet.yml files. One snippet
# can have multiple snippet.yml files.
set(SNIPPET_PATHS {snippet_path_list})
# Create variable scope for snippets build variables
zephyr_create_scope(snippets)
''')
for snippet_name in snippet_names:
@ -168,7 +171,7 @@ if("${{BOARD}}" STREQUAL "{board}")''')
space = ' ' * indent
for name, values in appends.items():
for value in values:
self.print(f'{space}list(APPEND {name} {value})')
self.print(f'{space}zephyr_set({name} {value} SCOPE snippets APPEND)')
def print(self, *args, **kwargs):
kwargs['file'] = self.out_file