Next: , Previous: Switches Related to Project Files, Up: gnatmake and Project Files


12.1.2 Switches and Project Files

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

The ^Default_Switches^Default_Switches^ attribute is an attribute indexed by language name (case insensitive) whose value is a string list. For example:

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

The Switches attribute is indexed on a file name (which may or may not be case sensitive, depending on the operating system) whose value is a string list. For example:

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

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

For each tool used in a program build (gnatmake, the compiler, the binder, and the linker), the corresponding package contributes a set of ^switches^switches^ for each file on which the tool is invoked, based on the ^switch^switch^-related attributes defined in the package. In particular, the ^switches^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^switches^ for the given file.

When gnatmake is invoked on a file, the ^switches^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^switches^ passed to the tool comprise three sets, in the following order:

  1. the applicable ^switches^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^switches^ reflects the fact that gnatmake ^switches^switches^ may or may not be passed to individual tools, depending on the individual ^switch^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^Default_Switches^ ("Ada")
               use ("^-g^-g^");
           for Switches ("a.adb")
               use ("^-O1^-O1^");
           for Switches ("b.adb")
               use ("^-O2^-O2^",
                    "^-gnaty^-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^switch^ ^-O1^-O1^, b.adb with ^switches^switches^ ^-O2^-O2^ and ^-gnaty^-gnaty^, and c.adb with ^-g^-g^.

The following example illustrates the ordering of the ^switches^switches^ contributed by different packages:

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

If you issue the command:

         gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main

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

        ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^

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

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

The ^-g^-g^ ^switch^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^Default_Switches^ ("Ada")
               use ("^-gnata^-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^-s^",
                    "^-g^-g^");
        end Builder;
     end Proj4;
     
     -- Ada source file:
     with Pack;
     procedure Foo_Main is
        ...
     end Foo_Main;

If the command is

     gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato

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

When using gnatmake with project files, some ^switches^switches^ or arguments may be expressed as relative paths. As the working directory where compilation occurs may change, these relative paths are converted to absolute paths. For the ^switches^switches^ found in a project file, the relative paths are relative to the project file directory, for the switches on the command line, they are relative to the directory where gnatmake is invoked. The ^switches^switches^ for which this occurs are: ^-I^-I^, ^-A^-A^, ^-L^-L^, ^-aO^-aO^, ^-aL^-aL^, ^-aI^-aI^, as well as all arguments that are not switches (arguments to ^switch^switch^ ^-o^-o^, object files specified in package Linker or after -largs on the command line). The exception to this rule is the ^switch^switch^ ^–RTS=^–RTS=^ for which a relative path argument is never converted.