[CMake] Creating file with dependencies

Bill Hoffman bill.hoffman at kitware.com
Wed Apr 8 14:54:59 EDT 2009


Jon Schewe wrote:
> I recently started using cmake and find it generally to be a nice tool,
> however I'm rather frustrated with it now. Can someone please tell me
> how to get cmake to conditionally create a file as part of executing a
> target? What I want to do looks like this, but apparently
> add_custom_command only can execute shell scripts, so I'm not sure how
> one can use these nice commands for writing files.
> 
custom command can run any program you want??   You can even run cmake 
-P and run a cmake script.
> FUNCTION(build_includes)
>   FILE(WRITE unit_test_includes.hpp "// Autogenerated - do not edit\n")
>   FOREACH(test ${TESTS})
>     FILE(APPEND unit_test_includes.hpp "#include \"${test}\"\n")
>   ENDFOREACH(test)
> ENDFUNCTION(build_includes)
> 
Put that in a file mycmake.cmake
> 
> ADD_CUSTOM_COMMAND(OUTPUT unit_test_includes.hpp
>                            COMMAND build_includes
>                                      DEPENDS ${TESTS}
>                                      COMMENT "Building
> unit_test_includes.hpp"
>                                      )

Run ${CMAKE_COMMAND} -P mycmake.cmake as the COMMAND for your custom 
command.

> 
> And then unit_test_includes.cpp is included in one of my hand written
> cpp files that is used to build the target.
> 
> Of course what doesn't work is the COMMAND of ADD_CUSTOM_COMMAND. How
> does one do this with cmake? I'd rather not write the shell script to do
> this because then it won't work on Windows systems.
> 


More information about the CMake mailing list