Unify and improve code for finding OpenSSL

* Unify code for use_openssl and use_crypto
* Make missing OpenSSL targets a fatal error if OpenSSL is required
* Add note that possibly the devel package is missing
    * Under Tumbleweed with missing devel package I've got `OpenSSL_FOUND`
      but no imported targets because the symlink from the devel package
      was missing. This might be due to stale values within the CMake
      cache.
This commit is contained in:
Martchus 2021-07-07 20:51:57 +02:00
parent eace9b44ec
commit db87472be9
2 changed files with 28 additions and 36 deletions

View File

@ -113,7 +113,7 @@ set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities") set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities")
set(META_VERSION_MAJOR 5) set(META_VERSION_MAJOR 5)
set(META_VERSION_MINOR 10) set(META_VERSION_MINOR 10)
set(META_VERSION_PATCH 5) set(META_VERSION_PATCH 6)
# find required 3rd party libraries # find required 3rd party libraries
include(3rdParty) include(3rdParty)

View File

@ -151,20 +151,29 @@ function (use_iconv)
PARENT_SCOPE) PARENT_SCOPE)
endfunction () endfunction ()
function (use_openssl) macro (_cpp_utilities_use_openssl OPENSSL_TARGETS)
parse_arguments_for_use_functions(${ARGN})
find_package(OpenSSL ${ARGS_FIND_PACKAGE}) find_package(OpenSSL ${ARGS_FIND_PACKAGE})
if (NOT OpenSSL_FOUND) if (NOT OpenSSL_FOUND)
message(STATUS "Unable to find OpenSSL") message(STATUS "Unable to find OpenSSL")
return() return()
endif () endif ()
if (NOT TARGET OpenSSL::SSL OR NOT TARGET OpenSSL::Crypto) foreach (OPENSSL_TARGET ${OPENSSL_TARGETS})
message(WARNING "Found OpenSSL but imported targets OpenSSL::SSL and/or OpenSSL::Crypto missing.") if (TARGET "${OPENSSL_TARGET}")
continue()
endif ()
set(MESSAGE_MODE WARNING)
if (REQUIRED IN_LIST ARGS_FIND_PACKAGE)
set(MESSAGE_MODE FATAL_ERROR)
endif ()
message(
"${MESSAGE_MODE}"
"Found OpenSSL but imported target \"${OPENSSL_TARGET}\" is missing. Possibly the devel package for OpenSSL is not installed."
)
return() return()
endif () endforeach ()
message(STATUS "Found OpenSSL")
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};OpenSSL::SSL;OpenSSL::Crypto") message(STATUS "Found required OpenSSL targets (${OPENSSL_TARGETS})")
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};${OPENSSL_TARGETS}")
if (WIN32 AND OPENSSL_USE_STATIC_LIBS) if (WIN32 AND OPENSSL_USE_STATIC_LIBS)
# FIXME: preferably use pkg-config to cover this case without hardcoding OpenSSL's dependencies under Windows # FIXME: preferably use pkg-config to cover this case without hardcoding OpenSSL's dependencies under Windows
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};-lws2_32;-lgdi32;-lcrypt32") set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};-lws2_32;-lgdi32;-lcrypt32")
@ -175,40 +184,23 @@ function (use_openssl)
set("${ARGS_LIBRARIES_VARIABLE}" set("${ARGS_LIBRARIES_VARIABLE}"
"${${ARGS_LIBRARIES_VARIABLE}}" "${${ARGS_LIBRARIES_VARIABLE}}"
PARENT_SCOPE) PARENT_SCOPE)
set("PKG_CONFIG_OpenSSL_SSL"
"libssl"
PARENT_SCOPE)
set("PKG_CONFIG_OpenSSL_Crypto" set("PKG_CONFIG_OpenSSL_Crypto"
"libcrypto" "libcrypto"
PARENT_SCOPE) PARENT_SCOPE)
endmacro ()
function (use_openssl)
parse_arguments_for_use_functions(${ARGN})
_cpp_utilities_use_openssl("OpenSSL::SSL;OpenSSL::Crypto")
set("PKG_CONFIG_OpenSSL_SSL"
"libssl"
PARENT_SCOPE)
endfunction () endfunction ()
function (use_crypto) function (use_crypto)
parse_arguments_for_use_functions(${ARGN}) parse_arguments_for_use_functions(${ARGN})
_cpp_utilities_use_openssl("OpenSSL::Crypto")
find_package(OpenSSL ${ARGS_FIND_PACKAGE})
if (NOT OpenSSL_FOUND)
message(STATUS "Unable to find OpenSSL")
return()
endif ()
if (NOT TARGET OpenSSL::Crypto)
message(WARNING "Found OpenSSL but imported target OpenSSL::Crypto missing.")
return()
endif ()
message(STATUS "Found OpenSSL")
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};OpenSSL::Crypto")
if (WIN32 AND OPENSSL_USE_STATIC_LIBS)
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};-lws2_32;-lgdi32;-lcrypt32")
endif ()
set("${ARGS_PACKAGES_VARIABLE}"
"${${ARGS_PACKAGES_VARIABLE}};OpenSSL"
PARENT_SCOPE)
set("${ARGS_LIBRARIES_VARIABLE}"
"${${ARGS_LIBRARIES_VARIABLE}}"
PARENT_SCOPE)
set("PKG_CONFIG_OpenSSL_Crypto"
"libcrypto"
PARENT_SCOPE)
endfunction () endfunction ()
function (use_zlib) function (use_zlib)