Use pre-compiled headers when building Qt with cmake

Some modules define their own manually-maintained lists, and we can rely
on the headers generated by each module to include in the pch as well
e.g. QtCore/QtCore.

There's also e.g. QtWidgetDepends for QtWidgets, but this only
works for modules, not for tools, examples or other applications.
For now we'll use the Qt<Module>/Qt<Module> headers for the
modules we depend on.

Building with PCH can be disabled with -DBUILD_WITH_PCH=NO, and it only
works for versions of CMake newer than 3.15.20190829.

Change-Id: Iae52bd69acfdfd58f4cd20d3cfa3c7f42775f732
Reviewed-by: Qt CMake Build Bot
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Mårten Nordheim 2019-07-05 10:47:26 +02:00 committed by Alexandru Croitor
parent 7df0868ad5
commit 2cf0ba1fba
21 changed files with 95 additions and 19 deletions

View File

@ -41,6 +41,9 @@ set(QT_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
## Should this Qt be built with Werror?
option(WARNINGS_ARE_ERRORS "Build Qt with warnings as errors" ${FEATURE_developer_build})
## Should Qt be built using PCH?
option(BUILD_WITH_PCH "Build Qt using precompiled headers?" ON)
## Decide whether tools will be built.
qt_check_if_tools_will_be_built()

View File

@ -915,6 +915,33 @@ function(qt_register_target_dependencies target public_libs private_libs)
set_target_properties("${target}" PROPERTIES _qt_target_deps "${target_deps}")
endfunction()
function(qt_update_precompiled_header target precompiled_header)
if (precompiled_header AND BUILD_WITH_PCH)
set_property(TARGET "${target}" APPEND PROPERTY "PRECOMPILE_HEADERS" "${precompiled_header}")
endif()
endfunction()
function(qt_update_precompiled_header_with_library target library)
if (TARGET "${library}")
get_target_property(TARGET_TYPE "${library}" TYPE)
if (NOT TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
get_target_property(HEADER "${library}" MODULE_HEADER)
qt_update_precompiled_header("${target}" "${HEADER}")
endif()
endif()
endfunction()
function(qt_update_ignore_pch_source target sources)
if (sources)
set_source_files_properties(${sources} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
endif()
endfunction()
function(qt_ignore_pch_obj_c_sources target sources)
list(FILTER sources INCLUDE REGEX "\\.mm$")
qt_update_ignore_pch_source("${target}" "${sources}")
endfunction()
# This function can be used to add sources/libraries/etc. to the specified CMake target
# if the provided CONDITION evaluates to true.
function(extend_target target)
@ -927,8 +954,8 @@ function(extend_target target)
if (NOT TARGET "${target}")
message(FATAL_ERROR "Trying to extend non-existing target \"${target}\".")
endif()
qt_parse_all_arguments(arg "extend_target" "HEADER_MODULE" ""
"CONDITION;${__default_public_args};${__default_private_args};COMPILE_FLAGS" ${ARGN})
qt_parse_all_arguments(arg "extend_target" "HEADER_MODULE" "PRECOMPILED_HEADER"
"CONDITION;${__default_public_args};${__default_private_args};COMPILE_FLAGS;NO_PCH_SOURCES" ${ARGN})
if ("x${arg_CONDITION}" STREQUAL x)
set(arg_CONDITION ON)
endif()
@ -950,6 +977,7 @@ function(extend_target target)
endforeach()
foreach(lib ${arg_PUBLIC_LIBRARIES} ${arg_LIBRARIES})
qt_update_precompiled_header_with_library("${target}" "${lib}")
string(REGEX REPLACE "_nolink$" "" base_lib "${lib}")
if(NOT base_lib STREQUAL lib)
qt_create_nolink_target("${base_lib}" ${target})
@ -1034,6 +1062,11 @@ function(extend_target target)
ENABLE_AUTOGEN_TOOLS ${arg_ENABLE_AUTOGEN_TOOLS}
DISABLE_AUTOGEN_TOOLS ${arg_DISABLE_AUTOGEN_TOOLS})
qt_update_precompiled_header("${target}" "${arg_PRECOMPILED_HEADER}")
qt_update_ignore_pch_source("${target}" "${arg_NO_PCH_SOURCES}")
## Ignore objective-c files for PCH (not supported atm)
qt_ignore_pch_obj_c_sources("${target}" "${arg_SOURCES}")
else()
if(QT_CMAKE_DEBUG_EXTEND_TARGET)
message("extend_target(${target} CONDITION ${arg_CONDITION} ...): Skipped")
@ -1218,8 +1251,8 @@ function(add_qt_module target)
# Process arguments:
qt_parse_all_arguments(arg "add_qt_module"
"NO_MODULE_HEADERS;STATIC;DISABLE_TOOLS_EXPORT;EXCEPTIONS;INTERNAL_MODULE;NO_SYNC_QT;NO_PRIVATE_MODULE;HEADER_MODULE"
"CONFIG_MODULE_NAME"
"${__default_private_args};${__default_public_args};QMAKE_MODULE_CONFIG;EXTRA_CMAKE_FILES;EXTRA_CMAKE_INCLUDES" ${ARGN})
"CONFIG_MODULE_NAME;PRECOMPILED_HEADER"
"${__default_private_args};${__default_public_args};QMAKE_MODULE_CONFIG;EXTRA_CMAKE_FILES;EXTRA_CMAKE_INCLUDES;NO_PCH_SOURCES" ${ARGN})
if(NOT DEFINED arg_CONFIG_MODULE_NAME)
set(arg_CONFIG_MODULE_NAME "${module_lower}")
@ -1275,6 +1308,8 @@ function(add_qt_module target)
set_property(TARGET "${target}" APPEND PROPERTY PUBLIC_HEADER "${module_headers_public}")
set_property(TARGET "${target}" APPEND PROPERTY PUBLIC_HEADER "${module_include_dir}/${module}Depends")
set_property(TARGET "${target}" APPEND PROPERTY PRIVATE_HEADER "${module_headers_private}")
set_property(TARGET "${target}" PROPERTY MODULE_HEADER "${module_include_dir}/${module}")
if(module_headers_qpa)
qt_install(FILES ${module_headers_qpa} DESTINATION ${INSTALL_INCLUDEDIR}/${module}/${PROJECT_VERSION}/${module}/qpa)
endif()
@ -1369,6 +1404,8 @@ function(add_qt_module target)
MOC_OPTIONS ${arg_MOC_OPTIONS}
ENABLE_AUTOGEN_TOOLS ${arg_ENABLE_AUTOGEN_TOOLS}
DISABLE_AUTOGEN_TOOLS ${arg_DISABLE_AUTOGEN_TOOLS}
PRECOMPILED_HEADER ${arg_PRECOMPILED_HEADER}
NO_PCH_SOURCES ${arg_NO_PCH_SOURCES}
)
if(NOT ${arg_EXCEPTIONS} AND NOT ${arg_HEADER_MODULE})

View File

@ -116,12 +116,13 @@ add_qt_tool(qmake # special case
library
$<TARGET_PROPERTY:Qt::CorePrivate,INTERFACE_INCLUDE_DIRECTORIES> # special case
${CMAKE_BINARY_DIR}/src/corelib/global # special case: for qconfig.cpp
PRECOMPILED_HEADER
"qmake_pch.h"
)
qt_internal_add_target_aliases(Bootstrap) # special case
#### Keys ignored in scope 1:.:.:qmake.pro:<TRUE>:
# PRECOMPILED_HEADER = "qmake_pch.h"
# _OPTION = "host_build"
## Scopes:

View File

@ -29,11 +29,11 @@ add_qt_module(Concurrent
Qt::CorePrivate
PUBLIC_LIBRARIES
Qt::Core
PRECOMPILED_HEADER
"../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:concurrent.pro:<TRUE>:
# CONFIG = "exceptions"
# PRECOMPILED_HEADER = "../corelib/global/qt_pch.h"
# _LOADED = "qt_module"
## Scopes:

View File

@ -250,6 +250,11 @@ add_qt_module(Core
PUBLIC_LIBRARIES # special case:
Qt::Platform # special case:
DISABLE_TOOLS_EXPORT # special case:
PRECOMPILED_HEADER
global/qt_pch.h
NO_PCH_SOURCES
text/qstring_compat.cpp
tools/qvector_msvc.cpp
# special case begin
# Generated in QtBaseGlobalTargets
EXTRA_CMAKE_FILES ${QT_CORE_RESOURCE_GENERATED_FILE_PATH}

View File

@ -264,6 +264,10 @@ add_qt_module(Gui
ZLIB::ZLIB
PUBLIC_LIBRARIES
Qt::Core
NO_PCH_SOURCES
"painting/qdrawhelper.cpp"
PRECOMPILED_HEADER
"kernel/qt_gui_pch.h"
)
# Resources:

View File

@ -60,6 +60,8 @@ add_qt_module(Network
ZLIB::ZLIB
PUBLIC_LIBRARIES
Qt::Core
PRECOMPILED_HEADER
"../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:network.pro:<TRUE>:

View File

@ -15,10 +15,11 @@ add_qt_module(EdidSupport
Qt::CorePrivate
PUBLIC_LIBRARIES
Qt::Core
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:edid.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "edid_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"

View File

@ -19,12 +19,13 @@ add_qt_module(EglSupport
Qt::CorePrivate
Qt::GuiPrivate
EGL::EGL # special case
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:eglconvenience.pro:<NONE>:
# CONFIG = "static" "internal_module" "egl"
# MODULE = "egl_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"
## Scopes:

View File

@ -17,12 +17,13 @@ add_qt_module(EventDispatcherSupport
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:eventdispatchers.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "eventdispatcher_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"
## Scopes:

View File

@ -21,10 +21,11 @@ add_qt_module(FbSupport
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:fbconvenience.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "fb_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"

View File

@ -18,12 +18,13 @@ add_qt_module(FontDatabaseSupport
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:fontdatabases.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "fontdatabase_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"
## Scopes:

View File

@ -22,12 +22,13 @@ add_qt_module(InputSupport
Qt::Core
Qt::DeviceDiscoverySupport
Qt::Gui
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 3:.:.:input-support.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "input_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"
## Scopes:

View File

@ -19,12 +19,13 @@ add_qt_module(XkbCommonSupport
Qt::Core
Qt::Gui
XKB::XKB
PRECOMPILED_HEADER
"../../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:xkbcommon.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "xkbcommon_support"
# PRECOMPILED_HEADER = "../../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"
## Scopes:

View File

@ -39,10 +39,11 @@ add_qt_module(LinuxAccessibilitySupport
Qt::Core
Qt::DBus
Qt::Gui
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:linuxaccessibility.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "linuxaccessibility_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"

View File

@ -17,12 +17,13 @@ add_qt_module(ServiceSupport
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:services.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "service_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"
## Scopes:

View File

@ -20,10 +20,11 @@ add_qt_module(VulkanSupport
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
PRECOMPILED_HEADER
"../../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:vkconvenience.pro:<TRUE>:
# CONFIG = "static" "internal_module"
# MODULE = "vulkan_support"
# PRECOMPILED_HEADER = "../../corelib/global/qt_pch.h"
# _LOADED = "qt_module"

View File

@ -27,11 +27,12 @@ add_qt_module(Sql
Qt::CorePrivate
PUBLIC_LIBRARIES
Qt::Core
PRECOMPILED_HEADER
"../corelib/global/qt_pch.h"
)
#### Keys ignored in scope 1:.:.:sql.pro:<TRUE>:
# MODULE_PLUGIN_TYPES = "sqldrivers"
# PRECOMPILED_HEADER = "../corelib/global/qt_pch.h"
# SQL_P = "sql"
# _LOADED = "qt_module"

View File

@ -63,6 +63,8 @@ add_qt_module(Widgets
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
PRECOMPILED_HEADER
"kernel/qt_widgets_pch.h"
)
add_dependencies(Widgets ${QT_CMAKE_EXPORT_NAMESPACE}::uic) # special case

View File

@ -19,7 +19,6 @@ add_qt_module(Xml
)
#### Keys ignored in scope 1:.:.:xml.pro:<TRUE>:
# PRECOMPILED_HEADER = <EMPTY>
# _LOADED = "qt_module"
## Scopes:

View File

@ -1534,6 +1534,18 @@ def write_sources_section(cm_fh: typing.IO[str], scope: Scope, *,
for mo in moc_options:
cm_fh.write('{} "{}"\n'.format(ind, mo))
precompiled_header = scope.get('PRECOMPILED_HEADER')
if precompiled_header:
cm_fh.write('{} PRECOMPILED_HEADER\n'.format(ind))
for header in precompiled_header:
cm_fh.write('{} "{}"\n'.format(ind, header))
no_pch_sources = scope.get('NO_PCH_SOURCES')
if no_pch_sources:
cm_fh.write('{} NO_PCH_SOURCES\n'.format(ind))
for source in no_pch_sources:
cm_fh.write('{} "{}"\n'.format(ind, source))
def is_simple_condition(condition: str) -> bool:
return ' ' not in condition \