Add support for new clang flags for PCH in `target_precompile_headers()`
In Clang 11 (in RC2 right now) there a few new options for the PCH added that will speed up compilation even more:
-fpch-instantiate-templates tries to instantiate templates already while generating a precompiled header. Such templates do not need to be instantiated every time the precompiled header is used, which saves compile time. This may result in an error during the precompiled header generation if the source header file is not self-contained. This option is enabled by default for clang-cl. -fpch-codegen and -fpch-debuginfo generate shared code and/or debuginfo for contents of a precompiled header in a separate object file. This object file needs to be linked in, but its contents do not need to be generated for other objects using the precompiled header. This should usually save compile time. If not using clang-cl, the separate object file needs to be created explicitly from the precompiled header. Example of use: $ clang++ -x c++-header header.h -o header.pch -fpch-codegen -fpch-debuginfo $ clang++ -c header.pch -o shared.o $ clang++ -c source.cpp -o source.o -include-pch header.pch $ clang++ -o binary source.o shared.o Using -fpch-instantiate-templates when generating the precompiled header usually increases the amount of code/debuginfo that can be shared. In some cases, especially when building with optimizations enabled, using -fpch-codegen may generate so much code in the shared object that compiling it may be a net loss in build time. Since headers may bring in private symbols of other libraries, it may be sometimes necessary to discard unused symbols (such as by adding -Wl,–gc-sections on ELF platforms to the linking command, and possibly adding -fdata-sections -ffunction-sections to the command generating the shared object).
It would be very neat if CMake supported these options.
The first option
-fpch-instantiate-templates can be set by just adding it to the CMAKE_CXX_FLAGS - but the codegen option needs to have more logic that can build the shared object and link it to the target. This can probably be done with custom logic - but I am not sure if we can currently set CXXFLAGS specifically for the PCH compilation.