Wefti

How to debug CMake

wefti adds commands that show where a build setting comes from, why a package was not found, and how two build trees differ. It also runs a Debug Adapter Protocol server, so you can set breakpoints in CMakeLists.txt. Your listfiles stay the same.

Run wefti explain <target> -B build --flag <flag> to trace a compiler flag, and wefti explain why-not <package> -B build to see why find_package() failed. Run wefti graph -B build --cycles-only for dependency cycles and wefti diff-config build-a build-b to compare two build trees. To step through configure, run wefti -S . -B build --debugger --debugger-pipe=<path> and attach VS Code.

Which command answers which CMake debugging question?

Each row starts from a common CMake problem. The explain, graph, unused, diff-config, and doctor commands read a configured build directory or evaluate the project in memory. They do not edit your listfiles.

CMake debugging questions and wefti 0.3.0 commands
QuestionCommandResult
Where does a compiler flag come from?wefti explain app -B build --flag -O2Each source of the flag across PRIVATE, PUBLIC, and INTERFACE usage chains
Where does a definition or include directory come from?wefti explain app -B build --define USE_TLS
wefti explain app -B build --include /opt/local/include
The listfile and usage chain that add it
Why does a target link a library?wefti explain app -B build --dep zlibLink and order-only paths to the dependency
Why did find_package() fail?wefti explain why-not Boost -B build --all-candidatesEvery tested search path and why candidate config files were rejected
Which targets does the project define?wefti explain --targets -B buildEach target with its type and source directory
Does the target graph have cycles or extra PUBLIC links?wefti graph -B build --cycles-only
wefti graph -B build --highlight-public-bloat
Legal and illegal cycles, and PUBLIC links that could be PRIVATE
Why do two build trees differ?wefti diff-config build-main build-prDifferences in targets, compile and link commands, toolchains, packages, and cache entries
Is CMakeCache.txt stale?wefti doctor -B buildChanged toolchain, prefix path, and compiler inputs since the last successful configure
How do I step through CMakeLists.txt?wefti -S . -B build --debugger --debugger-pipe=/tmp/wefti-dap.sockA Debug Adapter Protocol server with breakpoints, variables, and stepping
Which part of configure is slow?wefti -S . -B build --profileflamegraph.html, trace.json, and stacks.folded

How do I trace where a compiler flag, define, or include comes from?

Run wefti explain <target> -B build with one filter: --flag, --define, --include, or --dep. The command traces the value through PRIVATE, PUBLIC, and INTERFACE usage chains and shows the listfile that adds it. It evaluates the build directory in memory, starts no external processes, and changes no files.

wefti explain app -B build
wefti explain app -B build --flag -O2 --config Release
wefti explain app -B build --flag=--sysroot=/opt/sdk
wefti explain app -B build --define USE_TLS
wefti explain app -B build --include /opt/local/include
wefti explain app -B build --dep zlib --config Release
wefti explain CMAKE_CXX_FLAGS -B build --var
wefti explain --targets -B build

The filters --filter, --flag, --define, --include, and --dep exclude each other. Use --flag=<flag> for a flag that starts with --. A --dep search covers link and order-only edges. It stops at 16 paths, depth 32, or 20,000 edges, and then reports that the result is incomplete. Add --json for a wefti-explain-v1 report or --verbose for full values and call stacks.

To check a generator expression, run wefti explain --eval '$<$<CONFIG:Debug>:-DTRACE>' -B build --config Debug. The command prints the result and each intermediate step.

How do I debug a find_package() failure?

Run wefti explain why-not <package> -B build. The command shows each candidate config file that find_package() rejected and the reason. Run wefti explain --find <package> -B build to see how a found package was resolved.

wefti explain why-not Boost -B build --all-candidates
wefti explain --find OpenSSL -B build
wefti explain --find OpenSSL -B build --call 2 --fresh

When a fresh native trace exists at CMakeFiles/wefti/find-trace.v1.json, the command reads it. Otherwise it evaluates the project again in memory. Use --call <n> (1-based) when the project calls find_package() for the same package more than once. Use --fresh to ignore a saved trace.

Which CMake 4.5.1 trace flags does wefti accept?

wefti accepts the CMake 4.5.1 trace and debug flags in both modes. They work through the cmake alias and through the wefti command. Use them when you need the CMake output format, for example in an existing script.

CMake 4.5.1 trace and debug flags
FlagEffect
--trace, --trace-expandPrint each command that runs, with variables expanded for the second flag
--trace-source=<file>Trace only the commands of one listfile
--trace-format=json-v1Write the trace as JSON lines
--trace-redirect=<file>Write the trace to a file instead of standard error
--debug-find, --debug-find-pkg=<pkg>, --debug-find-var=<var>Print the search paths of find commands
--warn-uninitializedWarn when a listfile reads an unset variable

The wefti explain commands answer the same questions without a new configure and without long trace logs. The native subcommands work only through the wefti command.

How do I see the target dependency graph and find cycles?

Run wefti graph -B build. The command configures the project again in memory and prints the target dependency graph as a tree, Graphviz DOT, Mermaid, or JSON. It does not change the build tree.

wefti graph -B build                                  # tree view of all targets
wefti graph -B build --target app --format dot -o app.dot
wefti graph -B build --target zlib --reverse          # targets that depend on zlib
wefti graph -B build --cycles-only
wefti graph -B build --highlight-public-bloat --check-headers
Cycle detection
The command finds cycles with Tarjan's algorithm and marks each cycle as legal or illegal. CMake accepts only link cycles of static libraries. A cycle that contains an add_dependencies() edge is always illegal. --cycles-only shows only the targets in cycles.
PUBLIC to PRIVATE analysis
--highlight-public-bloat marks PUBLIC links that could be PRIVATE: a PUBLIC link on an executable without ENABLE_EXPORTS, a library with no consumers that no export set contains, and an edge with a high transitive fanout. --check-headers also reads public headers and marks a PUBLIC link when no public header includes a header of the dependency.
Selection
--target shows one target and its dependencies. --reverse shows its dependents. --depth <N> limits the levels, --config <cfg> selects a configuration, and --no-external omits IMPORTED targets and external libraries.

The command reads at most 50,000 nodes and 500,000 edges. The JSON format uses the schema wefti-graph-v1.

How do I compare two CMake build trees?

Run wefti diff-config build-a build-b. The command compares targets, compile commands, link commands, toolchains, packages, and cache entries. The build directories can come from wefti or from official cmake. wefti reads them and never runs cmake.

wefti diff-config --prepare build-main build-pr     # before configure
wefti diff-config build-main build-pr
wefti diff-config --preset debug --preset release --only compile
wefti diff-config build-gcc build-clang --json > diff.json
wefti diff-config --save baseline.json build-main   # portable snapshot
wefti diff-config baseline.json build-pr
wefti diff-config options
OptionEffect
--save <file>Save a portable JSON snapshot of one build directory or preset, then exit
--portableReplace the build, source, and home roots in paths. This is the default.
--raw-pathsKeep absolute paths (same as --no-normalize). Only for two build directories, not with snapshots.
--prepareCreate the File API query files in each build directory, then exit
--only <sections>Compare only some of targets, compile, link, toolchain, packages, and cache
--cache key|all|noneSelect the cache entries to compare. The default is key.
--config <a>[:<b>], --target <name>Compare some configurations or targets only

Exact target, compile, and link data comes from a CMake File API reply. Run --prepare before configure to create the four query files: codemodel-v2, cache-v2, toolchains-v1, and cmakeFiles-v1. The exit code is 0 when there are no differences and the evidence is complete, 1 when there are differences, and 2 when the evidence is incomplete or the arguments are invalid.

How do I detect a stale CMakeCache.txt?

Run wefti doctor -B build. The command compares the build directory with its last successful native configure and runs no compiler and no project code. It reports each changed toolchain file, CMAKE_PREFIX_PATH input, and compiler identity.

wefti doctor            # CMake root, tools, and probe cache status
wefti doctor -B build   # also compare the build directory with its last configure

A native configure writes the baseline to CMakeFiles/wefti/toolchain-inputs-v1.json. The exit code is 0 when the report has no problem, 1 when the build directory is not configured or changed after the last configure, and 2 for invalid arguments. To share compiler checks across machines, read the CI cache guide.

How do I step through CMakeLists.txt with a debugger?

Start wefti with --debugger --debugger-pipe=<path>. wefti then runs a CMake 4.5.1 Debug Adapter Protocol (DAP) server for configure runs and -P scripts. A DAP client, such as VS Code, connects to the pipe and controls the run.

# macOS and Linux: Unix domain socket
wefti -S . -B build --debugger --debugger-pipe=/tmp/wefti-dap.sock

# Windows: named pipe
wefti -S . -B build --debugger --debugger-pipe=\\.\pipe\wefti-dap

# Script mode, with a protocol log
wefti --debugger --debugger-pipe=/tmp/wefti-dap.sock --debugger-dap-log=dap.log -P script.cmake

The server supports line breakpoints, exception breakpoints, stack traces, scopes, variables, evaluate, pause, continue, step over, step in, and step out. -E capabilities reports "debugger": true. The debugger turns off incremental replay, so breakpoints stop live execution.

The VS Code extension in editors/vscode adds the wefti debug type. It also works in Cursor and Windsurf. Add this .vscode/launch.json file and press F5:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "wefti",
      "request": "launch",
      "name": "Debug CMake configure",
      "mode": "configure",
      "sourceDirectory": "${workspaceFolder}",
      "buildDirectory": "${workspaceFolder}/build",
      "configureArgs": ["-DCMAKE_BUILD_TYPE=Debug"]
    },
    {
      "type": "wefti",
      "request": "launch",
      "name": "Debug CMake script",
      "mode": "script",
      "script": "${file}"
    }
  ]
}

The launch configuration also accepts preset, env, and dapLog. For editor setup, read the CMake language server guide.

How do I find which part of CMake configure is slow?

Add --profile to a native configure. wefti writes trace.json, stacks.folded, and an interactive flamegraph.html into <build>/wefti-profile and keeps each run in a run-<id>/ directory.

wefti -S . -B build --profile
wefti -S . -B build --profile=/tmp/configure-profile

Open flamegraph.html in a browser to see the slow listfiles and commands. Load trace.json in a trace viewer, or give stacks.folded to other flame graph tools. For ways to make configure faster, read how to speed up CMake configure.

How do I export compile_commands.json for a multi-config build?

In native mode, add --export-compile-commands or set CMAKE_EXPORT_COMPILE_COMMANDS=ON. wefti writes one compile_commands_<Config>.json file for each configuration and a root compile_commands.json for the default configuration. This works with Ninja Multi-Config, Xcode, and Visual Studio.

wefti -S . -B build -G "Ninja Multi-Config" --export-compile-commands

CMake 4.5.1 exports one compile_commands.json only for Makefile and Ninja generators. wefti records the digest of each file that it owns in CMakeFiles/wefti/compile-commands-v1.json, so it never overwrites or deletes a file that you edited. Compatibility mode keeps the CMake 4.5.1 behavior.

How to debug a CMake configure

Use wefti explain, wefti graph, wefti diff-config, and the wefti debugger to find the cause of a wrong flag, a missing package, or a failed configure.

  1. Configure in native mode. Run wefti with your usual generator and options. Add --profile to record a flame graph of the configure.
wefti -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug --profile
  1. Trace the setting. Run wefti explain on the target with one filter, such as --flag, --define, --include, or --dep. Run wefti explain why-not for a package that find_package() did not find.
wefti explain app -B build --flag -O2
wefti explain why-not Boost -B build --all-candidates
  1. Check the target graph. Run wefti graph to find cycles and PUBLIC links that could be PRIVATE.
wefti graph -B build --cycles-only
wefti graph -B build --highlight-public-bloat --check-headers
  1. Compare with a known good build. Save a portable snapshot of a good build with wefti diff-config --save. Compare it with the failing build directory.
wefti diff-config --save good.json build-main
wefti diff-config good.json build
  1. Step through the configure. Start wefti with --debugger and --debugger-pipe, or press F5 in VS Code with a wefti launch configuration. Set breakpoints in CMakeLists.txt.
wefti -S . -B build --debugger --debugger-pipe=/tmp/wefti-dap.sock

Frequently asked questions

How do I see where a compiler flag comes from in CMake?

Run wefti explain <target> -B build --flag <flag>. The command traces the flag through PRIVATE, PUBLIC, and INTERFACE usage chains and shows the listfile that adds it. Use --define, --include, or --dep for definitions, include directories, and link paths.

How do I debug why find_package() cannot find a package?

Run wefti explain why-not <package> -B build --all-candidates. The command lists every search path that find_package() tested and why candidate config files were rejected. Run wefti explain --find <package> -B build to see how a found package was resolved.

Can I step through CMakeLists.txt with a debugger?

Yes. Run wefti -S . -B build --debugger --debugger-pipe=<path> to start a CMake 4.5.1 Debug Adapter Protocol server. Use a Unix domain socket on macOS and Linux or a named pipe on Windows. The VS Code extension adds a wefti debug type for breakpoints.

How do I compare two CMake build directories?

Run wefti diff-config build-a build-b. It compares targets, compile and link commands, toolchains, packages, and cache entries from build trees made by wefti or cmake. Use --save to make a portable snapshot that you can compare on another machine.

How do I find which part of CMake configure is slow?

Add --profile to a native wefti configure. wefti writes flamegraph.html, trace.json, and stacks.folded into <build>/wefti-profile. Open flamegraph.html in a browser to see the slow listfiles and commands.

How do I detect a stale CMakeCache.txt?

Run wefti doctor -B build. It compares the toolchain file, CMAKE_PREFIX_PATH inputs, and compiler identities with the last successful native configure. It runs no compiler and exits with code 1 when it finds a change.