[CMake] Adding a custom_command dependency to all sources

Pentecost, Caleb Caleb.Pentecost at cognex.com
Thu Feb 22 17:16:21 EST 2018


Howdy,

I'm looking to add a dependency to all source files to ensure that a generated file is up-to-date before compilation. This generated file is "passed in" to the compiler via a global compiler definition. Is there an easy or built-in way to do this, or will I need to manually add a made-up target to every file in every library directory?

Currently, the generated file is created in the CMake Options file, using an exec_program command near the top of the file. The output of this process is not tracked, but the GLOBAL_DEFS variable applied to every source file includes a line that directs the compiler to consume the new file. It looks a little like this:

Lib_options.cmake
[...]
exec_program(
    <executable>
    ARGS
    -o <outputFile>
    -I <inputFile>
    -x <extraArgs...>
)
[...]
set (GLOBAL_DEFS
  -D<definitions...>
  -@<outputFile>
)
[...]


However, the dependency tree is not aware of this file and will not prepare it in any way on a per-file bases. In this case the program is executed every build regardless of if <inputFile> has been modified. This adds unnecessary time to an incremental build if no change is made, and if <inputFile> does happen to change CMake will not recompile the sources accordingly. I have to do a clean/rebuild in order for the changes to "take".

What I would like to see is a way to actually add <outputFile> as a dependency to each source file. Now, I'm no CMake expert, but I think it would look something like this:

Lib_options.cmake
[...]
# Create a command/target for our output file
add_custom_command(
  OUTPUT
    <outputFile>
  DEPENDS
    <inputFile>
  COMMAND
  <executable>
    ARGS
      -o <outputFile>
      -I <inputFile>
      -x <extraArgs...>
)

# Do something to make all source files depend on <outputFile>
# <Magic goes here>

[...]
set (GLOBAL_DEFS
  -D<definitions...>
  -@<outputFile>
)
[...]


Would this be a feasible thing to do in CMake, especially at the options file level? If you need any more information please let me know.

Thank you in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180222/4ae4ddd9/attachment-0001.html>


More information about the CMake mailing list