Question about the precompiled-header feature of CMake 3.16.x

I started to use the precompiled-header feature introduced with Make 3.16.x (Thank you for this!)
It all works fine when using it in the planned way. What I would like to achieve is to extract the compile instruction used for a target that makes use of the precompiled headers and write it to a file. I require this file within a just-in-time compilation framework.

See this mini example:
add_executable(demo demo.cpp)
target_precompile_headers(demo PUBLIC “${CMAKE_CURRENT_SOURCE_DIR}/demo.hpp”)

What I tried so far is this
add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo “$<FILTER:$<TARGET_OBJECTS:demo>,INCLUDE, *.${CMAKE_PCH_EXTENSION}>”)

which outputs:

Is it possible to strip away the “.gch” suffix inside the generator expression? If so, I could piece together the compile instructions from CMAKE_CXX_COMPILE_OPTIONS_CREATE_PCH (-Winvalid-pch-xc+±header-include<PCH_HEADER>).

Alternatively, can I evaluate CMAKE_CXX_COMPILE_OPTIONS_CREATE_PCH for the target “demo” so that <PCH_HEADER> gets replaced by /home/<…>/build-gcc-5.5.0/libket/CMakeFiles/demo.dir/cmake_pch.hxx?

Thanks very much in advanced,

This sounds like something CMake should probably add to its compile_commands.json output. It’s probably worth an issue if one doesn’t already exist.

Cc: @cristianadam

Note that the compilation database format may not have an easy answer to some of the questions about where we’d put the information we have. It is TU-oriented, so you may just get the conglomerate input file rather than a list of individual headers in the format. This may be sufficient, but I’m not 100% sure.

compile_commands.json will contain the command line used to compile the PCH. See CMAKE_EXPORT_COMPILE_COMMANDS.

The cmake-file-api can also be used to query the information.

CMAKE_CXX_COMPILE_OPTIONS_CREATE_PCH variable is for CMake internal usage, and it’s not available via generator expressions.