diff --git a/CMakeLists.txt b/CMakeLists.txt index 537c68ddd..45b539de4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,6 +145,34 @@ else() ) endif() +# Optional precompiled headers -- see QET_ENABLE_PCH in +# cmake/developer_options.cmake for what this trades away. +# +# target_precompile_headers() needs CMake 3.16; the project still declares a +# 3.5 minimum, so guard rather than raise it for an opt-in developer feature. +# +# The generator expressions are load-bearing, not decoration: this target also +# compiles the 18 C files of the bundled LZMA decoder +# (sources/import/edz/lzma/*.c), and an unguarded list applies to every +# language in the target, so the Qt headers would reach the C compiler and fail +# with "unknown type name 'namespace'". $ is required because a +# literal '>' would terminate the generator expression. +if(QET_ENABLE_PCH) + if(CMAKE_VERSION VERSION_LESS 3.16) + message(WARNING + "QET_ENABLE_PCH needs CMake 3.16 or newer (found ${CMAKE_VERSION}); " + "building without precompiled headers.") + else() + target_precompile_headers(${PROJECT_NAME} PRIVATE + "$<$:>" + "$<$:>" + "$<$:>" + "$<$:>" + ) + message(STATUS "QET_ENABLE_PCH: precompiled headers enabled") + endif() +endif() + target_link_libraries( ${PROJECT_NAME} PUBLIC diff --git a/cmake/developer_options.cmake b/cmake/developer_options.cmake index 04f002da4..cec095273 100644 --- a/cmake/developer_options.cmake +++ b/cmake/developer_options.cmake @@ -33,3 +33,17 @@ add_definitions(-DQT_MESSAGELOGCONTEXT) # Build with KF5 option(BUILD_WITH_KF5 "Build with KF5" ON) + +# Precompiled headers for the Qt umbrella headers. +# +# Off by default and intended for local development only. Building QET is +# dominated by re-parsing Qt's headers: a 214-line .cpp expands to ~198,000 +# preprocessed lines, and compiling one translation unit costs ~4.1 s of which +# only ~0.35 s is optimisation (-O0 instead of -O3 saves 8%). A PCH caches the +# parsed header state and takes that ~4.1 s down to ~1.2 s. +# +# It is deliberately NOT on by default: a PCH satisfies includes that a source +# file forgot to make itself, so code written with it enabled can fail to +# compile for everyone else. Leaving it off keeps CI and contributors on the +# strict behaviour, and only developers who opt in trade that for the speed. +option(QET_ENABLE_PCH "Use precompiled headers (developer build speed; may mask missing #includes)" OFF)