Dynamic explicit dependency during build time

In short: Is there a way to generate and use dynamic explicit dependencies during build time for targets and/or custom commands?

In long:
Let’s use C++20 modules for example: compiling a source file will generate two outputs: a object file and an interface file that is required to compile other sources file that import it.

Then the compilation of a source file should dynamically explicitly depend on all interfaces of the sources it imports.

foreach(src ${sources})
        OUTPUT ${src}.interface ${src}.object
        COMMAND ${compiler} ${src}
        DEPENDS ???

A script can be written to discover these dependencies during build time, but how can I tell CMake to use these explicit dependencies?

add_custom_command has a DEPFILE option but it is very limited for only implicit dependencies similar to equivalent GNU’s -MF flags. So it is not up to this task.

If I understand correctly, CMake have to solve the same problem for supporting C++20 modules. How does/will CMake solve it and can I use the that for custom commands?

CMake will use “dynamic dependencies”. DEPFILE only supports “discovered dependencies”. I responded on the issue, but add_custom_command support for dynamic dependencies is not something that is planned at the moment. Very little actually ends up needing such power.

See this paper for the structure of such a “dynamic dependencies” build. Note that the collator needs to have enough information to understand what is going on to reconcile inter-target dependency logic.