Skip to content

Commit

Permalink
cmake: sysbuild: import image kconfig settings to image target
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
tejlmand authored and stephanosio committed Nov 2, 2022
1 parent 430370c commit e0fb044
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
27 changes: 21 additions & 6 deletions cmake/modules/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1425,13 +1425,21 @@ endfunction()

# 2.1 Misc
#
# import_kconfig(<prefix> <kconfig_fragment> [<keys>])
# import_kconfig(<prefix> <kconfig_fragment> [<keys>] [TARGET <target>])
#
# Parse a KConfig fragment (typically with extension .config) and
# introduce all the symbols that are prefixed with 'prefix' into the
# CMake namespace. List all created variable names in the 'keys'
# output variable if present.
#
# <prefix> : symbol prefix of settings in the Kconfig fragment.
# <kconfig_fragment>: absolute path to the config fragment file.
# <keys> : output variable which will be populated with variable
# names loaded from the kconfig fragment.
# TARGET <target> : set all symbols on <target> instead of adding them to the
# CMake namespace.
function(import_kconfig prefix kconfig_fragment)
cmake_parse_arguments(IMPORT_KCONFIG "" "TARGET" "" ${ARGN})
# Parse the lines prefixed with 'prefix' in ${kconfig_fragment}
file(
STRINGS
Expand All @@ -1457,16 +1465,23 @@ function(import_kconfig prefix kconfig_fragment)
set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})
endif()

set("${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}" PARENT_SCOPE)
if(DEFINED IMPORT_KCONFIG_TARGET)
set_property(TARGET ${IMPORT_KCONFIG_TARGET} APPEND PROPERTY "kconfigs" "${CONF_VARIABLE_NAME}")
set_property(TARGET ${IMPORT_KCONFIG_TARGET} PROPERTY "${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}")
else()
set("${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}" PARENT_SCOPE)
endif()
list(APPEND keys "${CONF_VARIABLE_NAME}")
endforeach()

if(ARGC GREATER 2)
if(ARGC GREATER 3)
list(LENGTH IMPORT_KCONFIG_UNPARSED_ARGUMENTS unparsed_length)
if(unparsed_length GREATER 0)
if(unparsed_length GREATER 1)
# Two mandatory arguments and one optional, anything after that is an error.
message(FATAL_ERROR "Unexpected argument after '<keys>': import_kconfig(... ${ARGV3})")
list(GET IMPORT_KCONFIG_UNPARSED_ARGUMENTS 1 first_invalid)
message(FATAL_ERROR "Unexpected argument after '<keys>': import_kconfig(... ${first_invalid})")
endif()
set(${ARGV2} "${keys}" PARENT_SCOPE)
set(${IMPORT_KCONFIG_UNPARSED_ARGUMENTS} "${keys}" PARENT_SCOPE)
endif()
endfunction()

Expand Down
26 changes: 21 additions & 5 deletions share/sysbuild/cmake/modules/sysbuild_extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function(load_cache)
endfunction()

# Usage:
# sysbuild_get(<variable> IMAGE <image> [VAR <image-variable>])
# sysbuild_get(<variable> IMAGE <image> [VAR <image-variable>] <KCONFIG|CACHE>)
#
# This function will return the variable found in the CMakeCache.txt file
# identified by image.
Expand All @@ -50,20 +50,26 @@ endfunction()
# The result will be returned in `<variable>`.
#
# Example use:
# sysbuild_get(PROJECT_NAME IMAGE my_sample)
# sysbuild_get(PROJECT_NAME IMAGE my_sample CACHE)
# will lookup PROJECT_NAME from the CMakeCache identified by `my_sample` and
# and return the value in the local variable `PROJECT_NAME`.
#
# sysbuild_get(my_sample_PROJECT_NAME IMAGE my_sample VAR PROJECT_NAME)
# sysbuild_get(my_sample_PROJECT_NAME IMAGE my_sample VAR PROJECT_NAME CACHE)
# will lookup PROJECT_NAME from the CMakeCache identified by `my_sample` and
# and return the value in the local variable `my_sample_PROJECT_NAME`.
#
# sysbuild_get(my_sample_CONFIG_FOO IMAGE my_sample VAR CONFIG_FOO KCONFIG)
# will lookup CONFIG_FOO from the KConfig identified by `my_sample` and
# and return the value in the local variable `my_sample_CONFIG_FOO`.
#
# <variable>: variable used for returning CMake cache value. Also used as lookup
# variable if `VAR` is not provided.
# IMAGE: image name identifying the cache to use for variable lookup.
# VAR: name of the CMake cache variable name to lookup.
# KCONFIG: Flag indicating that a Kconfig setting should be fetched.
# CACHE: Flag indicating that a CMake cache variable should be fetched.
function(sysbuild_get variable)
cmake_parse_arguments(GET_VAR "" "IMAGE;VAR" "" ${ARGN})
cmake_parse_arguments(GET_VAR "CACHE;KCONFIG" "IMAGE;VAR" "" ${ARGN})

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

get_property(${GET_VAR_IMAGE}_${GET_VAR_VAR} TARGET ${GET_VAR_IMAGE}_cache PROPERTY ${GET_VAR_VAR})
if(GET_VAR_KCONFIG)
set(variable_target ${GET_VAR_IMAGE})
elseif(GET_VAR_CACHE)
set(variable_target ${GET_VAR_IMAGE}_cache)
else()
message(WARNING "<CACHE> or <KCONFIG> not specified, defaulting to CACHE")
set(variable_target ${GET_VAR_IMAGE}_cache)
endif()

get_property(${GET_VAR_IMAGE}_${GET_VAR_VAR} TARGET ${variable_target} PROPERTY ${GET_VAR_VAR})
if(DEFINED ${GET_VAR_IMAGE}_${GET_VAR_VAR})
set(${variable} ${${GET_VAR_IMAGE}_${GET_VAR_VAR}} PARENT_SCOPE)
endif()
Expand Down Expand Up @@ -260,4 +275,5 @@ function(ExternalZephyrProject_Add)
BUILD_ALWAYS True
USES_TERMINAL_BUILD True
)
import_kconfig(CONFIG_ ${CMAKE_BINARY_DIR}/${ZBUILD_APPLICATION}/zephyr/.config TARGET ${ZBUILD_APPLICATION})
endfunction()

0 comments on commit e0fb044

Please sign in to comment.