1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-06-08 00:02:02 +03:00
libssh2/cmake/libssh2-config.cmake.in
Viktor Szakats ac80041852
cmake: IMPORTED target improvements and fixes
- fix `add_subdirectory` builds for old CMake versions.
- libssh2-config.cmake: fix to set CMP0099 for CMake 3.17+ only.
- libssh2-config.cmake: generalize code to support any number of deps.
  (mainly to sync with curl.)
- libssh2-config.cmake: bind dependencies to the static libssh2 only.

Follow-up to a0d8529b08831a1acf50d4afc0fde24a1716862f #1571
Follow-up to df0563a85732fd9a33bbb116d8c07ca18ba6af38 #1535

Closes #1581
2025-04-27 13:46:30 +02:00

63 lines
2.2 KiB
CMake

# Copyright (C) The libssh2 project and its contributors.
# SPDX-License-Identifier: BSD-3-Clause
option(LIBSSH2_USE_PKGCONFIG "Enable pkg-config to detect @PROJECT_NAME@ dependencies. Default: @LIBSSH2_USE_PKGCONFIG@" "@LIBSSH2_USE_PKGCONFIG@")
include(CMakeFindDependencyMacro)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_MODULE_PATH})
set(_libs "")
if("@CRYPTO_BACKEND@" STREQUAL "OpenSSL")
find_dependency(OpenSSL)
elseif("@CRYPTO_BACKEND@" STREQUAL "wolfSSL")
find_dependency(WolfSSL)
list(APPEND _libs libssh2::wolfssl)
elseif("@CRYPTO_BACKEND@" STREQUAL "Libgcrypt")
find_dependency(Libgcrypt)
list(APPEND _libs libssh2::libgcrypt)
elseif("@CRYPTO_BACKEND@" STREQUAL "mbedTLS")
find_dependency(MbedTLS)
list(APPEND _libs libssh2::mbedcrypto)
endif()
if(@ZLIB_FOUND@)
find_dependency(ZLIB)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.11 AND CMAKE_VERSION VERSION_LESS 3.18)
set_target_properties(@PROJECT_NAME@::@LIB_SELECTED@ PROPERTIES IMPORTED_GLOBAL TRUE)
endif()
# Alias for either shared or static library
if(NOT TARGET @PROJECT_NAME@::@LIB_NAME@)
add_library(@PROJECT_NAME@::@LIB_NAME@ ALIAS @PROJECT_NAME@::@LIB_SELECTED@)
endif()
# Compatibility alias
if(NOT TARGET Libssh2::@LIB_NAME@)
add_library(Libssh2::@LIB_NAME@ ALIAS @PROJECT_NAME@::@LIB_SELECTED@)
endif()
if(TARGET @PROJECT_NAME@::@LIB_STATIC@)
# CMake before CMP0099 (CMake 3.17 2020-03-20) did not propagate libdirs to
# targets. It expected libs to have an absolute filename. As a workaround,
# manually apply dependency libdirs, for CMake consumers without this policy.
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
cmake_policy(GET CMP0099 _has_CMP0099) # https://cmake.org/cmake/help/latest/policy/CMP0099.html
endif()
if(NOT _has_CMP0099 AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.13 AND _libs)
set(_libdirs "")
foreach(_lib IN LISTS _libs)
get_target_property(_libdir "${_lib}" INTERFACE_LINK_DIRECTORIES)
if(_libdir)
list(APPEND _libdirs "${_libdir}")
endif()
endforeach()
if(_libdirs)
target_link_directories(@PROJECT_NAME@::@LIB_STATIC@ INTERFACE ${_libdirs})
endif()
endif()
endif()