[CMake] write file if different?

Matthew Woehlke matthew.woehlke at kitware.com
Thu May 30 18:12:13 EDT 2013


On 2013-05-30 17:24, J Decker wrote:
> On Tue, May 28, 2013 at 1:07 PM, Matthew Woehlke
> <matthew.woehlke at kitware.com> wrote:
>> Is there a built-in way to write a string to a file (a la FILE(WRITE)) that
>> will only write the file if the content would be different? (Or does
>> FILE(WRITE) already work this way despite no obvious hint in the
>> documentation that it does?)
>>
>> Right now I am writing to a temporary file and running cmake -E
>> copy_if_different, then removing the temporary file, but this seems
>> inelegant and I am wondering if there is a better solution.
>
> I would add a minor note; this seems to be a good way, but you don't
> really need to delete the temp file, if you build it in the
> ${CMAKE_BINARY_DIR}, so it's not laying around in source trees.

Sure, but it offends my sense of tidiness to leave it ;-).

> It would be nice if such a simple thing didn't require launching a
> seperate instance of cmake to do; since it's already in cmake, would
> think there would be a way to just 'EXEC( ... )' instead of 'cmake -E
> (...)'

As pointed out, configure_file would probably also do the job. What I 
was hoping for, however, was a solution where I don't have to much about 
with the temporary file myself.

I had two main reasons for posting: to ask if there was an elegant way 
to do this that I didn't know about (I haven't heard one), and at some 
point ('now', apparently) to inquire if this would be a useful addition 
to CMake. A file(WRITE_IF_DIFFERENT) perhaps?

-- 
Matthew



More information about the CMake mailing list