From 04a91f68cd8831dc2f890940633688751ac6d4e0 Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Fri, 11 Jul 2025 11:30:56 +0200 Subject: [PATCH] feat(cmakev2/utilities): add __get_default_value helper Helper for obtaining the default value of a variable. It returns the value of the specified variable based on the following order of precedence, with the highest precedence first: 1. Environmental variable 2. CMake cache variable 3. Provided default value This can be used to retrieve the value of variables that might also be set in the environment or cache, such as PYTHON or SDKCONFIG. Signed-off-by: Frantisek Hrbata --- tools/cmakev2/utilities.cmake | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tools/cmakev2/utilities.cmake b/tools/cmakev2/utilities.cmake index 34ef335de6e..5c300324d84 100644 --- a/tools/cmakev2/utilities.cmake +++ b/tools/cmakev2/utilities.cmake @@ -163,3 +163,50 @@ function(__get_absolute_paths) set(${ARG_OUTPUT} "${absolute_paths}" PARENT_SCOPE) endfunction() + +#[[ + __get_default_value(VARIABLE + DEFAULT ) + OUTPUT ) + + :VARIABLE[in]: The name of the variable for which to obtain its default + value. + :DEFAULT[in]: Default variable value. + :OUTPUT[out]: Variable to store the default value of ````. + + Set the ```` to the value of ```` based on the following + order of precedence, with the first having the highest precedence. + + 1. environmental variable + 2. CMake cache variable + 3. ```` as specified with the ``DEFAULT`` option +#]] +function(__get_default_value) + set(options) + set(one_value VARIABLE DEFAULT OUTPUT) + set(multi_value) + cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN}) + + if(NOT DEFINED ARG_VARIABLE) + idf_die("VARIABLE option is required") + endif() + + if(NOT DEFINED ARG_OUTPUT) + idf_die("OUTPUT option is required") + endif() + + if(NOT DEFINED ARG_DEFAULT) + set(ARG_DEFAULT "") + endif() + + + if(DEFINED ENV{${ARG_VARIABLE}}) + set(value "$ENV{${ARG_VARIABLE}}") + elseif(DEFINED CACHE{${ARG_VARIABLE}}) + set(value "$CACHE{${ARG_VARIABLE}}") + else() + set(value "${ARG_DEFAULT}") + endif() + + set(${ARG_OUTPUT} "${value}" PARENT_SCOPE) +endfunction()