Previous: Importing Other Projects, Up: Examples of Project Files


10.2.4 Extending a Project

A common situation in large software systems is to have multiple implementations for a common interface; in Ada terms, multiple versions of a package body for the same specification. For example, one implementation might be safe for use in tasking programs, while another might only be used in sequential applications. This can be modeled in GNAT using the concept of project extension. If one project (the "child") extends another project (the "parent") then by default all source files of the parent project are inherited by the child, but the child project can override any of the parent's source files with new versions, and can also add new files. This facility is the project analog of extension in Object-Oriented Programming. Project hierarchies are permitted (a child project may be the parent of yet another project), and a project that inherits one project can also import other projects.

As an example, suppose that directory /seq contains the project file seq_proj.gpr and the source files pack.ads, pack.adb, and proc.adb:

     /seq
       pack.ads
       pack.adb
       proc.adb
       seq_proj.gpr

Note that the project file can simply be empty (that is, no attribute or package is defined):

     project Seq_Proj is
     end Seq_Proj;

implying that its source files are all the Ada source files in the project directory.

Suppose we want to supply an alternate version of pack.adb, in directory /tasking, but use the existing versions of pack.ads and proc.adb. We can define a project Tasking_Proj that inherits Seq_Proj:

     /tasking
       pack.adb
       tasking_proj.gpr
     
     project Tasking_Proj extends "/seq/seq_proj" is
     end Tasking_Proj;

The version of pack.adb used in a build depends on which project file is specified.

Note that we could have designed this using project import rather than project inheritance; a base project would contain the sources for pack.ads and proc.adb, a sequential project would import base and add pack.adb, and likewise a tasking project would import base and add a different version of pack.adb. The choice depends on whether other sources in the original project need to be overridden. If they do, then project extension is necessary, otherwise, importing is sufficient.