[CMake] Getting add_custom_command to work with multiple outputs

Roger Leigh rleigh at codelibre.net
Sat Aug 3 06:20:53 EDT 2013


Hi folks,

I'm very new to cmake, so apologies if this is an obvious question,
but I haven't found a way to do this after reading the documentation,
and a lot of googling.

I am trying to cmake-ify a build where the bulk of the sources are
generated.  I have a tool (python script) which reads a set of
templates and then generates many source and header files (~250).
I don't know in advance what the source files will be--it's
dependent upon the templates.  I've done this:

set(GEN_DIR gen)
set(GENERATOR path/to/script)
set(TEMPLATES template1 template2 template3)

execute_process(COMMAND ${GENERATOR} -d -o ${GEN_DIR} ${TEMPLATES}
  OUTPUT_VARIABLE GENERATED_SOURCES)

execute_process(COMMAND ${CMAKE} -E echo ${GENERATED_SOURCES})

This part works, running the generator in "dry run" mode to output
a list of files which will be generated, but not actually doing any
generation.  Running cmake gives me output like this:
gen/ome/xml/model/OME.cpp
gen/ome/xml/model/OME.h
gen/ome/xml/model/BinaryOnly.cpp
gen/ome/xml/model/BinaryOnly.h
gen/ome/xml/model/Image.cpp
gen/ome/xml/model/Image.h
gen/ome/xml/model/Pixels.cpp
gen/ome/xml/model/Pixels.h
gen/ome/xml/model/Plane.cpp
gen/ome/xml/model/Plane.h
gen/ome/xml/model/Channel.cpp
gen/ome/xml/model/Channel.h
gen/ome/xml/model/MetadataOnly.cpp
gen/ome/xml/model/MetadataOnly.h

So far, so good.  I've then tried to make the source generation occur
with a custom command.  This includes the templates as dependencies so
that the source will be regenerated if the templates change.

add_custom_command(OUTPUT ${GENERATED_SOURCES}
  COMMAND {GENERATOR} -o ${GEN_DIR} ${TEMPLATES}
  DEPENDS ${TEMPLATES}
  COMMENT Generate sources)

add_library(test SHARED ${GENERATED_SOURCES})

But this results in a broken Makefile.

gen/ome/xml/model/OME.cpp
gen/ome/xml/model/OME.h
gen/ome/xml/model/BinaryOnly.cpp
gen/ome/xml/model/BinaryOnly.h
gen/ome/xml/model/Image.cpp
gen/ome/xml/model/Image.h
gen/ome/xml/model/Pixels.cpp
gen/ome/xml/model/Pixels.h
[…]
: template1

i.e. the rules are broken due to being split over multiple lines.

I'm obviously doing something quite wrong here, but I'm not sure what.
Or indeed, if I'm approaching this from the right direction--maybe I
should be doing this completely differently.

Any thoughts and suggestions would be greatly appreciated!


Many thanks,
Roger

-- 
  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130803/51a18013/attachment.pgp>


More information about the CMake mailing list