VS: Precompiled headers in large projects causes significant unmeasured delay
When adding precompiled headers with target_precompile_headers
, Cmake will add the force include to each file individually rather than the project itself. This property is also added for each configuration, which can lead to significant increases in project file size and configure/generate time, even on incremental configures (although this isn't correctly measured by cmake, see below)
The issue should be noticable enough with this project setup, which generates 10k source files and adds some extra custom configurations to make it more obvious:
cmake_minimum_required(VERSION 3.26)
# Create a bunch of source files
if (NOT EXISTS src)
file(MAKE_DIRECTORY src)
foreach(I RANGE 10000)
file(TOUCH src/test${I}.cpp)
endforeach()
endif()
# Add some custom configuration types
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES};other;another;and_another")
project(pch)
file(GLOB SOURCES CONFIGURE_DEPENDS src/*.cpp)
add_executable(pch ${SOURCES})
target_precompile_headers(pch PRIVATE <iostream>)
Configuring this project (incrementally, not clean/fresh) with CMAKE_DISABLE_PRECOMPILE_HEADERS
set takes 1.1s on my machine (although cmake only reports 0.2s for the generating phase), and the pch.vcxproj file is ~10k lines
The same test with PCH enabled takes 3.5s (0.1 and 0.9 reported by cmake for configure/generate respectively) and the vcxproj file goes up to ~90k lines.
I appreciate there may be good reasons for the properties to be set on source files as they may be different files for different languages etc, but it's causing over 1 minute delay on every configure for our actual project which is a significant hit.
If this can't be changed, is there any way to control it so that it only sets the property on the project rather than the source files, or do we just have to set up the PCH manually without using the cmake feature?