From 6e9c679ce45db59255aa0ca29cd764572d83840b Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Fri, 11 Jul 2025 15:26:14 +0200 Subject: [PATCH] feat(cmakev2/idf): initialize IDF_TARGET build property Determine the IDF_TARGET from the following sources in this order: environmental variable, CMake cache variable, and sdkconfig files. Ensure there are no inconsistencies between the values set in these different locations. Signed-off-by: Frantisek Hrbata --- tools/cmakev2/idf.cmake | 75 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/tools/cmakev2/idf.cmake b/tools/cmakev2/idf.cmake index e31b535e59..343defc589 100644 --- a/tools/cmakev2/idf.cmake +++ b/tools/cmakev2/idf.cmake @@ -127,6 +127,77 @@ function(__init_sdkconfig_files) idf_build_set_property(SDKCONFIG_DEFAULTS "${sdkconfig_defaults_checked}") endfunction() +#[[ + __init_idf_target() + + Determine the IDF_TARGET value from the IDF_TARGET environment variable, the + CMake cache variable, or the sdkconfig files. If none of these are set, + use the default esp32 target. Ensure there are no inconsistencies in the + IDF_TARGET values set in different locations. + + Set the IDF_TARGET as a global variable, in the CMake cache, as an + environment variable, and as a build property. +#]] +function(__init_idf_target) + set(sdkconfig_target "") + set(target "") + set(sdkconfig_file "") + + idf_build_get_property(sdkconfig SDKCONFIG) + idf_build_get_property(sdkconfig_defaults SDKCONFIG_DEFAULTS) + + foreach(config ${sdkconfig} ${sdkconfig_defaults}) + idf_dbg("Searching for target in '${config}'") + __get_sdkconfig_option(OPTION CONFIG_IDF_TARGET + SDKCONFIG "${config}" + OUTPUT sdkconfig_target) + if(sdkconfig_target) + set(sdkconfig_file "${config}") + break() + endif() + endforeach() + + __get_default_value(VARIABLE IDF_TARGET + DEFAULT NOTFOUND + OUTPUT target) + + if(NOT target) + if(sdkconfig_target) + idf_msg("IDF_TARGET is not set, guessed '${sdkconfig_target}' " + "from sdkconfig '${sdkconfig_file}'") + set(target "${sdkconfig_target}") + else() + idf_msg("IDF_TARGET not set, using default target: esp32") + set(target "esp32") + endif() + endif() + + # Verify that the chosen target aligns with the CMake cache. + set(cache_target $CACHE{IDF_TARGET}) + if(cache_target) + if(NOT "${cache_target}" STREQUAL "${target}") + idf_die("IDF_TARGET '${cache_target}' in CMake cache does not match " + "currently selected IDF_TARGET '${target}'. " + "To change the target, clear the build directory and sdkconfig file, " + "and build the project again.") + endif() + endif() + + # Verify that the chosen target aligns with the sdkconfig. + if(sdkconfig_target) + if(NOT "${sdkconfig_target}" STREQUAL "${target}") + idf_die("Target '${sdkconfig_target}' in sdkconfig '${sdkconfig_file}' " + "does not match currently selected IDF_TARGET '${target}'. " + "To change the target, clear the build directory and sdkconfig file, " + "and build the project again.") + endif() + endif() + + idf_build_set_property(IDF_TARGET "${target}") + set(ENV{IDF_TARGET} ${target}) + set(IDF_TARGET ${target} CACHE STRING "IDF Build Target") +endfunction() + #[[ __init_components() @@ -234,6 +305,9 @@ __init_python() # Set SDKCONFIG and SDKCONFIG_DEFAULTS build properties. __init_sdkconfig_files() +# Set IDF_TARGET. +__init_idf_target() + # Discover and initialize components. __init_components() @@ -242,7 +316,6 @@ __init_components() Many of the following things are already implemented in PoC !38337, but they need to be reviewed. -* Set build target. * Set the toolchain before invoking project(). * Enable ccache if requested and available. * Generate initial sdkconfig for component manager.