Fortran dependency graph with CMake

CMake can generate GraphViz dependency graphs for languages including 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 is typically 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.

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 arbitrary and created automatically, to avoid making a mess of files in the top directory.

cmake -B build --graphviz=gfx/

cd gfx

dot -Tpng -o block.png

dot -Tsvg -o block.svg

h5fortran dependency graph

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

CMakeUtils can convert a whole directory of these DOT diagrams to SVG or PNG like:

python -m cmakeutils.graph ~/myprog/gfx

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