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

Alexander Neundorf a.neundorf-work at gmx.net
Thu Apr 17 16:15:20 EDT 2008


On Thursday 17 April 2008, Mark Jonas wrote:
> Hi Alex,
>
> thanks for your input.
>
> >  > 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.
> >
> >  You could use something like:
> >  ADD_CUSTOM_COMMAND(TARGET target POST_BUILD
> >                    COMMAND touch build_timestamp.c)
>
> I tried that before and it has a bad side effect: The linker is now
> called every time I run make although the source did not change. What
> I am shooting for is that build_timestamp.c is compiled only when the
> application would be linked anyhow.
>
> Additionally, the solution uses a command that wouldn't work if the
> output of CMake would be e.g. a Visual Studio project.
>
> >  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).
>
> Exactly. You might be asking now why I am insisting not calling the
> linker when it is not really necessary. The reason is that the
> application I would like to use CMake with has about 1,000,000 source
> lines. Linking the little monster takes about 5 minutes on a modern
> PC.
>
> Do you have another idea?

Hmm, so you need to execute something in case something will actually be 
built. I don't see a clean way, only hacks:

Maybe you can do something with add_custom_target() ?

add_custom_target(DateHelperTarget COMMAND your_fancy_date_tool)

add_executable(monsterapp ${sources})

add_dependencies(monsterapp DateHelperTarget)

 This way DateHelperTarget would be always built before monsterapp. It would 
be up to you to do something clever in your_fancy_date_tool.


Or create a static lib containing just that date file and link it to the 
executable. Build that static lib everytime and use 
add_custom_command(POST_BUILD) on that static lib to set its date back, so 
its older than the executable and will not cause linking ?

Alex


More information about the CMake mailing list