Skip to content

Commit

Permalink
[irods#7523] CMake: Introduce new function `target_deduplicate_interf…
Browse files Browse the repository at this point in the history
…ace_properties`
  • Loading branch information
SwooshyCueb authored and alanking committed Mar 7, 2024
1 parent 3f28f5f commit d791fe2
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
162 changes: 162 additions & 0 deletions cmake/Modules/ObjectTargetHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,57 @@ functions for working with these targets.
:command:`objects_link_libraries` is provided as an equivalent to
:command:`target_link_libraries` for use when linking from object libraries.
.. command: target_deduplicate_interface_properties
.. code-block:: cmake
target_deduplicate_interface_properties(<target>
[COMPILE_DEFINITIONS]
[COMPILE_FEATURES]
[COMPILE_OPTIONS]
[INCLUDE_DIRECTORIES]
[LINK_DIRECTORIES]
[LINK_LIBRARIES]
[LINK_OPTIONS]
[PRECOMPILE_HEADERS]
[SYSTEM_INCLUDE_DIRECTORIES])
While :command`target_link_objects` will skip processing an object library
if it has already processed the object library earlier in the dependency
tree, it only does so to detect circular dependencies, and makes no further
effort to avoid duplicating entries in a target's list properties, as this
can interfere with order-sensitive flags such as ``-Wl,--as-needed``. This
can result in very messy targets files.
:command:`target_deduplicate_interface_properties` is provided to solve
this by de-duplicating list entries in the ``INTERFACE_`` properties.
``COMPILE_DEFINITIONS``
De-duplicate entries in :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`
``COMPILE_FEATURES``
De-duplicate entries in :prop_tgt:`INTERFACE_COMPILE_FEATURES`
``COMPILE_OPTIONS``
De-duplicate entries in :prop_tgt:`INTERFACE_COMPILE_OPTIONS`
``INCLUDE_DIRECTORIES``
De-duplicate entries in :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
``LINK_DIRECTORIES``
De-duplicate entries in :prop_tgt:`INTERFACE_LINK_DIRECTORIES`
``LINK_LIBRARIES``
De-duplicate entries in :prop_tgt:`INTERFACE_LINK_LIBRARIES`
``LINK_OPTIONS``
De-duplicate entries in :prop_tgt:`INTERFACE_LINK_OPTIONS`
``PRECOMPILE_HEADERS``
De-duplicate entries in :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS`
``SYSTEM_INCLUDE_DIRECTORIES``
De-duplicate entries in :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES`
#]=======================================================================]

include_guard(GLOBAL)
Expand Down Expand Up @@ -397,4 +448,115 @@ function(objects_link_libraries target)
endforeach()
endfunction()

function(target_deduplicate_interface_properties target)
if (NOT TARGET "${target}")
message(FATAL_ERROR "target_deduplicate_interface_properties called on non-target:\n ${target}\n")
endif()

foreach (arg ${ARGN})
if (arg STREQUAL "COMPILE_DEFINITIONS")
get_target_property(__target_COMPILE_DEFINITIONS "${target}" INTERFACE_COMPILE_DEFINITIONS)
if (__target_COMPILE_DEFINITIONS)
list(REMOVE_DUPLICATES __target_COMPILE_DEFINITIONS)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_COMPILE_DEFINITIONS
"${__target_COMPILE_DEFINITIONS}"
)
endif()
elseif (arg STREQUAL "COMPILE_FEATURES")
get_target_property(__target_COMPILE_FEATURES "${target}" INTERFACE_COMPILE_FEATURES)
if (__target_COMPILE_FEATURES)
list(REMOVE_DUPLICATES __target_COMPILE_FEATURES)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_COMPILE_FEATURES
"${__target_COMPILE_FEATURES}"
)
endif()
elseif (arg STREQUAL "COMPILE_OPTIONS")
get_target_property(__target_COMPILE_OPTIONS "${target}" INTERFACE_COMPILE_OPTIONS)
if (__target_COMPILE_OPTIONS)
list(REMOVE_DUPLICATES __target_COMPILE_OPTIONS)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_COMPILE_OPTIONS
"${__target_COMPILE_OPTIONS}"
)
endif()
elseif (arg STREQUAL "INCLUDE_DIRECTORIES")
get_target_property(__target_INCLUDE_DIRECTORIES "${target}" INTERFACE_INCLUDE_DIRECTORIES)
if (__target_INCLUDE_DIRECTORIES)
list(REMOVE_DUPLICATES __target_INCLUDE_DIRECTORIES)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES
"${__target_INCLUDE_DIRECTORIES}"
)
endif()
elseif (arg STREQUAL "LINK_DIRECTORIES")
get_target_property(__target_LINK_DIRECTORIES "${target}" INTERFACE_LINK_DIRECTORIES)
if (__target_LINK_DIRECTORIES)
list(REMOVE_DUPLICATES __target_LINK_DIRECTORIES)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_LINK_DIRECTORIES
"${__target_LINK_DIRECTORIES}"
)
endif()
elseif (arg STREQUAL "LINK_LIBRARIES")
get_target_property(__target_LINK_LIBRARIES "${target}" INTERFACE_LINK_LIBRARIES)
if (__target_LINK_LIBRARIES)
list(REMOVE_DUPLICATES __target_LINK_LIBRARIES)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_LINK_LIBRARIES
"${__target_LINK_LIBRARIES}"
)
endif()
elseif (arg STREQUAL "LINK_OPTIONS")
get_target_property(__target_LINK_OPTIONS "${target}" INTERFACE_LINK_OPTIONS)
if (__target_LINK_OPTIONS)
list(REMOVE_DUPLICATES __target_LINK_OPTIONS)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_LINK_OPTIONS
"${__target_LINK_OPTIONS}"
)
endif()
elseif (arg STREQUAL "PRECOMPILE_HEADERS")
get_target_property(__target_PRECOMPILE_HEADERS "${target}" INTERFACE_PRECOMPILE_HEADERS)
if (__target_PRECOMPILE_HEADERS)
list(REMOVE_DUPLICATES __target_PRECOMPILE_HEADERS)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_PRECOMPILE_HEADERS
"${__target_PRECOMPILE_HEADERS}"
)
endif()
elseif (arg STREQUAL "SYSTEM_INCLUDE_DIRECTORIES")
get_target_property(__target_SYSTEM_INCLUDE_DIRECTORIES "${target}" INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
if (__target_SYSTEM_INCLUDE_DIRECTORIES)
list(REMOVE_DUPLICATES __target_SYSTEM_INCLUDE_DIRECTORIES)
set_target_properties(
"${target}"
PROPERTIES
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES
"${__target_SYSTEM_INCLUDE_DIRECTORIES}"
)
endif()
else()
message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
endif()
endforeach()
endfunction()

cmake_policy(POP)
21 changes: 21 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ target_link_libraries(
set_property(TARGET irods_common PROPERTY VERSION ${IRODS_VERSION})
set_property(TARGET irods_common PROPERTY SOVERSION ${IRODS_VERSION})

target_deduplicate_interface_properties(
irods_common
COMPILE_DEFINITIONS
INCLUDE_DIRECTORIES
LINK_LIBRARIES
)

add_dependencies(all-lib irods_common)
install(
TARGETS
Expand Down Expand Up @@ -85,6 +92,13 @@ target_link_libraries(
set_property(TARGET irods_client PROPERTY VERSION ${IRODS_VERSION})
set_property(TARGET irods_client PROPERTY SOVERSION ${IRODS_VERSION})

target_deduplicate_interface_properties(
irods_client
COMPILE_DEFINITIONS
INCLUDE_DIRECTORIES
LINK_LIBRARIES
)

add_dependencies(all-lib irods_client)
install(
TARGETS
Expand Down Expand Up @@ -136,6 +150,13 @@ target_link_libraries(
)
target_compile_definitions(RodsAPIs PRIVATE ${IRODS_COMPILE_DEFINITIONS_PRIVATE})

target_deduplicate_interface_properties(
RodsAPIs
COMPILE_DEFINITIONS
INCLUDE_DIRECTORIES
LINK_LIBRARIES
)

add_dependencies(all-lib RodsAPIs)
install(
TARGETS
Expand Down
7 changes: 7 additions & 0 deletions lib/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ irods_plugin_dependencies
set_property(TARGET irods_plugin_dependencies PROPERTY VERSION ${IRODS_VERSION})
set_property(TARGET irods_plugin_dependencies PROPERTY SOVERSION ${IRODS_VERSION})

target_deduplicate_interface_properties(
irods_plugin_dependencies
COMPILE_DEFINITIONS
INCLUDE_DIRECTORIES
LINK_LIBRARIES
)

install(
TARGETS
irods_plugin_dependencies
Expand Down
7 changes: 7 additions & 0 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ target_link_libraries(
set_property(TARGET irods_server PROPERTY VERSION ${IRODS_VERSION})
set_property(TARGET irods_server PROPERTY SOVERSION ${IRODS_VERSION})

target_deduplicate_interface_properties(
irods_server
COMPILE_DEFINITIONS
INCLUDE_DIRECTORIES
LINK_LIBRARIES
)

install(
TARGETS
irods_server
Expand Down

0 comments on commit d791fe2

Please sign in to comment.