diff --git a/tools/cmakev2/component.cmake b/tools/cmakev2/component.cmake index 8c3615224fd..c2dfa34ad5a 100644 --- a/tools/cmakev2/component.cmake +++ b/tools/cmakev2/component.cmake @@ -359,6 +359,11 @@ function(__init_component) __collect_kconfig_files_from_directory("${component_directory}" "${target}" component_kconfig component_projbuild component_rename) + set(component_project_include "${component_directory}/project_include.cmake") + if(NOT EXISTS "${component_project_include}") + set(component_project_include "") + endif() + __get_component_interface(COMPONENT "${component_name}" OUTPUT existing_component_interface) if(NOT "${existing_component_interface}" STREQUAL "NOTFOUND") # A component with the same name is already initialized. Check if it @@ -399,6 +404,9 @@ function(__init_component) idf_component_set_property("${component_name}" __KCONFIG "${component_kconfig}") idf_component_set_property("${component_name}" __KCONFIG_PROJBUILD "${component_projbuild}") idf_component_set_property("${component_name}" __SDKCONFIG_RENAME "${component_rename}") + + # Update component project_include.cmake path. + idf_component_set_property("${component_name}" __PROJECT_INCLUDE "${component_project_include}") endif() return() @@ -431,6 +439,9 @@ function(__init_component) idf_component_set_property("${component_name}" __KCONFIG "${component_kconfig}") idf_component_set_property("${component_name}" __KCONFIG_PROJBUILD "${component_projbuild}") idf_component_set_property("${component_name}" __SDKCONFIG_RENAME "${component_rename}") + + # Set component project_include.cmake. + idf_component_set_property("${component_name}" __PROJECT_INCLUDE "${component_project_include}") endfunction() #[[ diff --git a/tools/cmakev2/project.cmake b/tools/cmakev2/project.cmake index 48194b0cfcc..fe0b51e22c9 100644 --- a/tools/cmakev2/project.cmake +++ b/tools/cmakev2/project.cmake @@ -516,6 +516,13 @@ endfunction() build system functions. It initializes the PROJECT_NAME and PROJECT_VER build properties, as well as all default C, CXX, and ASM compile options, link options, and compile definitions. + + This macro also includes project_include.cmake files for the discovered + components, as these files define project-wide functionality that needs to + be available before any component's CMakeLists.txt is evaluated. The + project_include.cmake files should be evaluated in the global scope. + Therefore, this is defined as a macro and should be called only from the + global scope or from within another macro. #]] macro(idf_project_init) idf_build_get_property(project_initialized __PROJECT_INITIALIZED) @@ -532,6 +539,26 @@ macro(idf_project_init) # Initialize all compilation options and defines. __init_project_configuration() + # Include all project_include.cmake files for the components that have + # been discovered. + idf_build_get_property(component_names COMPONENTS_DISCOVERED) + foreach(component_name IN LISTS component_names) + idf_component_get_property(project_include ${component_name} __PROJECT_INCLUDE) + idf_component_get_property(component_dir ${component_name} COMPONENT_DIR) + if(project_include) + set(COMPONENT_NAME ${component_name}) + set(COMPONENT_DIR ${component_dir}) + # The use of COMPONENT_PATH is deprecated in cmakev1. Users + # are encouraged to use COMPONENT_DIR instead. + set(COMPONENT_PATH ${component_dir}) + idf_dbg("Including ${project_include}") + include("${project_include}") + unset(COMPONENT_NAME) + unset(COMPONENT_DIR) + unset(COMPONENT_PATH) + endif() + endforeach() + idf_build_set_property(__PROJECT_INITIALIZED YES) endif() unset(project_initialized)