# Generate fIle-api query files from CMake

**URL:** https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183
**Category:** Usage
**Created:** [April 21, 2021, 1:38pm UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183 "2021-04-21T13:38:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![thefifo](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@thefifo](https://discourse.cmake.org/u/thefifo)
#### Post date: [April 21, 2021, 1:38pm UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183/1 "2021-04-21T13:38:59Z")

</div>

Hello,

Is it possible to generate a file-api query file during a CMake configure? I tried to generate the query file from within my toolchain file or even in the CMakeLists.txt file before the project statement. However the reply is only generated once I re-run the configure step one more time.

Our use case: We are using a custom toolchain file and do some postprocessing on the generated cache output which relies on the file-api reply.

best regards,  
Wolfgang

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [April 21, 2021, 1:41pm UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183/2 "2021-04-21T13:41:00Z")

</div>

The File API is meant for IDEs and other tools outside of CMake to get information about the targets and their build commands. It’s not meant to be used inside CMake. If you want to do some postprocessing, it may be better to write a shell script which calls CMake and does the postprocessing afterward.

Cc: @brad.king

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 21, 2021, 9:19pm UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183/3 "2021-04-21T21:19:28Z")

</div>

I had to look into the same question not that long ago. The file API queries are read it very early, before any configure processing starts. That essentially means they have to exist before you run CMake.

---

<div class="post-metadata">

### Author: ![triddlelover69](https://discourse.cmake.org/user_avatar/discourse.cmake.org/triddlelover69/32/1326_2.png) [@triddlelover69](https://discourse.cmake.org/u/triddlelover69)
#### Post date: [April 22, 2021, 7:24am UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183/4 "2021-04-22T07:24:50Z")

</div>

@Wolfgang Issovits If you want to do some postprocessing, it may be better to write a shell script which calls CMake and does the postprocessing afterward. I think you should focus on this. It will make your mind clear for this work. 🙂

---

<div class="post-metadata">

### Author: ![cristianadam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cristianadam/32/124_2.png) [@cristianadam](https://discourse.cmake.org/u/cristianadam)
#### Post date: [July 1, 2021, 3:54pm UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183/5 "2021-07-01T15:54:32Z")

</div>

It works with a `PreLoad.cmake` file.

Try it with:

```cmake
foreach(file cache-v2 cmakeFiles-v1 codemodel-v2)
  file(WRITE "${CMAKE_BINARY_DIR}/.cmake/api/v1/query/${file}" "")
endforeach()

```

I don’t know how you would be able to generate the `PreLoad.cmake` file though.

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [July 1, 2021, 9:36pm UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183/6 "2021-07-01T21:36:59Z")

</div>

Would it be possible to move the File API handling to the end of the configure stage?

I wanted to use that information during a build step, and calling CMake configure inside Ninja seems to be a bit awkward.

---

<div class="post-metadata">

### Author: ![gabyx](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gabyx/32/2107_2.png) [@gabyx](https://discourse.cmake.org/u/gabyx)
#### Post date: [January 14, 2022, 5:31pm UTC](https://discourse.cmake.org/t/generate-file-api-query-files-from-cmake/3183/7 "2022-01-14T17:31:23Z")

</div>

We want to create a query file during cmake configure (which is then not picked up)  
and work around with

```auto
 # Generate the query file to produce CMake 'codemodel' query output. Note:
  # Writing this query here is actually too late, this file needs to be
  # existing before any CMake configure invocation is done! However we generate
  # it anyway and make a custom command which guarantees the reply exists.
  set(queryFile
      "${CMAKE_BINARY_DIR}/.cmake/api/v1/query/client-configure-cgo/codemodel-v2.json"
  )
  file(WRITE ${queryFile} "")
  set(replyDir "${CMAKE_BINARY_DIR}/.cmake/api/v1/reply")

  # Custom rule to depend on, to force the reply folder to exist (rerun CMake)
  set(replyGenerated
      "${CMAKE_BINARY_DIR}/.configure-cgo-reply-generated"
  )
  add_custom_command(
    OUTPUT ${replyGenerated}
    COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}
    COMMAND "touch" "${replyGenerated}"
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMENT "Generate CMake 'file-api' reply by rerunning `cmake`."
    VERBATIM)

```

not sure if that is safe, when `make -j ` is called, cmake is just rerun… hm…  
it works…

but it would be so much easier if the query files are first read before generate and not early in configure somewhere? Could that be done?
