Generate fIle-api query files from CMake

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

1 Like

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

1 Like

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.

@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. :slightly_smiling_face:

It works with a PreLoad.cmake file.

Try it with:

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.

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.

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

 # 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?