FindOpenSSL doesn't find debug libraries correctly with OpenSSL 1.1
In OpenSSL 1.1, whether you configure with VC-Win32
or debug-VC-Win32
, the output static link libraries do not have the d
suffix that FindOpenSSL expects:
C:\Users\qris0\cmake-32>dir Install\LIB
...
31/12/2017 21:00 929,202 libcrypto.lib
31/12/2017 21:00 97,452 libssl.lib
But if we look at FindOpenSSL.cmake we see:
find_library(LIB_EAY_DEBUG
NAMES
libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
libcrypto${_OPENSSL_MSVC_RT_MODE}d
libcryptod
libeay32${_OPENSSL_MSVC_RT_MODE}d
libeay32d
cryptod
NAMES_PER_DIR
${_OPENSSL_ROOT_HINTS_AND_PATHS}
PATH_SUFFIXES
${_OPENSSL_PATH_SUFFIXES}
)
Note that every one of NAMES
has a d
suffix. So if we happen to have SLproweb's OpenSSL distribution installed as well (as Appveyor CI does) then we get the following weird result:
-- Found OpenSSL: optimized;C:/projects/cmake/Install/lib/libcrypto.lib;debug;C:/OpenSSL-Win32/lib/VC/libeay32MDd.lib (found version "1.1.0g")
Note that it found the debug version that I just built, although it thinks that it's an optimized
version, and it wants to use the one from slproweb for debug
builds.
It seems impossible to disable this suffix. One workaround is to detect without REQUIRED, then reset the incorrect variables and reimplement the REQUIRED test:
find_package(OpenSSL)
set(OPENSSL_SSL_LIBRARY ${SSL_EAY_RELEASE})
set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY_RELEASE})
set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
find_package_handle_standard_args(OpenSSL
REQUIRED_VARS
OPENSSL_SSL_LIBRARY
OPENSSL_CRYPTO_LIBRARY
OPENSSL_INCLUDE_DIR
VERSION_VAR
OPENSSL_VERSION
FAIL_MESSAGE
"Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
system variable OPENSSL_ROOT_DIR"
)
Relatedly, it's strange that setting OPENSSL_ROOT_DIR
does not override the built-in search path, but merely augments it. There seems to be no way to override this, which would have made this error a very obvious failure to configure the build, instead of which it silently links against a completely different library to the one that I want: the one that I built myself for this purpose, and specified in OPENSSL_ROOT_DIR.
This latter issue may be indirectly related to #15887 (closed) and commit [ed1758f8], which introduced support for OpenSSL 1.1 but did not address the debug/release name suffixes.