Skip to content

Dependencies to execute_process() / file(WRITE) / configure()

I have a step in my build which needs to (1) build some other executable in the project (2) run that executable to get its output and then (3) use that output to generate some source file, to be used to build some other executable.

With simple Makefiles, this is fairly straightforward

generated-file : some-other-executable
    # some command that runs some-other-executable and writes $@

This seems like it should be straightforward with CMake - since we have execute_process for (2) and either file(WRITE) or configure() based on complexity for (3). But unfortunately none of these functions are dependency-aware, they just run during configure time directly, so I cannot use them due to the requirement of (1).

Instead, what I have to do is: first, create a script.cmake that does (2) and (3). And then, have an add_custom_target that invokes that script with the proper DEPENDS and BYPRODUCTS. This is very tedious first because passing arguments to the script is decidedly non-trivial (it took me multiple rounds of guess-and-check to get it right, while consulting StackOverflow, and to be honest I still have no idea why what I have works and the alternatives did not) and second because it requires adding an extra file (and thus indirection) to the build, which makes it harder to understand for readers.

It would be really helpful if this were possible to do more directly, as in:

execute_process(COMMAND some-other-executable arg arg arg 
    OUTPUT_VARIABLE exec_result
    DEPENDS some-other-executable)
configure_file(whatever.in whatever.out @ONLY DEPENDS exec_result)

(or whatever other syntax to accomplish this - the goal being to be able to write this directly without having to indirect through a script).

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information