TExcelTAD
Home › Modern CMake (with CTest & CPack) › Toolkit: Graphviz, Trace & Help

Toolkit: Graphviz, Trace & Help

When a CMake project misbehaves, the question is usually "what does CMake think my project is?" Three built-in tools answer it: a dependency graph of all targets, a trace of every command CMake executed, and the built-in reference.

cmake --graphviz: the target graph

--graphviz=<file> writes the target dependency graph in Graphviz's DOT format during configure: one file for the whole project, plus one per target (<file>.<target>) and per target's dependents (<file>.<target>.dependers).

bash · try it yourself
mkdir -p game && cd game
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Game LANGUAGES CXX)
find_package(Threads REQUIRED)
add_library(core STATIC core.cpp)
add_library(render STATIC render.cpp)
add_library(audio STATIC audio.cpp)
target_link_libraries(render PUBLIC core)
target_link_libraries(audio PRIVATE core Threads::Threads)
add_executable(game main.cpp)
target_link_libraries(game PRIVATE render audio)
add_executable(editor editor.cpp)
target_link_libraries(editor PRIVATE render)
CM
for f in core render audio main editor; do echo "int $f() { return 0; }" > $f.cpp; done
echo 'int main() { return 0; }' >> main.cpp; echo 'int main() { return 0; }' >> editor.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE --graphviz=graph/deps.dot > /dev/null
ls graph
echo "--- deps.dot"
cat graph/deps.dot
deps.dot
deps.dot.ThreadsThreads.dependers
deps.dot.audio
deps.dot.audio.dependers
deps.dot.core.dependers
deps.dot.editor
deps.dot.game
deps.dot.render
deps.dot.render.dependers
--- deps.dot
digraph "Game" {
node [
  fontsize = "12"
];
subgraph clusterLegend {
  label = "Legend";
  color = black;
  edge [ style = invis ];
  legendNode0 [ label = "Executable", shape = egg ];
  legendNode1 [ label = "Static Library", shape = octagon ];
  legendNode2 [ label = "Shared Library", shape = doubleoctagon ];
  legendNode3 [ label = "Module Library", shape = tripleoctagon ];
  legendNode4 [ label = "Interface Library", shape = pentagon ];
  legendNode5 [ label = "Object Library", shape = hexagon ];
  legendNode6 [ label = "Unknown Library", shape = septagon ];
  legendNode7 [ label = "Custom Target", shape = box ];
  legendNode0 -> legendNode1 [ style = solid ];
  legendNode0 -> legendNode2 [ style = solid ];
  legendNode0 -> legendNode3;
  legendNode1 -> legendNode4 [ label = "Interface", style = dashed ];
  legendNode2 -> legendNode5 [ label = "Private", style = dotted ];
  legendNode3 -> legendNode6 [ style = solid ];
  legendNode0 -> legendNode7;
}
    "node0" [ label = "audio", shape = octagon ];
    "node1" [ label = "Threads::Threads", shape = pentagon ];
    "node0" -> "node1" [ style = dotted ] // audio -> Threads::Threads
    "node2" [ label = "core", shape = octagon ];
    "node0" -> "node2" [ style = dotted ] // audio -> core
    "node3" [ label = "editor", shape = egg ];
    "node4" [ label = "render", shape = octagon ];
    "node4" -> "node2"  // render -> core
    "node3" -> "node4" [ style = dotted ] // editor -> render
    "node5" [ label = "game", shape = egg ];
    "node5" -> "node0" [ style = dotted ] // game -> audio
    "node5" -> "node4" [ style = dotted ] // game -> render
}

Read the file from the bottom: game depends on audio and render, render on core, audio on core and Threads::Threads. The Legend cluster at the top explains the encoding: the node shape is the target type (egg = executable, octagon = static library, pentagon = interface library, which is what Threads::Threads is here), and the edge style is the link scope (solid = PUBLIC, dashed = INTERFACE, dotted = PRIVATE). The // audio -> core comments make the file readable without drawing it.

A CMakeGraphVizOptions.cmake file in the build directory controls what is shown (set(GRAPHVIZ_EXTERNAL_LIBS FALSE), GRAPHVIZ_IGNORE_TARGETS…).

The per-target files answer two everyday questions: deps.dot.game shows everything game pulls in, and deps.dot.core.dependers shows everything that must be rebuilt or retested when core changes:

bash · try it yourself
mkdir -p game && cd game
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Game LANGUAGES CXX)
add_library(core STATIC core.cpp)
add_library(render STATIC render.cpp)
add_library(audio STATIC audio.cpp)
target_link_libraries(render PUBLIC core)
target_link_libraries(audio PRIVATE core)
add_executable(game main.cpp)
target_link_libraries(game PRIVATE render audio)
add_executable(editor editor.cpp)
target_link_libraries(editor PRIVATE render)
CM
touch core.cpp render.cpp audio.cpp main.cpp editor.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE --graphviz=graph/deps.dot > /dev/null
echo "who depends on core (core itself included):"
grep -o 'label = "[^"]*"' graph/deps.dot.core.dependers | cut -d'"' -f2 | sort
who depends on core (core itself included):
audio
core
editor
game
render

Drawing the graph with dot

Graphviz's dot turns any of these files into a picture: -Tsvg for a browser or a wiki, -Tpng for a chat message, -Tpdf for print. The per-target file deps.dot.game is small enough to read in full, and -Tplain prints the computed layout as text (here: label, shape and the node's x/y position in inches), which is handy for checking a graph without opening an image:

bash · try it yourself
mkdir -p game && cd game
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Game LANGUAGES CXX)
add_library(core STATIC core.cpp)
add_library(render STATIC render.cpp)
add_library(audio STATIC audio.cpp)
target_link_libraries(render PUBLIC core)
target_link_libraries(audio PRIVATE core)
add_executable(game main.cpp)
target_link_libraries(game PRIVATE render audio)
add_executable(editor editor.cpp)
target_link_libraries(editor PRIVATE render)
CM
touch core.cpp render.cpp audio.cpp main.cpp editor.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE --graphviz=graph/deps.dot > /dev/null
dot -Tsvg graph/deps.dot.game -o game.svg
dot -Tpng graph/deps.dot -o deps.png
file game.svg deps.png
echo "--- labels in game.svg:"
grep -o '<text[^>]*>[^<]*</text>' game.svg | sed 's/<[^>]*>//g'
echo "--- plain layout of deps.dot.game:"
dot -Tplain graph/deps.dot.game | grep '^node' | awk '{print $7, $9, "at", $3, $4}'
game.svg: SVG Scalable Vector Graphics image
deps.png: PNG image data, 1084 x 367, 8-bit/color RGBA, non-interlaced
--- labels in game.svg:
game
audio
render
core
--- plain layout of deps.dot.game:
game egg at 1.0738 2.25
audio octagon at 0.46271 1.25
core octagon at 1.0738 0.25
render octagon at 1.6988 1.25

dot ranks the graph top-down: game on the top row (y = 2.25), audio and render side by side below it, core at the bottom because both of them link it. The PNG of the whole project is 1084×367 pixels, legend included. In CMakeGraphVizOptions.cmake, set(GRAPHVIZ_GENERATE_PER_TARGET FALSE) and GRAPHVIZ_GENERATE_DEPENDERS FALSE skip the extra files, and for very large projects GRAPHVIZ_IGNORE_TARGETS (regular expressions) hides test and example targets.

--trace-expand: what actually ran

--trace prints every CMake command as it executes, with file and line; --trace-expand also substitutes variables, so you see the values. The output is huge (CMake's own modules are traced too), so narrow it with --trace-source=<file>, or filter it:

bash · try it yourself
mkdir -p tr && cd tr
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Tr LANGUAGES NONE)
set(FEATURES "net;gui")
if(NOT DEFINED APP_LEVEL)
  set(APP_LEVEL 2)
endif()
foreach(f IN LISTS FEATURES)
  string(TOUPPER "${f}" up)
  set(HAVE_${up} ON)
endforeach()
message("HAVE_NET=${HAVE_NET} HAVE_GUI=${HAVE_GUI} level=${APP_LEVEL}")
CM
cmake -S . -B build --trace-expand --trace-source=CMakeLists.txt 2>&1 | grep -v '^--'
echo "=== total trace lines without --trace-source:"
cmake -S . -B build2 --trace-expand 2>&1 | wc -l
Put cmake in trace mode, but with variables expanded.
Put cmake in trace mode, but output only lines of a specified file. Multiple options are allowed.
/home/learner/tr/CMakeLists.txt(1):  cmake_minimum_required(VERSION 3.25 )
/home/learner/tr/CMakeLists.txt(2):  project(Tr LANGUAGES NONE )
/home/learner/tr/CMakeLists.txt(3):  set(FEATURES net;gui )
/home/learner/tr/CMakeLists.txt(4):  if(NOT DEFINED APP_LEVEL )
/home/learner/tr/CMakeLists.txt(5):  set(APP_LEVEL 2 )
/home/learner/tr/CMakeLists.txt(6):  endif()
/home/learner/tr/CMakeLists.txt(7):  foreach(f IN LISTS FEATURES )
/home/learner/tr/CMakeLists.txt(8):  string(TOUPPER net up )
/home/learner/tr/CMakeLists.txt(9):  set(HAVE_NET ON )
/home/learner/tr/CMakeLists.txt(8):  string(TOUPPER gui up )
/home/learner/tr/CMakeLists.txt(9):  set(HAVE_GUI ON )
/home/learner/tr/CMakeLists.txt(10):  endforeach()
/home/learner/tr/CMakeLists.txt(11):  message(HAVE_NET=ON HAVE_GUI=ON level=2 )
HAVE_NET=ON HAVE_GUI=ON level=2
=== total trace lines without --trace-source:
280

The first two lines are CMake confirming the trace options. Each trace line is file(line): command(arguments) with the arguments already expanded: if(NOT DEFINED APP_LEVEL ) and the loop body run once per feature with string(TOUPPER net up ), then gui. For machine processing, --trace-format=json-v1 emits one JSON object per command, and --trace-redirect=trace.log keeps the trace out of your terminal. Without --trace-source, even this tiny project with LANGUAGES NONE traces 280 commands from CMake's own modules; with compiler detection it is thousands.

--help-*: the reference, offline

Every command, variable, property, module and policy is documented inside CMake: cmake --help-command add_custom_command, --help-variable CMAKE_INSTALL_PREFIX, --help-property INTERFACE_INCLUDE_DIRECTORIES, --help-module GNUInstallDirs, --help-policy CMP0168, and --help-<kind>-list for the index. They print the reStructuredText source of the online pages, for exactly the CMake version you are running, so they are the fastest way to check an argument without leaving the terminal. Pages are long; cut them with sed -n or head:

bash · try it yourself
cmake --help-command target_link_libraries | sed -n '1,8p'
echo ...
cmake --help-policy CMP0168 | sed -n '1,6p'
echo ...
for kind in command variable property module policy; do
  printf '%-9s %s pages\n' "$kind" "$(cmake --help-$kind-list | wc -l)"
done
target_link_libraries
---------------------

Specify libraries or flags to use when linking a given target and/or
its dependents.  :ref:`Usage requirements <Target Usage Requirements>`
from linked library targets will be propagated.  Usage requirements
of a target's dependencies affect compilation of its own sources.

...
CMP0168
-------

.. versionadded:: 3.30

The ``FetchContent`` module implements steps directly instead of through
...
command   132 pages
variable  810 pages
property  629 pages
module    269 pages
policy    220 pages

The .. versionadded:: 3.30 line answers a frequent question (can I use this with the CMake version my users have?) without a browser. Pipe a page into less for reading, or grep a -list to find a name you half remember (cmake --help-variable-list | grep INSTALL). See also Target Properties.

Other inspection switches

Option Shows
cmake -LAH -N build every cache variable with its help string
cmake --system-information info.txt everything CMake knows about the platform and compiler
ninja -C build -t graph | dot -Tsvg the file-level graph (every object file), not just targets
ninja -C build -t query <file> what produces a file and what uses it
cmake --profiling-output=p.json --profiling-format=google-trace where configure time goes (open in chrome://tracing or Perfetto)

Test yourself

Which option writes the target dependency graph?

  1. --trace
  2. --graphviz=<file>
  3. -LAH
  4. --system-information

What does --trace-expand add over --trace?

  1. Colours
  2. Variable references are replaced by their values in the printed commands
  3. It traces the build step
  4. It only traces errors

You change core. Which graphviz file lists what depends on it?

  1. deps.dot.core
  2. deps.dot.core.dependers
  3. deps.dot
  4. CMakeGraphVizOptions.cmake