Next: , Up: gnatmake and Project Files


10.13.1.1 Switches and Project Files

For each of the packages Builder, Compiler, Binder, and Linker, you can specify a Default_Switches attribute, a Switches attribute, or both; as their names imply, these switch-related attributes affect which switches are used for which files when gnatmake is invoked. As will be explained below, these package-contributed switches precede the switches passed on the gnatmake command line.

The Default_Switches attribute is an associative array indexed by language name (case insensitive) and returning a string list. For example:

     package Compiler is
       for Default_Switches ("Ada") use ("-gnaty", "-v");
     end Compiler;

The Switches attribute is also an associative array, indexed by a file name (which may or may not be case sensitive, depending on the operating system) and returning a string list. For example:

     package Builder is
        for Switches ("main1.adb") use ("-O2");
        for Switches ("main2.adb") use ("-g");
     end Builder;

For the Builder package, the file names should designate source files for main subprograms. For the Binder and Linker packages, the file names should designate ALI or source files for main subprograms. In each case just the file name (without explicit extension) is acceptable.

For each tool used in a program build (gnatmake, the compiler, the binder, and the linker), its corresponding package contributes a set of switches for each file on which the tool is invoked, based on the switch-related attributes defined in the package. In particular, the switches that each of these packages contributes for a given file f comprise:

If neither of these attributes is defined in the package, then the package does not contribute any switches for the given file.

When gnatmake is invoked on a file, the switches comprise two sets, in the following order: those contributed for the file by the Builder package; and the switches passed on the command line.

When gnatmake invokes a tool (compiler, binder, linker) on a file, the switches passed to the tool comprise three sets, in the following order:

  1. the applicable switches contributed for the file by the Builder package in the project file supplied on the command line;
  2. those contributed for the file by the package (in the relevant project file – see below) corresponding to the tool; and
  3. the applicable switches passed on the command line.

The term applicable switches reflects the fact that gnatmake switches may or may not be passed to individual tools, depending on the individual switch.

gnatmake may invoke the compiler on source files from different projects. The Project Manager will use the appropriate project file to determine the Compiler package for each source file being compiled. Likewise for the Binder and Linker packages.

As an example, consider the following package in a project file:

     project Proj1 is
        package Compiler is
           for Default_Switches ("Ada") use ("-g");
           for Switches ("a.adb") use ("-O1");
           for Switches ("b.adb") use ("-O2", "-gnaty");
        end Compiler;
     end Proj1;

If gnatmake is invoked with this project file, and it needs to compile, say, the files a.adb, b.adb, and c.adb, then a.adb will be compiled with the switch -O1, b.adb with switches -O2 and -gnaty, and c.adb with -g.

Another example illustrates the ordering of the switches contributed by different packages:

     project Proj2 is
        package Builder is
           for Switches ("main.adb") use ("-g", "-O1", "-f");
        end Builder;
     
        package Compiler is
           for Switches ("main.adb") use ("-O2");
        end Compiler;
     end Proj2;

If you issue the command:

         gnatmake -PProj2 -O0 main

then the compiler will be invoked on main.adb with the following sequence of switches

        -g -O1 -O2 -O0

with the last -O switch having precedence over the earlier ones; several other switches (such as -c) are added implicitly.

The switches -g and -O1 are contributed by package Builder, -O2 is contributed by the package Compiler and -O0 comes from the command line.

The -g switch will also be passed in the invocation of gnatlink.

A final example illustrates switch contributions from packages in different project files:

     project Proj3 is
        for Source_Files use ("pack.ads", "pack.adb");
        package Compiler is
           for Default_Switches ("Ada") use ("-gnata");
        end Compiler;
     end Proj3;
     
     with "Proj3";
     project Proj4 is
        for Source_Files use ("foo_main.adb", "bar_main.adb");
        package Builder is
           for Switches ("foo_main.adb") use ("-s", "-g");
        end Builder;
     end Proj4;
     
     -- Ada source file:
     with Pack;
     procedure Foo_Main is
        ...
     end Foo_Main;

If the command is

     gnatmake -PProj4 foo_main.adb -cargs -gnato

then the switches passed to the compiler for foo_main.adb are -g (contributed by the package Proj4.Builder) and -gnato (passed on the command line). When the imported package Pack is compiled, the switches used are -g from Proj4.Builder, -gnata (contributed from package Proj3.Compiler, and -gnato from the command line.