Using application defined Kconfig option in CMakeLists

Hi,

In our application we have custom Kconfig flag defined to wrap other Kconfig options and conditionally compile application files. However this Kconfig, name it CONFIG_X, is not visible in the application CMakeList.txt file unless it's forwarded along the build command instead of being set in prj.conf file or enabled by default. In all cases it gets set properly because it ends up in the resulting .config file but it's like "too late" for the cmake to grab it.

To clarify:

cmake_minimum_required(VERSION 3.20.0)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(BOARD_ROOT ${BOARD_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
set(CONF_FILE prj.conf)

if (CONFIG_X)
    # append files to compile
endif()

...

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(ev_charger)

...

In the application directory there is Kconfig file with:

config X
    bool "X description"
    default y
    depends on Y

Now the CONFIG_X is only equal to true in the CMakeLists.txt when using:
west build -b nrf52840dk_nrf52840 -- -DCONFIG_X=y

although with:

west build -b nrf52840dk_nrf52840

This Kconfig is still enabled in the .config/autoconf.h.

Is there any way to use Kconfig/CMake combination in such a way or it's not expected to work?

Thanks in advance!

Related