CMake: Fix Threads global promotion issue when using static openssl

and an older CMake 3.22, which is shipped by default on Ubuntu 22.04.

If for some reason there's a static openssl library lying around in
the default sysroot (or any ssl search path), like in
 /usr/lib/libssl.a,
then CMake's _OpenSSL_test_and_find_dependencies
will try to find_package(Threads) because it assumes it has a
dependency on the Threads package.

Because we do qt_find_package(WrapOpenSSLHeaders) in
qtbase/configure.cmake
and we do qt_find_package(Threads) in
src/corelib/CMakeLists.txt,
we would create the Threads target in the root directory scope, and
then try to promote it to global in the corelib subdirectory,
which fails with

CMake Error at qtbase/cmake/QtPublicTargetHelpers.cmake:260
  (set_property):
  Attempt to promote imported target "Threads::Threads" to global scope
  (by setting IMPORTED_GLOBAL) which is not built in this directory.
Call Stack (most recent call first):
  qtbase/cmake/QtFindPackageHelpers.cmake:211
  (__qt_internal_promote_target_to_global)
  qtbase/src/corelib/CMakeLists.txt:4 (qt_find_package)

Newer versions of CMake's FindOpenSSL actually try to determine if
the Threads package is really needed.

To avoid the issue entirely, just look up Threads before we look up
the OpenSSL package.

Pick-to: 6.5 6.6 6.7
Change-Id: Ia3cde93e26ba004f64105a5b457098e1b9870885
Reviewed-by:  Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Alexandru Croitor 2024-01-19 16:00:11 +01:00
parent 5122fe625d
commit 85c462855b
2 changed files with 4 additions and 1 deletions

View File

@ -18,6 +18,10 @@ if(TARGET ZLIB::ZLIB)
set_property(TARGET ZLIB::ZLIB PROPERTY IMPORTED_GLOBAL TRUE)
endif()
# Look for Threads in the same scope as OpenSSL package, because OpenSSL sometimes depends on
# Threads (for static OpenSSL builds) and we want to promote the target to global in the same
# directory scope.
qt_find_package(Threads PROVIDED_TARGETS Threads::Threads)
qt_find_package(WrapOpenSSLHeaders PROVIDED_TARGETS WrapOpenSSLHeaders::WrapOpenSSLHeaders MODULE_NAME core)
# openssl_headers
# OPENSSL_VERSION_MAJOR is not defined for OpenSSL 1.1.1

View File

@ -1,7 +1,6 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
qt_find_package(Threads PROVIDED_TARGETS Threads::Threads)
qt_find_package(WrapPCRE2 PROVIDED_TARGETS WrapPCRE2::WrapPCRE2)
qt_find_package(WrapZLIB PROVIDED_TARGETS WrapZLIB::WrapZLIB)