summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2025-07-30 16:38:47 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2025-08-01 16:36:43 +0200
commit26f8dff7daeb6538b9308d2bef77ecea8bd2a80e (patch)
treeba8b28235bef5f2e124e8326b7f4718ca3618d2f /src
parent346a748ffe5571e05b01a02ddbd3131c336ca433 (diff)
CMake: Add warnings as errors support for example builds
We want to ensure we don't introduce new build warnings in examples. We have a mechanism to enable warnings as errors for qt targets like modules or plugins, but not for targets in example projects. To enable this, we introduce a new internal target PlatformExampleInternal, which is linked to all targets created by qt_add_executable and qt_add_library-like functions in projects in the <repo>/examples subdirectory. The detection of targets in example projects is done by checking the QT_INTERNAL_IS_EXAMPLE_EP_BUILD variable or the QT_INTERNAL_IS_EXAMPLE_IN_TREE_BUILD variable. These are set either by ExternalProject_Add in the case of external example projects, or by qt_examples_build_begin in the case of in-tree example projects. The PlatformExampleInternal target gets the same warnings as errors flags as the other Platform*Internal targets. To enable gradual adoption, the new feature is opt-in rather than opt-out, on a per-repo basis. The QT_REPO_EXAMPLES_WARNINGS_CLEAN variable can be set to TRUE in a repo's '.cmake.conf' file. The warnings as errors behavior is controlled as usual by configuring qt with -DWARNINGS_ARE_ERRORS=ON. Pick-to: 6.8 6.9 6.10 Task-number: QTBUG-108448 Change-Id: If62b47b205c111f2a1fec791e5bb6502f5b64f9c Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/Qt6CoreMacros.cmake29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake
index 46283381a42..dd6108066b6 100644
--- a/src/corelib/Qt6CoreMacros.cmake
+++ b/src/corelib/Qt6CoreMacros.cmake
@@ -695,6 +695,31 @@ function(_qt_internal_disable_autorcc_zstd_when_not_supported target)
endif()
endfunction()
+# Link given target to PlatformExampleInternal when the target is part of an example build.
+function(_qt_internal_link_to_platform_example_internal target)
+ # The first variable is set when examples are built using ExternalProject_Add.
+ # The second is set when examples are built in-tree, the scope is in the <repo>/examples subdir.
+ if(QT_INTERNAL_IS_EXAMPLE_EP_BUILD
+ OR QT_INTERNAL_IS_EXAMPLE_IN_TREE_BUILD)
+ target_link_libraries("${target}" PRIVATE
+ ${QT_CMAKE_EXPORT_NAMESPACE}::PlatformExampleInternal)
+ endif()
+endfunction()
+
+# Set up warnings as errors for targets built in example projects.
+function(_qt_internal_setup_warnings_are_errors_for_example_target target)
+ # Only enable warnings as errors when the global variable is enabled and the repo is known
+ # to have clean examples.
+ if(QT_INTERNAL_IS_EXAMPLE_EP_BUILD
+ OR QT_INTERNAL_IS_EXAMPLE_IN_TREE_BUILD)
+ if(WARNINGS_ARE_ERRORS AND QT_REPO_EXAMPLES_WARNINGS_CLEAN)
+ _qt_internal_set_skip_warnings_are_errors("${target}" FALSE)
+ else()
+ _qt_internal_set_skip_warnings_are_errors("${target}" TRUE)
+ endif()
+ endif()
+endfunction()
+
function(_qt_internal_create_executable target)
if(ANDROID)
list(REMOVE_ITEM ARGN "WIN32" "MACOSX_BUNDLE")
@@ -727,6 +752,8 @@ function(_qt_internal_create_executable target)
endif()
_qt_internal_disable_autorcc_zstd_when_not_supported("${target}")
+ _qt_internal_link_to_platform_example_internal("${target}")
+ _qt_internal_setup_warnings_are_errors_for_example_target("${target}")
_qt_internal_set_up_static_runtime_library("${target}")
endfunction()
@@ -2834,6 +2861,8 @@ function(_qt_internal_add_library target)
cmake_policy(POP)
_qt_internal_disable_autorcc_zstd_when_not_supported("${target}")
+ _qt_internal_link_to_platform_example_internal("${target}")
+ _qt_internal_setup_warnings_are_errors_for_example_target("${target}")
_qt_internal_set_up_static_runtime_library(${target})
if(NOT type_to_create STREQUAL "INTERFACE" AND NOT type_to_create STREQUAL "OBJECT")