How to enable trace mode when CMake is re-run?

Trace mode can be enabled with --trace option when cmake is called to generate a project buildsystem. When a project is built with ‘cmake --build’ the buildsystem is regenerated if needed but there seems to be no way to enable trace mode. Is there a way?

For example, generate a project buildsystem with trace mode enabled:

cmake -S . -B out -G Ninja --trace-format=json-v1 --trace-redirect=trace.json -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

This creates trace.json and compile_commands.json.

Build the project:

cmake --build out

If CMakeLists.txt is modifed and the project is rebuilt:

cmake --build out

compile_commands.json is recreated but trace.json is not.

The rerun of CMake for --build is handled by the build tool itself via the normal rules, so the command line is already in the generated build instructions. I don’t think there’s a way to influence that.

Yes, that seems to be the case. For Ninja the command line is created here I guess: CMake/cmGlobalNinjaGenerator.cxx at 2488d6dbf7481087fa9ee4a0c77c5e3b0a503387 · Kitware/CMake · GitHub.
Is there a reason why the trace mode can be enabled only by using a command line option? It would be nice if it could be enabled by using a variable in the same way that compile_commands.json can be created (CMAKE_EXPORT_COMPILE_COMMANDS).

Because it hasn’t been asked for I guess? Note that tracing has more states than a simple boolean, so it’s going to be more complicated. Things to consider:

  • how to make it affect only builds? scripts? try_* projects? Some combination thereof?
  • what format?
  • what file to write it to (if any)?
  • what filters to apply?
  • expanded or not?

I created this Feature Request: Enable trace mode using variable(s) (#24876) · Issues · CMake / CMake · GitLab ( It’s true that more is needed than a boolean variable but it shouldn’t be too complicated.