From b5c3d4f615e53cbdb8951f72846f8cd7484b545f Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Tue, 16 Mar 2021 17:39:16 +0800 Subject: [PATCH] cmake: swap priority between EXTRA_COMPONENT_DIRS and project components --- docs/en/api-guides/build-system.rst | 2 +- tools/ci/test_build_system.sh | 13 +++++++++++++ tools/ci/test_build_system_cmake.sh | 15 +++++++++++++-- tools/cmake/project.cmake | 12 ++++++------ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/docs/en/api-guides/build-system.rst b/docs/en/api-guides/build-system.rst index 4555b0dc85..a10da9083c 100644 --- a/docs/en/api-guides/build-system.rst +++ b/docs/en/api-guides/build-system.rst @@ -343,7 +343,7 @@ When CMake runs to configure the project, it logs the components included in the Multiple components with the same name -------------------------------------- -When ESP-IDF is collecting all the components to compile, it will do this in the order specified by ``COMPONENT_DIRS``; by default, this means ESP-IDF's internal components first, then the project's components, and finally any components set in ``EXTRA_COMPONENT_DIRS``. If two or more of these directories contain component sub-directories with the same name, the component in the last place searched is used. This allows, for example, overriding ESP-IDF components with a modified version by copying that component from the ESP-IDF components directory to the project components directory and then modifying it there. If used in this way, the ESP-IDF directory itself can remain untouched. +When ESP-IDF is collecting all the components to compile, it will do this in the order specified by ``COMPONENT_DIRS``; by default, this means ESP-IDF's internal components first (``IDF_PATH/components``), then any components in directories specified in ``EXTRA_COMPONENT_DIRS``, and finally the project's components (``PROJECT_DIR/components``). If two or more of these directories contain component sub-directories with the same name, the component in the last place searched is used. This allows, for example, overriding ESP-IDF components with a modified version by copying that component from the ESP-IDF components directory to the project components directory and then modifying it there. If used in this way, the ESP-IDF directory itself can remain untouched. .. note:: If a component is overridden in an existing project by moving it to a new location, the project will not automatically see the new component path. Run ``idf.py reconfigure`` (or delete the project build folder) and then build again. diff --git a/tools/ci/test_build_system.sh b/tools/ci/test_build_system.sh index 3c8298a7f8..a9a0004578 100755 --- a/tools/ci/test_build_system.sh +++ b/tools/ci/test_build_system.sh @@ -419,6 +419,19 @@ endmenu\n" >> ${IDF_PATH}/Kconfig; git checkout -- sdkconfig.rename Kconfig popd + print_status "Project components prioritized over EXTRA_COMPONENT_DIRS" + clean_build_dir + mkdir -p extra_dir/my_component + echo "COMPONENT_CONFIG_ONLY := 1" > extra_dir/my_component/component.mk + cp Makefile Makefile.bak # set EXTRA_COMPONENT_DIRS to point to the other directory + sed -i "s%PROJECT_NAME := app-template%PROJECT_NAME := app-template\nEXTRA_COMPONENT_DIRS := extra_dir%" Makefile + (make list-components | grep "$PWD/extra_dir/my_component") || failure "Unable to find component specified in EXTRA_COMPONENT_DIRS" + mkdir -p components/my_component + echo "COMPONENT_CONFIG_ONLY := 1" > components/my_component/component.mk + (make list-components | grep "$PWD/components/my_component") || failure "Project components should be prioritized over EXTRA_COMPONENT_DIRS" + mv Makefile.bak Makefile # revert previous modifications + rm -rf extra_dir components + print_status "All tests completed" if [ -n "${FAILURES}" ]; then echo "Some failures were detected:" diff --git a/tools/ci/test_build_system_cmake.sh b/tools/ci/test_build_system_cmake.sh index a1b91bb9fa..0d2349696d 100755 --- a/tools/ci/test_build_system_cmake.sh +++ b/tools/ci/test_build_system_cmake.sh @@ -814,6 +814,19 @@ endmenu\n" >> ${IDF_PATH}/Kconfig (idf.py reconfigure | grep "kconfig:$IDF_PATH/components/esp32/Kconfig") || failure "Failed to verify original `main` directory" rm -rf components + print_status "Project components prioritized over EXTRA_COMPONENT_DIRS" + clean_build_dir + mkdir -p extra_dir/my_component + echo "idf_component_register()" > extra_dir/my_component/CMakeLists.txt + cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory + ${SED} -i "s%cmake_minimum_required(VERSION \([0-9]\+\).\([0-9]\+\))%cmake_minimum_required(VERSION \1.\2)\nset(EXTRA_COMPONENT_DIRS extra_dir)%" CMakeLists.txt + (idf.py reconfigure | grep "$PWD/extra_dir/my_component") || failure "Unable to find component specified in EXTRA_COMPONENT_DIRS" + mkdir -p components/my_component + echo "idf_component_register()" > components/my_component/CMakeLists.txt + (idf.py reconfigure | grep "$PWD/components/my_component") || failure "Project components should be prioritized over EXTRA_COMPONENT_DIRS" + mv CMakeLists.bak CMakeLists.txt # revert previous modifications + rm -rf extra_dir components + print_status "Create project using idf.py and build it" echo "Trying to create project." (idf.py -C projects create-project temp_test_project) || failure "Failed to create the project." @@ -850,8 +863,6 @@ endmenu\n" >> ${IDF_PATH}/Kconfig expected_failure $EXPECTED_EXIT_VALUE idf.py create-project --path "$IDF_PATH/example_proj" temp_test_project || failure "Command exit value is wrong." rm -rf "$IDF_PATH/example_proj" - - print_status "All tests completed" if [ -n "${FAILURES}" ]; then echo "Some failures were detected:" diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index c9479565e5..7b993b6801 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -180,18 +180,18 @@ function(__project_init components_var test_components_var) __project_component_dir(${component_dir}) endforeach() else() + if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/main") + __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/main") + endif() + spaces2list(EXTRA_COMPONENT_DIRS) foreach(component_dir ${EXTRA_COMPONENT_DIRS}) __project_component_dir("${component_dir}") endforeach() - __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/components") - # Look for components in the usual places: CMAKE_CURRENT_LIST_DIR/main, - # CMAKE_CURRENT_LIST_DIR/components, and the extra component dirs - if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/main") - __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/main") - endif() + # extra component dirs, and CMAKE_CURRENT_LIST_DIR/components + __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/components") endif() spaces2list(COMPONENTS)