[CMake] Problem with generated source and header files

Tyler Roscoe tyler at cryptio.net
Tue Jun 30 13:05:55 EDT 2009


On Tue, Jun 30, 2009 at 06:53:07PM +0200, Jörg Förstner wrote:
> The generator now is called EVERY TIME I run make, because
> - I need two separate CMakeLists.txt files now,
> - I have to make a custom target, otherwise CMake does not know "everything.cpp" and quits with an error.
> - the custom target is ALWAYS considered out-of-date.
> 
> --- File1: generator/CMakeLists.txt ---
> ADD_CUSTOM_COMMAND(
>   OUTPUT  everything.cpp
>   COMMAND generator.rb --output everything.cpp
>   VERBATIM
> )
> ADD_CUSTOM_TARGET( generate_it DEPENDS everything.cpp )
> ---------------------------------
> 
> --- File2: libxyz.so/CMakeLists.txt ---
> # Define a target shared object library
> ADD_LIBRARY( xyz.generated SHARED everything.cpp )
> 
> # Let CMake know that everything.cpp is a generated file
> SET_SOURCE_FILES_PROPERTIES( everything.cpp PROPERTIES GENERATED TRUE )
> 
> # Add dependencies between the library here and the custom target
> # from the other CMakeLists.txt file
> ADD_DEPENDENCIES( xyz.generated generate_it )
> 
> # Additional libraries to be linked to the target
> TARGET_LINK_LIBRARIES(
>   xyz.generated
>   lib1
>   lib2
>   ...
> )
> ---------------------------------
> 
> How can I have a custom target which is NOT built every time I call 'make'?

I think adding some appropriate DEPENDS to your custom_command should
help.

> How can I use a target from another directory, which is only build if it's necessary?

add_subdirectory() and maybe appropriate add_dependencies().

tyler


More information about the CMake mailing list