Setting root file Kconfig via CMake cache variable not propagated correctly in sysbuild context

I am looking for a solution where I can set custom and configurable root Kconfig file via Cmake cache variable, which can be specified through the west build command.

Steps to Reproduce:

  • Define a cache variable in CMakeLists.txt:
  • option(FOO "Enable Foo")
    • if(FOO)
        set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig_1)
      else()
         set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig_2)
      endif()
  • Attempt to override this variable using the west build command:
  • west build --sysbuild ... -- -Dmy_sample_FOO=ON (Ref: https://docs.zephyrproject.org/latest/build/sysbuild/index.html#cmake-variable-namespacing)
  • Set zephyr_get(FOO MERGE SYSBUILD GLOBAL)
    in CMakeLists.txt after 'find_package(Zephyr)
  • Observe that the value of FOO will set as Kconfig_2, irrespective of the FOO value passed in west build command

Expected Behavior: The value of FOO should be overridden to "ON" and set root Kconfig file as Kconfig_1 rather than Kconfig_1 file, when specified in the west build command.

Actual Behavior: The value of root Kconfig file as Kconfig_1 regardless of the override attempt.

Additional Information:
The issue persists even when using the zephyr_get function. Because by the time the zephyr_get function is called, the Zephyr system has already parsed the default Kconfig root file if it isn't found.

Sample Code:
CMakeLists.txt
option(FOO "Enable Foo")

Attempt to use the variable
message(STATUS "FOO is set to ${FOO}")

Build Command:
west build --sysbuild ... -- -Dmy_sample_FOO=ON

Environment:

Zephyr version: 3.6.0
CMake version: 3.31.6
West version: 1.2.0
Operating System: Windows

Related