From 3882e48e8adf6cd1f5d9e42be732f2253ba99db0 Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Tue, 4 Jun 2019 19:05:33 +0800 Subject: [PATCH] cmake: use new signature form of target_link_library to link components !4452 used setting LINK_LIBRARIES and INTERFACE_LINK_LIBRARIES to link components built under ESP-IDF build system. However, LINK_LIBRARIES does not produce behavior same as linking PRIVATE. This MR uses the new signature for target_link_libraries directly instead. This also moves setting dependencies during component registration rather than after all components have been processed. The consequence is that internally, components have to use the new signature form as well. This does not affect linking the components to external targets, such as with idf_as_lib example. This only affects linking additional libraries to ESP-IDF libraries outside component processing (after idf_build_process), which is not even possible for CMake") - target_link_libraries(${COMPONENT_LIB} extra) + target_link_libraries(${COMPONENT_LIB} PUBLIC extra) else() - target_link_libraries(${COMPONENT_LIB} ${LIBC} ${LIBM} gcc) + target_link_libraries(${COMPONENT_LIB} PUBLIC ${LIBC} ${LIBM} gcc) endif() set_source_files_properties(heap.c PROPERTIES COMPILE_FLAGS -fno-builtin) if(EXTRA_LINK_FLAGS) - target_link_libraries(${COMPONENT_LIB} "${EXTRA_LINK_FLAGS}") + target_link_libraries(${COMPONENT_LIB} INTERFACE "${EXTRA_LINK_FLAGS}") endif() diff --git a/components/ulp/project_include.cmake b/components/ulp/project_include.cmake index b5ac4c54e..95996b32f 100644 --- a/components/ulp/project_include.cmake +++ b/components/ulp/project_include.cmake @@ -61,7 +61,7 @@ function(ulp_embed_binary app_name s_sources exp_dep_srcs) add_dependencies(${COMPONENT_LIB} ${app_name}_artifacts) - target_linker_script(${COMPONENT_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${app_name}.ld) + target_linker_script(${COMPONENT_LIB} INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${app_name}.ld) target_add_binary_data(${COMPONENT_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${app_name}/${app_name}.bin BINARY) endif() endfunction() \ No newline at end of file diff --git a/components/vfs/CMakeLists.txt b/components/vfs/CMakeLists.txt index 1d8fb06ec..0b2f640ca 100644 --- a/components/vfs/CMakeLists.txt +++ b/components/vfs/CMakeLists.txt @@ -6,4 +6,4 @@ register_component() # Some newlib syscalls are implemented in vfs.c, make sure these are always # seen by the linker -target_link_libraries(${COMPONENT_LIB} "-u vfs_include_syscalls_impl") +target_link_libraries(${COMPONENT_LIB} INTERFACE "-u vfs_include_syscalls_impl") diff --git a/components/xtensa/CMakeLists.txt b/components/xtensa/CMakeLists.txt index 9044f14ae..cb9c9ce49 100644 --- a/components/xtensa/CMakeLists.txt +++ b/components/xtensa/CMakeLists.txt @@ -7,4 +7,4 @@ set(COMPONENT_PRIV_REQUIRES soc) register_component() -target_link_libraries(${COMPONENT_LIB} "${CMAKE_CURRENT_SOURCE_DIR}/${IDF_TARGET}/libhal.a") +target_link_libraries(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${IDF_TARGET}/libhal.a") diff --git a/examples/build_system/cmake/import_lib/main/CMakeLists.txt b/examples/build_system/cmake/import_lib/main/CMakeLists.txt index 8111cbe6a..fb136b8b9 100644 --- a/examples/build_system/cmake/import_lib/main/CMakeLists.txt +++ b/examples/build_system/cmake/import_lib/main/CMakeLists.txt @@ -23,4 +23,4 @@ endfunction() add_subdirectory(lib/tinyxml2) # Link tinyxml2 to main component -target_link_libraries(${COMPONENT_LIB} tinyxml2) +target_link_libraries(${COMPONENT_LIB} PUBLIC tinyxml2) diff --git a/tools/cmake/build.cmake b/tools/cmake/build.cmake index 6cf9a73b5..84f6f154f 100644 --- a/tools/cmake/build.cmake +++ b/tools/cmake/build.cmake @@ -436,29 +436,8 @@ endmacro() # generating additional binary files, generating files related to flashing, etc.) function(idf_build_executable elf) # Propagate link dependencies from component library targets to the executable - idf_build_get_property(build_components BUILD_COMPONENTS) - foreach(build_component ${build_components}) - get_target_property(type ${build_component} TYPE) - if(type STREQUAL "INTERFACE_LIBRARY") - get_target_property(iface_link_depends ${build_component} INTERFACE_LINK_DEPENDS) - else() - get_target_property(link_depends ${build_component} LINK_DEPENDS) - get_target_property(iface_link_depends ${build_component} INTERFACE_LINK_DEPENDS) - endif() - if(iface_link_depends) - list(APPEND _link_depends ${iface_link_depends}) - endif() - if(link_depends) - list(APPEND _link_depends ${link_depends}) - endif() - endforeach() - - idf_build_get_property(link_depends LINK_DEPENDS) - if(link_depends) - list(APPEND _link_depends ${link_depends}) - endif() - - set_property(TARGET ${elf} APPEND PROPERTY LINK_DEPENDS "${_link_depends}") + idf_build_get_property(link_depends __LINK_DEPENDS) + set_property(TARGET ${elf} APPEND PROPERTY LINK_DEPENDS "${link_depends}") # Set the EXECUTABLE_NAME and EXECUTABLE properties since there are generator expression # from components that depend on it diff --git a/tools/cmake/component.cmake b/tools/cmake/component.cmake index 6caca93a7..22b3528ec 100644 --- a/tools/cmake/component.cmake +++ b/tools/cmake/component.cmake @@ -288,6 +288,39 @@ macro(__component_check_target) endif() endmacro() +# __component_set_dependencies, __component_set_all_dependencies +# +# Links public and private requirements for the currently processed component +macro(__component_set_dependencies reqs type) + foreach(req ${reqs}) + if(req IN_LIST build_component_targets) + __component_get_property(req_lib ${req} COMPONENT_LIB) + target_link_libraries(${component_lib} ${type} ${req_lib}) + endif() + endforeach() +endmacro() + +macro(__component_set_all_dependencies) + __component_get_property(type ${component_target} COMPONENT_TYPE) + idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS) + + if(NOT type STREQUAL CONFIG_ONLY) + __component_get_property(reqs ${component_target} __REQUIRES) + __component_set_dependencies("${reqs}" PUBLIC) + + __component_get_property(priv_reqs ${component_target} __PRIV_REQUIRES) + __component_set_dependencies("${priv_reqs}" PRIVATE) + else() + __component_get_property(reqs ${component_target} __REQUIRES) + foreach(req ${reqs}) + if(req IN_LIST build_component_targets) + __component_get_property(req_lib ${req} COMPONENT_LIB) + target_link_libraries(${component_lib} INTERFACE ${req_lib}) + endif() + endforeach() + endif() +endmacro() + # idf_component_get_property # # @brief Retrieve the value of the specified component property @@ -331,6 +364,7 @@ function(idf_component_set_property component property val) endif() endfunction() + # idf_component_register # # @brief Register a component to the build, creating component library targets etc. @@ -431,6 +465,9 @@ function(idf_component_register) __ldgen_add_fragment_files("${__LDFRAGMENTS}") endif() + # Set dependencies + __component_set_all_dependencies() + # Add the component to built components idf_build_set_property(__BUILD_COMPONENTS ${component_lib} APPEND) idf_build_set_property(BUILD_COMPONENTS ${component_alias} APPEND) diff --git a/tools/cmake/ldgen.cmake b/tools/cmake/ldgen.cmake index b8cfaad49..27db72ae3 100644 --- a/tools/cmake/ldgen.cmake +++ b/tools/cmake/ldgen.cmake @@ -75,5 +75,5 @@ function(__ldgen_process_template template output) get_filename_component(_name ${output} NAME) add_custom_target(__ldgen_output_${_name} DEPENDS ${output}) add_dependencies(__idf_build_target __ldgen_output_${_name}) - idf_build_set_property(LINK_DEPENDS ${output} APPEND) + idf_build_set_property(__LINK_DEPENDS ${output} APPEND) endfunction() \ No newline at end of file diff --git a/tools/cmake/utilities.cmake b/tools/cmake/utilities.cmake index f5f7f9693..83403d9ce 100644 --- a/tools/cmake/utilities.cmake +++ b/tools/cmake/utilities.cmake @@ -130,7 +130,7 @@ endfunction() # 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 scriptfiles) +function(target_linker_script target deptype scriptfiles) cmake_parse_arguments(_ "" "PROCESS" "" ${ARGN}) foreach(scriptfile ${scriptfiles}) get_filename_component(abs_script "${scriptfile}" ABSOLUTE) @@ -145,12 +145,7 @@ function(target_linker_script target scriptfiles) get_filename_component(search_dir "${abs_script}" DIRECTORY) get_filename_component(scriptname "${abs_script}" NAME) - get_target_property(type ${target} TYPE) - if(type STREQUAL "INTERFACE_LIBRARY") - set(is_interface "INTERFACE") - endif() - - if(is_interface) + 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) @@ -158,10 +153,10 @@ function(target_linker_script target scriptfiles) 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}" "${is_interface}" "-L ${search_dir}") + target_link_libraries("${target}" "${deptype}" "-L ${search_dir}") endif() - target_link_libraries("${target}" "${is_interface}" "-T ${scriptname}") + target_link_libraries("${target}" "${deptype}" "-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. @@ -169,11 +164,7 @@ function(target_linker_script target scriptfiles) # Property INTERFACE_LINK_DEPENDS is available in CMake 3.13 which should propagate link # dependencies. if(NOT __PROCESS) - if(is_interface) - set_property(TARGET ${target} APPEND PROPERTY INTERFACE_LINK_DEPENDS ${abs_script}) - else() - set_property(TARGET ${target} APPEND PROPERTY LINK_DEPENDS ${abs_script}) - endif() + idf_build_set_property(__LINK_DEPENDS ${abs_script} APPEND) endif() endforeach() endfunction()