[multiple changes]
[gcc.git] / gcc / ada / make.adb
index 3ae13fc84aeb4fd31a48a93e67480fbc9a171b6f..567f1269510468d6fe03536374ca1382c2555593 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -27,6 +27,7 @@ with ALI;      use ALI;
 with ALI.Util; use ALI.Util;
 with Csets;
 with Debug;
+with Errutil;
 with Fmap;
 with Fname;    use Fname;
 with Fname.SF; use Fname.SF;
@@ -35,6 +36,7 @@ with Gnatvsn;  use Gnatvsn;
 with Hostparm; use Hostparm;
 with Makeusg;
 with Makeutl;  use Makeutl;
+with MLib;
 with MLib.Prj;
 with MLib.Tgt; use MLib.Tgt;
 with MLib.Utl;
@@ -47,10 +49,16 @@ with Prj;      use Prj;
 with Prj.Com;
 with Prj.Env;
 with Prj.Pars;
+with Prj.Tree; use Prj.Tree;
 with Prj.Util;
 with SFN_Scan;
 with Sinput.P;
 with Snames;   use Snames;
+
+pragma Warnings (Off);
+with System.HTable;
+pragma Warnings (On);
+
 with Switch;   use Switch;
 with Switch.M; use Switch.M;
 with Targparm; use Targparm;
@@ -62,11 +70,11 @@ with Ada.Exceptions;            use Ada.Exceptions;
 with Ada.Command_Line;          use Ada.Command_Line;
 
 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
+with GNAT.Dynamic_HTables;      use GNAT.Dynamic_HTables;
+with GNAT.HTable;
 with GNAT.Case_Util;            use GNAT.Case_Util;
 with GNAT.OS_Lib;               use GNAT.OS_Lib;
 
-with System.HTable;
-
 package body Make is
 
    use ASCII;
@@ -99,13 +107,17 @@ package body Make is
       Full_Source_File : File_Name_Type;
       Lib_File         : File_Name_Type;
       Source_Unit      : Unit_Name_Type;
+      Full_Lib_File    : File_Name_Type;
+      Lib_File_Attr    : aliased File_Attributes;
       Mapping_File     : Natural := No_Mapping_File;
       Project          : Project_Id := No_Project;
-      Syntax_Only      : Boolean := False;
-      Output_Is_Object : Boolean := True;
    end record;
    --  Data recorded for each compilation process spawned
 
+   No_Compilation_Data : constant Compilation_Data :=
+     (Invalid_Pid, No_File, No_File, No_Unit_Name, No_File, Unknown_Attributes,
+      No_Mapping_File, No_Project);
+
    type Comp_Data_Arr is array (Positive range <>) of Compilation_Data;
    type Comp_Data_Ptr is access Comp_Data_Arr;
    Running_Compile : Comp_Data_Ptr;
@@ -124,49 +136,6 @@ package body Make is
    --  complex, for example in main.1.ada, the termination in this name is
    --  ".1.ada" and in main_.ada the termination is "_.ada".
 
-   -------------------------------------
-   -- Queue (Q) Manipulation Routines --
-   -------------------------------------
-
-   --  The Q is used in Compile_Sources below. Its implementation uses the GNAT
-   --  generic package Table (basically an extensible array). Q_Front points to
-   --  the first valid element in the Q, whereas Q.First is the first element
-   --  ever enqueued, while Q.Last - 1 is the last element in the Q.
-   --
-   --        +---+--------------+---+---+---+-----------+---+--------
-   --    Q   |   |  ........    |   |   |   | .......   |   |
-   --        +---+--------------+---+---+---+-----------+---+--------
-   --          ^                  ^                       ^
-   --       Q.First             Q_Front               Q.Last - 1
-   --
-   --  The elements comprised between Q.First and Q_Front - 1 are the elements
-   --  that have been enqueued and then dequeued, while the elements between
-   --  Q_Front and Q.Last - 1 are the elements currently in the Q. When the Q
-   --  is initialized Q_Front = Q.First = Q.Last. After Compile_Sources has
-   --  terminated its execution, Q_Front = Q.Last and the elements contained
-   --  between Q.Front and Q.Last-1 are those that were explored and thus
-   --  marked by Compile_Sources. Whenever the Q is reinitialized, the elements
-   --  between Q.First and Q.Last - 1 are unmarked.
-
-   procedure Init_Q;
-   --  Must be called to (re)initialize the Q
-
-   procedure Insert_Q
-     (Source_File : File_Name_Type;
-      Source_Unit : Unit_Name_Type := No_Unit_Name;
-      Index       : Int            := 0);
-   --  Inserts Source_File at the end of Q. Provide Source_Unit when possible
-   --  for external use (gnatdist). Provide index for multi-unit sources.
-
-   function Empty_Q return Boolean;
-   --  Returns True if Q is empty
-
-   procedure Extract_From_Q
-     (Source_File  : out File_Name_Type;
-      Source_Unit  : out Unit_Name_Type;
-      Source_Index : out Int);
-   --  Extracts the first element from the Q
-
    procedure Insert_Project_Sources
      (The_Project  : Project_Id;
       All_Projects : Boolean;
@@ -179,39 +148,75 @@ package body Make is
    --  including, if The_Project is an extending project, sources inherited
    --  from projects being extended.
 
-   First_Q_Initialization : Boolean := True;
-   --  Will be set to false after Init_Q has been called once
-
-   Q_Front : Natural;
-   --  Points to the first valid element in the Q
-
    Unique_Compile : Boolean := False;
    --  Set to True if -u or -U or a project file with no main is used
 
    Unique_Compile_All_Projects : Boolean := False;
    --  Set to True if -U is used
 
+   Must_Compile : Boolean := False;
+   --  True if gnatmake is invoked with -f -u and one or several mains on the
+   --  command line.
+
+   Main_On_Command_Line : Boolean := False;
+   --  True if gnatmake is invoked with one or several mains on the command
+   --  line.
+
    RTS_Specified : String_Access := null;
    --  Used to detect multiple --RTS= switches
 
-   type Q_Record is record
-      File  : File_Name_Type;
-      Unit  : Unit_Name_Type;
-      Index : Int;
-   end record;
-   --  File is the name of the file to compile. Unit is for gnatdist
-   --  use in order to easily get the unit name of a file to compile
-   --  when its name is krunched or declared in gnat.adc. Index, when not 0,
-   --  is the index of the unit in a multi-unit source.
+   N_M_Switch : Natural := 0;
+   --  Used to count -mxxx switches that can affect multilib
 
-   package Q is new Table.Table (
-     Table_Component_Type => Q_Record,
-     Table_Index_Type     => Natural,
-     Table_Low_Bound      => 0,
-     Table_Initial        => 4000,
-     Table_Increment      => 100,
-     Table_Name           => "Make.Q");
-   --  This is the actual Q
+   package Queue is
+      ---------------------------------
+      -- Queue Manipulation Routines --
+      ---------------------------------
+
+      procedure Initialize (Queue_Per_Obj_Dir : Boolean);
+      --  Initialize the queue
+
+      function Is_Empty return Boolean;
+      --  Returns True if the queue is empty
+
+      function Is_Virtually_Empty return Boolean;
+      --  Returns True if the queue is empty or if all object directories are
+      --  busy.
+
+      procedure Insert
+        (Source_File_Name : File_Name_Type;
+         Project          : Project_Id;
+         Source_Unit      : Unit_Name_Type := No_Unit_Name;
+         Index            : Int            := 0);
+      --  Insert source in the queue
+
+      procedure Extract
+        (Source_File_Name  : out File_Name_Type;
+         Source_Unit       : out Unit_Name_Type;
+         Source_Index      : out Int);
+      --  Get the first source that can be compiled from the queue. If no
+      --  source may be compiled, return No_File/No_Source.
+
+      function Size return Natural;
+      --  Return the total size of the queue, including the sources already
+      --  extracted.
+
+      function Processed return Natural;
+      --  Return the number of source in the queue that have aready been
+      --  processed.
+
+      procedure Set_Obj_Dir_Busy (Obj_Dir : Path_Name_Type);
+      --  Indicate that this object directory is busy, so that when
+      --  One_Compilation_Per_Obj_Dir is True no other compilation occurs in
+      --  this object directory.
+
+      procedure Set_Obj_Dir_Free (Obj_Dir : Path_Name_Type);
+      --  Indicate that there is no compilation for this object directory
+
+      function Element (Rank : Positive) return File_Name_Type;
+      --  Get the file name for element of index Rank in the queue
+
+   end Queue;
 
    --  The 3 following packages are used to store gcc, gnatbind and gnatlink
    --  switches found in the project files.
@@ -319,6 +324,15 @@ package body Make is
 
    Saved_Maximum_Processes : Natural := 0;
 
+   Gnatmake_Switch_Found : Boolean;
+   --  Set by Scan_Make_Arg. True when the switch is a gnatmake switch.
+   --  Tested by Add_Switches when switches in package Builder must all be
+   --  gnatmake switches.
+
+   Switch_May_Be_Passed_To_The_Compiler : Boolean;
+   --  Set by Add_Switches and Switches_Of. True when unrecognized switches
+   --  are passed to the Ada compiler.
+
    type Arg_List_Ref is access Argument_List;
    The_Saved_Gcc_Switches : Arg_List_Ref;
 
@@ -331,15 +345,16 @@ package body Make is
    Current_Verbosity : Prj.Verbosity  := Prj.Default;
    --  Verbosity to parse the project files
 
-   Project_Tree : constant Project_Tree_Ref := new Project_Tree_Data;
-
    Main_Project : Prj.Project_Id := No_Project;
    --  The project id of the main project file, if any
 
    Project_Of_Current_Object_Directory : Project_Id := No_Project;
    --  The object directory of the project for the last compilation. Avoid
    --  calling Change_Dir if the current working directory is already this
-   --  directory
+   --  directory.
+
+   Map_File : String_Access := null;
+   --  Value of switch --create-map-file
 
    --  Packages of project files where unknown attributes are errors
 
@@ -435,10 +450,10 @@ package body Make is
    Link_With_Shared_Libgcc : Argument_List_Access :=
                                No_Shared_Libgcc_Switch'Access;
 
-   procedure Make_Failed (S1 : String; S2 : String := ""; S3 : String := "");
-   --  Delete all temp files created by Gnatmake and call Osint.Fail,
-   --  with the parameter S1, S2 and S3 (see osint.ads).
-   --  This is called from the Prj hierarchy and the MLib hierarchy.
+   procedure Make_Failed (S : String);
+   --  Delete all temp files created by Gnatmake and call Osint.Fail, with the
+   --  parameter S (see osint.ads). This is called from the Prj hierarchy and
+   --  the MLib hierarchy.
 
    --------------------------
    -- Obsolete Executables --
@@ -506,10 +521,6 @@ package body Make is
    Last_Argument : Natural := 0;
    --  Last index of arguments in Arguments above
 
-   Arguments_Collected : Boolean := False;
-   --  Set to True when the arguments for the next invocation of the compiler
-   --  have been collected.
-
    Arguments_Project : Project_Id;
    --  Project id, if any, of the source to be compiled
 
@@ -545,25 +556,6 @@ package body Make is
    procedure List_Bad_Compilations;
    --  Prints out the list of all files for which the compilation failed
 
-   procedure Verbose_Msg
-     (N1                : Name_Id;
-      S1                : String;
-      N2                : Name_Id := No_Name;
-      S2                : String  := "";
-      Prefix            : String  := "  -> ";
-      Minimum_Verbosity : Verbosity_Level_Type := Opt.Low);
-   procedure Verbose_Msg
-     (N1                : File_Name_Type;
-      S1                : String;
-      N2                : File_Name_Type := No_File;
-      S2                : String  := "";
-      Prefix            : String  := "  -> ";
-      Minimum_Verbosity : Verbosity_Level_Type := Opt.Low);
-   --  If the verbose flag (Verbose_Mode) is set and the verbosity level is
-   --  at least equal to Minimum_Verbosity, then print Prefix to standard
-   --  output followed by N1 and S1. If N2 /= No_Name then N2 is printed after
-   --  S1. S2 is printed last. Both N1 and N2 are printed in quotation marks.
-
    Usage_Needed : Boolean := True;
    --  Flag used to make sure Makeusg is call at most once
 
@@ -576,15 +568,9 @@ package body Make is
    procedure Debug_Msg (S : String; N : Unit_Name_Type);
    --  If Debug.Debug_Flag_W is set outputs string S followed by name N
 
-   procedure Recursive_Compute_Depth
-     (Project : Project_Id;
-      Depth   : Natural);
+   procedure Recursive_Compute_Depth (Project : Project_Id);
    --  Compute depth of Project and of the projects it depends on
 
-   procedure Compute_All_Imported_Projects (Project : Project_Id);
-   --  Compute, the list of the projects imported directly or indirectly by
-   --  project Project.
-
    -----------------------
    -- Gnatmake Routines --
    -----------------------
@@ -638,15 +624,16 @@ package body Make is
      (Source_File      : File_Name_Type;
       Source_File_Name : String;
       Source_Index     : Int;
-      Naming           : Naming_Data;
+      Project          : Project_Id;
       In_Package       : Package_Id;
       Allow_ALI        : Boolean) return Variable_Value;
    --  Return the switches for the source file in the specified package of a
    --  project file. If the Source_File ends with a standard GNAT extension
    --  (".ads" or ".adb"), try first the full name, then the name without the
    --  extension, then, if Allow_ALI is True, the name with the extension
-   --  ".ali". If there is no switches for either names, try the default
-   --  switches for Ada. If all failed, return No_Variable_Value.
+   --  ".ali". If there is no switches for either names, try first Switches
+   --  (others) then the default switches for Ada. If all failed, return
+   --  No_Variable_Value.
 
    function Is_In_Object_Directory
      (Source_File   : File_Name_Type;
@@ -655,25 +642,28 @@ package body Make is
    --  directory of the ultimate extending project. If it is not, we ignore
    --  the fact that this ALI file is read-only.
 
+   procedure Process_Multilib (Project_Node_Tree : Project_Node_Tree_Ref);
+   --  Add appropriate --RTS argument to handle multilib
+
    ----------------------------------------------------
    -- Compiler, Binder & Linker Data and Subprograms --
    ----------------------------------------------------
 
-   Gcc             : String_Access := Program_Name ("gcc", "gnatmake");
-   Gnatbind        : String_Access := Program_Name ("gnatbind", "gnatmake");
-   Gnatlink        : String_Access := Program_Name ("gnatlink", "gnatmake");
+   Gcc      : String_Access := Program_Name ("gcc", "gnatmake");
+   Gnatbind : String_Access := Program_Name ("gnatbind", "gnatmake");
+   Gnatlink : String_Access := Program_Name ("gnatlink", "gnatmake");
    --  Default compiler, binder, linker programs
 
-   Saved_Gcc       : String_Access := null;
-   Saved_Gnatbind  : String_Access := null;
-   Saved_Gnatlink  : String_Access := null;
+   Saved_Gcc      : String_Access := null;
+   Saved_Gnatbind : String_Access := null;
+   Saved_Gnatlink : String_Access := null;
    --  Given by the command line. Will be used, if non null
 
-   Gcc_Path        : String_Access :=
+   Gcc_Path      : String_Access :=
                        GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
-   Gnatbind_Path   : String_Access :=
+   Gnatbind_Path : String_Access :=
                        GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
-   Gnatlink_Path   : String_Access :=
+   Gnatlink_Path : String_Access :=
                        GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
    --  Path for compiler, binder, linker programs, defaulted now for gnatdist.
    --  Changed later if overridden on command line.
@@ -718,10 +708,12 @@ package body Make is
    --  file, to avoid displaying the -gnatec switch for a temporary file.
 
    procedure Add_Switches
-     (The_Package : Package_Id;
-      File_Name   : String;
-      Index       : Int;
-      Program     : Make_Program_Type);
+     (The_Package                      : Package_Id;
+      File_Name                        : String;
+      Index                            : Int;
+      Program                          : Make_Program_Type;
+      Unknown_Switches_To_The_Compiler : Boolean := True;
+      Project_Node_Tree                : Project_Node_Tree_Ref);
    procedure Add_Switch
      (S             : String_Access;
       Program       : Make_Program_Type;
@@ -746,6 +738,8 @@ package body Make is
       Is_Main_Source : Boolean;
       The_Args       : Argument_List;
       Lib_File       : File_Name_Type;
+      Full_Lib_File  : File_Name_Type;
+      Lib_File_Attr  : access File_Attributes;
       Read_Only      : Boolean;
       ALI            : out ALI_Id;
       O_File         : out File_Name_Type;
@@ -756,6 +750,10 @@ package body Make is
    --  ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
    --  up-to-date, then the corresponding source file needs to be recompiled.
    --  In this case ALI = No_ALI_Id.
+   --  Full_Lib_File must be the result of calling Osint.Full_Lib_File_Name on
+   --  Lib_File. Precomputing it saves system calls. Lib_File_Attr is the
+   --  initialized attributes of that file, which is also used to save on
+   --  system calls (it can safely be initialized to Unknown_Attributes).
 
    procedure Check_Linker_Options
      (E_Stamp : Time_Stamp_Type;
@@ -788,42 +786,48 @@ package body Make is
    --  Mapping files
    -----------------
 
-   type Temp_Path_Names is
-     array (Project_Id range <>, Positive range <>) of Path_Name_Type;
-
+   type Temp_Path_Names is array (Positive range <>) of Path_Name_Type;
    type Temp_Path_Ptr is access Temp_Path_Names;
 
-   type Indices is array (Project_Id range <>) of Natural;
+   type Free_File_Indices is array (Positive range <>) of Positive;
+   type Free_Indices_Ptr is access Free_File_Indices;
 
-   type Indices_Ptr is access Indices;
+   type Project_Compilation_Data is record
+      Mapping_File_Names : Temp_Path_Ptr;
+      --  The name ids of the temporary mapping files used. This is indexed
+      --  on the maximum number of compilation processes we will be spawning
+      --  (-j parameter)
 
-   type Free_File_Indices is array
-     (Project_Id range <>, Positive range <>) of Positive;
+      Last_Mapping_File_Names : Natural;
+      --  Index of the last mapping file created for this project
 
-   type Free_Indices_Ptr is access Free_File_Indices;
+      Free_Mapping_File_Indices : Free_Indices_Ptr;
+      --  Indices in Mapping_File_Names of the mapping file names that can be
+      --  reused for subsequent compilations.
 
-   The_Mapping_File_Names : Temp_Path_Ptr;
-   --  For each project, the name ids of the temporary mapping files used
+      Last_Free_Indices : Natural;
+      --  Number of mapping files that can be reused
+   end record;
+   --  Information necessary when compiling a project
 
-   Last_Mapping_File_Names : Indices_Ptr;
-   --  For each project, the index of the last mapping file created
+   type Project_Compilation_Access is access Project_Compilation_Data;
 
-   The_Free_Mapping_File_Indices : Free_Indices_Ptr;
-   --  For each project, the indices in The_Mapping_File_Names of the mapping
-   --  file names that can be reused for subsequent compilations.
+   package Project_Compilation_Htable is new Simple_HTable
+     (Header_Num => Prj.Header_Num,
+      Element    => Project_Compilation_Access,
+      No_Element => null,
+      Key        => Project_Id,
+      Hash       => Prj.Hash,
+      Equal      => "=");
 
-   Last_Free_Indices : Indices_Ptr;
-   --  For each project, the number of mapping files that can be reused
+   Project_Compilation : Project_Compilation_Htable.Instance;
 
    Gnatmake_Mapping_File : String_Access := null;
    --  The path name of a mapping file specified by switch -C=
 
-   procedure Delete_Mapping_Files;
-   --  Delete all temporary mapping files. Called only in Delete_All_Temp_Files
-   --  which ensures that Debug_Flag_N is False.
-
    procedure Init_Mapping_File
-     (Project : Project_Id;
+     (Project    : Project_Id;
+      Data       : in out Project_Compilation_Data;
       File_Index : in out Natural);
    --  Create a new temporary mapping file, and fill it with the project file
    --  mappings, when using project file(s). The out parameter File_Index is
@@ -1003,7 +1007,7 @@ package body Make is
    --  during a compilation are also transitively included in the W section
    --  of the originally compiled file.
 
-   procedure Initialize;
+   procedure Initialize (Project_Node_Tree : out Project_Node_Tree_Ref);
    --  Performs default and package initialization. Therefore,
    --  Compile_Sources can be called by an external unit.
 
@@ -1015,8 +1019,13 @@ package body Make is
    --  Args must have a lower bound of 1. Success indicates if the link
    --  succeeded or not.
 
-   procedure Scan_Make_Arg (Argv : String; And_Save : Boolean);
-   --  Scan make arguments. Argv is a single argument to be processed
+   procedure Scan_Make_Arg
+     (Project_Node_Tree : Project_Node_Tree_Ref;
+      Argv              : String;
+      And_Save          : Boolean);
+   --  Scan make arguments. Argv is a single argument to be processed.
+   --  Project_Node_Tree will be used to initialize external references. It
+   --  must have been initialized.
 
    -------------------
    -- Add_Arguments --
@@ -1067,8 +1076,7 @@ package body Make is
          Add_Lib_Search_Dir (Normalize_Pathname (Path));
 
       else
-         Get_Name_String
-           (Project_Tree.Projects.Table (Main_Project).Directory.Display_Name);
+         Get_Name_String (Main_Project.Directory.Display_Name);
          Add_Lib_Search_Dir
            (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
       end if;
@@ -1119,8 +1127,7 @@ package body Make is
          Add_Src_Search_Dir (Normalize_Pathname (Path));
 
       else
-         Get_Name_String
-           (Project_Tree.Projects.Table (Main_Project).Directory.Display_Name);
+         Get_Name_String (Main_Project.Directory.Display_Name);
          Add_Src_Search_Dir
            (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
       end if;
@@ -1236,64 +1243,40 @@ package body Make is
    ------------------
 
    procedure Add_Switches
-     (The_Package : Package_Id;
-      File_Name   : String;
-      Index       : Int;
-      Program     : Make_Program_Type)
+     (The_Package                      : Package_Id;
+      File_Name                        : String;
+      Index                            : Int;
+      Program                          : Make_Program_Type;
+      Unknown_Switches_To_The_Compiler : Boolean := True;
+      Project_Node_Tree                : Project_Node_Tree_Ref)
    is
       Switches    : Variable_Value;
       Switch_List : String_List_Id;
       Element     : String_Element;
 
    begin
+      Switch_May_Be_Passed_To_The_Compiler :=
+        Unknown_Switches_To_The_Compiler;
+
       if File_Name'Length > 0 then
-         Name_Len := File_Name'Length;
-         Name_Buffer (1 .. Name_Len) := File_Name;
+         Name_Len := 0;
+         Add_Str_To_Name_Buffer (File_Name);
          Switches :=
            Switches_Of
-           (Source_File      => Name_Find,
-            Source_File_Name => File_Name,
-            Source_Index     => Index,
-            Naming           => Project_Tree.Projects.Table
-                                  (Main_Project).Naming,
-            In_Package       => The_Package,
-            Allow_ALI        =>
-              Program = Binder or else Program = Linker);
-
-         case Switches.Kind is
-            when Undefined =>
-               null;
-
-            when List =>
-               Program_Args := Program;
-
-               Switch_List := Switches.Values;
-
-               while Switch_List /= Nil_String loop
-                  Element := Project_Tree.String_Elements.Table (Switch_List);
-                  Get_Name_String (Element.Value);
-
-                  if Name_Len > 0 then
-                     declare
-                        Argv : constant String := Name_Buffer (1 .. Name_Len);
-                        --  We need a copy, because Name_Buffer may be modified
-
-                     begin
-                        if Verbose_Mode then
-                           Write_Str ("   Adding ");
-                           Write_Line (Argv);
-                        end if;
-
-                        Scan_Make_Arg (Argv, And_Save => False);
-                     end;
-                  end if;
+             (Source_File      => Name_Find,
+              Source_File_Name => File_Name,
+              Source_Index     => Index,
+              Project          => Main_Project,
+              In_Package       => The_Package,
+              Allow_ALI        => Program = Binder or else Program = Linker);
 
-                  Switch_List := Element.Next;
-               end loop;
+         if Switches.Kind = List then
+            Program_Args := Program;
 
-            when Single =>
-               Program_Args := Program;
-               Get_Name_String (Switches.Value);
+            Switch_List := Switches.Values;
+            while Switch_List /= Nil_String loop
+               Element := Project_Tree.String_Elements.Table (Switch_List);
+               Get_Name_String (Element.Value);
 
                if Name_Len > 0 then
                   declare
@@ -1306,10 +1289,26 @@ package body Make is
                         Write_Line (Argv);
                      end if;
 
-                     Scan_Make_Arg (Argv, And_Save => False);
+                     Scan_Make_Arg
+                       (Project_Node_Tree, Argv, And_Save => False);
+
+                     if not Gnatmake_Switch_Found
+                       and then not Switch_May_Be_Passed_To_The_Compiler
+                     then
+                        Errutil.Error_Msg
+                          ('"' & Argv &
+                           """ is not a gnatmake switch. Consider moving " &
+                           "it to Global_Compilation_Switches.",
+                           Element.Location);
+                        Errutil.Finalize;
+                        Make_Failed ("*** illegal switch """ & Argv & """");
+                     end if;
                   end;
                end if;
-         end case;
+
+               Switch_List := Element.Next;
+            end loop;
+         end if;
       end if;
    end Add_Switches;
 
@@ -1356,7 +1355,7 @@ package body Make is
       Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
 
       if Gnatbind_Path = null then
-         Make_Failed ("error, unable to locate ", Gnatbind.all);
+         Make_Failed ("error, unable to locate " & Gnatbind.all);
       end if;
 
       GNAT.OS_Lib.Spawn
@@ -1372,34 +1371,24 @@ package body Make is
    --------------------------------
 
    procedure Change_To_Object_Directory (Project : Project_Id) is
-      Actual_Project   : Project_Id;
       Object_Directory : Path_Name_Type;
 
    begin
-      --  For sources outside of any project, compilation occurs in the object
-      --  directory of the main project, otherwise we use the project given.
-
-      if Project = No_Project then
-         Actual_Project := Main_Project;
-      else
-         Actual_Project := Project;
-      end if;
+      pragma Assert (Project /= No_Project);
 
       --  Nothing to do if the current working directory is already the correct
       --  object directory.
 
-      if Project_Of_Current_Object_Directory /= Actual_Project then
-         Project_Of_Current_Object_Directory := Actual_Project;
-         Object_Directory :=
-           Project_Tree.Projects.Table (Actual_Project).Object_Directory.Name;
+      if Project_Of_Current_Object_Directory /= Project then
+         Project_Of_Current_Object_Directory := Project;
+         Object_Directory := Project.Object_Directory.Display_Name;
 
          --  Set the working directory to the object directory of the actual
          --  project.
 
          if Verbose_Mode then
             Write_Str  ("Changing to object directory of """);
-            Write_Name
-              (Project_Tree.Projects.Table (Actual_Project).Display_Name);
+            Write_Name (Project.Display_Name);
             Write_Str  (""": """);
             Write_Name (Object_Directory);
             Write_Line ("""");
@@ -1414,11 +1403,9 @@ package body Make is
       when Directory_Error =>
          Make_Failed ("unable to change to object directory """ &
                       Path_Or_File_Name
-                        (Project_Tree.Projects.Table
-                           (Actual_Project).Object_Directory.Name) &
+                        (Project.Object_Directory.Display_Name) &
                       """ of project " &
-                      Get_Name_String (Project_Tree.Projects.Table
-                                         (Actual_Project).Display_Name));
+                      Get_Name_String (Project.Display_Name));
    end Change_To_Object_Directory;
 
    -----------
@@ -1431,6 +1418,8 @@ package body Make is
       Is_Main_Source : Boolean;
       The_Args       : Argument_List;
       Lib_File       : File_Name_Type;
+      Full_Lib_File  : File_Name_Type;
+      Lib_File_Attr  : access File_Attributes;
       Read_Only      : Boolean;
       ALI            : out ALI_Id;
       O_File         : out File_Name_Type;
@@ -1540,9 +1529,6 @@ package body Make is
       -- Data declarations for Check --
       ---------------------------------
 
-      Full_Lib_File : File_Name_Type;
-      --  Full name of current library file
-
       Full_Obj_File : File_Name_Type;
       --  Full name of the object file corresponding to Lib_File
 
@@ -1593,15 +1579,14 @@ package body Make is
                                                Check_Object_Consistency;
          begin
             Check_Object_Consistency := False;
-            Text := Read_Library_Info (Lib_File);
+            Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
             Check_Object_Consistency := Saved_Check_Object_Consistency;
          end;
 
       else
-         Text := Read_Library_Info (Lib_File);
+         Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
       end if;
 
-      Full_Lib_File := Full_Library_Info_Name;
       Full_Obj_File := Full_Object_File_Name;
       Lib_Stamp     := Current_Library_File_Stamp;
       Obj_Stamp     := Current_Object_File_Stamp;
@@ -1673,6 +1658,32 @@ package body Make is
             return;
          end if;
 
+         --  When compiling with -gnatc, don't take ALI file into account if
+         --  it has not been generated for the current source, for example if
+         --  it has been generated for the spec, but we are compiling the body.
+
+         if Operating_Mode = Check_Semantics then
+            declare
+               File_Name : constant String := Get_Name_String (Source_File);
+               OK        : Boolean := False;
+
+            begin
+               for U in ALIs.Table (ALI).First_Unit ..
+                 ALIs.Table (ALI).Last_Unit
+               loop
+                  OK := Get_Name_String (Units.Table (U).Sfile) = File_Name;
+                  exit when OK;
+               end loop;
+
+               if not OK then
+                  Verbose_Msg
+                    (Full_Lib_File, "not generated for the same source");
+                  ALI := No_ALI_Id;
+                  return;
+               end if;
+            end;
+         end if;
+
          --  Check for matching compiler switches if needed
 
          if Check_Switches then
@@ -1794,6 +1805,13 @@ package body Make is
 
          Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
 
+         --  To avoid using too much memory when switch -m is used, free the
+         --  memory allocated for the source file when computing the checksum.
+
+         if Minimal_Recompilation then
+            Sinput.P.Clear_Source_File_Table;
+         end if;
+
          if Modified_Source /= No_File then
             ALI := No_ALI_Id;
 
@@ -1823,58 +1841,12 @@ package body Make is
                   end if;
                end if;
 
-            elsif Main_Project /= No_Project then
-
-               --  Check if a file name does not correspond to the mapping of
-               --  units to file names.
-
-               declare
-                  WR        : With_Record;
-                  Unit_Name : Name_Id;
-                  UID       : Prj.Unit_Index;
-                  U_Data    : Unit_Data;
-
-               begin
-                  U_Chk :
-                  for U in ALIs.Table (ALI).First_Unit ..
-                           ALIs.Table (ALI).Last_Unit
-                  loop
-                     W_Check :
-                     for W in Units.Table (U).First_With
-                          ..
-                        Units.Table (U).Last_With
-                     loop
-                        WR := Withs.Table (W);
-
-                        if WR.Sfile /= No_File then
-                           Get_Name_String (WR.Uname);
-                           Name_Len := Name_Len - 2;
-                           Unit_Name := Name_Find;
-
-                           UID := Units_Htable.Get
-                                   (Project_Tree.Units_HT, Unit_Name);
-
-                           if UID /= Prj.No_Unit_Index then
-                              U_Data := Project_Tree.Units.Table (UID);
+            elsif not Read_Only and then Main_Project /= No_Project then
 
-                              if U_Data.File_Names (Body_Part).Name /= WR.Sfile
-                                and then
-                                  U_Data.File_Names (Specification).Name /=
-                                                                       WR.Sfile
-                              then
-                                 ALI := No_ALI_Id;
-
-                                 Verbose_Msg
-                                   (Unit_Name, " sources does not include ",
-                                    Name_Id (WR.Sfile));
-
-                                 return;
-                              end if;
-                           end if;
-                        end if;
-                     end loop W_Check;
-                  end loop U_Chk;
-               end;
+               if not Check_Source_Info_In_ALI (ALI, Project_Tree) then
+                  ALI := No_ALI_Id;
+                  return;
+               end if;
 
                --  Check that the ALI file is in the correct object directory.
                --  If it is in the object directory of a project that is
@@ -1889,23 +1861,25 @@ package body Make is
                ALI_Project := No_Project;
 
                declare
-                  Udata : Prj.Unit_Data;
+                  Udata : Prj.Unit_Index;
 
                begin
-                  for U in 1 .. Unit_Table.Last (Project_Tree.Units) loop
-                     Udata := Project_Tree.Units.Table (U);
-
-                     if Udata.File_Names (Body_Part).Name = Source_File then
-                        ALI_Project := Udata.File_Names (Body_Part).Project;
+                  Udata := Units_Htable.Get_First (Project_Tree.Units_HT);
+                  while Udata /= No_Unit_Index loop
+                     if Udata.File_Names (Impl) /= null
+                       and then Udata.File_Names (Impl).File = Source_File
+                     then
+                        ALI_Project := Udata.File_Names (Impl).Project;
                         exit;
 
-                     elsif
-                       Udata.File_Names (Specification).Name = Source_File
+                     elsif Udata.File_Names (Spec) /= null
+                       and then Udata.File_Names (Spec).File = Source_File
                      then
-                        ALI_Project :=
-                          Udata.File_Names (Specification).Project;
+                        ALI_Project := Udata.File_Names (Spec).Project;
                         exit;
                      end if;
+
+                     Udata := Units_Htable.Get_Next (Project_Tree.Units_HT);
                   end loop;
                end;
 
@@ -1919,29 +1893,24 @@ package body Make is
                                   Normalize_Pathname
                                     (Dir_Name
                                       (Get_Name_String (Full_Lib_File)),
-                                     Resolve_Links  => True,
+                                     Resolve_Links  =>
+                                       Opt.Follow_Links_For_Dirs,
                                      Case_Sensitive => False);
 
                begin
                   Name_Len := 0;
                   Add_Str_To_Name_Buffer (Res_Obj_Dir);
 
-                  if Name_Len > 1 and then
-                    (Name_Buffer (Name_Len) = '/' or else
-                       Name_Buffer (Name_Len) = Directory_Separator)
-                  then
-                     Name_Len := Name_Len - 1;
+                  if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
+                     Add_Char_To_Name_Buffer (Directory_Separator);
                   end if;
 
                   Obj_Dir := Name_Find;
 
-                  while ALI_Project /= No_Project and then
-                    Obj_Dir /=
-                      Project_Tree.Projects.Table
-                        (ALI_Project).Object_Directory.Name
+                  while ALI_Project /= No_Project
+                    and then Obj_Dir /= ALI_Project.Object_Directory.Name
                   loop
-                     ALI_Project :=
-                       Project_Tree.Projects.Table (ALI_Project).Extended_By;
+                     ALI_Project := ALI_Project.Extended_By;
                   end loop;
                end;
 
@@ -1956,9 +1925,7 @@ package body Make is
                --  If the ALI project is not extended, then it must be in
                --  the correct object directory.
 
-               if Project_Tree.Projects.Table (ALI_Project).Extended_By =
-                 No_Project
-               then
+               if ALI_Project.Extended_By = No_Project then
                   return;
                end if;
 
@@ -1972,7 +1939,7 @@ package body Make is
                   Num_Ext := 0;
                   Proj := ALI_Project;
                   loop
-                     Proj := Project_Tree.Projects.Table (Proj).Extended_By;
+                     Proj := Proj.Extended_By;
                      exit when Proj = No_Project;
                      Num_Ext := Num_Ext + 1;
                   end loop;
@@ -1983,11 +1950,12 @@ package body Make is
                      Projects : array (1 .. Num_Ext) of Project_Id;
                      Dep      : Sdep_Record;
                      OK       : Boolean := True;
+                     UID      : Unit_Index;
 
                   begin
                      Proj := ALI_Project;
                      for J in Projects'Range loop
-                        Proj := Project_Tree.Projects.Table (Proj).Extended_By;
+                        Proj := Proj.Extended_By;
                         Projects (J) := Proj;
                      end loop;
 
@@ -1999,24 +1967,20 @@ package body Make is
                        ALIs.Table (ALI).Last_Sdep
                      loop
                         Dep := Sdep.Table (D);
-
+                        UID  := Units_Htable.Get_First (Project_Tree.Units_HT);
                         Proj := No_Project;
 
                         Unit_Loop :
-                        for
-                          UID in 1 .. Unit_Table.Last (Project_Tree.Units)
-                        loop
-                           if Project_Tree.Units.Table (UID).
-                             File_Names (Body_Part).Name = Dep.Sfile
+                        while UID /= null loop
+                           if UID.File_Names (Impl) /= null
+                             and then UID.File_Names (Impl).File = Dep.Sfile
                            then
-                              Proj := Project_Tree.Units.Table (UID).
-                                File_Names (Body_Part).Project;
+                              Proj := UID.File_Names (Impl).Project;
 
-                           elsif Project_Tree.Units.Table (UID).
-                             File_Names (Specification).Name = Dep.Sfile
+                           elsif UID.File_Names (Spec) /= null
+                             and then UID.File_Names (Spec).File = Dep.Sfile
                            then
-                              Proj := Project_Tree.Units.Table (UID).
-                                File_Names (Specification).Project;
+                              Proj := UID.File_Names (Spec).Project;
                            end if;
 
                            --  If a source is in a project, check if it is one
@@ -2032,6 +1996,9 @@ package body Make is
 
                               exit Unit_Loop;
                            end if;
+
+                           UID :=
+                             Units_Htable.Get_Next (Project_Tree.Units_HT);
                         end loop Unit_Loop;
                      end loop D_Chk;
 
@@ -2270,7 +2237,6 @@ package body Make is
       Args           : Argument_List)
    is
    begin
-      Arguments_Collected := True;
       Arguments_Project := No_Project;
       Last_Argument := 0;
       Add_Arguments (Args);
@@ -2281,7 +2247,6 @@ package body Make is
                                  Get_Name_String (Source_File);
             Compiler_Package : Prj.Package_Id;
             Switches         : Prj.Variable_Value;
-            Data             : Project_Data;
 
          begin
             Prj.Env.
@@ -2298,29 +2263,23 @@ package body Make is
             if Arguments_Project = No_Project then
                Add_Arguments (The_Saved_Gcc_Switches.all);
 
-            elsif not Project_Tree.Projects.Table
-                        (Arguments_Project).Externally_Built
+            elsif not Arguments_Project.Externally_Built
+              or else Must_Compile
             then
                --  We get the project directory for the relative path
                --  switches and arguments.
 
-               Data := Project_Tree.Projects.Table (Arguments_Project);
-
-               --  If the source is in an extended project, we go to
-               --  the ultimate extending project.
-
-               while Data.Extended_By /= No_Project loop
-                  Arguments_Project := Data.Extended_By;
-                  Data := Project_Tree.Projects.Table (Arguments_Project);
-               end loop;
+               Arguments_Project :=
+                 Ultimate_Extending_Project_Of (Arguments_Project);
 
                --  If building a dynamic or relocatable library, compile with
                --  PIC option, if it exists.
 
-               if Data.Library and then Data.Library_Kind /= Static then
+               if Arguments_Project.Library
+                 and then Arguments_Project.Library_Kind /= Static
+               then
                   declare
                      PIC : constant String := MLib.Tgt.PIC_Option;
-
                   begin
                      if PIC /= "" then
                         Add_Arguments ((1 => new String'(PIC)));
@@ -2328,20 +2287,13 @@ package body Make is
                   end;
                end if;
 
-               if Data.Dir_Path = null then
-                  Data.Dir_Path :=
-                    new String'(Get_Name_String (Data.Directory.Display_Name));
-                  Project_Tree.Projects.Table (Arguments_Project) :=
-                    Data;
-               end if;
-
                --  We now look for package Compiler and get the switches from
                --  this package.
 
                Compiler_Package :=
                  Prj.Util.Value_Of
                    (Name        => Name_Compiler,
-                    In_Packages => Data.Decl.Packages,
+                    In_Packages => Arguments_Project.Decl.Packages,
                     In_Tree     => Project_Tree);
 
                if Compiler_Package /= No_Package then
@@ -2355,7 +2307,7 @@ package body Make is
                       (Source_File      => Source_File,
                        Source_File_Name => Source_File_Name,
                        Source_Index     => Source_Index,
-                       Naming           => Data.Naming,
+                       Project          => Arguments_Project,
                        In_Package       => Compiler_Package,
                        Allow_ALI        => False);
 
@@ -2384,6 +2336,8 @@ package body Make is
                         declare
                            New_Args : Argument_List (1 .. Number);
                            Last_New : Natural := 0;
+                           Dir_Path : constant String := Get_Name_String
+                             (Arguments_Project.Directory.Display_Name);
 
                         begin
                            Current := Switches.Values;
@@ -2399,7 +2353,7 @@ package body Make is
                                    new String'(Name_Buffer (1 .. Name_Len));
                                  Test_If_Relative_Path
                                    (New_Args (Last_New),
-                                    Parent => Data.Dir_Path,
+                                    Parent => Dir_Path,
                                     Including_Non_Switch => False);
                               end if;
 
@@ -2424,11 +2378,15 @@ package body Make is
                         New_Args : Argument_List :=
                                      (1 => new String'
                                             (Name_Buffer (1 .. Name_Len)));
+                        Dir_Path : constant String :=
+                                     Get_Name_String
+                                      (Arguments_Project.
+                                       Directory.Display_Name);
 
                      begin
                         Test_If_Relative_Path
                           (New_Args (1),
-                           Parent               => Data.Dir_Path,
+                           Parent               => Dir_Path,
                            Including_Non_Switch => False);
                         Add_Arguments
                           (Configuration_Pragmas_Switch (Arguments_Project) &
@@ -2498,62 +2456,22 @@ package body Make is
       Initialize_ALI_Data   : Boolean  := True;
       Max_Process           : Positive := 1)
    is
-      Source_Unit : Unit_Name_Type;
-      --  Current source unit
-
-      Source_File : File_Name_Type;
-      --  Current source file
-
-      Full_Source_File : File_Name_Type;
-      --  Full name of the current source file
-
-      Lib_File : File_Name_Type;
-      --  Current library file
-
-      Full_Lib_File : File_Name_Type;
-      --  Full name of the current library file
-
-      Obj_File : File_Name_Type;
-      --  Full name of the object file corresponding to Lib_File
-
-      Obj_Stamp : Time_Stamp_Type;
-      --  Time stamp of the current object file
-
-      Sfile : File_Name_Type;
-      --  Contains the source file of the units withed by Source_File
-
-      Uname : Unit_Name_Type;
-      --  Contains the unit name of the units withed by Source_File
-
-      ALI : ALI_Id;
-      --  ALI Id of the current ALI file
-
-      --  Comment following declarations ???
-
-      Read_Only : Boolean := False;
-
-      Compilation_OK  : Boolean;
-      Need_To_Compile : Boolean;
-
-      Pid  : Process_Id;
-      Text : Text_Buffer_Ptr;
-
-      Mfile : Natural := No_Mapping_File;
+      Mfile            : Natural := No_Mapping_File;
+      Mapping_File_Arg : String_Access;
+      --  Info on the mapping file
 
       Need_To_Check_Standard_Library : Boolean :=
-                                         Check_Readonly_Files
+                                         (Check_Readonly_Files or Must_Compile)
                                            and not Unique_Compile;
 
-      Mapping_File_Arg : String_Access;
-
-      Process_Created : Boolean := False;
-
       procedure Add_Process
-        (Pid   : Process_Id;
-         Sfile : File_Name_Type;
-         Afile : File_Name_Type;
-         Uname : Unit_Name_Type;
-         Mfile : Natural := No_Mapping_File);
+        (Pid           : Process_Id;
+         Sfile         : File_Name_Type;
+         Afile         : File_Name_Type;
+         Uname         : Unit_Name_Type;
+         Full_Lib_File : File_Name_Type;
+         Lib_File_Attr : File_Attributes;
+         Mfile         : Natural := No_Mapping_File);
       --  Adds process Pid to the current list of outstanding compilation
       --  processes and record the full name of the source file Sfile that
       --  we are compiling, the name of its library file Afile and the
@@ -2562,18 +2480,14 @@ package body Make is
       --  array The_Mapping_File_Names.
 
       procedure Await_Compile
-        (Sfile : out File_Name_Type;
-         Afile : out File_Name_Type;
-         Uname : out Unit_Name_Type;
+        (Data  : out Compilation_Data;
          OK    : out Boolean);
-      --  Awaits that an outstanding compilation process terminates. When
-      --  it does set Sfile to the name of the source file that was compiled
-      --  Afile to the name of its library file and Uname to the name of its
-      --  unit. Note that this time stamp can be used to check whether the
-      --  compilation did generate an object file. OK is set to True if the
-      --  compilation succeeded. Note that Sfile, Afile and Uname could be
-      --  resp. No_File, No_File and No_Name  if there were no compilations
-      --  to wait for.
+      --  Awaits that an outstanding compilation process terminates. When it
+      --  does set Data to the information registered for the corresponding
+      --  call to Add_Process. Note that this time stamp can be used to check
+      --  whether the compilation did generate an object file. OK is set to
+      --  True if the compilation succeeded. Data could be No_Compilation_Data
+      --  if there was no compilation to wait for.
 
       function Bad_Compilation_Count return Natural;
       --  Returns the number of compilation failures
@@ -2582,12 +2496,18 @@ package body Make is
       --  Check if s-stalib.adb needs to be compiled
 
       procedure Collect_Arguments_And_Compile
-        (Source_File  : File_Name_Type;
-         Source_Index : Int);
-      --  Collect arguments from project file (if any) and compile
+        (Full_Source_File : File_Name_Type;
+         Lib_File         : File_Name_Type;
+         Source_Index     : Int;
+         Pid              : out Process_Id;
+         Process_Created  : out Boolean);
+      --  Collect arguments from project file (if any) and compile. If no
+      --  compilation was attempted, Processed_Created is set to False, and the
+      --  value of Pid is unknown.
 
       function Compile
-        (S            : File_Name_Type;
+        (Project      : Project_Id;
+         S            : File_Name_Type;
          L            : File_Name_Type;
          Source_Index : Int;
          Args         : Argument_List) return Process_Id;
@@ -2596,8 +2516,13 @@ package body Make is
       --  library file name. Process_Id of the process spawned to execute the
       --  compilation.
 
+      type ALI_Project is record
+         ALI      : ALI_Id;
+         Project : Project_Id;
+      end record;
+
       package Good_ALI is new Table.Table (
-        Table_Component_Type => ALI_Id,
+        Table_Component_Type => ALI_Project,
         Table_Index_Type     => Natural,
         Table_Low_Bound      => 1,
         Table_Initial        => 50,
@@ -2612,7 +2537,7 @@ package body Make is
       --  Get a mapping file name. If there is one to be reused, reuse it.
       --  Otherwise, create a new mapping file.
 
-      function Get_Next_Good_ALI return ALI_Id;
+      function Get_Next_Good_ALI return ALI_Project;
       --  Returns the next good ALI_Id record
 
       procedure Record_Failure
@@ -2623,19 +2548,44 @@ package body Make is
       --  If Found is False then the compilation of File failed because we
       --  could not find it. Records also Unit when possible.
 
-      procedure Record_Good_ALI (A : ALI_Id);
+      procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id);
       --  Records in the previous set the Id of an ALI file
 
+      function Must_Exit_Because_Of_Error return Boolean;
+      --  Return True if there were errors and the user decided to exit in such
+      --  a case. This waits for any outstanding compilation.
+
+      function Start_Compile_If_Possible (Args : Argument_List) return Boolean;
+      --  Check if there is more work that we can do (i.e. the Queue is non
+      --  empty). If there is, do it only if we have not yet used up all the
+      --  available processes.
+      --  Returns True if we should exit the main loop
+
+      procedure Wait_For_Available_Slot;
+      --  Check if we should wait for a compilation to finish. This is the case
+      --  if all the available processes are busy compiling sources or there is
+      --  nothing else to do (that is the Q is empty and there are no good ALIs
+      --  to process).
+
+      procedure Fill_Queue_From_ALI_Files;
+      --  Check if we recorded good ALI files. If yes process them now in the
+      --  order in which they have been recorded. There are two occasions in
+      --  which we record good ali files. The first is in phase 1 when, after
+      --  scanning an existing ALI file we realize it is up-to-date, the second
+      --  instance is after a successful compilation.
+
       -----------------
       -- Add_Process --
       -----------------
 
       procedure Add_Process
-        (Pid   : Process_Id;
-         Sfile : File_Name_Type;
-         Afile : File_Name_Type;
-         Uname : Unit_Name_Type;
-         Mfile : Natural := No_Mapping_File)
+        (Pid           : Process_Id;
+         Sfile         : File_Name_Type;
+         Afile         : File_Name_Type;
+         Uname         : Unit_Name_Type;
+         Full_Lib_File : File_Name_Type;
+         Lib_File_Attr : File_Attributes;
+         Mfile         : Natural := No_Mapping_File)
       is
          OC1 : constant Positive := Outstanding_Compiles + 1;
 
@@ -2643,16 +2593,21 @@ package body Make is
          pragma Assert (OC1 <= Max_Process);
          pragma Assert (Pid /= Invalid_Pid);
 
-         Running_Compile (OC1).Pid              := Pid;
-         Running_Compile (OC1).Full_Source_File := Sfile;
-         Running_Compile (OC1).Lib_File         := Afile;
-         Running_Compile (OC1).Source_Unit      := Uname;
-         Running_Compile (OC1).Mapping_File     := Mfile;
-         Running_Compile (OC1).Project          := Arguments_Project;
-         Running_Compile (OC1).Syntax_Only      := Syntax_Only;
-         Running_Compile (OC1).Output_Is_Object := Output_Is_Object;
+         Running_Compile (OC1) :=
+           (Pid              => Pid,
+            Full_Source_File => Sfile,
+            Lib_File         => Afile,
+            Full_Lib_File    => Full_Lib_File,
+            Lib_File_Attr    => Lib_File_Attr,
+            Source_Unit      => Uname,
+            Mapping_File     => Mfile,
+            Project          => Arguments_Project);
 
          Outstanding_Compiles := OC1;
+
+         if Arguments_Project /= No_Project then
+            Queue.Set_Obj_Dir_Busy (Arguments_Project.Object_Directory.Name);
+         end if;
       end Add_Process;
 
       --------------------
@@ -2660,21 +2615,18 @@ package body Make is
       -------------------
 
       procedure Await_Compile
-        (Sfile  : out File_Name_Type;
-         Afile  : out File_Name_Type;
-         Uname  : out Unit_Name_Type;
-         OK     : out Boolean)
+        (Data : out Compilation_Data;
+         OK   : out Boolean)
       is
-         Pid     : Process_Id;
-         Project : Project_Id;
+         Pid       : Process_Id;
+         Project   : Project_Id;
+         Comp_Data : Project_Compilation_Access;
 
       begin
          pragma Assert (Outstanding_Compiles > 0);
 
-         Sfile := No_File;
-         Afile := No_File;
-         Uname := No_Unit_Name;
-         OK    := False;
+         Data := No_Compilation_Data;
+         OK   := False;
 
          --  The loop here is a work-around for a problem on VMS; in some
          --  circumstances (shared library and several executables, for
@@ -2691,22 +2643,25 @@ package body Make is
 
             for J in Running_Compile'First .. Outstanding_Compiles loop
                if Pid = Running_Compile (J).Pid then
-                  Sfile := Running_Compile (J).Full_Source_File;
-                  Afile := Running_Compile (J).Lib_File;
-                  Uname := Running_Compile (J).Source_Unit;
-                  Syntax_Only := Running_Compile (J).Syntax_Only;
-                  Output_Is_Object := Running_Compile (J).Output_Is_Object;
+                  Data    := Running_Compile (J);
                   Project := Running_Compile (J).Project;
 
-                  --  If a mapping file was used by this compilation,
-                  --  get its file name for reuse by a subsequent compilation
+                  if Project /= No_Project then
+                     Queue.Set_Obj_Dir_Free (Project.Object_Directory.Name);
+                  end if;
+
+                  --  If a mapping file was used by this compilation, get its
+                  --  file name for reuse by a subsequent compilation.
 
                   if Running_Compile (J).Mapping_File /= No_Mapping_File then
-                     Last_Free_Indices (Project) :=
-                       Last_Free_Indices (Project) + 1;
-                     The_Free_Mapping_File_Indices
-                       (Project, Last_Free_Indices (Project)) :=
-                       Running_Compile (J).Mapping_File;
+                     Comp_Data :=
+                       Project_Compilation_Htable.Get
+                         (Project_Compilation, Project);
+                     Comp_Data.Last_Free_Indices :=
+                       Comp_Data.Last_Free_Indices + 1;
+                     Comp_Data.Free_Mapping_File_Indices
+                       (Comp_Data.Last_Free_Indices) :=
+                         Running_Compile (J).Mapping_File;
                   end if;
 
                   --  To actually remove this Pid and related info from
@@ -2715,7 +2670,6 @@ package body Make is
 
                   if J = Outstanding_Compiles then
                      null;
-
                   else
                      Running_Compile (J) :=
                        Running_Compile (Outstanding_Compiles);
@@ -2729,6 +2683,8 @@ package body Make is
             --  This child process was not one of our compilation processes;
             --  just ignore it for now.
 
+            --  Why is this commented out code sitting here???
+
             --  raise Program_Error;
          end loop;
       end Await_Compile;
@@ -2756,17 +2712,15 @@ package body Make is
                Add_It : Boolean := True;
 
             begin
-               Name_Len := Standard_Library_Package_Body_Name'Length;
-               Name_Buffer (1 .. Name_Len) :=
-                 Standard_Library_Package_Body_Name;
+               Name_Len := 0;
+               Add_Str_To_Name_Buffer (Standard_Library_Package_Body_Name);
                Sfile := Name_Enter;
 
                --  If we have a special runtime, we add the standard
                --  library only if we can find it.
 
                if RTS_Switch then
-                  Add_It :=
-                    Find_File (Sfile, Osint.Source) /= No_File;
+                  Add_It := Full_Source_Name (Sfile) /= No_File;
                end if;
 
                if Add_It then
@@ -2776,7 +2730,7 @@ package body Make is
                      end if;
 
                   else
-                     Insert_Q (Sfile, Index => 0);
+                     Queue.Insert (Sfile, Project => No_Project, Index => 0);
                      Mark (Sfile, Index => 0);
                   end if;
                end if;
@@ -2789,22 +2743,14 @@ package body Make is
       -----------------------------------
 
       procedure Collect_Arguments_And_Compile
-        (Source_File  : File_Name_Type;
-         Source_Index : Int)
-      is
+        (Full_Source_File : File_Name_Type;
+         Lib_File         : File_Name_Type;
+         Source_Index     : Int;
+         Pid              : out Process_Id;
+         Process_Created  : out Boolean) is
       begin
-         --  Process_Created will be set True if an attempt is made to compile
-         --  the source, that is if it is not in an externally built project.
-
          Process_Created := False;
 
-         --  If arguments not yet collected (in Check), collect them now
-
-         if not Arguments_Collected then
-            Collect_Arguments
-              (Source_File, Source_Index, Source_File = Main_Source, Args);
-         end if;
-
          --  If we use mapping file (-P or -C switches), then get one
 
          if Create_Mapping_File then
@@ -2815,33 +2761,29 @@ package body Make is
          --  check for an eventual library project, and use the full path.
 
          if Arguments_Project /= No_Project then
-            if not Project_Tree.Projects.Table
-                     (Arguments_Project).Externally_Built
+            if not Arguments_Project.Externally_Built
+              or else Must_Compile
             then
-               Prj.Env.Set_Ada_Paths (Arguments_Project, Project_Tree, True);
+               Prj.Env.Set_Ada_Paths
+                 (Arguments_Project,
+                  Project_Tree,
+                  Including_Libraries => True,
+                  Include_Path        => Use_Include_Path_File);
 
                if not Unique_Compile
                  and then MLib.Tgt.Support_For_Libraries /= Prj.None
                then
                   declare
-                     The_Data : Project_Data :=
-                                  Project_Tree.Projects.Table
-                                    (Arguments_Project);
-
-                     Prj : Project_Id := Arguments_Project;
+                     Prj : constant Project_Id :=
+                             Ultimate_Extending_Project_Of (Arguments_Project);
 
                   begin
-                     while The_Data.Extended_By /= No_Project loop
-                        Prj := The_Data.Extended_By;
-                        The_Data := Project_Tree.Projects.Table (Prj);
-                     end loop;
-
-                     if The_Data.Library
-                       and then not The_Data.Externally_Built
-                       and then not The_Data.Need_To_Build_Lib
+                     if Prj.Library
+                       and then (not Prj.Externally_Built or else Must_Compile)
+                       and then not Prj.Need_To_Build_Lib
                      then
-                        --  Add to the Q all sources of the project that
-                        --  have not been marked
+                        --  Add to the Q all sources of the project that have
+                        --  not been marked.
 
                         Insert_Project_Sources
                           (The_Project  => Prj,
@@ -2850,23 +2792,18 @@ package body Make is
 
                         --  Now mark the project as processed
 
-                        Project_Tree.Projects.Table
-                          (Prj).Need_To_Build_Lib := True;
+                        Prj.Need_To_Build_Lib := True;
                      end if;
                   end;
                end if;
 
-               --  Change to the object directory of the project file,
-               --  if necessary.
-
-               Change_To_Object_Directory (Arguments_Project);
-
                Pid :=
                  Compile
-                   (File_Name_Type (Arguments_Path_Name),
-                    Lib_File,
-                    Source_Index,
-                    Arguments (1 .. Last_Argument));
+                   (Project       => Arguments_Project,
+                    S             => File_Name_Type (Arguments_Path_Name),
+                    L             => Lib_File,
+                    Source_Index  => Source_Index,
+                    Args          => Arguments (1 .. Last_Argument));
                Process_Created := True;
             end if;
 
@@ -2874,12 +2811,13 @@ package body Make is
             --  If this is a source outside of any project file, make sure it
             --  will be compiled in object directory of the main project file.
 
-            if Main_Project /= No_Project then
-               Change_To_Object_Directory (Arguments_Project);
-            end if;
-
-            Pid := Compile (Full_Source_File, Lib_File, Source_Index,
-                            Arguments (1 .. Last_Argument));
+            Pid :=
+              Compile
+                (Project        => Main_Project,
+                 S              => Full_Source_File,
+                 L              => Lib_File,
+                 Source_Index   => Source_Index,
+                 Args           => Arguments (1 .. Last_Argument));
             Process_Created := True;
          end if;
       end Collect_Arguments_And_Compile;
@@ -2889,12 +2827,13 @@ package body Make is
       -------------
 
       function Compile
-        (S            : File_Name_Type;
+        (Project      : Project_Id;
+         S            : File_Name_Type;
          L            : File_Name_Type;
          Source_Index : Int;
          Args         : Argument_List) return Process_Id
       is
-         Comp_Args : Argument_List (Args'First .. Args'Last + 9);
+         Comp_Args : Argument_List (Args'First .. Args'Last + 10);
          Comp_Next : Integer := Args'First;
          Comp_Last : Integer;
          Arg_Index : Integer;
@@ -2951,11 +2890,16 @@ package body Make is
             end if;
          end loop;
 
+         Comp_Args (Comp_Next) := new String'("-gnatea");
+         Comp_Next := Comp_Next + 1;
+
          Comp_Args (Comp_Next) := Comp_Flag;
          Comp_Next := Comp_Next + 1;
 
          --  Optimize the simple case where the gcc command line looks like
-         --     gcc -c -I. ... -I- file.adb  --into->  gcc -c ... file.adb
+         --     gcc -c -I. ... -I- file.adb
+         --  into
+         --     gcc -c ... file.adb
 
          if Args (Args'First).all = "-I" & Normalized_CWD
            and then Args (Args'Last).all = "-I-"
@@ -2987,7 +2931,7 @@ package body Make is
 
          begin
             if Is_Predefined_File_Name (Fname, False) then
-               if Check_Readonly_Files then
+               if Check_Readonly_Files or else Must_Compile then
                   Comp_Args (Comp_Args'First + 2 .. Comp_Last + 1) :=
                     Comp_Args (Comp_Args'First + 1 .. Comp_Last);
                   Comp_Last := Comp_Last + 1;
@@ -3058,9 +3002,9 @@ package body Make is
             end if;
          end if;
 
-         if Create_Mapping_File then
+         if Create_Mapping_File and then Mapping_File_Arg /= null then
             Comp_Last := Comp_Last + 1;
-            Comp_Args (Comp_Last) := Mapping_File_Arg;
+            Comp_Args (Comp_Last) := new String'(Mapping_File_Arg.all);
          end if;
 
          Get_Name_String (S);
@@ -3068,6 +3012,12 @@ package body Make is
          Comp_Last := Comp_Last + 1;
          Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
 
+         --  Change to object directory of the project file, if necessary
+
+         if Project /= No_Project then
+            Change_To_Object_Directory (Project);
+         end if;
+
          GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
 
          Comp_Last := Comp_Last + 1;
@@ -3076,7 +3026,7 @@ package body Make is
          Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
 
          if Gcc_Path = null then
-            Make_Failed ("error, unable to locate ", Gcc.all);
+            Make_Failed ("error, unable to locate " & Gcc.all);
          end if;
 
          return
@@ -3084,23 +3034,144 @@ package body Make is
              (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
       end Compile;
 
+      -------------------------------
+      -- Fill_Queue_From_ALI_Files --
+      -------------------------------
+
+      procedure Fill_Queue_From_ALI_Files is
+         ALI_P        : ALI_Project;
+         ALI          : ALI_Id;
+         Source_Index : Int;
+         Sfile        : File_Name_Type;
+         Uname        : Unit_Name_Type;
+         Unit_Name    : Name_Id;
+         Uid          : Prj.Unit_Index;
+
+      begin
+         while Good_ALI_Present loop
+            ALI_P        := Get_Next_Good_ALI;
+            ALI          := ALI_P.ALI;
+            Source_Index := Unit_Index_Of (ALIs.Table (ALI_P.ALI).Afile);
+
+            --  If we are processing the library file corresponding to the
+            --  main source file check if this source can be a main unit.
+
+            if ALIs.Table (ALI).Sfile = Main_Source
+              and then Source_Index = Main_Index
+            then
+               Main_Unit := ALIs.Table (ALI).Main_Program /= None;
+            end if;
+
+            --  The following adds the standard library (s-stalib) to the list
+            --  of files to be handled by gnatmake: this file and any files it
+            --  depends on are always included in every bind, even if they are
+            --  not in the explicit dependency list. Of course, it is not added
+            --  if Suppress_Standard_Library is True.
+
+            --  However, to avoid annoying output about s-stalib.ali being read
+            --  only, when "-v" is used, we add the standard library only when
+            --  "-a" is used.
+
+            if Need_To_Check_Standard_Library then
+               Check_Standard_Library;
+            end if;
+
+            --  Now insert in the Q the unmarked source files (i.e. those which
+            --  have never been inserted in the Q and hence never considered).
+            --  Only do that if Unique_Compile is False.
+
+            if not Unique_Compile then
+               for J in
+                 ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
+               loop
+                  for K in
+                    Units.Table (J).First_With .. Units.Table (J).Last_With
+                  loop
+                     Sfile := Withs.Table (K).Sfile;
+                     Uname := Withs.Table (K).Uname;
+
+                     --  If project files are used, find the proper source to
+                     --  compile in case Sfile is the spec but there is a body.
+
+                     if Main_Project /= No_Project then
+                        Get_Name_String (Uname);
+                        Name_Len  := Name_Len - 2;
+                        Unit_Name := Name_Find;
+                        Uid :=
+                          Units_Htable.Get (Project_Tree.Units_HT, Unit_Name);
+
+                        if Uid /= Prj.No_Unit_Index then
+                           if Uid.File_Names (Impl) /= null
+                             and then not Uid.File_Names (Impl).Locally_Removed
+                           then
+                              Sfile        := Uid.File_Names (Impl).File;
+                              Source_Index := Uid.File_Names (Impl).Index;
+
+                           elsif Uid.File_Names (Spec) /= null
+                             and then not Uid.File_Names (Spec).Locally_Removed
+                           then
+                              Sfile        := Uid.File_Names (Spec).File;
+                              Source_Index := Uid.File_Names (Spec).Index;
+                           end if;
+                        end if;
+                     end if;
+
+                     Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
+
+                     if Is_In_Obsoleted (Sfile) then
+                        Executable_Obsolete := True;
+                     end if;
+
+                     if Sfile = No_File then
+                        Debug_Msg ("Skipping generic:", Withs.Table (K).Uname);
+
+                     else
+                        Source_Index := Unit_Index_Of (Withs.Table (K).Afile);
+
+                        if Is_Marked (Sfile, Source_Index) then
+                           Debug_Msg ("Skipping marked file:", Sfile);
+
+                        elsif not (Check_Readonly_Files or Must_Compile)
+                          and then Is_Internal_File_Name (Sfile, False)
+                        then
+                           Debug_Msg ("Skipping internal file:", Sfile);
+
+                        else
+                           Queue.Insert
+                             (Sfile,
+                              ALI_P.Project,
+                              Withs.Table (K).Uname,
+                              Source_Index);
+                           Mark (Sfile, Source_Index);
+                        end if;
+                     end if;
+                  end loop;
+               end loop;
+            end if;
+         end loop;
+      end Fill_Queue_From_ALI_Files;
+
       ----------------------
       -- Get_Mapping_File --
       ----------------------
 
       procedure Get_Mapping_File (Project : Project_Id) is
+         Data : Project_Compilation_Access;
+
       begin
+         Data := Project_Compilation_Htable.Get (Project_Compilation, Project);
+
          --  If there is a mapping file ready to be reused, reuse it
 
-         if Last_Free_Indices (Project) > 0 then
-            Mfile := The_Free_Mapping_File_Indices
-                       (Project, Last_Free_Indices (Project));
-            Last_Free_Indices (Project) := Last_Free_Indices (Project) - 1;
+         if Data.Last_Free_Indices > 0 then
+            Mfile := Data.Free_Mapping_File_Indices (Data.Last_Free_Indices);
+            Data.Last_Free_Indices := Data.Last_Free_Indices - 1;
 
          --  Otherwise, create and initialize a new one
 
          else
-            Init_Mapping_File (Project => Project, File_Index => Mfile);
+            Init_Mapping_File
+              (Project => Project, Data => Data.all, File_Index => Mfile);
          end if;
 
          --  Put the name in the mapping file argument for the invocation
@@ -3109,23 +3180,21 @@ package body Make is
          Free (Mapping_File_Arg);
          Mapping_File_Arg :=
            new String'("-gnatem=" &
-                       Get_Name_String
-                         (The_Mapping_File_Names (Project, Mfile)));
-
+                       Get_Name_String (Data.Mapping_File_Names (Mfile)));
       end Get_Mapping_File;
 
       -----------------------
       -- Get_Next_Good_ALI --
       -----------------------
 
-      function Get_Next_Good_ALI return ALI_Id is
-         ALI : ALI_Id;
+      function Get_Next_Good_ALI return ALI_Project is
+         ALIP : ALI_Project;
 
       begin
          pragma Assert (Good_ALI_Present);
-         ALI := Good_ALI.Table (Good_ALI.Last);
+         ALIP := Good_ALI.Table (Good_ALI.Last);
          Good_ALI.Decrement_Last;
-         return ALI;
+         return ALIP;
       end Get_Next_Good_ALI;
 
       ----------------------
@@ -3137,6 +3206,30 @@ package body Make is
          return Good_ALI.First <= Good_ALI.Last;
       end Good_ALI_Present;
 
+      --------------------------------
+      -- Must_Exit_Because_Of_Error --
+      --------------------------------
+
+      function Must_Exit_Because_Of_Error return Boolean is
+         Data    : Compilation_Data;
+         Success : Boolean;
+
+      begin
+         if Bad_Compilation_Count > 0 and then not Keep_Going then
+            while Outstanding_Compiles > 0 loop
+               Await_Compile (Data, Success);
+
+               if not Success then
+                  Record_Failure (Data.Full_Source_File, Data.Source_Unit);
+               end if;
+            end loop;
+
+            return True;
+         end if;
+
+         return False;
+      end Must_Exit_Because_Of_Error;
+
       --------------------
       -- Record_Failure --
       --------------------
@@ -3155,286 +3248,341 @@ package body Make is
       -- Record_Good_ALI --
       ---------------------
 
-      procedure Record_Good_ALI (A : ALI_Id) is
+      procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id) is
       begin
          Good_ALI.Increment_Last;
-         Good_ALI.Table (Good_ALI.Last) := A;
+         Good_ALI.Table (Good_ALI.Last) := (A, Project);
       end Record_Good_ALI;
 
-   --  Start of processing for Compile_Sources
-
-   begin
-      pragma Assert (Args'First = 1);
+      -------------------------------
+      -- Start_Compile_If_Possible --
+      -------------------------------
 
-      Outstanding_Compiles := 0;
-      Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
+      function Start_Compile_If_Possible
+        (Args : Argument_List) return Boolean
+      is
+         In_Lib_Dir      : Boolean;
+         Need_To_Compile : Boolean;
+         Pid             : Process_Id;
+         Process_Created : Boolean;
 
-      --  Package and Queue initializations
+         Source_File      : File_Name_Type;
+         Full_Source_File : File_Name_Type;
+         Source_File_Attr : aliased File_Attributes;
+         --  The full name of the source file and its attributes (size, ...)
 
-      Good_ALI.Init;
+         Source_Unit  : Unit_Name_Type;
+         Source_Index : Int;
+         --  Index of the current unit in the current source file
 
-      if First_Q_Initialization then
-         Init_Q;
-      end if;
+         Lib_File      : File_Name_Type;
+         Full_Lib_File : File_Name_Type;
+         Lib_File_Attr : aliased File_Attributes;
+         Read_Only     : Boolean := False;
+         ALI           : ALI_Id;
+         --  The ALI file and its attributes (size, stamp, ...)
 
-      if Initialize_ALI_Data then
-         Initialize_ALI;
-         Initialize_ALI_Source;
-      end if;
+         Obj_File  : File_Name_Type;
+         Obj_Stamp : Time_Stamp_Type;
+         --  The object file
 
-      --  The following two flags affect the behavior of ALI.Set_Source_Table.
-      --  We set Check_Source_Files to True to ensure that source file
-      --  time stamps are checked, and we set All_Sources to False to
-      --  avoid checking the presence of the source files listed in the
-      --  source dependency section of an ali file (which would be a mistake
-      --  since the ali file may be obsolete).
+      begin
+         if not Queue.Is_Virtually_Empty and then
+            Outstanding_Compiles < Max_Process
+         then
+            Queue.Extract (Source_File, Source_Unit, Source_Index);
 
-      Check_Source_Files := True;
-      All_Sources        := False;
+            Osint.Full_Source_Name
+              (Source_File,
+               Full_File => Full_Source_File,
+               Attr      => Source_File_Attr'Access);
 
-      --  Only insert in the Q if it is not already done, to avoid simultaneous
-      --  compilations if -jnnn is used.
+            Lib_File := Osint.Lib_File_Name (Source_File, Source_Index);
 
-      if not Is_Marked (Main_Source, Main_Index) then
-         Insert_Q (Main_Source, Index => Main_Index);
-         Mark (Main_Source, Main_Index);
-      end if;
+            --  ??? This call could be avoided when using projects, since we
+            --  know where the ALI file is supposed to be. That would avoid
+            --  searches in the object directories, including in the runtime
+            --  dir. However, that would require getting access to the
+            --  Source_Id.
 
-      First_Compiled_File   := No_File;
-      Most_Recent_Obj_File  := No_File;
-      Most_Recent_Obj_Stamp := Empty_Time_Stamp;
-      Main_Unit             := False;
+            Osint.Full_Lib_File_Name
+              (Lib_File,
+               Lib_File => Full_Lib_File,
+               Attr     => Lib_File_Attr);
 
-      --  Keep looping until there is no more work to do (the Q is empty)
-      --  and all the outstanding compilations have terminated
+            --  If source has already been compiled, executable is obsolete
 
-      Make_Loop : while not Empty_Q or else Outstanding_Compiles > 0 loop
+            if Is_In_Obsoleted (Source_File) then
+               Executable_Obsolete := True;
+            end if;
 
-         --  If the user does not want to keep going in case of errors then
-         --  wait for the remaining outstanding compiles and then exit.
+            In_Lib_Dir := Full_Lib_File /= No_File
+                          and then In_Ada_Lib_Dir (Full_Lib_File);
 
-         if Bad_Compilation_Count > 0 and then not Keep_Going then
-            while Outstanding_Compiles > 0 loop
-               Await_Compile
-                 (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
+            --  Since the following requires a system call, we precompute it
+            --  when needed.
 
-               if not Compilation_OK then
-                  Record_Failure (Full_Source_File, Source_Unit);
+            if not In_Lib_Dir then
+               if Full_Lib_File /= No_File
+                 and then not (Check_Readonly_Files or else Must_Compile)
+               then
+                  Get_Name_String (Full_Lib_File);
+                  Name_Buffer (Name_Len + 1) := ASCII.NUL;
+                  Read_Only := not Is_Writable_File
+                    (Name_Buffer'Address, Lib_File_Attr'Access);
+               else
+                  Read_Only := False;
                end if;
-            end loop;
+            end if;
 
-            exit Make_Loop;
-         end if;
+            --  If the library file is an Ada library skip it
 
-         --  PHASE 1: Check if there is more work that we can do (i.e. the Q
-         --  is non empty). If there is, do it only if we have not yet used
-         --  up all the available processes.
+            if In_Lib_Dir then
+               Verbose_Msg
+                 (Lib_File,
+                  "is in an Ada library",
+                  Prefix => "  ",
+                  Minimum_Verbosity => Opt.High);
 
-         if not Empty_Q and then Outstanding_Compiles < Max_Process then
-            declare
-               Source_Index : Int;
-               --  Index of the current unit in the current source file
+               --  If the library file is a read-only library skip it, but only
+               --  if, when using project files, this library file is in the
+               --  right object directory (a read-only ALI file in the object
+               --  directory of a project being extended must not be skipped).
 
-            begin
-               Extract_From_Q (Source_File, Source_Unit, Source_Index);
-               Full_Source_File := Osint.Full_Source_Name (Source_File);
-               Lib_File         := Osint.Lib_File_Name
-                 (Source_File, Source_Index);
-               Full_Lib_File    := Osint.Full_Lib_File_Name (Lib_File);
+            elsif Read_Only
+              and then Is_In_Object_Directory (Source_File, Full_Lib_File)
+            then
+               Verbose_Msg
+                 (Lib_File,
+                  "is a read-only library",
+                  Prefix => "  ",
+                  Minimum_Verbosity => Opt.High);
 
-               --  If this source has already been compiled, the executable is
-               --  obsolete.
+               --  The source file that we are checking cannot be located
 
-               if Is_In_Obsoleted (Source_File) then
-                  Executable_Obsolete := True;
-               end if;
+            elsif Full_Source_File = No_File then
+               Record_Failure (Source_File, Source_Unit, False);
 
-               --  If the library file is an Ada library skip it
+               --  Source and library files can be located but are internal
+               --  files.
 
-               if Full_Lib_File /= No_File
-                 and then In_Ada_Lib_Dir (Full_Lib_File)
-               then
-                  Verbose_Msg
-                    (Lib_File,
-                     "is in an Ada library",
-                     Prefix => "  ",
-                     Minimum_Verbosity => Opt.High);
-
-                  --  If the library file is a read-only library skip it, but
-                  --  only if, when using project files, this library file is
-                  --  in the right object directory (a read-only ALI file
-                  --  in the object directory of a project being extended
-                  --  should not be skipped).
-
-               elsif Full_Lib_File /= No_File
-                 and then not Check_Readonly_Files
-                 and then Is_Readonly_Library (Full_Lib_File)
-                 and then Is_In_Object_Directory (Source_File, Full_Lib_File)
-               then
-                  Verbose_Msg
-                    (Lib_File,
-                     "is a read-only library",
-                     Prefix => "  ",
-                     Minimum_Verbosity => Opt.High);
+            elsif not (Check_Readonly_Files or else Must_Compile)
+              and then Full_Lib_File /= No_File
+              and then Is_Internal_File_Name (Source_File, False)
+            then
+               if Force_Compilations then
+                  Fail
+                    ("not allowed to compile """ &
+                     Get_Name_String (Source_File) &
+                     """; use -a switch, or compile file with " &
+                     """-gnatg"" switch");
+               end if;
+
+               Verbose_Msg
+                 (Lib_File,
+                  "is an internal library",
+                  Prefix => "  ",
+                  Minimum_Verbosity => Opt.High);
 
-                  --  The source file that we are checking cannot be located
+               --  The source file that we are checking can be located
 
-               elsif Full_Source_File = No_File then
-                  Record_Failure (Source_File, Source_Unit, False);
+            else
+               Collect_Arguments (Source_File, Source_Index,
+                                  Source_File = Main_Source, Args);
 
-                  --  Source and library files can be located but are internal
-                  --  files
+               --  Do nothing if project of source is externally built
 
-               elsif not Check_Readonly_Files
-                 and then Full_Lib_File /= No_File
-                 and then Is_Internal_File_Name (Source_File, False)
+               if Arguments_Project = No_Project
+                 or else not Arguments_Project.Externally_Built
+                 or else Must_Compile
                then
-                  if Force_Compilations then
-                     Fail
-                       ("not allowed to compile """ &
-                        Get_Name_String (Source_File) &
-                        """; use -a switch, or compile file with " &
-                        """-gnatg"" switch");
+                  --  Don't waste any time if we have to recompile anyway
+
+                  Obj_Stamp       := Empty_Time_Stamp;
+                  Need_To_Compile := Force_Compilations;
+
+                  if not Force_Compilations then
+                     Check (Source_File    => Source_File,
+                            Source_Index   => Source_Index,
+                            Is_Main_Source => Source_File = Main_Source,
+                            The_Args       => Args,
+                            Lib_File       => Lib_File,
+                            Full_Lib_File  => Full_Lib_File,
+                            Lib_File_Attr  => Lib_File_Attr'Access,
+                            Read_Only      => Read_Only,
+                            ALI            => ALI,
+                            O_File         => Obj_File,
+                            O_Stamp        => Obj_Stamp);
+                     Need_To_Compile := (ALI = No_ALI_Id);
                   end if;
 
-                  Verbose_Msg
-                    (Lib_File,
-                     "is an internal library",
-                     Prefix => "  ",
-                     Minimum_Verbosity => Opt.High);
-
-               --  The source file that we are checking can be located
+                  if not Need_To_Compile then
 
-               else
-                  Arguments_Collected := False;
+                     --  The ALI file is up-to-date; record its Id
 
-                  Collect_Arguments (Source_File, Source_Index,
-                                     Source_File = Main_Source, Args);
+                     Record_Good_ALI (ALI, Arguments_Project);
 
-                  --  Do nothing if project of source is externally built
+                     --  Record the time stamp of the most recent object
+                     --  file as long as no (re)compilations are needed.
 
-                  if Arguments_Project = No_Project
-                    or else not Project_Tree.Projects.Table
-                                  (Arguments_Project).Externally_Built
-                  then
-                     --  Don't waste any time if we have to recompile anyway
-
-                     Obj_Stamp       := Empty_Time_Stamp;
-                     Need_To_Compile := Force_Compilations;
-
-                     if not Force_Compilations then
-                        Read_Only :=
-                          Full_Lib_File /= No_File
-                          and then not Check_Readonly_Files
-                          and then Is_Readonly_Library (Full_Lib_File);
-                        Check (Source_File, Source_Index,
-                               Source_File = Main_Source, Args, Lib_File,
-                               Read_Only, ALI, Obj_File, Obj_Stamp);
-                        Need_To_Compile := (ALI = No_ALI_Id);
+                     if First_Compiled_File = No_File
+                       and then (Most_Recent_Obj_File = No_File
+                                  or else Obj_Stamp > Most_Recent_Obj_Stamp)
+                     then
+                        Most_Recent_Obj_File  := Obj_File;
+                        Most_Recent_Obj_Stamp := Obj_Stamp;
                      end if;
 
-                     if not Need_To_Compile then
-                        --  The ALI file is up-to-date. Record its Id
-
-                        Record_Good_ALI (ALI);
-
-                        --  Record the time stamp of the most recent object
-                        --  file as long as no (re)compilations are needed.
+                  else
+                     --  Check that switch -x has been used if a source outside
+                     --  of project files need to be compiled.
 
-                        if First_Compiled_File = No_File
-                          and then (Most_Recent_Obj_File = No_File
-                                    or else Obj_Stamp > Most_Recent_Obj_Stamp)
-                        then
-                           Most_Recent_Obj_File  := Obj_File;
-                           Most_Recent_Obj_Stamp := Obj_Stamp;
-                        end if;
+                     if Main_Project /= No_Project
+                       and then Arguments_Project = No_Project
+                       and then not External_Unit_Compilation_Allowed
+                     then
+                        Make_Failed ("external source ("
+                                     & Get_Name_String (Source_File)
+                                     & ") is not part of any project;"
+                                     & " cannot be compiled without"
+                                     & " gnatmake switch -x");
+                     end if;
 
-                     else
-                        --  Check that switch -x has been used if a source
-                        --  outside of project files need to be compiled.
+                     --  Is this the first file we have to compile?
 
-                        if Main_Project /= No_Project
-                          and then Arguments_Project = No_Project
-                          and then not External_Unit_Compilation_Allowed
-                        then
-                           Make_Failed ("external source (",
-                                        Get_Name_String (Source_File),
-                                        ") is not part of any project;"
-                                        & " cannot be compiled without" &
-                                        " gnatmake switch -x");
-                        end if;
+                     if First_Compiled_File = No_File then
+                        First_Compiled_File  := Full_Source_File;
+                        Most_Recent_Obj_File := No_File;
 
-                        --  Is this the first file we have to compile?
+                        if Do_Not_Execute then
 
-                        if First_Compiled_File = No_File then
-                           First_Compiled_File  := Full_Source_File;
-                           Most_Recent_Obj_File := No_File;
+                           --  Exit the main loop
 
-                           if Do_Not_Execute then
-                              exit Make_Loop;
-                           end if;
+                           return True;
                         end if;
+                     end if;
 
-                        if In_Place_Mode then
+                     --  Compute where the ALI file must be generated in
+                     --  In_Place_Mode (this does not require to know the
+                     --  location of the object directory).
+
+                     if In_Place_Mode then
+                        if Full_Lib_File = No_File then
 
                            --  If the library file was not found, then save
                            --  the library file near the source file.
 
-                           if Full_Lib_File = No_File then
-                              Lib_File := Osint.Lib_File_Name
-                                (Full_Source_File, Source_Index);
+                           Lib_File :=
+                             Osint.Lib_File_Name
+                               (Full_Source_File, Source_Index);
+                           Full_Lib_File := Lib_File;
+
+                        else
+                           --  If the library file was found, then save the
+                           --  library file in the same place.
+
+                           Lib_File := Full_Lib_File;
+                        end if;
+                     end if;
 
-                              --  If the library file was found, then save the
-                              --  library file in the same place.
+                     --  Start the compilation and record it. We can do this
+                     --  because there is at least one free process. This might
+                     --  change the current directory.
+
+                     Collect_Arguments_And_Compile
+                       (Full_Source_File => Full_Source_File,
+                        Lib_File         => Lib_File,
+                        Source_Index     => Source_Index,
+                        Pid              => Pid,
+                        Process_Created  => Process_Created);
+
+                     --  Compute where the ALI file will be generated (for
+                     --  cases that might require to know the current
+                     --  directory). The current directory might be changed
+                     --  when compiling other files so we cannot rely on it
+                     --  being the same to find the resulting ALI file.
+
+                     if not In_Place_Mode then
+
+                        --  Compute the expected location of the ALI file. This
+                        --  can be from several places:
+                        --    -i => in place mode. In such a case,
+                        --          Full_Lib_File has already been set above
+                        --    -D => if specified
+                        --    or defaults in current dir
+                        --  We could simply use a call similar to
+                        --     Osint.Full_Lib_File_Name (Lib_File)
+                        --  but that involves system calls and is thus slower
+
+                        if Object_Directory_Path /= null then
+                           Name_Len := 0;
+                           Add_Str_To_Name_Buffer (Object_Directory_Path.all);
+                           Add_Str_To_Name_Buffer (Get_Name_String (Lib_File));
+                           Full_Lib_File := Name_Find;
+
+                        else
+                           if Project_Of_Current_Object_Directory /=
+                             No_Project
+                           then
+                              Get_Name_String
+                                (Project_Of_Current_Object_Directory
+                                 .Object_Directory.Display_Name);
+                              Add_Str_To_Name_Buffer
+                                (Get_Name_String (Lib_File));
+                              Full_Lib_File := Name_Find;
 
                            else
-                              Lib_File := Full_Lib_File;
+                              Full_Lib_File := Lib_File;
                            end if;
-
                         end if;
 
-                        --  Start the compilation and record it. We can do
-                        --  this because there is at least one free process.
+                     end if;
 
-                        Collect_Arguments_And_Compile
-                          (Source_File, Source_Index);
+                     Lib_File_Attr := Unknown_Attributes;
 
-                        --  Make sure we could successfully start
-                        --  the Compilation.
+                     --  Make sure we could successfully start the compilation
 
-                        if Process_Created then
-                           if Pid = Invalid_Pid then
-                              Record_Failure (Full_Source_File, Source_Unit);
-                           else
-                              Add_Process
-                                (Pid,
-                                 Full_Source_File,
-                                 Lib_File,
-                                 Source_Unit,
-                                 Mfile);
-                           end if;
+                     if Process_Created then
+                        if Pid = Invalid_Pid then
+                           Record_Failure (Full_Source_File, Source_Unit);
+                        else
+                           Add_Process
+                             (Pid           => Pid,
+                              Sfile         => Full_Source_File,
+                              Afile         => Lib_File,
+                              Uname         => Source_Unit,
+                              Mfile         => Mfile,
+                              Full_Lib_File => Full_Lib_File,
+                              Lib_File_Attr => Lib_File_Attr);
                         end if;
                      end if;
                   end if;
                end if;
-            end;
+            end if;
          end if;
+         return False;
+      end Start_Compile_If_Possible;
+
+      -----------------------------
+      -- Wait_For_Available_Slot --
+      -----------------------------
 
-         --  PHASE 2: Now check if we should wait for a compilation to
-         --  finish. This is the case if all the available processes are
-         --  busy compiling sources or there is nothing else to do
-         --  (that is the Q is empty and there are no good ALIs to process).
+      procedure Wait_For_Available_Slot is
+         Compilation_OK : Boolean;
+         Text           : Text_Buffer_Ptr;
+         ALI            : ALI_Id;
+         Data           : Compilation_Data;
 
+      begin
          if Outstanding_Compiles = Max_Process
-           or else (Empty_Q
+           or else (Queue.Is_Virtually_Empty
                      and then not Good_ALI_Present
                      and then Outstanding_Compiles > 0)
          then
-            Await_Compile
-              (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
+            Await_Compile (Data, Compilation_OK);
 
             if not Compilation_OK then
-               Record_Failure (Full_Source_File, Source_Unit);
+               Record_Failure (Data.Full_Source_File, Data.Source_Unit);
             end if;
 
             if Compilation_OK or else Keep_Going then
@@ -3446,29 +3594,34 @@ package body Make is
                                                Check_Object_Consistency;
 
                begin
-                  --  If compilation was not OK, or if output is not an
-                  --  object file and we don't do the bind step, don't check
-                  --  for object consistency.
+                  --  If compilation was not OK, or if output is not an object
+                  --  file and we don't do the bind step, don't check for
+                  --  object consistency.
 
                   Check_Object_Consistency :=
                     Check_Object_Consistency
-                    and Compilation_OK
-                    and (Output_Is_Object or Do_Bind_Step);
-                  Text := Read_Library_Info (Lib_File);
+                      and Compilation_OK
+                      and (Output_Is_Object or Do_Bind_Step);
+
+                  Text :=
+                    Read_Library_Info_From_Full
+                      (Data.Full_Lib_File, Data.Lib_File_Attr'Access);
 
                   --  Restore Check_Object_Consistency to its initial value
 
                   Check_Object_Consistency := Saved_Object_Consistency;
                end;
 
-               --  If an ALI file was generated by this compilation, scan
-               --  the ALI file and record it.
+               --  If an ALI file was generated by this compilation, scan the
+               --  ALI file and record it.
+
                --  If the scan fails, a previous ali file is inconsistent with
                --  the unit just compiled.
 
                if Text /= null then
                   ALI :=
-                    Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
+                    Scan_ALI
+                      (Data.Lib_File, Text, Ignore_ED => False, Err => True);
 
                   if ALI = No_ALI_Id then
 
@@ -3476,15 +3629,18 @@ package body Make is
 
                      if Compilation_OK then
                         Inform
-                          (Lib_File,
+                          (Data.Lib_File,
                            "incompatible ALI file, please recompile");
-                        Record_Failure (Full_Source_File, Source_Unit);
+                        Record_Failure
+                          (Data.Full_Source_File, Data.Source_Unit);
                      end if;
+
                   else
-                     Free (Text);
-                     Record_Good_ALI (ALI);
+                     Record_Good_ALI (ALI, Data.Project);
                   end if;
 
+                  Free (Text);
+
                --  If we could not read the ALI file that was just generated
                --  then there could be a problem reading either the ALI or the
                --  corresponding object file (if Check_Object_Consistency is
@@ -3495,155 +3651,77 @@ package body Make is
                else
                   if Compilation_OK and not Syntax_Only then
                      Inform
-                       (Lib_File,
+                       (Data.Lib_File,
                         "WARNING: ALI or object file not found after compile");
-                     Record_Failure (Full_Source_File, Source_Unit);
+                     Record_Failure (Data.Full_Source_File, Data.Source_Unit);
                   end if;
                end if;
             end if;
          end if;
+      end Wait_For_Available_Slot;
 
-         --  PHASE 3: Check if we recorded good ALI files. If yes process
-         --  them now in the order in which they have been recorded. There
-         --  are two occasions in which we record good ali files. The first is
-         --  in phase 1 when, after scanning an existing ALI file we realize
-         --  it is up-to-date, the second instance is after a successful
-         --  compilation.
-
-         while Good_ALI_Present loop
-            ALI := Get_Next_Good_ALI;
-
-            declare
-               Source_Index : Int := Unit_Index_Of (ALIs.Table (ALI).Afile);
-
-            begin
-               --  If we are processing the library file corresponding to the
-               --  main source file check if this source can be a main unit.
-
-               if ALIs.Table (ALI).Sfile = Main_Source and then
-                 Source_Index = Main_Index
-               then
-                  Main_Unit := ALIs.Table (ALI).Main_Program /= None;
-               end if;
+   --  Start of processing for Compile_Sources
 
-               --  The following adds the standard library (s-stalib) to the
-               --  list of files to be handled by gnatmake: this file and any
-               --  files it depends on are always included in every bind,
-               --  even if they are not in the explicit dependency list.
-               --  Of course, it is not added if Suppress_Standard_Library
-               --  is True.
+   begin
+      pragma Assert (Args'First = 1);
 
-               --  However, to avoid annoying output about s-stalib.ali being
-               --  read only, when "-v" is used, we add the standard library
-               --  only when "-a" is used.
+      Outstanding_Compiles := 0;
+      Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
 
-               if Need_To_Check_Standard_Library then
-                  Check_Standard_Library;
-               end if;
+      --  Package and Queue initializations
 
-               --  Now insert in the Q the unmarked source files (i.e. those
-               --  which have never been inserted in the Q and hence never
-               --  considered). Only do that if Unique_Compile is False.
+      Good_ALI.Init;
 
-               if not Unique_Compile then
-                  for J in
-                    ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
-                  loop
-                     for K in
-                       Units.Table (J).First_With .. Units.Table (J).Last_With
-                     loop
-                        Sfile := Withs.Table (K).Sfile;
-                        Uname := Withs.Table (K).Uname;
+      if Initialize_ALI_Data then
+         Initialize_ALI;
+         Initialize_ALI_Source;
+      end if;
 
-                        --  If project files are used, find the proper source
-                        --  to compile, in case Sfile is the spec, but there
-                        --  is a body.
+      --  The following two flags affect the behavior of ALI.Set_Source_Table.
+      --  We set Check_Source_Files to True to ensure that source file time
+      --  stamps are checked, and we set All_Sources to False to avoid checking
+      --  the presence of the source files listed in the source dependency
+      --  section of an ali file (which would be a mistake since the ali file
+      --  may be obsolete).
 
-                        if Main_Project /= No_Project then
-                           declare
-                              Unit_Name : Name_Id;
-                              Uid       : Prj.Unit_Index;
-                              Udata     : Unit_Data;
+      Check_Source_Files := True;
+      All_Sources        := False;
 
-                           begin
-                              Get_Name_String (Uname);
-                              Name_Len := Name_Len - 2;
-                              Unit_Name := Name_Find;
-                              Uid :=
-                                Units_Htable.Get
-                                  (Project_Tree.Units_HT, Unit_Name);
-
-                              if Uid /= Prj.No_Unit_Index then
-                                 Udata := Project_Tree.Units.Table (Uid);
-
-                                 if
-                                    Udata.File_Names (Body_Part).Name /=
-                                                                       No_File
-                                   and then
-                                     Udata.File_Names (Body_Part).Path.Name /=
-                                       Slash
-                                 then
-                                    Sfile := Udata.File_Names (Body_Part).Name;
-                                    Source_Index :=
-                                      Udata.File_Names (Body_Part).Index;
-
-                                 elsif
-                                    Udata.File_Names (Specification).Name /=
-                                                                        No_File
-                                   and then
-                                     Udata.File_Names
-                                       (Specification).Path.Name /= Slash
-                                 then
-                                    Sfile :=
-                                      Udata.File_Names (Specification).Name;
-                                    Source_Index :=
-                                      Udata.File_Names (Specification).Index;
-                                 end if;
-                              end if;
-                           end;
-                        end if;
+      --  Only insert in the Q if it is not already done, to avoid simultaneous
+      --  compilations if -jnnn is used.
 
-                        Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
+      if not Is_Marked (Main_Source, Main_Index) then
+         Queue.Insert (Main_Source, Main_Project, Index => Main_Index);
+         Mark (Main_Source, Main_Index);
+      end if;
 
-                        if Is_In_Obsoleted (Sfile) then
-                           Executable_Obsolete := True;
-                        end if;
+      First_Compiled_File   := No_File;
+      Most_Recent_Obj_File  := No_File;
+      Most_Recent_Obj_Stamp := Empty_Time_Stamp;
+      Main_Unit             := False;
 
-                        if Sfile = No_File then
-                           Debug_Msg
-                             ("Skipping generic:", Withs.Table (K).Uname);
+      --  Keep looping until there is no more work to do (the Q is empty)
+      --  and all the outstanding compilations have terminated.
 
-                        else
-                           Source_Index :=
-                             Unit_Index_Of (Withs.Table (K).Afile);
+      Make_Loop :
+      while not Queue.Is_Empty or else Outstanding_Compiles > 0 loop
+         exit Make_Loop when Must_Exit_Because_Of_Error;
+         exit Make_Loop when Start_Compile_If_Possible (Args);
 
-                           if Is_Marked (Sfile, Source_Index) then
-                              Debug_Msg ("Skipping marked file:", Sfile);
+         Wait_For_Available_Slot;
 
-                           elsif not Check_Readonly_Files
-                             and then Is_Internal_File_Name (Sfile, False)
-                           then
-                              Debug_Msg ("Skipping internal file:", Sfile);
+         --  ??? Should be done as soon as we add a Good_ALI, wouldn't it avoid
+         --  the need for a list of good ALI?
 
-                           else
-                              Insert_Q
-                                (Sfile, Withs.Table (K).Uname, Source_Index);
-                              Mark (Sfile, Source_Index);
-                           end if;
-                        end if;
-                     end loop;
-                  end loop;
-               end if;
-            end;
-         end loop;
+         Fill_Queue_From_ALI_Files;
 
          if Display_Compilation_Progress then
             Write_Str ("completed ");
-            Write_Int (Int (Q_Front));
+            Write_Int (Int (Queue.Processed));
             Write_Str (" out of ");
-            Write_Int (Int (Q.Last));
+            Write_Int (Int (Queue.Size));
             Write_Str (" (");
-            Write_Int (Int ((Q_Front * 100) / (Q.Last - Q.First)));
+            Write_Int (Int ((Queue.Processed * 100) / Queue.Size));
             Write_Str ("%)...");
             Write_Eol;
          end if;
@@ -3660,95 +3738,6 @@ package body Make is
       end if;
    end Compile_Sources;
 
-   -----------------------------------
-   -- Compute_All_Imported_Projects --
-   -----------------------------------
-
-   procedure Compute_All_Imported_Projects (Project : Project_Id) is
-      procedure Add_To_List (Prj : Project_Id);
-      --  Add a project to the list All_Imported_Projects of project Project
-
-      procedure Recursive_Add_Imported (Project : Project_Id);
-      --  Recursively add the projects imported by project Project, but not
-      --  those that are extended.
-
-      -----------------
-      -- Add_To_List --
-      -----------------
-
-      procedure Add_To_List (Prj : Project_Id) is
-         Element : constant Project_Element :=
-           (Prj, Project_Tree.Projects.Table (Project).All_Imported_Projects);
-         List : Project_List;
-      begin
-         Project_List_Table.Increment_Last (Project_Tree.Project_Lists);
-         List := Project_List_Table.Last (Project_Tree.Project_Lists);
-         Project_Tree.Project_Lists.Table (List) := Element;
-         Project_Tree.Projects.Table (Project).All_Imported_Projects := List;
-      end Add_To_List;
-
-      ----------------------------
-      -- Recursive_Add_Imported --
-      ----------------------------
-
-      procedure Recursive_Add_Imported (Project : Project_Id) is
-         List    : Project_List;
-         Element : Project_Element;
-         Prj     : Project_Id;
-
-      begin
-         if Project /= No_Project then
-
-            --  For all the imported projects
-
-            List := Project_Tree.Projects.Table (Project).Imported_Projects;
-            while List /= Empty_Project_List loop
-               Element := Project_Tree.Project_Lists.Table (List);
-               Prj := Element.Project;
-
-               --  Get the ultimate extending project
-
-               while
-                 Project_Tree.Projects.Table (Prj).Extended_By /= No_Project
-               loop
-                  Prj := Project_Tree.Projects.Table (Prj).Extended_By;
-               end loop;
-
-               --  If project has not yet been visited, add to list and recurse
-
-               if not Project_Tree.Projects.Table (Prj).Seen then
-                  Project_Tree.Projects.Table (Prj).Seen := True;
-                  Add_To_List (Prj);
-                  Recursive_Add_Imported (Prj);
-               end if;
-
-               List := Element.Next;
-            end loop;
-
-            --  Recurse on projects being imported, if any
-
-            Recursive_Add_Imported
-              (Project_Tree.Projects.Table (Project).Extends);
-         end if;
-      end Recursive_Add_Imported;
-
-   begin
-      --  Reset the Seen flag for all projects
-
-      for Index in 1 .. Project_Table.Last (Project_Tree.Projects) loop
-         Project_Tree.Projects.Table (Index).Seen := False;
-      end loop;
-
-      --  Make sure the list is empty
-
-      Project_Tree.Projects.Table (Project).All_Imported_Projects :=
-        Empty_Project_List;
-
-      --  Add to the list all projects imported directly or indirectly
-
-      Recursive_Add_Imported (Project);
-   end Compute_All_Imported_Projects;
-
    ----------------------------------
    -- Configuration_Pragmas_Switch --
    ----------------------------------
@@ -3795,9 +3784,7 @@ package body Make is
             else
                declare
                   Parent_Directory : constant String :=
-                    Get_Name_String
-                      (Project_Tree.Projects.Table
-                           (Project).Directory.Display_Name);
+                    Get_Name_String (Project.Directory.Display_Name);
 
                begin
                   if Parent_Directory (Parent_Directory'Last) =
@@ -3817,20 +3804,14 @@ package body Make is
 
    begin
       Prj.Env.Create_Config_Pragmas_File
-        (For_Project, Main_Project, Project_Tree);
+        (For_Project, Project_Tree);
 
-      if Project_Tree.Projects.Table
-           (For_Project).Config_File_Name /= No_Path
-      then
-         Temporary_Config_File :=
-           Project_Tree.Projects.Table (For_Project).Config_File_Temp;
+      if For_Project.Config_File_Name /= No_Path then
+         Temporary_Config_File := For_Project.Config_File_Temp;
          Last := 1;
          Result (1) :=
            new String'
-                 ("-gnatec=" &
-                  Get_Name_String
-                    (Project_Tree.Projects.Table
-                       (For_Project).Config_File_Name));
+                 ("-gnatec=" & Get_Name_String (For_Project.Config_File_Name));
 
       else
          Temporary_Config_File := False;
@@ -3838,8 +3819,7 @@ package body Make is
 
       --  Check for attribute Builder'Global_Configuration_Pragmas
 
-      The_Packages := Project_Tree.Projects.Table
-                        (Main_Project).Decl.Packages;
+      The_Packages := Main_Project.Decl.Packages;
       Gnatmake :=
         Prj.Util.Value_Of
           (Name        => Name_Builder,
@@ -3866,12 +3846,11 @@ package body Make is
                if not Is_Regular_File (Path) then
                   if Debug.Debug_Flag_F then
                      Make_Failed
-                       ("cannot find configuration pragmas file ",
-                        File_Name (Path));
+                       ("cannot find configuration pragmas file "
+                        File_Name (Path));
                   else
                      Make_Failed
-                       ("cannot find configuration pragmas file ",
-                        Path);
+                       ("cannot find configuration pragmas file " & Path);
                   end if;
                end if;
 
@@ -3883,8 +3862,7 @@ package body Make is
 
       --  Check for attribute Compiler'Local_Configuration_Pragmas
 
-      The_Packages :=
-        Project_Tree.Projects.Table (For_Project).Decl.Packages;
+      The_Packages := For_Project.Decl.Packages;
       Compiler :=
         Prj.Util.Value_Of
           (Name        => Name_Compiler,
@@ -3911,12 +3889,12 @@ package body Make is
                if not Is_Regular_File (Path) then
                   if Debug.Debug_Flag_F then
                      Make_Failed
-                       ("cannot find configuration pragmas file ",
-                        File_Name (Path));
+                       ("cannot find configuration pragmas file "
+                        File_Name (Path));
 
                   else
                      Make_Failed
-                       ("cannot find configuration pragmas file ", Path);
+                       ("cannot find configuration pragmas file " & Path);
                   end if;
                end if;
 
@@ -3961,43 +3939,18 @@ package body Make is
    procedure Delete_All_Temp_Files is
    begin
       if not Debug.Debug_Flag_N then
-         Delete_Mapping_Files;
          Delete_Temp_Config_Files;
-         Prj.Env.Delete_All_Path_Files (Project_Tree);
+         Prj.Delete_All_Temp_Files (Project_Tree);
       end if;
    end Delete_All_Temp_Files;
 
-   --------------------------
-   -- Delete_Mapping_Files --
-   --------------------------
-
-   procedure Delete_Mapping_Files is
-      Success : Boolean;
-      pragma Warnings (Off, Success);
-
-   begin
-      --  The caller is responsible for ensuring that Debug_Flag_N is False
-
-      pragma Assert (not Debug.Debug_Flag_N);
-
-      if The_Mapping_File_Names /= null then
-         for Project in The_Mapping_File_Names'Range (1) loop
-            for Index in 1 .. Last_Mapping_File_Names (Project) loop
-               Delete_File
-                 (Name => Get_Name_String
-                            (The_Mapping_File_Names (Project, Index)),
-                  Success => Success);
-            end loop;
-         end loop;
-      end if;
-   end Delete_Mapping_Files;
-
    ------------------------------
    -- Delete_Temp_Config_Files --
    ------------------------------
 
    procedure Delete_Temp_Config_Files is
       Success : Boolean;
+      Proj    : Project_List;
       pragma Warnings (Off, Success);
 
    begin
@@ -4006,38 +3959,22 @@ package body Make is
       pragma Assert (not Debug.Debug_Flag_N);
 
       if Main_Project /= No_Project then
-         for Project in Project_Table.First ..
-                        Project_Table.Last (Project_Tree.Projects)
-         loop
-            if
-              Project_Tree.Projects.Table (Project).Config_File_Temp
-            then
-               if Verbose_Mode then
-                  Write_Str ("Deleting temp configuration file """);
-                  Write_Str (Get_Name_String
-                               (Project_Tree.Projects.Table
-                                  (Project).Config_File_Name));
-                  Write_Line ("""");
-               end if;
-
-               Delete_File
-                 (Name    => Get_Name_String
-                               (Project_Tree.Projects.Table
-                                  (Project).Config_File_Name),
-                  Success => Success);
+         Proj := Project_Tree.Projects;
+         while Proj /= null loop
+            if Proj.Project.Config_File_Temp then
+               Delete_Temporary_File
+                 (Project_Tree, Proj.Project.Config_File_Name);
 
                --  Make sure that we don't have a config file for this project,
                --  in case there are several mains. In this case, we will
                --  recreate another config file: we cannot reuse the one that
                --  we just deleted!
 
-               Project_Tree.Projects.Table (Project).
-                 Config_Checked := False;
-               Project_Tree.Projects.Table (Project).
-                 Config_File_Name := No_Path;
-               Project_Tree.Projects.Table (Project).
-                 Config_File_Temp := False;
+               Proj.Project.Config_Checked   := False;
+               Proj.Project.Config_File_Name := No_Path;
+               Proj.Project.Config_File_Temp := False;
             end if;
+            Proj := Proj.Next;
          end loop;
       end if;
    end Delete_Temp_Config_Files;
@@ -4055,10 +3992,12 @@ package body Make is
 
          for J in Args'Range loop
 
-            --  Never display -gnatez
-
-            if Args (J).all /= "-gnatez" then
+            --  Never display -gnatea nor -gnatez
 
+            if Args (J).all /= "-gnatea"
+                 and then
+               Args (J).all /= "-gnatez"
+            then
                --  Do not display the mapping file argument automatically
                --  created when using a project file.
 
@@ -4082,8 +4021,8 @@ package body Make is
                   then
                      Temporary_Config_File := False;
 
-                     --  Do not display the -F=mapping_file switch for
-                     --  gnatbind, if -dn is not specified.
+                     --  Do not display the -F=mapping_file switch for gnatbind
+                     --  if -dn is not specified.
 
                   elsif Debug.Debug_Flag_N
                     or else Args (J)'Length < 4
@@ -4143,29 +4082,6 @@ package body Make is
       Display_Executed_Programs := Display;
    end Display_Commands;
 
-   -------------
-   -- Empty_Q --
-   -------------
-
-   function Empty_Q return Boolean is
-   begin
-      if Debug.Debug_Flag_P then
-         Write_Str ("   Q := [");
-
-         for J in Q_Front .. Q.Last - 1 loop
-            Write_Str (" ");
-            Write_Name (Q.Table (J).File);
-            Write_Eol;
-            Write_Str ("         ");
-         end loop;
-
-         Write_Str ("]");
-         Write_Eol;
-      end if;
-
-      return Q_Front >= Q.Last;
-   end Empty_Q;
-
    --------------------------
    -- Enter_Into_Obsoleted --
    --------------------------
@@ -4197,39 +4113,6 @@ package body Make is
       Obsoleted.Set (F2, True);
    end Enter_Into_Obsoleted;
 
-   --------------------
-   -- Extract_From_Q --
-   --------------------
-
-   procedure Extract_From_Q
-     (Source_File  : out File_Name_Type;
-      Source_Unit  : out Unit_Name_Type;
-      Source_Index : out Int)
-   is
-      File  : constant File_Name_Type := Q.Table (Q_Front).File;
-      Unit  : constant Unit_Name_Type := Q.Table (Q_Front).Unit;
-      Index : constant Int            := Q.Table (Q_Front).Index;
-
-   begin
-      if Debug.Debug_Flag_Q then
-         Write_Str ("   Q := Q - [ ");
-         Write_Name (File);
-
-         if Index /= 0 then
-            Write_Str (", ");
-            Write_Int (Index);
-         end if;
-
-         Write_Str (" ]");
-         Write_Eol;
-      end if;
-
-      Q_Front := Q_Front + 1;
-      Source_File  := File;
-      Source_Unit  := Unit;
-      Source_Index := Index;
-   end Extract_From_Q;
-
    --------------
    -- Gnatmake --
    --------------
@@ -4243,8 +4126,7 @@ package body Make is
       Total_Compilation_Failures : Natural := 0;
 
       Is_Main_Unit : Boolean;
-      --  Set to True by Compile_Sources if the Main_Source_File can be a
-      --  main unit.
+      --  Set True by Compile_Sources if Main_Source_File can be a main unit
 
       Main_ALI_File : File_Name_Type;
       --  The ali file corresponding to Main_Source_File
@@ -4253,8 +4135,8 @@ package body Make is
       --  The file name of an executable
 
       Non_Std_Executable : Boolean := False;
-      --  Non_Std_Executable is set to True when there is a possibility
-      --  that the linker will not choose the correct executable file name.
+      --  Non_Std_Executable is set to True when there is a possibility that
+      --  the linker will not choose the correct executable file name.
 
       Current_Work_Dir : constant String_Access :=
                                     new String'(Get_Current_Dir);
@@ -4264,13 +4146,15 @@ package body Make is
       Current_Main_Index : Int := 0;
       --  If not zero, the index of the current main unit in its source file
 
-      There_Are_Stand_Alone_Libraries : Boolean := False;
+      Stand_Alone_Libraries : Boolean := False;
       --  Set to True when there are Stand-Alone Libraries, so that gnatbind
       --  is invoked with the -F switch to force checking of elaboration flags.
 
       Mapping_Path : Path_Name_Type := No_Path;
       --  The path name of the mapping file
 
+      Project_Node_Tree : Project_Node_Tree_Ref;
+
       Discard : Boolean;
       pragma Warnings (Off, Discard);
 
@@ -4278,10 +4162,6 @@ package body Make is
       --  Check that the main subprograms do exist and that they all
       --  belong to the same project file.
 
-      procedure Create_Binder_Mapping_File
-        (Args : in out Argument_List; Last_Arg : in out Natural);
-      --  Create a binder mapping file and add the necessary switch
-
       -----------------
       -- Check_Mains --
       -----------------
@@ -4293,8 +4173,6 @@ package body Make is
          Proj              : Project_Id := No_Project;
          --  The project of the current main
 
-         Data              : Project_Data;
-
          Real_Path         : String_Access;
 
       begin
@@ -4305,11 +4183,13 @@ package body Make is
          loop
             declare
                Main      : constant String := Mains.Next_Main;
-               --  The name specified on the command line may include
-               --  directory information.
+               --  The name specified on the command line may include directory
+               --  information.
 
                File_Name : constant String := Base_Name (Main);
-               --  The simple file name of the current main main
+               --  The simple file name of the current main
+
+               Lang : Language_Ptr;
 
             begin
                exit when Main = "";
@@ -4319,46 +4199,41 @@ package body Make is
                Proj := Prj.Env.Project_Of
                          (File_Name, Main_Project, Project_Tree);
 
-               --  Fail if the current main is not a source of a
-               --  project.
+               --  Fail if the current main is not a source of a project
 
                if Proj = No_Project then
                   Make_Failed
-                    ("""" & Main &
-                     """ is not a source of any project");
+                    ("""" & Main & """ is not a source of any project");
 
                else
-                  --  If there is directory information, check that
-                  --  the source exists and, if it does, that the path
-                  --  is the actual path of a source of a project.
+                  --  If there is directory information, check that the source
+                  --  exists and, if it does, that the path is the actual path
+                  --  of a source of a project.
 
                   if Main /= File_Name then
-                     Data :=
-                       Project_Tree.Projects.Table (Main_Project);
+                     Lang := Get_Language_From_Name (Main_Project, "ada");
 
                      Real_Path :=
                        Locate_Regular_File
-                         (Main &
-                          Body_Suffix_Of (Project_Tree, "ada", Data.Naming),
+                         (Main & Get_Name_String
+                              (Lang.Config.Naming_Data.Body_Suffix),
                           "");
                      if Real_Path = null then
                         Real_Path :=
                           Locate_Regular_File
-                            (Main &
-                             Spec_Suffix_Of (Project_Tree, "ada", Data.Naming),
+                            (Main & Get_Name_String
+                                 (Lang.Config.Naming_Data.Spec_Suffix),
                              "");
                      end if;
 
                      if Real_Path = null then
-                        Real_Path :=
-                          Locate_Regular_File (Main, "");
+                        Real_Path := Locate_Regular_File (Main, "");
                      end if;
 
                      --  Fail if the file cannot be found
 
                      if Real_Path = null then
-                        Make_Failed
-                          ("file """ & Main & """ does not exist");
+                        Make_Failed ("file """ & Main & """ does not exist");
                      end if;
 
                      declare
@@ -4407,22 +4282,20 @@ package body Make is
 
                      elsif Proj /= Real_Main_Project then
 
-                        --  Fail, as the current main is not a source
-                        --  of the same project as the first main.
+                        --  Fail, as the current main is not a source of the
+                        --  same project as the first main.
 
                         Make_Failed
                           ("""" & Main &
                            """ is not a source of project " &
-                           Get_Name_String
-                             (Project_Tree.Projects.Table
-                                (Real_Main_Project).Name));
+                           Get_Name_String (Real_Main_Project.Name));
                      end if;
                   end if;
                end if;
 
-               --  If -u and -U are not used, we may have mains that
-               --  are sources of a project that is not the one
-               --  specified with switch -P.
+               --  If -u and -U are not used, we may have mains that are
+               --  sources of a project that is not the one specified with
+               --  switch -P.
 
                if not Unique_Compile then
                   Main_Project := Real_Main_Project;
@@ -4431,239 +4304,37 @@ package body Make is
          end loop;
       end Check_Mains;
 
-      --------------------------------
-      -- Create_Binder_Mapping_File --
-      --------------------------------
+   --  Start of processing for Gnatmake
 
-      procedure Create_Binder_Mapping_File
-        (Args : in out Argument_List; Last_Arg : in out Natural)
-      is
-         Mapping_FD : File_Descriptor := Invalid_FD;
-         --  A File Descriptor for an eventual mapping file
+   --  This body is very long, should be broken down???
 
-         ALI_Unit : Unit_Name_Type := No_Unit_Name;
-         --  The unit name of an ALI file
+   begin
+      Install_Int_Handler (Sigint_Intercepted'Access);
 
-         ALI_Name : File_Name_Type := No_File;
-         --  The file name of the ALI file
+      Do_Compile_Step := True;
+      Do_Bind_Step    := True;
+      Do_Link_Step    := True;
 
-         ALI_Project : Project_Id := No_Project;
-         --  The project of the ALI file
+      Obsoleted.Reset;
 
-         Bytes : Integer;
-         OK    : Boolean := True;
+      Make.Initialize (Project_Node_Tree);
 
-         Status : Boolean;
-         --  For call to Close
+      Bind_Shared := No_Shared_Switch'Access;
+      Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
 
-      begin
-         Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
-         Record_Temp_File (Mapping_Path);
+      Failed_Links.Set_Last (0);
+      Successful_Links.Set_Last (0);
 
-         if Mapping_FD /= Invalid_FD then
+      --  Special case when switch -B was specified
 
-            --  Traverse all units
+      if Build_Bind_And_Link_Full_Project then
 
-            for J in Unit_Table.First ..
-                     Unit_Table.Last (Project_Tree.Units)
-            loop
-               declare
-                  Unit : constant Unit_Data := Project_Tree.Units.Table (J);
-               begin
-                  if Unit.Name /= No_Name then
+         --  When switch -B is specified, there must be a project file
 
-                     --  If there is a body, put it in the mapping
+         if Main_Project = No_Project then
+            Make_Failed ("-B cannot be used without a project file");
 
-                     if Unit.File_Names (Body_Part).Name /= No_File
-                       and then Unit.File_Names (Body_Part).Project /=
-                                                            No_Project
-                     then
-                        Get_Name_String (Unit.Name);
-                        Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "%b";
-                        Name_Len := Name_Len + 2;
-                        ALI_Unit := Name_Find;
-                        ALI_Name :=
-                          Lib_File_Name
-                            (Unit.File_Names (Body_Part).Display_Name);
-                        ALI_Project :=
-                          Unit.File_Names (Body_Part).Project;
-
-                        --  Otherwise, if there is a spec, put it
-                        --  in the mapping.
-
-                     elsif Unit.File_Names (Specification).Name /= No_File
-                       and then Unit.File_Names (Specification).Project /=
-                                                                No_Project
-                     then
-                        Get_Name_String (Unit.Name);
-                        Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "%s";
-                        Name_Len := Name_Len + 2;
-                        ALI_Unit := Name_Find;
-                        ALI_Name :=
-                          Lib_File_Name
-                            (Unit.File_Names (Specification).Display_Name);
-                        ALI_Project :=
-                          Unit.File_Names (Specification).Project;
-
-                     else
-                        ALI_Name := No_File;
-                     end if;
-
-                     --  If we have something to put in the mapping
-                     --  then we do it now. However, if the project
-                     --  is extended, we don't put anything in the
-                     --  mapping file, because we do not know where
-                     --  the ALI file is: it might be in the ext-
-                     --  ended project obj dir as well as in the
-                     --  extending project obj dir.
-
-                     if ALI_Name /= No_File
-                       and then
-                         Project_Tree.Projects.Table
-                           (ALI_Project).Extended_By = No_Project
-                         and then
-                           Project_Tree.Projects.Table
-                             (ALI_Project).Extends = No_Project
-                     then
-                        --  First check if the ALI file exists. If it does not,
-                        --  do not put the unit in the mapping file.
-
-                        declare
-                           ALI : constant String :=
-                                   Get_Name_String (ALI_Name);
-                           PD  : Project_Data renames
-                             Project_Tree.Projects.Table (ALI_Project);
-
-                        begin
-                           --  For library projects, use the library directory,
-                           --  for other projects, use the object directory.
-
-                           if PD.Library then
-                              Get_Name_String (PD.Library_Dir.Name);
-                           else
-                              Get_Name_String (PD.Object_Directory.Name);
-                           end if;
-
-                           if Name_Buffer (Name_Len) /=
-                             Directory_Separator
-                           then
-                              Name_Len := Name_Len + 1;
-                              Name_Buffer (Name_Len) :=
-                                Directory_Separator;
-                           end if;
-
-                           Name_Buffer
-                             (Name_Len + 1 ..
-                                Name_Len + ALI'Length) := ALI;
-                           Name_Len :=
-                             Name_Len + ALI'Length + 1;
-                           Name_Buffer (Name_Len) := ASCII.LF;
-
-                           declare
-                              ALI_Path_Name : constant String :=
-                                                Name_Buffer (1 .. Name_Len);
-
-                           begin
-                              if Is_Regular_File
-                                (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
-                              then
-
-                                 --  First line is the unit name
-
-                                 Get_Name_String (ALI_Unit);
-                                 Name_Len := Name_Len + 1;
-                                 Name_Buffer (Name_Len) := ASCII.LF;
-                                 Bytes :=
-                                   Write
-                                     (Mapping_FD,
-                                      Name_Buffer (1)'Address,
-                                      Name_Len);
-                                 OK := Bytes = Name_Len;
-
-                                 exit when not OK;
-
-                                 --  Second line it the ALI file name
-
-                                 Get_Name_String (ALI_Name);
-                                 Name_Len := Name_Len + 1;
-                                 Name_Buffer (Name_Len) := ASCII.LF;
-                                 Bytes :=
-                                   Write
-                                     (Mapping_FD,
-                                      Name_Buffer (1)'Address,
-                                      Name_Len);
-                                 OK := Bytes = Name_Len;
-
-                                 exit when not OK;
-
-                                 --  Third line it the ALI path name
-
-                                 Bytes :=
-                                   Write
-                                     (Mapping_FD,
-                                      ALI_Path_Name (1)'Address,
-                                      ALI_Path_Name'Length);
-                                 OK := Bytes = ALI_Path_Name'Length;
-
-                                 --  If OK is False, it means we were unable
-                                 --  to write a line. No point in continuing
-                                 --  with the other units.
-
-                                 exit when not OK;
-                              end if;
-                           end;
-                        end;
-                     end if;
-                  end if;
-               end;
-            end loop;
-
-            Close (Mapping_FD, Status);
-
-            OK := OK and Status;
-
-            --  If the creation of the mapping file was successful,
-            --  we add the switch to the arguments of gnatbind.
-
-            if OK then
-               Last_Arg := Last_Arg + 1;
-               Args (Last_Arg) :=
-                 new String'("-F=" & Get_Name_String (Mapping_Path));
-            end if;
-         end if;
-      end Create_Binder_Mapping_File;
-
-   --  Start of processing for Gnatmake
-
-   --  This body is very long, should be broken down ???
-
-   begin
-      Install_Int_Handler (Sigint_Intercepted'Access);
-
-      Do_Compile_Step := True;
-      Do_Bind_Step    := True;
-      Do_Link_Step    := True;
-
-      Obsoleted.Reset;
-
-      Make.Initialize;
-
-      Bind_Shared := No_Shared_Switch'Access;
-      Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
-
-      Failed_Links.Set_Last (0);
-      Successful_Links.Set_Last (0);
-
-      --  Special case when switch -B was specified
-
-      if Build_Bind_And_Link_Full_Project then
-
-         --  When switch -B is specified, there must be a project file
-
-         if Main_Project = No_Project then
-            Make_Failed ("-B cannot be used without a project file");
-
-         --  No main program may be specified on the command line
+         --  No main program may be specified on the command line
 
          elsif Osint.Number_Of_Files /= 0 then
             Make_Failed ("-B cannot be used with a main specified on " &
@@ -4671,7 +4342,7 @@ package body Make is
 
          --  And the project file cannot be a library project file
 
-         elsif Project_Tree.Projects.Table (Main_Project).Library then
+         elsif Main_Project.Library then
             Make_Failed ("-B cannot be used for a library project file");
 
          else
@@ -4688,17 +4359,17 @@ package body Make is
             end if;
 
             --  Specify -n for gnatbind and add the ALI files of all the
-            --  sources, except the one which is a fake main subprogram:
-            --  this is the one for the binder generated file and it will be
-            --  transmitted to gnatlink. These sources are those that are
-            --  in the queue.
+            --  sources, except the one which is a fake main subprogram: this
+            --  is the one for the binder generated file and it will be
+            --  transmitted to gnatlink. These sources are those that are in
+            --  the queue.
 
             Add_Switch ("-n", Binder, And_Save => True);
 
-            for J in Q.First .. Q.Last - 1 loop
+            for J in 1 .. Queue.Size loop
                Add_Switch
                  (Get_Name_String
-                    (Lib_File_Name (Q.Table (J).File)),
+                    (Lib_File_Name (Queue.Element (J))),
                   Binder, And_Save => True);
             end loop;
          end if;
@@ -4708,11 +4379,11 @@ package body Make is
 
       elsif Main_Project /= No_Project then
 
-         --  If the main project file is a library project file, main(s)
-         --  cannot be specified on the command line.
+         --  If the main project file is a library project file, main(s) cannot
+         --  be specified on the command line.
 
          if Osint.Number_Of_Files /= 0 then
-            if Project_Tree.Projects.Table (Main_Project).Library
+            if Main_Project.Library
               and then not Unique_Compile
               and then ((not Make_Steps) or else Bind_Only or else Link_Only)
             then
@@ -4727,10 +4398,10 @@ package body Make is
                Check_Mains;
             end if;
 
-         --  If no mains have been specified on the command line,
-         --  and we are using a project file, we either find the main(s)
-         --  in the attribute Main of the main project, or we put all
-         --  the sources of the project file as mains.
+         --  If no mains have been specified on the command line, and we are
+         --  using a project file, we either find the main(s) in attribute Main
+         --  of the main project, or we put all the sources of the project file
+         --  as mains.
 
          else
             if Main_Index /= 0 then
@@ -4739,21 +4410,19 @@ package body Make is
             end if;
 
             declare
-               Value : String_List_Id :=
-                         Project_Tree.Projects.Table (Main_Project).Mains;
+               Value : String_List_Id := Main_Project.Mains;
 
             begin
-               --  The attribute Main is an empty list or not specified,
-               --  or else gnatmake was invoked with the switch "-u".
+               --  The attribute Main is an empty list or not specified, or
+               --  else gnatmake was invoked with the switch "-u".
 
                if Value = Prj.Nil_String or else Unique_Compile then
 
                   if (not Make_Steps) or else Compile_Only
-                    or else not Project_Tree.Projects.Table
-                                  (Main_Project).Library
+                    or else not Main_Project.Library
                   then
-                     --  First make sure that the binder and the linker
-                     --  will not be invoked.
+                     --  First make sure that the binder and the linker will
+                     --  not be invoked.
 
                      Do_Bind_Step := False;
                      Do_Link_Step := False;
@@ -4780,22 +4449,18 @@ package body Make is
                   end if;
 
                else
-                  --  The attribute Main is not an empty list.
-                  --  Put all the main subprograms in the list as if there
-                  --  were specified on the command line. However, if attribute
-                  --  Languages includes a language other than Ada, only
-                  --  include the Ada mains; if there is no Ada main, compile
-                  --  all the sources of the project.
+                  --  The attribute Main is not an empty list. Put all the main
+                  --  subprograms in the list as if they were specified on the
+                  --  command line. However, if attribute Languages includes a
+                  --  language other than Ada, only include the Ada mains; if
+                  --  there is no Ada main, compile all sources of the project.
 
                   declare
-                     Data : constant Project_Data :=
-                           Project_Tree.Projects.Table (Main_Project);
-
                      Languages : constant Variable_Value :=
                                    Prj.Util.Value_Of
-                                    (Name_Languages,
-                                     Data.Decl.Attributes,
-                                     Project_Tree);
+                                     (Name_Languages,
+                                      Main_Project.Decl.Attributes,
+                                      Project_Tree);
 
                      Current : String_List_Id;
                      Element : String_Element;
@@ -4809,7 +4474,6 @@ package body Make is
 
                      if not Languages.Default then
                         Current := Languages.Values;
-
                         Look_For_Foreign :
                         while Current /= Nil_String loop
                            Element := Project_Tree.String_Elements.
@@ -4830,30 +4494,41 @@ package body Make is
                      --  language, all the Ada mains.
 
                      while Value /= Prj.Nil_String loop
-                        Get_Name_String
-                          (Project_Tree.String_Elements.Table
-                             (Value).Value);
-
                         --  To know if a main is an Ada main, get its project.
                         --  It should be the project specified on the command
                         --  line.
 
-                        if (not Foreign_Language) or else
-                            Prj.Env.Project_Of
-                              (Name_Buffer (1 .. Name_Len),
-                               Main_Project,
-                               Project_Tree) =
-                             Main_Project
-                        then
-                           At_Least_One_Main := True;
-                           Osint.Add_File
-                             (Get_Name_String
-                                (Project_Tree.String_Elements.Table
-                                   (Value).Value),
-                              Index =>
-                                Project_Tree.String_Elements.Table
-                                  (Value).Index);
-                        end if;
+                        Get_Name_String
+                          (Project_Tree.String_Elements.Table (Value).Value);
+
+                        declare
+                           Main_Name : constant String :=
+                              Get_Name_String
+                               (Project_Tree.String_Elements.Table
+                                    (Value).Value);
+                           Proj : constant Project_Id :=
+                             Prj.Env.Project_Of
+                              (Main_Name, Main_Project, Project_Tree);
+                        begin
+
+                           if Proj = Main_Project then
+
+                              At_Least_One_Main := True;
+                              Osint.Add_File
+                                (Get_Name_String
+                                   (Project_Tree.String_Elements.Table
+                                      (Value).Value),
+                                 Index =>
+                                   Project_Tree.String_Elements.Table
+                                     (Value).Index);
+
+                           elsif not Foreign_Language then
+                              Make_Failed
+                                ("""" & Main_Name &
+                                 """ is not a source of project " &
+                                 Get_Name_String (Main_Project.Display_Name));
+                           end if;
+                        end;
 
                         Value := Project_Tree.String_Elements.Table
                                    (Value).Next;
@@ -4865,7 +4540,7 @@ package body Make is
 
                      if not At_Least_One_Main then
                         Make_Failed
-                          ("no Ada mains; use -B to build foreign main");
+                          ("no Ada mains, use -B to build foreign main");
                      end if;
                   end;
 
@@ -4876,24 +4551,15 @@ package body Make is
 
       if Verbose_Mode then
          Write_Eol;
-         Display_Version ("GNATMAKE ", "1995");
-      end if;
-
-      if Main_Project /= No_Project
-        and then Project_Tree.Projects.Table
-                   (Main_Project).Externally_Built
-      then
-         Make_Failed
-           ("nothing to do for a main project that is externally built");
+         Display_Version ("GNATMAKE", "1995");
       end if;
 
       if Osint.Number_Of_Files = 0 then
          if Main_Project /= No_Project
-           and then Project_Tree.Projects.Table (Main_Project).Library
+           and then Main_Project.Library
          then
             if Do_Bind_Step
-              and then not Project_Tree.Projects.Table
-                             (Main_Project).Standalone_Library
+              and then not Main_Project.Standalone_Library
             then
                Make_Failed ("only stand-alone libraries may be bound");
             end if;
@@ -4929,6 +4595,11 @@ package body Make is
             Exit_Program (E_Success);
 
          else
+            --  Call Get_Target_Parameters to ensure that VM_Target and
+            --  AAMP_On_Target get set before calling Usage.
+
+            Targparm.Get_Target_Parameters;
+
             --  Output usage information if no files to compile
 
             Usage;
@@ -4987,9 +4658,7 @@ package body Make is
 
       if Main_Project /= No_Project then
 
-         if Project_Tree.Projects.Table
-              (Main_Project).Object_Directory /= No_Path_Information
-         then
+         if Main_Project.Object_Directory /= No_Path_Information then
             --  Change current directory to object directory of main project
 
             Project_Of_Current_Object_Directory := No_Project;
@@ -5015,8 +4684,7 @@ package body Make is
                                            not Unique_Compile);
 
             The_Packages : constant Package_Id :=
-                             Project_Tree.Projects.Table
-                               (Main_Project).Decl.Packages;
+                             Main_Project.Decl.Packages;
 
             Builder_Package : constant Prj.Package_Id :=
                                 Prj.Util.Value_Of
@@ -5036,16 +4704,22 @@ package body Make is
                                   In_Packages => The_Packages,
                                   In_Tree     => Project_Tree);
 
+            Default_Switches_Array : Array_Id;
+
+            Global_Compilation_Array    : Array_Element_Id;
+            Global_Compilation_Elem     : Array_Element;
+            Global_Compilation_Switches : Variable_Value;
+
          begin
             --  We fail if we cannot find the main source file
 
             if Main_Unit_File_Name = "" then
-               Make_Failed ('"' & Main_Source_File_Name,
-                            """ is not a unit of project ",
-                            Project_File_Name.all & ".");
+               Make_Failed ('"' & Main_Source_File_Name
+                            & """ is not a unit of project "
+                            Project_File_Name.all & ".");
             else
-               --  Remove any directory information from the main
-               --  source file name.
+               --  Remove any directory information from the main source file
+               --  file name.
 
                declare
                   Pos : Natural := Main_Unit_File_Name'Last;
@@ -5081,6 +4755,37 @@ package body Make is
 
             if Builder_Package /= No_Package then
 
+               Global_Compilation_Array := Prj.Util.Value_Of
+                 (Name      => Name_Global_Compilation_Switches,
+                  In_Arrays => Project_Tree.Packages.Table
+                    (Builder_Package).Decl.Arrays,
+                  In_Tree   => Project_Tree);
+
+               Default_Switches_Array :=
+                 Project_Tree.Packages.Table
+                   (Builder_Package).Decl.Arrays;
+
+               while Default_Switches_Array /= No_Array and then
+               Project_Tree.Arrays.Table (Default_Switches_Array).Name /=
+                 Name_Default_Switches
+               loop
+                  Default_Switches_Array :=
+                    Project_Tree.Arrays.Table (Default_Switches_Array).Next;
+               end loop;
+
+               if Global_Compilation_Array /= No_Array_Element and then
+                  Default_Switches_Array /= No_Array
+               then
+                  Errutil.Error_Msg
+                    ("Default_Switches forbidden in presence of " &
+                     "Global_Compilation_Switches. Use Switches instead.",
+                     Project_Tree.Arrays.Table
+                       (Default_Switches_Array).Location);
+                  Errutil.Finalize;
+                  Make_Failed
+                    ("*** illegal combination of Builder attributes");
+               end if;
+
                --  If there is only one main, we attempt to get the gnatmake
                --  switches for this main (if any). If there are no specific
                --  switch for this particular main, get the general gnatmake
@@ -5094,10 +4799,13 @@ package body Make is
                   end if;
 
                   Add_Switches
-                    (File_Name   => Main_Unit_File_Name,
-                     Index       => Main_Index,
-                     The_Package => Builder_Package,
-                     Program     => None);
+                    (Project_Node_Tree                => Project_Node_Tree,
+                     File_Name                        => Main_Unit_File_Name,
+                     Index                            => Main_Index,
+                     The_Package                      => Builder_Package,
+                     Program                          => None,
+                     Unknown_Switches_To_The_Compiler =>
+                       Global_Compilation_Array = No_Array_Element);
 
                else
                   --  If there are several mains, we always get the general
@@ -5108,49 +4816,130 @@ package body Make is
 
                   declare
                      Defaults : constant Variable_Value :=
-                       Prj.Util.Value_Of
-                         (Name                    => Name_Ada,
-                          Index                   => 0,
-                          Attribute_Or_Array_Name => Name_Default_Switches,
-                          In_Package              => Builder_Package,
-                          In_Tree                 => Project_Tree);
+                                  Prj.Util.Value_Of
+                                    (Name                    => Name_Ada,
+                                     Index                   => 0,
+                                     Attribute_Or_Array_Name =>
+                                       Name_Default_Switches,
+                                     In_Package              =>
+                                       Builder_Package,
+                                     In_Tree                 => Project_Tree);
 
                      Switches : constant Array_Element_Id :=
-                          Prj.Util.Value_Of
-                             (Name      => Name_Switches,
-                              In_Arrays =>
-                                Project_Tree.Packages.Table
-                                  (Builder_Package).Decl.Arrays,
-                              In_Tree   => Project_Tree);
+                                  Prj.Util.Value_Of
+                                    (Name      => Name_Switches,
+                                     In_Arrays =>
+                                       Project_Tree.Packages.Table
+                                         (Builder_Package).Decl.Arrays,
+                                     In_Tree   => Project_Tree);
+
+                     Other_Switches : constant Variable_Value :=
+                                        Prj.Util.Value_Of
+                                          (Name        => All_Other_Names,
+                                           Index       => 0,
+                                           Attribute_Or_Array_Name
+                                                       => Name_Switches,
+                                           In_Package  => Builder_Package,
+                                           In_Tree     => Project_Tree);
 
                   begin
-                     if Defaults /= Nil_Variable_Value then
-                        if (not Quiet_Output)
+                     if Other_Switches /= Nil_Variable_Value then
+                        if not Quiet_Output
                           and then Switches /= No_Array_Element
+                          and then Project_Tree.Array_Elements.Table
+                                     (Switches).Next /= No_Array_Element
                         then
                            Write_Line
-                             ("Warning: using Builder'Default_Switches" &
-                              "(""Ada""), as there are several mains");
+                             ("Warning: using Builder'Switches(others), "
+                              & "as there are several mains");
                         end if;
 
-                        --  As there is never a source with name " ", we are
-                        --  guaranteed to always get the general switches.
+                        Add_Switches
+                          (Project_Node_Tree              => Project_Node_Tree,
+                           File_Name                        => " ",
+                           Index                            => 0,
+                           The_Package                      => Builder_Package,
+                           Program                          => None,
+                           Unknown_Switches_To_The_Compiler => False);
+
+                     elsif Defaults /= Nil_Variable_Value then
+                        if not Quiet_Output
+                          and then Switches /= No_Array_Element
+                        then
+                           Write_Line
+                             ("Warning: using Builder'Default_Switches"
+                              & "(""Ada""), as there are several mains");
+                        end if;
 
                         Add_Switches
-                          (File_Name   => " ",
+                          (Project_Node_Tree => Project_Node_Tree,
+                           File_Name   => " ",
                            Index       => 0,
                            The_Package => Builder_Package,
                            Program     => None);
 
-                     elsif (not Quiet_Output)
+                     elsif not Quiet_Output
                        and then Switches /= No_Array_Element
                      then
                         Write_Line
-                          ("Warning: using no switches from package Builder," &
-                           " as there are several mains");
+                          ("Warning: using no switches from package "
+                           & "Builder, as there are several mains");
                      end if;
                   end;
                end if;
+
+               --  Take into account attribute Global_Compilation_Switches
+               --  ("Ada").
+
+               declare
+                  Index : Name_Id;
+                  List  : String_List_Id;
+                  Elem  : String_Element;
+
+               begin
+                  while Global_Compilation_Array /= No_Array_Element loop
+                     Global_Compilation_Elem :=
+                       Project_Tree.Array_Elements.Table
+                         (Global_Compilation_Array);
+
+                     Get_Name_String (Global_Compilation_Elem.Index);
+                     To_Lower (Name_Buffer (1 .. Name_Len));
+                     Index := Name_Find;
+
+                     if Index = Name_Ada then
+                        Global_Compilation_Switches :=
+                          Global_Compilation_Elem.Value;
+
+                        if Global_Compilation_Switches /= Nil_Variable_Value
+                          and then not Global_Compilation_Switches.Default
+                        then
+                           --  We have found attribute
+                           --  Global_Compilation_Switches ("Ada"): put the
+                           --  switches in the appropriate table.
+
+                           List := Global_Compilation_Switches.Values;
+
+                           while List /= Nil_String loop
+                              Elem :=
+                                Project_Tree.String_Elements.Table (List);
+
+                              if Elem.Value /= No_Name then
+                                 Add_Switch
+                                   (Get_Name_String (Elem.Value),
+                                    Compiler,
+                                    And_Save => False);
+                              end if;
+
+                              List := Elem.Next;
+                           end loop;
+
+                           exit;
+                        end if;
+                     end if;
+
+                     Global_Compilation_Array := Global_Compilation_Elem.Next;
+                  end loop;
+               end;
             end if;
 
             Osint.Add_Default_Search_Dirs;
@@ -5167,7 +4956,7 @@ package body Make is
 
             --  Add binder switches from the project file for the first main
 
-            if Do_Bind_Step and Binder_Package /= No_Package then
+            if Do_Bind_Step and then Binder_Package /= No_Package then
                if Verbose_Mode then
                   Write_Str ("Adding binder switches for """);
                   Write_Str (Main_Unit_File_Name);
@@ -5175,15 +4964,16 @@ package body Make is
                end if;
 
                Add_Switches
-                 (File_Name   => Main_Unit_File_Name,
-                  Index       => Main_Index,
-                  The_Package => Binder_Package,
-                  Program     => Binder);
+                 (Project_Node_Tree => Project_Node_Tree,
+                  File_Name         => Main_Unit_File_Name,
+                  Index             => Main_Index,
+                  The_Package       => Binder_Package,
+                  Program           => Binder);
             end if;
 
             --  Add linker switches from the project file for the first main
 
-            if Do_Link_Step and Linker_Package /= No_Package then
+            if Do_Link_Step and then Linker_Package /= No_Package then
                if Verbose_Mode then
                   Write_Str ("Adding linker switches for""");
                   Write_Str (Main_Unit_File_Name);
@@ -5191,17 +4981,37 @@ package body Make is
                end if;
 
                Add_Switches
-                 (File_Name   => Main_Unit_File_Name,
-                  Index       => Main_Index,
-                  The_Package => Linker_Package,
-                  Program     => Linker);
+                 (Project_Node_Tree => Project_Node_Tree,
+                  File_Name         => Main_Unit_File_Name,
+                  Index             => Main_Index,
+                  The_Package       => Linker_Package,
+                  Program           => Linker);
             end if;
          end;
       end if;
 
+      --  The combination of -f -u and one or several mains on the command line
+      --  implies -a.
+
+      if Force_Compilations
+        and then Unique_Compile
+        and then not Unique_Compile_All_Projects
+        and then Main_On_Command_Line
+      then
+         Must_Compile := True;
+      end if;
+
+      if Main_Project /= No_Project
+        and then not Must_Compile
+        and then Main_Project.Externally_Built
+      then
+         Make_Failed
+           ("nothing to do for a main project that is externally built");
+      end if;
+
       --  Get the target parameters, which are only needed for a couple of
-      --  cases in gnatmake. Protect against an exception, such as the case
-      --  of system.ads missing from the library, and fail gracefully.
+      --  cases in gnatmake. Protect against an exception, such as the case of
+      --  system.ads missing from the library, and fail gracefully.
 
       begin
          Targparm.Get_Target_Parameters;
@@ -5223,10 +5033,7 @@ package body Make is
                --  JVM machine since ".class" files are generated instead.
 
                Check_Object_Consistency := False;
-
-               Gcc := new String'("jgnat");
-               Gnatbind := new String'("jgnatbind");
-               Gnatlink := new String'("jgnatlink");
+               Gcc := new String'("jvm-gnatcompile");
 
             when Targparm.CLI_Target =>
                Gcc := new String'("dotnet-gnatcompile");
@@ -5249,55 +5056,56 @@ package body Make is
          if not Unique_Compile
            and then MLib.Tgt.Support_For_Libraries /= Prj.None
          then
-            for Proj in Project_Table.First ..
-                        Project_Table.Last (Project_Tree.Projects)
-            loop
-               if Project_Tree.Projects.Table (Proj).Library then
-                  Project_Tree.Projects.Table
-                    (Proj).Need_To_Build_Lib :=
-                      (not MLib.Tgt.Library_Exists_For (Proj, Project_Tree))
-                    and then (not Project_Tree.Projects.Table
-                                (Proj).Externally_Built);
-
-                  if Project_Tree.Projects.Table (Proj).Need_To_Build_Lib then
+            declare
+               Proj : Project_List;
 
-                     --  If there is no object directory, then it will be
-                     --  impossible to build the library. So fail immediately.
+            begin
+               Proj := Project_Tree.Projects;
+               while Proj /= null loop
+                  if Proj.Project.Library then
+                     Proj.Project.Need_To_Build_Lib :=
+                       not MLib.Tgt.Library_Exists_For
+                         (Proj.Project, Project_Tree)
+                       and then not Proj.Project.Externally_Built;
+
+                     if Proj.Project.Need_To_Build_Lib then
+
+                        --  If there is no object directory, then it will be
+                        --  impossible to build the library. So fail
+                        --  immediately.
+
+                        if
+                          Proj.Project.Object_Directory = No_Path_Information
+                        then
+                           Make_Failed
+                             ("no object files to build library for project """
+                              & Get_Name_String (Proj.Project.Name)
+                              & """");
+                           Proj.Project.Need_To_Build_Lib := False;
 
-                     if Project_Tree.Projects.Table (Proj).Object_Directory =
-                                                        No_Path_Information
-                     then
-                        Make_Failed
-                          ("no object files to build library for project """,
-                           Get_Name_String
-                             (Project_Tree.Projects.Table (Proj).Name),
-                           """");
-                        Project_Tree.Projects.Table
-                          (Proj).Need_To_Build_Lib := False;
+                        else
+                           if Verbose_Mode then
+                              Write_Str
+                                ("Library file does not exist for project """);
+                              Write_Str (Get_Name_String (Proj.Project.Name));
+                              Write_Line ("""");
+                           end if;
 
-                     else
-                        if Verbose_Mode then
-                           Write_Str
-                             ("Library file does not exist for project """);
-                           Write_Str
-                             (Get_Name_String
-                                (Project_Tree.Projects.Table
-                                   (Proj).Name));
-                           Write_Line ("""");
+                           Insert_Project_Sources
+                             (The_Project  => Proj.Project,
+                              All_Projects => False,
+                              Into_Q       => True);
                         end if;
-
-                        Insert_Project_Sources
-                          (The_Project  => Proj,
-                           All_Projects => False,
-                           Into_Q       => True);
                      end if;
                   end if;
-               end if;
-            end loop;
+
+                  Proj := Proj.Next;
+               end loop;
+            end;
          end if;
 
-         --  If a relative path output file has been specified, we add
-         --  the exec directory.
+         --  If a relative path output file has been specified, we add the
+         --  exec directory.
 
          for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
             if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
@@ -5308,18 +5116,15 @@ package body Make is
                begin
                   if not Is_Absolute_Path (Exec_File_Name) then
                      Get_Name_String
-                       (Project_Tree.Projects.Table
-                          (Main_Project).Exec_Directory.Name);
+                       (Main_Project.Exec_Directory.Display_Name);
 
-                     if Name_Buffer (Name_Len) /= Directory_Separator then
-                        Name_Len := Name_Len + 1;
-                        Name_Buffer (Name_Len) := Directory_Separator;
+                     if not
+                       Is_Directory_Separator (Name_Buffer (Name_Len))
+                     then
+                        Add_Char_To_Name_Buffer (Directory_Separator);
                      end if;
 
-                     Name_Buffer (Name_Len + 1 ..
-                                    Name_Len + Exec_File_Name'Length) :=
-                       Exec_File_Name;
-                     Name_Len := Name_Len + Exec_File_Name'Length;
+                     Add_Str_To_Name_Buffer (Exec_File_Name);
                      Saved_Linker_Switches.Table (J + 1) :=
                        new String'(Name_Buffer (1 .. Name_Len));
                   end if;
@@ -5335,10 +5140,8 @@ package body Make is
          --  project file.
 
          declare
-            Dir_Path : constant String_Access :=
-                         new String'(Get_Name_String
-                           (Project_Tree.Projects.Table
-                              (Main_Project).Directory.Name));
+            Dir_Path : constant String :=
+                         Get_Name_String (Main_Project.Directory.Display_Name);
          begin
             for J in 1 .. Binder_Switches.Last loop
                Test_If_Relative_Path
@@ -5349,7 +5152,7 @@ package body Make is
             for J in 1 .. Saved_Binder_Switches.Last loop
                Test_If_Relative_Path
                  (Saved_Binder_Switches.Table (J),
-                  Parent => Current_Work_Dir, Including_L_Switch => False);
+                  Parent => Current_Work_Dir.all, Including_L_Switch => False);
             end loop;
 
             for J in 1 .. Linker_Switches.Last loop
@@ -5359,20 +5162,21 @@ package body Make is
 
             for J in 1 .. Saved_Linker_Switches.Last loop
                Test_If_Relative_Path
-                 (Saved_Linker_Switches.Table (J), Parent => Current_Work_Dir);
+                 (Saved_Linker_Switches.Table (J),
+                  Parent => Current_Work_Dir.all);
             end loop;
 
             for J in 1 .. Gcc_Switches.Last loop
                Test_If_Relative_Path
                  (Gcc_Switches.Table (J),
-                  Parent => Dir_Path,
+                  Parent               => Dir_Path,
                   Including_Non_Switch => False);
             end loop;
 
             for J in 1 .. Saved_Gcc_Switches.Last loop
                Test_If_Relative_Path
                  (Saved_Gcc_Switches.Table (J),
-                  Parent => Current_Work_Dir,
+                  Parent               => Current_Work_Dir.all,
                   Including_Non_Switch => False);
             end loop;
          end;
@@ -5403,9 +5207,7 @@ package body Make is
       if Main_Project = No_Project then
          for J in 1 .. Saved_Gcc_Switches.Last loop
             Add_Switch
-              (Saved_Gcc_Switches.Table (J),
-               Compiler,
-              And_Save => False);
+              (Saved_Gcc_Switches.Table (J), Compiler, And_Save => False);
          end loop;
 
       else
@@ -5422,13 +5224,12 @@ package body Make is
 
          --  We never use gnat.adc when a project file is used
 
-         The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) :=
-           No_gnat_adc;
+         The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) := No_gnat_adc;
       end if;
 
-      --  If there was a --GCC, --GNATBIND or --GNATLINK switch on
-      --  the command line, then we have to use it, even if there was
-      --  another switch in the project file.
+      --  If there was a --GCC, --GNATBIND or --GNATLINK switch on the command
+      --  line, then we have to use it, even if there was another switch in
+      --  the project file.
 
       if Saved_Gcc /= null then
          Gcc := Saved_Gcc;
@@ -5454,32 +5255,45 @@ package body Make is
          Saved_Maximum_Processes := Maximum_Processes;
       end if;
 
-      --  Allocate as many temporary mapping file names as the maximum
-      --  number of compilation processed, for each possible project.
-
-      The_Mapping_File_Names :=
-        new Temp_Path_Names
-          (No_Project .. Project_Table.Last (Project_Tree.Projects),
-           1 .. Saved_Maximum_Processes);
-      Last_Mapping_File_Names :=
-        new Indices'
-          (No_Project .. Project_Table.Last (Project_Tree.Projects)
-            => 0);
-
-      The_Free_Mapping_File_Indices :=
-        new Free_File_Indices
-          (No_Project .. Project_Table.Last (Project_Tree.Projects),
-           1 .. Saved_Maximum_Processes);
-      Last_Free_Indices :=
-        new Indices'(No_Project .. Project_Table.Last
-                                     (Project_Tree.Projects) => 0);
+      --  Allocate as many temporary mapping file names as the maximum number
+      --  of compilations processed, for each possible project.
+
+      declare
+         Data : Project_Compilation_Access;
+         Proj : Project_List := Project_Tree.Projects;
+      begin
+         while Proj /= null loop
+            Data := new Project_Compilation_Data'
+              (Mapping_File_Names        => new Temp_Path_Names
+                                              (1 .. Saved_Maximum_Processes),
+               Last_Mapping_File_Names   => 0,
+               Free_Mapping_File_Indices => new Free_File_Indices
+                                              (1 .. Saved_Maximum_Processes),
+               Last_Free_Indices         => 0);
+
+            Project_Compilation_Htable.Set
+              (Project_Compilation, Proj.Project, Data);
+            Proj := Proj.Next;
+         end loop;
+
+         Data := new Project_Compilation_Data'
+           (Mapping_File_Names        => new Temp_Path_Names
+                                           (1 .. Saved_Maximum_Processes),
+            Last_Mapping_File_Names   => 0,
+            Free_Mapping_File_Indices => new Free_File_Indices
+                                           (1 .. Saved_Maximum_Processes),
+            Last_Free_Indices         => 0);
+
+         Project_Compilation_Htable.Set
+           (Project_Compilation, No_Project, Data);
+      end;
 
       Bad_Compilation.Init;
 
-      --  If project files are used, create the mapping of all the sources,
-      --  so that the correct paths will be found. Otherwise, if there is
-      --  a file which is not a source with the same name in a source directory
-      --  this file may be incorrectly found.
+      --  If project files are used, create the mapping of all the sources, so
+      --  that the correct paths will be found. Otherwise, if there is a file
+      --  which is not a source with the same name in a source directory this
+      --  file may be incorrectly found.
 
       if Main_Project /= No_Project then
          Prj.Env.Create_Mapping (Project_Tree);
@@ -5503,37 +5317,32 @@ package body Make is
          --  Look inside the linker switches to see if the name of the final
          --  executable program was specified.
 
-         for
-           J in reverse Linker_Switches.First .. Linker_Switches.Last
-         loop
+         for J in reverse Linker_Switches.First .. Linker_Switches.Last loop
             if Linker_Switches.Table (J).all = Output_Flag.all then
                pragma Assert (J < Linker_Switches.Last);
 
-               --  We cannot specify a single executable for several
-               --  main subprograms!
+               --  We cannot specify a single executable for several main
+               --  subprograms
 
                if Osint.Number_Of_Files > 1 then
                   Fail
-                    ("cannot specify a single executable " &
-                     "for several mains");
+                    ("cannot specify a single executable for several mains");
                end if;
 
-               Name_Len := Linker_Switches.Table (J + 1)'Length;
-               Name_Buffer (1 .. Name_Len) :=
-                 Linker_Switches.Table (J + 1).all;
+               Name_Len := 0;
+               Add_Str_To_Name_Buffer (Linker_Switches.Table (J + 1).all);
                Executable := Name_Enter;
 
                Verbose_Msg (Executable, "final executable");
             end if;
          end loop;
 
-         --  If the name of the final executable program was not specified
-         --  then construct it from the main input file.
+         --  If the name of the final executable program was not specified then
+         --  construct it from the main input file.
 
          if Executable = No_File then
             if Main_Project = No_Project then
-               Executable :=
-                 Executable_Name (Strip_Suffix (Main_Source_File));
+               Executable := Executable_Name (Strip_Suffix (Main_Source_File));
 
             else
                --  If we are using a project file, we attempt to remove the
@@ -5548,27 +5357,22 @@ package body Make is
             end if;
          end if;
 
-         if Main_Project /= No_Project then
+         if Main_Project /= No_Project
+           and then Main_Project.Exec_Directory /= No_Path_Information
+         then
             declare
                Exec_File_Name : constant String :=
                                   Get_Name_String (Executable);
 
             begin
                if not Is_Absolute_Path (Exec_File_Name) then
-                  Get_Name_String
-                    (Project_Tree.Projects.Table
-                       (Main_Project).Exec_Directory.Display_Name);
+                  Get_Name_String (Main_Project.Exec_Directory.Display_Name);
 
                   if Name_Buffer (Name_Len) /= Directory_Separator then
-                     Name_Len := Name_Len + 1;
-                     Name_Buffer (Name_Len) := Directory_Separator;
+                     Add_Char_To_Name_Buffer (Directory_Separator);
                   end if;
 
-                  Name_Buffer (Name_Len + 1 ..
-                                       Name_Len + Exec_File_Name'Length) :=
-                    Exec_File_Name;
-
-                  Name_Len := Name_Len + Exec_File_Name'Length;
+                  Add_Str_To_Name_Buffer (Exec_File_Name);
                   Executable := Name_Find;
                end if;
 
@@ -5586,6 +5390,7 @@ package body Make is
 
                Executable_Stamp : Time_Stamp_Type;
                --  Executable is the final executable program
+               --  ??? comment seems unrelated to declaration
 
                Library_Rebuilt : Boolean := False;
 
@@ -5594,6 +5399,10 @@ package body Make is
                   Args (J) := Gcc_Switches.Table (J);
                end loop;
 
+               Queue.Initialize
+                       (Main_Project /= No_Project and then
+                        One_Compilation_Per_Obj_Dir);
+
                --  Now we invoke Compile_Sources for the current main
 
                Compile_Sources
@@ -5618,16 +5427,13 @@ package body Make is
                   Write_Eol;
                end if;
 
-               --  Make sure the queue will be reinitialized for the next round
-
-               First_Q_Initialization := True;
-
                Total_Compilation_Failures :=
                  Total_Compilation_Failures + Compilation_Failures;
 
                if Total_Compilation_Failures /= 0 then
                   if Keep_Going then
                      goto Next_Main;
+
                   else
                      List_Bad_Compilations;
                      Report_Compilation_Failed;
@@ -5642,13 +5448,14 @@ package body Make is
                  and then (Do_Bind_Step
                              or Unique_Compile_All_Projects
                              or not Compile_Only)
-                 and then (Do_Link_Step or N_File = Osint.Number_Of_Files)
+                 and then (Do_Link_Step or else N_File = Osint.Number_Of_Files)
                then
                   Library_Projs.Init;
 
                   declare
                      Depth   : Natural;
                      Current : Natural;
+                     Proj1   : Project_List;
 
                      procedure Add_To_Library_Projs (Proj : Project_Id);
                      --  Add project Project to table Library_Projs in
@@ -5663,7 +5470,7 @@ package body Make is
 
                      begin
                         Library_Projs.Increment_Last;
-                        Depth := Project_Tree.Projects.Table (Proj).Depth;
+                        Depth := Proj.Depth;
 
                         --  Put the projects in decreasing depth order, so that
                         --  if libA depends on libB, libB is first in order.
@@ -5671,8 +5478,7 @@ package body Make is
                         Current := Library_Projs.Last;
                         while Current > 1 loop
                            Prj := Library_Projs.Table (Current - 1);
-                           exit when Project_Tree.Projects.Table
-                             (Prj).Depth >= Depth;
+                           exit when Prj.Depth >= Depth;
                            Library_Projs.Table (Current) := Prj;
                            Current := Current - 1;
                         end loop;
@@ -5684,98 +5490,81 @@ package body Make is
                   --  or probably better, break this out as a nested proc).
 
                   begin
-                     --  Put in Library_Projs table all library project
-                     --  file ids when the library need to be rebuilt.
+                     --  Put in Library_Projs table all library project file
+                     --  ids when the library need to be rebuilt.
 
-                     for Proj1 in Project_Table.First ..
-                                  Project_Table.Last (Project_Tree.Projects)
-                     loop
-                        if Project_Tree.Projects.Table
-                          (Proj1).Standalone_Library
-                        then
-                           There_Are_Stand_Alone_Libraries := True;
+                     Proj1 := Project_Tree.Projects;
+                     while Proj1 /= null loop
+                        if Proj1.Project.Standalone_Library then
+                           Stand_Alone_Libraries := True;
                         end if;
 
-                        if Project_Tree.Projects.Table (Proj1).Library then
-                           MLib.Prj.Check_Library (Proj1, Project_Tree);
+                        if Proj1.Project.Library then
+                           MLib.Prj.Check_Library
+                             (Proj1.Project, Project_Tree);
                         end if;
 
-                        if Project_Tree.Projects.Table
-                             (Proj1).Need_To_Build_Lib
-                        then
-                           Add_To_Library_Projs (Proj1);
+                        if Proj1.Project.Need_To_Build_Lib then
+                           Add_To_Library_Projs (Proj1.Project);
                         end if;
+
+                        Proj1 := Proj1.Next;
                      end loop;
 
                      --  Check if importing libraries should be regenerated
                      --  because at least an imported library will be
                      --  regenerated or is more recent.
 
-                     for Proj1 in Project_Table.First ..
-                                  Project_Table.Last (Project_Tree.Projects)
-                     loop
-                        if Project_Tree.Projects.Table (Proj1).Library
-                          and then
-                            Project_Tree.Projects.Table (Proj1).Library_Kind /=
-                                                                        Static
-                          and then not Project_Tree.Projects.Table
-                                         (Proj1).Need_To_Build_Lib
-                          and then not Project_Tree.Projects.Table
-                                         (Proj1).Externally_Built
+                     Proj1 := Project_Tree.Projects;
+                     while Proj1 /= null loop
+                        if Proj1.Project.Library
+                          and then Proj1.Project.Library_Kind /= Static
+                          and then not Proj1.Project.Need_To_Build_Lib
+                          and then not Proj1.Project.Externally_Built
                         then
                            declare
                               List    : Project_List;
-                              Element : Project_Element;
                               Proj2   : Project_Id;
                               Rebuild : Boolean := False;
 
                               Lib_Timestamp1 : constant Time_Stamp_Type :=
-                                                 Project_Tree.Projects.Table
-                                                   (Proj1).Library_TS;
+                                                 Proj1.Project.Library_TS;
 
                            begin
-                              List := Project_Tree.Projects.Table (Proj1).
-                                                      All_Imported_Projects;
-                              while List /= Empty_Project_List loop
-                                 Element :=
-                                   Project_Tree.Project_Lists.Table (List);
-                                 Proj2 := Element.Project;
-
-                                 if
-                                   Project_Tree.Projects.Table (Proj2).Library
-                                 then
-                                    if Project_Tree.Projects.Table (Proj2).
-                                          Need_To_Build_Lib
+                              List := Proj1.Project.All_Imported_Projects;
+                              while List /= null loop
+                                 Proj2 := List.Project;
+
+                                 if Proj2.Library then
+                                    if Proj2.Need_To_Build_Lib
                                       or else
-                                        (Lib_Timestamp1 <
-                                             Project_Tree.Projects.Table
-                                               (Proj2).Library_TS)
+                                        (Lib_Timestamp1 < Proj2.Library_TS)
                                     then
                                        Rebuild := True;
                                        exit;
                                     end if;
                                  end if;
 
-                                 List := Element.Next;
+                                 List := List.Next;
                               end loop;
 
                               if Rebuild then
-                                 Project_Tree.Projects.Table
-                                   (Proj1).Need_To_Build_Lib := True;
-                                 Add_To_Library_Projs (Proj1);
+                                 Proj1.Project.Need_To_Build_Lib := True;
+                                 Add_To_Library_Projs (Proj1.Project);
                               end if;
                            end;
                         end if;
+
+                        Proj1 := Proj1.Next;
                      end loop;
 
                      --  Reset the flags Need_To_Build_Lib for the next main,
                      --  to avoid rebuilding libraries uselessly.
 
-                     for Proj1 in Project_Table.First ..
-                                  Project_Table.Last (Project_Tree.Projects)
-                     loop
-                        Project_Tree.Projects.Table
-                          (Proj1).Need_To_Build_Lib := False;
+                     Proj1 := Project_Tree.Projects;
+                     while Proj1 /= null loop
+                        Proj1.Project.Need_To_Build_Lib := False;
+                        Proj1 := Proj1.Next;
                      end loop;
                   end;
 
@@ -5851,8 +5640,8 @@ package body Make is
 
                --  If the objects were up-to-date check if the executable file
                --  is also up-to-date. For now always bind and link on the JVM
-               --  since there is currently no simple way to check the
-               --  up-to-date status of objects
+               --  since there is currently no simple way to check whether
+               --  objects are up-to-date.
 
                if Targparm.VM_Target /= JVM_Target
                  and then First_Compiled_File = No_File
@@ -5891,8 +5680,40 @@ package body Make is
                      Executable_Obsolete := Youngest_Obj_File /= No_File;
                   end if;
 
-                  --  Return if the executable is up to date
-                  --  and otherwise motivate the relink/rebind.
+                  --  Check if any library file is more recent than the
+                  --  executable: there may be an externally built library
+                  --  file that has been modified.
+
+                  if not Executable_Obsolete
+                    and then Main_Project /= No_Project
+                  then
+                     declare
+                        Proj1 : Project_List;
+
+                     begin
+                        Proj1 := Project_Tree.Projects;
+                        while Proj1 /= null loop
+                           if Proj1.Project.Library
+                             and then
+                               Proj1.Project.Library_TS > Executable_Stamp
+                           then
+                              Executable_Obsolete := True;
+                              Youngest_Obj_Stamp := Proj1.Project.Library_TS;
+                              Name_Len := 0;
+                              Add_Str_To_Name_Buffer ("library ");
+                              Add_Str_To_Name_Buffer
+                                (Get_Name_String (Proj1.Project.Library_Name));
+                              Youngest_Obj_File := Name_Find;
+                              exit;
+                           end if;
+
+                           Proj1 := Proj1.Next;
+                        end loop;
+                     end;
+                  end if;
+
+                  --  Return if the executable is up to date and otherwise
+                  --  motivate the relink/rebind.
 
                   if not Executable_Obsolete then
                      if not Quiet_Output then
@@ -5939,9 +5760,9 @@ package body Make is
             Change_To_Object_Directory (Main_Project);
          end if;
 
-         --  If we are here, it means that we need to rebuilt the current
-         --  main. So we set Executable_Obsolete to True to make sure that
-         --  the subsequent mains will be rebuilt.
+         --  If we are here, it means that we need to rebuilt the current main,
+         --  so we set Executable_Obsolete to True to make sure that subsequent
+         --  mains will be rebuilt.
 
          Main_ALI_In_Place_Mode_Step : declare
             ALI_File : File_Name_Type;
@@ -5982,6 +5803,8 @@ package body Make is
                --  Set to True when there are shared library project files or
                --  when gnatbind is invoked with -shared.
 
+               Proj : Project_List;
+
             begin
                --  Check if there are shared libraries, so that gnatbind is
                --  called with -shared. Check also if gnatbind is called with
@@ -5991,17 +5814,16 @@ package body Make is
                if Main_Project /= No_Project
                  and then MLib.Tgt.Support_For_Libraries /= Prj.None
                then
-                  for Proj in Project_Table.First ..
-                        Project_Table.Last (Project_Tree.Projects)
-                  loop
-                     if Project_Tree.Projects.Table (Proj).Library
-                       and then Project_Tree.Projects.Table
-                                  (Proj).Library_Kind /= Static
+                  Proj := Project_Tree.Projects;
+                  while Proj /= null loop
+                     if Proj.Project.Library
+                       and then Proj.Project.Library_Kind /= Static
                      then
                         Shared_Libs := True;
                         Bind_Shared := Shared_Switch'Access;
                         exit;
                      end if;
+                     Proj := Proj.Next;
                   end loop;
                end if;
 
@@ -6029,7 +5851,7 @@ package body Make is
                   Args (J) := Binder_Switches.Table (J);
                end loop;
 
-               if There_Are_Stand_Alone_Libraries then
+               if Stand_Alone_Libraries then
                   Last_Arg := Last_Arg + 1;
                   Args (Last_Arg) := Force_Elab_Flags_String'Access;
                end if;
@@ -6037,14 +5859,22 @@ package body Make is
                if Main_Project /= No_Project then
 
                   --  Put all the source directories in ADA_INCLUDE_PATH,
-                  --  and all the object directories in ADA_OBJECTS_PATH.
+                  --  and all the object directories in ADA_OBJECTS_PATH,
+                  --  except those of library projects.
 
-                  Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
+                  Prj.Env.Set_Ada_Paths
+                    (Main_Project, Project_Tree, Use_Include_Path_File);
 
                   --  If switch -C was specified, create a binder mapping file
 
                   if Create_Mapping_File then
-                     Create_Binder_Mapping_File (Args, Last_Arg);
+                     Mapping_Path := Create_Binder_Mapping_File;
+
+                     if Mapping_Path /= No_Path then
+                        Last_Arg := Last_Arg + 1;
+                        Args (Last_Arg) :=
+                          new String'("-F=" & Get_Name_String (Mapping_Path));
+                     end if;
                   end if;
 
                end if;
@@ -6056,13 +5886,10 @@ package body Make is
                exception
                   when others =>
 
-                     --  If -dn was not specified, delete the temporary mapping
-                     --  file, if one was created.
+                     --  Delete the temporary mapping file if one was created
 
-                     if not Debug.Debug_Flag_N
-                       and then Mapping_Path /= No_Path
-                     then
-                        Delete_File (Get_Name_String (Mapping_Path), Discard);
+                     if Mapping_Path /= No_Path then
+                        Delete_Temporary_File (Project_Tree, Mapping_Path);
                      end if;
 
                      --  And reraise the exception
@@ -6070,11 +5897,11 @@ package body Make is
                      raise;
                end;
 
-               --  If -dn was not specified, delete the temporary mapping file,
+               --  If -dn was not specified, delete the temporary mapping file
                --  if one was created.
 
-               if not Debug.Debug_Flag_N and then Mapping_Path /= No_Path then
-                  Delete_File (Get_Name_String (Mapping_Path), Discard);
+               if Mapping_Path /= No_Path then
+                  Delete_Temporary_File (Project_Tree, Mapping_Path);
                end if;
             end Bind_Step;
          end if;
@@ -6084,10 +5911,11 @@ package body Make is
                Linker_Switches_Last : constant Integer := Linker_Switches.Last;
                Path_Option          : constant String_Access :=
                                         MLib.Linker_Library_Path_Option;
-               There_Are_Libraries  : Boolean := False;
+               Libraries_Present    : Boolean := False;
                Current              : Natural;
                Proj2                : Project_Id;
                Depth                : Natural;
+               Proj1                : Project_List;
 
             begin
                if not Run_Path_Option then
@@ -6104,17 +5932,15 @@ package body Make is
 
                      --  Check for library projects
 
-                     for Proj1 in Project_Table.First ..
-                           Project_Table.Last (Project_Tree.Projects)
-                     loop
-                        if Proj1 /= Main_Project
-                          and then
-                            Project_Tree.Projects.Table (Proj1).Library
+                     Proj1 := Project_Tree.Projects;
+                     while Proj1 /= null loop
+                        if Proj1.Project /= Main_Project
+                          and then Proj1.Project.Library
                         then
                            --  Add this project to table Library_Projs
 
-                           There_Are_Libraries := True;
-                           Depth := Project_Tree.Projects.Table (Proj1).Depth;
+                           Libraries_Present := True;
+                           Depth := Proj1.Project.Depth;
                            Library_Projs.Increment_Last;
                            Current := Library_Projs.Last;
 
@@ -6123,29 +5949,28 @@ package body Make is
 
                            while Current > 1 loop
                               Proj2 := Library_Projs.Table (Current - 1);
-                              exit when Project_Tree.Projects.Table
-                                          (Proj2).Depth <= Depth;
+                              exit when Proj2.Depth <= Depth;
                               Library_Projs.Table (Current) := Proj2;
                               Current := Current - 1;
                            end loop;
 
-                           Library_Projs.Table (Current) := Proj1;
+                           Library_Projs.Table (Current) := Proj1.Project;
 
                            --  If it is not a static library and path option
                            --  is set, add it to the Library_Paths table.
 
-                           if Project_Tree.Projects.Table
-                                (Proj1).Library_Kind /= Static
+                           if Proj1.Project.Library_Kind /= Static
                              and then Path_Option /= null
                            then
                               Library_Paths.Increment_Last;
                               Library_Paths.Table (Library_Paths.Last) :=
                                 new String'
                                   (Get_Name_String
-                                       (Project_Tree.Projects.Table
-                                            (Proj1).Library_Dir.Display_Name));
+                                     (Proj1.Project.Library_Dir.Display_Name));
                            end if;
                         end if;
+
+                        Proj1 := Proj1.Next;
                      end loop;
 
                      for Index in 1 .. Library_Projs.Last loop
@@ -6156,9 +5981,8 @@ package body Make is
                         Linker_Switches.Table (Linker_Switches.Last) :=
                           new String'("-L" &
                                       Get_Name_String
-                                        (Project_Tree.Projects.Table
-                                           (Library_Projs.Table (Index)).
-                                              Library_Dir.Display_Name));
+                                        (Library_Projs.Table (Index).
+                                            Library_Dir.Display_Name));
 
                         --  Add the -l switch
 
@@ -6166,13 +5990,12 @@ package body Make is
                         Linker_Switches.Table (Linker_Switches.Last) :=
                           new String'("-l" &
                                       Get_Name_String
-                                        (Project_Tree.Projects.Table
-                                           (Library_Projs.Table (Index)).
-                                              Library_Name));
+                                        (Library_Projs.Table (Index).
+                                           Library_Name));
                      end loop;
                   end if;
 
-                  if There_Are_Libraries then
+                  if Libraries_Present then
 
                      --  If Path_Option is not null, create the switch
                      --  ("-Wl,-rpath," or equivalent) with all the non static
@@ -6180,60 +6003,92 @@ package body Make is
                      --  We do that only if Run_Path_Option is True
                      --  (not disabled by -R switch).
 
-                     if Run_Path_Option and Path_Option /= null then
+                     if Run_Path_Option and then Path_Option /= null then
                         declare
                            Option  : String_Access;
                            Length  : Natural := Path_Option'Length;
                            Current : Natural;
 
                         begin
-                           for Index in
-                             Library_Paths.First .. Library_Paths.Last
-                           loop
-                              --  Add the length of the library dir plus one
-                              --  for the directory separator.
-
-                              Length :=
-                                Length +
-                                Library_Paths.Table (Index)'Length + 1;
-                           end loop;
+                           if MLib.Separate_Run_Path_Options then
+
+                              --  We are going to create one switch of the form
+                              --  "-Wl,-rpath,dir_N" for each directory to
+                              --  consider.
+
+                              --  One switch for each library directory
+
+                              for Index in
+                                Library_Paths.First .. Library_Paths.Last
+                              loop
+                                 Linker_Switches.Increment_Last;
+                                 Linker_Switches.Table
+                                   (Linker_Switches.Last) := new String'
+                                   (Path_Option.all &
+                                    Library_Paths.Table (Index).all);
+                              end loop;
 
-                           --  Finally, add the length of the standard GNAT
-                           --  library dir.
+                              --  One switch for the standard GNAT library dir
 
-                           Length := Length + MLib.Utl.Lib_Directory'Length;
-                           Option := new String (1 .. Length);
-                           Option (1 .. Path_Option'Length) := Path_Option.all;
-                           Current := Path_Option'Length;
+                              Linker_Switches.Increment_Last;
+                              Linker_Switches.Table
+                                (Linker_Switches.Last) := new String'
+                                (Path_Option.all & MLib.Utl.Lib_Directory);
 
-                           --  Put each library dir followed by a dir separator
+                           else
+                              --  We are going to create one switch of the form
+                              --  "-Wl,-rpath,dir_1:dir_2:dir_3"
+
+                              for Index in
+                                Library_Paths.First .. Library_Paths.Last
+                              loop
+                                 --  Add the length of the library dir plus one
+                                 --  for the directory separator.
+
+                                 Length :=
+                                   Length +
+                                     Library_Paths.Table (Index)'Length + 1;
+                              end loop;
 
-                           for Index in
-                             Library_Paths.First .. Library_Paths.Last
-                           loop
-                              Option
-                                (Current + 1 ..
+                              --  Finally, add the length of the standard GNAT
+                              --  library dir.
+
+                              Length := Length + MLib.Utl.Lib_Directory'Length;
+                              Option := new String (1 .. Length);
+                              Option (1 .. Path_Option'Length) :=
+                                Path_Option.all;
+                              Current := Path_Option'Length;
+
+                              --  Put each library dir followed by a dir
+                              --  separator.
+
+                              for Index in
+                                Library_Paths.First .. Library_Paths.Last
+                              loop
+                                 Option
+                                   (Current + 1 ..
+                                      Current +
+                                        Library_Paths.Table (Index)'Length) :=
+                                   Library_Paths.Table (Index).all;
+                                 Current :=
                                    Current +
-                                   Library_Paths.Table (Index)'Length) :=
-                                Library_Paths.Table (Index).all;
-                              Current :=
-                                Current +
-                                Library_Paths.Table (Index)'Length + 1;
-                              Option (Current) := Path_Separator;
-                           end loop;
+                                     Library_Paths.Table (Index)'Length + 1;
+                                 Option (Current) := Path_Separator;
+                              end loop;
 
-                           --  Finally put the standard GNAT library dir
+                              --  Finally put the standard GNAT library dir
 
-                           Option
-                             (Current + 1 ..
-                                Current + MLib.Utl.Lib_Directory'Length) :=
-                             MLib.Utl.Lib_Directory;
+                              Option
+                                (Current + 1 ..
+                                   Current + MLib.Utl.Lib_Directory'Length) :=
+                                  MLib.Utl.Lib_Directory;
 
-                           --  And add the switch to the linker switches
+                              --  And add the switch to the linker switches
 
-                           Linker_Switches.Increment_Last;
-                           Linker_Switches.Table (Linker_Switches.Last) :=
-                             Option;
+                              Linker_Switches.Increment_Last;
+                              Linker_Switches.Table (Linker_Switches.Last) :=
+                                Option;
+                           end if;
                         end;
                      end if;
 
@@ -6241,7 +6096,11 @@ package body Make is
 
                   --  Put the object directories in ADA_OBJECTS_PATH
 
-                  Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
+                  Prj.Env.Set_Ada_Paths
+                    (Main_Project,
+                     Project_Tree,
+                     Including_Libraries => False,
+                     Include_Path        => False);
 
                   --  Check for attributes Linker'Linker_Options in projects
                   --  other than the main project
@@ -6259,6 +6118,15 @@ package body Make is
                   end;
                end if;
 
+               --  Add switch -M to gnatlink if buider switch --create-map-file
+               --  has been specified.
+
+               if Map_File /= null then
+                  Linker_Switches.Increment_Last;
+                  Linker_Switches.Table (Linker_Switches.Last) :=
+                    new String'("-M" & Map_File.all);
+               end if;
+
                declare
                   Args : Argument_List
                            (Linker_Switches.First .. Linker_Switches.Last + 2);
@@ -6278,9 +6146,17 @@ package body Make is
                      then
                         Skip := True;
 
+                     --  Here we capture and duplicate the linker argument. We
+                     --  need to do the duplication since the arguments will
+                     --  get normalized. Not doing so will result in calling
+                     --  normalized two times for the same set of arguments if
+                     --  gnatmake is passed multiple mains. This can result in
+                     --  the wrong argument being passed to the linker.
+
                      else
                         Last_Arg := Last_Arg + 1;
-                        Args (Last_Arg) := Linker_Switches.Table (J);
+                        Args (Last_Arg) :=
+                          new String'(Linker_Switches.Table (J).all);
                      end if;
                   end loop;
 
@@ -6309,7 +6185,9 @@ package body Make is
                         Successful_Links.Table (Successful_Links.Last) :=
                           Main_ALI_File;
 
-                     elsif Osint.Number_Of_Files = 1 or not Keep_Going then
+                     elsif Osint.Number_Of_Files = 1
+                       or else not Keep_Going
+                     then
                         Make_Failed ("*** link failed.");
 
                      else
@@ -6362,8 +6240,7 @@ package body Make is
                                                  not Unique_Compile);
 
                   The_Packages : constant Package_Id :=
-                    Project_Tree.Projects.Table
-                      (Main_Project).Decl.Packages;
+                    Main_Project.Decl.Packages;
 
                   Binder_Package : constant Prj.Package_Id :=
                                Prj.Util.Value_Of
@@ -6382,9 +6259,9 @@ package body Make is
                   --  as an immediate source of the main project file.
 
                   if Main_Unit_File_Name = "" then
-                     Make_Failed ('"' & Main_Source_File_Name,
-                                  """ is not a unit of project ",
-                                  Project_File_Name.all & ".");
+                     Make_Failed ('"' & Main_Source_File_Name
+                                  & """ is not a unit of project "
+                                  Project_File_Name.all & ".");
 
                   else
                      --  Remove any directory information from the main
@@ -6424,7 +6301,7 @@ package body Make is
                   --  Add binder switches from the project file for this main,
                   --  if any.
 
-                  if Do_Bind_Step and Binder_Package /= No_Package then
+                  if Do_Bind_Step and then Binder_Package /= No_Package then
                      if Verbose_Mode then
                         Write_Str ("Adding binder switches for """);
                         Write_Str (Main_Unit_File_Name);
@@ -6432,16 +6309,17 @@ package body Make is
                      end if;
 
                      Add_Switches
-                       (File_Name   => Main_Unit_File_Name,
-                        Index       => Main_Index,
-                        The_Package => Binder_Package,
-                        Program     => Binder);
+                       (Project_Node_Tree => Project_Node_Tree,
+                        File_Name         => Main_Unit_File_Name,
+                        Index             => Main_Index,
+                        The_Package       => Binder_Package,
+                        Program           => Binder);
                   end if;
 
                   --  Add linker switches from the project file for this main,
                   --  if any.
 
-                  if Do_Link_Step and Linker_Package /= No_Package then
+                  if Do_Link_Step and then Linker_Package /= No_Package then
                      if Verbose_Mode then
                         Write_Str ("Adding linker switches for""");
                         Write_Str (Main_Unit_File_Name);
@@ -6449,10 +6327,11 @@ package body Make is
                      end if;
 
                      Add_Switches
-                       (File_Name   => Main_Unit_File_Name,
-                        Index       => Main_Index,
-                        The_Package => Linker_Package,
-                        Program     => Linker);
+                       (Project_Node_Tree => Project_Node_Tree,
+                        File_Name         => Main_Unit_File_Name,
+                        Index             => Main_Index,
+                        The_Package       => Linker_Package,
+                        Program           => Linker);
                   end if;
 
                   --  As we are using a project file, for relative paths we add
@@ -6461,10 +6340,10 @@ package body Make is
                   --  relative path in the project file.
 
                   declare
-                     Dir_Path : constant String_Access :=
-                       new String'(Get_Name_String
-                                     (Project_Tree.Projects.Table
-                                        (Main_Project).Directory.Name));
+                     Dir_Path : constant String :=
+                                  Get_Name_String
+                                    (Main_Project.Directory.Display_Name);
+
                   begin
                      for
                        J in Last_Binder_Switch + 1 .. Binder_Switches.Last
@@ -6577,6 +6456,7 @@ package body Make is
 
    procedure Init_Mapping_File
      (Project    : Project_Id;
+      Data       : in out Project_Compilation_Data;
       File_Index : in out Natural)
    is
       FD     : File_Descriptor;
@@ -6586,33 +6466,33 @@ package body Make is
    begin
       --  Increase the index of the last mapping file for this project
 
-      Last_Mapping_File_Names (Project) :=
-        Last_Mapping_File_Names (Project) + 1;
+      Data.Last_Mapping_File_Names := Data.Last_Mapping_File_Names + 1;
 
       --  If there is a project file, call Create_Mapping_File with
       --  the project id.
 
       if Project /= No_Project then
          Prj.Env.Create_Mapping_File
-           (Project, Project_Tree,
-            The_Mapping_File_Names
-              (Project, Last_Mapping_File_Names (Project)));
+           (Project,
+            In_Tree  => Project_Tree,
+            Language => Name_Ada,
+            Name     => Data.Mapping_File_Names
+                          (Data.Last_Mapping_File_Names));
 
       --  Otherwise, just create an empty file
 
       else
          Tempdir.Create_Temp_File
            (FD,
-            The_Mapping_File_Names
-              (No_Project, Last_Mapping_File_Names (No_Project)));
+            Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
 
          if FD = Invalid_FD then
             Make_Failed ("disk full");
 
          else
             Record_Temp_File
-              (The_Mapping_File_Names
-                 (No_Project, Last_Mapping_File_Names (No_Project)));
+              (Project_Tree,
+               Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
          end if;
 
          Close (FD, Status);
@@ -6624,25 +6504,14 @@ package body Make is
 
       --  And return the index of the newly created file
 
-      File_Index := Last_Mapping_File_Names (Project);
+      File_Index := Data.Last_Mapping_File_Names;
    end Init_Mapping_File;
 
-   ------------
-   -- Init_Q --
-   ------------
-
-   procedure Init_Q is
-   begin
-      First_Q_Initialization := False;
-      Q_Front := Q.First;
-      Q.Set_Last (Q.First);
-   end Init_Q;
-
    ----------------
    -- Initialize --
    ----------------
 
-   procedure Initialize is
+   procedure Initialize (Project_Node_Tree : out Project_Node_Tree_Ref) is
 
       procedure Check_Version_And_Help is
          new Check_Version_And_Help_G (Makeusg);
@@ -6650,7 +6519,12 @@ package body Make is
       --  Start of processing for Initialize
 
    begin
-      Prj.Set_Mode (Ada_Only);
+      --  Prepare the project's tree, since this is used to hold external
+      --  references, project path and other attributes that can be impacted by
+      --  the command line switches
+
+      Project_Node_Tree := new Project_Node_Tree_Data;
+      Prj.Tree.Initialize (Project_Node_Tree);
 
       --  Override default initialization of Check_Object_Consistency since
       --  this is normally False for GNATBIND, but is True for GNATMAKE since
@@ -6659,7 +6533,7 @@ package body Make is
 
       Check_Object_Consistency := True;
 
-      --  Package initializations. The order of calls is important here
+      --  Package initializations (the order of calls is important here)
 
       Output.Set_Standard_Error;
 
@@ -6668,8 +6542,6 @@ package body Make is
       Linker_Switches.Init;
 
       Csets.Initialize;
-      Namet.Initialize;
-
       Snames.Initialize;
 
       Prj.Initialize (Project_Tree);
@@ -6677,11 +6549,12 @@ package body Make is
       Dependencies.Init;
 
       RTS_Specified := null;
+      N_M_Switch := 0;
 
       Mains.Delete;
 
-      --  Add the directory where gnatmake is invoked in front of the
-      --  path, if gnatmake is invoked from a bin directory or with directory
+      --  Add the directory where gnatmake is invoked in front of the path,
+      --  if gnatmake is invoked from a bin directory or with directory
       --  information. Only do this if the platform is not VMS, where the
       --  notion of path does not really exist.
 
@@ -6733,9 +6606,14 @@ package body Make is
       --  do not include --version or --help.
 
       Scan_Args : for Next_Arg in 1 .. Argument_Count loop
-         Scan_Make_Arg (Argument (Next_Arg), And_Save => True);
+         Scan_Make_Arg
+           (Project_Node_Tree, Argument (Next_Arg), And_Save => True);
       end loop Scan_Args;
 
+      if N_M_Switch > 0 and RTS_Specified = null then
+         Process_Multilib (Project_Node_Tree);
+      end if;
+
       if Commands_To_Stdout then
          Set_Standard_Output;
       end if;
@@ -6817,7 +6695,9 @@ package body Make is
            (Project           => Main_Project,
             In_Tree           => Project_Tree,
             Project_File_Name => Project_File_Name.all,
-            Packages_To_Check => Packages_To_Check_By_Gnatmake);
+            Packages_To_Check => Packages_To_Check_By_Gnatmake,
+            Flags             => Gnatmake_Flags,
+            In_Node_Tree      => Project_Node_Tree);
 
          --  The parsing of project files may have changed the current output
 
@@ -6828,7 +6708,8 @@ package body Make is
          end if;
 
          if Main_Project = No_Project then
-            Make_Failed ("""", Project_File_Name.all, """ processing failed");
+            Make_Failed
+              ("""" & Project_File_Name.all & """ processing failed");
          end if;
 
          Create_Mapping_File := True;
@@ -6841,31 +6722,17 @@ package body Make is
             Write_Eol;
          end if;
 
-         --  We add the source directories and the object directories
-         --  to the search paths.
+         --  We add the source directories and the object directories to the
+         --  search paths.
+         --  ??? Why do we need these search directories, we already know the
+         --  locations from parsing the project, except for the runtime which
+         --  has its own directories anyway
 
          Add_Source_Directories (Main_Project, Project_Tree);
-         Add_Object_Directories (Main_Project, Project_Tree);
-
-         --  Compute depth of each project
-
-         for Proj in Project_Table.First ..
-                     Project_Table.Last (Project_Tree.Projects)
-         loop
-            Project_Tree.Projects.Table (Proj).Seen := False;
-            Project_Tree.Projects.Table (Proj).Depth := 0;
-         end loop;
-
-         Recursive_Compute_Depth (Main_Project, Depth => 1);
+         Add_Object_Directories (Main_Project);
 
-         --  For each project compute the list of the projects it imports
-         --  directly or indirectly.
-
-         for Proj in Project_Table.First ..
-                     Project_Table.Last (Project_Tree.Projects)
-         loop
-            Compute_All_Imported_Projects (Proj);
-         end loop;
+         Recursive_Compute_Depth (Main_Project);
+         Compute_All_Imported_Projects (Project_Tree);
 
       else
 
@@ -6907,13 +6774,12 @@ package body Make is
       Into_Q       : Boolean)
    is
       Put_In_Q : Boolean := Into_Q;
-      Unit     : Unit_Data;
+      Unit     : Unit_Index;
       Sfile    : File_Name_Type;
       Index    : Int;
+      Project  : Project_Id;
 
-      Extending : constant Boolean :=
-                    Project_Tree.Projects.Table
-                      (The_Project).Extends /= No_Project;
+      Extending : constant Boolean := The_Project.Extends /= No_Project;
 
       function Check_Project (P : Project_Id) return Boolean;
       --  Returns True if P is The_Project or a project extended by The_Project
@@ -6924,22 +6790,21 @@ package body Make is
 
       function Check_Project (P : Project_Id) return Boolean is
       begin
-         if All_Projects or P = The_Project then
+         if All_Projects or else P = The_Project then
             return True;
 
          elsif Extending then
             declare
-               Data : Project_Data :=
-                        Project_Tree.Projects.Table (The_Project);
+               Proj : Project_Id;
 
             begin
-               loop
-                  if P = Data.Extends then
+               Proj := The_Project;
+               while Proj /= null loop
+                  if P = Proj.Extends then
                      return True;
                   end if;
 
-                  Data := Project_Tree.Projects.Table (Data.Extends);
-                  exit when Data.Extends = No_Project;
+                  Proj := Proj.Extends;
                end loop;
             end;
          end if;
@@ -6952,27 +6817,27 @@ package body Make is
    begin
       --  For all the sources in the project files,
 
-      for Id in Unit_Table.First ..
-                Unit_Table.Last (Project_Tree.Units)
-      loop
-         Unit  := Project_Tree.Units.Table (Id);
-         Sfile := No_File;
-         Index := 0;
+      Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
+      while Unit /= null loop
+         Sfile   := No_File;
+         Index   := 0;
+         Project := No_Project;
 
          --  If there is a source for the body, and the body has not been
-         --  locally removed,
+         --  locally removed.
 
-         if Unit.File_Names (Body_Part).Name /= No_File
-           and then Unit.File_Names (Body_Part).Path.Name /= Slash
+         if Unit.File_Names (Impl) /= null
+           and then not Unit.File_Names (Impl).Locally_Removed
          then
             --  And it is a source for the specified project
 
-            if Check_Project (Unit.File_Names (Body_Part).Project) then
+            if Check_Project (Unit.File_Names (Impl).Project) then
+               Project := Unit.File_Names (Impl).Project;
 
                --  If we don't have a spec, we cannot consider the source
-               --  if it is a subunit
+               --  if it is a subunit.
 
-               if Unit.File_Names (Specification).Name = No_File then
+               if Unit.File_Names (Spec) = null then
                   declare
                      Src_Ind : Source_File_Index;
 
@@ -6990,7 +6855,7 @@ package body Make is
                   begin
                      Src_Ind := Sinput.P.Load_Project_File
                                   (Get_Name_String
-                                     (Unit.File_Names (Body_Part).Path.Name));
+                                   (Unit.File_Names (Impl).Path.Display_Name));
 
                      --  If it is a subunit, discard it
 
@@ -6998,60 +6863,56 @@ package body Make is
                         Sfile := No_File;
                         Index := 0;
                      else
-                        Sfile := Unit.File_Names (Body_Part).Display_Name;
-                        Index := Unit.File_Names (Body_Part).Index;
+                        Sfile := Unit.File_Names (Impl).Display_File;
+                        Index := Unit.File_Names (Impl).Index;
                      end if;
                   end;
 
                else
-                  Sfile := Unit.File_Names (Body_Part).Display_Name;
-                  Index := Unit.File_Names (Body_Part).Index;
+                  Sfile := Unit.File_Names (Impl).Display_File;
+                  Index := Unit.File_Names (Impl).Index;
                end if;
             end if;
 
-         elsif Unit.File_Names (Specification).Name /= No_File
-           and then Unit.File_Names (Specification).Path.Name /= Slash
-           and then Check_Project (Unit.File_Names (Specification).Project)
+         elsif Unit.File_Names (Spec) /= null
+           and then not Unit.File_Names (Spec).Locally_Removed
+           and then Check_Project (Unit.File_Names (Spec).Project)
          then
-            --  If there is no source for the body, but there is a source
-            --  for the spec which has not been locally removed, then we take
-            --  this one.
+            --  If there is no source for the body, but there is one for the
+            --  spec which has not been locally removed, then we take this one.
 
-            Sfile := Unit.File_Names (Specification).Display_Name;
-            Index := Unit.File_Names (Specification).Index;
+            Sfile := Unit.File_Names (Spec).Display_File;
+            Index := Unit.File_Names (Spec).Index;
+            Project := Unit.File_Names (Spec).Project;
          end if;
 
-         --  If Put_In_Q is True, we insert into the Q
+         --  For the first source inserted into the Q, we need to initialize
+         --  the Q, but not for the subsequent sources.
 
-         if Put_In_Q then
+         Queue.Initialize
+                 (Main_Project /= No_Project and then
+                  One_Compilation_Per_Obj_Dir);
 
-            --  For the first source inserted into the Q, we need to initialize
-            --  the Q, but not for the subsequent sources.
+         --  And of course, only insert in the Q if the source is not marked
 
-            if First_Q_Initialization then
-               Init_Q;
+         if Sfile /= No_File and then not Is_Marked (Sfile, Index) then
+            if Verbose_Mode then
+               Write_Str ("Adding """);
+               Write_Str (Get_Name_String (Sfile));
+               Write_Line (""" to the queue");
             end if;
 
-            --  And of course, we only insert in the Q if the source is not
-            --  marked.
-
-            if Sfile /= No_File and then not Is_Marked (Sfile, Index) then
-               if Verbose_Mode then
-                  Write_Str ("Adding """);
-                  Write_Str (Get_Name_String (Sfile));
-                  Write_Line (""" to the queue");
-               end if;
-
-               Insert_Q (Sfile, Index => Index);
-               Mark (Sfile, Index);
-            end if;
+            Queue.Insert (Sfile, Project, Index => Index);
+            Mark (Sfile, Index);
+         end if;
 
-         elsif Sfile /= No_File then
+         if not Put_In_Q and then Sfile /= No_File then
 
-            --  If Put_In_Q is False, we add the source as it it were specified
+            --  If Put_In_Q is False, we add the source as if it were specified
             --  on the command line, and we set Put_In_Q to True, so that the
-            --  following sources will be put directly in the queue. This will
-            --  allow parallel compilation processes if -jx switch is used.
+            --  following sources will only be put in the queue. The source is
+            --  aready in the Q, but we need at least one fake main to call
+            --  Compile_Sources.
 
             if Verbose_Mode then
                Write_Str ("Adding """);
@@ -7061,50 +6922,15 @@ package body Make is
 
             Osint.Add_File (Get_Name_String (Sfile), Index);
             Put_In_Q := True;
-
-            --  As we may look into the Q later, ensure the Q has been
-            --  initialized to avoid errors.
-
-            if First_Q_Initialization then
-               Init_Q;
-            end if;
          end if;
+
+         Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
       end loop;
    end Insert_Project_Sources;
 
-   --------------
-   -- Insert_Q --
-   --------------
-
-   procedure Insert_Q
-     (Source_File : File_Name_Type;
-      Source_Unit : Unit_Name_Type := No_Unit_Name;
-      Index       : Int            := 0)
-   is
-   begin
-      if Debug.Debug_Flag_Q then
-         Write_Str ("   Q := Q + [ ");
-         Write_Name (Source_File);
-
-         if Index /= 0 then
-            Write_Str (", ");
-            Write_Int (Index);
-         end if;
-
-         Write_Str (" ] ");
-         Write_Eol;
-      end if;
-
-      Q.Table (Q.Last) :=
-        (File  => Source_File,
-         Unit  => Source_Unit,
-         Index => Index);
-      Q.Increment_Last;
-   end Insert_Q;
-
-   ---------------------
-   -- Is_In_Obsoleted --
-   ---------------------
+   ---------------------
+   -- Is_In_Obsoleted --
+   ---------------------
 
    function Is_In_Obsoleted (F : File_Name_Type) return Boolean is
    begin
@@ -7149,8 +6975,8 @@ package body Make is
       Full_Lib_File : File_Name_Type) return Boolean
    is
    begin
-      --  There is something to check only when using project files.
-      --  Otherwise, this function returns True (last line of the function).
+      --  There is something to check only when using project files. Otherwise,
+      --  this function returns True (last line of the function).
 
       if Main_Project /= No_Project then
          declare
@@ -7158,7 +6984,6 @@ package body Make is
                                  Get_Name_String (Source_File);
             Saved_Verbosity  : constant Verbosity := Current_Verbosity;
             Project          : Project_Id         := No_Project;
-            Data             : Project_Data;
 
             Path_Name : Path_Name_Type := No_Path;
             pragma Warnings (Off, Path_Name);
@@ -7176,18 +7001,17 @@ package body Make is
                Path             => Path_Name);
             Current_Verbosity := Saved_Verbosity;
 
-            --  If this source is in a project, check that the ALI file is
-            --  in its object directory. If it is not, return False, so that
-            --  the ALI file will not be skipped.
+            --  If this source is in a project, check that the ALI file is in
+            --  its object directory. If it is not, return False, so that the
+            --  ALI file will not be skipped.
 
             if Project /= No_Project then
-               Data := Project_Tree.Projects.Table (Project);
-
                declare
                   Object_Directory : constant String :=
                                        Normalize_Pathname
                                         (Get_Name_String
-                                         (Data.Object_Directory.Display_Name));
+                                         (Project.
+                                            Object_Directory.Display_Name));
 
                   Olast : Natural := Object_Directory'Last;
 
@@ -7245,7 +7069,7 @@ package body Make is
       Display (Gnatlink.all, Link_Args);
 
       if Gnatlink_Path = null then
-         Make_Failed ("error, unable to locate ", Gnatlink.all);
+         Make_Failed ("error, unable to locate " & Gnatlink.all);
       end if;
 
       GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
@@ -7341,10 +7165,10 @@ package body Make is
    -- Make_Failed --
    -----------------
 
-   procedure Make_Failed (S1 : String; S2 : String := ""; S3 : String := "") is
+   procedure Make_Failed (S : String) is
    begin
       Delete_All_Temp_Files;
-      Osint.Fail (S1, S2, S3);
+      Osint.Fail (S);
    end Make_Failed;
 
    --------------------
@@ -7359,48 +7183,42 @@ package body Make is
       N : Name_Id;
       B : Byte;
 
-   begin
-      if On_Command_Line then
-         declare
-            Real_Path : constant String := Normalize_Pathname (Dir);
+      function Base_Directory return String;
+      --  If Dir comes from the command line, empty string (relative paths are
+      --  resolved with respect to the current directory), else return the main
+      --  project's directory.
 
-         begin
-            if Real_Path'Length = 0 then
-               Name_Len := Dir'Length;
-               Name_Buffer (1 .. Name_Len) := Dir;
+      --------------------
+      -- Base_Directory --
+      --------------------
 
-            else
-               Name_Len := Real_Path'Length;
-               Name_Buffer (1 .. Name_Len) := Real_Path;
-            end if;
-         end;
+      function Base_Directory return String is
+      begin
+         if On_Command_Line then
+            return "";
+         else
+            return Get_Name_String (Main_Project.Directory.Display_Name);
+         end if;
+      end Base_Directory;
 
-      else
-         declare
-            Real_Path : constant String :=
-              Normalize_Pathname
-                (Dir,
-                 Get_Name_String
-                   (Project_Tree.Projects.Table
-                                   (Main_Project).Directory.Display_Name));
+      Real_Path : constant String := Normalize_Pathname (Dir, Base_Directory);
 
-         begin
-            if Real_Path'Length = 0 then
-               Name_Len := Dir'Length;
-               Name_Buffer (1 .. Name_Len) := Dir;
+   --  Start of processing for Mark_Directory
 
-            else
-               Name_Len := Real_Path'Length;
-               Name_Buffer (1 .. Name_Len) := Real_Path;
-            end if;
-         end;
+   begin
+      Name_Len := 0;
+
+      if Real_Path'Length = 0 then
+         Add_Str_To_Name_Buffer (Dir);
+
+      else
+         Add_Str_To_Name_Buffer (Real_Path);
       end if;
 
       --  Last character is supposed to be a directory separator
 
       if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
-         Name_Len := Name_Len + 1;
-         Name_Buffer (Name_Len) := Directory_Separator;
+         Add_Char_To_Name_Buffer (Directory_Separator);
       end if;
 
       --  Add flags to the already existing flags
@@ -7410,55 +7228,462 @@ package body Make is
       Set_Name_Table_Byte (N, B or Mark);
    end Mark_Directory;
 
-   -----------------------------
-   -- Recursive_Compute_Depth --
-   -----------------------------
+   ----------------------
+   -- Process_Multilib --
+   ----------------------
 
-   procedure Recursive_Compute_Depth
-     (Project : Project_Id;
-      Depth   : Natural)
+   procedure Process_Multilib
+     (Project_Node_Tree : Project_Node_Tree_Ref)
    is
-      List : Project_List;
-      Proj : Project_Id;
+      Output_FD         : File_Descriptor;
+      Output_Name       : String_Access;
+      Arg_Index         : Natural := 0;
+      Success           : Boolean := False;
+      Return_Code       : Integer := 0;
+      Multilib_Gcc_Path : String_Access;
+      Multilib_Gcc      : String_Access;
+      N_Read            : Integer := 0;
+      Line              : String (1 .. 1000);
+      Args              : Argument_List (1 .. N_M_Switch + 1);
 
    begin
-      --  Nothing to do if there is no project or if the project has already
-      --  been seen or if the depth is large enough.
+      pragma Assert (N_M_Switch > 0 and RTS_Specified = null);
 
-      if Project = No_Project
-        or else Project_Tree.Projects.Table (Project).Seen
-        or else Project_Tree.Projects.Table (Project).Depth >= Depth
-      then
+      --  In case we detected a multilib switch and the user has not
+      --  manually specified a specific RTS we emulate the following command:
+      --  gnatmake $FLAGS --RTS=$(gcc -print-multi-directory $FLAGS)
+
+      --  First select the flags which might have an impact on multilib
+      --  processing. Note that this is an heuristic selection and it
+      --  will need to be maintained over time. The condition has to
+      --  be kept synchronized with N_M_Switch counting in Scan_Make_Arg.
+
+      for Next_Arg in 1 .. Argument_Count loop
+         declare
+            Argv : constant String := Argument (Next_Arg);
+         begin
+            if Argv'Length > 2
+              and then Argv (1) = '-'
+              and then Argv (2) = 'm'
+              and then Argv /= "-margs"
+
+              --  Ignore -mieee to avoid spawning an extra gcc in this case
+
+              and then Argv /= "-mieee"
+            then
+               Arg_Index := Arg_Index + 1;
+               Args (Arg_Index) := new String'(Argv);
+            end if;
+         end;
+      end loop;
+
+      pragma Assert (Arg_Index = N_M_Switch);
+
+      Args (Args'Last) := new String'("-print-multi-directory");
+
+      --  Call the GCC driver with the collected flags and save its
+      --  output. Alternate design would be to link in gnatmake the
+      --  relevant part of the GCC driver.
+
+      if Saved_Gcc /= null then
+         Multilib_Gcc := Saved_Gcc;
+      else
+         Multilib_Gcc := Gcc;
+      end if;
+
+      Multilib_Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Multilib_Gcc.all);
+
+      Create_Temp_Output_File (Output_FD, Output_Name);
+
+      if Output_FD = Invalid_FD then
          return;
       end if;
 
-      Project_Tree.Projects.Table (Project).Depth := Depth;
+      GNAT.OS_Lib.Spawn
+        (Multilib_Gcc_Path.all, Args, Output_FD, Return_Code, False);
+      Close (Output_FD);
+
+      if Return_Code /= 0 then
+         return;
+      end if;
 
-      --  Mark project as Seen to avoid endless loop caused by limited withs
+      --  Parse the GCC driver output which is a single line, removing CR/LF
 
-      Project_Tree.Projects.Table (Project).Seen := True;
+      Output_FD := Open_Read (Output_Name.all, Binary);
 
-      List := Project_Tree.Projects.Table (Project).Imported_Projects;
+      if Output_FD = Invalid_FD then
+         return;
+      end if;
 
-      --  Visit each imported project
+      N_Read := Read (Output_FD, Line (1)'Address, Line'Length);
+      Close (Output_FD);
+      Delete_File (Output_Name.all, Success);
 
-      while List /= Empty_Project_List loop
-         Proj := Project_Tree.Project_Lists.Table (List).Project;
-         List := Project_Tree.Project_Lists.Table (List).Next;
-         Recursive_Compute_Depth
-           (Project => Proj,
-            Depth => Depth + 1);
+      for J in reverse 1 .. N_Read loop
+         if Line (J) = ASCII.CR or else Line (J) = ASCII.LF then
+            N_Read := N_Read - 1;
+         else
+            exit;
+         end if;
       end loop;
 
-      --  Visit a project being extended, if any
+      --  In case the standard RTS is selected do nothing
+
+      if N_Read = 0 or else Line (1 .. N_Read) = "." then
+         return;
+      end if;
+
+      --  Otherwise add -margs --RTS=output
+
+      Scan_Make_Arg (Project_Node_Tree, "-margs", And_Save => True);
+      Scan_Make_Arg
+        (Project_Node_Tree, "--RTS=" & Line (1 .. N_Read), And_Save => True);
+   end Process_Multilib;
 
-      Recursive_Compute_Depth
-        (Project => Project_Tree.Projects.Table (Project).Extends,
-         Depth   => Depth + 1);
+   -----------
+   -- Queue --
+   -----------
+
+   package body Queue is
+
+      type Q_Record is record
+         File      : File_Name_Type;
+         Unit      : Unit_Name_Type;
+         Index     : Int;
+         Project   : Project_Id;
+         Processed : Boolean;
+      end record;
+      --  File is the name of the file to compile. Unit is for gnatdist use in
+      --  order to easily get the unit name of a file to compile when its name
+      --  is krunched or declared in gnat.adc. Index, when not 0, is the index
+      --  of the unit in a multi-unit source.
+
+      package Q is new Table.Table
+        (Table_Component_Type => Q_Record,
+         Table_Index_Type     => Positive,
+         Table_Low_Bound      => 1,
+         Table_Initial        => 4000,
+         Table_Increment      => 100,
+         Table_Name           => "Make.Queue.Q");
+      --  This is the actual Q
+
+      package Busy_Obj_Dirs is new GNAT.HTable.Simple_HTable
+        (Header_Num => Prj.Header_Num,
+         Element    => Boolean,
+         No_Element => False,
+         Key        => Path_Name_Type,
+         Hash       => Hash,
+         Equal      => "=");
+
+      Q_First : Natural := 1;
+      --  Points to the first valid element in the queue
+
+      Q_Processed           : Natural := 0;
+      One_Queue_Per_Obj_Dir : Boolean := False;
+      Q_Initialized         : Boolean := False;
+
+      -------------
+      -- Element --
+      -------------
+
+      function Element (Rank : Positive) return File_Name_Type is
+      begin
+         if Rank <= Q.Last then
+            return Q.Table (Rank).File;
+         else
+            return No_File;
+         end if;
+      end Element;
+
+      -------------
+      -- Extract --
+      -------------
+
+      --  This body needs commenting ???
+
+      procedure Extract
+        (Source_File_Name : out File_Name_Type;
+         Source_Unit      : out Unit_Name_Type;
+         Source_Index     : out Int)
+      is
+         Found : Boolean := False;
+
+      begin
+         if One_Queue_Per_Obj_Dir then
+            for J in Q_First .. Q.Last loop
+               if not Q.Table (J).Processed
+                 and then (Q.Table (J).Project = No_Project
+                            or else not
+                              Busy_Obj_Dirs.Get
+                                (Q.Table (J).Project.Object_Directory.Name))
+               then
+                  Found := True;
+                  Source_File_Name := Q.Table (J).File;
+                  Source_Unit      := Q.Table (J).Unit;
+                  Source_Index     := Q.Table (J).Index;
+                  Q.Table (J).Processed := True;
+
+                  if J = Q_First then
+                     while Q_First <= Q.Last
+                       and then Q.Table (Q_First).Processed
+                     loop
+                        Q_First := Q_First + 1;
+                     end loop;
+                  end if;
+
+                  exit;
+               end if;
+            end loop;
+
+         elsif Q_First <= Q.Last then
+            Source_File_Name := Q.Table (Q_First).File;
+            Source_Unit      := Q.Table (Q_First).Unit;
+            Source_Index     := Q.Table (Q_First).Index;
+            Q.Table (Q_First).Processed := True;
+            Q_First := Q_First + 1;
+            Found := True;
+         end if;
+
+         if Found then
+            Q_Processed := Q_Processed + 1;
+         else
+            Source_File_Name := No_File;
+            Source_Unit      := No_Unit_Name;
+            Source_Index     := 0;
+         end if;
+
+         if Found and then Debug.Debug_Flag_Q then
+            Write_Str ("   Q := Q - [ ");
+            Write_Name (Source_File_Name);
+
+            if Source_Index /= 0 then
+               Write_Str (", ");
+               Write_Int (Source_Index);
+            end if;
+
+            Write_Str (" ]");
+            Write_Eol;
+
+            Write_Str ("   Q_First =");
+            Write_Int (Int (Q_First));
+            Write_Eol;
+
+            Write_Str ("   Q.Last =");
+            Write_Int (Int (Q.Last));
+            Write_Eol;
+         end if;
+      end Extract;
 
-      --  Reset the Seen flag, as we leave this project
+      ----------------
+      -- Initialize --
+      ----------------
+
+      procedure Initialize (Queue_Per_Obj_Dir : Boolean) is
+      begin
+         if not Q_Initialized then
+            One_Queue_Per_Obj_Dir := Queue_Per_Obj_Dir;
+            Q.Init;
+            Q_Initialized := True;
+            Q_Processed   := 0;
+            Q_First       := 1;
+         end if;
+      end Initialize;
 
-      Project_Tree.Projects.Table (Project).Seen := False;
+      ------------
+      -- Insert --
+      ------------
+
+      --  This body needs commenting ???
+
+      procedure Insert
+        (Source_File_Name : File_Name_Type;
+         Project          : Project_Id;
+         Source_Unit      : Unit_Name_Type := No_Unit_Name;
+         Index            : Int            := 0)
+      is
+      begin
+         Q.Append
+           ((File      => Source_File_Name,
+             Project   => Project,
+             Unit      => Source_Unit,
+             Index     => Index,
+             Processed => False));
+
+         if Debug.Debug_Flag_Q then
+            Write_Str ("   Q := Q + [ ");
+            Write_Name (Source_File_Name);
+
+            if Index /= 0 then
+               Write_Str (", ");
+               Write_Int (Index);
+            end if;
+
+            Write_Str (" ] ");
+            Write_Eol;
+
+            Write_Str ("   Q_First =");
+            Write_Int (Int (Q_First));
+            Write_Eol;
+
+            Write_Str ("   Q.Last =");
+            Write_Int (Int (Q.Last));
+            Write_Eol;
+         end if;
+      end Insert;
+
+      --------------
+      -- Is_Empty --
+      --------------
+
+      function Is_Empty return Boolean is
+      begin
+         if Debug.Debug_Flag_P then
+            Write_Str ("   Q := [");
+
+            for J in Q_First .. Q.Last loop
+               if not Q.Table (J).Processed then
+                  Write_Str (" ");
+                  Write_Name (Q.Table (J).File);
+                  Write_Eol;
+                  Write_Str ("         ");
+               end if;
+            end loop;
+
+            Write_Str ("]");
+            Write_Eol;
+         end if;
+
+         return Q_First > Q.Last;
+      end Is_Empty;
+
+      ------------------------
+      -- Is_Virtually_Empty --
+      ------------------------
+
+      function Is_Virtually_Empty return Boolean is
+      begin
+         if One_Queue_Per_Obj_Dir then
+            for J in Q_First .. Q.Last loop
+               if not Q.Table (J).Processed
+                 and then
+                   (Q.Table (J).Project = No_Project
+                     or else not
+                       Busy_Obj_Dirs.Get
+                         (Q.Table (J).Project.Object_Directory.Name))
+               then
+                  return False;
+               end if;
+            end loop;
+
+            return True;
+
+         else
+            return Is_Empty;
+         end if;
+      end Is_Virtually_Empty;
+
+      ---------------
+      -- Processed --
+      ---------------
+
+      function Processed return Natural is
+      begin
+         return Q_Processed;
+      end Processed;
+
+      ----------------------
+      -- Set_Obj_Dir_Busy --
+      ----------------------
+
+      procedure Set_Obj_Dir_Busy (Obj_Dir : Path_Name_Type) is
+      begin
+         if One_Queue_Per_Obj_Dir then
+            Busy_Obj_Dirs.Set (Obj_Dir, True);
+         end if;
+      end Set_Obj_Dir_Busy;
+
+      ----------------------
+      -- Set_Obj_Dir_Free --
+      ----------------------
+
+      procedure Set_Obj_Dir_Free (Obj_Dir : Path_Name_Type) is
+      begin
+         if One_Queue_Per_Obj_Dir then
+            Busy_Obj_Dirs.Set (Obj_Dir, False);
+         end if;
+      end Set_Obj_Dir_Free;
+
+      ----------
+      -- Size --
+      ----------
+
+      function Size return Natural is
+      begin
+         return Q.Last;
+      end Size;
+
+   end Queue;
+
+   -----------------------------
+   -- Recursive_Compute_Depth --
+   -----------------------------
+
+   procedure Recursive_Compute_Depth (Project : Project_Id) is
+      use Project_Boolean_Htable;
+      Seen : Project_Boolean_Htable.Instance := Project_Boolean_Htable.Nil;
+
+      procedure Recurse (Prj : Project_Id; Depth : Natural);
+      --  Recursive procedure that does the work, keeping track of the depth
+
+      -------------
+      -- Recurse --
+      -------------
+
+      procedure Recurse (Prj : Project_Id; Depth : Natural) is
+         List : Project_List;
+         Proj : Project_Id;
+
+      begin
+         if Prj.Depth >= Depth or else Get (Seen, Prj) then
+            return;
+         end if;
+
+         --  We need a test to avoid infinite recursions with limited withs:
+         --  If we have A -> B -> A, then when set level of A to n, we try and
+         --  set level of B to n+1, and then level of A to n + 2, ...
+
+         Set (Seen, Prj, True);
+
+         Prj.Depth := Depth;
+
+         --  Visit each imported project
+
+         List := Prj.Imported_Projects;
+         while List /= null loop
+            Proj := List.Project;
+            List := List.Next;
+            Recurse (Prj => Proj, Depth => Depth + 1);
+         end loop;
+
+         --  We again allow changing the depth of this project later on if it
+         --  is in fact imported by a lower-level project.
+
+         Set (Seen, Prj, False);
+      end Recurse;
+
+      Proj : Project_List;
+
+   --  Start of processing for Recursive_Compute_Depth
+
+   begin
+      Proj := Project_Tree.Projects;
+      while Proj /= null loop
+         Proj.Project.Depth := 0;
+         Proj := Proj.Next;
+      end loop;
+
+      Recurse (Project, Depth => 1);
+      Reset (Seen);
    end Recursive_Compute_Depth;
 
    -------------------------------
@@ -7477,6 +7702,7 @@ package body Make is
 
    procedure Sigint_Intercepted is
       SIGINT  : constant := 2;
+
    begin
       Set_Standard_Error;
       Write_Line ("*** Interrupted ***");
@@ -7497,10 +7723,16 @@ package body Make is
    -- Scan_Make_Arg --
    -------------------
 
-   procedure Scan_Make_Arg (Argv : String; And_Save : Boolean) is
+   procedure Scan_Make_Arg
+     (Project_Node_Tree : Project_Node_Tree_Ref;
+      Argv              : String;
+      And_Save          : Boolean)
+   is
       Success : Boolean;
 
    begin
+      Gnatmake_Switch_Found := True;
+
       pragma Assert (Argv'First = 1);
 
       if Argv'Length = 0 then
@@ -7539,7 +7771,7 @@ package body Make is
 
       --  If the previous switch has set the Object_Directory_Present flag
       --  (that is we have seen a -D), then the next argument is the path name
-      --  of the object directory..
+      --  of the object directory.
 
       elsif Object_Directory_Present
         and then not Object_Directory_Seen
@@ -7550,25 +7782,30 @@ package body Make is
             Make_Failed ("object directory path name missing after -D");
 
          elsif not Is_Directory (Argv) then
-            Make_Failed ("cannot find object directory """, Argv, """");
+            Make_Failed ("cannot find object directory """ & Argv & """");
 
          else
-            Add_Lib_Search_Dir (Argv);
+            --  Record the object directory. Make sure it ends with a directory
+            --  separator.
+
+            declare
+               Norm : constant String := Normalize_Pathname (Argv);
+
+            begin
+               if Norm (Norm'Last) = Directory_Separator then
+                  Object_Directory_Path := new String'(Norm);
+               else
+                  Object_Directory_Path :=
+                    new String'(Norm & Directory_Separator);
+               end if;
 
-            --  Specify the object directory to the binder
+               Add_Lib_Search_Dir (Norm);
 
-            Add_Switch ("-aO" & Argv, Binder, And_Save => And_Save);
+               --  Specify the object directory to the binder
 
-            --  Record the object directory. Make sure it ends with a directory
-            --  separator.
+               Add_Switch ("-aO" & Norm, Binder, And_Save => And_Save);
+            end;
 
-            if Argv (Argv'Last) = Directory_Separator then
-               Object_Directory_Path :=
-                 new String'(Argv);
-            else
-               Object_Directory_Path :=
-                 new String'(Argv & Directory_Separator);
-            end if;
          end if;
 
       --  Then check if we are dealing with -cargs/-bargs/-largs/-margs
@@ -7591,9 +7828,8 @@ package body Make is
                raise Program_Error;
          end case;
 
-      --  A special test is needed for the -o switch within a -largs
-      --  since that is another way to specify the name of the final
-      --  executable.
+      --  A special test is needed for the -o switch within a -largs since that
+      --  is another way to specify the name of the final executable.
 
       elsif Program_Args = Linker
         and then Argv = "-o"
@@ -7601,8 +7837,8 @@ package body Make is
          Make_Failed ("switch -o not allowed within a -largs. " &
                       "Use -o directly.");
 
-      --  Check to see if we are reading switches after a -cargs,
-      --  -bargs or -largs switch. If yes save it.
+      --  Check to see if we are reading switches after a -cargs, -bargs or
+      --  -largs switch. If so, save it.
 
       elsif Program_Args /= None then
 
@@ -7645,9 +7881,7 @@ package body Make is
 
                for J in 2 .. Program_Args.all'Last loop
                   Add_Switch
-                    (Program_Args.all (J).all,
-                     Compiler,
-                     And_Save => And_Save);
+                    (Program_Args.all (J).all, Compiler, And_Save => And_Save);
                end loop;
             end;
 
@@ -7695,7 +7929,7 @@ package body Make is
            Argv (1 .. 5) = "--RTS"
          then
             Add_Switch (Argv, Compiler, And_Save => And_Save);
-            Add_Switch (Argv, Binder, And_Save => And_Save);
+            Add_Switch (Argv, Binder,   And_Save => And_Save);
 
             if Argv'Length <= 6 or else Argv (6) /= '=' then
                Make_Failed ("missing path for --RTS");
@@ -7737,8 +7971,8 @@ package body Make is
                      RTS_Src_Path_Name := Src_Path_Name;
                      RTS_Lib_Path_Name := Lib_Path_Name;
 
-                  elsif  Src_Path_Name = null
-                    and Lib_Path_Name = null
+                  elsif Src_Path_Name = null
+                    and then Lib_Path_Name = null
                   then
                      Make_Failed ("RTS path not valid: missing " &
                                   "adainclude and adalib directories");
@@ -7754,8 +7988,33 @@ package body Make is
                end;
             end if;
 
+         elsif Argv'Length > Source_Info_Option'Length and then
+           Argv (1 .. Source_Info_Option'Length) = Source_Info_Option
+         then
+            Project_Tree.Source_Info_File_Name :=
+              new String'(Argv (Source_Info_Option'Length + 1 .. Argv'Last));
+
+         elsif Argv'Length >= 8 and then
+           Argv (1 .. 8) = "--param="
+         then
+            Add_Switch (Argv, Compiler, And_Save => And_Save);
+            Add_Switch (Argv, Linker,   And_Save => And_Save);
+
+         elsif Argv = Create_Map_File_Switch then
+            Map_File := new String'("");
+
+         elsif Argv'Length > Create_Map_File_Switch'Length + 1
+           and then
+             Argv (1 .. Create_Map_File_Switch'Length) = Create_Map_File_Switch
+           and then
+             Argv (Create_Map_File_Switch'Length + 1) = '='
+         then
+            Map_File :=
+              new String'
+                (Argv (Create_Map_File_Switch'Length + 2 .. Argv'Last));
+
          else
-            Scan_Make_Switches (Argv, Success);
+            Scan_Make_Switches (Project_Node_Tree, Argv, Success);
          end if;
 
       --  If we have seen a regular switch process it
@@ -7768,10 +8027,11 @@ package body Make is
 
          elsif     (Argv'Length > 5  and then Argv (1 .. 5) = "-RTS=")
            or else (Argv'Length > 5  and then Argv (1 .. 5) = "-GCC=")
+           or else (Argv'Length > 8  and then Argv (1 .. 7) = "-param=")
            or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATLINK=")
            or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATBIND=")
          then
-            Make_Failed ("option ", Argv, " should start with '--'");
+            Make_Failed ("option " & Argv & " should start with '--'");
 
          --  -I-
 
@@ -7783,23 +8043,23 @@ package body Make is
          elsif (Argv'Length = 3 and then Argv (3) = '-')
            or else (Argv'Length = 4 and then Argv (4) = '-')
          then
-            Make_Failed ("trailing ""-"" at the end of ", Argv, " forbidden.");
+            Make_Failed
+              ("trailing ""-"" at the end of " & Argv & " forbidden.");
 
          --  -Idir
 
          elsif Argv (2) = 'I' then
-            Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
+            Add_Source_Search_Dir  (Argv (3 .. Argv'Last), And_Save);
             Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
             Add_Switch (Argv, Compiler, And_Save => And_Save);
-            Add_Switch (Argv, Binder, And_Save => And_Save);
+            Add_Switch (Argv, Binder,   And_Save => And_Save);
 
          --  -aIdir (to gcc this is like a -I switch)
 
          elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
             Add_Source_Search_Dir (Argv (4 .. Argv'Last), And_Save);
-            Add_Switch ("-I" & Argv (4 .. Argv'Last),
-                        Compiler,
-                        And_Save => And_Save);
+            Add_Switch
+              ("-I" & Argv (4 .. Argv'Last), Compiler, And_Save => And_Save);
             Add_Switch (Argv, Binder, And_Save => And_Save);
 
          --  -aOdir
@@ -7813,9 +8073,8 @@ package body Make is
          elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
             Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir, And_Save);
             Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
-            Add_Switch ("-aO" & Argv (4 .. Argv'Last),
-                        Binder,
-                        And_Save => And_Save);
+            Add_Switch
+              ("-aO" & Argv (4 .. Argv'Last), Binder, And_Save => And_Save);
 
          --  -aamp_target=...
 
@@ -7833,26 +8092,24 @@ package body Make is
 
          elsif Argv (2) = 'A' then
             Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir, And_Save);
-            Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
+            Add_Source_Search_Dir  (Argv (3 .. Argv'Last), And_Save);
             Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
-            Add_Switch ("-I"  & Argv (3 .. Argv'Last),
-                        Compiler,
-                        And_Save => And_Save);
-            Add_Switch ("-aO" & Argv (3 .. Argv'Last),
-                        Binder,
-                        And_Save => And_Save);
+            Add_Switch
+              ("-I"  & Argv (3 .. Argv'Last), Compiler, And_Save => And_Save);
+            Add_Switch
+              ("-aO" & Argv (3 .. Argv'Last), Binder,   And_Save => And_Save);
 
          --  -Ldir
 
          elsif Argv (2) = 'L' then
             Add_Switch (Argv, Linker, And_Save => And_Save);
 
-         --  For -gxxxxx, -pg, -mxxx, -fxxx: give the switch to both the
-         --  compiler and the linker (except for -gnatxxx which is only for
-         --  the compiler). Some of the -mxxx (for example -m64) and -fxxx
-         --  (for example -ftest-coverage for gcov) need to be used when
-         --  compiling the binder generated files, and using all these gcc
-         --  switches for the binder generated files should not be a problem.
+         --  For -gxxx, -pg, -mxxx, -fxxx, -Oxxx, pass the switch to both the
+         --  compiler and the linker (except for -gnatxxx which is only for the
+         --  compiler). Some of the -mxxx (for example -m64) and -fxxx (for
+         --  example -ftest-coverage for gcov) need to be used when compiling
+         --  the binder generated files, and using all these gcc switches for
+         --  them should not be a problem. Pass -Oxxx to the linker for LTO.
 
          elsif
            (Argv (2) = 'g' and then (Argv'Last < 5
@@ -7860,16 +8117,26 @@ package body Make is
              or else Argv (2 .. Argv'Last) = "pg"
              or else (Argv (2) = 'm' and then Argv'Last > 2)
              or else (Argv (2) = 'f' and then Argv'Last > 2)
+             or else (Argv (2) = 'O' and then Argv'Last > 2)
          then
             Add_Switch (Argv, Compiler, And_Save => And_Save);
-            Add_Switch (Argv, Linker, And_Save => And_Save);
+            Add_Switch (Argv, Linker,   And_Save => And_Save);
+
+            --  The following condition has to be kept synchronized with
+            --  the Process_Multilib one.
+
+            if Argv (2) = 'm'
+              and then Argv /= "-mieee"
+            then
+               N_M_Switch := N_M_Switch + 1;
+            end if;
 
          --  -C=<mapping file>
 
          elsif Argv'Last > 2 and then Argv (2) = 'C' then
             if And_Save then
                if Argv (3) /= '=' or else Argv'Last <= 3 then
-                  Make_Failed ("illegal switch ", Argv);
+                  Make_Failed ("illegal switch " & Argv);
                end if;
 
                Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
@@ -7879,34 +8146,32 @@ package body Make is
 
          elsif Argv'Last = 2 and then Argv (2) = 'D' then
             if Project_File_Name /= null then
-               Make_Failed ("-D cannot be used in conjunction with a " &
-                            "project file");
+               Make_Failed
+                 ("-D cannot be used in conjunction with a project file");
 
             else
-               Scan_Make_Switches (Argv, Success);
+               Scan_Make_Switches (Project_Node_Tree, Argv, Success);
             end if;
 
          --  -d
 
-         elsif Argv (2) = 'd'
-           and then Argv'Last = 2
-         then
+         elsif Argv (2) = 'd' and then Argv'Last = 2 then
             Display_Compilation_Progress := True;
 
          --  -i
 
          elsif Argv'Last = 2 and then Argv (2) = 'i' then
             if Project_File_Name /= null then
-               Make_Failed ("-i cannot be used in conjunction with a " &
-                            "project file");
+               Make_Failed
+                 ("-i cannot be used in conjunction with a project file");
             else
-               Scan_Make_Switches (Argv, Success);
+               Scan_Make_Switches (Project_Node_Tree, Argv, Success);
             end if;
 
          --  -j (need to save the result)
 
          elsif Argv (2) = 'j' then
-            Scan_Make_Switches (Argv, Success);
+            Scan_Make_Switches (Project_Node_Tree, Argv, Success);
 
             if And_Save then
                Saved_Maximum_Processes := Maximum_Processes;
@@ -7914,20 +8179,16 @@ package body Make is
 
          --  -m
 
-         elsif Argv (2) = 'm'
-           and then Argv'Last = 2
-         then
+         elsif Argv (2) = 'm' and then Argv'Last = 2 then
             Minimal_Recompilation := True;
 
          --  -u
 
-         elsif Argv (2) = 'u'
-           and then Argv'Last = 2
-         then
-            Unique_Compile   := True;
-            Compile_Only := True;
-            Do_Bind_Step     := False;
-            Do_Link_Step     := False;
+         elsif Argv (2) = 'u' and then Argv'Last = 2 then
+            Unique_Compile := True;
+            Compile_Only   := True;
+            Do_Bind_Step   := False;
+            Do_Link_Step   := False;
 
          --  -U
 
@@ -7935,10 +8196,10 @@ package body Make is
            and then Argv'Last = 2
          then
             Unique_Compile_All_Projects := True;
-            Unique_Compile   := True;
-            Compile_Only := True;
-            Do_Bind_Step     := False;
-            Do_Link_Step     := False;
+            Unique_Compile := True;
+            Compile_Only   := True;
+            Do_Bind_Step   := False;
+            Do_Link_Step   := False;
 
          --  -Pprj or -P prj (only once, and only on the command line)
 
@@ -7947,16 +8208,16 @@ package body Make is
                Make_Failed ("cannot have several project files specified");
 
             elsif Object_Directory_Path /= null then
-               Make_Failed ("-D cannot be used in conjunction with a " &
-                            "project file");
+               Make_Failed
+                 ("-D cannot be used in conjunction with a project file");
 
             elsif In_Place_Mode then
-               Make_Failed ("-i cannot be used in conjunction with a " &
-                            "project file");
+               Make_Failed
+                 ("-i cannot be used in conjunction with a project file");
 
             elsif not And_Save then
 
-               --  It could be a tool other than gnatmake (i.e, gnatdist)
+               --  It could be a tool other than gnatmake (e.g. gnatdist)
                --  or a -P switch inside a project file.
 
                Fail
@@ -7995,43 +8256,40 @@ package body Make is
          --  -Xext=val  (External assignment)
 
          elsif Argv (2) = 'X'
-           and then Is_External_Assignment (Argv)
+           and then Is_External_Assignment (Project_Node_Tree, Argv)
          then
-            --  Is_External_Assignment has side effects
-            --  when it returns True;
+            --  Is_External_Assignment has side effects when it returns True
 
             null;
 
-         --  If -gnath is present, then generate the usage information
-         --  right now and do not pass this option on to the compiler calls.
+         --  If -gnath is present, then generate the usage information right
+         --  now and do not pass this option on to the compiler calls.
 
          elsif Argv = "-gnath" then
             Usage;
 
-         --  If -gnatc is specified, make sure the bind step and the link
-         --  step are not executed.
+         --  If -gnatc is specified, make sure the bind and link steps are not
+         --  executed.
 
          elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
 
-            --  If -gnatc is specified, make sure the bind step and the link
-            --  step are not executed.
+            --  If -gnatc is specified, make sure the bind and link steps are
+            --  not executed.
 
             Add_Switch (Argv, Compiler, And_Save => And_Save);
-            Operating_Mode := Check_Semantics;
+            Operating_Mode           := Check_Semantics;
             Check_Object_Consistency := False;
             Compile_Only             := True;
-            Do_Bind_Step                 := False;
-            Do_Link_Step                 := False;
+            Do_Bind_Step             := False;
+            Do_Link_Step             := False;
 
          elsif Argv (2 .. Argv'Last) = "nostdlib" then
 
-            --  Don't pass -nostdlib to gnatlink, it will disable
-            --  linking with all standard library files.
+            --  Pass -nstdlib to gnatbind and gnatlink
 
             No_Stdlib := True;
-
-            Add_Switch (Argv, Compiler, And_Save => And_Save);
             Add_Switch (Argv, Binder, And_Save => And_Save);
+            Add_Switch (Argv, Linker, And_Save => And_Save);
 
          elsif Argv (2 .. Argv'Last) = "nostdinc" then
 
@@ -8039,16 +8297,17 @@ package body Make is
 
             No_Stdinc := True;
             Add_Switch (Argv, Compiler, And_Save => And_Save);
-            Add_Switch (Argv, Binder, And_Save => And_Save);
+            Add_Switch (Argv, Binder,   And_Save => And_Save);
 
-         --  All other switches are processed by Scan_Make_Switches.
-         --  If the call returns with Success = False, then the switch is
-         --  passed to the compiler.
+         --  All other switches are processed by Scan_Make_Switches. If the
+         --  call returns with Gnatmake_Switch_Found = False, then the switch
+         --  is passed to the compiler.
 
          else
-            Scan_Make_Switches (Argv, Success);
+            Scan_Make_Switches
+              (Project_Node_Tree, Argv, Gnatmake_Switch_Found);
 
-            if not Success then
+            if not Gnatmake_Switch_Found then
                Add_Switch (Argv, Compiler, And_Save => And_Save);
             end if;
          end if;
@@ -8056,6 +8315,10 @@ package body Make is
       --  If not a switch it must be a file name
 
       else
+         if And_Save then
+            Main_On_Command_Line := True;
+         end if;
+
          Add_File (Argv);
          Mains.Add_Main (Argv);
       end if;
@@ -8069,10 +8332,12 @@ package body Make is
      (Source_File      : File_Name_Type;
       Source_File_Name : String;
       Source_Index     : Int;
-      Naming           : Naming_Data;
+      Project          : Project_Id;
       In_Package       : Package_Id;
       Allow_ALI        : Boolean) return Variable_Value
    is
+      Lang : constant Language_Ptr := Get_Language_From_Name (Project, "ada");
+
       Switches : Variable_Value;
 
       Defaults : constant Array_Element_Id :=
@@ -8092,21 +8357,29 @@ package body Make is
                             In_Tree   => Project_Tree);
 
    begin
+      --  First, try Switches (<file name>)
+
       Switches :=
         Prj.Util.Value_Of
-          (Index     => Name_Id (Source_File),
-           Src_Index => Source_Index,
-           In_Array  => Switches_Array,
-           In_Tree   => Project_Tree);
+          (Index           => Name_Id (Source_File),
+           Src_Index       => Source_Index,
+           In_Array        => Switches_Array,
+           In_Tree         => Project_Tree,
+           Allow_Wildcards => True);
 
-      if Switches = Nil_Variable_Value then
+      --  Check also without the suffix
+
+      if Switches = Nil_Variable_Value
+        and then Lang /= null
+      then
          declare
+            Naming      : Lang_Naming_Data renames Lang.Config.Naming_Data;
             Name        : String (1 .. Source_File_Name'Length + 3);
             Last        : Positive := Source_File_Name'Length;
             Spec_Suffix : constant String :=
-                            Spec_Suffix_Of (Project_Tree, "ada", Naming);
+                            Get_Name_String (Naming.Spec_Suffix);
             Body_Suffix : constant String :=
-                            Body_Suffix_Of (Project_Tree, "ada", Naming);
+                            Get_Name_String (Naming.Body_Suffix);
             Truncated   : Boolean := False;
 
          begin
@@ -8130,27 +8403,27 @@ package body Make is
             end if;
 
             if Truncated then
-               Name_Len := Last;
-               Name_Buffer (1 .. Name_Len) := Name (1 .. Last);
+               Name_Len := 0;
+               Add_Str_To_Name_Buffer (Name (1 .. Last));
                Switches :=
                  Prj.Util.Value_Of
-                   (Index     => Name_Find,
-                    Src_Index => 0,
-                    In_Array  => Switches_Array,
-                    In_Tree   => Project_Tree);
+                   (Index           => Name_Find,
+                    Src_Index       => 0,
+                    In_Array        => Switches_Array,
+                    In_Tree         => Project_Tree,
+                    Allow_Wildcards => True);
 
-               if Switches = Nil_Variable_Value
-                 and then Allow_ALI
-               then
+               if Switches = Nil_Variable_Value and then Allow_ALI then
                   Last := Source_File_Name'Length;
 
                   while Name (Last) /= '.' loop
                      Last := Last - 1;
                   end loop;
 
-                  Name (Last + 1 .. Last + 3) := "ali";
-                  Name_Len := Last + 3;
-                  Name_Buffer (1 .. Name_Len) := Name (1 .. Name_Len);
+                  Name_Len := 0;
+                  Add_Str_To_Name_Buffer (Name (1 .. Last));
+                  Add_Str_To_Name_Buffer ("ali");
+
                   Switches :=
                     Prj.Util.Value_Of
                       (Index     => Name_Find,
@@ -8162,6 +8435,39 @@ package body Make is
          end;
       end if;
 
+      --  Next, try Switches ("Ada")
+
+      if Switches = Nil_Variable_Value then
+         Switches :=
+           Prj.Util.Value_Of
+             (Index                  => Name_Ada,
+              Src_Index              => 0,
+              In_Array               => Switches_Array,
+              In_Tree                => Project_Tree,
+              Force_Lower_Case_Index => True);
+
+         if Switches /= Nil_Variable_Value then
+            Switch_May_Be_Passed_To_The_Compiler := False;
+         end if;
+      end if;
+
+      --  Next, try Switches (others)
+
+      if Switches = Nil_Variable_Value then
+         Switches :=
+           Prj.Util.Value_Of
+             (Index     => All_Other_Names,
+              Src_Index => 0,
+              In_Array  => Switches_Array,
+              In_Tree   => Project_Tree);
+
+         if Switches /= Nil_Variable_Value then
+            Switch_May_Be_Passed_To_The_Compiler := False;
+         end if;
+      end if;
+
+      --  And finally, Default_Switches ("Ada")
+
       if Switches = Nil_Variable_Value then
          Switches :=
            Prj.Util.Value_Of
@@ -8186,52 +8492,6 @@ package body Make is
       end if;
    end Usage;
 
-   -----------------
-   -- Verbose_Msg --
-   -----------------
-
-   procedure Verbose_Msg
-     (N1                : Name_Id;
-      S1                : String;
-      N2                : Name_Id := No_Name;
-      S2                : String  := "";
-      Prefix            : String := "  -> ";
-      Minimum_Verbosity : Verbosity_Level_Type := Opt.Low)
-   is
-   begin
-      if (not Verbose_Mode) or else (Minimum_Verbosity > Verbosity_Level) then
-         return;
-      end if;
-
-      Write_Str (Prefix);
-      Write_Str ("""");
-      Write_Name (N1);
-      Write_Str (""" ");
-      Write_Str (S1);
-
-      if N2 /= No_Name then
-         Write_Str (" """);
-         Write_Name (N2);
-         Write_Str (""" ");
-      end if;
-
-      Write_Str (S2);
-      Write_Eol;
-   end Verbose_Msg;
-
-   procedure Verbose_Msg
-     (N1                : File_Name_Type;
-      S1                : String;
-      N2                : File_Name_Type := No_File;
-      S2                : String  := "";
-      Prefix            : String := "  -> ";
-      Minimum_Verbosity : Verbosity_Level_Type := Opt.Low)
-   is
-   begin
-      Verbose_Msg
-        (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);
-   end Verbose_Msg;
-
 begin
    --  Make sure that in case of failure, the temp files will be deleted