Selaa lähdekoodia

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.
Ivan Grokhotkov 3 vuotta sitten
vanhempi
sitoutus
237b2ce40c

+ 5 - 5
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)

+ 3 - 3
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'

+ 1 - 1
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)

+ 1 - 1
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 $<TARGET_FILE:${esp_phy_lib}> lib802154.a libphy.a libbtbb.a
                             $<TARGET_FILE:${esp_phy_lib}>)

+ 1 - 2
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)

+ 6 - 2
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()
 

+ 10 - 16
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()