Skip to content

Commit e0fb044

Browse files
tejlmandstephanosio
authored andcommitted
cmake: sysbuild: import image kconfig settings to image target
Load image kconfig setting into image target properties. This allows sysbuild to evaluate and check image configuration as part of CMake invocation. sysbuild_get() is updated to support reading of CMake cache or Kconfig settings for an image. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 430370c commit e0fb044

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

cmake/modules/extensions.cmake

+21-6
Original file line numberDiff line numberDiff line change
@@ -1425,13 +1425,21 @@ endfunction()
14251425

14261426
# 2.1 Misc
14271427
#
1428-
# import_kconfig(<prefix> <kconfig_fragment> [<keys>])
1428+
# import_kconfig(<prefix> <kconfig_fragment> [<keys>] [TARGET <target>])
14291429
#
14301430
# Parse a KConfig fragment (typically with extension .config) and
14311431
# introduce all the symbols that are prefixed with 'prefix' into the
14321432
# CMake namespace. List all created variable names in the 'keys'
14331433
# output variable if present.
1434+
#
1435+
# <prefix> : symbol prefix of settings in the Kconfig fragment.
1436+
# <kconfig_fragment>: absolute path to the config fragment file.
1437+
# <keys> : output variable which will be populated with variable
1438+
# names loaded from the kconfig fragment.
1439+
# TARGET <target> : set all symbols on <target> instead of adding them to the
1440+
# CMake namespace.
14341441
function(import_kconfig prefix kconfig_fragment)
1442+
cmake_parse_arguments(IMPORT_KCONFIG "" "TARGET" "" ${ARGN})
14351443
# Parse the lines prefixed with 'prefix' in ${kconfig_fragment}
14361444
file(
14371445
STRINGS
@@ -1457,16 +1465,23 @@ function(import_kconfig prefix kconfig_fragment)
14571465
set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})
14581466
endif()
14591467

1460-
set("${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}" PARENT_SCOPE)
1468+
if(DEFINED IMPORT_KCONFIG_TARGET)
1469+
set_property(TARGET ${IMPORT_KCONFIG_TARGET} APPEND PROPERTY "kconfigs" "${CONF_VARIABLE_NAME}")
1470+
set_property(TARGET ${IMPORT_KCONFIG_TARGET} PROPERTY "${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}")
1471+
else()
1472+
set("${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}" PARENT_SCOPE)
1473+
endif()
14611474
list(APPEND keys "${CONF_VARIABLE_NAME}")
14621475
endforeach()
14631476

1464-
if(ARGC GREATER 2)
1465-
if(ARGC GREATER 3)
1477+
list(LENGTH IMPORT_KCONFIG_UNPARSED_ARGUMENTS unparsed_length)
1478+
if(unparsed_length GREATER 0)
1479+
if(unparsed_length GREATER 1)
14661480
# Two mandatory arguments and one optional, anything after that is an error.
1467-
message(FATAL_ERROR "Unexpected argument after '<keys>': import_kconfig(... ${ARGV3})")
1481+
list(GET IMPORT_KCONFIG_UNPARSED_ARGUMENTS 1 first_invalid)
1482+
message(FATAL_ERROR "Unexpected argument after '<keys>': import_kconfig(... ${first_invalid})")
14681483
endif()
1469-
set(${ARGV2} "${keys}" PARENT_SCOPE)
1484+
set(${IMPORT_KCONFIG_UNPARSED_ARGUMENTS} "${keys}" PARENT_SCOPE)
14701485
endif()
14711486
endfunction()
14721487

share/sysbuild/cmake/modules/sysbuild_extensions.cmake

+21-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function(load_cache)
3939
endfunction()
4040

4141
# Usage:
42-
# sysbuild_get(<variable> IMAGE <image> [VAR <image-variable>])
42+
# sysbuild_get(<variable> IMAGE <image> [VAR <image-variable>] <KCONFIG|CACHE>)
4343
#
4444
# This function will return the variable found in the CMakeCache.txt file
4545
# identified by image.
@@ -50,20 +50,26 @@ endfunction()
5050
# The result will be returned in `<variable>`.
5151
#
5252
# Example use:
53-
# sysbuild_get(PROJECT_NAME IMAGE my_sample)
53+
# sysbuild_get(PROJECT_NAME IMAGE my_sample CACHE)
5454
# will lookup PROJECT_NAME from the CMakeCache identified by `my_sample` and
5555
# and return the value in the local variable `PROJECT_NAME`.
5656
#
57-
# sysbuild_get(my_sample_PROJECT_NAME IMAGE my_sample VAR PROJECT_NAME)
57+
# sysbuild_get(my_sample_PROJECT_NAME IMAGE my_sample VAR PROJECT_NAME CACHE)
5858
# will lookup PROJECT_NAME from the CMakeCache identified by `my_sample` and
5959
# and return the value in the local variable `my_sample_PROJECT_NAME`.
6060
#
61+
# sysbuild_get(my_sample_CONFIG_FOO IMAGE my_sample VAR CONFIG_FOO KCONFIG)
62+
# will lookup CONFIG_FOO from the KConfig identified by `my_sample` and
63+
# and return the value in the local variable `my_sample_CONFIG_FOO`.
64+
#
6165
# <variable>: variable used for returning CMake cache value. Also used as lookup
6266
# variable if `VAR` is not provided.
6367
# IMAGE: image name identifying the cache to use for variable lookup.
6468
# VAR: name of the CMake cache variable name to lookup.
69+
# KCONFIG: Flag indicating that a Kconfig setting should be fetched.
70+
# CACHE: Flag indicating that a CMake cache variable should be fetched.
6571
function(sysbuild_get variable)
66-
cmake_parse_arguments(GET_VAR "" "IMAGE;VAR" "" ${ARGN})
72+
cmake_parse_arguments(GET_VAR "CACHE;KCONFIG" "IMAGE;VAR" "" ${ARGN})
6773

6874
if(NOT DEFINED GET_VAR_IMAGE)
6975
message(FATAL_ERROR "sysbuild_get(...) requires IMAGE.")
@@ -81,7 +87,16 @@ function(sysbuild_get variable)
8187
set(GET_VAR_VAR ${variable})
8288
endif()
8389

84-
get_property(${GET_VAR_IMAGE}_${GET_VAR_VAR} TARGET ${GET_VAR_IMAGE}_cache PROPERTY ${GET_VAR_VAR})
90+
if(GET_VAR_KCONFIG)
91+
set(variable_target ${GET_VAR_IMAGE})
92+
elseif(GET_VAR_CACHE)
93+
set(variable_target ${GET_VAR_IMAGE}_cache)
94+
else()
95+
message(WARNING "<CACHE> or <KCONFIG> not specified, defaulting to CACHE")
96+
set(variable_target ${GET_VAR_IMAGE}_cache)
97+
endif()
98+
99+
get_property(${GET_VAR_IMAGE}_${GET_VAR_VAR} TARGET ${variable_target} PROPERTY ${GET_VAR_VAR})
85100
if(DEFINED ${GET_VAR_IMAGE}_${GET_VAR_VAR})
86101
set(${variable} ${${GET_VAR_IMAGE}_${GET_VAR_VAR}} PARENT_SCOPE)
87102
endif()
@@ -260,4 +275,5 @@ function(ExternalZephyrProject_Add)
260275
BUILD_ALWAYS True
261276
USES_TERMINAL_BUILD True
262277
)
278+
import_kconfig(CONFIG_ ${CMAKE_BINARY_DIR}/${ZBUILD_APPLICATION}/zephyr/.config TARGET ${ZBUILD_APPLICATION})
263279
endfunction()

0 commit comments

Comments
 (0)