[Previous][Up] Reference for unit 'fpmkunit' (#fcl)

Handling dependencies

When compiling units and programs, the installer will by default compile all targets in the order that they were specified

However, if unita depends on unitb, and unita was specified before unitb, then unitb will be compiled twice: when compiling unita the compiler will compile it because it needs unitb. Then unitb2 will be compiled because it is in the list of targets. Clearly, this is not ideal or efficient.

There are 2 ways to solve this:

  1. Specify the dependencies of the units. In this case, when a target has dependencies, the installer will compile dependencies first, and will compile the target last
  2. Switch to using a "build unit". This unit is an empty unit which simply lists all units to be compiled. Instead of compiling the units one by one, the installer will try to compile the buildunit, and then the compiler will make sure that the units are compiled in the correct order, so every unit is compiled only once (for interdependent units the compiler may still compile them twice).

The latter is clearly the easier solution. When using a build unit, the installer will of course delete the unit sources and .ppu file after compiling, since they are not needed any more after compilation.

When running the installer, you can request that it tries to use a build-unit by giving the -bu or --buildunit command-line option. However, there are some cases when using a build unit is not an option. Therefore, the TPackage.SupportedBuildModes property can be used to indicate which build modes can be used for a package. if the bmBuildUnit build mode (TBuildMode) is not in the set of buildmodes, then the installer will compile one by one, even when a buildunit was requested.

To specify dependencies, you can use the Dependencies property of TTarget:

uses fpmkunit;

var
  P: TPackage;
  T : TTarget;
  
begin
  With Installer do
    begin
    P:=Packages.AddPackage('mypackage');
    P.Dependencies.Clear;
    T:=P.Targets.AddUnit('src2/unita.pp');
    T.Dependencies.AddUnit('unitb');
    P.Targets.AddUnit('src2/unitb.pp');
    Run;
    end;
end.

You can add a unit to the dependencies using TDependencies.AddUnit, or a complete package using TDependencies.Add.

When compiling the above and running it, the following output can be seen

./fpmake4 compile -v
Start compiling package mypackage for target x86_64-linux.
      Compiling target unitb
        Executing command "/home/michael/bin/fpc \
                           -Tlinux -Px86_64 \
                           -FUunits/x86_64-linux/ \
                           src2/unitb.pp"
  Compiling target unita
    Executing command "/home/michael/bin/fpc \
                       -Tlinux -Px86_64 \
                       -FUunits/x86_64-linux/ \
                       src2/unita.pp"
Generating "mypackage-x86_64-linux.fpm"
[100%] Compiled package mypackage

As you can see, the installed compiled unitb first.

See also

CommandLineOptions

  

Commandline options

Usage

  

Usage description


Documentation generated on: Dec 15 2025