Skip to content

Commit cc53ad6

Browse files
nordicjmcarlescufi
authored andcommitted
sysbuild: Add sysbuild_cache_set CMake function
Adds a new CMake helper function that can be used from sysbuild to set or update sysbuild CMake cache variables. Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent cd9465a commit cc53ad6

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

share/sysbuild/cmake/modules/sysbuild_extensions.cmake

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,5 +384,70 @@ function(sysbuild_module_call)
384384
endforeach()
385385
endif()
386386
endforeach()
387+
endfunction()
388+
389+
# Usage:
390+
# sysbuild_cache_set(VAR <variable> [APPEND [REMOVE_DUPLICATES]] <value>)
391+
#
392+
# This function will set the specified value of the sysbuild cache variable in
393+
# the CMakeCache.txt file which can then be accessed by images.
394+
# `VAR` specifies the variable name to set/update.
395+
#
396+
# The result will be returned in `<variable>`.
397+
#
398+
# Example use:
399+
# sysbuild_cache_set(VAR ATTRIBUTES APPEND REMOVE_DUPLICATES battery)
400+
# Will add the item `battery` to the `ATTRIBUTES` variable as a new element
401+
# in the list in the CMakeCache and remove any duplicates from the list.
402+
#
403+
# <variable>: Name of variable in CMake cache.
404+
# APPEND: If specified then will append the supplied data to the
405+
# existing value as a list.
406+
# REMOVE_DUPLICATES: If specified then remove duplicate entries contained
407+
# within the list before saving to the cache.
408+
# <value>: Value to set/update.
409+
function(sysbuild_cache_set)
410+
cmake_parse_arguments(VARS "APPEND;REMOVE_DUPLICATES" "VAR" "" ${ARGN})
411+
412+
zephyr_check_arguments_required(sysbuild_cache_set VARS VAR)
413+
414+
if(NOT VARS_UNPARSED_ARGUMENTS AND VARS_APPEND)
415+
# Nothing to append so do nothing
416+
return()
417+
elseif(VARS_REMOVE_DUPLICATES AND NOT VARS_APPEND)
418+
message(FATAL_ERROR
419+
"sysbuild_set(VAR <var> APPEND REMOVE_DUPLICATES ...) missing required APPEND option")
420+
endif()
421+
422+
get_property(var_type CACHE ${VARS_VAR} PROPERTY TYPE)
423+
get_property(var_help CACHE ${VARS_VAR} PROPERTY HELPSTRING)
424+
425+
# If the variable type is not set, use UNINITIALIZED which will not apply any
426+
# specific formatting.
427+
if(NOT var_type)
428+
set(var_type "UNINITIALIZED")
429+
endif()
430+
431+
if(VARS_APPEND)
432+
set(var_new "$CACHE{${VARS_VAR}}")
433+
434+
# Search for these exact items in the existing value and prevent adding
435+
# them if they are already present which avoids issues with double addition
436+
# when cmake is reran.
437+
string(FIND "$CACHE{${VARS_VAR}}" "${VARS_UNPARSED_ARGUMENTS}" index)
438+
439+
if(NOT ${index} EQUAL -1)
440+
return()
441+
endif()
442+
443+
list(APPEND var_new "${VARS_UNPARSED_ARGUMENTS}")
444+
445+
if(VARS_REMOVE_DUPLICATES)
446+
list(REMOVE_DUPLICATES var_new)
447+
endif()
448+
else()
449+
set(var_new "${VARS_UNPARSED_ARGUMENTS}")
450+
endif()
387451

452+
set(${VARS_VAR} "${var_new}" CACHE "${var_type}" "${var_help}" FORCE)
388453
endfunction()

0 commit comments

Comments
 (0)