# Generate code from code?

**URL:** <https://discourse.cmake.org/t/generate-code-from-code/4700>\
**Category:** Code\
**Created:** [December 17, 2021, 6:41pm UTC](https://discourse.cmake.org/t/generate-code-from-code/4700 "2021-12-17T18:41:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clevijoki](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clevijoki/32/2042_2.png) [@clevijoki](https://discourse.cmake.org/u/clevijoki)\
**Post date:** [December 17, 2021, 6:41pm UTC](https://discourse.cmake.org/t/generate-code-from-code/4700/1 "2021-12-17T18:41:29Z")

</div>

What I want to do is add a custom command that runs on all of the source/dependencies of a target.

So I want to know all the .c/.cpp and indirectly referenced .h files to generate code, which would also have to be compiled/linked back in to the final executable.

This would be similar to how Qt moc works.

Is this possible to do somehow without modifying cmake?

I could probably make a CMakeLists.txt generator that contains a bunch of custom commands, but it would end up being a manual step people would need to run.

---

<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:** [December 21, 2021, 12:14pm UTC](https://discourse.cmake.org/t/generate-code-from-code/4700/2 "2021-12-21T12:14:53Z")

</div>

I think the only reliable way of doing this right now is to pass all of the sources in from a genex to a single command (this can then dispatch as needed) because the source list isn’t “known” at configure time due to `target_sources` adding files after any given point and genexes for conditional sources not being computable either.

Getting the indirectly referenced `.h` files is almost certainly out of the question too as that is content-based and you _really_ don’t want your configure to depend on the content of anything other than CMake files (otherwise any source change could force a rerun of CMake in addition to the build).
