The basics of Delphi programs

Delphi programs (strictly speaking, BDS 2006 programs) are composed of many different types of files.

The ingredients of a program

An item in the list that has a nested item participates in a source-to-product relationship, so a Unit contains source code and is compiled to a CompiledUnit, which contains the corresponding binary code.

The full definition is found in delpy/model.py.

The composition of a program

The source code of a Delphi program is composed of:

  1. One Program (alternatively, a Library or Package)
  2. Some number of Units
  3. Some number of Forms
  4. Some number of Resources
  5. Some number of BinaryObjects (less common)

The Program file contains the source code executed immediately upon running the program, and Units contain additional source code.

To compile a program, then, we need to:

  1. Compile all of the Units (written to disk as intermediate CompiledUnit files),
  2. Compile the Program file, and
  3. Link all of the above along with the Forms, Resources and BinaryObjects into a CompiledProgram.

However, in all but trivial cases there is also a DelphiProject file, which contains crucial metadata used in compilation, such as:

  1. Compiler and linker flags (duplicated in DelphiCompilerFlags)
  2. Preprocessor symbols
  3. Search paths for additional units (source or binary)
  4. Various other parameters, such as where to output the CompiledProgram, where to output the CompiledUnits etc.

A program graph

A program graph is a graph that contains all the sources (whether source code, or binary files like Resources) of a program. The root of the graph is either the program entry point (Program/Library/Package), which refers to Units (which in turn refer to more Units and so on) or a DelphiProject (if present).

A program graph

Here we see the graph of a simple program. The program has one form (Gui.dfm), one resource file (PriceCheck.res) and six Units.

The arrows represent a dependency relationship, so in order to compile SocketMarshall.pas you first need to compile SocketTypes.pas.