CMake: Handle finding of OpenSSL headers correctly

In Coin when provisioning for Android, we download and configure
the OpenSSL package, but don't actually build it. This means that
find_package(OpenSSL) can find the headers, but not the library,
and thus the package is marked as not found.

Previously the openssl_headers feature used the result of finding
the OpenSSL package, which led to it being disabled in the above
described Android case.

Introduce 2 new find scripts FindWrapOpenSSL and
FindWrapOpenSSLHeaders. FindWrapOpenSSLHeaders wraps FindOpenSSL,
and checks if the headers were found, regardless of the OpenSSL_FOUND
value, which can be used for implementing the openssl_headers feature.

FindWrapOpenSSL uses FindWrapOpenSSLHeaders, and simply wraps the
OpenSSL target if available.

The find scripts also have to set CMAKE_FIND_ROOT_PATH for Android.
Otherwise when someone passes in an OPENSSL_ROOT_DIR, its value will
always be prepended to the Android sysroot, causing the package not
to be found.

Adjust the mapping in helper.py to use the targets created by these
find scripts. This also replaces the openssl/nolink target.

Adjust the projects and tests to use the new target names.

Adjust the compile tests for dtls and oscp to use the
WrapOpenSSLHeaders target, so that the features can be enabled even
if the library is dlopen-ed (like on Android).

Task-number: QTBUG-83371
Change-Id: I738600e5aafef47a57e1db070be40116ca8ab995
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Alexandru Croitor 2020-04-07 17:54:49 +02:00
parent 0a13c3a3f0
commit e0346df1b2
10 changed files with 114 additions and 19 deletions

View File

@ -0,0 +1,21 @@
# We can't create the same interface imported target multiple times, CMake will complain if we do
# that. This can happen if the find_package call is done in multiple different subdirectories.
if(TARGET WrapOpenSSL::WrapOpenSSL)
set(WrapOpenSSL_FOUND ON)
return()
endif()
set(WrapOpenSSL_FOUND OFF)
# Reuse logic from the headers find script.
find_package(WrapOpenSSLHeaders ${WrapOpenSSL_FIND_VERSION})
if(OpenSSL_FOUND)
set(WrapOpenSSL_FOUND ON)
add_library(WrapOpenSSL::WrapOpenSSL INTERFACE IMPORTED)
target_link_libraries(WrapOpenSSL::WrapOpenSSL INTERFACE OpenSSL::SSL)
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(WrapOpenSSL DEFAULT_MSG WrapOpenSSL_FOUND)

View File

@ -0,0 +1,34 @@
# We can't create the same interface imported target multiple times, CMake will complain if we do
# that. This can happen if the find_package call is done in multiple different subdirectories.
if(TARGET WrapOpenSSLHeaders::WrapOpenSSLHeaders)
set(WrapOpenSSLHeaders_FOUND ON)
return()
endif()
set(WrapOpenSSLHeaders_FOUND OFF)
# When cross-compiling (to Android for example), we need to add the OPENSSL_ROOT_DIR as a root path,
# otherwise the value would just be appended to the sysroot, which is wrong.
if(OPENSSL_ROOT_DIR)
set(__find_wrap_openssl_headers_backup_root_dir "${CMAKE_FIND_ROOT_PATH}")
list(APPEND CMAKE_FIND_ROOT_PATH "${OPENSSL_ROOT_DIR}")
endif()
find_package(OpenSSL ${WrapOpenSSLHeaders_FIND_VERSION})
if(OPENSSL_ROOT_DIR)
set(CMAKE_FIND_ROOT_PATH "${__find_wrap_openssl_headers_backup_root_dir}")
endif()
# We are interested only in include headers. The libraries might be missing, so we can't check the
# _FOUND variable.
if(OPENSSL_INCLUDE_DIR)
set(WrapOpenSSLHeaders_FOUND ON)
add_library(WrapOpenSSLHeaders::WrapOpenSSLHeaders INTERFACE IMPORTED)
target_include_directories(WrapOpenSSLHeaders::WrapOpenSSLHeaders INTERFACE
${OPENSSL_INCLUDE_DIR})
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(WrapOpenSSLHeaders DEFAULT_MSG WrapOpenSSLHeaders_FOUND)

View File

@ -411,12 +411,12 @@ qt_extend_target(Network CONDITION ANDROID AND QT_FEATURE_openssl AND QT_FEATURE
qt_extend_target(Network CONDITION QT_FEATURE_openssl AND QT_FEATURE_openssl_linked AND QT_FEATURE_ssl AND NOT ANDROID
LIBRARIES
OpenSSL::SSL
WrapOpenSSL::WrapOpenSSL
)
qt_extend_target(Network CONDITION QT_FEATURE_openssl AND QT_FEATURE_ssl AND NOT QT_FEATURE_openssl_linked
LIBRARIES
OpenSSL::SSL_nolink
WrapOpenSSLHeaders::WrapOpenSSLHeaders
)
qt_extend_target(Network CONDITION QT_FEATURE_openssl AND QT_FEATURE_ssl AND WIN32

View File

@ -411,12 +411,12 @@ qt_extend_target(Network CONDITION ANDROID AND QT_FEATURE_openssl AND QT_FEATURE
qt_extend_target(Network CONDITION QT_FEATURE_openssl AND QT_FEATURE_openssl_linked AND QT_FEATURE_ssl AND NOT ANDROID
LIBRARIES
OpenSSL::SSL
WrapOpenSSL::WrapOpenSSL
)
qt_extend_target(Network CONDITION QT_FEATURE_openssl AND QT_FEATURE_ssl AND NOT QT_FEATURE_openssl_linked
LIBRARIES
OpenSSL::SSL_nolink
WrapOpenSSLHeaders::WrapOpenSSLHeaders
)
qt_extend_target(Network CONDITION QT_FEATURE_openssl AND QT_FEATURE_ssl AND WIN32

View File

@ -7,11 +7,11 @@
#### Libraries
qt_find_package(Libproxy PROVIDED_TARGETS PkgConfig::Libproxy)
qt_find_package(OpenSSL PROVIDED_TARGETS OpenSSL::SSL)
qt_find_package(WrapOpenSSLHeaders PROVIDED_TARGETS WrapOpenSSLHeaders::WrapOpenSSLHeaders)
# openssl_headers
qt_config_compile_test(openssl_headers
LIBRARIES
OpenSSL::SSL
WrapOpenSSLHeaders::WrapOpenSSLHeaders
CODE
"
#include <openssl/ssl.h>
@ -32,11 +32,11 @@ int main(int argc, char **argv)
}
")
qt_find_package(OpenSSL PROVIDED_TARGETS OpenSSL::SSL)
qt_find_package(WrapOpenSSL PROVIDED_TARGETS WrapOpenSSL::WrapOpenSSL)
# openssl
qt_config_compile_test(openssl
LIBRARIES
OpenSSL::SSL
WrapOpenSSL::WrapOpenSSL
CODE
"
#include <openssl/ssl.h>
@ -164,7 +164,7 @@ socklen_t sctpInitMsgSize = sizeof(sctpInitMsg);
qt_config_compile_test(dtls
LABEL "DTLS support in OpenSSL"
LIBRARIES
OpenSSL::SSL
WrapOpenSSLHeaders::WrapOpenSSLHeaders
CODE
"
#include <openssl/ssl.h>
@ -185,7 +185,7 @@ int main(int argc, char **argv)
qt_config_compile_test(ocsp
LABEL "OCSP stapling support in OpenSSL"
LIBRARIES
OpenSSL::SSL
WrapOpenSSLHeaders::WrapOpenSSLHeaders
CODE
"
#include <openssl/ssl.h>

View File

@ -7,7 +7,8 @@ if(QT_BUILD_STANDALONE_TESTS)
qt_find_package(WrapDBus1 PROVIDED_TARGETS dbus-1)
qt_find_package(ICU COMPONENTS i18n uc data PROVIDED_TARGETS ICU::i18n ICU::uc ICU::data)
qt_find_package(Threads PROVIDED_TARGETS Threads::Threads)
qt_find_package(OpenSSL PROVIDED_TARGETS OpenSSL::OpenSSL)
qt_find_package(WrapOpenSSL PROVIDED_TARGETS WrapOpenSSL::WrapOpenSSL)
qt_find_package(WrapOpenSSLHeaders PROVIDED_TARGETS WrapOpenSSLHeaders::WrapOpenSSLHeaders)
# special case end
endif()
qt_build_tests()

View File

@ -9,7 +9,7 @@ if (QT_FEATURE_private_tests)
endif()
if (QT_FEATURE_openssl AND QT_FEATURE_ssl AND NOT QT_FEATURE_openssl_linked)
include_directories($<TARGET_PROPERTY:${INSTALL_CMAKE_NAMESPACE}::SSL_nolink,INTERFACE_INCLUDE_DIRECTORIES>)
include_directories($<TARGET_PROPERTY:WrapOpenSSLHeaders::WrapOpenSSLHeaders,INTERFACE_INCLUDE_DIRECTORIES>)
endif()
endif()
# special case end

View File

@ -10,7 +10,7 @@ if (QT_FEATURE_private_tests)
endif()
if (QT_FEATURE_openssl AND QT_FEATURE_ssl AND NOT QT_FEATURE_openssl_linked)
include_directories($<TARGET_PROPERTY:${INSTALL_CMAKE_NAMESPACE}::SSL_nolink,INTERFACE_INCLUDE_DIRECTORIES>)
include_directories($<TARGET_PROPERTY:WrapOpenSSLHeaders::WrapOpenSSLHeaders,INTERFACE_INCLUDE_DIRECTORIES>)
endif()
endif()
# special case end

View File

@ -40,6 +40,7 @@ from helper import (
map_platform,
find_3rd_party_library_mapping,
generate_find_package_info,
get_compile_test_dependent_library_mapping,
)
knownTests = set() # type: Set[str]
@ -700,7 +701,8 @@ def write_compile_test(
if len(library) == 0:
continue
library_usage = get_library_usage_for_compile_test(library)
adjusted_library = get_compile_test_dependent_library_mapping(name, library)
library_usage = get_library_usage_for_compile_test(adjusted_library)
if "fixme" in library_usage:
qmakeFixme += library_usage["fixme"]
continue

View File

@ -44,6 +44,7 @@ class LibraryMapping:
is_bundled_with_qt: bool = False,
test_library_overwrite: str = "",
run_library_test: bool = False,
no_link_so_name: str = "",
) -> None:
self.soName = soName
self.packageName = packageName
@ -66,6 +67,9 @@ class LibraryMapping:
# Run the library compile test of configure.json
self.run_library_test = run_library_test
# The custom nolink library mapping associated with this one.
self.no_link_so_name = no_link_so_name
def is_qt(self) -> bool:
return self.packageName == "Qt" or self.packageName == "Qt5" or self.packageName == "Qt6"
@ -447,20 +451,21 @@ _library_map = [
LibraryMapping("opengl", "OpenGL", "OpenGL::GL", resultVariable="OpenGL_OpenGL"),
LibraryMapping(
"openssl_headers",
"OpenSSL",
"OpenSSL::SSL_nolink",
"WrapOpenSSLHeaders",
"WrapOpenSSLHeaders::WrapOpenSSLHeaders",
resultVariable="TEST_openssl_headers",
appendFoundSuffix=False,
test_library_overwrite="OpenSSL::SSL",
test_library_overwrite="WrapOpenSSLHeaders::WrapOpenSSLHeaders",
run_library_test=True,
),
LibraryMapping(
"openssl",
"OpenSSL",
"OpenSSL::SSL",
"WrapOpenSSL",
"WrapOpenSSL::WrapOpenSSL",
resultVariable="TEST_openssl",
appendFoundSuffix=False,
run_library_test=True,
no_link_so_name="openssl_headers",
),
LibraryMapping("oci", "Oracle", "Oracle::OCI"),
LibraryMapping(
@ -698,24 +703,56 @@ def map_platform(platform: str) -> str:
def is_known_3rd_party_library(lib: str) -> bool:
handling_no_link = False
if lib.endswith("/nolink") or lib.endswith("_nolink"):
lib = lib[:-7]
handling_no_link = True
mapping = find_3rd_party_library_mapping(lib)
if handling_no_link and mapping and mapping.no_link_so_name:
no_link_mapping = find_3rd_party_library_mapping(mapping.no_link_so_name)
if no_link_mapping:
mapping = no_link_mapping
return mapping is not None
def map_3rd_party_library(lib: str) -> str:
handling_no_link = False
libpostfix = ""
if lib.endswith("/nolink"):
lib = lib[:-7]
libpostfix = "_nolink"
handling_no_link = True
mapping = find_3rd_party_library_mapping(lib)
if handling_no_link and mapping and mapping.no_link_so_name:
no_link_mapping = find_3rd_party_library_mapping(mapping.no_link_so_name)
if no_link_mapping:
mapping = no_link_mapping
libpostfix = ""
if not mapping or not mapping.targetName:
return lib
return mapping.targetName + libpostfix
compile_test_dependent_library_mapping = {
"dtls": {"openssl": "openssl_headers"},
"ocsp": {"openssl": "openssl_headers"},
}
def get_compile_test_dependent_library_mapping(compile_test_name: str, dependency_name: str):
if compile_test_name in compile_test_dependent_library_mapping:
mapping = compile_test_dependent_library_mapping[compile_test_name]
if dependency_name in mapping:
return mapping[dependency_name]
return dependency_name
def generate_find_package_info(
lib: LibraryMapping,
use_qt_find_package: bool = True,