[CMake] don't auto-clean generated files?

Alexander Neundorf a.neundorf-work at gmx.net
Tue Sep 8 13:36:16 EDT 2009


On Tuesday 08 September 2009, Aaron Turner wrote:
> On Mon, Sep 7, 2009 at 1:00 PM, Alexander
>
> Neundorf<a.neundorf-work at gmx.net> wrote:
> > On Monday 07 September 2009, Aaron Turner wrote:
> >> I'm look for a way to tell cmake to not automatically remove certain
> >> auto-generated files when the 'clean' target is invoked.
> >>
> >> Basically I have:
> >>
> >>     add_custom_command(OUTPUT tcpbridge_opts.c tcpbridge_opts.h
> >>         COMMAND ${AUTOGEN} -L tcpedit tcpbridge_opts.def
> >>         DEPENDS tcpbridge_opts.def
> >>     )
> >>
> >> And of course, other files depend on tcpbridge_opts.[ch]
> >>
> >> The problem is that I don't want tcpbridge_opts.[ch] deleted when
> >> 'make clean' is run.
> >
> > Why don't you want this file to be deleted ?
>
> Because, counter to my general rule of thumb of never committing
> generated code, I've decided to check in some generated code.  These
> files also need to be shipped in the source tarballs.  Long story
> short: The templates used to generate the files is *very* particular
> about what version of autogen is used and I find I get far too many
> users checking out the code who have problems when they use the wrong
> version.

That's indeed what I was assuming, and for this case add_custom_target() 
should be used.

> > Maybe an ADD_CUSTOM_TARGET() would be more suitable ?
>
> I was under the impression that the outputs of targets are always
> deleted when 'make clean' is run... at least that's how it seems to
> work.

With ADD_CUSTOM_TARGET() no output files are specified, so they cannot be 
cleaned. 
Here's a simple example:
$ cat ../CMakeLists.txt
cmake_minimum_required(VERSION 2.6)

add_custom_target(MakeMyFiles COMMAND touch abc )

add_executable(foo main.c)
$

This gives you a custom target "MakeMyFiles", which is only executed if you 
explicitely build it. And I think that's also what you actually want.
I would suggest to have the custom target generate the files in the build 
tree, and then you can do with them what you want. You can copy them into the 
source tree, check them into the version control system, etc.

If you use add_custom_command() you will always have the rule to regenerate 
the files in your makefiles, and so your users can still get the problem 
(since the custom command would *have* to depend on the input file) if for 
whatever reason their template file got a newer date than the generated file.

Alex


More information about the CMake mailing list