How to set <build-tool-options> via CMakeLists.txt?

I’d like to have a CMake-option called ENABLE_NINJA_EXPLAIN that when enabled causes the build to be invoked like so:

ninja -d explain

Now, when invoking CMake from command-line, you can do:

cmake --build --preset <preset> [<options>] [-- <build-tool-options>]

This will pass <build-tool-options> to the underlying build tool. I’d like to set <build-tool-options> programmatically in CMakeLists.txt, such that ninja is given the -d explain-argument. How can I do that?

I don’t think there’s any mechanism ninja gives us to do this natively (i.e., in build.ninja itself). You might be able to set CMAKE_MAKE_PROGRAM to ninja;-d;explain, but some tools might not be expecting this extra output (Qt Creator comes to mind).

I don’t believe I need a mechanism from ninja to support this.

I tried setting CMAKE_MAKE_PROGRAM just like you said prior to creating this question, but then the following call happened at some point during CMake configuration: ninja -d explain --version which causes configuration to fail since it’s not a valid command.

I mean, if CMake is able to forward arguments to the underlying build-tool (be it make or ninja or something else) via command-line via the double-dash --, then I suppose CMake could do that very same thing via a cmake-variable instead? E.g

set(BUILD_TOOL_OPTIONS "-d explain")

where ${BUILD_TOOL_OPTIONS} is used “as-if” I instead invoked: cmake --build --preset my_preset -- -d explain]. Does this make sense?

I haven’t found any variable that resembles BUILD_TOOL_OPTIONS in the official documentation, hence my question :slight_smile: .

I take it no such variable exists?

No, cmake --build reads the cache, but any local variable is gone by then. We’d need to add a new variable for such things. A feature request would be the next step I think.