[CMake] Fwd: How to have generated source compiled multiple ways?

Eric Noulard eric.noulard at gmail.com
Mon Aug 12 18:24:26 EDT 2013


2013/8/12 Paul Smith <paul at mad-scientist.net>:
> On Mon, 2013-08-12 at 22:35 +0200, Eric Noulard wrote:
>> 2013/8/12 Paul Smith <paul at mad-scientist.net>:
>> >
>> > So I have tried using add_custom_command() with OUTPUT, but the
>> > documentation says:
>> >
>> > "Do not list the output in more than one independent target that may build
>> > in parallel or the two instances of the rule may conflict (instead use
>> > add_custom_target to drive the command and make the other targets depend on
>> > that one)."
>> >
>> > Sure enough, it seems that if I have my different libraries depend on these
>> > outputs I get problems during parallel builds.  So then I tried to use
>> > add_custom_target(), but that says:
>> >
>> > "The target has no output file and is ALWAYS CONSIDERED OUT OF DATE"
>> >
>> > and, sure enough, if I do it this way my source files (and everything that
>> > depends on them) rebuild every time I run the build, even if nothing has
>> > changed.  This is a big bummer.
>>
>> I did never tried befoire but think you should "simply" keep your
>> current add_custom_command and create a new target with
>> add_custom_target whose only purpose is to "serialize" the dependency
>> for the 2 (or more) independent libraries which use the output of your
>> custom command concurrently.
>
>> add_custom_target(SerializeTarget
>>   DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/generated.c)
>>
>> add_library(MyLib1 ${CMAKE_CURRENT_BINARY_DIR}/generated.c lib1.c)
>> set_target_properties(MyLib1 PROPERTIES COMPILE_FLAGS -DDEFGEN=3)
>> add_dependencies(MyLib1 SerializeTarget)
>>
>> add_library(MyLib2 ${CMAKE_CURRENT_BINARY_DIR}/generated.c lib2.c)
>> set_target_properties(MyLib2 PROPERTIES COMPILE_FLAGS -DDEFGEN=4)
>> add_dependencies(MyLib2 SerializeTarget)
>
> OK, maybe I'm misunderstanding but if the SerializeTarget is considered
> "always out of date" because it was defined with add_custom_target()
> then won't that cause the two libraries to always rebuild, since
> something they depend on is always out of date?  Maybe I'm thinking of
> it too much like make.

This is puzzling but as far as I understand it, since SerializeTarget
has no output
it cannot trigger the rebuilt of a target (nioether Lib1 nor Lib2),
however whenever
you need (or ask) to build Lib1 or Lib2 then SerializeTarget has to be executed
and consequently the add_custom_command will be if the "generated.c" file is
out of date.

i.e. if you build Lib1 or Lib2 then SerializeTarget will be built.
Building Lib1 target does not mean recompilation will occur
because the "file dependency" is automatically handled by
cmake using the source files arguments of the add_library call.

so you may have:
$ make
[ 14%] Built target SerializeTarget
[ 57%] Built target MyLib1
[100%] Built target MyLib2

every expected target is built (because of the target dependencies) but
no compilation occurs because none is necessary.

but
$ rm generated.c
$ make
[ 14%] Generating generated.c
[ 14%] Built target SerializeTarget
Scanning dependencies of target MyLib1
[ 28%] Building C object CMakeFiles/MyLib1.dir/generated.c.o
Linking C static library libMyLib1.a
[ 57%] Built target MyLib1
Scanning dependencies of target MyLib2
[ 71%] Building C object CMakeFiles/MyLib2.dir/generated.c.o
Linking C static library libMyLib2.a
[100%] Built target MyLib2

works as expected, and
$ touch ../lib1.c
erk at ct-wdtim102h:b$ make
[ 14%] Built target SerializeTarget
Scanning dependencies of target MyLib1
[ 28%] Building C object CMakeFiles/MyLib1.dir/lib1.c.o
Linking C static library libMyLib1.a
[ 57%] Built target MyLib1
[100%] Built target MyLib2

as well.

> I'll look at the stuff you've sent and try to adjust it for my
> situation.

It should work.

-- 
Erk
L'élection n'est pas la démocratie -- http://www.le-message.org


More information about the CMake mailing list