# Dynamic explicit dependency during build time

**URL:** https://discourse.cmake.org/t/dynamic-explicit-dependency-during-build-time/8633
**Category:** Development
**Created:** [July 29, 2023, 8:40am UTC](https://discourse.cmake.org/t/dynamic-explicit-dependency-during-build-time/8633 "2023-07-29T08:40:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![namniav](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@namniav](https://discourse.cmake.org/u/namniav)
#### Post date: [July 29, 2023, 8:40am UTC](https://discourse.cmake.org/t/dynamic-explicit-dependency-during-build-time/8633/1 "2023-07-29T08:40:09Z")

</div>

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.

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

```

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](https://gitlab.kitware.com/cmake/cmake/-/issues/25134#note_1393189) 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?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 29, 2023, 8:49pm UTC](https://discourse.cmake.org/t/dynamic-explicit-dependency-during-build-time/8633/2 "2023-07-29T20:49:26Z")

</div>

> [@namniav](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 29, 2023, 8:50pm UTC](https://discourse.cmake.org/t/dynamic-explicit-dependency-during-build-time/8633/3 "2023-07-29T20:50:54Z")

</div>

See [this paper](https://mathstuf.fedorapeople.org/fortran-modules/fortran-modules.html) 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.
