CMake dependency graph

CMake can generate GraphViz dependency graphs for CMake-supported project code languages including C, C++, and Fortran. Fortran submodule are not shown in the graph, but executables and modules are shown in the directed dependency graph.

The “dot” GraphViz program converts the .dot files to PNG, SVG, etc. dot program is available on Linux, Homebrew, Windows MSYS2, Cygwin, WSL, etc. by installing the “graphviz” program via the package manager.

Generating the dependency graph requires CMake configure and generate. Thus, the compiler and generator needed by the CMake project must be working. The project does not need to be compiled before generating the dependency graph. However, the user should select the same CMake configure options as they would for compiling the project.

Example: h5fortran HDF5 object-oriented Fortran dependency graph is below. SVG can be a useful format since it’s vector instead of raster and can be zoomed arbitrarily large in a web browser, while PNG is viewable by almost anything. The “gfx/” directory is to avoid making a mess of files in the source directory.

cmake -B build --graphviz=gfx/block.dot

cd gfx

dot -Tpng -o block.png block.dot

dot -Tsvg -o block.svg block.dot

h5fortran dependency graph

The “dependers” files show only the nodes depending on a node.

CMakeUtils graph.py converts a directory of CMake dot diagrams to SVG or PNG and collects them in an HTML document for easy viewing:

python cmakeutils/graph.py ~/myprog/gfx

To open a web browser from the Terminal:

python -m webbrowser -t file:///$HOME/myprog/gfx/index.html

Note that “file:///” has three slashes and the file path must be absolute.


Related: Dependency graphs are also easily created in Python and Matlab.