1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-31 00:03:07 +03:00

cmake: use imported targets for OpenSSL and zlib

Imported targets are highly preferred over the individual variables
for includes and libs because they will be used in a coherent way
and any spelling mistakes or unavailability won't go unnoticed.

Also it will prevent bugs like conan-io/conan-center-index#16900
or using mismatching header/libs combinations.

Signed-off-by: Gregor Jasny <gjasny@googlemail.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Gregor Jasny
2023-04-05 16:36:19 +02:00
committed by Jakub Jelen
parent 486d2289fa
commit 6ad455a8ac
7 changed files with 8 additions and 56 deletions

View File

@ -1,5 +1,4 @@
cmake_minimum_required(VERSION 3.12.0) cmake_minimum_required(VERSION 3.12.0)
cmake_policy(SET CMP0048 NEW)
# Specify search path for CMake modules to be loaded by include() # Specify search path for CMake modules to be loaded by include()
# and find_package() # and find_package()
@ -53,11 +52,6 @@ elseif(WITH_MBEDTLS)
find_package(MbedTLS REQUIRED) find_package(MbedTLS REQUIRED)
else() else()
find_package(OpenSSL 1.1.1 REQUIRED) find_package(OpenSSL 1.1.1 REQUIRED)
# On CMake < 3.16, OPENSSL_CRYPTO_LIBRARIES is usually a synonym for OPENSSL_CRYPTO_LIBRARY, but is not defined
# when building on Windows outside of Cygwin. We provide the synonym here, if FindOpenSSL didn't define it already.
if (NOT DEFINED OPENSSL_CRYPTO_LIBRARIES)
set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY})
endif (NOT DEFINED OPENSSL_CRYPTO_LIBRARIES)
endif() endif()
if (UNIT_TESTING) if (UNIT_TESTING)

View File

@ -76,53 +76,32 @@ if (WIN32)
endif (WIN32) endif (WIN32)
if (OPENSSL_FOUND) if (OPENSSL_FOUND)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR}) set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto)
check_include_file(openssl/des.h HAVE_OPENSSL_DES_H) check_include_file(openssl/des.h HAVE_OPENSSL_DES_H)
if (NOT HAVE_OPENSSL_DES_H) if (NOT HAVE_OPENSSL_DES_H)
message(FATAL_ERROR "Could not detect openssl/des.h") message(FATAL_ERROR "Could not detect openssl/des.h")
endif() endif()
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
check_include_file(openssl/aes.h HAVE_OPENSSL_AES_H) check_include_file(openssl/aes.h HAVE_OPENSSL_AES_H)
if (NOT HAVE_OPENSSL_AES_H) if (NOT HAVE_OPENSSL_AES_H)
message(FATAL_ERROR "Could not detect openssl/aes.h") message(FATAL_ERROR "Could not detect openssl/aes.h")
endif() endif()
if (WITH_BLOWFISH_CIPHER) if (WITH_BLOWFISH_CIPHER)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
check_include_file(openssl/blowfish.h HAVE_OPENSSL_BLOWFISH_H) check_include_file(openssl/blowfish.h HAVE_OPENSSL_BLOWFISH_H)
endif() endif()
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
check_include_file(openssl/ecdh.h HAVE_OPENSSL_ECDH_H) check_include_file(openssl/ecdh.h HAVE_OPENSSL_ECDH_H)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
check_include_file(openssl/ec.h HAVE_OPENSSL_EC_H) check_include_file(openssl/ec.h HAVE_OPENSSL_EC_H)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
check_include_file(openssl/ecdsa.h HAVE_OPENSSL_ECDSA_H) check_include_file(openssl/ecdsa.h HAVE_OPENSSL_ECDSA_H)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_CRYPTO_LIBRARIES})
check_function_exists(EVP_KDF_CTX_new_id HAVE_OPENSSL_EVP_KDF_CTX_NEW_ID) check_function_exists(EVP_KDF_CTX_new_id HAVE_OPENSSL_EVP_KDF_CTX_NEW_ID)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_CRYPTO_LIBRARIES})
check_function_exists(EVP_KDF_CTX_new HAVE_OPENSSL_EVP_KDF_CTX_NEW) check_function_exists(EVP_KDF_CTX_new HAVE_OPENSSL_EVP_KDF_CTX_NEW)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_CRYPTO_LIBRARIES})
check_function_exists(FIPS_mode HAVE_OPENSSL_FIPS_MODE) check_function_exists(FIPS_mode HAVE_OPENSSL_FIPS_MODE)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_CRYPTO_LIBRARIES})
check_function_exists(RAND_priv_bytes HAVE_OPENSSL_RAND_PRIV_BYTES) check_function_exists(RAND_priv_bytes HAVE_OPENSSL_RAND_PRIV_BYTES)
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_CRYPTO_LIBRARIES})
check_function_exists(EVP_chacha20 HAVE_OPENSSL_EVP_CHACHA20) check_function_exists(EVP_chacha20 HAVE_OPENSSL_EVP_CHACHA20)
unset(CMAKE_REQUIRED_INCLUDES)
unset(CMAKE_REQUIRED_LIBRARIES) unset(CMAKE_REQUIRED_LIBRARIES)
endif() endif()

View File

@ -8,17 +8,9 @@ set(LIBSSH_LINK_LIBRARIES
${LIBSSH_REQUIRED_LIBRARIES} ${LIBSSH_REQUIRED_LIBRARIES}
) )
if (OPENSSL_CRYPTO_LIBRARIES) if (TARGET OpenSSL::Crypto)
set(LIBSSH_PRIVATE_INCLUDE_DIRS list(APPEND LIBSSH_LINK_LIBRARIES OpenSSL::Crypto)
${LIBSSH_PRIVATE_INCLUDE_DIRS} endif ()
${OPENSSL_INCLUDE_DIR}
)
set(LIBSSH_LINK_LIBRARIES
${LIBSSH_LINK_LIBRARIES}
${OPENSSL_CRYPTO_LIBRARIES}
)
endif (OPENSSL_CRYPTO_LIBRARIES)
if (MBEDTLS_CRYPTO_LIBRARY) if (MBEDTLS_CRYPTO_LIBRARY)
set(LIBSSH_PRIVATE_INCLUDE_DIRS set(LIBSSH_PRIVATE_INCLUDE_DIRS
@ -43,15 +35,7 @@ if (GCRYPT_LIBRARIES)
endif() endif()
if (WITH_ZLIB) if (WITH_ZLIB)
set(LIBSSH_PRIVATE_INCLUDE_DIRS list(APPEND LIBSSH_LINK_LIBRARIES ZLIB::ZLIB)
${LIBSSH_PRIVATE_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIR}
)
set(LIBSSH_LINK_LIBRARIES
${LIBSSH_LINK_LIBRARIES}
${ZLIB_LIBRARY}
)
endif (WITH_ZLIB) endif (WITH_ZLIB)
if (WITH_GSSAPI AND GSSAPI_FOUND) if (WITH_GSSAPI AND GSSAPI_FOUND)

View File

@ -6,9 +6,7 @@ endif (BSD OR SOLARIS OR OSX)
set(TORTURE_LIBRARY torture) set(TORTURE_LIBRARY torture)
include_directories(${OPENSSL_INCLUDE_DIR} include_directories(${CMOCKA_INCLUDE_DIR}
${CMOCKA_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${libssh_BINARY_DIR}/include ${libssh_BINARY_DIR}/include
${libssh_BINARY_DIR} ${libssh_BINARY_DIR}
${libssh_SOURCE_DIR}/src ${libssh_SOURCE_DIR}/src

View File

@ -44,7 +44,7 @@ else ()
${libssh_SOURCE_DIR}/src/md_crypto.c ${libssh_SOURCE_DIR}/src/md_crypto.c
) )
set(override_libs set(override_libs
${OPENSSL_CRYPTO_LIBRARIES} OpenSSL::Crypto
) )
endif (WITH_GCRYPT) endif (WITH_GCRYPT)

View File

@ -5,7 +5,6 @@ if (WITH_SERVER AND UNIX AND NOT WIN32)
include_directories(${libssh_SOURCE_DIR}/include include_directories(${libssh_SOURCE_DIR}/include
${libssh_BINARY_DIR}/include ${libssh_BINARY_DIR}/include
${CMOCKA_INCLUDE_DIR} ${CMOCKA_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}
${libssh_SOURCE_DIR}/src ${libssh_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}) ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -1,7 +1,5 @@
project(unittests C) project(unittests C)
include_directories(${OPENSSL_INCLUDE_DIR})
set(LIBSSH_UNIT_TESTS set(LIBSSH_UNIT_TESTS
torture_bignum torture_bignum
torture_buffer torture_buffer