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
This commit is contained in:
Djordje Nedic 2022-05-03 13:45:42 +02:00
parent 31b7694551
commit 11f3898e33
3 changed files with 26 additions and 8 deletions

View File

@ -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")

View File

@ -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()

View File

@ -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()