From 7493bd2e28d4339f7a81598b184bcd238515fe45 Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Thu, 9 May 2019 15:19:59 +0800 Subject: [PATCH] cmake: add command to get config value --- docs/en/api-guides/build-system-cmake.rst | 8 ++++++ tools/cmake/build.cmake | 31 +++++++++++++++++++++++ tools/cmake/project.cmake | 11 ++++++++ 3 files changed, 50 insertions(+) diff --git a/docs/en/api-guides/build-system-cmake.rst b/docs/en/api-guides/build-system-cmake.rst index 734588838f..a3c068bf90 100644 --- a/docs/en/api-guides/build-system-cmake.rst +++ b/docs/en/api-guides/build-system-cmake.rst @@ -980,6 +980,14 @@ The call requires the target chip to be specified with *target* argument. Option Specify the executable *executable* for ESP-IDF build. This attaches additional targets such as dependencies related to flashing, generating additional binary files, etc. Should be called after ``idf_build_process``. +.. code-block:: none + + idf_build_get_config(var config [GENERATOR_EXPRESSION]) + +Get the value of the specified config. Much like build properties, specifying +*GENERATOR_EXPRESSION* will retrieve the generator expression string for that config, instead of the actual value, which +can be used with CMake commands that support generator expressions. Actual config values are only known after call to `idf_build_process`, however. + .. _cmake-build-properties: idf-build-properties diff --git a/tools/cmake/build.cmake b/tools/cmake/build.cmake index 51bcfcd129..bd2184977b 100644 --- a/tools/cmake/build.cmake +++ b/tools/cmake/build.cmake @@ -297,6 +297,23 @@ macro(__build_set_default var default) unset(_var) endmacro() +# +# Import configs as build instance properties so that they are accessible +# using idf_build_get_config(). Config has to have been generated before calling +# this command. +# +function(__build_import_configs) + # Include the sdkconfig cmake file, since the following operations require + # knowledge of config values. + idf_build_get_property(sdkconfig_cmake SDKCONFIG_CMAKE) + include(${sdkconfig_cmake}) + + idf_build_set_property(__CONFIG_VARIABLES "${CONFIGS_LIST}") + foreach(config ${CONFIGS_LIST}) + set_property(TARGET __idf_build_target PROPERTY ${config} "${${config}}") + endforeach() +endfunction() + # idf_build_process # # @brief Main processing step for ESP-IDF build: config generation, adding components to the build, @@ -349,6 +366,7 @@ macro(idf_build_process target) idf_build_get_property(sdkconfig SDKCONFIG) idf_build_get_property(sdkconfig_defaults SDKCONFIG_DEFAULTS) __kconfig_generate_config("${sdkconfig}" "${sdkconfig_defaults}") + __build_import_configs() # Write the partial build properties to a temporary file. # The path to this generated file is set to a short-lived build @@ -468,4 +486,17 @@ function(idf_build_executable elf) # Add dependency of the build target to the executable add_dependencies(${elf} __idf_build_target) +endfunction() + +# idf_build_get_config +# +# @brief Get value of specified config variable +function(idf_build_get_config var config) + cmake_parse_arguments(_ "GENERATOR_EXPRESSION" "" "" ${ARGN}) + if(__GENERATOR_EXPRESSION) + set(val "$") + else() + get_property(val TARGET __idf_build_target PROPERTY ${config}) + endif() + set(${var} ${val} PARENT_SCOPE) endfunction() \ No newline at end of file diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index 4522d0a2cf..d7bf90f05b 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -397,4 +397,15 @@ macro(project project_name) idf_build_executable(${project_elf}) __project_info("${test_components}") + + # Make build variables and config variables available after project call (of course the value + # of these variables can be accessed via idf_build_get_property or idf_build_get_config) + idf_build_get_property(sdkconfig_cmake SDKCONFIG_CMAKE) + include(${sdkconfig_cmake}) + + idf_build_get_property(build_properties __BUILD_PROPERTIES) + foreach(build_property ${build_properties}) + idf_build_get_property(val ${build_property}) + set(${build_property} "${val}") + endforeach() endmacro()