Build CMake using PCH to speed up build
It seems that Precompiled Headers (PCH) are not being used in any of the CMakeLists from CMake, and I was wondering whether this was a deliberate choice. Enabling PCH seems like a very easy way to speed up the build of CMake itself.
To test this out, I added in Source/CMakeLists.txt
:
#Toggle precompiled headers on/off
option(enable-pch "Speed up the building of CMake with precompiled headers" OFF)
if(enable-pch)
#List of common STL headers (<string>,<memory>,etc), only for C++ with generator expressions
set(PCH_LIST
"$<$<COMPILE_LANGUAGE:CXX>:<string$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<functional$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<memory$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<fstream$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<istream$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<set$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<chrono$<ANGLE-R>>"
)
#Precompile headers for a few targets with many sources
target_precompile_headers(CMakeLib PRIVATE ${PCH_LIST})
target_precompile_headers(CTestLib PRIVATE ${PCH_LIST})
target_precompile_headers(CPackLib PRIVATE ${PCH_LIST})
endif()
As you can see, I only added PCH for a few "large" targets. I didn't bother messing with the REUSE_FROM keyword (and any problems that might give). The performance impact is not earth-shattering, but still a decent 20-30%:
#with PCH:
cmake .. -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -Denable-pch=1
time ninja -j24
real 0m15.153s
user 4m16.579s
sys 0m27.343s
#without PCH:
cmake .. -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -Denable-pch=0
time ninja -j24
real 0m19.265s
user 5m58.963s
sys 0m31.163s
I have not tested other compilers, nor have I tried to optimize the list of headers by analyzing a profiler output.
This might also benefit #23787.