2

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?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
kreuzerkrieg
  • 3,009
  • 3
  • 28
  • 59
  • 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 `cmake` – Basile Starynkevitch Oct 15 '18 at 05:08
  • @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 headers – kreuzerkrieg Oct 15 '18 at 05:25
  • You also need to add a dependency from `server.h` and all included headers there to `server.h.gch` – Basile Starynkevitch Oct 15 '18 at 05:32
  • @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 Oct 15 '18 at 05:39
  • @BasileStarynkevitch, like adding (and maintaining) myriads of include files in `MAIN_DEPENDENCY`? – kreuzerkrieg Oct 15 '18 at 05:47
  • I am not familiar enough with `cmake` to help you more. And I don't like much `cmake` for those reasons – Basile Starynkevitch Oct 15 '18 at 06:13

1 Answers1

0

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

kreuzerkrieg
  • 3,009
  • 3
  • 28
  • 59