From 11f3898e33ac32fd16ee2eee6f9906035d2a34d5 Mon Sep 17 00:00:00 2001 From: Djordje Nedic Date: Tue, 3 May 2022 13:45:42 +0200 Subject: [PATCH] tools: Fix cmake variable propagation This fixes the issue where component CMAKE variables propagate by default to the entire project, for instance compiler flags. Useful for when you want to have component specific compiler flags or defines. Closes https://github.com/espressif/esp-idf/pull/8806 Closes https://github.com/espressif/esp-idf/issues/8881 --- docs/en/migration-guides/build-system.rst | 15 +++++++++++++++ tools/cmake/project.cmake | 17 ++++++++++------- tools/test_apps/system/memprot/CMakeLists.txt | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/docs/en/migration-guides/build-system.rst b/docs/en/migration-guides/build-system.rst index 42a8711bd3..11e9f7aee6 100644 --- a/docs/en/migration-guides/build-system.rst +++ b/docs/en/migration-guides/build-system.rst @@ -55,3 +55,18 @@ instead of:: set(EXTRA_COMPONENT_DIRS "${EXTRA_COMPONENT_DIRS} path3") Defining these variables as CMake lists is compatible with previous IDF versions. + +Update usage of target_link_libraries with project_elf +------------------------------------------------------ + +ESP-IDF v5.0 fixes issues with CMake variable propagation for components, which caused among others compiler flags and defines applied to one component to be applied to every component in the project. + +As a side effect of this ESP-IDF user projects from ESP-IDF v5.0 onwards using ``target_link_libraries`` with ``project_elf`` explicitly and custom CMake projects must specify ``PRIVATE``, ``PUBLIC`` or ``INTERFACE`` arguments. This is a breaking change and is not backwards compatible with previous ESP-IDF versions. + +For example:: + + target_link_libraries(${project_elf} PRIVATE "-Wl,--wrap=esp_panic_handler") + +instead of:: + + target_link_libraries(${project_elf} "-Wl,--wrap=esp_panic_handler") diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index 250ade412e..def45ee20c 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -473,17 +473,17 @@ macro(project project_name) add_dependencies(${project_elf} _project_elf_src) if(__PROJECT_GROUP_LINK_COMPONENTS) - target_link_libraries(${project_elf} "-Wl,--start-group") + target_link_libraries(${project_elf} PRIVATE "-Wl,--start-group") endif() if(test_components) - target_link_libraries(${project_elf} "-Wl,--whole-archive") + target_link_libraries(${project_elf} PRIVATE "-Wl,--whole-archive") foreach(test_component ${test_components}) if(TARGET ${test_component}) - target_link_libraries(${project_elf} ${test_component}) + target_link_libraries(${project_elf} PRIVATE ${test_component}) endif() endforeach() - target_link_libraries(${project_elf} "-Wl,--no-whole-archive") + target_link_libraries(${project_elf} PRIVATE "-Wl,--no-whole-archive") endif() idf_build_get_property(build_components BUILD_COMPONENT_ALIASES) @@ -496,9 +496,12 @@ macro(project project_name) __component_get_property(whole_archive ${build_component_target} WHOLE_ARCHIVE) if(whole_archive) message(STATUS "Component ${build_component} will be linked with -Wl,--whole-archive") - target_link_libraries(${project_elf} "-Wl,--whole-archive" ${build_component} "-Wl,--no-whole-archive") + target_link_libraries(${project_elf} PRIVATE + "-Wl,--whole-archive" + ${build_component} + "-Wl,--no-whole-archive") else() - target_link_libraries(${project_elf} ${build_component}) + target_link_libraries(${project_elf} PRIVATE ${build_component}) endif() endforeach() @@ -507,7 +510,7 @@ macro(project project_name) set(mapfile "${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.map") set(idf_target "${IDF_TARGET}") string(TOUPPER ${idf_target} idf_target) - target_link_libraries(${project_elf} "-Wl,--cref" "-Wl,--defsym=IDF_TARGET_${idf_target}=0" + target_link_libraries(${project_elf} PRIVATE "-Wl,--cref" "-Wl,--defsym=IDF_TARGET_${idf_target}=0" "-Wl,--Map=\"${mapfile}\"") unset(idf_target) endif() diff --git a/tools/test_apps/system/memprot/CMakeLists.txt b/tools/test_apps/system/memprot/CMakeLists.txt index c1fbe08208..fc362b1d4d 100644 --- a/tools/test_apps/system/memprot/CMakeLists.txt +++ b/tools/test_apps/system/memprot/CMakeLists.txt @@ -4,6 +4,6 @@ if((IDF_TARGET STREQUAL "esp32s2") OR (IDF_TARGET STREQUAL "esp32c3")) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(test_memprot) - target_link_libraries(${project_elf} "-Wl,--wrap=esp_panic_handler") + target_link_libraries(${project_elf} PRIVATE "-Wl,--wrap=esp_panic_handler") endif()