Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - Automatically Parsing Dependencies of Precompiled Header?

Tags:

c++

c

gcc

cmake

As of yet, at least to my knowledge, there is no standard way in CMake to specify the addition of a precompiled header (PCH) to a project in a cross-platform manner because the way PCHs are handled by C++ compilers is very different among vendors. For G++, this is usually this is worked around by simply adding a custom command which takes care of invoking the compiler with the appropriate input and has it generate the PCH.

My current problem is that CMake will not parse the dependencies of the dependencies you specify for the custom command. For instance, assume the following structure:

pch.h
 |- dependA.h
 |- dependB.h
 ...

Only providing pch.h as a dependency will lead to the generation of the appropriate target in the corresponding makefile, which tracks changes to pch.h. However, CMake does not parse the includes inside pch.h and will therefore not recognize changes to dependA.h and dependB.h. This extends furhter if there are dependencies for dependsA.h and so on.

Note: I'm aware that the fact that PCH dependencies can and do change regularly puts the whole process in question. However, this is just the way it is and I can't really do anything about it.

Since the task isn't too hard, there are a couple of obvious ideas that come to mind:

Solution A:

Enter all the dependencies by hand. Obviously this works, but is tedious as hell and doesn't scale at all.

Solution B:

If possible, write a CMake function that automates the process and parse the includes "manually".

Solution C:

Do something similar using a different language, for instance Python, and just provide CMake a list of dependencies to add to the custom command.

Solution D:

Use gcc/g++'s feature to parse and print out the dependency tree of the PCH and parse the output to extract the list of dependencies.

My question is: does anyone know a more convenient and faster way to get this done?

like image 226
thokra Avatar asked Oct 14 '13 08:10

thokra


1 Answers

The IMPLICIT_DEPENDS option of the add_custom_command might do the trick:

add_custom_command(
    OUTPUT outFile
    COMMAND ...
    IMPLICIT_DEPENDS CXX "pch.h")

The IMPLICIT_DEPENDS option makes the generated build system scan the implicit dependencies of the given input file at build time. It is only supported for Makefile generators, though.

like image 132
sakra Avatar answered Oct 21 '22 07:10

sakra