OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / make.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                 M A K E                                  --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with ALI;      use ALI;
28 with ALI.Util; use ALI.Util;
29 with Csets;
30 with Debug;
31 with Fmap;
32 with Fname;    use Fname;
33 with Fname.SF; use Fname.SF;
34 with Fname.UF; use Fname.UF;
35 with Gnatvsn;  use Gnatvsn;
36 with Hostparm; use Hostparm;
37 with Makeusg;
38 with Makeutl;  use Makeutl;
39 with MLib.Prj;
40 with MLib.Tgt; use MLib.Tgt;
41 with MLib.Utl;
42 with Namet;    use Namet;
43 with Opt;      use Opt;
44 with Osint.M;  use Osint.M;
45 with Osint;    use Osint;
46 with Output;   use Output;
47 with Prj;      use Prj;
48 with Prj.Com;
49 with Prj.Env;
50 with Prj.Pars;
51 with Prj.Util;
52 with SFN_Scan;
53 with Sinput.P;
54 with Snames;   use Snames;
55 with Switch;   use Switch;
56 with Switch.M; use Switch.M;
57 with Targparm;
58 with Tempdir;
59
60 with Ada.Exceptions;            use Ada.Exceptions;
61 with Ada.Command_Line;          use Ada.Command_Line;
62
63 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
64 with GNAT.Case_Util;            use GNAT.Case_Util;
65
66 with System.HTable;
67
68 package body Make is
69
70    use ASCII;
71    --  Make control characters visible
72
73    Standard_Library_Package_Body_Name : constant String := "s-stalib.adb";
74    --  Every program depends on this package, that must then be checked,
75    --  especially when -f and -a are used.
76
77    type Sigint_Handler is access procedure;
78
79    procedure Install_Int_Handler (Handler : Sigint_Handler);
80    pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
81    --  Called by Gnatmake to install the SIGINT handler below
82
83    procedure Sigint_Intercepted;
84    --  Called when the program is interrupted by Ctrl-C to delete the
85    --  temporary mapping files and configuration pragmas files.
86
87    -------------------------
88    -- Note on terminology --
89    -------------------------
90
91    --  In this program, we use the phrase "termination" of a file name to
92    --  refer to the suffix that appears after the unit name portion. Very
93    --  often this is simply the extension, but in some cases, the sequence
94    --  may be more complex, for example in main.1.ada, the termination in
95    --  this name is ".1.ada" and in main_.ada the termination is "_.ada".
96
97    -------------------------------------
98    -- Queue (Q) Manipulation Routines --
99    -------------------------------------
100
101    --  The Q is used in Compile_Sources below. Its implementation uses the
102    --  GNAT generic package Table (basically an extensible array). Q_Front
103    --  points to the first valid element in the Q, whereas Q.First is the first
104    --  element ever enqueued, while Q.Last - 1 is the last element in the Q.
105    --
106    --        +---+--------------+---+---+---+-----------+---+--------
107    --    Q   |   |  ........    |   |   |   | .......   |   |
108    --        +---+--------------+---+---+---+-----------+---+--------
109    --          ^                  ^                       ^
110    --       Q.First             Q_Front               Q.Last - 1
111    --
112    --  The elements comprised between Q.First and Q_Front - 1 are the
113    --  elements that have been enqueued and then dequeued, while the
114    --  elements between Q_Front and Q.Last - 1 are the elements currently
115    --  in the Q. When the Q is initialized Q_Front = Q.First = Q.Last.
116    --  After Compile_Sources has terminated its execution, Q_Front = Q.Last
117    --  and the elements contained between Q.Front and Q.Last-1 are those that
118    --  were explored and thus marked by Compile_Sources. Whenever the Q is
119    --  reinitialized, the elements between Q.First and Q.Last - 1 are unmarked.
120
121    procedure Init_Q;
122    --  Must be called to (re)initialize the Q
123
124    procedure Insert_Q
125      (Source_File : File_Name_Type;
126       Source_Unit : Unit_Name_Type := No_Name;
127       Index       : Int            := 0);
128    --  Inserts Source_File at the end of Q. Provide Source_Unit when possible
129    --  for external use (gnatdist). Provide index for multi-unit sources.
130
131    function Empty_Q return Boolean;
132    --  Returns True if Q is empty
133
134    procedure Extract_From_Q
135      (Source_File  : out File_Name_Type;
136       Source_Unit  : out Unit_Name_Type;
137       Source_Index : out Int);
138    --  Extracts the first element from the Q
139
140    procedure Insert_Project_Sources
141      (The_Project  : Project_Id;
142       All_Projects : Boolean;
143       Into_Q       : Boolean);
144    --  If Into_Q is True, insert all sources of the project file(s) that are
145    --  not already marked into the Q. If Into_Q is False, call Osint.Add_File
146    --  for the first source, then insert all other sources that are not already
147    --  marked into the Q. If All_Projects is True, all sources of all projects
148    --  are concerned; otherwise, only sources of The_Project are concerned,
149    --  including, if The_Project is an extending project, sources inherited
150    --  from projects being extended.
151
152    First_Q_Initialization : Boolean := True;
153    --  Will be set to false after Init_Q has been called once
154
155    Q_Front : Natural;
156    --  Points to the first valid element in the Q
157
158    Unique_Compile : Boolean := False;
159    --  Set to True if -u or -U or a project file with no main is used
160
161    Unique_Compile_All_Projects : Boolean := False;
162    --  Set to True if -U is used
163
164    RTS_Specified : String_Access := null;
165    --  Used to detect multiple --RTS= switches
166
167    type Q_Record is record
168       File  : File_Name_Type;
169       Unit  : Unit_Name_Type;
170       Index : Int;
171    end record;
172    --  File is the name of the file to compile. Unit is for gnatdist
173    --  use in order to easily get the unit name of a file to compile
174    --  when its name is krunched or declared in gnat.adc. Index, when not 0,
175    --  is the index of the unit in a multi-unit source.
176
177    package Q is new Table.Table (
178      Table_Component_Type => Q_Record,
179      Table_Index_Type     => Natural,
180      Table_Low_Bound      => 0,
181      Table_Initial        => 4000,
182      Table_Increment      => 100,
183      Table_Name           => "Make.Q");
184    --  This is the actual Q
185
186    --  The following instantiations and variables are necessary to save what
187    --  is found on the command line, in case there is a project file specified.
188
189    package Saved_Gcc_Switches is new Table.Table (
190      Table_Component_Type => String_Access,
191      Table_Index_Type     => Integer,
192      Table_Low_Bound      => 1,
193      Table_Initial        => 20,
194      Table_Increment      => 100,
195      Table_Name           => "Make.Saved_Gcc_Switches");
196
197    package Saved_Binder_Switches is new Table.Table (
198      Table_Component_Type => String_Access,
199      Table_Index_Type     => Integer,
200      Table_Low_Bound      => 1,
201      Table_Initial        => 20,
202      Table_Increment      => 100,
203      Table_Name           => "Make.Saved_Binder_Switches");
204
205    package Saved_Linker_Switches is new Table.Table
206      (Table_Component_Type => String_Access,
207       Table_Index_Type     => Integer,
208       Table_Low_Bound      => 1,
209       Table_Initial        => 20,
210       Table_Increment      => 100,
211       Table_Name           => "Make.Saved_Linker_Switches");
212
213    package Switches_To_Check is new Table.Table (
214      Table_Component_Type => String_Access,
215      Table_Index_Type     => Integer,
216      Table_Low_Bound      => 1,
217      Table_Initial        => 20,
218      Table_Increment      => 100,
219      Table_Name           => "Make.Switches_To_Check");
220
221    package Library_Paths is new Table.Table (
222      Table_Component_Type => String_Access,
223      Table_Index_Type     => Integer,
224      Table_Low_Bound      => 1,
225      Table_Initial        => 20,
226      Table_Increment      => 100,
227      Table_Name           => "Make.Library_Paths");
228
229    package Failed_Links is new Table.Table (
230      Table_Component_Type => File_Name_Type,
231      Table_Index_Type     => Integer,
232      Table_Low_Bound      => 1,
233      Table_Initial        => 10,
234      Table_Increment      => 100,
235      Table_Name           => "Make.Failed_Links");
236
237    package Successful_Links is new Table.Table (
238      Table_Component_Type => File_Name_Type,
239      Table_Index_Type     => Integer,
240      Table_Low_Bound      => 1,
241      Table_Initial        => 10,
242      Table_Increment      => 100,
243      Table_Name           => "Make.Successful_Links");
244
245    package Library_Projs is new Table.Table (
246      Table_Component_Type => Project_Id,
247      Table_Index_Type     => Integer,
248      Table_Low_Bound      => 1,
249      Table_Initial        => 10,
250      Table_Increment      => 100,
251      Table_Name           => "Make.Library_Projs");
252
253    --  Two variables to keep the last binder and linker switch index
254    --  in tables Binder_Switches and Linker_Switches, before adding
255    --  switches from the project file (if any) and switches from the
256    --  command line (if any).
257
258    Last_Binder_Switch : Integer := 0;
259    Last_Linker_Switch : Integer := 0;
260
261    Normalized_Switches : Argument_List_Access := new Argument_List (1 .. 10);
262    Last_Norm_Switch    : Natural := 0;
263
264    Saved_Maximum_Processes : Natural := 0;
265
266    type Arg_List_Ref is access Argument_List;
267    The_Saved_Gcc_Switches : Arg_List_Ref;
268
269    Project_File_Name : String_Access  := null;
270    --  The path name of the main project file, if any
271
272    Project_File_Name_Present : Boolean := False;
273    --  True when -P is used with a space between -P and the project file name
274
275    Current_Verbosity : Prj.Verbosity  := Prj.Default;
276    --  Verbosity to parse the project files
277
278    Project_Tree : constant Project_Tree_Ref := new Project_Tree_Data;
279
280    Main_Project : Prj.Project_Id := No_Project;
281    --  The project id of the main project file, if any
282
283    Project_Object_Directory : Project_Id := No_Project;
284    --  The object directory of the project for the last compilation.
285    --  Avoid calling Change_Dir if the current working directory is already
286    --  this directory
287
288    --  Packages of project files where unknown attributes are errors
289
290    Naming_String   : aliased String := "naming";
291    Builder_String  : aliased String := "builder";
292    Compiler_String : aliased String := "compiler";
293    Binder_String   : aliased String := "binder";
294    Linker_String   : aliased String := "linker";
295
296    Gnatmake_Packages : aliased String_List :=
297      (Naming_String   'Access,
298       Builder_String  'Access,
299       Compiler_String 'Access,
300       Binder_String   'Access,
301       Linker_String   'Access);
302
303    Packages_To_Check_By_Gnatmake : constant String_List_Access :=
304      Gnatmake_Packages'Access;
305
306    procedure Add_Source_Dir (N : String);
307    --  Call Add_Src_Search_Dir.
308    --  Output one line when in verbose mode.
309
310    procedure Add_Source_Directories is
311      new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
312
313    procedure Add_Object_Dir (N : String);
314    --  Call Add_Lib_Search_Dir.
315    --  Output one line when in verbose mode.
316
317    procedure Add_Object_Directories is
318      new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
319
320    procedure Change_To_Object_Directory (Project : Project_Id);
321    --  Change to the object directory of project Project, if this is not
322    --  already the current working directory.
323
324    type Bad_Compilation_Info is record
325       File  : File_Name_Type;
326       Unit  : Unit_Name_Type;
327       Found : Boolean;
328    end record;
329    --  File is the name of the file for which a compilation failed.
330    --  Unit is for gnatdist use in order to easily get the unit name
331    --  of a file when its name is krunched or declared in gnat.adc.
332    --  Found is False if the compilation failed because the file could
333    --  not be found.
334
335    package Bad_Compilation is new Table.Table (
336      Table_Component_Type => Bad_Compilation_Info,
337      Table_Index_Type     => Natural,
338      Table_Low_Bound      => 1,
339      Table_Initial        => 20,
340      Table_Increment      => 100,
341      Table_Name           => "Make.Bad_Compilation");
342    --  Full name of all the source files for which compilation fails
343
344    Do_Compile_Step : Boolean := True;
345    Do_Bind_Step    : Boolean := True;
346    Do_Link_Step    : Boolean := True;
347    --  Flags to indicate what step should be executed.
348    --  Can be set to False with the switches -c, -b and -l.
349    --  These flags are reset to True for each invokation of procedure Gnatmake.
350
351    Shared_String           : aliased String := "-shared";
352    Force_Elab_Flags_String : aliased String := "-F";
353
354    No_Shared_Switch : aliased Argument_List := (1 .. 0 => null);
355    Shared_Switch    : aliased Argument_List := (1 => Shared_String'Access);
356    Bind_Shared      : Argument_List_Access := No_Shared_Switch'Access;
357    --  Switch to added in front of gnatbind switches. By default no switch is
358    --  added. Switch "-shared" is added if there is a non-static Library
359    --  Project File.
360
361    Shared_Libgcc : aliased String := "-shared-libgcc";
362
363    No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
364    Shared_Libgcc_Switch    : aliased Argument_List :=
365                                (1 => Shared_Libgcc'Access);
366    Link_With_Shared_Libgcc : Argument_List_Access :=
367                                No_Shared_Libgcc_Switch'Access;
368
369    procedure Make_Failed (S1 : String; S2 : String := ""; S3 : String := "");
370    --  Delete all temp files created by Gnatmake and call Osint.Fail,
371    --  with the parameter S1, S2 and S3 (see osint.ads).
372    --  This is called from the Prj hierarchy and the MLib hierarchy.
373
374    --------------------------
375    -- Obsolete Executables --
376    --------------------------
377
378    Executable_Obsolete : Boolean := False;
379    --  Executable_Obsolete is initially set to False for each executable,
380    --  and is set to True whenever one of the source of the executable is
381    --  compiled, or has already been compiled for another executable.
382
383    Max_Header : constant := 200;
384    --  This needs a proper comment, it used to say "arbitrary"
385    --  that's not an adequate comment ???
386
387    type Header_Num is range 1 .. Max_Header;
388    --  Header_Num for the hash table Obsoleted below
389
390    function Hash (F : Name_Id) return Header_Num;
391    --  Hash function for the hash table Obsoleted below
392
393    package Obsoleted is new System.HTable.Simple_HTable
394      (Header_Num => Header_Num,
395       Element    => Boolean,
396       No_Element => False,
397       Key        => Name_Id,
398       Hash       => Hash,
399       Equal      => "=");
400    --  A hash table to keep all files that have been compiled, to detect
401    --  if an executable is up to date or not.
402
403    procedure Enter_Into_Obsoleted (F : Name_Id);
404    --  Enter a file name, without directory information, into the has table
405    --  Obsoleted.
406
407    function Is_In_Obsoleted (F : Name_Id) return Boolean;
408    --  Check if a file name, without directory information, has already been
409    --  entered into the hash table Obsoleted.
410
411    type Dependency is record
412       This       : Name_Id;
413       Depends_On : Name_Id;
414    end record;
415    --  Components of table Dependencies below
416
417    package Dependencies is new Table.Table (
418      Table_Component_Type => Dependency,
419      Table_Index_Type     => Integer,
420      Table_Low_Bound      => 1,
421      Table_Initial        => 20,
422      Table_Increment      => 100,
423      Table_Name           => "Make.Dependencies");
424    --  A table to keep dependencies, to be able to decide if an executable
425    --  is obsolete.
426
427    procedure Add_Dependency (S : Name_Id; On : Name_Id);
428    --  Add one entry in table Dependencies
429
430    ----------------------------
431    -- Arguments and Switches --
432    ----------------------------
433
434    Arguments : Argument_List_Access;
435    --  Used to gather the arguments for invocation of the compiler
436
437    Last_Argument : Natural := 0;
438    --  Last index of arguments in Arguments above
439
440    Arguments_Collected : Boolean := False;
441    --  Set to True when the arguments for the next invocation of the compiler
442    --  have been collected.
443
444    Arguments_Project : Project_Id;
445    --  Project id, if any, of the source to be compiled
446
447    Arguments_Path_Name : File_Name_Type;
448    --  Full path of the source to be compiled, when Arguments_Project is not
449    --  No_Project.
450
451    Dummy_Switch : constant String_Access := new String'("- ");
452    --  Used to initialized Prev_Switch in procedure Check
453
454    procedure Add_Arguments (Args : Argument_List);
455    --  Add arguments to global variable Arguments, increasing its size
456    --  if necessary and adjusting Last_Argument.
457
458    function Configuration_Pragmas_Switch
459      (For_Project : Project_Id) return Argument_List;
460    --  Return an argument list of one element, if there is a configuration
461    --  pragmas file to be specified for For_Project,
462    --  otherwise return an empty argument list.
463
464    -------------------
465    -- Misc Routines --
466    -------------------
467
468    procedure List_Depend;
469    --  Prints to standard output the list of object dependencies. This list
470    --  can be used directly in a Makefile. A call to Compile_Sources must
471    --  precede the call to List_Depend. Also because this routine uses the
472    --  ALI files that were originally loaded and scanned by Compile_Sources,
473    --  no additional ALI files should be scanned between the two calls (i.e.
474    --  between the call to Compile_Sources and List_Depend.)
475
476    procedure Inform (N : Name_Id := No_Name; Msg : String);
477    --  Prints out the program name followed by a colon, N and S
478
479    procedure List_Bad_Compilations;
480    --  Prints out the list of all files for which the compilation failed
481
482    procedure Verbose_Msg
483      (N1     : Name_Id;
484       S1     : String;
485       N2     : Name_Id := No_Name;
486       S2     : String  := "";
487       Prefix : String  := "  -> ");
488    --  If the verbose flag (Verbose_Mode) is set then print Prefix to standard
489    --  output followed by N1 and S1. If N2 /= No_Name then N2 is printed after
490    --  S1. S2 is printed last. Both N1 and N2 are printed in quotation marks.
491
492    Usage_Needed : Boolean := True;
493    --  Flag used to make sure Makeusg is call at most once
494
495    procedure Usage;
496    --  Call Makeusg, if Usage_Needed is True.
497    --  Set Usage_Needed to False.
498
499    procedure Debug_Msg (S : String; N : Name_Id);
500    --  If Debug.Debug_Flag_W is set outputs string S followed by name N
501
502    procedure Recursive_Compute_Depth
503      (Project : Project_Id;
504       Depth   : Natural);
505    --  Compute depth of Project and of the projects it depends on
506
507    -----------------------
508    -- Gnatmake Routines --
509    -----------------------
510
511    Gnatmake_Called : Boolean := False;
512    --  Set to True when procedure Gnatmake is called.
513    --  Attempt to delete temporary files is made only when Gnatmake_Called
514    --  is True.
515
516    subtype Lib_Mark_Type is Byte;
517    --  Used in Mark_Directory
518
519    Ada_Lib_Dir : constant Lib_Mark_Type := 1;
520    --  Used to mark a directory as a GNAT lib dir
521
522    --  Note that the notion of GNAT lib dir is no longer used. The code
523    --  related to it has not been removed to give an idea on how to use
524    --  the directory prefix marking mechanism.
525
526    --  An Ada library directory is a directory containing ali and object
527    --  files but no source files for the bodies (the specs can be in the
528    --  same or some other directory). These directories are specified
529    --  in the Gnatmake command line with the switch "-Adir" (to specify the
530    --  spec location -Idir cab be used).  Gnatmake skips the missing sources
531    --  whose ali are in Ada library directories. For an explanation of why
532    --  Gnatmake behaves that way, see the spec of Make.Compile_Sources.
533    --  The directory lookup penalty is incurred every single time this
534    --  routine is called.
535
536    procedure Check_Steps;
537    --  Check what steps (Compile, Bind, Link) must be executed.
538    --  Set the step flags accordingly.
539
540    function In_Ada_Lib_Dir  (File : File_Name_Type) return Boolean;
541    --  Get directory prefix of this file and get lib mark stored in name
542    --  table for this directory. Then check if an Ada lib mark has been set.
543
544    procedure Mark_Directory
545      (Dir  : String;
546       Mark : Lib_Mark_Type);
547    --  Store Dir in name table and set lib mark as name info to identify
548    --  Ada libraries.
549
550    Output_Is_Object : Boolean := True;
551    --  Set to False when using a switch -S for the compiler
552
553    procedure Check_For_S_Switch;
554    --  Set Output_Is_Object to False when the -S switch is used for the
555    --  compiler.
556
557    function Switches_Of
558      (Source_File      : Name_Id;
559       Source_File_Name : String;
560       Source_Index     : Int;
561       Naming           : Naming_Data;
562       In_Package       : Package_Id;
563       Allow_ALI        : Boolean) return Variable_Value;
564    --  Return the switches for the source file in the specified package
565    --  of a project file. If the Source_File ends with a standard GNAT
566    --  extension (".ads" or ".adb"), try first the full name, then the
567    --  name without the extension, then, if Allow_ALI is True, the name with
568    --  the extension ".ali". If there is no switches for either names, try the
569    --  default switches for Ada. If all failed, return No_Variable_Value.
570
571    function Is_In_Object_Directory
572      (Source_File   : File_Name_Type;
573       Full_Lib_File : File_Name_Type) return Boolean;
574    --  Check if, when using a project file, the ALI file is in the project
575    --  directory of the ultimate extending project. If it is not, we ignore
576    --  the fact that this ALI file is read-only.
577
578    ----------------------------------------------------
579    -- Compiler, Binder & Linker Data and Subprograms --
580    ----------------------------------------------------
581
582    Gcc             : String_Access := Program_Name ("gcc");
583    Gnatbind        : String_Access := Program_Name ("gnatbind");
584    Gnatlink        : String_Access := Program_Name ("gnatlink");
585    --  Default compiler, binder, linker programs
586
587    Saved_Gcc       : String_Access := null;
588    Saved_Gnatbind  : String_Access := null;
589    Saved_Gnatlink  : String_Access := null;
590    --  Given by the command line. Will be used, if non null
591
592    Gcc_Path        : String_Access :=
593                        GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
594    Gnatbind_Path   : String_Access :=
595                        GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
596    Gnatlink_Path   : String_Access :=
597                        GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
598    --  Path for compiler, binder, linker programs, defaulted now for gnatdist.
599    --  Changed later if overridden on command line.
600
601    Comp_Flag         : constant String_Access := new String'("-c");
602    Output_Flag       : constant String_Access := new String'("-o");
603    Ada_Flag_1        : constant String_Access := new String'("-x");
604    Ada_Flag_2        : constant String_Access := new String'("ada");
605    No_gnat_adc       : constant String_Access := new String'("-gnatA");
606    GNAT_Flag         : constant String_Access := new String'("-gnatpg");
607    Do_Not_Check_Flag : constant String_Access := new String'("-x");
608
609    Object_Suffix     : constant String := Get_Object_Suffix.all;
610    Executable_Suffix : constant String := Get_Executable_Suffix.all;
611
612    Syntax_Only : Boolean := False;
613    --  Set to True when compiling with -gnats
614
615    Display_Executed_Programs : Boolean := True;
616    --  Set to True if name of commands should be output on stderr
617
618    Output_File_Name_Seen : Boolean := False;
619    --  Set to True after having scanned the file_name for
620    --  switch "-o file_name"
621
622    Object_Directory_Seen : Boolean := False;
623    --  Set to True after having scanned the object directory for
624    --  switch "-D obj_dir".
625
626    Object_Directory_Path : String_Access := null;
627    --  The path name of the object directory, set with switch -D
628
629    type Make_Program_Type is (None, Compiler, Binder, Linker);
630
631    Program_Args : Make_Program_Type := None;
632    --  Used to indicate if we are scanning gnatmake, gcc, gnatbind, or gnatbind
633    --  options within the gnatmake command line. Used in Scan_Make_Arg only,
634    --  but must be global since value preserved from one call to another.
635
636    Temporary_Config_File : Boolean := False;
637    --  Set to True when there is a temporary config file used for a project
638    --  file, to avoid displaying the -gnatec switch for a temporary file.
639
640    procedure Add_Switches
641      (The_Package : Package_Id;
642       File_Name   : String;
643       Index       : Int;
644       Program     : Make_Program_Type);
645    procedure Add_Switch
646      (S             : String_Access;
647       Program       : Make_Program_Type;
648       Append_Switch : Boolean := True;
649       And_Save      : Boolean := True);
650    procedure Add_Switch
651      (S             : String;
652       Program       : Make_Program_Type;
653       Append_Switch : Boolean := True;
654       And_Save      : Boolean := True);
655    --  Make invokes one of three programs (the compiler, the binder or the
656    --  linker). For the sake of convenience, some program specific switches
657    --  can be passed directly on the gnatmake commande line. This procedure
658    --  records these switches so that gnamake can pass them to the right
659    --  program.  S is the switch to be added at the end of the command line
660    --  for Program if Append_Switch is True. If Append_Switch is False S is
661    --  added at the beginning of the command line.
662
663    procedure Check
664      (Source_File  : File_Name_Type;
665       Source_Index : Int;
666       The_Args     : Argument_List;
667       Lib_File     : File_Name_Type;
668       Read_Only    : Boolean;
669       ALI          : out ALI_Id;
670       O_File       : out File_Name_Type;
671       O_Stamp      : out Time_Stamp_Type);
672    --  Determines whether the library file Lib_File is up-to-date or not. The
673    --  full name (with path information) of the object file corresponding to
674    --  Lib_File is returned in O_File. Its time stamp is saved in O_Stamp.
675    --  ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
676    --  up-to-date, then the corresponding source file needs to be recompiled.
677    --  In this case ALI = No_ALI_Id.
678
679    procedure Check_Linker_Options
680      (E_Stamp : Time_Stamp_Type;
681       O_File  : out File_Name_Type;
682       O_Stamp : out Time_Stamp_Type);
683    --  Checks all linker options for linker files that are newer
684    --  than E_Stamp. If such objects are found, the youngest object
685    --  is returned in O_File and its stamp in O_Stamp.
686    --
687    --  If no obsolete linker files were found, the first missing
688    --  linker file is returned in O_File and O_Stamp is empty.
689    --  Otherwise O_File is No_File.
690
691    procedure Collect_Arguments
692      (Source_File  : File_Name_Type;
693       Source_Index : Int;
694       Args         : Argument_List);
695    --  Collect all arguments for a source to be compiled, including those
696    --  that come from a project file.
697
698    procedure Display (Program : String; Args : Argument_List);
699    --  Displays Program followed by the arguments in Args if variable
700    --  Display_Executed_Programs is set. The lower bound of Args must be 1.
701
702    -----------------
703    --  Mapping files
704    -----------------
705
706    type Temp_File_Names is
707      array (Project_Id range <>, Positive range <>) of Name_Id;
708
709    type Temp_Files_Ptr is access Temp_File_Names;
710
711    type Indices is array (Project_Id range <>) of Natural;
712
713    type Indices_Ptr is access Indices;
714
715    type Free_File_Indices is array
716      (Project_Id range <>, Positive range <>) of Positive;
717
718    type Free_Indices_Ptr is access Free_File_Indices;
719
720    The_Mapping_File_Names : Temp_Files_Ptr;
721    --  For each project, the name ids of the temporary mapping files used
722
723    Last_Mapping_File_Names : Indices_Ptr;
724    --  For each project, the index of the last mapping file created
725
726    The_Free_Mapping_File_Indices : Free_Indices_Ptr;
727    --  For each project, the indices in The_Mapping_File_Names of the mapping
728    --  file names that can be reused for subsequent compilations.
729
730    Last_Free_Indices : Indices_Ptr;
731    --  For each project, the number of mapping files that can be reused
732
733    Gnatmake_Mapping_File : String_Access := null;
734    --  The path name of a mapping file specified by switch -C=
735
736    procedure Delete_Mapping_Files;
737    --  Delete all temporary mapping files
738
739    procedure Init_Mapping_File
740      (Project : Project_Id;
741       File_Index : in out Natural);
742    --  Create a new temporary mapping file, and fill it with the project file
743    --  mappings, when using project file(s). The out parameter File_Index is
744    --  the index to the name of the file in the array The_Mapping_File_Names.
745
746    procedure Delete_Temp_Config_Files;
747    --  Delete all temporary config files
748
749    procedure Delete_All_Temp_Files;
750    --  Delete all temp files (config files, mapping files, path files)
751
752    -------------------
753    -- Add_Arguments --
754    -------------------
755
756    procedure Add_Arguments (Args : Argument_List) is
757    begin
758       if Arguments = null then
759          Arguments := new Argument_List (1 .. Args'Length + 10);
760
761       else
762          while Last_Argument + Args'Length > Arguments'Last loop
763             declare
764                New_Arguments : constant Argument_List_Access :=
765                                  new Argument_List (1 .. Arguments'Last * 2);
766             begin
767                New_Arguments (1 .. Last_Argument) :=
768                  Arguments (1 .. Last_Argument);
769                Arguments := New_Arguments;
770             end;
771          end loop;
772       end if;
773
774       Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
775       Last_Argument := Last_Argument + Args'Length;
776    end Add_Arguments;
777
778    --------------------
779    -- Add_Dependency --
780    --------------------
781
782    procedure Add_Dependency (S : Name_Id; On : Name_Id) is
783    begin
784       Dependencies.Increment_Last;
785       Dependencies.Table (Dependencies.Last) := (S, On);
786    end Add_Dependency;
787
788    --------------------
789    -- Add_Object_Dir --
790    --------------------
791
792    procedure Add_Object_Dir (N : String) is
793    begin
794       Add_Lib_Search_Dir (N);
795
796       if Verbose_Mode then
797          Write_Str ("Adding object directory """);
798          Write_Str (N);
799          Write_Str (""".");
800          Write_Eol;
801       end if;
802    end Add_Object_Dir;
803
804    --------------------
805    -- Add_Source_Dir --
806    --------------------
807
808    procedure Add_Source_Dir (N : String) is
809    begin
810       Add_Src_Search_Dir (N);
811
812       if Verbose_Mode then
813          Write_Str ("Adding source directory """);
814          Write_Str (N);
815          Write_Str (""".");
816          Write_Eol;
817       end if;
818    end Add_Source_Dir;
819
820    ----------------
821    -- Add_Switch --
822    ----------------
823
824    procedure Add_Switch
825      (S             : String_Access;
826       Program       : Make_Program_Type;
827       Append_Switch : Boolean := True;
828       And_Save      : Boolean := True)
829    is
830       generic
831          with package T is new Table.Table (<>);
832       procedure Generic_Position (New_Position : out Integer);
833       --  Generic procedure that chooses a position for S in T at the
834       --  beginning or the end, depending on the boolean Append_Switch.
835       --  Calling this procedure may expand the table.
836
837       ----------------------
838       -- Generic_Position --
839       ----------------------
840
841       procedure  Generic_Position (New_Position : out Integer) is
842       begin
843          T.Increment_Last;
844
845          if Append_Switch then
846             New_Position := Integer (T.Last);
847          else
848             for J in reverse T.Table_Index_Type'Succ (T.First) .. T.Last loop
849                T.Table (J) := T.Table (T.Table_Index_Type'Pred (J));
850             end loop;
851
852             New_Position := Integer (T.First);
853          end if;
854       end Generic_Position;
855
856       procedure Gcc_Switches_Pos    is new Generic_Position (Gcc_Switches);
857       procedure Binder_Switches_Pos is new Generic_Position (Binder_Switches);
858       procedure Linker_Switches_Pos is new Generic_Position (Linker_Switches);
859
860       procedure Saved_Gcc_Switches_Pos is new
861         Generic_Position (Saved_Gcc_Switches);
862
863       procedure Saved_Binder_Switches_Pos is new
864         Generic_Position (Saved_Binder_Switches);
865
866       procedure Saved_Linker_Switches_Pos is new
867         Generic_Position (Saved_Linker_Switches);
868
869       New_Position : Integer;
870
871    --  Start of processing for Add_Switch
872
873    begin
874       if And_Save then
875          case Program is
876             when Compiler =>
877                Saved_Gcc_Switches_Pos (New_Position);
878                Saved_Gcc_Switches.Table (New_Position) := S;
879
880             when Binder   =>
881                Saved_Binder_Switches_Pos (New_Position);
882                Saved_Binder_Switches.Table (New_Position) := S;
883
884             when Linker   =>
885                Saved_Linker_Switches_Pos (New_Position);
886                Saved_Linker_Switches.Table (New_Position) := S;
887
888             when None =>
889                raise Program_Error;
890          end case;
891
892       else
893          case Program is
894             when Compiler =>
895                Gcc_Switches_Pos (New_Position);
896                Gcc_Switches.Table (New_Position) := S;
897
898             when Binder   =>
899                Binder_Switches_Pos (New_Position);
900                Binder_Switches.Table (New_Position) := S;
901
902             when Linker   =>
903                Linker_Switches_Pos (New_Position);
904                Linker_Switches.Table (New_Position) := S;
905
906             when None =>
907                raise Program_Error;
908          end case;
909       end if;
910    end Add_Switch;
911
912    procedure Add_Switch
913      (S             : String;
914       Program       : Make_Program_Type;
915       Append_Switch : Boolean := True;
916       And_Save      : Boolean := True)
917    is
918    begin
919       Add_Switch (S             => new String'(S),
920                   Program       => Program,
921                   Append_Switch => Append_Switch,
922                   And_Save      => And_Save);
923    end Add_Switch;
924
925    ------------------
926    -- Add_Switches --
927    ------------------
928
929    procedure Add_Switches
930      (The_Package : Package_Id;
931       File_Name   : String;
932       Index       : Int;
933       Program     : Make_Program_Type)
934    is
935       Switches    : Variable_Value;
936       Switch_List : String_List_Id;
937       Element     : String_Element;
938
939    begin
940       if File_Name'Length > 0 then
941          Name_Len := File_Name'Length;
942          Name_Buffer (1 .. Name_Len) := File_Name;
943          Switches :=
944            Switches_Of
945            (Source_File      => Name_Find,
946             Source_File_Name => File_Name,
947             Source_Index     => Index,
948             Naming           => Project_Tree.Projects.Table
949                                   (Main_Project).Naming,
950             In_Package       => The_Package,
951             Allow_ALI        =>
952               Program = Binder or else Program = Linker);
953
954          case Switches.Kind is
955             when Undefined =>
956                null;
957
958             when List =>
959                Program_Args := Program;
960
961                Switch_List := Switches.Values;
962
963                while Switch_List /= Nil_String loop
964                   Element :=
965                     Project_Tree.String_Elements.Table (Switch_List);
966                   Get_Name_String (Element.Value);
967
968                   if Name_Len > 0 then
969                      declare
970                         Argv : constant String := Name_Buffer (1 .. Name_Len);
971                         --  We need a copy, because Name_Buffer may be
972                         --  modified.
973
974                      begin
975                         if Verbose_Mode then
976                            Write_Str ("   Adding ");
977                            Write_Line (Argv);
978                         end if;
979
980                         Scan_Make_Arg (Argv, And_Save => False);
981                      end;
982                   end if;
983
984                   Switch_List := Element.Next;
985                end loop;
986
987             when Single =>
988                Program_Args := Program;
989                Get_Name_String (Switches.Value);
990
991                if Name_Len > 0 then
992                   declare
993                      Argv : constant String := Name_Buffer (1 .. Name_Len);
994                      --  We need a copy, because Name_Buffer may be modified
995
996                   begin
997                      if Verbose_Mode then
998                         Write_Str ("   Adding ");
999                         Write_Line (Argv);
1000                      end if;
1001
1002                      Scan_Make_Arg (Argv, And_Save => False);
1003                   end;
1004                end if;
1005          end case;
1006       end if;
1007    end Add_Switches;
1008
1009    ----------
1010    -- Bind --
1011    ----------
1012
1013    procedure Bind (ALI_File : File_Name_Type; Args : Argument_List) is
1014       Bind_Args : Argument_List (1 .. Args'Last + 2);
1015       Bind_Last : Integer;
1016       Success   : Boolean;
1017
1018    begin
1019       pragma Assert (Args'First = 1);
1020
1021       --  Optimize the simple case where the gnatbind command line looks like
1022       --     gnatbind -aO. -I- file.ali   --into->   gnatbind file.adb
1023
1024       if Args'Length = 2
1025         and then Args (Args'First).all = "-aO" & Normalized_CWD
1026         and then Args (Args'Last).all = "-I-"
1027         and then ALI_File = Strip_Directory (ALI_File)
1028       then
1029          Bind_Last := Args'First - 1;
1030
1031       else
1032          Bind_Last := Args'Last;
1033          Bind_Args (Args'Range) := Args;
1034       end if;
1035
1036       --  It is completely pointless to re-check source file time stamps.
1037       --  This has been done already by gnatmake
1038
1039       Bind_Last := Bind_Last + 1;
1040       Bind_Args (Bind_Last) := Do_Not_Check_Flag;
1041
1042       Get_Name_String (ALI_File);
1043
1044       Bind_Last := Bind_Last + 1;
1045       Bind_Args (Bind_Last) := new String'(Name_Buffer (1 .. Name_Len));
1046
1047       GNAT.OS_Lib.Normalize_Arguments (Bind_Args (Args'First .. Bind_Last));
1048
1049       Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
1050
1051       if Gnatbind_Path = null then
1052          Make_Failed ("error, unable to locate ", Gnatbind.all);
1053       end if;
1054
1055       GNAT.OS_Lib.Spawn
1056         (Gnatbind_Path.all, Bind_Args (Args'First .. Bind_Last), Success);
1057
1058       if not Success then
1059          raise Bind_Failed;
1060       end if;
1061    end Bind;
1062
1063    --------------------------------
1064    -- Change_To_Object_Directory --
1065    --------------------------------
1066
1067    procedure Change_To_Object_Directory (Project : Project_Id) is
1068    begin
1069       --  Nothing to do if the current working directory is alresdy the one
1070       --  we want.
1071
1072       if Project_Object_Directory /= Project then
1073          Project_Object_Directory := Project;
1074
1075          --  If in a real project, set the working directory to the object
1076          --  directory of the project.
1077
1078          if Project /= No_Project then
1079             Change_Dir
1080               (Get_Name_String
1081                  (Project_Tree.Projects.Table
1082                     (Project).Object_Directory));
1083
1084          --  Otherwise, for sources outside of any project, set the working
1085          --  directory to the object directory of the main project.
1086
1087          elsif Main_Project /= No_Project then
1088             Change_Dir
1089               (Get_Name_String
1090                  (Project_Tree.Projects.Table
1091                     (Main_Project).Object_Directory));
1092          end if;
1093       end if;
1094    end Change_To_Object_Directory;
1095
1096    -----------
1097    -- Check --
1098    -----------
1099
1100    procedure Check
1101      (Source_File  : File_Name_Type;
1102       Source_Index : Int;
1103       The_Args     : Argument_List;
1104       Lib_File     : File_Name_Type;
1105       Read_Only    : Boolean;
1106       ALI          : out ALI_Id;
1107       O_File       : out File_Name_Type;
1108       O_Stamp      : out Time_Stamp_Type)
1109    is
1110       function First_New_Spec (A : ALI_Id) return File_Name_Type;
1111       --  Looks in the with table entries of A and returns the spec file name
1112       --  of the first withed unit (subprogram) for which no spec existed when
1113       --  A was generated but for which there exists one now, implying that A
1114       --  is now obsolete. If no such unit is found No_File is returned.
1115       --  Otherwise the spec file name of the unit is returned.
1116       --
1117       --  **WARNING** in the event of Uname format modifications, one *MUST*
1118       --  make sure this function is also updated.
1119       --
1120       --  Note: This function should really be in ali.adb and use Uname
1121       --  services, but this causes the whole compiler to be dragged along
1122       --  for gnatbind and gnatmake.
1123
1124       --------------------
1125       -- First_New_Spec --
1126       --------------------
1127
1128       function First_New_Spec (A : ALI_Id) return File_Name_Type is
1129          Spec_File_Name : File_Name_Type := No_File;
1130
1131          function New_Spec (Uname : Unit_Name_Type) return Boolean;
1132          --  Uname is the name of the spec or body of some ada unit.
1133          --  This function returns True if the Uname is the name of a body
1134          --  which has a spec not mentioned inali file A. If True is returned
1135          --  Spec_File_Name above is set to the name of this spec file.
1136
1137          --------------
1138          -- New_Spec --
1139          --------------
1140
1141          function New_Spec (Uname : Unit_Name_Type) return Boolean is
1142             Spec_Name : Unit_Name_Type;
1143             File_Name : File_Name_Type;
1144
1145          begin
1146             --  Test whether Uname is the name of a body unit (ie ends with %b)
1147
1148             Get_Name_String (Uname);
1149             pragma
1150               Assert (Name_Len > 2 and then Name_Buffer (Name_Len - 1) = '%');
1151
1152             if Name_Buffer (Name_Len) /= 'b' then
1153                return False;
1154             end if;
1155
1156             --  Convert unit name into spec name
1157
1158             --  ??? this code seems dubious in presence of pragma
1159             --  Source_File_Name since there is no more direct relationship
1160             --  between unit name and file name.
1161
1162             --  ??? Further, what about alternative subunit naming
1163
1164             Name_Buffer (Name_Len) := 's';
1165             Spec_Name := Name_Find;
1166             File_Name := Get_File_Name (Spec_Name, Subunit => False);
1167
1168             --  Look if File_Name is mentioned in A's sdep list.
1169             --  If not look if the file exists. If it does return True.
1170
1171             for D in
1172               ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
1173             loop
1174                if Sdep.Table (D).Sfile = File_Name then
1175                   return False;
1176                end if;
1177             end loop;
1178
1179             if Full_Source_Name (File_Name) /= No_File then
1180                Spec_File_Name := File_Name;
1181                return True;
1182             end if;
1183
1184             return False;
1185          end New_Spec;
1186
1187       --  Start of processing for First_New_Spec
1188
1189       begin
1190          U_Chk : for U in
1191            ALIs.Table (A).First_Unit .. ALIs.Table (A).Last_Unit
1192          loop
1193             exit U_Chk when Units.Table (U).Utype = Is_Body_Only
1194                and then New_Spec (Units.Table (U).Uname);
1195
1196             for W in Units.Table (U).First_With
1197                        ..
1198                      Units.Table (U).Last_With
1199             loop
1200                exit U_Chk when
1201                  Withs.Table (W).Afile /= No_File
1202                  and then New_Spec (Withs.Table (W).Uname);
1203             end loop;
1204          end loop U_Chk;
1205
1206          return Spec_File_Name;
1207       end First_New_Spec;
1208
1209       ---------------------------------
1210       -- Data declarations for Check --
1211       ---------------------------------
1212
1213       Full_Lib_File : File_Name_Type;
1214       --  Full name of current library file
1215
1216       Full_Obj_File : File_Name_Type;
1217       --  Full name of the object file corresponding to Lib_File
1218
1219       Lib_Stamp : Time_Stamp_Type;
1220       --  Time stamp of the current ada library file
1221
1222       Obj_Stamp : Time_Stamp_Type;
1223       --  Time stamp of the current object file
1224
1225       Modified_Source : File_Name_Type;
1226       --  The first source in Lib_File whose current time stamp differs
1227       --  from that stored in Lib_File.
1228
1229       New_Spec : File_Name_Type;
1230       --  If Lib_File contains in its W (with) section a body (for a
1231       --  subprogram) for which there exists a spec and the spec did not
1232       --  appear in the Sdep section of Lib_File, New_Spec contains the file
1233       --  name of this new spec.
1234
1235       Source_Name : Name_Id;
1236       Text        : Text_Buffer_Ptr;
1237
1238       Prev_Switch : String_Access;
1239       --  Previous switch processed
1240
1241       Arg : Arg_Id := Arg_Id'First;
1242       --  Current index in Args.Table for a given unit (init to stop warning)
1243
1244       Switch_Found : Boolean;
1245       --  True if a given switch has been found
1246
1247    --  Start of processing for Check
1248
1249    begin
1250       pragma Assert (Lib_File /= No_File);
1251
1252       --  If the ALI file is read-only, set temporarily
1253       --  Check_Object_Consistency to False: we don't care if the object file
1254       --  is not there; presumably, a library will be used for linking.
1255
1256       if Read_Only then
1257          declare
1258             Saved_Check_Object_Consistency : constant Boolean :=
1259                                                Check_Object_Consistency;
1260          begin
1261             Check_Object_Consistency := False;
1262             Text := Read_Library_Info (Lib_File);
1263             Check_Object_Consistency := Saved_Check_Object_Consistency;
1264          end;
1265
1266       else
1267          Text := Read_Library_Info (Lib_File);
1268       end if;
1269
1270       Full_Lib_File := Full_Library_Info_Name;
1271       Full_Obj_File := Full_Object_File_Name;
1272       Lib_Stamp     := Current_Library_File_Stamp;
1273       Obj_Stamp     := Current_Object_File_Stamp;
1274
1275       if Full_Lib_File = No_File then
1276          Verbose_Msg (Lib_File, "being checked ...", Prefix => "  ");
1277       else
1278          Verbose_Msg (Full_Lib_File, "being checked ...", Prefix => "  ");
1279       end if;
1280
1281       ALI     := No_ALI_Id;
1282       O_File  := Full_Obj_File;
1283       O_Stamp := Obj_Stamp;
1284
1285       if Text = null then
1286          if Full_Lib_File = No_File then
1287             Verbose_Msg (Lib_File, "missing.");
1288
1289          elsif Obj_Stamp (Obj_Stamp'First) = ' ' then
1290             Verbose_Msg (Full_Obj_File, "missing.");
1291
1292          else
1293             Verbose_Msg
1294               (Full_Lib_File, "(" & String (Lib_Stamp) & ") newer than",
1295                Full_Obj_File, "(" & String (Obj_Stamp) & ")");
1296          end if;
1297
1298       else
1299          ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1300          Free (Text);
1301
1302          if ALI = No_ALI_Id then
1303             Verbose_Msg (Full_Lib_File, "incorrectly formatted ALI file");
1304             return;
1305
1306          elsif ALIs.Table (ALI).Ver (1 .. ALIs.Table (ALI).Ver_Len) /=
1307                  Verbose_Library_Version
1308          then
1309             Verbose_Msg (Full_Lib_File, "compiled with old GNAT version");
1310             ALI := No_ALI_Id;
1311             return;
1312          end if;
1313
1314          --  Don't take Ali file into account if it was generated with
1315          --  errors.
1316
1317          if ALIs.Table (ALI).Compile_Errors then
1318             Verbose_Msg (Full_Lib_File, "had errors, must be recompiled");
1319             ALI := No_ALI_Id;
1320             return;
1321          end if;
1322
1323          --  Don't take Ali file into account if it was generated without
1324          --  object.
1325
1326          if Operating_Mode /= Check_Semantics
1327            and then ALIs.Table (ALI).No_Object
1328          then
1329             Verbose_Msg (Full_Lib_File, "has no corresponding object");
1330             ALI := No_ALI_Id;
1331             return;
1332          end if;
1333
1334          --  Check for matching compiler switches if needed
1335
1336          if Check_Switches then
1337
1338             --  First, collect all the switches
1339
1340             Collect_Arguments (Source_File, Source_Index, The_Args);
1341
1342             Prev_Switch := Dummy_Switch;
1343
1344             Get_Name_String (ALIs.Table (ALI).Sfile);
1345
1346             Switches_To_Check.Set_Last (0);
1347
1348             for J in 1 .. Last_Argument loop
1349
1350                --  Skip non switches -c, -I and -o switches
1351
1352                if Arguments (J) (1) = '-'
1353                  and then Arguments (J) (2) /= 'c'
1354                  and then Arguments (J) (2) /= 'o'
1355                  and then Arguments (J) (2) /= 'I'
1356                then
1357                   Normalize_Compiler_Switches
1358                     (Arguments (J).all,
1359                      Normalized_Switches,
1360                      Last_Norm_Switch);
1361
1362                   for K in 1 .. Last_Norm_Switch loop
1363                      Switches_To_Check.Increment_Last;
1364                      Switches_To_Check.Table (Switches_To_Check.Last) :=
1365                        Normalized_Switches (K);
1366                   end loop;
1367                end if;
1368             end loop;
1369
1370             for J in 1 .. Switches_To_Check.Last loop
1371
1372                --  Comparing switches is delicate because gcc reorders
1373                --  a number of switches, according to lang-specs.h, but
1374                --  gnatmake doesn't have the sufficient knowledge to
1375                --  perform the same reordering. Instead, we ignore orders
1376                --  between different "first letter" switches, but keep
1377                --  orders between same switches, e.g -O -O2 is different
1378                --  than -O2 -O, but -g -O is equivalent to -O -g.
1379
1380                if Switches_To_Check.Table (J) (2) /= Prev_Switch (2) or else
1381                    (Prev_Switch'Length >= 6 and then
1382                     Prev_Switch (2 .. 5) = "gnat" and then
1383                     Switches_To_Check.Table (J)'Length >= 6 and then
1384                     Switches_To_Check.Table (J) (2 .. 5) = "gnat" and then
1385                     Prev_Switch (6) /= Switches_To_Check.Table (J) (6))
1386                then
1387                   Prev_Switch := Switches_To_Check.Table (J);
1388                   Arg :=
1389                     Units.Table (ALIs.Table (ALI).First_Unit).First_Arg;
1390                end if;
1391
1392                Switch_Found := False;
1393
1394                for K in Arg ..
1395                  Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1396                loop
1397                   if
1398                     Switches_To_Check.Table (J).all = Args.Table (K).all
1399                   then
1400                      Arg := K + 1;
1401                      Switch_Found := True;
1402                      exit;
1403                   end if;
1404                end loop;
1405
1406                if not Switch_Found then
1407                   if Verbose_Mode then
1408                      Verbose_Msg (ALIs.Table (ALI).Sfile,
1409                                   "switch mismatch """ &
1410                                   Switches_To_Check.Table (J).all & '"');
1411                   end if;
1412
1413                   ALI := No_ALI_Id;
1414                   return;
1415                end if;
1416             end loop;
1417
1418             if Switches_To_Check.Last /=
1419               Integer (Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg -
1420                        Units.Table (ALIs.Table (ALI).First_Unit).First_Arg + 1)
1421             then
1422                if Verbose_Mode then
1423                   Verbose_Msg (ALIs.Table (ALI).Sfile,
1424                                "different number of switches");
1425
1426                   for K in Units.Table (ALIs.Table (ALI).First_Unit).First_Arg
1427                     .. Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1428                   loop
1429                      Write_Str (Args.Table (K).all);
1430                      Write_Char (' ');
1431                   end loop;
1432
1433                   Write_Eol;
1434
1435                   for J in 1 .. Switches_To_Check.Last loop
1436                      Write_Str (Switches_To_Check.Table (J).all);
1437                      Write_Char (' ');
1438                   end loop;
1439
1440                   Write_Eol;
1441                end if;
1442
1443                ALI := No_ALI_Id;
1444                return;
1445             end if;
1446          end if;
1447
1448          --  Get the source files and their message digests. Note that some
1449          --  sources may be missing if ALI is out-of-date.
1450
1451          Set_Source_Table (ALI);
1452
1453          Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
1454
1455          if Modified_Source /= No_File then
1456             ALI := No_ALI_Id;
1457
1458             if Verbose_Mode then
1459                Source_Name := Full_Source_Name (Modified_Source);
1460
1461                if Source_Name /= No_File then
1462                   Verbose_Msg (Source_Name, "time stamp mismatch");
1463                else
1464                   Verbose_Msg (Modified_Source, "missing");
1465                end if;
1466             end if;
1467
1468          else
1469             New_Spec := First_New_Spec (ALI);
1470
1471             if New_Spec /= No_File then
1472                ALI := No_ALI_Id;
1473
1474                if Verbose_Mode then
1475                   Source_Name := Full_Source_Name (New_Spec);
1476
1477                   if Source_Name /= No_File then
1478                      Verbose_Msg (Source_Name, "new spec");
1479                   else
1480                      Verbose_Msg (New_Spec, "old spec missing");
1481                   end if;
1482                end if;
1483             end if;
1484          end if;
1485       end if;
1486    end Check;
1487
1488    ------------------------
1489    -- Check_For_S_Switch --
1490    ------------------------
1491
1492    procedure Check_For_S_Switch is
1493    begin
1494       --  By default, we generate an object file
1495
1496       Output_Is_Object := True;
1497
1498       for Arg in 1 .. Last_Argument loop
1499          if Arguments (Arg).all = "-S" then
1500             Output_Is_Object := False;
1501
1502          elsif Arguments (Arg).all = "-c" then
1503             Output_Is_Object := True;
1504          end if;
1505       end loop;
1506    end Check_For_S_Switch;
1507
1508    --------------------------
1509    -- Check_Linker_Options --
1510    --------------------------
1511
1512    procedure Check_Linker_Options
1513      (E_Stamp   : Time_Stamp_Type;
1514       O_File    : out File_Name_Type;
1515       O_Stamp   : out Time_Stamp_Type)
1516    is
1517       procedure Check_File (File : File_Name_Type);
1518       --  Update O_File and O_Stamp if the given file is younger than E_Stamp
1519       --  and O_Stamp, or if O_File is No_File and File does not exist.
1520
1521       function Get_Library_File (Name : String) return File_Name_Type;
1522       --  Return the full file name including path of a library based
1523       --  on the name specified with the -l linker option, using the
1524       --  Ada object path. Return No_File if no such file can be found.
1525
1526       type Char_Array is array (Natural) of Character;
1527       type Char_Array_Access is access constant Char_Array;
1528
1529       Template : Char_Array_Access;
1530       pragma Import (C, Template, "__gnat_library_template");
1531
1532       ----------------
1533       -- Check_File --
1534       ----------------
1535
1536       procedure Check_File (File : File_Name_Type) is
1537          Stamp : Time_Stamp_Type;
1538          Name  : File_Name_Type := File;
1539
1540       begin
1541          Get_Name_String (Name);
1542
1543          --  Remove any trailing NUL characters
1544
1545          while Name_Len >= Name_Buffer'First
1546            and then Name_Buffer (Name_Len) = NUL
1547          loop
1548             Name_Len := Name_Len - 1;
1549          end loop;
1550
1551          if Name_Len <= 0 then
1552             return;
1553
1554          elsif Name_Buffer (1) = '-' then
1555
1556             --  Do not check if File is a switch other than "-l"
1557
1558             if Name_Buffer (2) /= 'l' then
1559                return;
1560             end if;
1561
1562             --  The argument is a library switch, get actual name. It
1563             --  is necessary to make a copy of the relevant part of
1564             --  Name_Buffer as Get_Library_Name uses Name_Buffer as well.
1565
1566             declare
1567                Base_Name : constant String := Name_Buffer (3 .. Name_Len);
1568
1569             begin
1570                Name := Get_Library_File (Base_Name);
1571             end;
1572
1573             if Name = No_File then
1574                return;
1575             end if;
1576          end if;
1577
1578          Stamp := File_Stamp (Name);
1579
1580          --  Find the youngest object file that is younger than the
1581          --  executable. If no such file exist, record the first object
1582          --  file that is not found.
1583
1584          if (O_Stamp < Stamp and then E_Stamp < Stamp)
1585            or else (O_File = No_File and then Stamp (Stamp'First) = ' ')
1586          then
1587             O_Stamp := Stamp;
1588             O_File := Name;
1589
1590             --  Strip the trailing NUL if present
1591
1592             Get_Name_String (O_File);
1593
1594             if Name_Buffer (Name_Len) = NUL then
1595                Name_Len := Name_Len - 1;
1596                O_File := Name_Find;
1597             end if;
1598          end if;
1599       end Check_File;
1600
1601       ----------------------
1602       -- Get_Library_Name --
1603       ----------------------
1604
1605       --  See comments in a-adaint.c about template syntax
1606
1607       function Get_Library_File (Name : String) return File_Name_Type is
1608          File : File_Name_Type := No_File;
1609
1610       begin
1611          Name_Len := 0;
1612
1613          for Ptr in Template'Range loop
1614             case Template (Ptr) is
1615                when '*'    =>
1616                   Add_Str_To_Name_Buffer (Name);
1617
1618                when ';'    =>
1619                   File := Full_Lib_File_Name (Name_Find);
1620                   exit when File /= No_File;
1621                   Name_Len := 0;
1622
1623                when NUL    =>
1624                   exit;
1625
1626                when others =>
1627                   Add_Char_To_Name_Buffer (Template (Ptr));
1628             end case;
1629          end loop;
1630
1631          --  The for loop exited because the end of the template
1632          --  was reached. File contains the last possible file name
1633          --  for the library.
1634
1635          if File = No_File and then Name_Len > 0 then
1636             File := Full_Lib_File_Name (Name_Find);
1637          end if;
1638
1639          return File;
1640       end Get_Library_File;
1641
1642    --  Start of processing for Check_Linker_Options
1643
1644    begin
1645       O_File  := No_File;
1646       O_Stamp := (others => ' ');
1647
1648       --  Process linker options from the ALI files
1649
1650       for Opt in 1 .. Linker_Options.Last loop
1651          Check_File (Linker_Options.Table (Opt).Name);
1652       end loop;
1653
1654       --  Process options given on the command line
1655
1656       for Opt in Linker_Switches.First .. Linker_Switches.Last loop
1657
1658          --  Check if the previous Opt has one of the two switches
1659          --  that take an extra parameter. (See GCC manual.)
1660
1661          if Opt = Linker_Switches.First
1662            or else (Linker_Switches.Table (Opt - 1).all /= "-u"
1663                       and then
1664                     Linker_Switches.Table (Opt - 1).all /= "-Xlinker"
1665                       and then
1666                     Linker_Switches.Table (Opt - 1).all /= "-L")
1667          then
1668             Name_Len := 0;
1669             Add_Str_To_Name_Buffer (Linker_Switches.Table (Opt).all);
1670             Check_File (Name_Find);
1671          end if;
1672       end loop;
1673
1674    end Check_Linker_Options;
1675
1676    -----------------
1677    -- Check_Steps --
1678    -----------------
1679
1680    procedure Check_Steps is
1681    begin
1682       --  If either -c, -b or -l has been specified, we will not necessarily
1683       --  execute all steps.
1684
1685       if Make_Steps then
1686          Do_Compile_Step := Do_Compile_Step and Compile_Only;
1687          Do_Bind_Step    := Do_Bind_Step    and Bind_Only;
1688          Do_Link_Step    := Do_Link_Step    and Link_Only;
1689
1690          --  If -c has been specified, but not -b, ignore any potential -l
1691
1692          if Do_Compile_Step and then not Do_Bind_Step then
1693             Do_Link_Step := False;
1694          end if;
1695       end if;
1696    end Check_Steps;
1697
1698    -----------------------
1699    -- Collect_Arguments --
1700    -----------------------
1701
1702    procedure Collect_Arguments
1703      (Source_File  : File_Name_Type;
1704       Source_Index : Int;
1705       Args         : Argument_List)
1706    is
1707    begin
1708       Arguments_Collected := True;
1709       Arguments_Project := No_Project;
1710       Last_Argument := 0;
1711       Add_Arguments (Args);
1712
1713       if Main_Project /= No_Project then
1714          declare
1715             Source_File_Name : constant String :=
1716                                  Get_Name_String (Source_File);
1717             Compiler_Package : Prj.Package_Id;
1718             Switches         : Prj.Variable_Value;
1719             Data             : Project_Data;
1720
1721          begin
1722             Prj.Env.
1723               Get_Reference
1724               (Source_File_Name => Source_File_Name,
1725                Project          => Arguments_Project,
1726                Path             => Arguments_Path_Name,
1727                In_Tree          => Project_Tree);
1728
1729             --  If the source is not a source of a project file, check if
1730             --  this is allowed.
1731
1732             if Arguments_Project = No_Project then
1733                if not External_Unit_Compilation_Allowed then
1734                   Make_Failed ("external source (", Source_File_Name,
1735                                ") is not part of any project; cannot be " &
1736                                "compiled without gnatmake switch -x");
1737                end if;
1738
1739                --  If it is allowed, simply add the saved gcc switches
1740
1741                Add_Arguments (The_Saved_Gcc_Switches.all);
1742
1743             else
1744                --  We get the project directory for the relative path
1745                --  switches and arguments.
1746
1747                Data := Project_Tree.Projects.Table (Arguments_Project);
1748
1749                --  If the source is in an extended project, we go to
1750                --  the ultimate extending project.
1751
1752                while Data.Extended_By /= No_Project loop
1753                   Arguments_Project := Data.Extended_By;
1754                   Data :=
1755                     Project_Tree.Projects.Table (Arguments_Project);
1756                end loop;
1757
1758                --  If building a dynamic or relocatable library, compile with
1759                --  PIC option, if it exists.
1760
1761                if Data.Library and then Data.Library_Kind /= Static then
1762                   declare
1763                      PIC : constant String := MLib.Tgt.PIC_Option;
1764
1765                   begin
1766                      if PIC /= "" then
1767                         Add_Arguments ((1 => new String'(PIC)));
1768                      end if;
1769                   end;
1770                end if;
1771
1772                if Data.Dir_Path = null then
1773                   Data.Dir_Path :=
1774                     new String'(Get_Name_String (Data.Display_Directory));
1775                   Project_Tree.Projects.Table (Arguments_Project) :=
1776                     Data;
1777                end if;
1778
1779                --  We now look for package Compiler
1780                --  and get the switches from this package.
1781
1782                Compiler_Package :=
1783                  Prj.Util.Value_Of
1784                    (Name        => Name_Compiler,
1785                     In_Packages => Data.Decl.Packages,
1786                     In_Tree     => Project_Tree);
1787
1788                if Compiler_Package /= No_Package then
1789
1790                   --  If package Gnatmake.Compiler exists, we get
1791                   --  the specific switches for the current source,
1792                   --  or the global switches, if any.
1793
1794                   Switches := Switches_Of
1795                     (Source_File      => Source_File,
1796                      Source_File_Name => Source_File_Name,
1797                      Source_Index     => Source_Index,
1798                      Naming           => Data.Naming,
1799                      In_Package       => Compiler_Package,
1800                      Allow_ALI        => False);
1801
1802                end if;
1803
1804                case Switches.Kind is
1805
1806                   --  We have a list of switches. We add these switches,
1807                   --  plus the saved gcc switches.
1808
1809                   when List =>
1810
1811                      declare
1812                         Current : String_List_Id := Switches.Values;
1813                         Element : String_Element;
1814                         Number  : Natural := 0;
1815
1816                      begin
1817                         while Current /= Nil_String loop
1818                            Element := Project_Tree.String_Elements.
1819                                         Table (Current);
1820                            Number  := Number + 1;
1821                            Current := Element.Next;
1822                         end loop;
1823
1824                         declare
1825                            New_Args : Argument_List (1 .. Number);
1826
1827                         begin
1828                            Current := Switches.Values;
1829
1830                            for Index in New_Args'Range loop
1831                               Element := Project_Tree.String_Elements.
1832                                            Table (Current);
1833                               Get_Name_String (Element.Value);
1834                               New_Args (Index) :=
1835                                 new String'(Name_Buffer (1 .. Name_Len));
1836                               Test_If_Relative_Path
1837                                 (New_Args (Index), Parent => Data.Dir_Path);
1838                               Current := Element.Next;
1839                            end loop;
1840
1841                            Add_Arguments
1842                              (Configuration_Pragmas_Switch
1843                                 (Arguments_Project) &
1844                               New_Args & The_Saved_Gcc_Switches.all);
1845                         end;
1846                      end;
1847
1848                      --  We have a single switch. We add this switch,
1849                      --  plus the saved gcc switches.
1850
1851                   when Single =>
1852                      Get_Name_String (Switches.Value);
1853
1854                      declare
1855                         New_Args : Argument_List :=
1856                                      (1 => new String'
1857                                             (Name_Buffer (1 .. Name_Len)));
1858
1859                      begin
1860                         Test_If_Relative_Path
1861                           (New_Args (1), Parent => Data.Dir_Path);
1862                         Add_Arguments
1863                           (Configuration_Pragmas_Switch (Arguments_Project) &
1864                            New_Args & The_Saved_Gcc_Switches.all);
1865                      end;
1866
1867                      --  We have no switches from Gnatmake.Compiler.
1868                      --  We add the saved gcc switches.
1869
1870                   when Undefined =>
1871                      Add_Arguments
1872                        (Configuration_Pragmas_Switch (Arguments_Project) &
1873                         The_Saved_Gcc_Switches.all);
1874                end case;
1875             end if;
1876          end;
1877       end if;
1878
1879       --  Set Output_Is_Object, depending if there is a -S switch.
1880       --  If the bind step is not performed, and there is a -S switch,
1881       --  then we will not check for a valid object file.
1882
1883       Check_For_S_Switch;
1884    end Collect_Arguments;
1885
1886    ---------------------
1887    -- Compile_Sources --
1888    ---------------------
1889
1890    procedure Compile_Sources
1891      (Main_Source           : File_Name_Type;
1892       Args                  : Argument_List;
1893       First_Compiled_File   : out Name_Id;
1894       Most_Recent_Obj_File  : out Name_Id;
1895       Most_Recent_Obj_Stamp : out Time_Stamp_Type;
1896       Main_Unit             : out Boolean;
1897       Compilation_Failures  : out Natural;
1898       Main_Index            : Int      := 0;
1899       Check_Readonly_Files  : Boolean  := False;
1900       Do_Not_Execute        : Boolean  := False;
1901       Force_Compilations    : Boolean  := False;
1902       Keep_Going            : Boolean  := False;
1903       In_Place_Mode         : Boolean  := False;
1904       Initialize_ALI_Data   : Boolean  := True;
1905       Max_Process           : Positive := 1)
1906    is
1907       No_Mapping_File : constant Natural := 0;
1908
1909       type Compilation_Data is record
1910          Pid              : Process_Id;
1911          Full_Source_File : File_Name_Type;
1912          Lib_File         : File_Name_Type;
1913          Source_Unit      : Unit_Name_Type;
1914          Mapping_File     : Natural := No_Mapping_File;
1915          Project          : Project_Id := No_Project;
1916          Syntax_Only      : Boolean := False;
1917          Output_Is_Object : Boolean := True;
1918       end record;
1919
1920       Running_Compile : array (1 .. Max_Process) of Compilation_Data;
1921       --  Used to save information about outstanding compilations
1922
1923       Outstanding_Compiles : Natural := 0;
1924       --  Current number of outstanding compiles
1925
1926       Source_Unit : Unit_Name_Type;
1927       --  Current source unit
1928
1929       Source_File : File_Name_Type;
1930       --  Current source file
1931
1932       Full_Source_File : File_Name_Type;
1933       --  Full name of the current source file
1934
1935       Lib_File : File_Name_Type;
1936       --  Current library file
1937
1938       Full_Lib_File : File_Name_Type;
1939       --  Full name of the current library file
1940
1941       Obj_File : File_Name_Type;
1942       --  Full name of the object file corresponding to Lib_File
1943
1944       Obj_Stamp : Time_Stamp_Type;
1945       --  Time stamp of the current object file
1946
1947       Sfile : File_Name_Type;
1948       --  Contains the source file of the units withed by Source_File
1949
1950       ALI : ALI_Id;
1951       --  ALI Id of the current ALI file
1952
1953       --  Comment following declarations ???
1954
1955       Read_Only : Boolean := False;
1956
1957       Compilation_OK  : Boolean;
1958       Need_To_Compile : Boolean;
1959
1960       Pid  : Process_Id;
1961       Text : Text_Buffer_Ptr;
1962
1963       Mfile : Natural := No_Mapping_File;
1964
1965       Need_To_Check_Standard_Library : Boolean :=
1966                                          Check_Readonly_Files
1967                                            and not Unique_Compile;
1968
1969       Mapping_File_Arg : String_Access;
1970
1971       Process_Created : Boolean := False;
1972
1973       procedure Add_Process
1974         (Pid    : Process_Id;
1975          Sfile  : File_Name_Type;
1976          Afile  : File_Name_Type;
1977          Uname  : Unit_Name_Type;
1978          Mfile  : Natural := No_Mapping_File);
1979       --  Adds process Pid to the current list of outstanding compilation
1980       --  processes and record the full name of the source file Sfile that
1981       --  we are compiling, the name of its library file Afile and the
1982       --  name of its unit Uname. If Mfile is not equal to No_Mapping_File,
1983       --  it is the index of the mapping file used during compilation in the
1984       --  array The_Mapping_File_Names.
1985
1986       procedure Await_Compile
1987         (Sfile : out File_Name_Type;
1988          Afile : out File_Name_Type;
1989          Uname : out Unit_Name_Type;
1990          OK    : out Boolean);
1991       --  Awaits that an outstanding compilation process terminates. When
1992       --  it does set Sfile to the name of the source file that was compiled
1993       --  Afile to the name of its library file and Uname to the name of its
1994       --  unit. Note that this time stamp can be used to check whether the
1995       --  compilation did generate an object file. OK is set to True if the
1996       --  compilation succeeded. Note that Sfile, Afile and Uname could be
1997       --  resp. No_File, No_File and No_Name  if there were no compilations
1998       --  to wait for.
1999
2000       function Bad_Compilation_Count return Natural;
2001       --  Returns the number of compilation failures
2002
2003       procedure Check_Standard_Library;
2004       --  Check if s-stalib.adb needs to be compiled
2005
2006       procedure Collect_Arguments_And_Compile
2007         (Source_File : File_Name_Type; Source_Index : Int);
2008       --  Collect arguments from project file (if any) and compile
2009
2010       function Compile
2011         (S            : Name_Id;
2012          L            : Name_Id;
2013          Source_Index : Int;
2014          Args         : Argument_List) return Process_Id;
2015       --  Compiles S using Args. If S is a GNAT predefined source
2016       --  "-gnatpg" is added to Args. Non blocking call. L corresponds to the
2017       --  expected library file name. Process_Id of the process spawned to
2018       --  execute the compile.
2019
2020       package Good_ALI is new Table.Table (
2021         Table_Component_Type => ALI_Id,
2022         Table_Index_Type     => Natural,
2023         Table_Low_Bound      => 1,
2024         Table_Initial        => 50,
2025         Table_Increment      => 100,
2026         Table_Name           => "Make.Good_ALI");
2027       --  Contains the set of valid ALI files that have not yet been scanned
2028
2029       function Good_ALI_Present return Boolean;
2030       --  Returns True if any ALI file was recorded in the previous set
2031
2032       procedure Get_Mapping_File (Project : Project_Id);
2033       --  Get a mapping file name. If there is one to be reused, reuse it.
2034       --  Otherwise, create a new mapping file.
2035
2036       function Get_Next_Good_ALI return ALI_Id;
2037       --  Returns the next good ALI_Id record
2038
2039       procedure Record_Failure
2040         (File  : File_Name_Type;
2041          Unit  : Unit_Name_Type;
2042          Found : Boolean := True);
2043       --  Records in the previous table that the compilation for File failed.
2044       --  If Found is False then the compilation of File failed because we
2045       --  could not find it. Records also Unit when possible.
2046
2047       procedure Record_Good_ALI (A : ALI_Id);
2048       --  Records in the previous set the Id of an ALI file
2049
2050       -----------------
2051       -- Add_Process --
2052       -----------------
2053
2054       procedure Add_Process
2055         (Pid    : Process_Id;
2056          Sfile  : File_Name_Type;
2057          Afile  : File_Name_Type;
2058          Uname  : Unit_Name_Type;
2059          Mfile  : Natural := No_Mapping_File)
2060       is
2061          OC1 : constant Positive := Outstanding_Compiles + 1;
2062
2063       begin
2064          pragma Assert (OC1 <= Max_Process);
2065          pragma Assert (Pid /= Invalid_Pid);
2066
2067          Running_Compile (OC1).Pid              := Pid;
2068          Running_Compile (OC1).Full_Source_File := Sfile;
2069          Running_Compile (OC1).Lib_File         := Afile;
2070          Running_Compile (OC1).Source_Unit      := Uname;
2071          Running_Compile (OC1).Mapping_File     := Mfile;
2072          Running_Compile (OC1).Project          := Arguments_Project;
2073          Running_Compile (OC1).Syntax_Only      := Syntax_Only;
2074          Running_Compile (OC1).Output_Is_Object := Output_Is_Object;
2075
2076          Outstanding_Compiles := OC1;
2077       end Add_Process;
2078
2079       --------------------
2080       -- Await_Compile --
2081       -------------------
2082
2083       procedure Await_Compile
2084         (Sfile  : out File_Name_Type;
2085          Afile  : out File_Name_Type;
2086          Uname  : out File_Name_Type;
2087          OK     : out Boolean)
2088       is
2089          Pid : Process_Id;
2090          Project : Project_Id;
2091
2092       begin
2093          pragma Assert (Outstanding_Compiles > 0);
2094
2095          Sfile := No_File;
2096          Afile := No_File;
2097          Uname := No_Name;
2098          OK    := False;
2099
2100          --  The loop here is a work-around for a problem on VMS; in some
2101          --  circumstances (shared library and several executables, for
2102          --  example), there are child processes other than compilation
2103          --  processes that are received. Until this problem is resolved,
2104          --  we will ignore such processes.
2105
2106          loop
2107             Wait_Process (Pid, OK);
2108
2109             if Pid = Invalid_Pid then
2110                return;
2111             end if;
2112
2113             for J in Running_Compile'First .. Outstanding_Compiles loop
2114                if Pid = Running_Compile (J).Pid then
2115                   Sfile := Running_Compile (J).Full_Source_File;
2116                   Afile := Running_Compile (J).Lib_File;
2117                   Uname := Running_Compile (J).Source_Unit;
2118                   Syntax_Only := Running_Compile (J).Syntax_Only;
2119                   Output_Is_Object := Running_Compile (J).Output_Is_Object;
2120                   Project := Running_Compile (J).Project;
2121
2122                   --  If a mapping file was used by this compilation,
2123                   --  get its file name for reuse by a subsequent compilation
2124
2125                   if Running_Compile (J).Mapping_File /= No_Mapping_File then
2126                      Last_Free_Indices (Project) :=
2127                        Last_Free_Indices (Project) + 1;
2128                      The_Free_Mapping_File_Indices
2129                        (Project, Last_Free_Indices (Project)) :=
2130                        Running_Compile (J).Mapping_File;
2131                   end if;
2132
2133                   --  To actually remove this Pid and related info from
2134                   --  Running_Compile replace its entry with the last valid
2135                   --  entry in Running_Compile.
2136
2137                   if J = Outstanding_Compiles then
2138                      null;
2139
2140                   else
2141                      Running_Compile (J) :=
2142                        Running_Compile (Outstanding_Compiles);
2143                   end if;
2144
2145                   Outstanding_Compiles := Outstanding_Compiles - 1;
2146                   return;
2147                end if;
2148             end loop;
2149
2150             --  This child process was not one of our compilation processes;
2151             --  just ignore it for now.
2152
2153             --  raise Program_Error;
2154          end loop;
2155       end Await_Compile;
2156
2157       ---------------------------
2158       -- Bad_Compilation_Count --
2159       ---------------------------
2160
2161       function Bad_Compilation_Count return Natural is
2162       begin
2163          return Bad_Compilation.Last - Bad_Compilation.First + 1;
2164       end Bad_Compilation_Count;
2165
2166       ----------------------------
2167       -- Check_Standard_Library --
2168       ----------------------------
2169
2170       procedure Check_Standard_Library is
2171       begin
2172          Need_To_Check_Standard_Library := False;
2173
2174          if not Targparm.Suppress_Standard_Library_On_Target then
2175             declare
2176                Sfile  : Name_Id;
2177                Add_It : Boolean := True;
2178
2179             begin
2180                Name_Len := Standard_Library_Package_Body_Name'Length;
2181                Name_Buffer (1 .. Name_Len) :=
2182                  Standard_Library_Package_Body_Name;
2183                Sfile := Name_Enter;
2184
2185                --  If we have a special runtime, we add the standard
2186                --  library only if we can find it.
2187
2188                if RTS_Switch then
2189                   Add_It :=
2190                     Find_File (Sfile, Osint.Source) /= No_File;
2191                end if;
2192
2193                if Add_It then
2194                   if Is_Marked (Sfile) then
2195                      if Is_In_Obsoleted (Sfile) then
2196                         Executable_Obsolete := True;
2197                      end if;
2198
2199                   else
2200                      Insert_Q (Sfile, Index => 0);
2201                      Mark (Sfile, Index => 0);
2202                   end if;
2203                end if;
2204             end;
2205          end if;
2206       end Check_Standard_Library;
2207
2208       -----------------------------------
2209       -- Collect_Arguments_And_Compile --
2210       -----------------------------------
2211
2212       procedure Collect_Arguments_And_Compile
2213         (Source_File : File_Name_Type; Source_Index : Int)
2214       is
2215       begin
2216          --  Process_Created will be set True if an attempt is made to compile
2217          --  the source, that is if it is not in an externally built project.
2218
2219          Process_Created := False;
2220
2221          --  If arguments not yet collected (in Check), collect them now
2222
2223          if not Arguments_Collected then
2224             Collect_Arguments (Source_File, Source_Index, Args);
2225          end if;
2226
2227          --  If we use mapping file (-P or -C switches), then get one
2228
2229          if Create_Mapping_File then
2230             Get_Mapping_File (Arguments_Project);
2231          end if;
2232
2233          --  If the source is part of a project file, we set the ADA_*_PATHs,
2234          --  check for an eventual library project, and use the full path.
2235
2236          if Arguments_Project /= No_Project then
2237             if not Project_Tree.Projects.Table
2238                      (Arguments_Project).Externally_Built
2239             then
2240                Prj.Env.Set_Ada_Paths
2241                  (Arguments_Project, Project_Tree, True);
2242
2243                if not Unique_Compile
2244                  and then MLib.Tgt.Support_For_Libraries /= MLib.Tgt.None
2245                then
2246                   declare
2247                      The_Data : Project_Data :=
2248                                   Project_Tree.Projects.Table
2249                                     (Arguments_Project);
2250
2251                      Prj : Project_Id := Arguments_Project;
2252
2253                   begin
2254                      while The_Data.Extended_By /= No_Project loop
2255                         Prj := The_Data.Extended_By;
2256                         The_Data := Project_Tree.Projects.Table (Prj);
2257                      end loop;
2258
2259                      if The_Data.Library
2260                        and then not The_Data.Need_To_Build_Lib
2261                      then
2262                         --  Add to the Q all sources of the project that
2263                         --  have not been marked
2264
2265                         Insert_Project_Sources
2266                           (The_Project  => Prj,
2267                            All_Projects => False,
2268                            Into_Q       => True);
2269
2270                         --  Now mark the project as processed
2271
2272                         Project_Tree.Projects.Table
2273                           (Prj).Need_To_Build_Lib := True;
2274                      end if;
2275                   end;
2276                end if;
2277
2278                --  Change to the object directory of the project file,
2279                --  if necessary.
2280
2281                Change_To_Object_Directory (Arguments_Project);
2282
2283                Pid := Compile (Arguments_Path_Name, Lib_File, Source_Index,
2284                                Arguments (1 .. Last_Argument));
2285                Process_Created := True;
2286             end if;
2287
2288          else
2289             --  If this is a source outside of any project file, make sure it
2290             --  will be compiled in object directory of the main project file.
2291
2292             if Main_Project /= No_Project then
2293                Change_To_Object_Directory (Arguments_Project);
2294             end if;
2295
2296             Pid := Compile (Full_Source_File, Lib_File, Source_Index,
2297                             Arguments (1 .. Last_Argument));
2298             Process_Created := True;
2299          end if;
2300       end Collect_Arguments_And_Compile;
2301
2302       -------------
2303       -- Compile --
2304       -------------
2305
2306       function Compile
2307         (S            : Name_Id;
2308          L            : Name_Id;
2309          Source_Index : Int;
2310          Args         : Argument_List) return Process_Id
2311       is
2312          Comp_Args : Argument_List (Args'First .. Args'Last + 9);
2313          Comp_Next : Integer := Args'First;
2314          Comp_Last : Integer;
2315
2316          function Ada_File_Name (Name : Name_Id) return Boolean;
2317          --  Returns True if Name is the name of an ada source file
2318          --  (i.e. suffix is .ads or .adb)
2319
2320          -------------------
2321          -- Ada_File_Name --
2322          -------------------
2323
2324          function Ada_File_Name (Name : Name_Id) return Boolean is
2325          begin
2326             Get_Name_String (Name);
2327             return
2328               Name_Len > 4
2329                 and then Name_Buffer (Name_Len - 3 .. Name_Len - 1) = ".ad"
2330                 and then (Name_Buffer (Name_Len) = 'b'
2331                             or else
2332                           Name_Buffer (Name_Len) = 's');
2333          end Ada_File_Name;
2334
2335       --  Start of processing for Compile
2336
2337       begin
2338          Enter_Into_Obsoleted (S);
2339
2340          --  By default, Syntax_Only is False
2341
2342          Syntax_Only := False;
2343
2344          for J in Args'Range loop
2345             if Args (J).all = "-gnats" then
2346
2347                --  If we compile with -gnats, the bind step and the link step
2348                --  are inhibited. Also, we set Syntax_Only to True, so that
2349                --  we don't fail when we don't find the ALI file, after
2350                --  compilation.
2351
2352                Do_Bind_Step := False;
2353                Do_Link_Step := False;
2354                Syntax_Only  := True;
2355
2356             elsif Args (J).all = "-gnatc" then
2357
2358                --  If we compile with -gnatc, the bind step and the link step
2359                --  are inhibited. We set Syntax_Only to False for the case when
2360                --  -gnats was previously specified.
2361
2362                Do_Bind_Step := False;
2363                Do_Link_Step := False;
2364                Syntax_Only  := False;
2365             end if;
2366          end loop;
2367
2368          Comp_Args (Comp_Next) := Comp_Flag;
2369          Comp_Next := Comp_Next + 1;
2370
2371          --  Optimize the simple case where the gcc command line looks like
2372          --     gcc -c -I. ... -I- file.adb  --into->  gcc -c ... file.adb
2373
2374          if Args (Args'First).all = "-I" & Normalized_CWD
2375            and then Args (Args'Last).all = "-I-"
2376            and then S = Strip_Directory (S)
2377          then
2378             Comp_Last := Comp_Next + Args'Length - 3;
2379             Comp_Args (Comp_Next .. Comp_Last) :=
2380               Args (Args'First + 1 .. Args'Last - 1);
2381
2382          else
2383             Comp_Last := Comp_Next + Args'Length - 1;
2384             Comp_Args (Comp_Next .. Comp_Last) := Args;
2385          end if;
2386
2387          --  Set -gnatpg for predefined files (for this purpose the renamings
2388          --  such as Text_IO do not count as predefined). Note that we strip
2389          --  the directory name from the source file name becase the call to
2390          --  Fname.Is_Predefined_File_Name cannot deal with directory prefixes.
2391
2392          declare
2393             Fname : constant File_Name_Type := Strip_Directory (S);
2394
2395          begin
2396             if Is_Predefined_File_Name (Fname, False) then
2397                if Check_Readonly_Files then
2398                   Comp_Last := Comp_Last + 1;
2399                   Comp_Args (Comp_Last) := GNAT_Flag;
2400
2401                else
2402                   Make_Failed
2403                     ("not allowed to compile """ &
2404                      Get_Name_String (Fname) &
2405                      """; use -a switch, or compile file with " &
2406                      """-gnatg"" switch");
2407                end if;
2408             end if;
2409          end;
2410
2411          --  Now check if the file name has one of the suffixes familiar to
2412          --  the gcc driver. If this is not the case then add the ada flag
2413          --  "-x ada".
2414
2415          if not Ada_File_Name (S) and then not Targparm.AAMP_On_Target then
2416             Comp_Last := Comp_Last + 1;
2417             Comp_Args (Comp_Last) := Ada_Flag_1;
2418             Comp_Last := Comp_Last + 1;
2419             Comp_Args (Comp_Last) := Ada_Flag_2;
2420          end if;
2421
2422          if Source_Index /= 0 then
2423             declare
2424                Num : constant String := Source_Index'Img;
2425             begin
2426                Comp_Last := Comp_Last + 1;
2427                Comp_Args (Comp_Last) :=
2428                  new String'("-gnateI" & Num (Num'First + 1 .. Num'Last));
2429             end;
2430          end if;
2431
2432          if Source_Index /= 0 or else
2433            L /= Strip_Directory (L) or else
2434            Object_Directory_Path /= null
2435          then
2436             --  Build -o argument
2437
2438             Get_Name_String (L);
2439
2440             for J in reverse 1 .. Name_Len loop
2441                if Name_Buffer (J) = '.' then
2442                   Name_Len := J + Object_Suffix'Length - 1;
2443                   Name_Buffer (J .. Name_Len) := Object_Suffix;
2444                   exit;
2445                end if;
2446             end loop;
2447
2448             Comp_Last := Comp_Last + 1;
2449             Comp_Args (Comp_Last) := Output_Flag;
2450             Comp_Last := Comp_Last + 1;
2451
2452             --  If an object directory was specified, prepend the object file
2453             --  name with this object directory.
2454
2455             if Object_Directory_Path /= null then
2456                Comp_Args (Comp_Last) :=
2457                  new String'(Object_Directory_Path.all &
2458                                Name_Buffer (1 .. Name_Len));
2459
2460             else
2461                Comp_Args (Comp_Last) :=
2462                  new String'(Name_Buffer (1 .. Name_Len));
2463             end if;
2464          end if;
2465
2466          if Create_Mapping_File then
2467             Comp_Last := Comp_Last + 1;
2468             Comp_Args (Comp_Last) := Mapping_File_Arg;
2469          end if;
2470
2471          Get_Name_String (S);
2472
2473          Comp_Last := Comp_Last + 1;
2474          Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
2475
2476          GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
2477
2478          Comp_Last := Comp_Last + 1;
2479          Comp_Args (Comp_Last) := new String'("-gnatez");
2480
2481          Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
2482
2483          if Gcc_Path = null then
2484             Make_Failed ("error, unable to locate ", Gcc.all);
2485          end if;
2486
2487          return
2488            GNAT.OS_Lib.Non_Blocking_Spawn
2489              (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
2490       end Compile;
2491
2492       ----------------------
2493       -- Get_Mapping_File --
2494       ----------------------
2495
2496       procedure Get_Mapping_File (Project : Project_Id) is
2497       begin
2498          --  If there is a mapping file ready to be reused, reuse it
2499
2500          if Last_Free_Indices (Project) > 0 then
2501             Mfile := The_Free_Mapping_File_Indices
2502                        (Project, Last_Free_Indices (Project));
2503             Last_Free_Indices (Project) := Last_Free_Indices (Project) - 1;
2504
2505          --  Otherwise, create and initialize a new one
2506
2507          else
2508             Init_Mapping_File (Project => Project, File_Index => Mfile);
2509          end if;
2510
2511          --  Put the name in the mapping file argument for the invocation
2512          --  of the compiler.
2513
2514          Free (Mapping_File_Arg);
2515          Mapping_File_Arg :=
2516            new String'("-gnatem=" &
2517                        Get_Name_String
2518                          (The_Mapping_File_Names (Project, Mfile)));
2519
2520       end Get_Mapping_File;
2521
2522       -----------------------
2523       -- Get_Next_Good_ALI --
2524       -----------------------
2525
2526       function Get_Next_Good_ALI return ALI_Id is
2527          ALI : ALI_Id;
2528
2529       begin
2530          pragma Assert (Good_ALI_Present);
2531          ALI := Good_ALI.Table (Good_ALI.Last);
2532          Good_ALI.Decrement_Last;
2533          return ALI;
2534       end Get_Next_Good_ALI;
2535
2536       ----------------------
2537       -- Good_ALI_Present --
2538       ----------------------
2539
2540       function Good_ALI_Present return Boolean is
2541       begin
2542          return Good_ALI.First <= Good_ALI.Last;
2543       end Good_ALI_Present;
2544
2545       --------------------
2546       -- Record_Failure --
2547       --------------------
2548
2549       procedure Record_Failure
2550         (File  : File_Name_Type;
2551          Unit  : Unit_Name_Type;
2552          Found : Boolean := True)
2553       is
2554       begin
2555          Bad_Compilation.Increment_Last;
2556          Bad_Compilation.Table (Bad_Compilation.Last) := (File, Unit, Found);
2557       end Record_Failure;
2558
2559       ---------------------
2560       -- Record_Good_ALI --
2561       ---------------------
2562
2563       procedure Record_Good_ALI (A : ALI_Id) is
2564       begin
2565          Good_ALI.Increment_Last;
2566          Good_ALI.Table (Good_ALI.Last) := A;
2567       end Record_Good_ALI;
2568
2569    --  Start of processing for Compile_Sources
2570
2571    begin
2572       pragma Assert (Args'First = 1);
2573
2574       --  Package and Queue initializations
2575
2576       Good_ALI.Init;
2577       Output.Set_Standard_Error;
2578
2579       if First_Q_Initialization then
2580          Init_Q;
2581       end if;
2582
2583       if Initialize_ALI_Data then
2584          Initialize_ALI;
2585          Initialize_ALI_Source;
2586       end if;
2587
2588       --  The following two flags affect the behavior of ALI.Set_Source_Table.
2589       --  We set Check_Source_Files to True to ensure that source file
2590       --  time stamps are checked, and we set All_Sources to False to
2591       --  avoid checking the presence of the source files listed in the
2592       --  source dependency section of an ali file (which would be a mistake
2593       --  since the ali file may be obsolete).
2594
2595       Check_Source_Files := True;
2596       All_Sources        := False;
2597
2598       --  Only insert in the Q if it is not already done, to avoid simultaneous
2599       --  compilations if -jnnn is used.
2600
2601       if not Is_Marked (Main_Source, Main_Index) then
2602          Insert_Q (Main_Source, Index => Main_Index);
2603          Mark (Main_Source, Main_Index);
2604       end if;
2605
2606       First_Compiled_File   := No_File;
2607       Most_Recent_Obj_File  := No_File;
2608       Most_Recent_Obj_Stamp := Empty_Time_Stamp;
2609       Main_Unit             := False;
2610
2611       --  Keep looping until there is no more work to do (the Q is empty)
2612       --  and all the outstanding compilations have terminated
2613
2614       Make_Loop : while not Empty_Q or else Outstanding_Compiles > 0 loop
2615
2616          --  If the user does not want to keep going in case of errors then
2617          --  wait for the remaining outstanding compiles and then exit.
2618
2619          if Bad_Compilation_Count > 0 and then not Keep_Going then
2620             while Outstanding_Compiles > 0 loop
2621                Await_Compile
2622                  (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
2623
2624                if not Compilation_OK then
2625                   Record_Failure (Full_Source_File, Source_Unit);
2626                end if;
2627             end loop;
2628
2629             exit Make_Loop;
2630          end if;
2631
2632          --  PHASE 1: Check if there is more work that we can do (ie the Q
2633          --  is non empty). If there is, do it only if we have not yet used
2634          --  up all the available processes.
2635
2636          if not Empty_Q and then Outstanding_Compiles < Max_Process then
2637             declare
2638                Source_Index : Int;
2639                --  Index of the current unit in the current source file
2640
2641             begin
2642                Extract_From_Q (Source_File, Source_Unit, Source_Index);
2643                Full_Source_File := Osint.Full_Source_Name (Source_File);
2644                Lib_File         := Osint.Lib_File_Name
2645                  (Source_File, Source_Index);
2646                Full_Lib_File    := Osint.Full_Lib_File_Name (Lib_File);
2647
2648                --  If this source has already been compiled, the executable is
2649                --  obsolete.
2650
2651                if Is_In_Obsoleted (Source_File) then
2652                   Executable_Obsolete := True;
2653                end if;
2654
2655                --  If the library file is an Ada library skip it
2656
2657                if Full_Lib_File /= No_File
2658                  and then In_Ada_Lib_Dir (Full_Lib_File)
2659                then
2660                   Verbose_Msg
2661                     (Lib_File, "is in an Ada library", Prefix => "  ");
2662
2663                   --  If the library file is a read-only library skip it, but
2664                   --  only if, when using project files, this library file is
2665                   --  in the right object directory (a read-only ALI file
2666                   --  in the object directory of a project being extended
2667                   --  should not be skipped).
2668
2669                elsif Full_Lib_File /= No_File
2670                  and then not Check_Readonly_Files
2671                  and then Is_Readonly_Library (Full_Lib_File)
2672                  and then Is_In_Object_Directory (Source_File, Full_Lib_File)
2673                then
2674                   Verbose_Msg
2675                     (Lib_File, "is a read-only library", Prefix => "  ");
2676
2677                   --  The source file that we are checking cannot be located
2678
2679                elsif Full_Source_File = No_File then
2680                   Record_Failure (Source_File, Source_Unit, False);
2681
2682                   --  Source and library files can be located but are internal
2683                   --  files
2684
2685                elsif not Check_Readonly_Files
2686                  and then Full_Lib_File /= No_File
2687                  and then Is_Internal_File_Name (Source_File)
2688                then
2689                   if Force_Compilations then
2690                      Fail
2691                        ("not allowed to compile """ &
2692                         Get_Name_String (Source_File) &
2693                         """; use -a switch, or compile file with " &
2694                         """-gnatg"" switch");
2695                   end if;
2696
2697                   Verbose_Msg
2698                     (Lib_File, "is an internal library", Prefix => "  ");
2699
2700                --  The source file that we are checking can be located
2701
2702                else
2703                   Arguments_Collected := False;
2704
2705                   --  Don't waste any time if we have to recompile anyway
2706
2707                   Obj_Stamp       := Empty_Time_Stamp;
2708                   Need_To_Compile := Force_Compilations;
2709
2710                   if not Force_Compilations then
2711                      Read_Only :=
2712                        Full_Lib_File /= No_File
2713                        and then not Check_Readonly_Files
2714                        and then Is_Readonly_Library (Full_Lib_File);
2715                      Check (Source_File, Source_Index, Args, Lib_File,
2716                             Read_Only, ALI, Obj_File, Obj_Stamp);
2717                      Need_To_Compile := (ALI = No_ALI_Id);
2718                   end if;
2719
2720                   if not Need_To_Compile then
2721
2722                      --  The ALI file is up-to-date. Record its Id
2723
2724                      Record_Good_ALI (ALI);
2725
2726                      --  Record the time stamp of the most recent object file
2727                      --  as long as no (re)compilations are needed.
2728
2729                      if First_Compiled_File = No_File
2730                        and then (Most_Recent_Obj_File = No_File
2731                                    or else Obj_Stamp > Most_Recent_Obj_Stamp)
2732                      then
2733                         Most_Recent_Obj_File  := Obj_File;
2734                         Most_Recent_Obj_Stamp := Obj_Stamp;
2735                      end if;
2736
2737                   else
2738                      --  Is this the first file we have to compile?
2739
2740                      if First_Compiled_File = No_File then
2741                         First_Compiled_File  := Full_Source_File;
2742                         Most_Recent_Obj_File := No_File;
2743
2744                         if Do_Not_Execute then
2745                            exit Make_Loop;
2746                         end if;
2747                      end if;
2748
2749                      if In_Place_Mode then
2750
2751                         --  If the library file was not found, then save the
2752                         --  library file near the source file.
2753
2754                         if Full_Lib_File = No_File then
2755                            Lib_File := Osint.Lib_File_Name
2756                              (Full_Source_File, Source_Index);
2757
2758                            --  If the library file was found, then save the
2759                            --  library file in the same place.
2760
2761                         else
2762                            Lib_File := Full_Lib_File;
2763                         end if;
2764
2765                      end if;
2766
2767                      --  Start the compilation and record it. We can do this
2768                      --  because there is at least one free process.
2769
2770                      Collect_Arguments_And_Compile (Source_File, Source_Index);
2771
2772                      --  Make sure we could successfully start the compilation
2773
2774                      if Process_Created then
2775                         if Pid = Invalid_Pid then
2776                            Record_Failure (Full_Source_File, Source_Unit);
2777                         else
2778                            Add_Process
2779                              (Pid,
2780                               Full_Source_File,
2781                               Lib_File,
2782                               Source_Unit,
2783                               Mfile);
2784                         end if;
2785                      end if;
2786                   end if;
2787                end if;
2788             end;
2789          end if;
2790
2791          --  PHASE 2: Now check if we should wait for a compilation to
2792          --  finish. This is the case if all the available processes are
2793          --  busy compiling sources or there is nothing else to do
2794          --  (that is the Q is empty and there are no good ALIs to process).
2795
2796          if Outstanding_Compiles = Max_Process
2797            or else (Empty_Q
2798                      and then not Good_ALI_Present
2799                      and then Outstanding_Compiles > 0)
2800          then
2801             Await_Compile
2802               (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
2803
2804             if not Compilation_OK then
2805                Record_Failure (Full_Source_File, Source_Unit);
2806             end if;
2807
2808             if Compilation_OK or else Keep_Going then
2809
2810                --  Re-read the updated library file
2811
2812                declare
2813                   Saved_Object_Consistency : constant Boolean :=
2814                                                Check_Object_Consistency;
2815
2816                begin
2817                   --  If compilation was not OK, or if output is not an
2818                   --  object file and we don't do the bind step, don't check
2819                   --  for object consistency.
2820
2821                   Check_Object_Consistency :=
2822                     Check_Object_Consistency
2823                     and Compilation_OK
2824                     and (Output_Is_Object or Do_Bind_Step);
2825                   Text := Read_Library_Info (Lib_File);
2826
2827                   --  Restore Check_Object_Consistency to its initial value
2828
2829                   Check_Object_Consistency := Saved_Object_Consistency;
2830                end;
2831
2832                --  If an ALI file was generated by this compilation, scan
2833                --  the ALI file and record it.
2834                --  If the scan fails, a previous ali file is inconsistent with
2835                --  the unit just compiled.
2836
2837                if Text /= null then
2838                   ALI :=
2839                     Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
2840
2841                   if ALI = No_ALI_Id then
2842
2843                      --  Record a failure only if not already done
2844
2845                      if Compilation_OK then
2846                         Inform
2847                           (Lib_File,
2848                            "incompatible ALI file, please recompile");
2849                         Record_Failure (Full_Source_File, Source_Unit);
2850                      end if;
2851                   else
2852                      Free (Text);
2853                      Record_Good_ALI (ALI);
2854                   end if;
2855
2856                --  If we could not read the ALI file that was just generated
2857                --  then there could be a problem reading either the ALI or the
2858                --  corresponding object file (if Check_Object_Consistency
2859                --  is set Read_Library_Info checks that the time stamp of the
2860                --  object file is more recent than that of the ALI). For an
2861                --  example of problems caught by this test see [6625-009].
2862                --  However, we record a failure only if not already done.
2863
2864                else
2865                   if Compilation_OK and not Syntax_Only then
2866                      Inform
2867                        (Lib_File,
2868                         "WARNING: ALI or object file not found after compile");
2869                      Record_Failure (Full_Source_File, Source_Unit);
2870                   end if;
2871                end if;
2872             end if;
2873          end if;
2874
2875          --  PHASE 3: Check if we recorded good ALI files. If yes process
2876          --  them now in the order in which they have been recorded. There
2877          --  are two occasions in which we record good ali files. The first is
2878          --  in phase 1 when, after scanning an existing ALI file we realize
2879          --  it is up-to-date, the second instance is after a successful
2880          --  compilation.
2881
2882          while Good_ALI_Present loop
2883             ALI := Get_Next_Good_ALI;
2884
2885             declare
2886                Source_Index : Int := Unit_Index_Of (ALIs.Table (ALI).Afile);
2887
2888             begin
2889                --  If we are processing the library file corresponding to the
2890                --  main source file check if this source can be a main unit.
2891
2892                if ALIs.Table (ALI).Sfile = Main_Source and then
2893                  Source_Index = Main_Index
2894                then
2895                   Main_Unit := ALIs.Table (ALI).Main_Program /= None;
2896                end if;
2897
2898                --  The following adds the standard library (s-stalib) to the
2899                --  list of files to be handled by gnatmake: this file and any
2900                --  files it depends on are always included in every bind,
2901                --  even if they are not in the explicit dependency list.
2902                --  Of course, it is not added if Suppress_Standard_Library
2903                --  is True.
2904
2905                --  However, to avoid annoying output about s-stalib.ali being
2906                --  read only, when "-v" is used, we add the standard library
2907                --  only when "-a" is used.
2908
2909                if Need_To_Check_Standard_Library then
2910                   Check_Standard_Library;
2911                end if;
2912
2913                --  Now insert in the Q the unmarked source files (i.e. those
2914                --  which have never been inserted in the Q and hence never
2915                --  considered). Only do that if Unique_Compile is False.
2916
2917                if not Unique_Compile then
2918                   for J in
2919                     ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
2920                   loop
2921                      for K in
2922                        Units.Table (J).First_With .. Units.Table (J).Last_With
2923                      loop
2924                         Sfile := Withs.Table (K).Sfile;
2925                         Add_Dependency (ALIs.Table (ALI).Sfile, Sfile);
2926
2927                         if Is_In_Obsoleted (Sfile) then
2928                            Executable_Obsolete := True;
2929                         end if;
2930
2931                         if Sfile = No_File then
2932                            Debug_Msg
2933                              ("Skipping generic:", Withs.Table (K).Uname);
2934
2935                         else
2936                            Source_Index :=
2937                              Unit_Index_Of (Withs.Table (K).Afile);
2938
2939                            if Is_Marked (Sfile, Source_Index) then
2940                               Debug_Msg ("Skipping marked file:", Sfile);
2941
2942                            elsif not Check_Readonly_Files
2943                              and then Is_Internal_File_Name (Sfile)
2944                            then
2945                               Debug_Msg ("Skipping internal file:", Sfile);
2946
2947                            else
2948                               Insert_Q
2949                                 (Sfile, Withs.Table (K).Uname, Source_Index);
2950                               Mark (Sfile, Source_Index);
2951                            end if;
2952                         end if;
2953                      end loop;
2954                   end loop;
2955                end if;
2956             end;
2957          end loop;
2958
2959          if Display_Compilation_Progress then
2960             Write_Str ("completed ");
2961             Write_Int (Int (Q_Front));
2962             Write_Str (" out of ");
2963             Write_Int (Int (Q.Last));
2964             Write_Str (" (");
2965             Write_Int (Int ((Q_Front * 100) / (Q.Last - Q.First)));
2966             Write_Str ("%)...");
2967             Write_Eol;
2968          end if;
2969       end loop Make_Loop;
2970
2971       Compilation_Failures := Bad_Compilation_Count;
2972
2973       --  Compilation is finished
2974
2975       --  Delete any temporary configuration pragma file
2976
2977       Delete_Temp_Config_Files;
2978
2979    end Compile_Sources;
2980
2981    ----------------------------------
2982    -- Configuration_Pragmas_Switch --
2983    ----------------------------------
2984
2985    function Configuration_Pragmas_Switch
2986      (For_Project : Project_Id) return Argument_List
2987    is
2988       The_Packages : Package_Id;
2989       Gnatmake     : Package_Id;
2990       Compiler     : Package_Id;
2991
2992       Global_Attribute : Variable_Value := Nil_Variable_Value;
2993       Local_Attribute  : Variable_Value := Nil_Variable_Value;
2994
2995       Global_Attribute_Present : Boolean := False;
2996       Local_Attribute_Present  : Boolean := False;
2997
2998       Result : Argument_List (1 .. 3);
2999       Last   : Natural := 0;
3000
3001       function Absolute_Path
3002         (Path    : Name_Id;
3003          Project : Project_Id) return String;
3004       --  Returns an absolute path for a configuration pragmas file
3005
3006       -------------------
3007       -- Absolute_Path --
3008       -------------------
3009
3010       function Absolute_Path
3011         (Path    : Name_Id;
3012          Project : Project_Id) return String
3013       is
3014       begin
3015          Get_Name_String (Path);
3016
3017          declare
3018             Path_Name : constant String := Name_Buffer (1 .. Name_Len);
3019
3020          begin
3021             if Is_Absolute_Path (Path_Name) then
3022                return Path_Name;
3023
3024             else
3025                declare
3026                   Parent_Directory : constant String :=
3027                     Get_Name_String
3028                       (Project_Tree.Projects.Table
3029                            (Project).Directory);
3030
3031                begin
3032                   if Parent_Directory (Parent_Directory'Last) =
3033                                                  Directory_Separator
3034                   then
3035                      return Parent_Directory & Path_Name;
3036
3037                   else
3038                      return Parent_Directory & Directory_Separator & Path_Name;
3039                   end if;
3040                end;
3041             end if;
3042          end;
3043       end Absolute_Path;
3044
3045    --  Start of processing for Configuration_Pragmas_Switch
3046
3047    begin
3048       Prj.Env.Create_Config_Pragmas_File
3049         (For_Project, Main_Project, Project_Tree);
3050
3051       if Project_Tree.Projects.Table
3052            (For_Project).Config_File_Name /= No_Name
3053       then
3054          Temporary_Config_File :=
3055            Project_Tree.Projects.Table (For_Project).Config_File_Temp;
3056          Last := 1;
3057          Result (1) :=
3058            new String'
3059                  ("-gnatec=" &
3060                   Get_Name_String
3061                     (Project_Tree.Projects.Table
3062                        (For_Project).Config_File_Name));
3063
3064       else
3065          Temporary_Config_File := False;
3066       end if;
3067
3068       --  Check for attribute Builder'Global_Configuration_Pragmas
3069
3070       The_Packages := Project_Tree.Projects.Table
3071                         (Main_Project).Decl.Packages;
3072       Gnatmake :=
3073         Prj.Util.Value_Of
3074           (Name        => Name_Builder,
3075            In_Packages => The_Packages,
3076            In_Tree     => Project_Tree);
3077
3078       if Gnatmake /= No_Package then
3079          Global_Attribute := Prj.Util.Value_Of
3080            (Variable_Name => Name_Global_Configuration_Pragmas,
3081             In_Variables  => Project_Tree.Packages.Table
3082                                (Gnatmake).Decl.Attributes,
3083             In_Tree       => Project_Tree);
3084          Global_Attribute_Present :=
3085            Global_Attribute /= Nil_Variable_Value
3086            and then Get_Name_String (Global_Attribute.Value) /= "";
3087
3088          if Global_Attribute_Present then
3089             declare
3090                Path : constant String :=
3091                         Absolute_Path
3092                           (Global_Attribute.Value, Global_Attribute.Project);
3093             begin
3094                if not Is_Regular_File (Path) then
3095                   Make_Failed
3096                     ("cannot find configuration pragmas file ", Path);
3097                end if;
3098
3099                Last := Last + 1;
3100                Result (Last) := new String'("-gnatec=" &  Path);
3101             end;
3102          end if;
3103       end if;
3104
3105       --  Check for attribute Compiler'Local_Configuration_Pragmas
3106
3107       The_Packages :=
3108         Project_Tree.Projects.Table (For_Project).Decl.Packages;
3109       Compiler :=
3110         Prj.Util.Value_Of
3111           (Name        => Name_Compiler,
3112            In_Packages => The_Packages,
3113            In_Tree     => Project_Tree);
3114
3115       if Compiler /= No_Package then
3116          Local_Attribute := Prj.Util.Value_Of
3117            (Variable_Name => Name_Local_Configuration_Pragmas,
3118             In_Variables  => Project_Tree.Packages.Table
3119                                (Compiler).Decl.Attributes,
3120             In_Tree       => Project_Tree);
3121          Local_Attribute_Present :=
3122            Local_Attribute /= Nil_Variable_Value
3123            and then Get_Name_String (Local_Attribute.Value) /= "";
3124
3125          if Local_Attribute_Present then
3126             declare
3127                Path : constant String :=
3128                         Absolute_Path
3129                           (Local_Attribute.Value, Local_Attribute.Project);
3130             begin
3131                if not Is_Regular_File (Path) then
3132                   Make_Failed
3133                     ("cannot find configuration pragmas file ", Path);
3134                end if;
3135
3136                Last := Last + 1;
3137                Result (Last) := new String'("-gnatec=" & Path);
3138             end;
3139          end if;
3140       end if;
3141
3142       return Result (1 .. Last);
3143    end Configuration_Pragmas_Switch;
3144
3145    ---------------
3146    -- Debug_Msg --
3147    ---------------
3148
3149    procedure Debug_Msg (S : String; N : Name_Id) is
3150    begin
3151       if Debug.Debug_Flag_W then
3152          Write_Str ("   ... ");
3153          Write_Str (S);
3154          Write_Str (" ");
3155          Write_Name (N);
3156          Write_Eol;
3157       end if;
3158    end Debug_Msg;
3159
3160    ---------------------------
3161    -- Delete_All_Temp_Files --
3162    ---------------------------
3163
3164    procedure Delete_All_Temp_Files is
3165    begin
3166       if Gnatmake_Called and not Debug.Debug_Flag_N then
3167          Delete_Mapping_Files;
3168          Delete_Temp_Config_Files;
3169          Prj.Env.Delete_All_Path_Files (Project_Tree);
3170       end if;
3171    end Delete_All_Temp_Files;
3172
3173    --------------------------
3174    -- Delete_Mapping_Files --
3175    --------------------------
3176
3177    procedure Delete_Mapping_Files is
3178       Success : Boolean;
3179    begin
3180       if not Debug.Debug_Flag_N then
3181          if The_Mapping_File_Names /= null then
3182             for Project in The_Mapping_File_Names'Range (1) loop
3183                for Index in 1 .. Last_Mapping_File_Names (Project) loop
3184                   Delete_File
3185                     (Name => Get_Name_String
3186                                (The_Mapping_File_Names (Project, Index)),
3187                      Success => Success);
3188                end loop;
3189             end loop;
3190          end if;
3191       end if;
3192    end Delete_Mapping_Files;
3193
3194    ------------------------------
3195    -- Delete_Temp_Config_Files --
3196    ------------------------------
3197
3198    procedure Delete_Temp_Config_Files is
3199       Success : Boolean;
3200    begin
3201       if (not Debug.Debug_Flag_N) and Main_Project /= No_Project then
3202          for Project in Project_Table.First ..
3203                         Project_Table.Last (Project_Tree.Projects)
3204          loop
3205             if
3206               Project_Tree.Projects.Table (Project).Config_File_Temp
3207             then
3208                if Verbose_Mode then
3209                   Write_Str ("Deleting temp configuration file """);
3210                   Write_Str (Get_Name_String
3211                                (Project_Tree.Projects.Table
3212                                   (Project).Config_File_Name));
3213                   Write_Line ("""");
3214                end if;
3215
3216                Delete_File
3217                  (Name    => Get_Name_String
3218                                (Project_Tree.Projects.Table
3219                                   (Project).Config_File_Name),
3220                   Success => Success);
3221
3222                --  Make sure that we don't have a config file for this
3223                --  project, in case when there are several mains.
3224                --  In this case, we will recreate another config file:
3225                --  we cannot reuse the one that we just deleted!
3226
3227                Project_Tree.Projects.Table (Project).
3228                  Config_Checked := False;
3229                Project_Tree.Projects.Table (Project).
3230                  Config_File_Name := No_Name;
3231                Project_Tree.Projects.Table (Project).
3232                  Config_File_Temp := False;
3233             end if;
3234          end loop;
3235       end if;
3236    end Delete_Temp_Config_Files;
3237
3238    -------------
3239    -- Display --
3240    -------------
3241
3242    procedure Display (Program : String; Args : Argument_List) is
3243    begin
3244       pragma Assert (Args'First = 1);
3245
3246       if Display_Executed_Programs then
3247          Write_Str (Program);
3248
3249          for J in Args'Range loop
3250
3251             --  Never display -gnatez
3252
3253             if Args (J).all /= "-gnatez" then
3254
3255                --  Do not display the mapping file argument automatically
3256                --  created when using a project file.
3257
3258                if Main_Project = No_Project
3259                  or else Debug.Debug_Flag_N
3260                  or else Args (J)'Length < 8
3261                  or else
3262                    Args (J) (Args (J)'First .. Args (J)'First + 6) /= "-gnatem"
3263                then
3264                   --  When -dn is not specified, do not display the config
3265                   --  pragmas switch (-gnatec) for the temporary file created
3266                   --  by the project manager (always the first -gnatec switch).
3267                   --  Reset Temporary_Config_File to False so that the eventual
3268                   --  other -gnatec switches will be displayed.
3269
3270                   if (not Debug.Debug_Flag_N)
3271                     and then Temporary_Config_File
3272                     and then Args (J)'Length > 7
3273                     and then Args (J) (Args (J)'First .. Args (J)'First + 6)
3274                     = "-gnatec"
3275                   then
3276                      Temporary_Config_File := False;
3277
3278                      --  Do not display the -F=mapping_file switch for
3279                      --  gnatbind, if -dn is not specified.
3280
3281                   elsif Debug.Debug_Flag_N
3282                     or else Args (J)'Length < 4
3283                     or else
3284                       Args (J) (Args (J)'First .. Args (J)'First + 2) /= "-F="
3285                   then
3286                      Write_Str (" ");
3287                      Write_Str (Args (J).all);
3288                   end if;
3289                end if;
3290             end if;
3291          end loop;
3292
3293          Write_Eol;
3294       end if;
3295    end Display;
3296
3297    ----------------------
3298    -- Display_Commands --
3299    ----------------------
3300
3301    procedure Display_Commands (Display : Boolean := True) is
3302    begin
3303       Display_Executed_Programs := Display;
3304    end Display_Commands;
3305
3306    -------------
3307    -- Empty_Q --
3308    -------------
3309
3310    function Empty_Q return Boolean is
3311    begin
3312       if Debug.Debug_Flag_P then
3313          Write_Str ("   Q := [");
3314
3315          for J in Q_Front .. Q.Last - 1 loop
3316             Write_Str (" ");
3317             Write_Name (Q.Table (J).File);
3318             Write_Eol;
3319             Write_Str ("         ");
3320          end loop;
3321
3322          Write_Str ("]");
3323          Write_Eol;
3324       end if;
3325
3326       return Q_Front >= Q.Last;
3327    end Empty_Q;
3328
3329    --------------------------
3330    -- Enter_Into_Obsoleted --
3331    --------------------------
3332
3333    procedure Enter_Into_Obsoleted (F : Name_Id) is
3334       Name  : constant String := Get_Name_String (F);
3335       First : Natural := Name'Last;
3336       F2    : Name_Id := F;
3337
3338    begin
3339       while First > Name'First
3340         and then Name (First - 1) /= Directory_Separator
3341         and then Name (First - 1) /= '/'
3342       loop
3343          First := First - 1;
3344       end loop;
3345
3346       if First /= Name'First then
3347          Name_Len := 0;
3348          Add_Str_To_Name_Buffer (Name (First .. Name'Last));
3349          F2 := Name_Find;
3350       end if;
3351
3352       Debug_Msg ("New entry in Obsoleted table:", F2);
3353       Obsoleted.Set (F2, True);
3354    end Enter_Into_Obsoleted;
3355
3356    ---------------------
3357    -- Extract_Failure --
3358    ---------------------
3359
3360    procedure Extract_Failure
3361      (File  : out File_Name_Type;
3362       Unit  : out Unit_Name_Type;
3363       Found : out Boolean)
3364    is
3365    begin
3366       File  := Bad_Compilation.Table (Bad_Compilation.Last).File;
3367       Unit  := Bad_Compilation.Table (Bad_Compilation.Last).Unit;
3368       Found := Bad_Compilation.Table (Bad_Compilation.Last).Found;
3369       Bad_Compilation.Decrement_Last;
3370    end Extract_Failure;
3371
3372    --------------------
3373    -- Extract_From_Q --
3374    --------------------
3375
3376    procedure Extract_From_Q
3377      (Source_File  : out File_Name_Type;
3378       Source_Unit  : out Unit_Name_Type;
3379       Source_Index : out Int)
3380    is
3381       File  : constant File_Name_Type := Q.Table (Q_Front).File;
3382       Unit  : constant Unit_Name_Type := Q.Table (Q_Front).Unit;
3383       Index : constant Int            := Q.Table (Q_Front).Index;
3384
3385    begin
3386       if Debug.Debug_Flag_Q then
3387          Write_Str ("   Q := Q - [ ");
3388          Write_Name (File);
3389
3390          if Index /= 0 then
3391             Write_Str (", ");
3392             Write_Int (Index);
3393          end if;
3394
3395          Write_Str (" ]");
3396          Write_Eol;
3397       end if;
3398
3399       Q_Front := Q_Front + 1;
3400       Source_File  := File;
3401       Source_Unit  := Unit;
3402       Source_Index := Index;
3403    end Extract_From_Q;
3404
3405    --------------
3406    -- Gnatmake --
3407    --------------
3408
3409    procedure Gnatmake is
3410       Main_Source_File : File_Name_Type;
3411       --  The source file containing the main compilation unit
3412
3413       Compilation_Failures : Natural;
3414
3415       Total_Compilation_Failures : Natural := 0;
3416
3417       Is_Main_Unit : Boolean;
3418       --  Set to True by Compile_Sources if the Main_Source_File can be a
3419       --  main unit.
3420
3421       Main_ALI_File : File_Name_Type;
3422       --  The ali file corresponding to Main_Source_File
3423
3424       Executable : File_Name_Type := No_File;
3425       --  The file name of an executable
3426
3427       Non_Std_Executable : Boolean := False;
3428       --  Non_Std_Executable is set to True when there is a possibility
3429       --  that the linker will not choose the correct executable file name.
3430
3431       Current_Work_Dir : constant String_Access :=
3432                                     new String'(Get_Current_Dir);
3433       --  The current working directory, used to modify some relative path
3434       --  switches on the command line when a project file is used.
3435
3436       Current_Main_Index : Int := 0;
3437       --  If not zero, the index of the current main unit in its source file
3438
3439       There_Are_Stand_Alone_Libraries : Boolean := False;
3440       --  Set to True when there are Stand-Alone Libraries, so that gnatbind
3441       --  is invoked with the -F switch to force checking of elaboration flags.
3442
3443       Mapping_Path : Name_Id := No_Name;
3444       --  The path name of the mapping file
3445
3446       Discard : Boolean;
3447
3448       procedure Check_Mains;
3449       --  Check that the main subprograms do exist and that they all
3450       --  belong to the same project file.
3451
3452       procedure Create_Binder_Mapping_File
3453         (Args : in out Argument_List; Last_Arg : in out Natural);
3454       --  Create a binder mapping file and add the necessary switch
3455
3456       -----------------
3457       -- Check_Mains --
3458       -----------------
3459
3460       procedure Check_Mains is
3461          Real_Main_Project : Project_Id := No_Project;
3462          --  The project of the first main
3463
3464          Proj              : Project_Id := No_Project;
3465          --  The project of the current main
3466
3467          Data              : Project_Data;
3468
3469          Real_Path         : String_Access;
3470
3471       begin
3472          Mains.Reset;
3473
3474          --  Check each main
3475
3476          loop
3477             declare
3478                Main      : constant String := Mains.Next_Main;
3479                --  The name specified on the command line may include
3480                --  directory information.
3481
3482                File_Name : constant String := Base_Name (Main);
3483                --  The simple file name of the current main main
3484
3485             begin
3486                exit when Main = "";
3487
3488                --  Get the project of the current main
3489
3490                Proj := Prj.Env.Project_Of
3491                          (File_Name, Main_Project, Project_Tree);
3492
3493                --  Fail if the current main is not a source of a
3494                --  project.
3495
3496                if Proj = No_Project then
3497                   Make_Failed
3498                     ("""" & Main &
3499                      """ is not a source of any project");
3500
3501                else
3502                   --  If there is directory information, check that
3503                   --  the source exists and, if it does, that the path
3504                   --  is the actual path of a source of a project.
3505
3506                   if Main /= File_Name then
3507                      Data :=
3508                        Project_Tree.Projects.Table (Main_Project);
3509
3510                      Real_Path :=
3511                        Locate_Regular_File
3512                          (Main &
3513                           Get_Name_String
3514                             (Data.Naming.Ada_Body_Suffix),
3515                           "");
3516                      if Real_Path = null then
3517                         Real_Path :=
3518                           Locate_Regular_File
3519                             (Main &
3520                              Get_Name_String
3521                                (Data.Naming.Ada_Spec_Suffix),
3522                              "");
3523                      end if;
3524
3525                      if Real_Path = null then
3526                         Real_Path :=
3527                           Locate_Regular_File (Main, "");
3528                      end if;
3529
3530                      --  Fail if the file cannot be found
3531
3532                      if Real_Path = null then
3533                         Make_Failed
3534                           ("file """ & Main & """ does not exist");
3535                      end if;
3536
3537                      declare
3538                         Project_Path : constant String :=
3539                                          Prj.Env.File_Name_Of_Library_Unit_Body
3540                                            (Name              => File_Name,
3541                                             Project           => Main_Project,
3542                                             In_Tree           => Project_Tree,
3543                                             Main_Project_Only => False,
3544                                             Full_Path         => True);
3545                         Normed_Path  : constant String :=
3546                                          Normalize_Pathname
3547                                            (Real_Path.all,
3548                                             Case_Sensitive => False);
3549                         Proj_Path    : constant String :=
3550                                          Normalize_Pathname
3551                                            (Project_Path,
3552                                             Case_Sensitive => False);
3553
3554                      begin
3555                         Free (Real_Path);
3556
3557                         --  Fail if it is not the correct path
3558
3559                         if Normed_Path /= Proj_Path then
3560                            if Verbose_Mode then
3561                               Write_Str (Normed_Path);
3562                               Write_Str (" /= ");
3563                               Write_Line (Proj_Path);
3564                            end if;
3565
3566                            Make_Failed
3567                              ("""" & Main &
3568                               """ is not a source of any project");
3569                         end if;
3570                      end;
3571                   end if;
3572
3573                   if not Unique_Compile then
3574
3575                      --  Record the project, if it is the first main
3576
3577                      if Real_Main_Project = No_Project then
3578                         Real_Main_Project := Proj;
3579
3580                      elsif Proj /= Real_Main_Project then
3581
3582                         --  Fail, as the current main is not a source
3583                         --  of the same project as the first main.
3584
3585                         Make_Failed
3586                           ("""" & Main &
3587                            """ is not a source of project " &
3588                            Get_Name_String
3589                              (Project_Tree.Projects.Table
3590                                 (Real_Main_Project).Name));
3591                      end if;
3592                   end if;
3593                end if;
3594
3595                --  If -u and -U are not used, we may have mains that
3596                --  are sources of a project that is not the one
3597                --  specified with switch -P.
3598
3599                if not Unique_Compile then
3600                   Main_Project := Real_Main_Project;
3601                end if;
3602             end;
3603          end loop;
3604       end Check_Mains;
3605
3606       --------------------------------
3607       -- Create_Binder_Mapping_File --
3608       --------------------------------
3609
3610       procedure Create_Binder_Mapping_File
3611         (Args : in out Argument_List; Last_Arg : in out Natural)
3612       is
3613          Mapping_FD : File_Descriptor := Invalid_FD;
3614          --  A File Descriptor for an eventual mapping file
3615
3616          ALI_Unit     : Name_Id := No_Name;
3617          --  The unit name of an ALI file
3618
3619          ALI_Name     : Name_Id := No_Name;
3620          --  The file name of the ALI file
3621
3622          ALI_Project  : Project_Id := No_Project;
3623          --  The project of the ALI file
3624
3625          Bytes        : Integer;
3626          OK           : Boolean := True;
3627
3628          Status       : Boolean;
3629          --  For call to Close
3630
3631       begin
3632          Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
3633
3634          if Mapping_FD /= Invalid_FD then
3635
3636             --  Traverse all units
3637
3638             for J in Unit_Table.First ..
3639                      Unit_Table.Last (Project_Tree.Units)
3640             loop
3641                declare
3642                   Unit : constant Unit_Data :=
3643                            Project_Tree.Units.Table (J);
3644                begin
3645                   if Unit.Name /= No_Name then
3646
3647                      --  If there is a body, put it in the mapping
3648
3649                      if Unit.File_Names (Body_Part).Name /= No_Name
3650                        and then Unit.File_Names (Body_Part).Project
3651                        /= No_Project
3652                      then
3653                         Get_Name_String (Unit.Name);
3654                         Name_Buffer
3655                           (Name_Len + 1 .. Name_Len + 2) := "%b";
3656                         Name_Len := Name_Len + 2;
3657                         ALI_Unit := Name_Find;
3658                         ALI_Name :=
3659                           Lib_File_Name
3660                             (Unit.File_Names (Body_Part).Name);
3661                         ALI_Project :=
3662                           Unit.File_Names (Body_Part).Project;
3663
3664                         --  Otherwise, if there is a spec, put it
3665                         --  in the mapping.
3666
3667                      elsif Unit.File_Names (Specification).Name
3668                        /= No_Name
3669                        and then Unit.File_Names
3670                          (Specification).Project
3671                          /= No_Project
3672                      then
3673                         Get_Name_String (Unit.Name);
3674                         Name_Buffer
3675                           (Name_Len + 1 .. Name_Len + 2) := "%s";
3676                         Name_Len := Name_Len + 2;
3677                         ALI_Unit := Name_Find;
3678                         ALI_Name := Lib_File_Name
3679                           (Unit.File_Names (Specification).Name);
3680                         ALI_Project :=
3681                           Unit.File_Names (Specification).Project;
3682
3683                      else
3684                         ALI_Name := No_Name;
3685                      end if;
3686
3687                      --  If we have something to put in the mapping
3688                      --  then we do it now. However, if the project
3689                      --  is extended, we don't put anything in the
3690                      --  mapping file, because we do not know where
3691                      --  the ALI file is: it might be in the ext-
3692                      --  ended project obj dir as well as in the
3693                      --  extending project obj dir.
3694
3695                      if ALI_Name /= No_Name
3696                        and then
3697                          Project_Tree.Projects.Table
3698                            (ALI_Project).Extended_By = No_Project
3699                        and then
3700                          Project_Tree.Projects.Table
3701                            (ALI_Project).Extends = No_Project
3702                      then
3703                         --  First line is the unit name
3704
3705                         Get_Name_String (ALI_Unit);
3706                         Name_Len := Name_Len + 1;
3707                         Name_Buffer (Name_Len) := ASCII.LF;
3708                         Bytes :=
3709                           Write
3710                             (Mapping_FD,
3711                              Name_Buffer (1)'Address,
3712                              Name_Len);
3713                         OK := Bytes = Name_Len;
3714
3715                         exit when not OK;
3716
3717                         --  Second line it the ALI file name
3718
3719                         Get_Name_String (ALI_Name);
3720                         Name_Len := Name_Len + 1;
3721                         Name_Buffer (Name_Len) := ASCII.LF;
3722                         Bytes :=
3723                           Write
3724                             (Mapping_FD,
3725                              Name_Buffer (1)'Address,
3726                              Name_Len);
3727                         OK := Bytes = Name_Len;
3728
3729                         exit when not OK;
3730
3731                         --  Third line it the ALI path name,
3732                         --  concatenation of the project
3733                         --  directory with the ALI file name.
3734
3735                         declare
3736                            ALI : constant String :=
3737                                    Get_Name_String (ALI_Name);
3738                         begin
3739                            Get_Name_String
3740                              (Project_Tree.Projects.Table
3741                                 (ALI_Project).Object_Directory);
3742
3743                            if Name_Buffer (Name_Len) /=
3744                              Directory_Separator
3745                            then
3746                               Name_Len := Name_Len + 1;
3747                               Name_Buffer (Name_Len) :=
3748                                 Directory_Separator;
3749                            end if;
3750
3751                            Name_Buffer
3752                              (Name_Len + 1 ..
3753                                 Name_Len + ALI'Length) := ALI;
3754                            Name_Len :=
3755                              Name_Len + ALI'Length + 1;
3756                            Name_Buffer (Name_Len) := ASCII.LF;
3757                            Bytes :=
3758                              Write
3759                                (Mapping_FD,
3760                                 Name_Buffer (1)'Address,
3761                                 Name_Len);
3762                            OK := Bytes = Name_Len;
3763                         end;
3764
3765                         --  If OK is False, it means we were unable
3766                         --  to write a line. No point in continuing
3767                         --  with the other units.
3768
3769                         exit when not OK;
3770                      end if;
3771                   end if;
3772                end;
3773             end loop;
3774
3775             Close (Mapping_FD, Status);
3776
3777             OK := OK and Status;
3778
3779             --  If the creation of the mapping file was successful,
3780             --  we add the switch to the arguments of gnatbind.
3781
3782             if OK then
3783                Last_Arg := Last_Arg + 1;
3784                Args (Last_Arg) :=
3785                  new String'("-F=" & Get_Name_String (Mapping_Path));
3786             end if;
3787          end if;
3788       end Create_Binder_Mapping_File;
3789
3790    --  Start of processing for Gnatmake
3791
3792    --  This body is very long, should be broken down ???
3793
3794    begin
3795       Gnatmake_Called := True;
3796
3797       Install_Int_Handler (Sigint_Intercepted'Access);
3798
3799       Do_Compile_Step := True;
3800       Do_Bind_Step    := True;
3801       Do_Link_Step    := True;
3802
3803       Obsoleted.Reset;
3804
3805       Make.Initialize;
3806
3807       Bind_Shared := No_Shared_Switch'Access;
3808       Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
3809
3810       Failed_Links.Set_Last (0);
3811       Successful_Links.Set_Last (0);
3812
3813       if Hostparm.Java_VM then
3814          Gcc := new String'("jgnat");
3815          Gnatbind := new String'("jgnatbind");
3816          Gnatlink := new String'("jgnatlink");
3817
3818          --  Do not check for an object file (".o") when compiling to
3819          --  Java bytecode since ".class" files are generated instead.
3820
3821          Check_Object_Consistency := False;
3822       end if;
3823
3824       --  Special case when switch -B was specified
3825
3826       if Build_Bind_And_Link_Full_Project then
3827
3828          --  When switch -B is specified, there must be a project file
3829
3830          if Main_Project = No_Project then
3831             Make_Failed ("-B cannot be used without a project file");
3832
3833          --  No main program may be specified on the command line
3834
3835          elsif Osint.Number_Of_Files /= 0 then
3836             Make_Failed ("-B cannot be used with a main specified on " &
3837                          "the command line");
3838
3839          --  And the project file cannot be a library project file
3840
3841          elsif Project_Tree.Projects.Table (Main_Project).Library then
3842             Make_Failed ("-B cannot be used for a library project file");
3843
3844          else
3845             Insert_Project_Sources
3846               (The_Project  => Main_Project,
3847                All_Projects => Unique_Compile_All_Projects,
3848                Into_Q       => False);
3849
3850             --  If there are no sources to compile, we fail
3851
3852             if Osint.Number_Of_Files = 0 then
3853                Make_Failed ("no sources to compile");
3854             end if;
3855
3856             --  Specify -n for gnatbind and add the ALI files of all the
3857             --  sources, except the one which is a fake main subprogram:
3858             --  this is the one for the binder generated file and it will be
3859             --  transmitted to gnatlink. These sources are those that are
3860             --  in the queue.
3861
3862             Add_Switch ("-n", Binder, And_Save => True);
3863
3864             for J in Q.First .. Q.Last - 1 loop
3865                Add_Switch
3866                  (Get_Name_String
3867                     (Lib_File_Name (Q.Table (J).File)),
3868                   Binder, And_Save => True);
3869             end loop;
3870          end if;
3871
3872       elsif Main_Index /= 0 and then Osint.Number_Of_Files > 1 then
3873          Make_Failed ("cannot specify several mains with a multi-unit index");
3874
3875       elsif Main_Project /= No_Project then
3876
3877          --  If the main project file is a library project file, main(s)
3878          --  cannot be specified on the command line.
3879
3880          if Osint.Number_Of_Files /= 0 then
3881             if Project_Tree.Projects.Table (Main_Project).Library
3882               and then not Unique_Compile
3883               and then ((not Make_Steps) or else Bind_Only or else Link_Only)
3884             then
3885                Make_Failed ("cannot specify a main program " &
3886                             "on the command line for a library project file");
3887
3888             else
3889                --  Check that each main on the command line is a source of a
3890                --  project file and, if there are several mains, each of them
3891                --  is a source of the same project file.
3892
3893                Check_Mains;
3894             end if;
3895
3896          --  If no mains have been specified on the command line,
3897          --  and we are using a project file, we either find the main(s)
3898          --  in the attribute Main of the main project, or we put all
3899          --  the sources of the project file as mains.
3900
3901          else
3902             if Main_Index /= 0 then
3903                Make_Failed ("cannot specify a multi-unit index but no main " &
3904                             "on the command line");
3905             end if;
3906
3907             declare
3908                Value : String_List_Id :=
3909                  Project_Tree.Projects.Table (Main_Project).Mains;
3910
3911             begin
3912                --  The attribute Main is an empty list or not specified,
3913                --  or else gnatmake was invoked with the switch "-u".
3914
3915                if Value = Prj.Nil_String or else Unique_Compile then
3916
3917                   if (not Make_Steps) or else Compile_Only
3918                     or else not Project_Tree.Projects.Table
3919                                   (Main_Project).Library
3920                   then
3921                      --  First make sure that the binder and the linker
3922                      --  will not be invoked.
3923
3924                      Do_Bind_Step := False;
3925                      Do_Link_Step := False;
3926
3927                      --  Put all the sources in the queue
3928
3929                      Insert_Project_Sources
3930                        (The_Project  => Main_Project,
3931                         All_Projects => Unique_Compile_All_Projects,
3932                         Into_Q       => False);
3933
3934                      --  If no sources to compile, then there is nothing to do
3935
3936                      if Osint.Number_Of_Files = 0 then
3937                         if not Debug.Debug_Flag_N then
3938                            Delete_Mapping_Files;
3939                            Prj.Env.Delete_All_Path_Files (Project_Tree);
3940                         end if;
3941
3942                         if not Quiet_Output then
3943                            Osint.Write_Program_Name;
3944                            Write_Line (": no sources to compile");
3945                         end if;
3946
3947                         Exit_Program (E_Success);
3948                      end if;
3949                   end if;
3950
3951                else
3952                   --  The attribute Main is not an empty list.
3953                   --  Put all the main subprograms in the list as if there
3954                   --  were specified on the command line. However, if attribute
3955                   --  Languages includes a language other than Ada, only
3956                   --  include the Ada mains; if there is no Ada main, compile
3957                   --  all the sources of the project.
3958
3959                   declare
3960                      Data : constant Project_Data :=
3961                            Project_Tree.Projects.Table (Main_Project);
3962
3963                      Languages : constant Variable_Value :=
3964                                    Prj.Util.Value_Of
3965                                     (Name_Languages,
3966                                      Data.Decl.Attributes,
3967                                      Project_Tree);
3968
3969                      Current : String_List_Id;
3970                      Element : String_Element;
3971
3972                      Foreign_Language  : Boolean := False;
3973                      At_Least_One_Main : Boolean := False;
3974
3975                   begin
3976                      --  First, determine if there is a foreign language in
3977                      --  attribute Languages.
3978
3979                      if not Languages.Default then
3980                         Current := Languages.Values;
3981
3982                         Look_For_Foreign :
3983                         while Current /= Nil_String loop
3984                            Element := Project_Tree.String_Elements.
3985                                         Table (Current);
3986                            Get_Name_String (Element.Value);
3987                            To_Lower (Name_Buffer (1 .. Name_Len));
3988
3989                            if Name_Buffer (1 .. Name_Len) /= "ada" then
3990                               Foreign_Language := True;
3991                               exit Look_For_Foreign;
3992                            end if;
3993
3994                            Current := Element.Next;
3995                         end loop Look_For_Foreign;
3996                      end if;
3997
3998                      --  Then, find all mains, or if there is a foreign
3999                      --  language, all the Ada mains.
4000
4001                      while Value /= Prj.Nil_String loop
4002                         Get_Name_String
4003                           (Project_Tree.String_Elements.Table
4004                              (Value).Value);
4005
4006                         --  To know if a main is an Ada main, get its project.
4007                         --  It should be the project specified on the command
4008                         --  line.
4009
4010                         if (not Foreign_Language) or else
4011                             Prj.Env.Project_Of
4012                               (Name_Buffer (1 .. Name_Len),
4013                                Main_Project,
4014                                Project_Tree) =
4015                              Main_Project
4016                         then
4017                            At_Least_One_Main := True;
4018                            Osint.Add_File
4019                              (Get_Name_String
4020                                 (Project_Tree.String_Elements.Table
4021                                    (Value).Value),
4022                               Index =>
4023                                 Project_Tree.String_Elements.Table
4024                                   (Value).Index);
4025                         end if;
4026
4027                         Value := Project_Tree.String_Elements.Table
4028                                    (Value).Next;
4029                      end loop;
4030
4031                      --  If we did not get any main, it means that all mains
4032                      --  in attribute Mains are in a foreign language and -B
4033                      --  was not specified to gnatmake; so, we fail.
4034
4035                      if not At_Least_One_Main then
4036                         Make_Failed
4037                           ("no Ada mains; use -B to build foreign main");
4038                      end if;
4039                   end;
4040
4041                end if;
4042             end;
4043          end if;
4044       end if;
4045
4046       if Verbose_Mode then
4047          Write_Eol;
4048          Write_Str ("GNATMAKE ");
4049          Write_Str (Gnatvsn.Gnat_Version_String);
4050          Write_Eol;
4051          Write_Str ("Copyright 1995-2004 Free Software Foundation, Inc.");
4052          Write_Eol;
4053       end if;
4054
4055       if Main_Project /= No_Project
4056         and then Project_Tree.Projects.Table
4057                    (Main_Project).Externally_Built
4058       then
4059          Make_Failed
4060            ("nothing to do for a main project that is externally built");
4061       end if;
4062
4063       if Osint.Number_Of_Files = 0 then
4064          if Main_Project /= No_Project
4065            and then Project_Tree.Projects.Table (Main_Project).Library
4066          then
4067             if Do_Bind_Step
4068               and then not Project_Tree.Projects.Table
4069                              (Main_Project).Standalone_Library
4070             then
4071                Make_Failed ("only stand-alone libraries may be bound");
4072             end if;
4073
4074             --  Add the default search directories to be able to find libgnat
4075
4076             Osint.Add_Default_Search_Dirs;
4077
4078             --  And bind and or link the library
4079
4080             MLib.Prj.Build_Library
4081               (For_Project   => Main_Project,
4082                In_Tree       => Project_Tree,
4083                Gnatbind      => Gnatbind.all,
4084                Gnatbind_Path => Gnatbind_Path,
4085                Gcc           => Gcc.all,
4086                Gcc_Path      => Gcc_Path,
4087                Bind          => Bind_Only,
4088                Link          => Link_Only);
4089             Exit_Program (E_Success);
4090
4091          else
4092             --  Output usage information if no files to compile
4093
4094             Usage;
4095             Exit_Program (E_Fatal);
4096          end if;
4097       end if;
4098
4099       --  If -M was specified, behave as if -n was specified
4100
4101       if List_Dependencies then
4102          Do_Not_Execute := True;
4103       end if;
4104
4105       --  Note that Osint.Next_Main_Source will always return the (possibly
4106       --  abbreviated file) without any directory information.
4107
4108       Main_Source_File := Next_Main_Source;
4109
4110       if Current_File_Index /= No_Index then
4111          Main_Index := Current_File_Index;
4112       end if;
4113
4114       Add_Switch ("-I-", Binder, And_Save => True);
4115       Add_Switch ("-I-", Compiler, And_Save => True);
4116
4117       if Main_Project = No_Project then
4118          if Look_In_Primary_Dir then
4119
4120             Add_Switch
4121               ("-I" &
4122                Normalize_Directory_Name
4123                (Get_Primary_Src_Search_Directory.all).all,
4124                Compiler, Append_Switch => False,
4125                And_Save => False);
4126
4127             Add_Switch ("-aO" & Normalized_CWD,
4128                         Binder,
4129                         Append_Switch => False,
4130                         And_Save => False);
4131          end if;
4132
4133       else
4134          --  If we use a project file, we have already checked that a main
4135          --  specified on the command line with directory information has the
4136          --  path name corresponding to a correct source in the project tree.
4137          --  So, we don't need the directory information to be taken into
4138          --  account by Find_File, and in fact it may lead to take the wrong
4139          --  sources for other compilation units, when there are extending
4140          --  projects.
4141
4142          Look_In_Primary_Dir := False;
4143       end if;
4144
4145       --  If the user wants a program without a main subprogram, add the
4146       --  appropriate switch to the binder.
4147
4148       if No_Main_Subprogram then
4149          Add_Switch ("-z", Binder, And_Save => True);
4150       end if;
4151
4152       if Main_Project /= No_Project then
4153
4154          if Project_Tree.Projects.Table
4155               (Main_Project).Object_Directory /= No_Name
4156          then
4157             --  Change current directory to object directory of main project
4158
4159             begin
4160                Project_Object_Directory := No_Project;
4161                Change_To_Object_Directory (Main_Project);
4162
4163             exception
4164                when Directory_Error =>
4165
4166                   --  This should never happen. But, if it does, display the
4167                   --  content of the parent directory of the obj dir.
4168
4169                   declare
4170                      Parent : constant Dir_Name_Str :=
4171                                 Dir_Name
4172                                   (Get_Name_String
4173                                      (Project_Tree.Projects.Table
4174                                         (Main_Project).Object_Directory));
4175
4176                      Dir  : Dir_Type;
4177                      Str  : String (1 .. 200);
4178                      Last : Natural;
4179
4180                   begin
4181                      Write_Str ("Contents of directory """);
4182                      Write_Str (Parent);
4183                      Write_Line (""":");
4184
4185                      Open (Dir, Parent);
4186
4187                      loop
4188                         Read (Dir, Str, Last);
4189                         exit when Last = 0;
4190                         Write_Str ("   ");
4191                         Write_Line (Str (1 .. Last));
4192                      end loop;
4193
4194                      Close (Dir);
4195
4196                   exception
4197                      when X : others =>
4198                         Write_Line ("(unexpected exception)");
4199                         Write_Line (Exception_Information (X));
4200
4201                         if Is_Open (Dir) then
4202                            Close (Dir);
4203                         end if;
4204                   end;
4205
4206                   Make_Failed
4207                     ("unable to change working directory to """,
4208                      Get_Name_String
4209                        (Project_Tree.Projects.Table
4210                           (Main_Project).Object_Directory),
4211                      """");
4212             end;
4213          end if;
4214
4215          --  Source file lookups should be cached for efficiency.
4216          --  Source files are not supposed to change.
4217
4218          Osint.Source_File_Data (Cache => True);
4219
4220          --  Find the file name of the (first) main unit
4221
4222          declare
4223             Main_Source_File_Name : constant String :=
4224                                       Get_Name_String (Main_Source_File);
4225             Main_Unit_File_Name   : constant String :=
4226                                       Prj.Env.File_Name_Of_Library_Unit_Body
4227                                         (Name    => Main_Source_File_Name,
4228                                          Project => Main_Project,
4229                                          In_Tree => Project_Tree,
4230                                          Main_Project_Only =>
4231                                            not Unique_Compile);
4232
4233             The_Packages : constant Package_Id :=
4234               Project_Tree.Projects.Table (Main_Project).Decl.Packages;
4235
4236             Builder_Package : constant Prj.Package_Id :=
4237                          Prj.Util.Value_Of
4238                            (Name        => Name_Builder,
4239                             In_Packages => The_Packages,
4240                             In_Tree     => Project_Tree);
4241
4242             Binder_Package : constant Prj.Package_Id :=
4243                          Prj.Util.Value_Of
4244                            (Name        => Name_Binder,
4245                             In_Packages => The_Packages,
4246                             In_Tree     => Project_Tree);
4247
4248             Linker_Package : constant Prj.Package_Id :=
4249                          Prj.Util.Value_Of
4250                            (Name        => Name_Linker,
4251                             In_Packages => The_Packages,
4252                             In_Tree     => Project_Tree);
4253
4254          begin
4255             --  We fail if we cannot find the main source file
4256
4257             if Main_Unit_File_Name = "" then
4258                Make_Failed ('"' & Main_Source_File_Name,
4259                             """ is not a unit of project ",
4260                             Project_File_Name.all & ".");
4261             else
4262                --  Remove any directory information from the main
4263                --  source file name.
4264
4265                declare
4266                   Pos : Natural := Main_Unit_File_Name'Last;
4267
4268                begin
4269                   loop
4270                      exit when Pos < Main_Unit_File_Name'First or else
4271                        Main_Unit_File_Name (Pos) = Directory_Separator;
4272                      Pos := Pos - 1;
4273                   end loop;
4274
4275                   Name_Len := Main_Unit_File_Name'Last - Pos;
4276
4277                   Name_Buffer (1 .. Name_Len) :=
4278                     Main_Unit_File_Name
4279                     (Pos + 1 .. Main_Unit_File_Name'Last);
4280
4281                   Main_Source_File := Name_Find;
4282
4283                   --  We only output the main source file if there is only one
4284
4285                   if Verbose_Mode and then Osint.Number_Of_Files = 1 then
4286                      Write_Str ("Main source file: """);
4287                      Write_Str (Main_Unit_File_Name
4288                                 (Pos + 1 .. Main_Unit_File_Name'Last));
4289                      Write_Line (""".");
4290                   end if;
4291                end;
4292             end if;
4293
4294             --  If there is a package Builder in the main project file, add
4295             --  the switches from it.
4296
4297             if Builder_Package /= No_Package then
4298
4299                --  If there is only one main, we attempt to get the gnatmake
4300                --  switches for this main (if any). If there are no specific
4301                --  switch for this particular main, get the general gnatmake
4302                --  switches (if any).
4303
4304                if Osint.Number_Of_Files = 1 then
4305                   if Verbose_Mode then
4306                      Write_Str ("Adding gnatmake switches for """);
4307                      Write_Str (Main_Unit_File_Name);
4308                      Write_Line (""".");
4309                   end if;
4310
4311                   Add_Switches
4312                     (File_Name   => Main_Unit_File_Name,
4313                      Index       => Main_Index,
4314                      The_Package => Builder_Package,
4315                      Program     => None);
4316
4317                else
4318                   --  If there are several mains, we always get the general
4319                   --  gnatmake switches (if any).
4320
4321                   --  Warn the user, if necessary, so that he is not surprized
4322                   --  that specific switches are not taken into account.
4323
4324                   declare
4325                      Defaults : constant Variable_Value :=
4326                        Prj.Util.Value_Of
4327                          (Name                    => Name_Ada,
4328                           Index                   => 0,
4329                           Attribute_Or_Array_Name => Name_Default_Switches,
4330                           In_Package              => Builder_Package,
4331                           In_Tree                 => Project_Tree);
4332
4333                      Switches : constant Array_Element_Id :=
4334                           Prj.Util.Value_Of
4335                              (Name      => Name_Switches,
4336                               In_Arrays =>
4337                                 Project_Tree.Packages.Table
4338                                   (Builder_Package).Decl.Arrays,
4339                               In_Tree   => Project_Tree);
4340
4341                   begin
4342                      if Defaults /= Nil_Variable_Value then
4343                         if (not Quiet_Output)
4344                           and then Switches /= No_Array_Element
4345                         then
4346                            Write_Line
4347                              ("Warning: using Builder'Default_Switches" &
4348                               "(""Ada""), as there are several mains");
4349                         end if;
4350
4351                         --  As there is never a source with name " ", we are
4352                         --  guaranteed to always get the general switches.
4353
4354                         Add_Switches
4355                           (File_Name   => " ",
4356                            Index       => 0,
4357                            The_Package => Builder_Package,
4358                            Program     => None);
4359
4360                      elsif (not Quiet_Output)
4361                        and then Switches /= No_Array_Element
4362                      then
4363                         Write_Line
4364                           ("Warning: using no switches from package Builder," &
4365                            " as there are several mains");
4366                      end if;
4367                   end;
4368                end if;
4369             end if;
4370
4371             Osint.Add_Default_Search_Dirs;
4372
4373             --  Record the current last switch index for table Binder_Switches
4374             --  and Linker_Switches, so that these tables may be reset before
4375             --  for each main, before adding swiches from the project file
4376             --  and from the command line.
4377
4378             Last_Binder_Switch := Binder_Switches.Last;
4379             Last_Linker_Switch := Linker_Switches.Last;
4380
4381             Check_Steps;
4382
4383             --  Add binder switches from the project file for the first main
4384
4385             if Do_Bind_Step and Binder_Package /= No_Package then
4386                if Verbose_Mode then
4387                   Write_Str ("Adding binder switches for """);
4388                   Write_Str (Main_Unit_File_Name);
4389                   Write_Line (""".");
4390                end if;
4391
4392                Add_Switches
4393                  (File_Name   => Main_Unit_File_Name,
4394                   Index       => Main_Index,
4395                   The_Package => Binder_Package,
4396                   Program     => Binder);
4397             end if;
4398
4399             --  Add linker switches from the project file for the first main
4400
4401             if Do_Link_Step and Linker_Package /= No_Package then
4402                if Verbose_Mode then
4403                   Write_Str ("Adding linker switches for""");
4404                   Write_Str (Main_Unit_File_Name);
4405                   Write_Line (""".");
4406                end if;
4407
4408                Add_Switches
4409                  (File_Name   => Main_Unit_File_Name,
4410                   Index       => Main_Index,
4411                   The_Package => Linker_Package,
4412                   Program     => Linker);
4413             end if;
4414          end;
4415       end if;
4416
4417       --  Get the target parameters, which are only needed for a couple of
4418       --  cases in gnatmake. Protect against an exception, such as the case
4419       --  of system.ads missing from the library, and fail gracefully.
4420
4421       begin
4422          Targparm.Get_Target_Parameters;
4423
4424       exception
4425          when Unrecoverable_Error =>
4426             Make_Failed ("*** make failed.");
4427       end;
4428
4429       Display_Commands (not Quiet_Output);
4430
4431       Check_Steps;
4432
4433       if Main_Project /= No_Project then
4434
4435          --  For all library project, if the library file does not exist
4436          --  put all the project sources in the queue, and flag the project
4437          --  so that the library is generated.
4438
4439          if not Unique_Compile
4440            and then MLib.Tgt.Support_For_Libraries /= MLib.Tgt.None
4441          then
4442             for Proj in Project_Table.First ..
4443                         Project_Table.Last (Project_Tree.Projects)
4444             loop
4445                if Project_Tree.Projects.Table (Proj).Library then
4446                   Project_Tree.Projects.Table
4447                     (Proj).Need_To_Build_Lib :=
4448                       (not MLib.Tgt.Library_Exists_For (Proj, Project_Tree))
4449                     and then (not Project_Tree.Projects.Table
4450                                 (Proj).Externally_Built);
4451
4452                   if Project_Tree.Projects.Table
4453                        (Proj).Need_To_Build_Lib
4454                   then
4455                      --  If there is no object directory, then it will be
4456                      --  impossible to build the library. So fail immediately.
4457
4458                      if Project_Tree.Projects.Table
4459                        (Proj).Object_Directory = No_Name
4460                      then
4461                         Make_Failed
4462                           ("no object files to build library for project """,
4463                            Get_Name_String
4464                              (Project_Tree.Projects.Table (Proj).Name),
4465                            """");
4466                         Project_Tree.Projects.Table
4467                           (Proj).Need_To_Build_Lib := False;
4468
4469                      else
4470                         if Verbose_Mode then
4471                            Write_Str
4472                              ("Library file does not exist for project """);
4473                            Write_Str
4474                              (Get_Name_String
4475                                 (Project_Tree.Projects.Table
4476                                    (Proj).Name));
4477                            Write_Line ("""");
4478                         end if;
4479
4480                         Insert_Project_Sources
4481                           (The_Project  => Proj,
4482                            All_Projects => False,
4483                            Into_Q       => True);
4484                      end if;
4485                   end if;
4486                end if;
4487             end loop;
4488          end if;
4489
4490          --  If a relative path output file has been specified, we add
4491          --  the exec directory.
4492
4493          for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
4494             if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
4495                declare
4496                   Exec_File_Name : constant String :=
4497                     Saved_Linker_Switches.Table (J + 1).all;
4498
4499                begin
4500                   if not Is_Absolute_Path (Exec_File_Name) then
4501                      for Index in Exec_File_Name'Range loop
4502                         if Exec_File_Name (Index) = Directory_Separator then
4503                            Make_Failed ("relative executable (""",
4504                                         Exec_File_Name,
4505                                         """) with directory part not " &
4506                                         "allowed when using project files");
4507                         end if;
4508                      end loop;
4509
4510                      Get_Name_String
4511                        (Project_Tree.Projects.Table
4512                           (Main_Project).Exec_Directory);
4513
4514                      if Name_Buffer (Name_Len) /= Directory_Separator then
4515                         Name_Len := Name_Len + 1;
4516                         Name_Buffer (Name_Len) := Directory_Separator;
4517                      end if;
4518
4519                      Name_Buffer (Name_Len + 1 ..
4520                                     Name_Len + Exec_File_Name'Length) :=
4521                        Exec_File_Name;
4522                      Name_Len := Name_Len + Exec_File_Name'Length;
4523                      Saved_Linker_Switches.Table (J + 1) :=
4524                        new String'(Name_Buffer (1 .. Name_Len));
4525                   end if;
4526                end;
4527
4528                exit;
4529             end if;
4530          end loop;
4531
4532          --  If we are using a project file, for relative paths we add the
4533          --  current working directory for any relative path on the command
4534          --  line and the project directory, for any relative path in the
4535          --  project file.
4536
4537          declare
4538             Dir_Path : constant String_Access :=
4539               new String'(Get_Name_String
4540                             (Project_Tree.Projects.Table
4541                                (Main_Project).Directory));
4542          begin
4543             for J in 1 .. Binder_Switches.Last loop
4544                Test_If_Relative_Path
4545                  (Binder_Switches.Table (J),
4546                   Parent => Dir_Path, Including_L_Switch => False);
4547             end loop;
4548
4549             for J in 1 .. Saved_Binder_Switches.Last loop
4550                Test_If_Relative_Path
4551                  (Saved_Binder_Switches.Table (J),
4552                   Parent => Current_Work_Dir, Including_L_Switch => False);
4553             end loop;
4554
4555             for J in 1 .. Linker_Switches.Last loop
4556                Test_If_Relative_Path
4557                  (Linker_Switches.Table (J), Parent => Dir_Path);
4558             end loop;
4559
4560             for J in 1 .. Saved_Linker_Switches.Last loop
4561                Test_If_Relative_Path
4562                  (Saved_Linker_Switches.Table (J), Parent => Current_Work_Dir);
4563             end loop;
4564
4565             for J in 1 .. Gcc_Switches.Last loop
4566                Test_If_Relative_Path
4567                  (Gcc_Switches.Table (J), Parent => Dir_Path);
4568             end loop;
4569
4570             for J in 1 .. Saved_Gcc_Switches.Last loop
4571                Test_If_Relative_Path
4572                  (Saved_Gcc_Switches.Table (J), Parent => Current_Work_Dir);
4573             end loop;
4574          end;
4575       end if;
4576
4577       --  We now put in the Binder_Switches and Linker_Switches tables, the
4578       --  binder and linker switches of the command line that have been put in
4579       --  the Saved_ tables. If a project file was used, then the command line
4580       --  switches will follow the project file switches.
4581
4582       for J in 1 .. Saved_Binder_Switches.Last loop
4583          Add_Switch
4584            (Saved_Binder_Switches.Table (J),
4585             Binder,
4586             And_Save => False);
4587       end loop;
4588
4589       for J in 1 .. Saved_Linker_Switches.Last loop
4590          Add_Switch
4591            (Saved_Linker_Switches.Table (J),
4592             Linker,
4593             And_Save => False);
4594       end loop;
4595
4596       --  If no project file is used, we just put the gcc switches
4597       --  from the command line in the Gcc_Switches table.
4598
4599       if Main_Project = No_Project then
4600          for J in 1 .. Saved_Gcc_Switches.Last loop
4601             Add_Switch
4602               (Saved_Gcc_Switches.Table (J),
4603                Compiler,
4604               And_Save => False);
4605          end loop;
4606
4607       else
4608          --  And we put the command line gcc switches in the variable
4609          --  The_Saved_Gcc_Switches. They are going to be used later
4610          --  in procedure Compile_Sources.
4611
4612          The_Saved_Gcc_Switches :=
4613            new Argument_List (1 .. Saved_Gcc_Switches.Last + 1);
4614
4615          for J in 1 .. Saved_Gcc_Switches.Last loop
4616             The_Saved_Gcc_Switches (J) := Saved_Gcc_Switches.Table (J);
4617          end loop;
4618
4619          --  We never use gnat.adc when a project file is used
4620
4621          The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) :=
4622            No_gnat_adc;
4623
4624       end if;
4625
4626       --  If there was a --GCC, --GNATBIND or --GNATLINK switch on
4627       --  the command line, then we have to use it, even if there was
4628       --  another switch in the project file.
4629
4630       if Saved_Gcc /= null then
4631          Gcc := Saved_Gcc;
4632       end if;
4633
4634       if Saved_Gnatbind /= null then
4635          Gnatbind := Saved_Gnatbind;
4636       end if;
4637
4638       if Saved_Gnatlink /= null then
4639          Gnatlink := Saved_Gnatlink;
4640       end if;
4641
4642       Gcc_Path       := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
4643       Gnatbind_Path  := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
4644       Gnatlink_Path  := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
4645
4646       --  If we have specified -j switch both from the project file
4647       --  and on the command line, the one from the command line takes
4648       --  precedence.
4649
4650       if Saved_Maximum_Processes = 0 then
4651          Saved_Maximum_Processes := Maximum_Processes;
4652       end if;
4653
4654       --  Allocate as many temporary mapping file names as the maximum
4655       --  number of compilation processed, for each possible project.
4656
4657       The_Mapping_File_Names :=
4658         new Temp_File_Names
4659           (No_Project .. Project_Table.Last (Project_Tree.Projects),
4660            1 .. Saved_Maximum_Processes);
4661       Last_Mapping_File_Names :=
4662         new Indices'
4663           (No_Project .. Project_Table.Last (Project_Tree.Projects)
4664             => 0);
4665
4666       The_Free_Mapping_File_Indices :=
4667         new Free_File_Indices
4668           (No_Project .. Project_Table.Last (Project_Tree.Projects),
4669            1 .. Saved_Maximum_Processes);
4670       Last_Free_Indices :=
4671         new Indices'(No_Project .. Project_Table.Last
4672                                      (Project_Tree.Projects) => 0);
4673
4674       Bad_Compilation.Init;
4675
4676       Current_Main_Index := Main_Index;
4677
4678       --  Here is where the make process is started
4679
4680       --  We do the same process for each main
4681
4682       Multiple_Main_Loop : for N_File in 1 .. Osint.Number_Of_Files loop
4683
4684          --  First, find the executable name and path
4685
4686          Executable          := No_File;
4687          Executable_Obsolete := False;
4688          Non_Std_Executable  := False;
4689
4690          --  Look inside the linker switches to see if the name
4691          --  of the final executable program was specified.
4692
4693          for
4694            J in reverse Linker_Switches.First .. Linker_Switches.Last
4695          loop
4696             if Linker_Switches.Table (J).all = Output_Flag.all then
4697                pragma Assert (J < Linker_Switches.Last);
4698
4699                --  We cannot specify a single executable for several
4700                --  main subprograms!
4701
4702                if Osint.Number_Of_Files > 1 then
4703                   Fail
4704                     ("cannot specify a single executable " &
4705                      "for several mains");
4706                end if;
4707
4708                Name_Len := Linker_Switches.Table (J + 1)'Length;
4709                Name_Buffer (1 .. Name_Len) :=
4710                  Linker_Switches.Table (J + 1).all;
4711                Executable := Name_Enter;
4712
4713                Verbose_Msg (Executable, "final executable");
4714             end if;
4715          end loop;
4716
4717          --  If the name of the final executable program was not
4718          --  specified then construct it from the main input file.
4719
4720          if Executable = No_File then
4721             if Main_Project = No_Project then
4722                Executable :=
4723                  Executable_Name (Strip_Suffix (Main_Source_File));
4724
4725             else
4726                --  If we are using a project file, we attempt to
4727                --  remove the body (or spec) termination of the main
4728                --  subprogram. We find it the the naming scheme of the
4729                --  project file. This will avoid to generate an
4730                --  executable "main.2" for a main subprogram
4731                --  "main.2.ada", when the body termination is ".2.ada".
4732
4733                Executable :=
4734                  Prj.Util.Executable_Of
4735                    (Main_Project, Project_Tree, Main_Source_File, Main_Index);
4736             end if;
4737          end if;
4738
4739          if Main_Project /= No_Project then
4740             declare
4741                Exec_File_Name : constant String :=
4742                  Get_Name_String (Executable);
4743
4744             begin
4745                if not Is_Absolute_Path (Exec_File_Name) then
4746                   for Index in Exec_File_Name'Range loop
4747                      if Exec_File_Name (Index) = Directory_Separator then
4748                         Make_Failed ("relative executable (""",
4749                                            Exec_File_Name,
4750                                            """) with directory part not " &
4751                                            "allowed when using project files");
4752                      end if;
4753                   end loop;
4754
4755                   Get_Name_String (Project_Tree.Projects.Table
4756                                            (Main_Project).Exec_Directory);
4757
4758                   if
4759                     Name_Buffer (Name_Len) /= Directory_Separator
4760                   then
4761                      Name_Len := Name_Len + 1;
4762                      Name_Buffer (Name_Len) := Directory_Separator;
4763                   end if;
4764
4765                   Name_Buffer (Name_Len + 1 ..
4766                                        Name_Len + Exec_File_Name'Length) :=
4767                     Exec_File_Name;
4768
4769                   Name_Len := Name_Len + Exec_File_Name'Length;
4770                   Executable := Name_Find;
4771                   Non_Std_Executable := True;
4772                end if;
4773             end;
4774          end if;
4775
4776          if Do_Compile_Step then
4777             Recursive_Compilation_Step : declare
4778                Args : Argument_List (1 .. Gcc_Switches.Last);
4779
4780                First_Compiled_File : Name_Id;
4781                Youngest_Obj_File   : Name_Id;
4782                Youngest_Obj_Stamp  : Time_Stamp_Type;
4783
4784                Executable_Stamp : Time_Stamp_Type;
4785                --  Executable is the final executable program
4786
4787                Library_Rebuilt : Boolean := False;
4788
4789             begin
4790                for J in 1 .. Gcc_Switches.Last loop
4791                   Args (J) := Gcc_Switches.Table (J);
4792                end loop;
4793
4794                --  Now we invoke Compile_Sources for the current main
4795
4796                Compile_Sources
4797                  (Main_Source           => Main_Source_File,
4798                   Args                  => Args,
4799                   First_Compiled_File   => First_Compiled_File,
4800                   Most_Recent_Obj_File  => Youngest_Obj_File,
4801                   Most_Recent_Obj_Stamp => Youngest_Obj_Stamp,
4802                   Main_Unit             => Is_Main_Unit,
4803                   Main_Index            => Current_Main_Index,
4804                   Compilation_Failures  => Compilation_Failures,
4805                   Check_Readonly_Files  => Check_Readonly_Files,
4806                   Do_Not_Execute        => Do_Not_Execute,
4807                   Force_Compilations    => Force_Compilations,
4808                   In_Place_Mode         => In_Place_Mode,
4809                   Keep_Going            => Keep_Going,
4810                   Initialize_ALI_Data   => True,
4811                   Max_Process           => Saved_Maximum_Processes);
4812
4813                if Verbose_Mode then
4814                   Write_Str ("End of compilation");
4815                   Write_Eol;
4816                end if;
4817
4818                --  Make sure the queue will be reinitialized for the next round
4819
4820                First_Q_Initialization := True;
4821
4822                Total_Compilation_Failures :=
4823                  Total_Compilation_Failures + Compilation_Failures;
4824
4825                if Total_Compilation_Failures /= 0 then
4826                   if Keep_Going then
4827                      goto Next_Main;
4828                   else
4829                      List_Bad_Compilations;
4830                      raise Compilation_Failed;
4831                   end if;
4832                end if;
4833
4834                --  Regenerate libraries, if any, and if object files
4835                --  have been regenerated.
4836
4837                if Main_Project /= No_Project
4838                  and then MLib.Tgt.Support_For_Libraries /= MLib.Tgt.None
4839                  and then (Do_Bind_Step or Unique_Compile_All_Projects
4840                            or not Compile_Only)
4841                  and then (Do_Link_Step or N_File = Osint.Number_Of_Files)
4842                then
4843                   Library_Projs.Init;
4844
4845                   declare
4846                      Proj2   : Project_Id;
4847                      Depth   : Natural;
4848                      Current : Natural;
4849
4850                   begin
4851                      --  Put in Library_Projs table all library project
4852                      --  file ids when the library need to be rebuilt.
4853
4854                      for Proj1 in Project_Table.First ..
4855                                   Project_Table.Last (Project_Tree.Projects)
4856                      loop
4857                         if Project_Tree.Projects.Table
4858                           (Proj1).Standalone_Library
4859                         then
4860                            There_Are_Stand_Alone_Libraries := True;
4861                         end if;
4862
4863                         if Project_Tree.Projects.Table (Proj1).Library
4864                           and then not Project_Tree.Projects.Table
4865                                          (Proj1).Need_To_Build_Lib
4866                           and then not Project_Tree.Projects.Table
4867                                          (Proj1).Externally_Built
4868                         then
4869                            MLib.Prj.Check_Library (Proj1, Project_Tree);
4870                         end if;
4871
4872                         if Project_Tree.Projects.Table
4873                              (Proj1).Need_To_Build_Lib
4874                         then
4875                            Library_Projs.Increment_Last;
4876                            Current := Library_Projs.Last;
4877                            Depth := Project_Tree.Projects.Table
4878                                       (Proj1).Depth;
4879
4880                            --  Put the projects in decreasing depth order,
4881                            --  so that if libA depends on libB, libB is first
4882                            --  in order.
4883
4884                            while Current > 1 loop
4885                               Proj2 := Library_Projs.Table (Current - 1);
4886                               exit when Project_Tree.Projects.Table
4887                                           (Proj2).Depth >= Depth;
4888                               Library_Projs.Table (Current) := Proj2;
4889                               Current := Current - 1;
4890                            end loop;
4891
4892                            Library_Projs.Table (Current) := Proj1;
4893                            Project_Tree.Projects.Table
4894                              (Proj1).Need_To_Build_Lib := False;
4895                         end if;
4896                      end loop;
4897                   end;
4898
4899                   --  Build the libraries, if any need to be built
4900
4901                   for J in 1 .. Library_Projs.Last loop
4902                      Library_Rebuilt := True;
4903                      MLib.Prj.Build_Library
4904                        (For_Project   => Library_Projs.Table (J),
4905                         In_Tree       => Project_Tree,
4906                         Gnatbind      => Gnatbind.all,
4907                         Gnatbind_Path => Gnatbind_Path,
4908                         Gcc           => Gcc.all,
4909                         Gcc_Path      => Gcc_Path);
4910                   end loop;
4911                end if;
4912
4913                if List_Dependencies then
4914                   if First_Compiled_File /= No_File then
4915                      Inform
4916                        (First_Compiled_File,
4917                         "must be recompiled. Can't generate dependence list.");
4918                   else
4919                      List_Depend;
4920                   end if;
4921
4922                elsif First_Compiled_File = No_File
4923                  and then not Do_Bind_Step
4924                  and then not Quiet_Output
4925                  and then not Library_Rebuilt
4926                  and then Osint.Number_Of_Files = 1
4927                then
4928                   Inform (Msg => "objects up to date.");
4929
4930                elsif Do_Not_Execute
4931                  and then First_Compiled_File /= No_File
4932                then
4933                   Write_Name (First_Compiled_File);
4934                   Write_Eol;
4935                end if;
4936
4937                --  Stop after compile step if any of:
4938
4939                --    1) -n (Do_Not_Execute) specified
4940
4941                --    2) -M (List_Dependencies) specified (also sets
4942                --       Do_Not_Execute above, so this is probably superfluous).
4943
4944                --    3) -c (Compile_Only) specified, but not -b (Bind_Only)
4945
4946                --    4) Made unit cannot be a main unit
4947
4948                if (Do_Not_Execute
4949                    or List_Dependencies
4950                    or not Do_Bind_Step
4951                    or not Is_Main_Unit)
4952                  and then not No_Main_Subprogram
4953                  and then not Build_Bind_And_Link_Full_Project
4954                then
4955                   if Osint.Number_Of_Files = 1 then
4956                      exit Multiple_Main_Loop;
4957
4958                   else
4959                      goto Next_Main;
4960                   end if;
4961                end if;
4962
4963                --  If the objects were up-to-date check if the executable file
4964                --  is also up-to-date. For now always bind and link on the JVM
4965                --  since there is currently no simple way to check the
4966                --  up-to-date status of objects
4967
4968                if not Hostparm.Java_VM
4969                  and then First_Compiled_File = No_File
4970                then
4971                   Executable_Stamp    := File_Stamp (Executable);
4972
4973                   if not Executable_Obsolete then
4974                      Executable_Obsolete :=
4975                        Youngest_Obj_Stamp > Executable_Stamp;
4976                   end if;
4977
4978                   if not Executable_Obsolete then
4979                      for Index in reverse 1 .. Dependencies.Last loop
4980                         if Is_In_Obsoleted
4981                              (Dependencies.Table (Index).Depends_On)
4982                         then
4983                            Enter_Into_Obsoleted
4984                              (Dependencies.Table (Index).This);
4985                         end if;
4986                      end loop;
4987
4988                      Executable_Obsolete := Is_In_Obsoleted (Main_Source_File);
4989                      Dependencies.Init;
4990                   end if;
4991
4992                   if not Executable_Obsolete then
4993
4994                      --  If no Ada object files obsolete the executable, check
4995                      --  for younger or missing linker files.
4996
4997                      Check_Linker_Options
4998                        (Executable_Stamp,
4999                         Youngest_Obj_File,
5000                         Youngest_Obj_Stamp);
5001
5002                      Executable_Obsolete := Youngest_Obj_File /= No_File;
5003                   end if;
5004
5005                   --  Return if the executable is up to date
5006                   --  and otherwise motivate the relink/rebind.
5007
5008                   if not Executable_Obsolete then
5009                      if not Quiet_Output then
5010                         Inform (Executable, "up to date.");
5011                      end if;
5012
5013                      if Osint.Number_Of_Files = 1 then
5014                         exit Multiple_Main_Loop;
5015
5016                      else
5017                         goto Next_Main;
5018                      end if;
5019                   end if;
5020
5021                   if Executable_Stamp (1) = ' ' then
5022                      Verbose_Msg (Executable, "missing.", Prefix => "  ");
5023
5024                   elsif Youngest_Obj_Stamp (1) = ' ' then
5025                      Verbose_Msg
5026                        (Youngest_Obj_File,
5027                         "missing.",
5028                         Prefix => "  ");
5029
5030                   elsif Youngest_Obj_Stamp > Executable_Stamp then
5031                      Verbose_Msg
5032                        (Youngest_Obj_File,
5033                         "(" & String (Youngest_Obj_Stamp) & ") newer than",
5034                         Executable,
5035                         "(" & String (Executable_Stamp) & ")");
5036
5037                   else
5038                      Verbose_Msg
5039                        (Executable, "needs to be rebuild.",
5040                         Prefix => "  ");
5041
5042                   end if;
5043                end if;
5044             end Recursive_Compilation_Step;
5045          end if;
5046
5047          --  For binding and linking, we need to be in the object directory of
5048          --  the main project.
5049
5050          if Main_Project /= No_Project then
5051             Change_To_Object_Directory (Main_Project);
5052          end if;
5053
5054          --  If we are here, it means that we need to rebuilt the current
5055          --  main. So we set Executable_Obsolete to True to make sure that
5056          --  the subsequent mains will be rebuilt.
5057
5058          Main_ALI_In_Place_Mode_Step : declare
5059             ALI_File : File_Name_Type;
5060             Src_File : File_Name_Type;
5061
5062          begin
5063             Src_File      := Strip_Directory (Main_Source_File);
5064             ALI_File      := Lib_File_Name (Src_File, Current_Main_Index);
5065             Main_ALI_File := Full_Lib_File_Name (ALI_File);
5066
5067             --  When In_Place_Mode, the library file can be located in the
5068             --  Main_Source_File directory which may not be present in the
5069             --  library path. In this case, use the corresponding library file
5070             --  name.
5071
5072             if Main_ALI_File = No_File and then In_Place_Mode then
5073                Get_Name_String (Get_Directory (Full_Source_Name (Src_File)));
5074                Get_Name_String_And_Append (ALI_File);
5075                Main_ALI_File := Name_Find;
5076                Main_ALI_File := Full_Lib_File_Name (Main_ALI_File);
5077             end if;
5078
5079             if Main_ALI_File = No_File then
5080                Make_Failed ("could not find the main ALI file");
5081             end if;
5082          end Main_ALI_In_Place_Mode_Step;
5083
5084          if Do_Bind_Step then
5085             Bind_Step : declare
5086                Args : Argument_List
5087                         (Binder_Switches.First .. Binder_Switches.Last + 2);
5088                --  The arguments for the invocation of gnatbind
5089
5090                Last_Arg : Natural := Binder_Switches.Last;
5091                --  Index of the last argument in Args
5092
5093                Shared_Libs : Boolean := False;
5094                --  Set to True when there are shared library project files or
5095                --  when gnatbind is invoked with -shared.
5096
5097             begin
5098                --  Check if there are shared libraries, so that gnatbind is
5099                --  called with -shared. Check also if gnatbind is called with
5100                --  -shared, so that gnatlink is called with -shared-libgcc
5101                --  for GCC version 3 and above, ensuring that the shared
5102                --  version of libgcc will be used.
5103
5104                if Main_Project /= No_Project
5105                  and then MLib.Tgt.Support_For_Libraries /= MLib.Tgt.None
5106                then
5107                   for Proj in Project_Table.First ..
5108                         Project_Table.Last (Project_Tree.Projects)
5109                   loop
5110                      if Project_Tree.Projects.Table (Proj).Library
5111                        and then Project_Tree.Projects.Table
5112                                   (Proj).Library_Kind /= Static
5113                      then
5114                         Shared_Libs := True;
5115                         Bind_Shared := Shared_Switch'Access;
5116                         exit;
5117                      end if;
5118                   end loop;
5119                end if;
5120
5121                --  Check now for switch -shared
5122
5123                if not Shared_Libs then
5124                   for J in Binder_Switches.First .. Last_Arg loop
5125                      if Binder_Switches.Table (J).all = "-shared" then
5126                         Shared_Libs := True;
5127                         exit;
5128                      end if;
5129                   end loop;
5130                end if;
5131
5132                --  If there are shared libraries, invoke gnatlink with
5133                --  -shared-libgcc if GCC version is 3 or more.
5134
5135                if Shared_Libs and then GCC_Version >= 3 then
5136                   Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
5137                end if;
5138
5139                --  Get all the binder switches
5140
5141                for J in Binder_Switches.First .. Last_Arg loop
5142                   Args (J) := Binder_Switches.Table (J);
5143                end loop;
5144
5145                if There_Are_Stand_Alone_Libraries then
5146                   Last_Arg := Last_Arg + 1;
5147                   Args (Last_Arg) := Force_Elab_Flags_String'Access;
5148                end if;
5149
5150                if Main_Project /= No_Project then
5151
5152                   --  Put all the source directories in ADA_INCLUDE_PATH,
5153                   --  and all the object directories in ADA_OBJECTS_PATH
5154
5155                   Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
5156
5157                   --  If switch -C was specified, create a binder mapping file
5158
5159                   if Create_Mapping_File then
5160                      Create_Binder_Mapping_File (Args, Last_Arg);
5161                   end if;
5162
5163                end if;
5164
5165                begin
5166                   Bind (Main_ALI_File,
5167                         Bind_Shared.all & Args (Args'First .. Last_Arg));
5168
5169                exception
5170                   when others =>
5171
5172                      --  If -dn was not specified, delete the temporary mapping
5173                      --  file, if one was created.
5174
5175                      if not Debug.Debug_Flag_N
5176                        and then Mapping_Path /= No_Name
5177                      then
5178                         Delete_File (Get_Name_String (Mapping_Path), Discard);
5179                      end if;
5180
5181                      --  And reraise the exception
5182
5183                      raise;
5184                end;
5185
5186                --  If -dn was not specified, delete the temporary mapping file,
5187                --  if one was created.
5188
5189                if not Debug.Debug_Flag_N and then Mapping_Path /= No_Name then
5190                   Delete_File (Get_Name_String (Mapping_Path), Discard);
5191                end if;
5192             end Bind_Step;
5193          end if;
5194
5195          if Do_Link_Step then
5196             Link_Step : declare
5197                There_Are_Libraries  : Boolean := False;
5198                Linker_Switches_Last : constant Integer := Linker_Switches.Last;
5199                Path_Option : constant String_Access :=
5200                                MLib.Linker_Library_Path_Option;
5201                Current : Natural;
5202                Proj2   : Project_Id;
5203                Depth   : Natural;
5204
5205             begin
5206                if not Run_Path_Option then
5207                   Linker_Switches.Increment_Last;
5208                   Linker_Switches.Table (Linker_Switches.Last) :=
5209                     new String'("-R");
5210                end if;
5211
5212                if Main_Project /= No_Project then
5213                   Library_Paths.Set_Last (0);
5214                   Library_Projs.Init;
5215
5216                   if MLib.Tgt.Support_For_Libraries /= MLib.Tgt.None then
5217                      --  Check for library projects
5218
5219                      for Proj1 in Project_Table.First ..
5220                            Project_Table.Last (Project_Tree.Projects)
5221                      loop
5222                         if Proj1 /= Main_Project
5223                           and then
5224                             Project_Tree.Projects.Table (Proj1).Library
5225                         then
5226                            --  Add this project to table Library_Projs
5227
5228                            There_Are_Libraries := True;
5229                            Depth :=
5230                              Project_Tree.Projects.Table (Proj1).Depth;
5231                            Library_Projs.Increment_Last;
5232                            Current := Library_Projs.Last;
5233
5234                            --  Any project with a greater depth should be
5235                            --  after this project in the list.
5236
5237                            while Current > 1 loop
5238                               Proj2 := Library_Projs.Table (Current - 1);
5239                               exit when Project_Tree.Projects.Table
5240                                           (Proj2).Depth <= Depth;
5241                               Library_Projs.Table (Current) := Proj2;
5242                               Current := Current - 1;
5243                            end loop;
5244
5245                            Library_Projs.Table (Current) := Proj1;
5246
5247                            --  If it is not a static library and path option
5248                            --  is set, add it to the Library_Paths table.
5249
5250                            if Project_Tree.Projects.Table
5251                                 (Proj1).Library_Kind /= Static
5252                              and then Path_Option /= null
5253                            then
5254                               Library_Paths.Increment_Last;
5255                               Library_Paths.Table (Library_Paths.Last) :=
5256                                 new String'
5257                                   (Get_Name_String
5258                                        (Project_Tree.Projects.Table
5259                                             (Proj1).Library_Dir));
5260                            end if;
5261                         end if;
5262                      end loop;
5263
5264                      for Index in 1 .. Library_Projs.Last loop
5265                         --  Add the -L switch
5266
5267                         Linker_Switches.Increment_Last;
5268                         Linker_Switches.Table (Linker_Switches.Last) :=
5269                           new String'("-L" &
5270                                       Get_Name_String
5271                                         (Project_Tree.Projects.Table
5272                                            (Library_Projs.Table (Index)).
5273                                               Library_Dir));
5274
5275                         --  Add the -l switch
5276
5277                         Linker_Switches.Increment_Last;
5278                         Linker_Switches.Table (Linker_Switches.Last) :=
5279                           new String'("-l" &
5280                                       Get_Name_String
5281                                         (Project_Tree.Projects.Table
5282                                            (Library_Projs.Table (Index)).
5283                                               Library_Name));
5284                      end loop;
5285                   end if;
5286
5287                   if There_Are_Libraries then
5288
5289                      --  If Path_Option is not null, create the switch
5290                      --  ("-Wl,-rpath," or equivalent) with all the non static
5291                      --  library dirs plus the standard GNAT library dir.
5292                      --  We do that only if Run_Path_Option is True
5293                      --  (not disabled by -R switch).
5294
5295                      if Run_Path_Option and Path_Option /= null then
5296                         declare
5297                            Option  : String_Access;
5298                            Length  : Natural := Path_Option'Length;
5299                            Current : Natural;
5300
5301                         begin
5302                            for Index in
5303                              Library_Paths.First .. Library_Paths.Last
5304                            loop
5305                               --  Add the length of the library dir plus one
5306                               --  for the directory separator.
5307
5308                               Length :=
5309                                 Length +
5310                                 Library_Paths.Table (Index)'Length + 1;
5311                            end loop;
5312
5313                            --  Finally, add the length of the standard GNAT
5314                            --  library dir.
5315
5316                            Length := Length + MLib.Utl.Lib_Directory'Length;
5317                            Option := new String (1 .. Length);
5318                            Option (1 .. Path_Option'Length) := Path_Option.all;
5319                            Current := Path_Option'Length;
5320
5321                            --  Put each library dir followed by a dir separator
5322
5323                            for Index in
5324                              Library_Paths.First .. Library_Paths.Last
5325                            loop
5326                               Option
5327                                 (Current + 1 ..
5328                                    Current +
5329                                    Library_Paths.Table (Index)'Length) :=
5330                                 Library_Paths.Table (Index).all;
5331                               Current :=
5332                                 Current +
5333                                 Library_Paths.Table (Index)'Length + 1;
5334                               Option (Current) := Path_Separator;
5335                            end loop;
5336
5337                            --  Finally put the standard GNAT library dir
5338
5339                            Option
5340                              (Current + 1 ..
5341                                 Current + MLib.Utl.Lib_Directory'Length) :=
5342                              MLib.Utl.Lib_Directory;
5343
5344                            --  And add the switch to the linker switches
5345
5346                            Linker_Switches.Increment_Last;
5347                            Linker_Switches.Table (Linker_Switches.Last) :=
5348                              Option;
5349                         end;
5350                      end if;
5351
5352                   end if;
5353
5354                   --  Put the object directories in ADA_OBJECTS_PATH
5355
5356                   Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
5357
5358                   --  Check for attributes Linker'Linker_Options in projects
5359                   --  other than the main project
5360
5361                   declare
5362                      Linker_Options : constant String_List :=
5363                                         Linker_Options_Switches
5364                                           (Main_Project, Project_Tree);
5365                   begin
5366                      for Option in Linker_Options'Range loop
5367                         Linker_Switches.Increment_Last;
5368                         Linker_Switches.Table (Linker_Switches.Last) :=
5369                           Linker_Options (Option);
5370                      end loop;
5371                   end;
5372                end if;
5373
5374                declare
5375                   Args : Argument_List
5376                            (Linker_Switches.First .. Linker_Switches.Last + 2);
5377
5378                   Last_Arg : Integer := Linker_Switches.First - 1;
5379                   Skip     : Boolean := False;
5380
5381                begin
5382                   --  Get all the linker switches
5383
5384                   for J in Linker_Switches.First .. Linker_Switches.Last loop
5385                      if Skip then
5386                         Skip := False;
5387
5388                      elsif Non_Std_Executable
5389                        and then Linker_Switches.Table (J).all = "-o"
5390                      then
5391                         Skip := True;
5392
5393                      else
5394                         Last_Arg := Last_Arg + 1;
5395                         Args (Last_Arg) := Linker_Switches.Table (J);
5396                      end if;
5397                   end loop;
5398
5399                   --  If need be, add the -o switch
5400
5401                   if Non_Std_Executable then
5402                      Last_Arg := Last_Arg + 1;
5403                      Args (Last_Arg) := new String'("-o");
5404                      Last_Arg := Last_Arg + 1;
5405                      Args (Last_Arg) :=
5406                        new String'(Get_Name_String (Executable));
5407                   end if;
5408
5409                   --  And invoke the linker
5410
5411                   begin
5412                      Link (Main_ALI_File,
5413                            Link_With_Shared_Libgcc.all &
5414                            Args (Args'First .. Last_Arg));
5415                      Successful_Links.Increment_Last;
5416                      Successful_Links.Table (Successful_Links.Last) :=
5417                        Main_ALI_File;
5418
5419                   exception
5420                      when Link_Failed =>
5421                         if Osint.Number_Of_Files = 1 or not Keep_Going then
5422                            raise;
5423
5424                         else
5425                            Write_Line ("*** link failed");
5426                            Failed_Links.Increment_Last;
5427                            Failed_Links.Table (Failed_Links.Last) :=
5428                              Main_ALI_File;
5429                         end if;
5430                   end;
5431                end;
5432
5433                Linker_Switches.Set_Last (Linker_Switches_Last);
5434             end Link_Step;
5435          end if;
5436
5437          --  We go to here when we skip the bind and link steps
5438
5439          <<Next_Main>>
5440
5441          --  We go to the next main, if we did not process the last one
5442
5443          if N_File < Osint.Number_Of_Files then
5444             Main_Source_File := Next_Main_Source;
5445
5446             if Current_File_Index /= No_Index then
5447                Main_Index := Current_File_Index;
5448             end if;
5449
5450             if Main_Project /= No_Project then
5451
5452                --  Find the file name of the main unit
5453
5454                declare
5455                   Main_Source_File_Name : constant String :=
5456                                             Get_Name_String (Main_Source_File);
5457
5458                   Main_Unit_File_Name : constant String :=
5459                                           Prj.Env.
5460                                             File_Name_Of_Library_Unit_Body
5461                                               (Name => Main_Source_File_Name,
5462                                                Project => Main_Project,
5463                                                In_Tree => Project_Tree,
5464                                                Main_Project_Only =>
5465                                                  not Unique_Compile);
5466
5467                   The_Packages : constant Package_Id :=
5468                     Project_Tree.Projects.Table
5469                       (Main_Project).Decl.Packages;
5470
5471                   Binder_Package : constant Prj.Package_Id :=
5472                                Prj.Util.Value_Of
5473                                  (Name        => Name_Binder,
5474                                   In_Packages => The_Packages,
5475                                   In_Tree     => Project_Tree);
5476
5477                   Linker_Package : constant Prj.Package_Id :=
5478                                Prj.Util.Value_Of
5479                                  (Name        => Name_Linker,
5480                                   In_Packages => The_Packages,
5481                                   In_Tree     => Project_Tree);
5482
5483                begin
5484                   --  We fail if we cannot find the main source file
5485                   --  as an immediate source of the main project file.
5486
5487                   if Main_Unit_File_Name = "" then
5488                      Make_Failed ('"' & Main_Source_File_Name,
5489                                   """ is not a unit of project ",
5490                                   Project_File_Name.all & ".");
5491
5492                   else
5493                      --  Remove any directory information from the main
5494                      --  source file name.
5495
5496                      declare
5497                         Pos : Natural := Main_Unit_File_Name'Last;
5498
5499                      begin
5500                         loop
5501                            exit when Pos < Main_Unit_File_Name'First
5502                              or else
5503                              Main_Unit_File_Name (Pos) = Directory_Separator;
5504                            Pos := Pos - 1;
5505                         end loop;
5506
5507                         Name_Len := Main_Unit_File_Name'Last - Pos;
5508
5509                         Name_Buffer (1 .. Name_Len) :=
5510                           Main_Unit_File_Name
5511                           (Pos + 1 .. Main_Unit_File_Name'Last);
5512
5513                         Main_Source_File := Name_Find;
5514                      end;
5515                   end if;
5516
5517                   --  We now deal with the binder and linker switches.
5518                   --  If no project file is used, there is nothing to do
5519                   --  because the binder and linker switches are the same
5520                   --  for all mains.
5521
5522                   --  Reset the tables Binder_Switches and Linker_Switches
5523
5524                   Binder_Switches.Set_Last (Last_Binder_Switch);
5525                   Linker_Switches.Set_Last (Last_Linker_Switch);
5526
5527                   --  Add binder switches from the project file for this main,
5528                   --  if any.
5529
5530                   if Do_Bind_Step and Binder_Package /= No_Package then
5531                      if Verbose_Mode then
5532                         Write_Str ("Adding binder switches for """);
5533                         Write_Str (Main_Unit_File_Name);
5534                         Write_Line (""".");
5535                      end if;
5536
5537                      Add_Switches
5538                        (File_Name   => Main_Unit_File_Name,
5539                         Index       => Main_Index,
5540                         The_Package => Binder_Package,
5541                         Program     => Binder);
5542                   end if;
5543
5544                   --  Add linker switches from the project file for this main,
5545                   --  if any.
5546
5547                   if Do_Link_Step and Linker_Package /= No_Package then
5548                      if Verbose_Mode then
5549                         Write_Str ("Adding linker switches for""");
5550                         Write_Str (Main_Unit_File_Name);
5551                         Write_Line (""".");
5552                      end if;
5553
5554                      Add_Switches
5555                        (File_Name   => Main_Unit_File_Name,
5556                         Index       => Main_Index,
5557                         The_Package => Linker_Package,
5558                         Program     => Linker);
5559                   end if;
5560
5561                   --  As we are using a project file, for relative paths we add
5562                   --  the current working directory for any relative path on
5563                   --  the command line and the project directory, for any
5564                   --  relative path in the project file.
5565
5566                   declare
5567                      Dir_Path : constant String_Access :=
5568                        new String'(Get_Name_String
5569                                      (Project_Tree.Projects.Table
5570                                         (Main_Project).Directory));
5571                   begin
5572                      for
5573                        J in Last_Binder_Switch + 1 .. Binder_Switches.Last
5574                      loop
5575                         Test_If_Relative_Path
5576                           (Binder_Switches.Table (J),
5577                            Parent => Dir_Path, Including_L_Switch => False);
5578                      end loop;
5579
5580                      for
5581                        J in Last_Linker_Switch + 1 .. Linker_Switches.Last
5582                      loop
5583                         Test_If_Relative_Path
5584                           (Linker_Switches.Table (J), Parent => Dir_Path);
5585                      end loop;
5586                   end;
5587
5588                   --  We now put in the Binder_Switches and Linker_Switches
5589                   --  tables, the binder and linker switches of the command
5590                   --  line that have been put in the Saved_ tables.
5591                   --  These switches will follow the project file switches.
5592
5593                   for J in 1 .. Saved_Binder_Switches.Last loop
5594                      Add_Switch
5595                        (Saved_Binder_Switches.Table (J),
5596                         Binder,
5597                         And_Save => False);
5598                   end loop;
5599
5600                   for J in 1 .. Saved_Linker_Switches.Last loop
5601                      Add_Switch
5602                        (Saved_Linker_Switches.Table (J),
5603                         Linker,
5604                         And_Save => False);
5605                   end loop;
5606                end;
5607             end if;
5608          end if;
5609
5610          --  Remove all marks to be sure to check sources for all executables,
5611          --  as the switches may be different and -s may be in use.
5612
5613          Delete_All_Marks;
5614       end loop Multiple_Main_Loop;
5615
5616       if Failed_Links.Last > 0 then
5617          for Index in 1 .. Successful_Links.Last loop
5618             Write_Str ("Linking of """);
5619             Write_Str (Get_Name_String (Successful_Links.Table (Index)));
5620             Write_Line (""" succeeded.");
5621          end loop;
5622
5623          for Index in 1 .. Failed_Links.Last loop
5624             Write_Str ("Linking of """);
5625             Write_Str (Get_Name_String (Failed_Links.Table (Index)));
5626             Write_Line (""" failed.");
5627          end loop;
5628
5629          if Total_Compilation_Failures = 0 then
5630             raise Compilation_Failed;
5631          end if;
5632       end if;
5633
5634       if Total_Compilation_Failures /= 0 then
5635          List_Bad_Compilations;
5636          raise Compilation_Failed;
5637       end if;
5638
5639       --  Delete the temporary mapping file that was created if we are
5640       --  using project files.
5641
5642       if not Debug.Debug_Flag_N then
5643          Delete_Mapping_Files;
5644          Prj.Env.Delete_All_Path_Files (Project_Tree);
5645       end if;
5646
5647       Exit_Program (E_Success);
5648
5649    exception
5650       when Bind_Failed =>
5651          Make_Failed ("*** bind failed.");
5652
5653       when Compilation_Failed =>
5654          if not Debug.Debug_Flag_N then
5655             Delete_Mapping_Files;
5656             Prj.Env.Delete_All_Path_Files (Project_Tree);
5657          end if;
5658
5659          Exit_Program (E_Fatal);
5660
5661       when Link_Failed =>
5662          Make_Failed ("*** link failed.");
5663
5664       when X : others =>
5665          Write_Line (Exception_Information (X));
5666          Make_Failed ("INTERNAL ERROR. Please report.");
5667    end Gnatmake;
5668
5669    ----------
5670    -- Hash --
5671    ----------
5672
5673    function Hash (F : Name_Id) return Header_Num is
5674    begin
5675       return Header_Num (1 + F mod Max_Header);
5676    end Hash;
5677
5678    --------------------
5679    -- In_Ada_Lib_Dir --
5680    --------------------
5681
5682    function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean is
5683       D : constant Name_Id := Get_Directory (File);
5684       B : constant Byte    := Get_Name_Table_Byte (D);
5685    begin
5686       return (B and Ada_Lib_Dir) /= 0;
5687    end In_Ada_Lib_Dir;
5688
5689    ------------
5690    -- Inform --
5691    ------------
5692
5693    procedure Inform (N : Name_Id := No_Name; Msg : String) is
5694    begin
5695       Osint.Write_Program_Name;
5696
5697       Write_Str (": ");
5698
5699       if N /= No_Name then
5700          Write_Str ("""");
5701          Write_Name (N);
5702          Write_Str (""" ");
5703       end if;
5704
5705       Write_Str (Msg);
5706       Write_Eol;
5707    end Inform;
5708
5709    -----------------------
5710    -- Init_Mapping_File --
5711    -----------------------
5712
5713    procedure Init_Mapping_File
5714      (Project    : Project_Id;
5715       File_Index : in out Natural)
5716    is
5717       FD : File_Descriptor;
5718
5719       Status : Boolean;
5720       --  For call to Close
5721
5722    begin
5723       --  Increase the index of the last mapping file for this project
5724
5725       Last_Mapping_File_Names (Project) :=
5726         Last_Mapping_File_Names (Project) + 1;
5727
5728       --  If there is a project file, call Create_Mapping_File with
5729       --  the project id.
5730
5731       if Project /= No_Project then
5732          Prj.Env.Create_Mapping_File
5733            (Project, Project_Tree,
5734             The_Mapping_File_Names
5735               (Project, Last_Mapping_File_Names (Project)));
5736
5737       --  Otherwise, just create an empty file
5738
5739       else
5740          Tempdir.Create_Temp_File
5741            (FD,
5742             The_Mapping_File_Names
5743               (No_Project, Last_Mapping_File_Names (No_Project)));
5744          if FD = Invalid_FD then
5745             Make_Failed ("disk full");
5746          end if;
5747
5748          Close (FD, Status);
5749
5750          if not Status then
5751             Make_Failed ("disk full");
5752          end if;
5753       end if;
5754
5755       --  And return the index of the newly created file
5756
5757       File_Index := Last_Mapping_File_Names (Project);
5758    end Init_Mapping_File;
5759
5760    ------------
5761    -- Init_Q --
5762    ------------
5763
5764    procedure Init_Q is
5765    begin
5766       First_Q_Initialization := False;
5767       Q_Front := Q.First;
5768       Q.Set_Last (Q.First);
5769    end Init_Q;
5770
5771    ----------------
5772    -- Initialize --
5773    ----------------
5774
5775    procedure Initialize is
5776    begin
5777       --  Override default initialization of Check_Object_Consistency
5778       --  since this is normally False for GNATBIND, but is True for
5779       --  GNATMAKE since we do not need to check source consistency
5780       --  again once GNATMAKE has looked at the sources to check.
5781
5782       Check_Object_Consistency := True;
5783
5784       --  Package initializations. The order of calls is important here
5785
5786       Output.Set_Standard_Error;
5787
5788       Gcc_Switches.Init;
5789       Binder_Switches.Init;
5790       Linker_Switches.Init;
5791
5792       Csets.Initialize;
5793       Namet.Initialize;
5794
5795       Snames.Initialize;
5796
5797       Prj.Initialize (Project_Tree);
5798
5799       Dependencies.Init;
5800
5801       RTS_Specified := null;
5802
5803       Mains.Delete;
5804
5805       --  Add the directory where gnatmake is invoked in front of the
5806       --  path, if gnatmake is invoked with directory information.
5807       --  Only do this if the platform is not VMS, where the notion of path
5808       --  does not really exist.
5809
5810       if not OpenVMS then
5811          declare
5812             Command : constant String := Command_Name;
5813
5814          begin
5815             for Index in reverse Command'Range loop
5816                if Command (Index) = Directory_Separator then
5817                   declare
5818                      Absolute_Dir : constant String :=
5819                                       Normalize_Pathname
5820                                         (Command (Command'First .. Index));
5821
5822                      PATH : constant String :=
5823                                       Absolute_Dir &
5824                                       Path_Separator &
5825                                       Getenv ("PATH").all;
5826
5827                   begin
5828                      Setenv ("PATH", PATH);
5829                   end;
5830
5831                   exit;
5832                end if;
5833             end loop;
5834          end;
5835       end if;
5836
5837       --  Scan the switches and arguments
5838
5839       Scan_Args : for Next_Arg in 1 .. Argument_Count loop
5840          Scan_Make_Arg (Argument (Next_Arg), And_Save => True);
5841       end loop Scan_Args;
5842
5843       if Usage_Requested then
5844          Usage;
5845       end if;
5846
5847       --  Test for trailing -P switch
5848
5849       if Project_File_Name_Present and then Project_File_Name = null then
5850          Make_Failed ("project file name missing after -P");
5851
5852       --  Test for trailing -o switch
5853
5854       elsif Output_File_Name_Present
5855         and then not Output_File_Name_Seen
5856       then
5857          Make_Failed ("output file name missing after -o");
5858
5859       --  Test for trailing -D switch
5860
5861       elsif Object_Directory_Present
5862         and then not Object_Directory_Seen then
5863          Make_Failed ("object directory missing after -D");
5864       end if;
5865
5866       --  Test for simultaneity of -i and -D
5867
5868       if Object_Directory_Path /= null and then In_Place_Mode then
5869          Make_Failed ("-i and -D cannot be used simutaneously");
5870       end if;
5871
5872       --  Deal with -C= switch
5873
5874       if Gnatmake_Mapping_File /= null then
5875          --  First, check compatibility with other switches
5876
5877          if Project_File_Name /= null then
5878             Make_Failed ("-C= switch is not compatible with -P switch");
5879
5880          elsif Saved_Maximum_Processes > 1 then
5881             Make_Failed ("-C= switch is not compatible with -jnnn switch");
5882          end if;
5883
5884          Fmap.Initialize (Gnatmake_Mapping_File.all);
5885          Add_Switch
5886            ("-gnatem=" & Gnatmake_Mapping_File.all,
5887             Compiler,
5888             And_Save => True);
5889       end if;
5890
5891       if Project_File_Name /= null then
5892
5893          --  A project file was specified by a -P switch
5894
5895          if Verbose_Mode then
5896             Write_Eol;
5897             Write_Str ("Parsing Project File """);
5898             Write_Str (Project_File_Name.all);
5899             Write_Str (""".");
5900             Write_Eol;
5901          end if;
5902
5903          --  Avoid looking in the current directory for ALI files
5904
5905          --  Look_In_Primary_Dir := False;
5906
5907          --  Set the project parsing verbosity to whatever was specified
5908          --  by a possible -vP switch.
5909
5910          Prj.Pars.Set_Verbosity (To => Current_Verbosity);
5911
5912          --  Parse the project file.
5913          --  If there is an error, Main_Project will still be No_Project.
5914
5915          Prj.Pars.Parse
5916            (Project           => Main_Project,
5917             In_Tree           => Project_Tree,
5918             Project_File_Name => Project_File_Name.all,
5919             Packages_To_Check => Packages_To_Check_By_Gnatmake);
5920
5921          if Main_Project = No_Project then
5922             Make_Failed ("""", Project_File_Name.all, """ processing failed");
5923          end if;
5924
5925          if Verbose_Mode then
5926             Write_Eol;
5927             Write_Str ("Parsing of Project File """);
5928             Write_Str (Project_File_Name.all);
5929             Write_Str (""" is finished.");
5930             Write_Eol;
5931          end if;
5932
5933          --  We add the source directories and the object directories
5934          --  to the search paths.
5935
5936          Add_Source_Directories (Main_Project, Project_Tree);
5937          Add_Object_Directories (Main_Project, Project_Tree);
5938
5939          --  Compute depth of each project
5940
5941          for Proj in Project_Table.First ..
5942                      Project_Table.Last (Project_Tree.Projects)
5943          loop
5944             Project_Tree.Projects.Table (Proj).Seen := False;
5945             Project_Tree.Projects.Table (Proj).Depth := 0;
5946          end loop;
5947
5948          Recursive_Compute_Depth
5949            (Main_Project, Depth => 1);
5950
5951       else
5952
5953          Osint.Add_Default_Search_Dirs;
5954
5955          --  Source file lookups should be cached for efficiency.
5956          --  Source files are not supposed to change. However, we do that now
5957          --  only if no project file is used; if a project file is used, we
5958          --  do it just after changing the directory to the object directory.
5959
5960          Osint.Source_File_Data (Cache => True);
5961
5962          --  Read gnat.adc file to initialize Fname.UF
5963
5964          Fname.UF.Initialize;
5965
5966          begin
5967             Fname.SF.Read_Source_File_Name_Pragmas;
5968
5969          exception
5970             when Err : SFN_Scan.Syntax_Error_In_GNAT_ADC =>
5971                Make_Failed (Exception_Message (Err));
5972          end;
5973       end if;
5974
5975       --  Make sure no project object directory is recorded
5976
5977       Project_Object_Directory := No_Project;
5978
5979    end Initialize;
5980
5981    ----------------------------
5982    -- Insert_Project_Sources --
5983    ----------------------------
5984
5985    procedure Insert_Project_Sources
5986      (The_Project  : Project_Id;
5987       All_Projects : Boolean;
5988       Into_Q       : Boolean)
5989    is
5990       Put_In_Q : Boolean := Into_Q;
5991       Unit     : Unit_Data;
5992       Sfile    : Name_Id;
5993
5994       Extending : constant Boolean :=
5995                     Project_Tree.Projects.Table
5996                       (The_Project).Extends /= No_Project;
5997
5998       function Check_Project (P : Project_Id) return Boolean;
5999       --  Returns True if P is The_Project or a project extended by
6000       --  The_Project.
6001
6002       -------------------
6003       -- Check_Project --
6004       -------------------
6005
6006       function Check_Project (P : Project_Id) return Boolean is
6007       begin
6008          if All_Projects or P = The_Project then
6009             return True;
6010          elsif Extending then
6011             declare
6012                Data : Project_Data :=
6013                         Project_Tree.Projects.Table (The_Project);
6014
6015             begin
6016                loop
6017                   if P = Data.Extends then
6018                      return True;
6019                   end if;
6020
6021                   Data := Project_Tree.Projects.Table (Data.Extends);
6022                   exit when Data.Extends = No_Project;
6023                end loop;
6024             end;
6025          end if;
6026
6027          return False;
6028       end Check_Project;
6029
6030    --  Start of processing for Insert_Project_Sources
6031
6032    begin
6033       --  For all the sources in the project files,
6034
6035       for Id in Unit_Table.First ..
6036                 Unit_Table.Last (Project_Tree.Units)
6037       loop
6038          Unit  := Project_Tree.Units.Table (Id);
6039          Sfile := No_Name;
6040
6041          --  If there is a source for the body, and the body has not been
6042          --  locally removed,
6043
6044          if Unit.File_Names (Body_Part).Name /= No_Name
6045            and then Unit.File_Names (Body_Part).Path /= Slash
6046          then
6047             --  And it is a source for the specified project
6048
6049             if Check_Project (Unit.File_Names (Body_Part).Project) then
6050
6051                --  If we don't have a spec, we cannot consider the source
6052                --  if it is a subunit
6053
6054                if Unit.File_Names (Specification).Name = No_Name then
6055                   declare
6056                      Src_Ind : Source_File_Index;
6057
6058                      --  Here we are cheating a little bit: we don't want to
6059                      --  use Sinput.L, because it depends on the GNAT tree
6060                      --  (Atree, Sinfo, ...). So, we pretend that it is
6061                      --  a project file, and we use Sinput.P.
6062                      --  Source_File_Is_Subunit is just scanning through
6063                      --  the file until it finds one of the reserved words
6064                      --  separate, procedure, function, generic or package.
6065                      --  Fortunately, these Ada reserved words are also
6066                      --  reserved for project files.
6067
6068                   begin
6069                      Src_Ind := Sinput.P.Load_Project_File
6070                                   (Get_Name_String
6071                                      (Unit.File_Names (Body_Part).Path));
6072
6073                      --  If it is a subunit, discard it
6074
6075                      if Sinput.P.Source_File_Is_Subunit (Src_Ind) then
6076                         Sfile := No_Name;
6077
6078                      else
6079                         Sfile := Unit.File_Names (Body_Part).Name;
6080                      end if;
6081                   end;
6082
6083                else
6084                   Sfile := Unit.File_Names (Body_Part).Name;
6085                end if;
6086             end if;
6087
6088          elsif Unit.File_Names (Specification).Name /= No_Name
6089            and then Unit.File_Names (Specification).Path /= Slash
6090            and then Check_Project (Unit.File_Names (Specification).Project)
6091          then
6092             --  If there is no source for the body, but there is a source
6093             --  for the spec which has not been locally removed, then we take
6094             --  this one.
6095
6096             Sfile := Unit.File_Names (Specification).Name;
6097          end if;
6098
6099          --  If Put_In_Q is True, we insert into the Q
6100
6101          if Put_In_Q then
6102
6103             --  For the first source inserted into the Q, we need
6104             --  to initialize the Q, but not for the subsequent sources.
6105
6106             if First_Q_Initialization then
6107                Init_Q;
6108             end if;
6109
6110             --  And of course, we only insert in the Q if the source
6111             --  is not marked.
6112
6113             if Sfile /= No_Name and then not Is_Marked (Sfile) then
6114                if Verbose_Mode then
6115                   Write_Str ("Adding """);
6116                   Write_Str (Get_Name_String (Sfile));
6117                   Write_Line (""" to the queue");
6118                end if;
6119
6120                Insert_Q (Sfile);
6121                Mark (Sfile);
6122             end if;
6123
6124          elsif Sfile /= No_Name then
6125
6126             --  If Put_In_Q is False, we add the source as it it were
6127             --  specified on the command line, and we set Put_In_Q to True,
6128             --  so that the following sources will be put directly in the
6129             --  queue. This will allow parallel compilation processes if -jx
6130             --  switch is used.
6131
6132             if Verbose_Mode then
6133                Write_Str ("Adding """);
6134                Write_Str (Get_Name_String (Sfile));
6135                Write_Line (""" as if on the command line");
6136             end if;
6137
6138             Osint.Add_File (Get_Name_String (Sfile));
6139             Put_In_Q := True;
6140
6141             --  As we may look into the Q later, ensure the Q has been
6142             --  initialized to avoid errors.
6143
6144             if First_Q_Initialization then
6145                Init_Q;
6146             end if;
6147          end if;
6148       end loop;
6149    end Insert_Project_Sources;
6150
6151    --------------
6152    -- Insert_Q --
6153    --------------
6154
6155    procedure Insert_Q
6156      (Source_File : File_Name_Type;
6157       Source_Unit : Unit_Name_Type := No_Name;
6158       Index       : Int            := 0)
6159    is
6160    begin
6161       if Debug.Debug_Flag_Q then
6162          Write_Str ("   Q := Q + [ ");
6163          Write_Name (Source_File);
6164
6165          if Index /= 0 then
6166             Write_Str (", ");
6167             Write_Int (Index);
6168          end if;
6169
6170          Write_Str (" ] ");
6171          Write_Eol;
6172       end if;
6173
6174       Q.Table (Q.Last) :=
6175         (File  => Source_File,
6176          Unit  => Source_Unit,
6177          Index => Index);
6178       Q.Increment_Last;
6179    end Insert_Q;
6180
6181    ---------------------
6182    -- Is_In_Obsoleted --
6183    ---------------------
6184
6185    function Is_In_Obsoleted (F : Name_Id) return Boolean is
6186    begin
6187       if F = No_File then
6188          return False;
6189
6190       else
6191          declare
6192             Name  : constant String := Get_Name_String (F);
6193             First : Natural := Name'Last;
6194             F2    : Name_Id := F;
6195
6196          begin
6197             while First > Name'First
6198               and then Name (First - 1) /= Directory_Separator
6199               and then Name (First - 1) /= '/'
6200             loop
6201                First := First - 1;
6202             end loop;
6203
6204             if First /= Name'First then
6205                Name_Len := 0;
6206                Add_Str_To_Name_Buffer (Name (First .. Name'Last));
6207                F2 := Name_Find;
6208             end if;
6209
6210             return Obsoleted.Get (F2);
6211          end;
6212       end if;
6213    end Is_In_Obsoleted;
6214
6215    ----------------------------
6216    -- Is_In_Object_Directory --
6217    ----------------------------
6218
6219    function Is_In_Object_Directory
6220      (Source_File   : File_Name_Type;
6221       Full_Lib_File : File_Name_Type) return Boolean
6222    is
6223    begin
6224       --  There is something to check only when using project files.
6225       --  Otherwise, this function returns True (last line of the function).
6226
6227       if Main_Project /= No_Project then
6228          declare
6229             Source_File_Name : constant String :=
6230                                  Get_Name_String (Source_File);
6231             Saved_Verbosity  : constant Verbosity := Current_Verbosity;
6232             Project          : Project_Id := No_Project;
6233             Path_Name        : Name_Id := No_Name;
6234             Data             : Project_Data;
6235
6236          begin
6237             --  Call Get_Reference to know the ultimate extending project of
6238             --  the source. Call it with verbosity default to avoid verbose
6239             --  messages.
6240
6241             Current_Verbosity := Default;
6242             Prj.Env.
6243               Get_Reference
6244               (Source_File_Name => Source_File_Name,
6245                Project          => Project,
6246                In_Tree          => Project_Tree,
6247                Path             => Path_Name);
6248             Current_Verbosity := Saved_Verbosity;
6249
6250             --  If this source is in a project, check that the ALI file is
6251             --  in its object directory. If it is not, return False, so that
6252             --  the ALI file will not be skipped.
6253
6254             --  If the source is not in an extending project, we fall back to
6255             --  the general case and return True at the end of the function.
6256
6257             if Project /= No_Project
6258               and then Project_Tree.Projects.Table
6259                          (Project).Extends /= No_Project
6260             then
6261                Data := Project_Tree.Projects.Table (Project);
6262
6263                declare
6264                   Object_Directory : constant String :=
6265                                        Normalize_Pathname
6266                                          (Get_Name_String
6267                                            (Data.Object_Directory));
6268
6269                   Olast : Natural := Object_Directory'Last;
6270
6271                   Lib_File_Directory : constant String :=
6272                                          Normalize_Pathname (Dir_Name
6273                                            (Get_Name_String (Full_Lib_File)));
6274
6275                   Llast : Natural := Lib_File_Directory'Last;
6276
6277                begin
6278                   --  For directories, Normalize_Pathname may or may not put
6279                   --  a directory separator at the end, depending on its input.
6280                   --  Remove any last directory separator before comparaison.
6281                   --  Returns True only if the two directories are the same.
6282
6283                   if Object_Directory (Olast) = Directory_Separator then
6284                      Olast := Olast - 1;
6285                   end if;
6286
6287                   if Lib_File_Directory (Llast) = Directory_Separator then
6288                      Llast := Llast - 1;
6289                   end if;
6290
6291                   return Object_Directory (Object_Directory'First .. Olast) =
6292                         Lib_File_Directory (Lib_File_Directory'First .. Llast);
6293                end;
6294             end if;
6295          end;
6296       end if;
6297
6298       --  When the source is not in a project file, always return True
6299
6300       return True;
6301    end Is_In_Object_Directory;
6302
6303    ----------
6304    -- Link --
6305    ----------
6306
6307    procedure Link (ALI_File : File_Name_Type; Args : Argument_List) is
6308       Link_Args : Argument_List (1 .. Args'Length + 1);
6309       Success   : Boolean;
6310
6311    begin
6312       Get_Name_String (ALI_File);
6313       Link_Args (1) := new String'(Name_Buffer (1 .. Name_Len));
6314
6315       Link_Args (2 .. Args'Length + 1) :=  Args;
6316
6317       GNAT.OS_Lib.Normalize_Arguments (Link_Args);
6318
6319       Display (Gnatlink.all, Link_Args);
6320
6321       if Gnatlink_Path = null then
6322          Make_Failed ("error, unable to locate ", Gnatlink.all);
6323       end if;
6324
6325       GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
6326
6327       if not Success then
6328          raise Link_Failed;
6329       end if;
6330    end Link;
6331
6332    ---------------------------
6333    -- List_Bad_Compilations --
6334    ---------------------------
6335
6336    procedure List_Bad_Compilations is
6337    begin
6338       for J in Bad_Compilation.First .. Bad_Compilation.Last loop
6339          if Bad_Compilation.Table (J).File = No_File then
6340             null;
6341          elsif not Bad_Compilation.Table (J).Found then
6342             Inform (Bad_Compilation.Table (J).File, "not found");
6343          else
6344             Inform (Bad_Compilation.Table (J).File, "compilation error");
6345          end if;
6346       end loop;
6347    end List_Bad_Compilations;
6348
6349    -----------------
6350    -- List_Depend --
6351    -----------------
6352
6353    procedure List_Depend is
6354       Lib_Name  : Name_Id;
6355       Obj_Name  : Name_Id;
6356       Src_Name  : Name_Id;
6357
6358       Len       : Natural;
6359       Line_Pos  : Natural;
6360       Line_Size : constant := 77;
6361
6362    begin
6363       Set_Standard_Output;
6364
6365       for A in ALIs.First .. ALIs.Last loop
6366          Lib_Name := ALIs.Table (A).Afile;
6367
6368          --  We have to provide the full library file name in In_Place_Mode
6369
6370          if In_Place_Mode then
6371             Lib_Name := Full_Lib_File_Name (Lib_Name);
6372          end if;
6373
6374          Obj_Name := Object_File_Name (Lib_Name);
6375          Write_Name (Obj_Name);
6376          Write_Str (" :");
6377
6378          Get_Name_String (Obj_Name);
6379          Len := Name_Len;
6380          Line_Pos := Len + 2;
6381
6382          for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
6383             Src_Name := Sdep.Table (D).Sfile;
6384
6385             if Is_Internal_File_Name (Src_Name)
6386               and then not Check_Readonly_Files
6387             then
6388                null;
6389             else
6390                if not Quiet_Output then
6391                   Src_Name := Full_Source_Name (Src_Name);
6392                end if;
6393
6394                Get_Name_String (Src_Name);
6395                Len := Name_Len;
6396
6397                if Line_Pos + Len + 1 > Line_Size then
6398                   Write_Str (" \");
6399                   Write_Eol;
6400                   Line_Pos := 0;
6401                end if;
6402
6403                Line_Pos := Line_Pos + Len + 1;
6404
6405                Write_Str (" ");
6406                Write_Name (Src_Name);
6407             end if;
6408          end loop;
6409
6410          Write_Eol;
6411       end loop;
6412
6413       Set_Standard_Error;
6414    end List_Depend;
6415
6416    -----------------
6417    -- Make_Failed --
6418    -----------------
6419
6420    procedure Make_Failed (S1 : String; S2 : String := ""; S3 : String := "") is
6421    begin
6422       Delete_All_Temp_Files;
6423       Osint.Fail (S1, S2, S3);
6424    end Make_Failed;
6425
6426    --------------------
6427    -- Mark_Directory --
6428    --------------------
6429
6430    procedure Mark_Directory
6431      (Dir  : String;
6432       Mark : Lib_Mark_Type)
6433    is
6434       N : Name_Id;
6435       B : Byte;
6436
6437    begin
6438       --  Dir last character is supposed to be a directory separator
6439
6440       Name_Len := Dir'Length;
6441       Name_Buffer (1 .. Name_Len) := Dir;
6442
6443       if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
6444          Name_Len := Name_Len + 1;
6445          Name_Buffer (Name_Len) := Directory_Separator;
6446       end if;
6447
6448       --  Add flags to the already existing flags
6449
6450       N := Name_Find;
6451       B := Get_Name_Table_Byte (N);
6452       Set_Name_Table_Byte (N, B or Mark);
6453    end Mark_Directory;
6454
6455    -----------------------------
6456    -- Recursive_Compute_Depth --
6457    -----------------------------
6458
6459    procedure Recursive_Compute_Depth
6460      (Project : Project_Id;
6461       Depth   : Natural)
6462    is
6463       List : Project_List;
6464       Proj : Project_Id;
6465
6466    begin
6467       --  Nothing to do if there is no project or if the project has already
6468       --  been seen or if the depth is large enough.
6469
6470       if Project = No_Project
6471         or else Project_Tree.Projects.Table (Project).Seen
6472         or else Project_Tree.Projects.Table (Project).Depth >= Depth
6473       then
6474          return;
6475       end if;
6476
6477       Project_Tree.Projects.Table (Project).Depth := Depth;
6478
6479       --  Mark the project as Seen to avoid endless loop caused by limited
6480       --  withs.
6481
6482       Project_Tree.Projects.Table (Project).Seen := True;
6483
6484       List := Project_Tree.Projects.Table (Project).Imported_Projects;
6485
6486       --  Visit each imported project
6487
6488       while List /= Empty_Project_List loop
6489          Proj := Project_Tree.Project_Lists.Table (List).Project;
6490          List := Project_Tree.Project_Lists.Table (List).Next;
6491          Recursive_Compute_Depth
6492            (Project => Proj,
6493             Depth => Depth + 1);
6494       end loop;
6495
6496       --  Visit a project being extended, if any
6497
6498       Recursive_Compute_Depth
6499         (Project => Project_Tree.Projects.Table (Project).Extends,
6500          Depth   => Depth + 1);
6501
6502       --  Reset the Seen flag, as we leave this project
6503
6504       Project_Tree.Projects.Table (Project).Seen := False;
6505    end Recursive_Compute_Depth;
6506
6507    -----------------------
6508    -- Sigint_Intercpted --
6509    -----------------------
6510
6511    procedure Sigint_Intercepted is
6512    begin
6513       Write_Line ("*** Interrupted ***");
6514       Delete_All_Temp_Files;
6515       OS_Exit (1);
6516    end Sigint_Intercepted;
6517
6518    -------------------
6519    -- Scan_Make_Arg --
6520    -------------------
6521
6522    procedure Scan_Make_Arg (Argv : String; And_Save : Boolean) is
6523    begin
6524       pragma Assert (Argv'First = 1);
6525
6526       if Argv'Length = 0 then
6527          return;
6528       end if;
6529
6530       --  If the previous switch has set the Project_File_Name_Present
6531       --  flag (that is we have seen a -P alone), then the next argument is
6532       --  the name of the project file.
6533
6534       if Project_File_Name_Present and then Project_File_Name = null then
6535          if Argv (1) = '-' then
6536             Make_Failed ("project file name missing after -P");
6537
6538          else
6539             Project_File_Name_Present := False;
6540             Project_File_Name := new String'(Argv);
6541          end if;
6542
6543       --  If the previous switch has set the Output_File_Name_Present
6544       --  flag (that is we have seen a -o), then the next argument is
6545       --  the name of the output executable.
6546
6547       elsif Output_File_Name_Present
6548         and then not Output_File_Name_Seen
6549       then
6550          Output_File_Name_Seen := True;
6551
6552          if Argv (1) = '-' then
6553             Make_Failed ("output file name missing after -o");
6554
6555          else
6556             Add_Switch ("-o", Linker, And_Save => And_Save);
6557
6558             --  Automatically add the executable suffix if it has not been
6559             --  specified explicitly.
6560
6561             declare
6562                Canonical_Argv : String := Argv;
6563             begin
6564                --  Get the file name in canonical case to accept as is
6565                --  names ending with ".EXE" on VMS and Windows.
6566
6567                Canonical_Case_File_Name (Canonical_Argv);
6568
6569                if Executable_Suffix'Length /= 0
6570                  and then (Canonical_Argv'Length <= Executable_Suffix'Length
6571                         or else Canonical_Argv
6572                                   (Canonical_Argv'Last -
6573                                    Executable_Suffix'Length + 1
6574                                    .. Canonical_Argv'Last)
6575                                 /= Executable_Suffix)
6576                then
6577                   Add_Switch
6578                     (Argv & Executable_Suffix,
6579                      Linker,
6580                      And_Save => And_Save);
6581                else
6582                   Add_Switch (Argv, Linker, And_Save => And_Save);
6583                end if;
6584             end;
6585          end if;
6586
6587       --  If the previous switch has set the Object_Directory_Present flag
6588       --  (that is we have seen a -D), then the next argument is
6589       --  the path name of the object directory..
6590
6591       elsif Object_Directory_Present
6592         and then not Object_Directory_Seen
6593       then
6594          Object_Directory_Seen := True;
6595
6596          if Argv (1) = '-' then
6597             Make_Failed ("object directory path name missing after -D");
6598
6599          elsif not Is_Directory (Argv) then
6600             Make_Failed ("cannot find object directory """, Argv, """");
6601
6602          else
6603             Add_Lib_Search_Dir (Argv);
6604
6605             --  Specify the object directory to the binder
6606
6607             Add_Switch ("-aO" & Argv, Binder, And_Save => And_Save);
6608
6609             --  Record the object directory. Make sure it ends with a directory
6610             --  separator.
6611
6612             if Argv (Argv'Last) = Directory_Separator then
6613                Object_Directory_Path := new String'(Argv);
6614
6615             else
6616                Object_Directory_Path :=
6617                  new String'(Argv & Directory_Separator);
6618             end if;
6619          end if;
6620
6621       --  Then check if we are dealing with -cargs/-bargs/-largs/-margs
6622
6623       elsif Argv = "-bargs"
6624               or else
6625             Argv = "-cargs"
6626               or else
6627             Argv = "-largs"
6628               or else
6629             Argv = "-margs"
6630       then
6631          case Argv (2) is
6632             when 'c' => Program_Args := Compiler;
6633             when 'b' => Program_Args := Binder;
6634             when 'l' => Program_Args := Linker;
6635             when 'm' => Program_Args := None;
6636
6637             when others =>
6638                raise Program_Error;
6639          end case;
6640
6641       --  A special test is needed for the -o switch within a -largs
6642       --  since that is another way to specify the name of the final
6643       --  executable.
6644
6645       elsif Program_Args = Linker
6646         and then Argv = "-o"
6647       then
6648          Make_Failed ("switch -o not allowed within a -largs. " &
6649                       "Use -o directly.");
6650
6651       --  Check to see if we are reading switches after a -cargs,
6652       --  -bargs or -largs switch. If yes save it.
6653
6654       elsif Program_Args /= None then
6655
6656          --  Check to see if we are reading -I switches in order
6657          --  to take into account in the src & lib search directories.
6658
6659          if Argv'Length > 2 and then Argv (1 .. 2) = "-I" then
6660             if Argv (3 .. Argv'Last) = "-" then
6661                Look_In_Primary_Dir := False;
6662
6663             elsif Program_Args = Compiler then
6664                if Argv (3 .. Argv'Last) /= "-" then
6665                   Add_Src_Search_Dir (Argv (3 .. Argv'Last));
6666                end if;
6667
6668             elsif Program_Args = Binder then
6669                Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
6670             end if;
6671          end if;
6672
6673          Add_Switch (Argv, Program_Args, And_Save => And_Save);
6674
6675       --  Handle non-default compiler, binder, linker, and handle --RTS switch
6676
6677       elsif Argv'Length > 2 and then Argv (1 .. 2) = "--" then
6678          if Argv'Length > 6
6679            and then Argv (1 .. 6) = "--GCC="
6680          then
6681             declare
6682                Program_Args : constant Argument_List_Access :=
6683                                 Argument_String_To_List
6684                                   (Argv (7 .. Argv'Last));
6685
6686             begin
6687                if And_Save then
6688                   Saved_Gcc := new String'(Program_Args.all (1).all);
6689                else
6690                   Gcc := new String'(Program_Args.all (1).all);
6691                end if;
6692
6693                for J in 2 .. Program_Args.all'Last loop
6694                   Add_Switch
6695                     (Program_Args.all (J).all,
6696                      Compiler,
6697                      And_Save => And_Save);
6698                end loop;
6699             end;
6700
6701          elsif Argv'Length > 11
6702            and then Argv (1 .. 11) = "--GNATBIND="
6703          then
6704             declare
6705                Program_Args : constant Argument_List_Access :=
6706                                 Argument_String_To_List
6707                                   (Argv (12 .. Argv'Last));
6708
6709             begin
6710                if And_Save then
6711                   Saved_Gnatbind := new String'(Program_Args.all (1).all);
6712                else
6713                   Gnatbind := new String'(Program_Args.all (1).all);
6714                end if;
6715
6716                for J in 2 .. Program_Args.all'Last loop
6717                   Add_Switch
6718                     (Program_Args.all (J).all, Binder, And_Save => And_Save);
6719                end loop;
6720             end;
6721
6722          elsif Argv'Length > 11
6723            and then Argv (1 .. 11) = "--GNATLINK="
6724          then
6725             declare
6726                Program_Args : constant Argument_List_Access :=
6727                                 Argument_String_To_List
6728                                   (Argv (12 .. Argv'Last));
6729             begin
6730                if And_Save then
6731                   Saved_Gnatlink := new String'(Program_Args.all (1).all);
6732                else
6733                   Gnatlink := new String'(Program_Args.all (1).all);
6734                end if;
6735
6736                for J in 2 .. Program_Args.all'Last loop
6737                   Add_Switch (Program_Args.all (J).all, Linker);
6738                end loop;
6739             end;
6740
6741          elsif Argv'Length >= 5 and then
6742            Argv (1 .. 5) = "--RTS"
6743          then
6744             Add_Switch (Argv, Compiler, And_Save => And_Save);
6745             Add_Switch (Argv, Binder, And_Save => And_Save);
6746
6747             if Argv'Length <= 6 or else Argv (6) /= '=' then
6748                Make_Failed ("missing path for --RTS");
6749
6750             else
6751                --  Check that this is the first time we see this switch or
6752                --  if it is not the first time, the same path is specified.
6753
6754                if RTS_Specified = null then
6755                   RTS_Specified := new String'(Argv (7 .. Argv'Last));
6756
6757                elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
6758                   Make_Failed ("--RTS cannot be specified multiple times");
6759                end if;
6760
6761                --  Valid --RTS switch
6762
6763                No_Stdinc := True;
6764                No_Stdlib := True;
6765                RTS_Switch := True;
6766
6767                declare
6768                   Src_Path_Name : constant String_Ptr :=
6769                                     Get_RTS_Search_Dir
6770                                       (Argv (7 .. Argv'Last), Include);
6771
6772                   Lib_Path_Name : constant String_Ptr :=
6773                                     Get_RTS_Search_Dir
6774                                       (Argv (7 .. Argv'Last), Objects);
6775
6776                begin
6777                   if Src_Path_Name /= null and then
6778                     Lib_Path_Name /= null
6779                   then
6780                      --  Set the RTS_*_Path_Name variables, so that the correct
6781                      --  directories will be set when
6782                      --  Osint.Add_Default_Search_Dirs will be called later.
6783
6784                      RTS_Src_Path_Name := Src_Path_Name;
6785                      RTS_Lib_Path_Name := Lib_Path_Name;
6786
6787                   elsif  Src_Path_Name = null
6788                     and Lib_Path_Name = null then
6789                      Make_Failed ("RTS path not valid: missing " &
6790                                   "adainclude and adalib directories");
6791
6792                   elsif Src_Path_Name = null then
6793                      Make_Failed ("RTS path not valid: missing adainclude " &
6794                                   "directory");
6795
6796                   elsif  Lib_Path_Name = null then
6797                      Make_Failed ("RTS path not valid: missing adalib " &
6798                                   "directory");
6799                   end if;
6800                end;
6801             end if;
6802
6803          else
6804             Make_Failed ("unknown switch: ", Argv);
6805          end if;
6806
6807       --  If we have seen a regular switch process it
6808
6809       elsif Argv (1) = '-' then
6810
6811          if Argv'Length = 1 then
6812             Make_Failed ("switch character cannot be followed by a blank");
6813
6814          --  -I-
6815
6816          elsif Argv (2 .. Argv'Last) = "I-" then
6817             Look_In_Primary_Dir := False;
6818
6819          --  Forbid  -?-  or  -??-  where ? is any character
6820
6821          elsif (Argv'Length = 3 and then Argv (3) = '-')
6822            or else (Argv'Length = 4 and then Argv (4) = '-')
6823          then
6824             Make_Failed ("trailing ""-"" at the end of ", Argv, " forbidden.");
6825
6826          --  -Idir
6827
6828          elsif Argv (2) = 'I' then
6829             Add_Src_Search_Dir (Argv (3 .. Argv'Last));
6830             Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
6831             Add_Switch (Argv, Compiler, And_Save => And_Save);
6832             Add_Switch (Argv, Binder, And_Save => And_Save);
6833
6834          --  -aIdir (to gcc this is like a -I switch)
6835
6836          elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
6837             Add_Src_Search_Dir (Argv (4 .. Argv'Last));
6838             Add_Switch ("-I" & Argv (4 .. Argv'Last),
6839                         Compiler,
6840                         And_Save => And_Save);
6841             Add_Switch (Argv, Binder, And_Save => And_Save);
6842
6843          --  -aOdir
6844
6845          elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
6846             Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
6847             Add_Switch (Argv, Binder, And_Save => And_Save);
6848
6849          --  -aLdir (to gnatbind this is like a -aO switch)
6850
6851          elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
6852             Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir);
6853             Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
6854             Add_Switch ("-aO" & Argv (4 .. Argv'Last),
6855                         Binder,
6856                         And_Save => And_Save);
6857
6858          --  -Adir (to gnatbind this is like a -aO switch, to gcc like a -I)
6859
6860          elsif Argv (2) = 'A' then
6861             Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir);
6862             Add_Src_Search_Dir (Argv (3 .. Argv'Last));
6863             Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
6864             Add_Switch ("-I"  & Argv (3 .. Argv'Last),
6865                         Compiler,
6866                         And_Save => And_Save);
6867             Add_Switch ("-aO" & Argv (3 .. Argv'Last),
6868                         Binder,
6869                         And_Save => And_Save);
6870
6871          --  -Ldir
6872
6873          elsif Argv (2) = 'L' then
6874             Add_Switch (Argv, Linker, And_Save => And_Save);
6875
6876          --  For -gxxxxx, -pg, -mxxx, -fxxx: give the switch to both the
6877          --  compiler and the linker (except for -gnatxxx which is only for
6878          --  the compiler). Some of the -mxxx (for example -m64) and -fxxx
6879          --  (for example -ftest-coverage for gcov) need to be used when
6880          --  compiling the binder generated files, and using all these gcc
6881          --  switches for the binder generated files should not be a problem.
6882
6883          elsif
6884            (Argv (2) = 'g' and then (Argv'Last < 5
6885                                        or else Argv (2 .. 5) /= "gnat"))
6886              or else Argv (2 .. Argv'Last) = "pg"
6887              or else (Argv (2) = 'm' and then Argv'Last > 2)
6888              or else (Argv (2) = 'f' and then Argv'Last > 2)
6889          then
6890             Add_Switch (Argv, Compiler, And_Save => And_Save);
6891             Add_Switch (Argv, Linker, And_Save => And_Save);
6892
6893          --  -C=<mapping file>
6894
6895          elsif Argv'Last > 2 and then Argv (2) = 'C' then
6896             if And_Save then
6897                if Argv (3) /= '=' or else Argv'Last <= 3 then
6898                   Make_Failed ("illegal switch ", Argv);
6899                end if;
6900
6901                Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
6902             end if;
6903
6904          --  -D
6905
6906          elsif Argv'Last = 2 and then Argv (2) = 'D' then
6907             if Project_File_Name /= null then
6908                Make_Failed ("-D cannot be used in conjunction with a " &
6909                             "project file");
6910
6911             else
6912                Scan_Make_Switches (Argv);
6913             end if;
6914
6915          --  -d
6916
6917          elsif Argv (2) = 'd'
6918            and then Argv'Last = 2
6919          then
6920             Display_Compilation_Progress := True;
6921
6922          --  -i
6923
6924          elsif Argv'Last = 2 and then Argv (2) = 'i' then
6925             if Project_File_Name /= null then
6926                Make_Failed ("-i cannot be used in conjunction with a " &
6927                             "project file");
6928
6929             else
6930                Scan_Make_Switches (Argv);
6931             end if;
6932
6933          --  -j (need to save the result)
6934
6935          elsif Argv (2) = 'j' then
6936             Scan_Make_Switches (Argv);
6937
6938             if And_Save then
6939                Saved_Maximum_Processes := Maximum_Processes;
6940             end if;
6941
6942          --  -m
6943
6944          elsif Argv (2) = 'm'
6945            and then Argv'Last = 2
6946          then
6947             Minimal_Recompilation := True;
6948
6949          --  -u
6950
6951          elsif Argv (2) = 'u'
6952            and then Argv'Last = 2
6953          then
6954             Unique_Compile   := True;
6955             Compile_Only := True;
6956             Do_Bind_Step     := False;
6957             Do_Link_Step     := False;
6958
6959          --  -U
6960
6961          elsif Argv (2) = 'U'
6962            and then Argv'Last = 2
6963          then
6964             Unique_Compile_All_Projects := True;
6965             Unique_Compile   := True;
6966             Compile_Only := True;
6967             Do_Bind_Step     := False;
6968             Do_Link_Step     := False;
6969
6970          --  -Pprj or -P prj (only once, and only on the command line)
6971
6972          elsif Argv (2) = 'P' then
6973             if Project_File_Name /= null then
6974                Make_Failed ("cannot have several project files specified");
6975
6976             elsif Object_Directory_Path /= null then
6977                Make_Failed ("-D cannot be used in conjunction with a " &
6978                             "project file");
6979
6980             elsif In_Place_Mode then
6981                Make_Failed ("-i cannot be used in conjunction with a " &
6982                             "project file");
6983
6984             elsif not And_Save then
6985
6986                --  It could be a tool other than gnatmake (i.e, gnatdist)
6987                --  or a -P switch inside a project file.
6988
6989                Fail
6990                  ("either the tool is not ""project-aware"" or " &
6991                   "a project file is specified inside a project file");
6992
6993             elsif Argv'Last = 2 then
6994
6995                --  -P is used alone: the project file name is the next option
6996
6997                Project_File_Name_Present := True;
6998
6999             else
7000                Project_File_Name := new String'(Argv (3 .. Argv'Last));
7001             end if;
7002
7003          --  -vPx  (verbosity of the parsing of the project files)
7004
7005          elsif Argv'Last = 4
7006            and then Argv (2 .. 3) = "vP"
7007            and then Argv (4) in '0' .. '2'
7008          then
7009             if And_Save then
7010                case Argv (4) is
7011                   when '0' =>
7012                      Current_Verbosity := Prj.Default;
7013                   when '1' =>
7014                      Current_Verbosity := Prj.Medium;
7015                   when '2' =>
7016                      Current_Verbosity := Prj.High;
7017                   when others =>
7018                      null;
7019                end case;
7020             end if;
7021
7022          --  -Xext=val  (External assignment)
7023
7024          elsif Argv (2) = 'X'
7025            and then Is_External_Assignment (Argv)
7026          then
7027             --  Is_External_Assignment has side effects
7028             --  when it returns True;
7029
7030             null;
7031
7032          --  If -gnath is present, then generate the usage information
7033          --  right now and do not pass this option on to the compiler calls.
7034
7035          elsif Argv = "-gnath" then
7036             Usage;
7037
7038          --  If -gnatc is specified, make sure the bind step and the link
7039          --  step are not executed.
7040
7041          elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
7042
7043             --  If -gnatc is specified, make sure the bind step and the link
7044             --  step are not executed.
7045
7046             Add_Switch (Argv, Compiler, And_Save => And_Save);
7047             Operating_Mode := Check_Semantics;
7048             Check_Object_Consistency := False;
7049             Compile_Only             := True;
7050             Do_Bind_Step                 := False;
7051             Do_Link_Step                 := False;
7052
7053          elsif Argv (2 .. Argv'Last) = "nostdlib" then
7054
7055             --  Don't pass -nostdlib to gnatlink, it will disable
7056             --  linking with all standard library files.
7057
7058             No_Stdlib := True;
7059
7060             Add_Switch (Argv, Compiler, And_Save => And_Save);
7061             Add_Switch (Argv, Binder, And_Save => And_Save);
7062
7063          elsif Argv (2 .. Argv'Last) = "nostdinc" then
7064
7065             --  Pass -nostdinc to the Compiler and to gnatbind
7066
7067             No_Stdinc := True;
7068             Add_Switch (Argv, Compiler, And_Save => And_Save);
7069             Add_Switch (Argv, Binder, And_Save => And_Save);
7070
7071             --  By default all switches with more than one character
7072             --  or one character switches which are not in 'a' .. 'z'
7073             --  (except 'C', 'F', 'M' and 'B') are passed to the compiler,
7074             --  unless we are dealing with a debug switch (starts with 'd')
7075             --  or an extended gnatmake switch (starts with 'e').
7076
7077          elsif Argv (2) /= 'd'
7078            and then Argv (2) /= 'e'
7079            and then Argv (2 .. Argv'Last) /= "C"
7080            and then Argv (2 .. Argv'Last) /= "F"
7081            and then Argv (2 .. Argv'Last) /= "M"
7082            and then Argv (2 .. Argv'Last) /= "B"
7083            and then (Argv'Length > 2 or else Argv (2) not in 'a' .. 'z')
7084          then
7085             Add_Switch (Argv, Compiler, And_Save => And_Save);
7086
7087          --  All other options are handled by Scan_Make_Switches
7088
7089          else
7090             Scan_Make_Switches (Argv);
7091          end if;
7092
7093       --  If not a switch it must be a file name
7094
7095       else
7096          Add_File (Argv);
7097          Mains.Add_Main (Argv);
7098       end if;
7099    end Scan_Make_Arg;
7100
7101    -----------------
7102    -- Switches_Of --
7103    -----------------
7104
7105    function Switches_Of
7106      (Source_File      : Name_Id;
7107       Source_File_Name : String;
7108       Source_Index     : Int;
7109       Naming           : Naming_Data;
7110       In_Package       : Package_Id;
7111       Allow_ALI        : Boolean) return Variable_Value
7112    is
7113       Switches : Variable_Value;
7114
7115       Defaults : constant Array_Element_Id :=
7116                    Prj.Util.Value_Of
7117                      (Name      => Name_Default_Switches,
7118                       In_Arrays =>
7119                         Project_Tree.Packages.Table
7120                           (In_Package).Decl.Arrays,
7121                       In_Tree   => Project_Tree);
7122
7123       Switches_Array : constant Array_Element_Id :=
7124                          Prj.Util.Value_Of
7125                            (Name      => Name_Switches,
7126                             In_Arrays =>
7127                               Project_Tree.Packages.Table
7128                                 (In_Package).Decl.Arrays,
7129                             In_Tree   => Project_Tree);
7130
7131    begin
7132       Switches :=
7133         Prj.Util.Value_Of
7134           (Index     => Source_File,
7135            Src_Index => Source_Index,
7136            In_Array  => Switches_Array,
7137            In_Tree   => Project_Tree);
7138
7139       if Switches = Nil_Variable_Value then
7140          declare
7141             Name        : String (1 .. Source_File_Name'Length + 3);
7142             Last        : Positive := Source_File_Name'Length;
7143             Spec_Suffix : constant String :=
7144                             Get_Name_String (Naming.Ada_Spec_Suffix);
7145             Body_Suffix : constant String :=
7146                             Get_Name_String (Naming.Ada_Body_Suffix);
7147             Truncated   : Boolean := False;
7148
7149          begin
7150             Name (1 .. Last) := Source_File_Name;
7151
7152             if Last > Body_Suffix'Length
7153                and then Name (Last - Body_Suffix'Length + 1 .. Last) =
7154                                                                   Body_Suffix
7155             then
7156                Truncated := True;
7157                Last := Last - Body_Suffix'Length;
7158             end if;
7159
7160             if not Truncated
7161               and then Last > Spec_Suffix'Length
7162               and then Name (Last - Spec_Suffix'Length + 1 .. Last) =
7163                                                                  Spec_Suffix
7164             then
7165                Truncated := True;
7166                Last := Last - Spec_Suffix'Length;
7167             end if;
7168
7169             if Truncated then
7170                Name_Len := Last;
7171                Name_Buffer (1 .. Name_Len) := Name (1 .. Last);
7172                Switches :=
7173                  Prj.Util.Value_Of
7174                    (Index     => Name_Find,
7175                     Src_Index => 0,
7176                     In_Array  => Switches_Array,
7177                     In_Tree   => Project_Tree);
7178
7179                if Switches = Nil_Variable_Value
7180                  and then Allow_ALI
7181                then
7182                   Last := Source_File_Name'Length;
7183
7184                   while Name (Last) /= '.' loop
7185                      Last := Last - 1;
7186                   end loop;
7187
7188                   Name (Last + 1 .. Last + 3) := "ali";
7189                   Name_Len := Last + 3;
7190                   Name_Buffer (1 .. Name_Len) := Name (1 .. Name_Len);
7191                   Switches :=
7192                     Prj.Util.Value_Of
7193                       (Index     => Name_Find,
7194                        Src_Index => 0,
7195                        In_Array  => Switches_Array,
7196                        In_Tree   => Project_Tree);
7197                end if;
7198             end if;
7199          end;
7200       end if;
7201
7202       if Switches = Nil_Variable_Value then
7203          Switches :=
7204            Prj.Util.Value_Of
7205              (Index     => Name_Ada,
7206               Src_Index => 0,
7207               In_Array  => Defaults,
7208               In_Tree   => Project_Tree);
7209       end if;
7210
7211       return Switches;
7212    end Switches_Of;
7213
7214    -----------
7215    -- Usage --
7216    -----------
7217
7218    procedure Usage is
7219    begin
7220       if Usage_Needed then
7221          Usage_Needed := False;
7222          Makeusg;
7223       end if;
7224    end Usage;
7225
7226    -----------------
7227    -- Verbose_Msg --
7228    -----------------
7229
7230    procedure Verbose_Msg
7231      (N1     : Name_Id;
7232       S1     : String;
7233       N2     : Name_Id := No_Name;
7234       S2     : String  := "";
7235       Prefix : String := "  -> ")
7236    is
7237    begin
7238       if not Verbose_Mode then
7239          return;
7240       end if;
7241
7242       Write_Str (Prefix);
7243       Write_Str ("""");
7244       Write_Name (N1);
7245       Write_Str (""" ");
7246       Write_Str (S1);
7247
7248       if N2 /= No_Name then
7249          Write_Str (" """);
7250          Write_Name (N2);
7251          Write_Str (""" ");
7252       end if;
7253
7254       Write_Str (S2);
7255       Write_Eol;
7256    end Verbose_Msg;
7257
7258 begin
7259    --  Make sure that in case of failure, the temp files will be deleted
7260
7261    Prj.Com.Fail    := Make_Failed'Access;
7262    MLib.Fail       := Make_Failed'Access;
7263    Makeutl.Do_Fail := Make_Failed'Access;
7264 end Make;