From 47b5a506885d1271cdf69388369714f5313a419b Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Wed, 10 Oct 2018 20:16:04 -0600 Subject: [PATCH] CMake: Don't always override target_link_libraries() Previously, we would override target_link_libraries() to support object "linking" since this wasn't supported in versions of CMake before 3.12. Now that 3.12 is released, we only do it for versions of CMake before that release. --- cmake/macros/BuildMetalib.cmake | 122 ++++++++++++++++---------------- 1 file changed, 62 insertions(+), 60 deletions(-) diff --git a/cmake/macros/BuildMetalib.cmake b/cmake/macros/BuildMetalib.cmake index 2b69f530f0..0fa9675825 100644 --- a/cmake/macros/BuildMetalib.cmake +++ b/cmake/macros/BuildMetalib.cmake @@ -10,70 +10,72 @@ # # Overrides CMake's target_link_libraries() to support "linking" object # libraries. This is a partial reimplementation of CMake commit dc38970f83, -# which as of this writing has not yet landed in any release. +# which is only available in CMake 3.12+ # -function(target_link_libraries target) - get_target_property(target_type "${target}" TYPE) - if(NOT target_type STREQUAL "OBJECT_LIBRARY") - _target_link_libraries("${target}" ${ARGN}) - return() - endif() - - foreach(library ${ARGN}) - # This is a quick and dirty regex to tell targets apart from other stuff. - # It just checks if it's alphanumeric and starts with p3/panda. - if(library MATCHES "^(PKG::|p3|panda)[A-Za-z0-9]*$") - # We need to add "library"'s include directories to "target" - # (and transitively to INTERFACE_INCLUDE_DIRECTORIES so further - # dependencies will work) - set(include_directories "$") - set_property(TARGET "${target}" APPEND PROPERTY INCLUDE_DIRECTORIES "${include_directories}") - set_property(TARGET "${target}" APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${include_directories}") - - # And for INTERFACE_COMPILE_DEFINITIONS as well - set(compile_definitions "$") - set_property(TARGET "${target}" APPEND PROPERTY COMPILE_DEFINITIONS "${compile_definitions}") - - # Build up some generator expressions for determining whether `library` - # is a component library or not. - if(library MATCHES ".*::.*") - # "::" messes up CMake's genex parser; fortunately, a library whose - # name contains that is either an interface library or alias, and - # definitely not a component - set(is_component 0) - set(name_of_component "") - set(name_of_non_component "${library}") - else() - set(is_component "$") - - # CMake complains if we lookup IS_COMPONENT on an INTERFACE library :( - set(is_object "$,OBJECT_LIBRARY>") - set(is_component "$>") - - set(name_of_component "$<${is_component}:${library}>") - set(name_of_non_component "$<$:${library}>") - endif() - - # Libraries are only linked transitively if they aren't components. - set_property(TARGET "${target}" APPEND PROPERTY - INTERFACE_LINK_LIBRARIES "${name_of_non_component}") - - # Also build with the same BUILDING_ macros, because these will all end - # up in the same library. - set(compile_definitions "$") - set_property(TARGET "${target}" APPEND PROPERTY - COMPILE_DEFINITIONS "$<${is_component}:${compile_definitions}>") - else() - # This is a file path to an out-of-tree library - this needs to be - # recorded so that the metalib can link them. (They aren't needed at - # all for the object libraries themselves, so they don't have to work - # transitively.) - set_property(TARGET "${target}" APPEND PROPERTY INTERFACE_LINK_LIBRARIES "${library}") +if(CMAKE_VERSION VERSION_LESS "3.12") + function(target_link_libraries target) + get_target_property(target_type "${target}" TYPE) + if(NOT target_type STREQUAL "OBJECT_LIBRARY") + _target_link_libraries("${target}" ${ARGN}) + return() endif() - endforeach(library) + foreach(library ${ARGN}) + # This is a quick and dirty regex to tell targets apart from other stuff. + # It just checks if it's alphanumeric and starts with p3/panda. + if(library MATCHES "^(PKG::|p3|panda)[A-Za-z0-9]*$") + # We need to add "library"'s include directories to "target" + # (and transitively to INTERFACE_INCLUDE_DIRECTORIES so further + # dependencies will work) + set(include_directories "$") + set_property(TARGET "${target}" APPEND PROPERTY INCLUDE_DIRECTORIES "${include_directories}") + set_property(TARGET "${target}" APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${include_directories}") -endfunction(target_link_libraries) + # And for INTERFACE_COMPILE_DEFINITIONS as well + set(compile_definitions "$") + set_property(TARGET "${target}" APPEND PROPERTY COMPILE_DEFINITIONS "${compile_definitions}") + + # Build up some generator expressions for determining whether `library` + # is a component library or not. + if(library MATCHES ".*::.*") + # "::" messes up CMake's genex parser; fortunately, a library whose + # name contains that is either an interface library or alias, and + # definitely not a component + set(is_component 0) + set(name_of_component "") + set(name_of_non_component "${library}") + else() + set(is_component "$") + + # CMake complains if we lookup IS_COMPONENT on an INTERFACE library :( + set(is_object "$,OBJECT_LIBRARY>") + set(is_component "$>") + + set(name_of_component "$<${is_component}:${library}>") + set(name_of_non_component "$<$:${library}>") + endif() + + # Libraries are only linked transitively if they aren't components. + set_property(TARGET "${target}" APPEND PROPERTY + INTERFACE_LINK_LIBRARIES "${name_of_non_component}") + + # Also build with the same BUILDING_ macros, because these will all end + # up in the same library. + set(compile_definitions "$") + set_property(TARGET "${target}" APPEND PROPERTY + COMPILE_DEFINITIONS "$<${is_component}:${compile_definitions}>") + else() + # This is a file path to an out-of-tree library - this needs to be + # recorded so that the metalib can link them. (They aren't needed at + # all for the object libraries themselves, so they don't have to work + # transitively.) + set_property(TARGET "${target}" APPEND PROPERTY INTERFACE_LINK_LIBRARIES "${library}") + endif() + + endforeach(library) + + endfunction(target_link_libraries) +endif() # # Function: add_component_library(target [SYMBOL building_symbol]