2
votes

I'm trying to figure out how to maintain dependencies of my precompiled headers. It includes STL headers, some third-parties like boost and some of our rarely changing infrastructure headers.

I came out with something like this

SET(PCH_DIR ${CMAKE_CURRENT_BINARY_DIR})
SET(PCH_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/../include/server/server.h)
SET(PCH_DST server.h.gch)
ADD_CUSTOM_TARGET(serverPCH DEPENDS ${PCH_DST})
ADD_CUSTOM_COMMAND(OUTPUT ${PCH_DST} ${PCH_DEP}
                   COMMAND ${CMAKE_CXX_COMPILER} -x c++-header ${COMMON_CXXFLAGS} ${COMPILER_DEFINITIONS} -std=gnu++1z -c ${PCH_HEADER} -o ${PCH_DST} -I${CMAKE_SOURCE_DIR}/lib/include/server -I${CMAKE_SOURCE_DIR}/lib/include
                   MAIN_DEPENDENCY ${PCH_HEADER}
                   WORKING_DIRECTORY ${PCH_DIR}
                   COMMENT "Building precompiled header"
                   VERBATIM)

Looks like its doing its job and it gets recompiled once the header is edited. However, PCH recompilation is not triggered when one of files included in server.h is changed. Is there a way to trigger re-compilation if any of headers included in server.h is changed?

1
You need to add a dependency on server.h.gch for all your *.o object files. I don't know how to do that in cmakeBasile Starynkevitch
@BasileStarynkevitch I know how to do it, but 1) it is ugly :) 2) I dont think it will trigger PCH rebuild since from the CMake point of view the server.h.gch is up to date since it knows nothing about its included headerskreuzerkrieg
You also need to add a dependency from server.h and all included headers there to server.h.gchBasile Starynkevitch
@BasileStarynkevitch dependency from server.h and all included headers there to server.h.gch Shouldn't it be vice versa (i.e. dependency of server.h.gch from server.h)? I believe I got your wording wrong.Scheff's Cat
@BasileStarynkevitch, like adding (and maintaining) myriads of include files in MAIN_DEPENDENCY?kreuzerkrieg

1 Answers

0
votes

Well, 2 years later. CMake now supports precompiled headers and unity builds.