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.

Requirements

The “dot” GraphViz program converts the .dot files to PNG, SVG, etc. dot is typically available on Linux, Homebrew, Windows MSYS2, Cygwin, WSL, etc.

  • Linux: apt install graphviz
  • MacOS: brew install graphviz
  • MSYS2: pacman -S mingw-w64-x86_64-graphviz

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 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/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 can convert a whole directory of these DOT diagrams to SVG or PNG like:

python -m cmakeutils.graph ~/myprog/gfx