CMake: Fix sqldrivers project to allow building as Debug MSVC config

When the user:
- has a non-developer-build -debug-and-release Qt
- and tries to configure the sqldrivers project with
  -DCMAKE_BUILD_TYPE=Debug
our build system discarded the user request, and defaulted to
'Release'.

That happens because CMake sets CMAKE_BUILD_TYPE_INIT to 'Debug' by
default on Windows-MSVC, and we have no marker to differentiate
that the 'Debug' value was user-specified.

We have such a marker
- via the
  __qt_auto_detect_cmake_build_type_before_project_call variable
  when configuring qtbase / top-level qt
- via the
  __qt_toolchain_cmake_build_type_before_project_call variable when
  configuring via the qt toolchain file (although that doesn't apply
  when configuring a multi-config build for obscure reasons, which
  should be addressed).

A conservative fix is to add a new variable / marker called
__qt_internal_standalone_project_cmake_build_type_before_project_call
which the 'sqldrivers' project will set with the build type that is
detected before the first project() call, and use that to decide
whether to override the build type, similar how we do with toolchain
file variable.

We could reuse one of the previous variables, but I figured it's better
to be explicit with a new one. And hopefully we can clean up the whole
logic in a follow-up commit.

Amends 48841c34d2e86a741ec9992b9704c0fa5973503c
Amends 8c912cddebe544010e7da3f87af5b21f3328d7ec

Pick-to: 6.5 6.6 6.7
Fixes: QTBUG-120436
Task-number: QTBUG-114958
Change-Id: I37e3d8041088fe6084a9976ecc80ddd62d73ef81
Reviewed-by:  Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Alexandru Croitor 2024-01-22 17:18:41 +01:00
parent 12203e94f5
commit 5b5fa7b75a
2 changed files with 13 additions and 2 deletions

View File

@ -10,9 +10,11 @@ macro(qt_internal_set_default_build_type)
# Try to detect if an explicit CMAKE_BUILD_TYPE was set by the user.
# CMake sets CMAKE_BUILD_TYPE_INIT to Debug on most Windows platforms and doesn't set
# anything for UNIXes. CMake assigns CMAKE_BUILD_TYPE_INIT to CMAKE_BUILD_TYPE during
# first project() if CMAKE_BUILD_TYPE has no previous value.
# the first project() call, if CMAKE_BUILD_TYPE had no previous value.
# We use extra information about the state of CMAKE_BUILD_TYPE before the first
# project() call that's set in QtAutodetect.
# project() call that's set in QtAutoDetect.cmake or manually in a project via the
# __qt_internal_standalone_project_cmake_build_type_before_project_call variable (as done
# for the qtbase sqldrivers project).
# STREQUAL check needs to have expanded variables because an undefined var is not equal
# to an empty defined var.
# See also qt_internal_force_set_cmake_build_type_conditionally which is used
@ -20,6 +22,7 @@ macro(qt_internal_set_default_build_type)
if("${CMAKE_BUILD_TYPE}" STREQUAL "${CMAKE_BUILD_TYPE_INIT}"
AND NOT __qt_auto_detect_cmake_build_type_before_project_call
AND NOT __qt_build_internals_cmake_build_type
AND NOT __qt_internal_standalone_project_cmake_build_type_before_project_call
AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${_default_build_type}' as none was specified.")
set(CMAKE_BUILD_TYPE "${_default_build_type}" CACHE STRING "Choose the type of build." FORCE)

View File

@ -4,6 +4,14 @@
cmake_minimum_required(VERSION 3.16)
if (NOT CMAKE_PROJECT_NAME STREQUAL "QtBase" AND NOT CMAKE_PROJECT_NAME STREQUAL "Qt")
include(.cmake.conf)
# Store initial build type (if any is specified) to be read by
# qt_internal_set_default_build_type().
# See qt_internal_set_default_build_type() for details.
if(DEFINED CACHE{CMAKE_BUILD_TYPE})
set(__qt_internal_standalone_project_cmake_build_type_before_project_call
"${CMAKE_BUILD_TYPE}")
endif()
project(QSQLiteDriverPlugins
VERSION "${QT_REPO_MODULE_VERSION}"
DESCRIPTION "Qt6 SQL driver plugins"