Improve the internal handling of unity build

- Removed the NO_UNITY_BUILD argument from commands that disable it by
  default.
- Add a warning in case NO_UNITY_BUILD or NO_UNITY_BUILD_SOURCES is
  being used where it is already disabled, e.g., qt_internal_add_test
- Exclude all sources of a target from unity build if NO_UNITY_BUILD
  is set on the target. This sounds a bit harsh, but I have noticed that
  sometimes the same source file can be included somewhere else, and
  some unexpected collision may occur.
- qt_examples_build_end excludes all its examples from the unity build.
- qt_build_test now sets the CMAKE_UNITY_BUILD to OFF before configuring
  the tests, and restore its value when done.

Pick-to: 6.5
Task-number: QTBUG-109394
Change-Id: Ia42e7dd5a5bfb151db241deb639325720fd91eec
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Amir Masoud Abdol 2023-03-09 14:53:14 +01:00
parent cfed658747
commit 3602936230
5 changed files with 28 additions and 18 deletions

View File

@ -50,12 +50,6 @@ function(qt_internal_add_common_qt_library_helper target)
set(arg_MODULE STATIC)
endif()
if(arg_NO_UNITY_BUILD)
set(arg_NO_UNITY_BUILD NO_UNITY_BUILD)
else()
set(arg_NO_UNITY_BUILD "")
endif()
_qt_internal_add_library(${target} ${arg_STATIC} ${arg_SHARED} ${arg_MODULE} ${arg_INTERFACE})
if(arg_NO_UNITY_BUILD)
@ -85,6 +79,7 @@ function(qt_internal_add_cmake_library target)
"${multi_args}"
)
_qt_internal_validate_all_args_are_parsed(arg)
_qt_internal_validate_no_unity_build(arg)
qt_remove_args(library_helper_args
ARGS_TO_REMOVE
@ -110,12 +105,6 @@ function(qt_internal_add_cmake_library target)
)
endif()
if(arg_NO_UNITY_BUILD)
set(arg_NO_UNITY_BUILD NO_UNITY_BUILD)
else()
set(arg_NO_UNITY_BUILD "")
endif()
qt_internal_extend_target("${target}"
SOURCES ${arg_SOURCES}
INCLUDE_DIRECTORIES
@ -137,8 +126,7 @@ function(qt_internal_add_cmake_library target)
MOC_OPTIONS ${arg_MOC_OPTIONS}
ENABLE_AUTOGEN_TOOLS ${arg_ENABLE_AUTOGEN_TOOLS}
DISABLE_AUTOGEN_TOOLS ${arg_DISABLE_AUTOGEN_TOOLS}
NO_UNITY_BUILD_SOURCES ${arg_NO_UNITY_BUILD_SOURCES}
${arg_NO_UNITY_BUILD}
NO_UNITY_BUILD # Disabled by default
)
endfunction()
@ -167,6 +155,7 @@ function(qt_internal_add_3rdparty_library target)
"${multi_args}"
)
_qt_internal_validate_all_args_are_parsed(arg)
_qt_internal_validate_no_unity_build(arg)
qt_remove_args(library_helper_args
ARGS_TO_REMOVE

View File

@ -689,6 +689,8 @@ function(qt_internal_get_standalone_tests_config_file_name out_var)
endfunction()
macro(qt_build_tests)
set(CMAKE_UNITY_BUILD OFF)
if(QT_BUILD_STANDALONE_TESTS)
# Find location of TestsConfig.cmake. These contain the modules that need to be
# find_package'd when testing.
@ -759,6 +761,8 @@ macro(qt_build_tests)
add_subdirectory(manual)
endif()
endif()
set(CMAKE_UNITY_BUILD ${QT_UNITY_BUILD})
endmacro()
function(qt_compute_relative_path_from_cmake_config_dir_to_prefix)
@ -973,6 +977,7 @@ macro(qt_examples_build_end)
if(TARGET Qt::Widgets)
qt_autogen_tools(${target} ENABLE_AUTOGEN_TOOLS "uic")
endif()
set_target_properties(${target} PROPERTIES UNITY_BUILD OFF)
endforeach()
install(CODE "

View File

@ -212,11 +212,16 @@ function(qt_internal_extend_target target)
DISABLE_AUTOGEN_TOOLS ${arg_DISABLE_AUTOGEN_TOOLS})
qt_update_precompiled_header("${target}" "${arg_PRECOMPILED_HEADER}")
## Also exclude them from unity build
qt_update_ignore_pch_source("${target}" "${arg_NO_PCH_SOURCES}")
qt_update_ignore_unity_build_sources("${target}" "${arg_NO_UNITY_BUILD_SOURCES}")
## Ignore objective-c files for PCH (not supported atm)
qt_ignore_pch_obj_c_sources("${target}" "${arg_SOURCES}")
qt_update_ignore_unity_build_sources("${target}" "${arg_NO_UNITY_BUILD_SOURCES}")
if(arg_NO_UNITY_BUILD)
set_target_properties("${target}" PROPERTIES UNITY_BUILD OFF)
qt_update_ignore_unity_build_sources("${target}" "${arg_SOURCES}")
endif()
else()
if(QT_CMAKE_DEBUG_EXTEND_TARGET)
message("qt_extend_target(${target} CONDITION ${arg_CONDITION} ...): Skipped")
@ -234,9 +239,6 @@ function(qt_internal_extend_target target)
${sources_property} "${arg_CONDITION_INDEPENDENT_SOURCES}")
endif()
if(arg_NO_UNITY_BUILD)
set_target_properties(${target} PROPERTIES UNITY_BUILD OFF)
endif()
endfunction()
function(qt_is_imported_target target out_var)

View File

@ -15,6 +15,7 @@ function(qt_internal_add_benchmark target)
"${__qt_internal_add_executable_multi_args}"
)
_qt_internal_validate_all_args_are_parsed(arg)
_qt_internal_validate_no_unity_build(arg)
qt_remove_args(exec_args
ARGS_TO_REMOVE
@ -239,6 +240,8 @@ function(qt_internal_add_test_to_batch batch_name name)
arg "${optional_args}" "${single_value_args}" "${multi_value_args}" ${ARGN})
qt_internal_prepare_test_target_flags(version_arg exceptions_text gui_text ${ARGN})
_qt_internal_validate_no_unity_build(arg)
_qt_internal_test_batch_target_name(target)
# Lazy-init the test batch
@ -324,6 +327,7 @@ function(qt_internal_add_test_to_batch batch_name name)
ENABLE_AUTOGEN_TOOLS ${arg_ENABLE_AUTOGEN_TOOLS}
DISABLE_AUTOGEN_TOOLS ${arg_DISABLE_AUTOGEN_TOOLS}
NO_UNITY_BUILD # Tests should not be built using UNITY_BUILD
NO_UNITY_BUILD_SOURCES ${arg_SOURCES}
)
foreach(source ${arg_SOURCES})
@ -426,6 +430,7 @@ function(qt_internal_add_test name)
"${multi_value_args}"
)
_qt_internal_validate_all_args_are_parsed(arg)
_qt_internal_validate_no_unity_build(arg)
set(batch_current_test FALSE)
if(QT_BUILD_TESTS_BATCHED AND NOT arg_NO_BATCH AND NOT arg_QMLTEST AND NOT arg_MANUAL

View File

@ -1,6 +1,15 @@
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
# Check whether no unity build is requested where it is disabled by default.
function(_qt_internal_validate_no_unity_build prefix)
if(${prefix}_NO_UNITY_BUILD OR ${prefix}_NO_UNITY_BUILD_SOURCES)
message(WARNING
"Unity build is disabled by default for this target, and its sources. "
"You may remove the NO_UNITY_BUILD and/or NO_UNITY_BUILD_SOURCES arguments.")
endif()
endfunction()
function(qt_update_ignore_unity_build_sources target sources)
if (sources)
set_source_files_properties(${sources} PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)