diff --git a/cmake/GzRelocatableBinaries.cmake b/cmake/GzRelocatableBinaries.cmake index 36986b45..f4f64c6f 100644 --- a/cmake/GzRelocatableBinaries.cmake +++ b/cmake/GzRelocatableBinaries.cmake @@ -74,19 +74,12 @@ macro(gz_add_get_install_prefix_impl) "gz_add_get_install_prefix_impl: missing parameter OVERRIDE_INSTALL_PREFIX_ENV_VARIABLE") endif() - if(NOT TARGET ${PROJECT_LIBRARY_TARGET_NAME}) message(FATAL_ERROR "Target ${PROJECT_LIBRARY_TARGET_NAME} required by gz_add_get_install_prefix_impl\n" "does not exist.") endif() - if(NOT TARGET ${DL_TARGET}) - message(FATAL_ERROR - "gz_add_get_install_prefix_impl called without DL_TARGET defined,\n" - "please add gz_find_package(DL) if you want to use gz_add_get_install_prefix_impl.") - endif() - get_target_property(target_type ${PROJECT_LIBRARY_TARGET_NAME} TYPE) if(NOT (target_type STREQUAL "STATIC_LIBRARY" OR target_type STREQUAL "MODULE_LIBRARY" OR target_type STREQUAL "SHARED_LIBRARY")) message(FATAL_ERROR "gz_add_get_install_prefix_impl: library ${_library} is of unsupported type ${target_type}") @@ -208,7 +201,16 @@ endif() # Add cpp to library target_sources(${PROJECT_LIBRARY_TARGET_NAME} PRIVATE ${gz_add_get_install_prefix_impl_GENERATED_CPP}) +# Only link DL in the case that it is needed. +if ((target_type STREQUAL "MODULE_LIBRARY" OR target_type STREQUAL "SHARED_LIBRARY") AND GZ_ENABLE_RELOCATABLE_INSTALL) + if(NOT TARGET ${DL_TARGET}) + message(FATAL_ERROR + "gz_add_get_install_prefix_impl called without DL_TARGET defined,\n" + "please add gz_find_package(DL) if you want to use gz_add_get_install_prefix_impl.") + endif() + # Link DL_TARGET that provides dladdr target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME} PRIVATE ${DL_TARGET}) +endif() endmacro()