From 237b2ce40c04ba7feb141e56c91fe3a0c25b6926 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sun, 28 Aug 2022 17:07:49 +0200 Subject: [PATCH] cmake: use target_link_options and _directories supported since 3.13 Now that the supported CMake version is >=3.16, this code can be simplified. The code to deduplicate the directories can be removed since this is handled by target_link_directories. --- components/bt/CMakeLists.txt | 10 +++++----- components/esp_phy/CMakeLists.txt | 6 +++--- components/esp_wifi/CMakeLists.txt | 2 +- components/ieee802154/CMakeLists.txt | 2 +- tools/cmake/build.cmake | 3 +-- tools/cmake/project.cmake | 8 ++++++-- tools/cmake/utilities.cmake | 26 ++++++++++---------------- 7 files changed, 27 insertions(+), 30 deletions(-) diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 47d99d3cb6..9518f7b66c 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -687,17 +687,17 @@ idf_component_register(SRCS "${srcs}" if(CONFIG_BT_ENABLED) target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-implicit-fallthrough -Wno-unused-const-variable) if(CONFIG_IDF_TARGET_ESP32) - target_link_libraries(${COMPONENT_LIB} INTERFACE "-L${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32/esp32") + target_link_directories(${COMPONENT_LIB} INTERFACE "${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32/esp32") target_link_libraries(${COMPONENT_LIB} PUBLIC btdm_app) target_link_libraries(${COMPONENT_LIB} INTERFACE "-u ld_include_hli_vectors_bt") elseif(CONFIG_IDF_TARGET_ESP32C3) - target_link_libraries(${COMPONENT_LIB} INTERFACE - "-L${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32c3") + target_link_directories(${COMPONENT_LIB} INTERFACE + "${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32c3") target_link_libraries(${COMPONENT_LIB} PUBLIC btdm_app) elseif(CONFIG_IDF_TARGET_ESP32S3) - target_link_libraries(${COMPONENT_LIB} INTERFACE - "-L${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32s3") + target_link_directories(${COMPONENT_LIB} INTERFACE + "${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c3_family/esp32s3") target_link_libraries(${COMPONENT_LIB} PUBLIC btdm_app) elseif(CONFIG_IDF_TARGET_ESP32H2) if(CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_1) diff --git a/components/esp_phy/CMakeLists.txt b/components/esp_phy/CMakeLists.txt index 10550e7fd8..cd35c9b460 100644 --- a/components/esp_phy/CMakeLists.txt +++ b/components/esp_phy/CMakeLists.txt @@ -39,13 +39,13 @@ idf_component_register(SRCS "${srcs}" set(target_name "${idf_target}") if(IDF_TARGET STREQUAL "esp32h2") if(CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_2) - target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev2\"") + target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev2") endif() if(CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_1) - target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev1\"") + target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/rev1") endif() else() - target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}\"") + target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}") endif() # Override functions in PHY lib with the functions in 'phy_override.c' diff --git a/components/esp_wifi/CMakeLists.txt b/components/esp_wifi/CMakeLists.txt index 1323d51e3f..5722e0a00c 100644 --- a/components/esp_wifi/CMakeLists.txt +++ b/components/esp_wifi/CMakeLists.txt @@ -39,7 +39,7 @@ if(CONFIG_ESP32_WIFI_ENABLED) idf_build_get_property(build_dir BUILD_DIR) set(target_name "${idf_target}") - target_link_libraries(${COMPONENT_LIB} PUBLIC "-L \"${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}\"") + target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}") if(link_binary_libs) if(CONFIG_IDF_TARGET_ESP32C2) diff --git a/components/ieee802154/CMakeLists.txt b/components/ieee802154/CMakeLists.txt index 65f3f1760d..c96b558752 100644 --- a/components/ieee802154/CMakeLists.txt +++ b/components/ieee802154/CMakeLists.txt @@ -22,7 +22,7 @@ if(CONFIG_IEEE802154_ENABLED) "-L ${CMAKE_CURRENT_SOURCE_DIR}/lib/${idf_target}/rev2") endif() else() - target_link_libraries(${COMPONENT_LIB} INTERFACE "-L ${CMAKE_CURRENT_SOURCE_DIR}/lib/${idf_target}") + target_link_directories(${COMPONENT_LIB} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/lib/${idf_target}") endif() target_link_libraries(${COMPONENT_LIB} INTERFACE $ lib802154.a libphy.a libbtbb.a $) diff --git a/tools/cmake/build.cmake b/tools/cmake/build.cmake index 265aaafbf6..e467a06700 100644 --- a/tools/cmake/build.cmake +++ b/tools/cmake/build.cmake @@ -612,8 +612,7 @@ endmacro() function(idf_build_executable elf) # Set additional link flags for the executable idf_build_get_property(link_options LINK_OPTIONS) - # Using LINK_LIBRARIES here instead of LINK_OPTIONS, as the latter is not in CMake 3.5. - set_property(TARGET ${elf} APPEND PROPERTY LINK_LIBRARIES "${link_options}") + set_property(TARGET ${elf} APPEND PROPERTY LINK_OPTIONS "${link_options}") # Propagate link dependencies from component library targets to the executable idf_build_get_property(link_depends __LINK_DEPENDS) diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index 99230900ee..1604b9840d 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -524,8 +524,12 @@ 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} PRIVATE "-Wl,--cref" "-Wl,--defsym=IDF_TARGET_${idf_target}=0" - "-Wl,--Map=\"${mapfile}\"") + # Add cross-reference table to the map file + target_link_options(${project_elf} PRIVATE "-Wl,--cref") + # Add this symbol as a hint for idf_size.py to guess the target name + target_link_options(${project_elf} PRIVATE "-Wl,--defsym=IDF_TARGET_${idf_target}=0") + # Enable map file output + target_link_options(${project_elf} PRIVATE "-Wl,--Map=${mapfile}") unset(idf_target) endif() diff --git a/tools/cmake/utilities.cmake b/tools/cmake/utilities.cmake index b035f278c7..9d1a301b9f 100644 --- a/tools/cmake/utilities.cmake +++ b/tools/cmake/utilities.cmake @@ -152,24 +152,18 @@ function(target_linker_script target deptype scriptfiles) get_filename_component(search_dir "${abs_script}" DIRECTORY) get_filename_component(scriptname "${abs_script}" NAME) - if(deptype STREQUAL INTERFACE OR deptype STREQUAL PUBLIC) - get_target_property(link_libraries "${target}" INTERFACE_LINK_LIBRARIES) - else() - get_target_property(link_libraries "${target}" LINK_LIBRARIES) - endif() - - list(FIND "${link_libraries}" "-L \"${search_dir}\"" found_search_dir) - if(found_search_dir EQUAL "-1") # not already added as a search path - target_link_libraries("${target}" "${deptype}" "-L \"${search_dir}\"") - endif() - - target_link_libraries("${target}" "${deptype}" "-T ${scriptname}") + target_link_directories("${target}" "${deptype}" ${search_dir}) + # Regarding the usage of SHELL, see + # https://cmake.org/cmake/help/latest/command/target_link_options.html#option-de-duplication + target_link_options("${target}" "${deptype}" "SHELL:-T ${scriptname}") # Note: In ESP-IDF, most targets are libraries and libary LINK_DEPENDS don't propagate to - # executable(s) the library is linked to. Attach manually to executable once it is known. - # - # Property INTERFACE_LINK_DEPENDS is available in CMake 3.13 which should propagate link - # dependencies. + # executable(s) the library is linked to. Since CMake 3.13, INTERFACE_LINK_DEPENDS is + # available to solve this. However, when GNU Make generator is used, this property also + # propagates INTERFACE_LINK_DEPENDS dependencies to other static libraries. + # TODO: see if this is an expected behavior and possibly report this as a bug to CMake. + # For the time being, record all linker scripts in __LINK_DEPENDS and attach manually to + # the executable target once it is known. if(NOT __PROCESS) idf_build_set_property(__LINK_DEPENDS ${abs_script} APPEND) endif()