[CMake] Generated source files and dependencies(+)

Esben Mose Hansen kde at mosehansen.dk
Mon Sep 15 03:15:44 EDT 2008


On Sunday 14 September 2008 15:14:15 ZNV wrote:
> Hi!
>
> I am generating header file for subsequent use by multiple
> libraries/executables in CMake-controlled build.
> My preliminary solution which happens to work is the following; I
> wonder if it is correct.

If you look at my google protocols buffer module (which I submitted a little 
above), it uses the same strategy

>
> project(FOO)
> add_custom_target(generate ALL DEPENDS ${FOO_BINARY_DIR}/generated.h)
> add_custom_command(OUTPUT ${FOO_BINARY_DIR}/generated.h COMMAND ...)
> include_directories(${FOO_BINARY_DIR})
> add_library(foo foo.c) # foo.c includes generated.h
> add_library(bar bar.c) # bar.c includes generated.h as well
>
> The first question is, does CMake detect that 'foo' library depends on
> 'generated.h' automaticly?

Since it includes the generate.h, that should not be a problem. 

>
> The second question is, when are source/header files dependencies
> scanned? Does it happen
> during configure stage (ie when CMake runs)? Does it happen during
> build stage (when CMake-
> generated Makefile/whatever runs)?

In big projects, you can actually see CMake writing "scanning dependencies 
for". I think it does it before building each target.

> Anyway, I do not understand, how #include dependencies created by
> generated files are handled.

Same way. The dependencies are scanned before the target that depends on 
generated.h is compiled. It would seem to me that this means scanning the 
dependencies for libfoo twice, but I'm no expect in this.

> Assume 'foobar.c' is generated. For sure it includes something. During
> configure stage there is
> no 'foobar.c'. Do I have to manually enlist 'foobar.c' dependencies
> via OBJECT_DEPENDS property?
> Or may be 'foobar.c' is scanned on build stage, and everything just works
> fine?

The latter. It works fine with the protocols buffer this way, and also the 
FindQt.cmake seems to work like this (that include is somewhat involved, 
though)

>
> Finally, how do I control targets ordering during build? I have
> 'generate' target that generates 'generated.h'.
> I have 'foo' and 'bar' targets that use 'generated.h'. How can I force
> 'generate' to be build before 'foo' and 'bar'?

> Is ADD_DEPENDENCIES the only option? Maybe a clever hack exists to
> built the given target *before* all
> other project's targets?

That is what dependency tracking is for. And that should be handled 
automatically by the add_custom_target.

-- 
kind regards, Esben


More information about the CMake mailing list