# Automatically detecting generated dependencies

**URL:** https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183
**Category:** Usage
**Created:** [May 11, 2020, 9:40pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183 "2020-05-11T21:40:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![chris.olton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@chris.olton](https://discourse.cmake.org/u/chris.olton)
#### Post date: [May 11, 2020, 9:40pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/1 "2020-05-11T21:40:35Z")

</div>

Our project has multiple source files and multiple generated header files (in addition to non-generated header files if that matters). These source files variously depend on these generated header files. How can I get cmake to recognize that some of the header files referenced in a source file need to be generated from a another file without explicitly declaring any dependencies. I’m thinking of something like: "if cmake can’t find a referenced header file in the source tree, but is somehow declared in the CMakeLists.txt, to use that non-existent file as a dependency as if it existed somewhere.

---

<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: [May 12, 2020, 2:03pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/2 "2020-05-12T14:03:25Z")

</div>

You need to either add a dependency on the generated header from the library (`add_library(mylib genheader.h)`) if it is the only library that needs it (or is at a unique root of a dep graph needing it) or `add_custom_target(genheader DEPENDS genheader.h)` then `add_dependencies(mylib genheader)` for each library needing it).

I don’t think this can be implicit behavior; a missing header versus a generated header is not solvable at any moment when CMake is guaranteed to actually be running across all generators.

---

<div class="post-metadata">

### Author: ![chris.olton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@chris.olton](https://discourse.cmake.org/u/chris.olton)
#### Post date: [May 12, 2020, 2:21pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/3 "2020-05-12T14:21:46Z")

</div>

This is more than one header file and in fact, the number of header files can change if the source data for those headers change. I was hoping to have some automated way of creating and updating those header files and have their usage by source files automatically detected. To add more complication, these header files are used by multiple libraries most with a lot of sources and not all libraries use all the generated headers. I wonder if there’s a “next best thing” than explicitly detailing a libraries dependencies on these headers.

---

<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: [May 12, 2020, 2:53pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/4 "2020-05-12T14:53:54Z")

</div>

Each of those headers would have an `add_custom_command`. I’d attach a target to each (or maybe one target to all headers if that works out better):

```cmake
add_custom_command(OUTPUT "${outdir}/genhdr1.h" DEPENDS hdr1.input COMMAND …)
add_custom_command(OUTPUT "${outdir}/genhdr2.h" DEPENDS hdr2.input COMMAND …)

if (target_per_header) # Decide which side is better for your use case.
  add_custom_target(genhdr1 DEPENDS "${outdir}/genhdr1.h")
  add_custom_target(genhdr2 DEPENDS "${outdir}/genhdr2.h")
  add_custom_target(genhdrs DEPENDS genhdr1 genhdr2)
else ()
  add_custom_target(genhdrs DEPENDS "${outdir}/genhdr1.h" "${outdir}/genhdr2.h")
endif ()

add_library(mylib …)
add_dependencies(mylib genhdrs) # Or each header target individually.

```

There’s no other way to reliably do what you want as far as I know. And as I said above, CMake would not be able to do this automatically. The headers generated are known at configure/generate time, but CMake can only look at source during the build which would mean shuttling all this data across and then somehow updating the build to add the link for the generated header. Not all generators support such a thing, so it would not be a solution anyways.

---

<div class="post-metadata">

### Author: ![airwin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@airwin](https://discourse.cmake.org/u/airwin)
#### Post date: [May 13, 2020, 4:43am UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/5 "2020-05-13T04:43:17Z")

</div>

Hi Chris:

I am not sure, but are you perhaps looking for the GENERATED property?  
That property is documented (with details about how dependencies are related to that property)  
at [https://cmake.org/cmake/help/latest/prop\_sf/GENERATED.html](https://cmake.org/cmake/help/latest/prop_sf/GENERATED.html).

Alan

---

<div class="post-metadata">

### Author: ![chris.olton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@chris.olton](https://discourse.cmake.org/u/chris.olton)
#### Post date: [May 13, 2020, 4:13pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/6 "2020-05-13T16:13:55Z")

</div>

Thanks for help.

I am using add\_custom\_command to generate the headers. I believe that marks those files GENERATED. Is there any reason why this wouldn’t?

The headers are spread across many libraries and not every library will need every generated header. Two things I’m thinking of trying: Add the generated headers a dependencies of the top-level library; I’m hoping that will get them in the door so-to-speak and cmake might recognize these headers even though they don’t yet exist. I’m also thinking of generating the headers before running cmake or maybe before declaring the top-level library; I’m hoping that, not only will cmake definitely see the headers, but cmake will also respect the rules to generate them. When I have the time I will try these things.

---

<div class="post-metadata">

### Author: ![chris.olton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@chris.olton](https://discourse.cmake.org/u/chris.olton)
#### Post date: [November 9, 2020, 8:07pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/7 "2020-11-09T20:07:33Z")

</div>

I saw a topic reference this one and I thought I’d suggest some functionality for CMake that would be useful. We couldn’t go with CMake largely because of this problem and instead went with an in-house solution. We can declare virtual header files that the dependency scanner is aware of. These files are not guaranteed to exist during scanning and are not scanned themselves; nonetheless, the proper dependency is made between the source file and the various generated headers. It would be nice if CMake had such a capability for its dependency scanner.

---

<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: [November 9, 2020, 8:25pm UTC](https://discourse.cmake.org/t/automatically-detecting-generated-dependencies/1183/8 "2020-11-09T20:25:45Z")

</div>

`add_custom_command(DEPFILE)` does exist, but it only works with Ninja. It may work with the Makefiles generators in the future, but I don’t know its status. The MR to overhaul Makefiles generator dependency scanning is [here](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5440).
