[CMake] Dependencies to generated C header files in another CMake project

Brad King brad.king at kitware.com
Fri Dec 10 14:33:58 EST 2004


Stefan Granqvist wrote:
> I have the situation where one CMake project generates a couple of 
> header files, placing the resulting header files in 
> ${PROJECT_BINARY_DIR} for the project. The files are generated using one 
> ADD_CUSTOM_COMMAND for each header files to generate. In addition I also 
> have a corresponding ADD_CUSTOM_TARGET with a dummy target which depends 
> on the generated header file. My experiences is that the custom target 
> is needed to make the generated file to be produced also when if there 
> is no other target in the project using the generated file.  Let us call 
> this project P1.
> 
> I also have another CMake project (here called P2) that would also like 
> to use the generated header files from the P1. But I have failed to 
> setup a dependency in P2 that uses P1 to build the generated files. It 
> would be very nice if I could build P2 using make without having to 
> build P1 manually, prior to P2.
> 
> Both these projects are subdirectories in a CMake "main" CMake project 
> (P0).

We call the behavior to which you are referring "jump-and-build".  This 
is a feature introduced to help projects that have circular library 
dependencies, but it does not work as well as it might appear.

Assume for the moment that instead P1 has a library used by P2.  When 
you run make in the P2 directory the Makefile there does not have all of 
P1's dependency information.  Therefore the best it can do is have a 
rule to jump over to P1 and build its library ONLY WHEN IT DOES NOT 
EXIST, which will only happen the first time.  Once the library exists 
it is not guaranteed that running make in P2 will bring P1's library up 
to date.

The feature is implemented by keeping track of the directory containing 
any library or executable target using internal entries in the 
CMakeCache.txt.  These entries tell P2 about P1's library and its 
location.  There are not currently such entries for other target types 
such as those created by ADD_CUSTOM_TARGET.  Therefore these targets 
cannot be the destination of a "jump-and-build" rule.

It appears at first glance, however, that there would not be a problem 
with adding this feature to custom targets.  Since the custom target's 
file may not exist in P2's directory then it would always do the 
jump-and-build rule for this target, which is the behavior you want.

Please submit this as a feature request here:

http://www.cmake.org/Bug

Meanwhile you can create a dummy library in P1 that links a single 
source file that has no purpose but to depend on the generated header. 
Then I think you can create a dependency on this library in P2 to drive 
the header generation.  This should work around the problem for now. 
Note however that CMake's design philosophy is that it only guarantees 
an up-to-date build if one runs make from the top level build tree.

-Brad


More information about the CMake mailing list