Consider BUILD_SHARED_LIBS when adding libraries using _qt_internal_add_library

It does look safe to consider the value of BUILD_SHARED_LIBS in
_qt_internal_add_library. Current behavior might confuse users that
specify BUILD_SHARED_LIBS explicitly but get the library types
according to Qt build type.

[ChangeLog][CMake] qt6_add_library now considers the value of the
BUILD_SHARED_LIBS variable. If the variable is DEFINED it has
higher priority than the library type detecting logic in
qt6_add_library when adding the library targets.

Change-Id: I1c40e887c4e481424a596c870a8ff2784b08fcbb
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Alexey Edelev 2023-07-06 21:11:58 +02:00
parent ce8874fc3b
commit 674134f08d
3 changed files with 21 additions and 20 deletions

View File

@ -77,24 +77,24 @@ project(QtBase
LANGUAGES CXX C ASM
)
# Should this Qt be static or dynamically linked?
option(BUILD_SHARED_LIBS "Build Qt statically or dynamically" ON)
set(QT_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
# This variable is also set in Qt6CoreConfigExtras.cmake, but it's not loaded when building
# qtbase. Set it here so qt_add_plugin can compute the proper plugin flavor.
set(QT6_IS_SHARED_LIBS_BUILD ${BUILD_SHARED_LIBS})
# BUILD_SHARED_LIBS influences the minimum required CMake version. The value is set either by:
# a cache variable provided on the configure command line
# or set by QtAutoDetect.cmake depending on the platform
# or specified via a toolchain file that is loaded by the project() call
# or set by the option() call above
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/QtCMakeVersionHelpers.cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/QtPublicCMakeVersionHelpers.cmake")
qt_internal_check_and_warn_about_unsuitable_cmake_version()
if(NOT QT_BUILD_STANDALONE_TESTS)
# Should this Qt be static or dynamically linked?
option(BUILD_SHARED_LIBS "Build Qt statically or dynamically" ON)
set(QT_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
# This variable is also set in Qt6CoreConfigExtras.cmake, but it's not loaded when building
# qtbase. Set it here so qt_add_plugin can compute the proper plugin flavor.
set(QT6_IS_SHARED_LIBS_BUILD ${BUILD_SHARED_LIBS})
# BUILD_SHARED_LIBS influences the minimum required CMake version. The value is set either by:
# a cache variable provided on the configure command line
# or set by QtAutoDetect.cmake depending on the platform
# or specified via a toolchain file that is loaded by the project() call
# or set by the option() call above
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/QtCMakeVersionHelpers.cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/QtPublicCMakeVersionHelpers.cmake")
qt_internal_check_and_warn_about_unsuitable_cmake_version()
## Add some paths to check for cmake modules:
list(PREPEND CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"

View File

@ -2516,7 +2516,7 @@ function(_qt_internal_add_library target)
# This in contrast to CMake which defaults to STATIC.
if(NOT arg_STATIC AND NOT arg_SHARED AND NOT arg_MODULE AND NOT arg_INTERFACE
AND NOT arg_OBJECT)
if(QT6_IS_SHARED_LIBS_BUILD)
if(BUILD_SHARED_LIBS OR (NOT DEFINED BUILD_SHARED_LIBS AND QT6_IS_SHARED_LIBS_BUILD))
set(type_to_create SHARED)
else()
set(type_to_create STATIC)

View File

@ -45,8 +45,9 @@ library type created depends on how Qt was built. If Qt was built statically,
a static library will be created. Otherwise, a shared library will
be created. Note that this is different to how CMake's \c{add_library()}
command works, where the \c BUILD_SHARED_LIBS variable controls the type of
library created. The \c{qt_add_library()} command does not consider
\c BUILD_SHARED_LIBS when deciding the library type.
library created. The \c{qt_add_library()} command considers
\c BUILD_SHARED_LIBS when deciding the library type only if the variable is set
explicitly.
Any \c{sources} provided will be passed through to the internal call to
\c{add_library()}.