Next: , Previous: Tools Supporting Project Files, Up: GNAT Project Manager


11.16 An Extended Example

Suppose that we have two programs, prog1 and prog2, whose sources are in corresponding directories. We would like to build them with a single gnatmake command, and we want to place their object files into build subdirectories of the source directories. Furthermore, we want to have to have two separate subdirectories in buildrelease and debug – which will contain the object files compiled with different set of compilation flags.

In other words, we have the following structure:

        main
          |- prog1
          |    |- build
          |         | debug
          |         | release
          |- prog2
               |- build
                    | debug
                    | release

Here are the project files that we must place in a directory main to maintain this structure:

  1. We create a Common project with a package Compiler that specifies the compilation switches:
              File "common.gpr":
              project Common is
              
                 for Source_Dirs use (); -- No source files
              
                 type Build_Type is ("release", "debug");
                 Build : Build_Type := External ("BUILD", "debug");
                 package Compiler is
                    case Build is
                       when "release" =>
                         for Default_Switches ("Ada")
                                 use ("-O2");
                       when "debug"   =>
                         for Default_Switches ("Ada")
                                 use ("-g");
                    end case;
                 end Compiler;
              
              end Common;
         
  2. We create separate projects for the two programs:
              File "prog1.gpr":
              
              with "common";
              project Prog1 is
              
                  for Source_Dirs use ("prog1");
                  for Object_Dir  use "prog1/build/" & Common.Build;
              
                  package Compiler renames Common.Compiler;
              
              end Prog1;
         
              File "prog2.gpr":
              
              with "common";
              project Prog2 is
              
                  for Source_Dirs use ("prog2");
                  for Object_Dir  use "prog2/build/" & Common.Build;
              
                  package Compiler renames Common.Compiler;
              end Prog2;
         
  3. We create a wrapping project Main:
              File "main.gpr":
              
              with "common";
              with "prog1";
              with "prog2";
              project Main is
              
                 package Compiler renames Common.Compiler;
              
              end Main;
         
  4. Finally we need to create a dummy procedure that withs (either explicitly or implicitly) all the sources of our two programs.

Now we can build the programs using the command

        gnatmake -Pmain dummy

for the Debug mode, or

        gnatmake -Pmain -XBUILD=release

for the Release mode.