# file-api: expose \`configure\_file\` and \`add\_custom\_command\`

**URL:** https://discourse.cmake.org/t/file-api-expose-configure-file-and-add-custom-command/7667
**Category:** Development
**Created:** [March 13, 2023, 11:16am UTC](https://discourse.cmake.org/t/file-api-expose-configure-file-and-add-custom-command/7667 "2023-03-13T11:16:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)
#### Post date: [March 13, 2023, 11:16am UTC](https://discourse.cmake.org/t/file-api-expose-configure-file-and-add-custom-command/7667/1 "2023-03-13T11:16:45Z")

</div>

Just to confirm first, there is nothing in the `file-api` that can be queried to get a record of the files generated by `configure_file` and `add_custom_command(OUTPUT)`? Specifically the source/command and the destination files

This would be useful for code analyzers like `clang-format`/`clang-tidy`, for example to try formatting the output file and then try to format back the equivalent lines in the input file. An example issue: [`lizard` does not work · Issue #47 · Takishima/cmake-pre-commit-hooks · GitHub](https://github.com/Takishima/cmake-pre-commit-hooks/issues/47#issuecomment-1423985887).

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [March 13, 2023, 5:46pm UTC](https://discourse.cmake.org/t/file-api-expose-configure-file-and-add-custom-command/7667/2 "2023-03-13T17:46:01Z")

</div>

For custom command outputs, one can look through the [`codemodel-v2`](https://cmake.org/cmake/help/v3.26/manual/cmake-file-api.7.html#object-kind-codemodel) object to find source files with the `isGenerated` field set.

For `configure_file` and `file(GENERATE)` outputs, this is not currently available. I opened [CMake Issue 24594](https://gitlab.kitware.com/cmake/cmake/-/issues/24594) for that.

---

<div class="post-metadata">

### Author: ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)
#### Post date: [March 13, 2023, 5:58pm UTC](https://discourse.cmake.org/t/file-api-expose-configure-file-and-add-custom-command/7667/3 "2023-03-13T17:58:43Z")

</div>

The `isGenerated` would only work if the files are actually used in a target by the current configuration right? That is ok, but it is harder to find if one has to navigate through all targets to find the target that uses that file.

But also, there doesn’t seem to be a record of the `add_custom_command` that generated that file. It would be useful to get additional information from both `add_custom_command` and `add_custom_target` like `DEPENDS` and `BYPRODUCT`.

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [March 13, 2023, 6:12pm UTC](https://discourse.cmake.org/t/file-api-expose-configure-file-and-add-custom-command/7667/4 "2023-03-13T18:12:47Z")

</div>

> The `isGenerated` would only work if the files are actually used in a target by the current configuration right?

Typically if the outputs are not used then the custom command won’t even be included in the generated build system.

> doesn’t seem to be a record of the `add_custom_command` that generated that file.

[CMake Issue 20463](https://gitlab.kitware.com/cmake/cmake/-/issues/20463) requests custom command information in the fileapi.

> try formatting the output file and then try to format back the equivalent lines in the input file.

In the linked issue I see discussion of configuring `config.h.in` to `config.h` as an example. Over in KWSys (one of CMake’s dependencies) we `clang-format` the `.in` files directly using a script [like this](https://gitlab.kitware.com/utils/kwsys/-/blob/a3ff01ab80/clang-format.bash#L123-128) to replace `@` placeholders with valid identifiers, run `clang-format` on the temporary file, and then replace the identifiers with `@` placeholders again.
