# cmake code generator language

**URL:** <https://discourse.cmake.org/t/cmake-code-generator-language/4747>\
**Category:** Code\
**Created:** [December 30, 2021, 3:50am UTC](https://discourse.cmake.org/t/cmake-code-generator-language/4747 "2021-12-30T03:50:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lawrence\_Ibarria](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lawrence_ibarria/32/2061_2.png) [@Lawrence\_Ibarria](https://discourse.cmake.org/u/Lawrence_Ibarria)\
**Post date:** [December 30, 2021, 3:50am UTC](https://discourse.cmake.org/t/cmake-code-generator-language/4747/1 "2021-12-30T03:50:03Z")

</div>

In our C++ project we use something similar to google’s protocol buffers.  
That consists of a compiler and a source file, when the compiler runs the source generates a C header (optionally a C source) that later is used in the build.  
This source file can have includes, basically it needs to be rebuilt every time the source, the compiler or the included files change.  
Today we implement this using add\_custom\_command, but it only partially works. There is no easy way to automatically get the dependencies and to link those with the C files.  
How could this be improved? Maybe have the protobuf be a new cmake language (where we could use target\_include, where it could automatically call the compiler to get a list of dependent files… ).  
I am open to anything if there is an example or some sort of path to follow.

Thank you very much,

---

<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:** [January 2, 2022, 5:35pm UTC](https://discourse.cmake.org/t/cmake-code-generator-language/4747/2 "2022-01-02T17:35:54Z")

</div>

With more modern CMake versions (3.21), custom commands can now specify that they work with depfiles in all of the major generators. If your tool can output that, it will be scheduled to rebuild as necessary.

---

<div class="post-metadata">

**Author:** ![Lawrence\_Ibarria](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lawrence_ibarria/32/2061_2.png) [@Lawrence\_Ibarria](https://discourse.cmake.org/u/Lawrence_Ibarria)\
**Post date:** [January 2, 2022, 6:18pm UTC](https://discourse.cmake.org/t/cmake-code-generator-language/4747/3 "2022-01-02T18:18:30Z")

</div>

Thank you for the reply! Just to clarify, I got this section from the cmake online documentation:

> `DEPFILE`
> 
> New in version 3.7.
> 
> Specify a `.d` depfile which holds dependencies for the custom command. It is usually emitted by the custom command itself. This keyword may only be used if the generator supports it, as detailed below.
> 
> New in version 3.7: The `Ninja` generator supports `DEPFILE` since the keyword was first added.
> 
> New in version 3.17: Added the `Ninja Multi-Config` generator, which included support for the `DEPFILE` keyword.
> 
> New in version 3.20: Added support for Makefile Generators.

Some things there are not clear to me, for example:

- should the depfile be created in a separate call or in the same call that creates the output file the custom\_command generates?
- Is there a description of the file, or just simply whatever a C compiler would generate for dependencies (I think it was a flat list of files)

Thank you,

---

<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:** [January 2, 2022, 6:52pm UTC](https://discourse.cmake.org/t/cmake-code-generator-language/4747/4 "2022-01-02T18:52:33Z")

</div>

The format is makefile-like I think with a `output: in1 in2`-like syntax. But I agree that the docs should probably expand on what the `.d` depfiles look like.

---

<div class="post-metadata">

**Author:** ![Lawrence\_Ibarria](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lawrence_ibarria/32/2061_2.png) [@Lawrence\_Ibarria](https://discourse.cmake.org/u/Lawrence_Ibarria)\
**Post date:** [January 2, 2022, 7:31pm UTC](https://discourse.cmake.org/t/cmake-code-generator-language/4747/5 "2022-01-02T19:31:50Z")

</div>

This worked like a charm, I just tested it and dependencies are now being followed!
