Delphi 12 Graphviz Dependency Graph

It was interesting to hear that D12 was adding a link to Graphviz (a graph network standard) …

but I don’t know where it is. :slight_smile: Can anyone point it out?

Here’s a short video from @Marco_Cantu via Code Partners about it :

Graphviz turns text like this :

digraph { a → b }

Into : (I made it grainy, 'cos Discourse doesn’t take SVGs)

image

From the Delphi documentation.

GraphViz file export for the Delphi Compiler

GraphViz file export for the Delphi Compiler

From RAD Studio


As of RAD Studio 12.0, the Delphi compiler has an experimental feature to help understand the structure of a project and avoid circular unit references, slowing down compilation and causing side effects on the compiler when combining with other language features. This feature has the ability to generate a uses statement graph directly at the compiler level (without a separate parsing tool).

The compiler has a new --graphviz command option, which generates a unit dependency graph in a .gv GraphViz file, which can be later processed by GraphViz itself (an open source tool available at https://graphviz.org/) and also at http://magjac.com/graphviz-visual-editor/. There is also a second option to exclude units (individually or by family) from the graph:

  • --graphviz (Outputs <exename>.gv file)
  • --graphviz-exclude:<UnitList> (Excludes specific unit names from the output)


For --graphviz-exclude, the Unit name pattern can include the '*' wild card and multiple unit name patterns can be specified in <UnitList> separated by ';'.

For example: --graphviz-exclude:System.*;VCL.*;FMX.* excludes all System, VCL, and FireMonkey units.

Note: Required units, System, SysInit, and System.Variants are always excluded.


Considering an example of a simple application with a main form, a secondary dialog box, and a data module, they refer to each other via some uses statements in the interface or implementation section. You can build it with the following command line (excluding system units):

dcc32 --graphviz --graphviz-exclude=System.*;Vcl.*;WinApi.* GraphTest.dpr

It will generate the following .gv file, in which the style=dashed is used for uses statements in the implementation section:

digraph GraphTest {
      GraphTest -> { GT_mainform GT_dialog GT_datamodule }
      GT_mainform -> { GT_datamodule GT_dialog }
      GT_datamodule
      GT_dialog -> { GT_datamodule GT_mainform } [arrowhead=open,style=dashed]
}
Note: The generation of the .gv file only works on the command line or when using external MSBUILD from the IDE, not while building directly from the IDE.


Well, I’ve had a head-busting time trying to get Dev-Cpp to compile on the command-line with Delphi 12. :disappointed:

But I have managed to get a much smaller section to work :

and

CppPreprocessor.pas … with and without the System stuff excluded … :sweat_smile: