]> granicus.if.org Git - esp-idf/commitdiff
cmake: Add link-time dependencies for linker script files
authorAngus Gratton <angus@espressif.com>
Thu, 1 Mar 2018 06:02:08 +0000 (17:02 +1100)
committerAngus Gratton <gus@projectgus.com>
Sun, 29 Apr 2018 23:59:20 +0000 (09:59 +1000)
Requires some hackery around limitations in CMake's LINK_DEPENDS

components/bootloader/subproject/CMakeLists.txt
components/esp32/CMakeLists.txt
tools/cmake/components.cmake
tools/cmake/utilities.cmake

index 9d99e6e0c72738105a8f51c6a2e102a609b9cbc5..5ac887a4046a0e9c621b26c99cea173cecf995bb 100644 (file)
@@ -14,8 +14,7 @@ set(MAIN_SRCS main/bootloader_start.c)
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 project(bootloader)
 
-target_link_libraries(bootloader.elf "-L ${CMAKE_CURRENT_SOURCE_DIR}/main")
-target_link_libraries(bootloader.elf "-T esp32.bootloader.ld")
-target_link_libraries(bootloader.elf "-T esp32.bootloader.rom.ld")
+target_linker_script(bootloader.elf "main/esp32.bootloader.ld")
+target_linker_script(bootloader.elf "main/esp32.bootloader.rom.ld")
 target_link_libraries(bootloader.elf "${BOOTLOADER_LINKER_ARGS}")
 target_link_libraries(bootloader.elf gcc)
index 77d4fd0b4004f8e287b380433a40eb446bed2601..1172dda7fb48d8ed45d525db2e6a63ff0996f988 100644 (file)
@@ -24,24 +24,24 @@ else()
     if(NOT CONFIG_NO_BLOBS)
         target_link_libraries(esp32 coexist core espnow net80211 phy pp rtc smartconfig wpa2 wpa wps)
     endif()
-    target_link_libraries(esp32 "-L ${CMAKE_CURRENT_SOURCE_DIR}/ld")
-    target_link_libraries(esp32 "-T ${CMAKE_CURRENT_BINARY_DIR}/esp32_out.ld")
-    target_link_libraries(esp32 "-T esp32.common.ld")
-    target_link_libraries(esp32 "-T esp32.rom.ld")
-    target_link_libraries(esp32 "-T esp32.peripherals.ld")
+    target_linker_script(esp32 "${CMAKE_CURRENT_BINARY_DIR}/esp32_out.ld")
+
+    target_linker_script(esp32 "ld/esp32.common.ld")
+    target_linker_script(esp32 "ld/esp32.rom.ld")
+    target_linker_script(esp32 "ld/esp32.peripherals.ld")
 
     if(CONFIG_SPIRAM_CACHE_WORKAROUND)
         add_compile_options(-mfix-esp32-psram-cache-issue)
     else()
-        target_link_libraries(esp32 "-T esp32.rom.spiram_incompatible_fns.ld")
+        target_linker_script(esp32 "ld/esp32.rom.spiram_incompatible_fns.ld")
     endif()
 
     if(CONFIG_NEWLIB_NANO_FORMAT)
-        target_link_libraries(esp32 "-T esp32.rom.nanofmt.ld")
+        target_linker_script(esp32 "ld/esp32.rom.nanofmt.ld")
     endif()
 
     if(NOT CONFIG_SPI_FLASH_ROM_DRIVER_PATCH)
-        target_link_libraries(esp32 "-T esp32.rom.spiflash.ld")
+        target_linker_script(esp32 "ld/esp32.rom.spiflash.ld")
     endif()
 
     target_link_libraries(esp32 "${CMAKE_CURRENT_SOURCE_DIR}/libhal.a")
index 8c6518703586f90aea7775fc20d3eafb3c3314bc..4334ffee7df903b9e6aaa6c877b58071cb6f5521 100644 (file)
@@ -161,6 +161,24 @@ function(components_finish_registration)
         endif()
     endforeach()
 
+    # Add each component library's link-time dependencies (which are otherwise ignored) to the executable
+    # LINK_DEPENDS in order to trigger a re-link when needed (on Ninja/Makefile generators at least).
+    # (maybe this should probably be something CMake does, but it doesn't do it...)
+    foreach(component ${COMPONENTS})
+        if(TARGET ${component})
+            get_target_property(imported ${component} IMPORTED)
+            get_target_property(type ${component} TYPE)
+            if(NOT imported)
+                if(${type} STREQUAL STATIC_LIBRARY OR ${type} STREQUAL EXECUTABLE)
+                    get_target_property(link_depends "${component}" LINK_DEPENDS)
+                    if(link_depends)
+                        set_property(TARGET ${CMAKE_PROJECT_NAME}.elf APPEND PROPERTY LINK_DEPENDS "${link_depends}")
+                    endif()
+                endif()
+            endif()
+        endif()
+    endforeach()
+
     # Embedded binary & text files
     spaces2list(COMPONENT_EMBED_FILES)
     foreach(embed_src ${COMPONENT_EMBED_FILES})
index 0415e64978672109b6fbf7f7ca591d43b8e2edee..ec6323d90f724c1fb015d2d8d13cd6551958928d 100644 (file)
@@ -145,3 +145,27 @@ function(file_append_line file line)
     endif()
     file(APPEND "${file}" "${line}${line_ending}")
 endfunction()
+
+# Add a linker script to the target, including a link-time dependency
+#
+# Automatically adds a -L search path for the containing directory (if found),
+# and then adds -T with the filename only. This allows INCLUDE directives to be
+# used to include other linker scripts in the same directory.
+function(target_linker_script target scriptfile)
+    get_filename_component(abs_script "${scriptfile}" ABSOLUTE)
+
+    get_filename_component(search_dir "${abs_script}" DIRECTORY)
+    get_filename_component(scriptname "${abs_script}" NAME)
+
+    get_target_property(link_libraries "${target}" LINK_LIBRARIES)
+    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}" "-L ${search_dir}")
+    endif()
+
+    target_link_libraries("${target}" "-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. This is done manually in components.cmake.
+    set_property(TARGET "${target}" APPEND PROPERTY LINK_DEPENDS "${abs_script}")
+endfunction()