14

I have a project under CMake with some files generated with python generator from XML files. I cannot specify all files generated by this generator in CMakeLists.txt so I use file globbing for this.

The problem is that when I update my XML files or generator sources (which are in the same repository) I would like to have my build system reconfigured so changed files are taken into account when rebuilding the code (via make for example).

Is it possible to make CMake treat some files like it treats CMakeLists.txt files and to make it regenerate build system when those file are changed?

cmorse
  • 337
  • 1
  • 7
  • 15
Netrix
  • 342
  • 3
  • 10

3 Answers3

29

It doesn't require any kind of workarounds. The standard way is to use CMAKE_CONFIGURE_DEPENDS property:

set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS <filename>)
Antonio
  • 19,451
  • 13
  • 99
  • 197
roolebo
  • 819
  • 9
  • 15
  • Can you do *.xml in the filename to select all xml files? – Lucas Nov 04 '15 at 20:13
  • 1
    @lnunno sure you can use file(GLOB to get a list of files and then append the list. However, it's highly discouraged as you'll be forced to invoke CMake each time you add a new file. – roolebo Nov 04 '15 at 20:39
2

Yes, you should be able to do that by (ab)using configure_file(). Configuring a file makes the source a dependency of the CMake run, so that any changes in it cause a reconfiguration. Simply like this:

configure_file(MyInputFile.xml DummyOutput.xml)
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • It does reconfigure of the project but there is another problem. I'm using add_custom_command and add_custom_target which triggers regeneration of files but those targets are done in a different moment than reconfigure. Do you think is it possible to run reconfigure right after those targets are build and then CMake could detect changes? I'm 99% sure that it is impossible and I think I will be forced to used execute_command instead of add_custom_command but if you know the answer I would aprieciate it. – Netrix Jun 20 '14 at 11:44
  • 1
    @Netrix You're mixing up times. The commands of `add_custom_target` and `add_custom_command` are executed at build time (i.e. when you run `make`). This is quite unrelated to CMake configuration time (when CMake is running). Once build time starts (i.e. custom commands are running), CMake is no longer in the picture - its run has already finished. – Angew is no longer proud of SO Jun 20 '14 at 11:51
  • I thought so. So I guess only solution will be to generate all files in configuration time with execute_process and then file(GLOB) those files and add to target. Am I correct? – Netrix Jun 20 '14 at 12:51
0

Since it has been a while I will add to @roolebo's answer.

There's actually a better command to add a dependency on a file: set_directory_properties(PROPERTIES CMAKE_CONFIGURE_DEPENDS <relative_or_full_path_to_file>)

What might be confusing is that this command adds a property to the current directory. Well, it does not matter since you can set a full path to a file that resides outside of the current directory's scope, for instance: ../../config.json

Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
  • Does the `set_directory_properties()` append the new property or is it going to smash the existing property? This is really not clear from the docs... – Alexis Wilke Jan 01 '22 at 20:33
  • 1
    @AlexisWilke this is a legit question that also goes for `set_target_properties`. I'd advise you to open a separate question – Sergey Kolesnik Jan 01 '22 at 20:46