[CMake] How to compile a file only when linking anyhow?

Alexander Neundorf a.neundorf-work at gmx.net
Wed Apr 16 13:43:51 EDT 2008


On Wednesday 16 April 2008, Mark Jonas wrote:
> Hi,
>
> what I am trying to achieve is that every time my application is
> linked a C file is compiled just before linking. Nothing should be
> done if linking is not necessary.
>
> The reason for wanting this is that I want to give the application a
> time stamp when it was linked. The time stamp should be a C string. A
> C file with this contents would do:
>
> char __build_timestamp__[]=  __DATE__ " " __TIME__ ;
>
> I tried the following but it does not do what I want. It generates and
> compiles the file only once.
>
> ADD_CUSTOM_COMMAND(
> OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.c
> COMMAND echo
> ARGS 'char __build_timestamp__[]=  __DATE__ \" \" __TIME__ \;' >
> ${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.c
> )
> SET( SRCS ${SRCS}
> ${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.c
> )
>
> ADD_EXECUTABLE(${EXENAME} ${SRCS})
>
> How do I tell the custom command to run when the linker will be
> called? It should not run always because that would cause the linker
> to be called whenever I call make.
>
> A dependency to ${SRCS} would not be enough because when only a .h
> file changes the linker would run but the generated C file wouldn't be
> recompiled.
>
> BTW, I am using CMake 2.6.

You could use something like:
ADD_CUSTOM_COMMAND(TARGET target POST_BUILD
                   COMMAND touch build_timestamp.c)

then the file will be newer than the executable everytime the executable is 
built (and have build_timestamp.c part of your regular set of source files).

Alex


More information about the CMake mailing list