Is it possible to make CMake print one variable at the end from command line?

Viewed 34

Scenario

I'm in the process of debugging a CMake script. In order to produce usable output for me to inspect, I use the following invocation:

cmake -S . -B build -DENABLE_MODULE_A=OFF -DENABLE_MODULE_B=OFF -DENABLE_MODULE_C=OFF --trace-source=CMakeLists.txt --trace-expand |& grep VARIABLE_IM_INTERESTED_IN > build/testout.utf8

Problem

This approach is sub-par, because I'm really only interested in one variable that is expanded in the CMake script in question. grep-ing for that after running it in trace-mode seems to be quite cumbersome, especially since the variable gets expanded multiple times, however I'm interested only in the last, full expansion of said variable.

Desired result

Ideally, there is a command line option for CMake, to print/expand only the one variable I'm interested in. I can filter for the last occurrence of that variable with external tools (for instance tail), but grepping the entire trace output for just one file, seems excessive. In essence I'm looking for a command line option to inspect an arbitrary variable, instead of tracing the entirety of the CMake script.

It is however, not an option to change the CMakeFiles.txt such that this one variable is printed. That is for external reasons, of which I have no control of.

1 Answers

Use variable_watch and CMAKE_PROJECT_INCLUDE_BEFORE as a debugging tool. Create a file called watch_var.cmake with the following contents:

cmake_minimum_required(VERSION 3.24)

variable_watch("${watch_var}")

Now here's an example project:

cmake_minimum_required(VERSION 3.24)
project(example)

set(VARIABLE_IM_INTERESTED_IN foo)
string(APPEND VARIABLE_IM_INTERESTED_IN " bar")

At the command line:

$ cmake -G Ninja -S . -B build -DCMAKE_PROJECT_INCLUDE_BEFORE=$PWD/watch_var.cmake -Dwatch_var=VARIABLE_IM_INTERESTED_IN
-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Debug Log at CMakeLists.txt:4 (set):
  Variable "VARIABLE_IM_INTERESTED_IN" was accessed using MODIFIED_ACCESS
  with value "foo".


CMake Debug Log at CMakeLists.txt:5 (string):
  Variable "VARIABLE_IM_INTERESTED_IN" was accessed using READ_ACCESS with
  value "foo".


CMake Debug Log at CMakeLists.txt:5 (string):
  Variable "VARIABLE_IM_INTERESTED_IN" was accessed using MODIFIED_ACCESS
  with value "foo bar".


-- Configuring done
-- Generating done
-- Build files have been written to: /home/reinking/test2/build

Docs: https://cmake.org/cmake/help/latest/command/variable_watch.html

Related