2.1.5. Scenario VariablesΒΆ

GNAT project files can define “scenario variables” that allow conditional specifications and external dependencies, among other capabilities.

For example, scenario variables could be used to define two build scenarios in which different switch settings are applied: one for a debugging release and one for a production release. The debugging release would enable debugging information, reduce optimizations for the sake of debugging, and turn on other checks. The production release would, in contrast, disable debugging information and enable extensive optimizations.

External dependencies are possible because the values of scenario variables can be read from environment variables defined at the operating system level. Access to the environment variables is via a function named external.

In the following figure, one scenario variable defines the two debug/release build scenarios described above. First, the type Build_modes defines two possible values (line 3). The a scenario variable of that type named Mode is declared (line 4). The initial value of Mode is from the function external that reads the value of an environment variable named “BUILD”, with a default value of “Debug” corresponding to one of the two values that Mode can take on. The value of Mode is then used to further control switch settings. On line 10, for example, the location of the object directory is determined. On line 17 the default compilation switch settings are defined for the two builder modes: lines 19-21 specify the “Debug” mode compilation settings, whereas lines 22-23 do so for the “Release” mode compilation settings.

project file debug-release scenario example

GNATbench defines a wizard that creates a project file defining exactly this builder scenario. In fact, the project file in the figure above was generated entirely automatically by that wizard, with inputs from the user.

See section 11 of the GNAT User’s Guide, “GNAT Project Manager”, for full details on project files and scenario variables.