Page 1 of 1

Cmake does not have access to kconfig variables before register

Posted: Sun Nov 19, 2023 8:27 pm
by KallDrexx
I am trying to do conditional compilation for my project based on menuconfig selections.

I have a custom Kconfig.projbuild and setting options via menuconfig. In my SDKConfig I see

Code: Select all

CONFIG_MICROGPU_DATABUS_SPI=y
In my cmake file I then have

Code: Select all

set(SOURCES
        displays/i80_display.c
        main.c
        ${MICROGPU_COMMON_SOURCES}
        ../../microgpu-common/color_rgb565.c
)

if (CONFIG_MICROGPU_DATABUS_SPI)
    list(APPEND SOURCES "spi_databus.c")
elseif (CONFIG_MICROGPU_DATABUS_BENCHMARK)
    list(APPEND SOURCES "benchmark_databus.c")
else ()
    message(FATAL_ERROR  "No databus type set")
endif ()

idf_component_register(SRCS ${SOURCES}
        INCLUDE_DIRS "." "../../")
This fails because it claims both of those options are not set, and thus my fatal message gets called.

However, putting this block after the component register "works". It appears that we do not have access to the kconfig variables until *after* the `idf_component_register()` call. This seems to be in direct contradiction to the build system documentation.

Am I doing something wrong? Is there a way to add sources after a `idf_component_register()` call?

Re: Cmake does not have access to kconfig variables before register

Posted: Sun Nov 19, 2023 8:37 pm
by KallDrexx
Ok I see,

It appears that esp-idf does a 2 pass setup (I guess?), meaning it's not correct to do a cmake error if a menuconfig option is not set as expected. While the variable is not present in the first cmake pass, the 2nd cmake pass works. So not erroring in the cmake but erroring in my C code itself seems to be the proper solution.

It may not be a double pass thing, I'm a newbie at cmake so I'm probably missing some other nuance, but the bigger take away is that any cmake directives that rely on menuconfig options prior to the component registration call will show up as empty, but will infact resolve correctly eventually.