GraphViz file export for the Delphi Compiler
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 combined 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/). 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.
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] }
This produces a graph like the following: