[CMake] Generated source files and dependencies(+)

ZNV mejedi at gmail.com
Sun Sep 14 09:14:15 EDT 2008


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.

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?

I very much hope so, since 'generated.h' could be included indirectly,
ie from another header file.
Imagine someone includes a random header file and project now depends
on 'generated.h' since
that random header includes 'generated.h' either directly or not. The
need to track these issues
manually is so scary!

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)?

Anyway, I do not understand, how #include dependencies created by
generated files are handled.
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?

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?

WBR, Mejedi


More information about the CMake mailing list