How to copy an auxiliary file in the build directory that will be updated by CMake when needed

Dear CMake community,

I am developing and maintaining the gtk-fortran project since 2011. Its main build system is CMake. Although I have learned since 2016 to maintain myself the CMake part, I am far from being an expert… I am now using CMake 3.22.1 under Ubuntu 22.04 and Fedora.

A gtkbuilder2 executable is built in the build/examples/ directory but it needs the file gtkbuilder.ui (an XML file describing the Graphical User Interface to build) in the same directory. This is done with that line:


The problem is that I recently realized that if I modify the gtkbuilder.ui file in the source directory, CMake does not see it has been updated and does not update the copy in the build directory.
After some research, it seems I could do that with something like:

configure_file(gtkbuilder.ui gtkbuilder.ui COPYONLY)

Am I on the right way?

Note that I had already use configure_file() to substitutes variable values referenced as @VAR@ but I did not realized it could be used to simply copy a file without substitution.

It would me much simpler to copy the file at build time (if it really needs to copied) using add_custom_command() and adding the file in the binary directory to a target. Then make or ninja ensure that it is updated when needed.

Thanks for you quick answer.

If I use add_custom_command(), I suppose the command will be OS dependent. Is there any solution to avoid that?

${CMAKE_COMMAND} -E copy_if_different <file>... destination>

is your friend :slight_smile:

1 Like