OSDN Git Service

Remove s-crtl-vms64.ads, no longer used.
[pf3gnuchains/gcc-fork.git] / gcc / ada / makegpr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              M A K E G P R                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2007, 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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Csets;
27 with Gnatvsn;
28 with Hostparm; use Hostparm;
29 with Makeutl;  use Makeutl;
30 with MLib.Tgt; use MLib.Tgt;
31 with Namet;    use Namet;
32 with Output;   use Output;
33 with Opt;      use Opt;
34 with Osint;    use Osint;
35 with Prj;      use Prj;
36 with Prj.Ext;  use Prj.Ext;
37 with Prj.Pars;
38 with Prj.Util; use Prj.Util;
39 with Snames;   use Snames;
40 with Table;
41 with Types;    use Types;
42
43 with Ada.Command_Line;           use Ada.Command_Line;
44 with Ada.Strings.Fixed;          use Ada.Strings.Fixed;
45 with Ada.Text_IO;                use Ada.Text_IO;
46 with Ada.Unchecked_Deallocation;
47
48 with GNAT.Directory_Operations;  use GNAT.Directory_Operations;
49 with GNAT.Dynamic_Tables;
50 with GNAT.Expect;                use GNAT.Expect;
51 with GNAT.HTable;
52 with GNAT.OS_Lib;                use GNAT.OS_Lib;
53 with GNAT.Regpat;                use GNAT.Regpat;
54
55 with System;
56 with System.Case_Util;           use System.Case_Util;
57
58 package body Makegpr is
59
60    On_Windows : constant Boolean := Directory_Separator = '\';
61    --  True when on Windows. Used in Check_Compilation_Needed when processing
62    --  C/C++ dependency files for backslash handling.
63
64    Max_In_Archives : constant := 50;
65    --  The maximum number of arguments for a single invocation of the
66    --  Archive Indexer (ar).
67
68    No_Argument : aliased Argument_List := (1 .. 0 => null);
69    --  Null argument list representing case of no arguments
70
71    FD : Process_Descriptor;
72    --  The process descriptor used when invoking a non GNU compiler with -M
73    --  and getting the output with GNAT.Expect.
74
75    Line_Matcher : constant Pattern_Matcher := Compile ("^.*?\n", Single_Line);
76    --  Pattern for GNAT.Expect for the invocation of a non GNU compiler with -M
77
78    Name_Ide              : Name_Id;
79    Name_Compiler_Command : Name_Id;
80    --  Names of package IDE and its attribute Compiler_Command.
81    --  Set up by Initialize.
82
83    Unique_Compile : Boolean := False;
84    --  True when switch -u is used on the command line
85
86    type Source_Index_Rec is record
87       Project : Project_Id;
88       Id      : Other_Source_Id;
89       Found   : Boolean := False;
90    end record;
91    --  Used as Source_Indexes component to check if archive needs to be rebuilt
92
93    type Source_Index_Array is array (Positive range <>) of Source_Index_Rec;
94    type Source_Indexes_Ref is access Source_Index_Array;
95
96    procedure Free is new Ada.Unchecked_Deallocation
97      (Source_Index_Array, Source_Indexes_Ref);
98
99    Initial_Source_Index_Count : constant Positive := 20;
100    Source_Indexes : Source_Indexes_Ref :=
101      new Source_Index_Array (1 .. Initial_Source_Index_Count);
102    --  A list of the Other_Source_Ids of a project file, with an indication
103    --  that they have been found in the archive dependency file.
104
105    Last_Source : Natural := 0;
106    --  The index of the last valid component of Source_Indexes
107
108    Compiler_Names : array (First_Language_Indexes) of String_Access;
109    --  The names of the compilers to be used. Set up by Get_Compiler.
110    --  Used to display the commands spawned.
111
112    Gnatmake_String       : constant String_Access := new String'("gnatmake");
113    GCC_String            : constant String_Access := new String'("gcc");
114    G_Plus_Plus_String    : constant String_Access := new String'("g++");
115
116    Default_Compiler_Names : constant array
117      (First_Language_Indexes range
118         Ada_Language_Index .. C_Plus_Plus_Language_Index)
119      of String_Access :=
120        (Ada_Language_Index         => Gnatmake_String,
121         C_Language_Index           => GCC_String,
122         C_Plus_Plus_Language_Index => G_Plus_Plus_String);
123
124    Compiler_Paths : array (First_Language_Indexes) of String_Access;
125    --  The path names of the compiler to be used. Set up by Get_Compiler.
126    --  Used to spawn compiling/linking processes.
127
128    Compiler_Is_Gcc : array (First_Language_Indexes) of Boolean;
129    --  An indication that a compiler is a GCC compiler, to be able to use
130    --  specific GCC switches.
131
132    Archive_Builder_Path : String_Access := null;
133    --  The path name of the archive builder (ar). To be used when spawning
134    --  ar commands.
135
136    Archive_Indexer_Path : String_Access := null;
137    --  The path name of the archive indexer (ranlib), if it exists
138
139    Copyright_Output : Boolean := False;
140    Usage_Output     : Boolean := False;
141    --  Flags to avoid multiple displays of Copyright notice and of Usage
142
143    Output_File_Name           : String_Access := null;
144    --  The name given after a switch -o
145
146    Output_File_Name_Expected  : Boolean := False;
147    --  True when last switch was -o
148
149    Project_File_Name          : String_Access := null;
150    --  The name of the project file specified with switch -P
151
152    Project_File_Name_Expected : Boolean := False;
153    --  True when last switch was -P
154
155    Naming_String   : aliased String := "naming";
156    Builder_String  : aliased String := "builder";
157    Compiler_String : aliased String := "compiler";
158    Binder_String   : aliased String := "binder";
159    Linker_String   : aliased String := "linker";
160    --  Name of packages to be checked when parsing/processing project files
161
162    List_Of_Packages : aliased String_List :=
163      (Naming_String   'Access,
164       Builder_String  'Access,
165       Compiler_String 'Access,
166       Binder_String   'Access,
167       Linker_String   'Access);
168    Packages_To_Check : constant String_List_Access := List_Of_Packages'Access;
169    --  List of the packages to be checked when parsing/processing project files
170
171    Project_Tree : constant Project_Tree_Ref := new Project_Tree_Data;
172
173    Main_Project : Project_Id;
174    --  The project id of the main project
175
176    type Processor is (None, Linker, Compiler);
177    Current_Processor : Processor := None;
178    --  This variable changes when switches -*args are used
179
180    Current_Language  : Language_Index := Ada_Language_Index;
181    --  The compiler language to consider when Processor is Compiler
182
183    package Comp_Opts is new GNAT.Dynamic_Tables
184      (Table_Component_Type => String_Access,
185       Table_Index_Type     => Integer,
186       Table_Low_Bound      => 1,
187       Table_Initial        => 20,
188       Table_Increment      => 100);
189    Options : array (First_Language_Indexes) of Comp_Opts.Instance;
190    --  Tables to store compiling options for the different compilers
191
192    package Linker_Options is new Table.Table
193      (Table_Component_Type => String_Access,
194       Table_Index_Type     => Integer,
195       Table_Low_Bound      => 1,
196       Table_Initial        => 20,
197       Table_Increment      => 100,
198       Table_Name           => "Makegpr.Linker_Options");
199    --  Table to store the linking options
200
201    package Library_Opts is new Table.Table
202      (Table_Component_Type => String_Access,
203       Table_Index_Type     => Integer,
204       Table_Low_Bound      => 1,
205       Table_Initial        => 20,
206       Table_Increment      => 100,
207       Table_Name           => "Makegpr.Library_Opts");
208    --  Table to store the linking options
209
210    package Ada_Mains is new Table.Table
211      (Table_Component_Type => String_Access,
212       Table_Index_Type     => Integer,
213       Table_Low_Bound      => 1,
214       Table_Initial        => 20,
215       Table_Increment      => 100,
216       Table_Name           => "Makegpr.Ada_Mains");
217    --  Table to store the Ada mains, either specified on the command line
218    --  or found in attribute Main of the main project file.
219
220    package Other_Mains is new Table.Table
221      (Table_Component_Type => Other_Source,
222       Table_Index_Type     => Integer,
223       Table_Low_Bound      => 1,
224       Table_Initial        => 20,
225       Table_Increment      => 100,
226       Table_Name           => "Makegpr.Other_Mains");
227    --  Table to store the mains of languages other than Ada, either specified
228    --  on the command line or found in attribute Main of the main project file.
229
230    package Sources_Compiled is new GNAT.HTable.Simple_HTable
231      (Header_Num => Header_Num,
232       Element    => Boolean,
233       No_Element => False,
234       Key        => File_Name_Type,
235       Hash       => Hash,
236       Equal      => "=");
237
238    package Saved_Switches is new Table.Table
239      (Table_Component_Type => String_Access,
240       Table_Index_Type     => Integer,
241       Table_Low_Bound      => 1,
242       Table_Initial        => 10,
243       Table_Increment      => 100,
244       Table_Name           => "Makegpr.Saved_Switches");
245    --  Table to store the switches to be passed to gnatmake
246
247    Initial_Argument_Count : constant Positive := 20;
248    type Boolean_Array is array (Positive range <>) of Boolean;
249    type Booleans is access Boolean_Array;
250
251    procedure Free is new Ada.Unchecked_Deallocation (Boolean_Array, Booleans);
252
253    Arguments : Argument_List_Access :=
254      new Argument_List (1 .. Initial_Argument_Count);
255    --  Used to store lists of arguments to be used when spawning a process
256
257    Arguments_Displayed : Booleans :=
258      new Boolean_Array (1 .. Initial_Argument_Count);
259    --  For each argument in Arguments, indicate if the argument should be
260    --  displayed when procedure Display_Command is called.
261
262    Last_Argument : Natural := 0;
263    --  Index of the last valid argument in Arguments
264
265    package Cache_Args is new Table.Table
266      (Table_Component_Type => String_Access,
267       Table_Index_Type     => Integer,
268       Table_Low_Bound      => 1,
269       Table_Initial        => 200,
270       Table_Increment      => 100,
271       Table_Name           => "Makegpr.Cache_Args");
272    --  A table to cache arguments, to avoid multiple allocation of the same
273    --  strings. It is not possible to use a hash table, because String is
274    --  an unconstrained type.
275
276    --  Various switches used when spawning processes:
277
278    Dash_B_String     : aliased  String := "-B";
279    Dash_B            : constant String_Access := Dash_B_String'Access;
280    Dash_c_String     : aliased  String := "-c";
281    Dash_c            : constant String_Access := Dash_c_String'Access;
282    Dash_cargs_String : aliased  String := "-cargs";
283    Dash_cargs        : constant String_Access := Dash_cargs_String'Access;
284    Dash_d_String     : aliased  String := "-d";
285    Dash_d            : constant String_Access := Dash_d_String'Access;
286    Dash_f_String     : aliased  String := "-f";
287    Dash_f            : constant String_Access := Dash_f_String'Access;
288    Dash_k_String     : aliased  String := "-k";
289    Dash_k            : constant String_Access := Dash_k_String'Access;
290    Dash_largs_String : aliased  String := "-largs";
291    Dash_largs        : constant String_Access := Dash_largs_String'Access;
292    Dash_M_String     : aliased  String := "-M";
293    Dash_M            : constant String_Access := Dash_M_String'Access;
294    Dash_margs_String : aliased  String := "-margs";
295    Dash_margs        : constant String_Access := Dash_margs_String'Access;
296    Dash_o_String     : aliased  String := "-o";
297    Dash_o            : constant String_Access := Dash_o_String'Access;
298    Dash_P_String     : aliased  String := "-P";
299    Dash_P            : constant String_Access := Dash_P_String'Access;
300    Dash_q_String     : aliased  String := "-q";
301    Dash_q            : constant String_Access := Dash_q_String'Access;
302    Dash_u_String     : aliased  String := "-u";
303    Dash_u            : constant String_Access := Dash_u_String'Access;
304    Dash_v_String     : aliased  String := "-v";
305    Dash_v            : constant String_Access := Dash_v_String'Access;
306    Dash_vP1_String   : aliased  String := "-vP1";
307    Dash_vP1          : constant String_Access := Dash_vP1_String'Access;
308    Dash_vP2_String   : aliased  String := "-vP2";
309    Dash_vP2          : constant String_Access := Dash_vP2_String'Access;
310    Dash_x_String     : aliased  String := "-x";
311    Dash_x            : constant String_Access := Dash_x_String'Access;
312    r_String          : aliased  String := "r";
313    r                 : constant String_Access := r_String'Access;
314
315    CPATH : constant String := "CPATH";
316    --  The environment variable to set when compiler is a GCC compiler
317    --  to indicate the include directory path.
318
319    Current_Include_Paths : array (First_Language_Indexes) of String_Access;
320    --  A cache for the paths of included directories, to avoid setting
321    --  env var CPATH unnecessarily.
322
323    C_Plus_Plus_Is_Used : Boolean := False;
324    --  True when there are sources in C++
325
326    Link_Options_Switches : Argument_List_Access := null;
327    --  The link options coming from the attributes Linker'Linker_Options in
328    --  project files imported, directly or indirectly, by the main project.
329
330    Total_Number_Of_Errors : Natural := 0;
331    --  Used when Keep_Going is True (switch -k) to keep the total number
332    --  of compilation/linking errors, to report at the end of execution.
333
334    Need_To_Rebuild_Global_Archive : Boolean := False;
335
336    Error_Header : constant String := "*** ERROR: ";
337    --  The beginning of error message, when Keep_Going is True
338
339    Need_To_Relink : Boolean := False;
340    --  True when an executable of a language other than Ada need to be linked
341
342    Global_Archive_Exists : Boolean := False;
343    --  True if there is a non empty global archive, to prevent creation
344    --  of such archives.
345
346    Path_Option : String_Access;
347    --  The path option switch, when supported
348
349    Project_Of_Current_Object_Directory : Project_Id := No_Project;
350    --  The object directory of the project for the last compilation. Avoid
351    --  calling Change_Dir if the current working directory is already this
352    --  directory.
353
354    package Lib_Path is new Table.Table
355      (Table_Component_Type => Character,
356       Table_Index_Type     => Integer,
357       Table_Low_Bound      => 1,
358       Table_Initial        => 200,
359       Table_Increment      => 100,
360       Table_Name           => "Makegpr.Lib_Path");
361    --  A table to compute the path to put in the path option switch, when it
362    --  is supported.
363
364    procedure Add_Archives (For_Gnatmake : Boolean);
365    --  Add to Arguments the list of archives for linking an executable
366
367    procedure Add_Argument (Arg : String_Access; Display : Boolean);
368    procedure Add_Argument (Arg : String; Display : Boolean);
369    --  Add an argument to Arguments. Reallocate if necessary
370
371    procedure Add_Arguments (Args : Argument_List; Display : Boolean);
372    --  Add a list of arguments to Arguments. Reallocate if necessary
373
374    procedure Add_Option (Arg : String);
375    --  Add a switch for the Ada, C or C++ compiler, or for the linker.
376    --  The table where this option is stored depends on the values of
377    --  Current_Processor and Current_Language.
378
379    procedure Add_Search_Directories
380      (Data     : Project_Data;
381       Language : First_Language_Indexes);
382    --  Either add to the Arguments the necessary -I switches needed to
383    --  compile, or, when compiler is gcc/g++, set up the C*INCLUDE_PATH
384    --  environment variable, if necessary.
385
386    procedure Add_Source_Id (Project : Project_Id; Id : Other_Source_Id);
387    --  Add a source id to Source_Indexes, with Found set to False
388
389    procedure Add_Switches
390      (Data      : Project_Data;
391       Proc      : Processor;
392       Language  : Language_Index;
393       File_Name : File_Name_Type);
394    --  Add to Arguments the switches, if any, for a source (attribute Switches)
395    --  or language (attribute Default_Switches), coming from package Compiler
396    --  or Linker (depending on Proc) of a specified project file.
397
398    procedure Build_Global_Archive;
399    --  Build the archive for the main project
400
401    procedure Build_Library (Project : Project_Id; Unconditionally : Boolean);
402    --  Build the library for a library project. If Unconditionally is
403    --  False, first check if the library is up to date, and build it only
404    --  if it is not.
405
406    procedure Check (Option : String);
407    --  Check that a switch coming from a project file is not the concatenation
408    --  of several valid switch, for example "-g -v". If it is, issue a warning.
409
410    procedure Check_Archive_Builder;
411    --  Check if the archive builder (ar) is there
412
413    procedure Check_Compilation_Needed
414      (Source          : Other_Source;
415       Need_To_Compile : out Boolean);
416    --  Check if a source of a language other than Ada needs to be compiled or
417    --  recompiled.
418
419    procedure Check_For_C_Plus_Plus;
420    --  Check if C++ is used in at least one project
421
422    procedure Compile
423      (Source_Id    : Other_Source_Id;
424       Data         : Project_Data;
425       Local_Errors : in out Boolean);
426    --  Compile one non-Ada source
427
428    procedure Compile_Individual_Sources;
429    --  Compile the sources specified on the command line, when in
430    --  Unique_Compile mode.
431
432    procedure Compile_Link_With_Gnatmake (Mains_Specified : Boolean);
433    --  Compile/Link with gnatmake when there are Ada sources in the main
434    --  project. Arguments may already contain options to be used by
435    --  gnatmake. Used for both Ada mains and mains of other languages.
436    --  When Compile_Only is True, do not use the linking options
437
438    procedure Compile_Sources;
439    --  Compile the sources of languages other than Ada, if necessary
440
441    procedure Copyright;
442    --  Output the Copyright notice
443
444    procedure Create_Archive_Dependency_File
445      (Name         : String;
446       First_Source : Other_Source_Id);
447    --  Create the archive dependency file for a library project
448
449    procedure Create_Global_Archive_Dependency_File (Name : String);
450    --  Create the archive depenency file for the main project
451
452    procedure Display_Command
453      (Name    : String;
454       Path    : String_Access;
455       CPATH   : String_Access := null;
456       Ellipse : Boolean := False);
457    --  Display the command for a spawned process, if in Verbose_Mode or not in
458    --  Quiet_Output. In non verbose mode, when Ellipse is True, display "..."
459    --  in place of the first argument that has Display set to False.
460
461    procedure Get_Compiler (For_Language : First_Language_Indexes);
462    --  Find the compiler name and path name for a specified programming
463    --  language, if not already done. Results are in the corresponding elements
464    --  of arrays Compiler_Names and Compiler_Paths. Name of compiler is found
465    --  in package IDE of the main project, or defaulted. Fail if compiler
466    --  cannot be found on the path. For the Ada language, gnatmake, rather than
467    --  the Ada compiler is returned.
468
469    procedure Get_Imported_Directories
470      (Project : Project_Id;
471       Data    : in out Project_Data);
472    --  Find the necessary switches -I to be used when compiling sources of
473    --  languages other than Ada, in a specified project file. Cache the result
474    --  in component Imported_Directories_Switches of the project data. For
475    --  gcc/g++ compilers, get the value of the C*_INCLUDE_PATH, instead.
476
477    procedure Initialize;
478    --  Do the necessary package initialization and process the command line
479    --  arguments.
480
481    function Is_Included_In_Global_Archive
482      (Object_Name : File_Name_Type;
483       Project     : Project_Id) return Boolean;
484    --  Return True if the object Object_Name is not overridden by a source
485    --  in a project extending project Project.
486
487    procedure Link_Executables;
488    --  Link executables
489
490    procedure Report_Error (S1 : String; S2 : String := ""; S3 : String := "");
491    --  Report an error. If Keep_Going is False, just call Osint.Fail. If
492    --  Keep_Going is True, display the error and increase the total number of
493    --  errors.
494
495    procedure Report_Total_Errors (Kind : String);
496    --  If Total_Number_Of_Errors is not zero, report it, and fail
497
498    procedure Scan_Arg (Arg : String);
499    --  Process one command line argument
500
501    function Strip_CR_LF (Text : String) return String;
502    --  Remove characters ASCII.CR and ASCII.LF from a String
503
504    procedure Usage;
505    --  Display the usage
506
507    ------------------
508    -- Add_Archives --
509    ------------------
510
511    procedure Add_Archives (For_Gnatmake : Boolean) is
512       Last_Arg : constant Natural := Last_Argument;
513       --  The position of the last argument before adding the archives. Used to
514       --  reverse the order of the arguments added when processing the
515       --  archives.
516
517       procedure Recursive_Add_Archives (Project : Project_Id);
518       --  Recursive procedure to add the archive of a project file, if any,
519       --  then call itself for the project imported.
520
521       ----------------------------
522       -- Recursive_Add_Archives --
523       ----------------------------
524
525       procedure Recursive_Add_Archives (Project : Project_Id) is
526          Data     : Project_Data;
527          Imported : Project_List;
528          Prj      : Project_Id;
529
530          procedure Add_Archive_Path;
531          --  For a library project or the main project, add the archive
532          --  path to the arguments.
533
534          ----------------------
535          -- Add_Archive_Path --
536          ----------------------
537
538          procedure Add_Archive_Path is
539             Increment : Positive;
540             Prev_Last : Positive;
541
542          begin
543             if Data.Library then
544
545                --  If it is a library project file, nothing to do if gnatmake
546                --  will be invoked, because gnatmake will take care of it, even
547                --  if the library is not an Ada library.
548
549                if not For_Gnatmake then
550                   if Data.Library_Kind = Static then
551                      Add_Argument
552                        (Get_Name_String (Data.Display_Library_Dir) &
553                         Directory_Separator &
554                         "lib" & Get_Name_String (Data.Library_Name) &
555                         '.' & Archive_Ext,
556                         Verbose_Mode);
557
558                   else
559                      --  As we first insert in the reverse order,
560                      --  -L<dir> is put after -l<lib>
561
562                      Add_Argument
563                        ("-l" & Get_Name_String (Data.Library_Name),
564                         Verbose_Mode);
565
566                      Get_Name_String (Data.Display_Library_Dir);
567
568                      Add_Argument
569                        ("-L" & Name_Buffer (1 .. Name_Len),
570                         Verbose_Mode);
571
572                      --  If there is a run path option, prepend this directory
573                      --  to the library path. It is probable that the order of
574                      --  the directories in the path option is not important,
575                      --  but just in case put the directories in the same order
576                      --  as the libraries.
577
578                      if Path_Option /= null then
579
580                         --  If it is not the first directory, make room at the
581                         --  beginning of the table, including for a path
582                         --  separator.
583
584                         if Lib_Path.Last > 0 then
585                            Increment := Name_Len + 1;
586                            Prev_Last := Lib_Path.Last;
587                            Lib_Path.Set_Last (Prev_Last + Increment);
588
589                            for Index in reverse 1 .. Prev_Last loop
590                               Lib_Path.Table (Index + Increment) :=
591                                 Lib_Path.Table (Index);
592                            end loop;
593
594                            Lib_Path.Table (Increment) := Path_Separator;
595
596                         else
597                            --  If it is the first directory, just set
598                            --  Last to the length of the directory.
599
600                            Lib_Path.Set_Last (Name_Len);
601                         end if;
602
603                         --  Put the directory at the beginning of the
604                         --  table.
605
606                         for Index in 1 .. Name_Len loop
607                            Lib_Path.Table (Index) := Name_Buffer (Index);
608                         end loop;
609                      end if;
610                   end if;
611                end if;
612
613             --  For a non-library project, the only archive needed is the one
614             --  for the main project, if there is one.
615
616             elsif Project = Main_Project and then Global_Archive_Exists then
617                Add_Argument
618                  (Get_Name_String (Data.Display_Object_Dir) &
619                   Directory_Separator &
620                   "lib" & Get_Name_String (Data.Display_Name)
621                   & '.' & Archive_Ext,
622                   Verbose_Mode);
623             end if;
624          end Add_Archive_Path;
625
626       begin
627          --  Nothing to do when there is no project specified
628
629          if Project /= No_Project then
630             Data := Project_Tree.Projects.Table (Project);
631
632             --  Nothing to do if the project has already been processed
633
634             if not Data.Seen then
635
636                --  Mark the project as processed, to avoid processing it again
637
638                Project_Tree.Projects.Table (Project).Seen := True;
639
640                Recursive_Add_Archives (Data.Extends);
641
642                Imported := Data.Imported_Projects;
643
644                --  Call itself recursively for all imported projects
645
646                while Imported /= Empty_Project_List loop
647                   Prj := Project_Tree.Project_Lists.Table
648                            (Imported).Project;
649
650                   if Prj /= No_Project then
651                      while Project_Tree.Projects.Table
652                              (Prj).Extended_By /= No_Project
653                      loop
654                         Prj := Project_Tree.Projects.Table
655                                  (Prj).Extended_By;
656                      end loop;
657
658                      Recursive_Add_Archives (Prj);
659                   end if;
660
661                   Imported := Project_Tree.Project_Lists.Table
662                                 (Imported).Next;
663                end loop;
664
665                --  If there is sources of language other than Ada in this
666                --  project, add the path of the archive to Arguments.
667
668                if Project = Main_Project
669                  or else Data.Other_Sources_Present
670                then
671                   Add_Archive_Path;
672                end if;
673             end if;
674          end if;
675       end Recursive_Add_Archives;
676
677    --  Start of processing for Add_Archives
678
679    begin
680       --  First, mark all projects as not processed
681
682       for Project in Project_Table.First ..
683                      Project_Table.Last (Project_Tree.Projects)
684       loop
685          Project_Tree.Projects.Table (Project).Seen := False;
686       end loop;
687
688       --  Take care of the run path option
689
690       if Path_Option = null then
691          Path_Option := MLib.Linker_Library_Path_Option;
692       end if;
693
694       Lib_Path.Set_Last (0);
695
696       --  Add archives in the reverse order
697
698       Recursive_Add_Archives (Main_Project);
699
700       --  And reverse the order
701
702       declare
703          First : Positive;
704          Last  : Natural;
705          Temp  : String_Access;
706
707       begin
708          First := Last_Arg + 1;
709          Last  := Last_Argument;
710          while First < Last loop
711             Temp := Arguments (First);
712             Arguments (First) := Arguments (Last);
713             Arguments (Last)  := Temp;
714             First := First + 1;
715             Last := Last - 1;
716          end loop;
717       end;
718    end Add_Archives;
719
720    ------------------
721    -- Add_Argument --
722    ------------------
723
724    procedure Add_Argument (Arg : String_Access; Display : Boolean) is
725    begin
726       --  Nothing to do if no argument is specified or if argument is empty
727
728       if Arg /= null or else Arg'Length = 0 then
729
730          --  Reallocate arrays if necessary
731
732          if Last_Argument = Arguments'Last then
733             declare
734                New_Arguments : constant Argument_List_Access :=
735                                  new Argument_List
736                                    (1 .. Last_Argument +
737                                            Initial_Argument_Count);
738
739                New_Arguments_Displayed : constant Booleans :=
740                                            new Boolean_Array
741                                              (1 .. Last_Argument +
742                                                      Initial_Argument_Count);
743
744             begin
745                New_Arguments (Arguments'Range) := Arguments.all;
746
747                --  To avoid deallocating the strings, nullify all components
748                --  of Arguments before calling Free.
749
750                Arguments.all := (others => null);
751
752                Free (Arguments);
753                Arguments := New_Arguments;
754
755                New_Arguments_Displayed (Arguments_Displayed'Range) :=
756                  Arguments_Displayed.all;
757                Free (Arguments_Displayed);
758                Arguments_Displayed := New_Arguments_Displayed;
759             end;
760          end if;
761
762          --  Add the argument and its display indication
763
764          Last_Argument := Last_Argument + 1;
765          Arguments (Last_Argument) := Arg;
766          Arguments_Displayed (Last_Argument) := Display;
767       end if;
768    end Add_Argument;
769
770    procedure Add_Argument (Arg : String; Display : Boolean) is
771       Argument : String_Access := null;
772
773    begin
774       --  Nothing to do if argument is empty
775
776       if Arg'Length > 0 then
777
778          --  Check if the argument is already in the Cache_Args table.
779          --  If it is already there, reuse the allocated value.
780
781          for Index in 1 .. Cache_Args.Last loop
782             if Cache_Args.Table (Index).all = Arg then
783                Argument := Cache_Args.Table (Index);
784                exit;
785             end if;
786          end loop;
787
788          --  If the argument is not in the cache, create a new entry in the
789          --  cache.
790
791          if Argument = null then
792             Argument := new String'(Arg);
793             Cache_Args.Increment_Last;
794             Cache_Args.Table (Cache_Args.Last) := Argument;
795          end if;
796
797          --  And add the argument
798
799          Add_Argument (Argument, Display);
800       end if;
801    end Add_Argument;
802
803    -------------------
804    -- Add_Arguments --
805    -------------------
806
807    procedure Add_Arguments (Args : Argument_List; Display : Boolean) is
808    begin
809       --  Reallocate the arrays, if necessary
810
811       if Last_Argument + Args'Length > Arguments'Last then
812          declare
813             New_Arguments : constant Argument_List_Access :=
814                               new Argument_List
815                                     (1 .. Last_Argument + Args'Length +
816                                           Initial_Argument_Count);
817
818             New_Arguments_Displayed : constant Booleans :=
819                                         new Boolean_Array
820                                               (1 .. Last_Argument +
821                                                     Args'Length +
822                                                     Initial_Argument_Count);
823
824          begin
825             New_Arguments (1 .. Last_Argument) :=
826               Arguments (1 .. Last_Argument);
827
828             --  To avoid deallocating the strings, nullify all components
829             --  of Arguments before calling Free.
830
831             Arguments.all := (others => null);
832             Free (Arguments);
833
834             Arguments := New_Arguments;
835             New_Arguments_Displayed (1 .. Last_Argument) :=
836               Arguments_Displayed (1 .. Last_Argument);
837             Free (Arguments_Displayed);
838             Arguments_Displayed := New_Arguments_Displayed;
839          end;
840       end if;
841
842       --  Add the new arguments and the display indications
843
844       Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
845       Arguments_Displayed (Last_Argument + 1 .. Last_Argument + Args'Length) :=
846         (others => Display);
847       Last_Argument := Last_Argument + Args'Length;
848    end Add_Arguments;
849
850    ----------------
851    -- Add_Option --
852    ----------------
853
854    procedure Add_Option (Arg : String) is
855       Option : constant String_Access := new String'(Arg);
856
857    begin
858       case Current_Processor is
859          when None =>
860             null;
861
862          when Linker =>
863
864             --  Add option to the linker table
865
866             Linker_Options.Increment_Last;
867             Linker_Options.Table (Linker_Options.Last) := Option;
868
869          when Compiler =>
870
871             --  Add option to the compiler option table, depending on the
872             --  value of Current_Language.
873
874             Comp_Opts.Increment_Last (Options (Current_Language));
875             Options (Current_Language).Table
876               (Comp_Opts.Last (Options (Current_Language))) := Option;
877
878       end case;
879    end Add_Option;
880
881    -------------------
882    -- Add_Source_Id --
883    -------------------
884
885    procedure Add_Source_Id (Project : Project_Id; Id : Other_Source_Id) is
886    begin
887       --  Reallocate the array, if necessary
888
889       if Last_Source = Source_Indexes'Last then
890          declare
891             New_Indexes : constant Source_Indexes_Ref :=
892                             new Source_Index_Array
893                               (1 .. Source_Indexes'Last +
894                                       Initial_Source_Index_Count);
895          begin
896             New_Indexes (Source_Indexes'Range) := Source_Indexes.all;
897             Free (Source_Indexes);
898             Source_Indexes := New_Indexes;
899          end;
900       end if;
901
902       Last_Source := Last_Source + 1;
903       Source_Indexes (Last_Source) := (Project, Id, False);
904    end Add_Source_Id;
905
906    ----------------------------
907    -- Add_Search_Directories --
908    ----------------------------
909
910    procedure Add_Search_Directories
911      (Data     : Project_Data;
912       Language : First_Language_Indexes)
913    is
914    begin
915       --  If a GNU compiler is used, set the CPATH environment variable,
916       --  if it does not already has the correct value.
917
918       if Compiler_Is_Gcc (Language) then
919          if Current_Include_Paths (Language) /= Data.Include_Path then
920             Current_Include_Paths (Language) := Data.Include_Path;
921             Setenv (CPATH, Data.Include_Path.all);
922          end if;
923
924       else
925          Add_Arguments (Data.Imported_Directories_Switches.all, Verbose_Mode);
926       end if;
927    end Add_Search_Directories;
928
929    ------------------
930    -- Add_Switches --
931    ------------------
932
933    procedure Add_Switches
934      (Data      : Project_Data;
935       Proc      : Processor;
936       Language  : Language_Index;
937       File_Name : File_Name_Type)
938    is
939       Switches       : Variable_Value;
940       --  The switches, if any, for the file/language
941
942       Pkg            : Package_Id;
943       --  The id of the package where to look for the switches
944
945       Defaults       : Array_Element_Id;
946       --  The Default_Switches associative array
947
948       Switches_Array : Array_Element_Id;
949       --  The Switches associative array
950
951       Element_Id     : String_List_Id;
952       Element        : String_Element;
953
954    begin
955       --  First, choose the proper package
956
957       case Proc is
958          when None =>
959             raise Program_Error;
960
961          when Linker =>
962             Pkg := Value_Of (Name_Linker, Data.Decl.Packages, Project_Tree);
963
964          when Compiler =>
965             Pkg := Value_Of (Name_Compiler, Data.Decl.Packages, Project_Tree);
966       end case;
967
968       if Pkg /= No_Package then
969
970          --  Get the Switches ("file name"), if they exist
971
972          Switches_Array := Prj.Util.Value_Of
973            (Name      => Name_Switches,
974             In_Arrays => Project_Tree.Packages.Table
975                           (Pkg).Decl.Arrays,
976             In_Tree   => Project_Tree);
977
978          Switches :=
979            Prj.Util.Value_Of
980              (Index     => Name_Id (File_Name),
981               Src_Index => 0,
982               In_Array  => Switches_Array,
983               In_Tree   => Project_Tree);
984
985          --  Otherwise, get the Default_Switches ("language"), if they exist
986
987          if Switches = Nil_Variable_Value then
988             Defaults := Prj.Util.Value_Of
989               (Name      => Name_Default_Switches,
990                In_Arrays => Project_Tree.Packages.Table
991                               (Pkg).Decl.Arrays,
992                In_Tree   => Project_Tree);
993             Switches := Prj.Util.Value_Of
994               (Index     => Language_Names.Table (Language),
995                Src_Index => 0,
996                In_Array  => Defaults,
997                In_Tree   => Project_Tree);
998          end if;
999
1000          --  If there are switches, add them to Arguments
1001
1002          if Switches /= Nil_Variable_Value then
1003             Element_Id := Switches.Values;
1004             while Element_Id /= Nil_String loop
1005                Element := Project_Tree.String_Elements.Table
1006                             (Element_Id);
1007
1008                if Element.Value /= No_Name then
1009                   Get_Name_String (Element.Value);
1010
1011                   if not Quiet_Output then
1012
1013                      --  When not in quiet output (no -q), check that the
1014                      --  switch is not the concatenation of several valid
1015                      --  switches, such as "-g -v". If it is, issue a warning.
1016
1017                      Check (Option => Name_Buffer (1 .. Name_Len));
1018                   end if;
1019
1020                   Add_Argument (Name_Buffer (1 .. Name_Len), True);
1021                end if;
1022
1023                Element_Id := Element.Next;
1024             end loop;
1025          end if;
1026       end if;
1027    end Add_Switches;
1028
1029    --------------------------
1030    -- Build_Global_Archive --
1031    --------------------------
1032
1033    procedure Build_Global_Archive is
1034       Data      : Project_Data := Project_Tree.Projects.Table (Main_Project);
1035       Source_Id : Other_Source_Id;
1036       S_Id      : Other_Source_Id;
1037       Source    : Other_Source;
1038       Success   : Boolean;
1039
1040       Archive_Name : constant String :=
1041                        "lib"
1042                          & Get_Name_String (Data.Display_Name)
1043                          & '.'
1044                          & Archive_Ext;
1045       --  The name of the archive file for this project
1046
1047       Archive_Dep_Name : constant String :=
1048                            "lib"
1049                              & Get_Name_String (Data.Display_Name)
1050                              & ".deps";
1051       --  The name of the archive dependency file for this project
1052
1053       Need_To_Rebuild : Boolean := Need_To_Rebuild_Global_Archive;
1054       --  When True, archive will be rebuilt
1055
1056       File                : Prj.Util.Text_File;
1057       Object_Path         : Path_Name_Type;
1058       Time_Stamp          : Time_Stamp_Type;
1059       Saved_Last_Argument : Natural;
1060       First_Object        : Natural;
1061       Discard             : Boolean;
1062
1063    begin
1064       Check_Archive_Builder;
1065
1066       if Project_Of_Current_Object_Directory /= Main_Project then
1067          Project_Of_Current_Object_Directory := Main_Project;
1068          Change_Dir (Get_Name_String (Data.Object_Directory));
1069
1070          if Verbose_Mode then
1071             Write_Str  ("Changing to object directory of """);
1072             Write_Name (Data.Display_Name);
1073             Write_Str  (""": """);
1074             Write_Name (Data.Display_Object_Dir);
1075             Write_Line ("""");
1076          end if;
1077       end if;
1078
1079       if not Need_To_Rebuild then
1080          if Verbose_Mode then
1081             Write_Str  ("   Checking ");
1082             Write_Line (Archive_Name);
1083          end if;
1084
1085          --  If the archive does not exist, of course it needs to be built
1086
1087          if not Is_Regular_File (Archive_Name) then
1088             Need_To_Rebuild := True;
1089
1090             if Verbose_Mode then
1091                Write_Line ("      -> archive does not exist");
1092             end if;
1093
1094          --  Archive does exist
1095
1096          else
1097             --  Check the archive dependency file
1098
1099             Open (File, Archive_Dep_Name);
1100
1101             --  If the archive dependency file does not exist, we need to
1102             --  rebuild the archive and to create its dependency file.
1103
1104             if not Is_Valid (File) then
1105                Need_To_Rebuild := True;
1106
1107                if Verbose_Mode then
1108                   Write_Str  ("      -> archive dependency file ");
1109                   Write_Str  (Archive_Dep_Name);
1110                   Write_Line (" does not exist");
1111                end if;
1112
1113             else
1114                --  Put all sources of language other than Ada in Source_Indexes
1115
1116                declare
1117                   Local_Data : Project_Data;
1118
1119                begin
1120                   Last_Source := 0;
1121
1122                   for Proj in Project_Table.First ..
1123                     Project_Table.Last (Project_Tree.Projects)
1124                   loop
1125                      Local_Data := Project_Tree.Projects.Table (Proj);
1126
1127                      if not Local_Data.Library then
1128                         Source_Id := Local_Data.First_Other_Source;
1129                         while Source_Id /= No_Other_Source loop
1130                            Add_Source_Id (Proj, Source_Id);
1131                            Source_Id := Project_Tree.Other_Sources.Table
1132                              (Source_Id).Next;
1133                         end loop;
1134                      end if;
1135                   end loop;
1136                end;
1137
1138                --  Read the dependency file, line by line
1139
1140                while not End_Of_File (File) loop
1141                   Get_Line (File, Name_Buffer, Name_Len);
1142
1143                   --  First line is the path of the object file
1144
1145                   Object_Path := Name_Find;
1146                   Source_Id := No_Other_Source;
1147
1148                   --  Check if this object file is for a source of this project
1149
1150                   for S in 1 .. Last_Source loop
1151                      S_Id := Source_Indexes (S).Id;
1152                      Source := Project_Tree.Other_Sources.Table (S_Id);
1153
1154                      if (not Source_Indexes (S).Found)
1155                        and then Source.Object_Path = Object_Path
1156                      then
1157                         --  We have found the object file: get the source data,
1158                         --  and mark it as found.
1159
1160                         Source_Id := S_Id;
1161                         Source_Indexes (S).Found := True;
1162                         exit;
1163                      end if;
1164                   end loop;
1165
1166                   --  If it is not for a source of this project, then the
1167                   --  archive needs to be rebuilt.
1168
1169                   if Source_Id = No_Other_Source then
1170                      Need_To_Rebuild := True;
1171                      if Verbose_Mode then
1172                         Write_Str  ("      -> ");
1173                         Write_Str  (Get_Name_String (Object_Path));
1174                         Write_Line (" is not an object of any project");
1175                      end if;
1176
1177                      exit;
1178                   end if;
1179
1180                   --  The second line is the time stamp of the object file. If
1181                   --  there is no next line, then the dependency file is
1182                   --  truncated, and the archive need to be rebuilt.
1183
1184                   if End_Of_File (File) then
1185                      Need_To_Rebuild := True;
1186
1187                      if Verbose_Mode then
1188                         Write_Str  ("      -> archive dependency file ");
1189                         Write_Line (" is truncated");
1190                      end if;
1191
1192                      exit;
1193                   end if;
1194
1195                   Get_Line (File, Name_Buffer, Name_Len);
1196
1197                   --  If the line has the wrong number of characters, then
1198                   --  the dependency file is incorrectly formatted, and the
1199                   --  archive needs to be rebuilt.
1200
1201                   if Name_Len /= Time_Stamp_Length then
1202                      Need_To_Rebuild := True;
1203
1204                      if Verbose_Mode then
1205                         Write_Str  ("      -> archive dependency file ");
1206                         Write_Line (" is incorrectly formatted (time stamp)");
1207                      end if;
1208
1209                      exit;
1210                   end if;
1211
1212                   Time_Stamp := Time_Stamp_Type (Name_Buffer (1 .. Name_Len));
1213
1214                   --  If the time stamp in the dependency file is different
1215                   --  from the time stamp of the object file, then the archive
1216                   --  needs to be rebuilt.
1217
1218                   if Time_Stamp /= Source.Object_TS then
1219                      Need_To_Rebuild := True;
1220
1221                      if Verbose_Mode then
1222                         Write_Str  ("      -> time stamp of ");
1223                         Write_Str  (Get_Name_String (Object_Path));
1224                         Write_Str  (" is incorrect in the archive");
1225                         Write_Line (" dependency file");
1226                      end if;
1227
1228                      exit;
1229                   end if;
1230                end loop;
1231
1232                Close (File);
1233             end if;
1234          end if;
1235       end if;
1236
1237       if not Need_To_Rebuild then
1238          if Verbose_Mode then
1239             Write_Line  ("      -> up to date");
1240          end if;
1241
1242          --  No need to create a global archive, if there is no object
1243          --  file to put into.
1244
1245          Global_Archive_Exists := Last_Source /= 0;
1246
1247       --  Archive needs to be rebuilt
1248
1249       else
1250          --  If archive already exists, first delete it
1251
1252          --  Comment needed on why we discard result???
1253
1254          if Is_Regular_File (Archive_Name) then
1255             Delete_File (Archive_Name, Discard);
1256          end if;
1257
1258          Last_Argument := 0;
1259
1260          --  Start with the options found in MLib.Tgt (usually just "rc")
1261
1262          Add_Arguments (Archive_Builder_Options.all, True);
1263
1264          --  Followed by the archive name
1265
1266          Add_Argument (Archive_Name, True);
1267
1268          First_Object := Last_Argument;
1269
1270          --  Followed by all the object files of the non library projects
1271
1272          for Proj in Project_Table.First ..
1273                      Project_Table.Last (Project_Tree.Projects)
1274          loop
1275             Data := Project_Tree.Projects.Table (Proj);
1276
1277             if not Data.Library then
1278                Source_Id := Data.First_Other_Source;
1279                while Source_Id /= No_Other_Source loop
1280                   Source :=
1281                     Project_Tree.Other_Sources.Table (Source_Id);
1282
1283                   --  Only include object file name that have not been
1284                   --  overriden in extending projects.
1285
1286                   if Is_Included_In_Global_Archive
1287                        (Source.Object_Name, Proj)
1288                   then
1289                      Add_Argument
1290                        (Get_Name_String (Source.Object_Path),
1291                         Verbose_Mode or (First_Object = Last_Argument));
1292                   end if;
1293
1294                   Source_Id := Source.Next;
1295                end loop;
1296             end if;
1297          end loop;
1298
1299          --  No need to create a global archive, if there is no object
1300          --  file to put into.
1301
1302          Global_Archive_Exists := Last_Argument > First_Object;
1303
1304          if Global_Archive_Exists then
1305
1306             --  If the archive is built, then linking will need to occur
1307             --  unconditionally.
1308
1309             Need_To_Relink := True;
1310
1311             --  Spawn the archive builder (ar)
1312
1313             Saved_Last_Argument := Last_Argument;
1314             Last_Argument := First_Object + Max_In_Archives;
1315             loop
1316                if Last_Argument > Saved_Last_Argument then
1317                   Last_Argument := Saved_Last_Argument;
1318                end if;
1319
1320                Display_Command
1321                  (Archive_Builder,
1322                   Archive_Builder_Path,
1323                   Ellipse => True);
1324
1325                Spawn
1326                  (Archive_Builder_Path.all,
1327                   Arguments (1 .. Last_Argument),
1328                   Success);
1329
1330                exit when not Success
1331                  or else Last_Argument = Saved_Last_Argument;
1332
1333                Arguments (1) := r;
1334                Arguments (3 .. Saved_Last_Argument - Last_Argument + 2) :=
1335                  Arguments (Last_Argument + 1 .. Saved_Last_Argument);
1336                Saved_Last_Argument := Saved_Last_Argument - Last_Argument + 2;
1337             end loop;
1338
1339             --  If the archive was built, run the archive indexer (ranlib)
1340             --  if there is one.
1341
1342             if Success then
1343
1344                if Archive_Indexer_Path /= null then
1345                   Last_Argument := 0;
1346                   Add_Argument (Archive_Name, True);
1347
1348                   Display_Command (Archive_Indexer, Archive_Indexer_Path);
1349
1350                   Spawn
1351                     (Archive_Indexer_Path.all, Arguments (1 .. 1), Success);
1352
1353                   if not Success then
1354
1355                      --  Running ranlib failed, delete the dependency file,
1356                      --  if it exists.
1357
1358                      if Is_Regular_File (Archive_Dep_Name) then
1359                         Delete_File (Archive_Dep_Name, Success);
1360                      end if;
1361
1362                      --  And report the error
1363
1364                      Report_Error
1365                        ("running" & Archive_Indexer & " for project """,
1366                         Get_Name_String (Data.Display_Name),
1367                         """ failed");
1368                      return;
1369                   end if;
1370                end if;
1371
1372                --  The archive was correctly built, create its dependency file
1373
1374                Create_Global_Archive_Dependency_File (Archive_Dep_Name);
1375
1376             --  Building the archive failed, delete dependency file if one
1377             --  exists.
1378
1379             else
1380                if Is_Regular_File (Archive_Dep_Name) then
1381                   Delete_File (Archive_Dep_Name, Success);
1382                end if;
1383
1384                --  And report the error
1385
1386                Report_Error
1387                  ("building archive for project """,
1388                   Get_Name_String (Data.Display_Name),
1389                   """ failed");
1390             end if;
1391          end if;
1392       end if;
1393    end Build_Global_Archive;
1394
1395    -------------------
1396    -- Build_Library --
1397    -------------------
1398
1399    procedure Build_Library (Project : Project_Id; Unconditionally : Boolean) is
1400       Data      : constant Project_Data :=
1401                     Project_Tree.Projects.Table (Project);
1402       Source_Id : Other_Source_Id;
1403       Source    : Other_Source;
1404
1405       Archive_Name : constant String :=
1406                        "lib" & Get_Name_String (Data.Library_Name)
1407                        & '.' & Archive_Ext;
1408       --  The name of the archive file for this project
1409
1410       Archive_Dep_Name : constant String :=
1411                            "lib" & Get_Name_String (Data.Library_Name)
1412                            & ".deps";
1413       --  The name of the archive dependency file for this project
1414
1415       Need_To_Rebuild : Boolean := Unconditionally;
1416       --  When True, archive will be rebuilt
1417
1418       File : Prj.Util.Text_File;
1419
1420       Object_Name : File_Name_Type;
1421       Time_Stamp  : Time_Stamp_Type;
1422       Driver_Name : Name_Id := No_Name;
1423
1424       Lib_Opts    : Argument_List_Access := No_Argument'Access;
1425
1426    begin
1427       --  Nothing to do if the project is externally built
1428
1429       if Data.Externally_Built then
1430          return;
1431       end if;
1432
1433       Check_Archive_Builder;
1434
1435       --  If Unconditionally is False, check if the archive need to be built
1436
1437       if not Need_To_Rebuild then
1438          if Verbose_Mode then
1439             Write_Str  ("   Checking ");
1440             Write_Line (Archive_Name);
1441          end if;
1442
1443          --  If the archive does not exist, of course it needs to be built
1444
1445          if not Is_Regular_File (Archive_Name) then
1446             Need_To_Rebuild := True;
1447
1448             if Verbose_Mode then
1449                Write_Line ("      -> archive does not exist");
1450             end if;
1451
1452          --  Archive does exist
1453
1454          else
1455             --  Check the archive dependency file
1456
1457             Open (File, Archive_Dep_Name);
1458
1459             --  If the archive dependency file does not exist, we need to
1460             --  rebuild the archive and to create its dependency file.
1461
1462             if not Is_Valid (File) then
1463                Need_To_Rebuild := True;
1464
1465                if Verbose_Mode then
1466                   Write_Str  ("      -> archive dependency file ");
1467                   Write_Str  (Archive_Dep_Name);
1468                   Write_Line (" does not exist");
1469                end if;
1470
1471             else
1472                --  Put all sources of language other than Ada in Source_Indexes
1473
1474                Last_Source := 0;
1475
1476                Source_Id := Data.First_Other_Source;
1477                while Source_Id /= No_Other_Source loop
1478                   Add_Source_Id (Project, Source_Id);
1479                   Source_Id :=
1480                     Project_Tree.Other_Sources.Table (Source_Id).Next;
1481                end loop;
1482
1483                --  Read the dependency file, line by line
1484
1485                while not End_Of_File (File) loop
1486                   Get_Line (File, Name_Buffer, Name_Len);
1487
1488                   --  First line is the name of an object file
1489
1490                   Object_Name := Name_Find;
1491                   Source_Id := No_Other_Source;
1492
1493                   --  Check if this object file is for a source of this project
1494
1495                   for S in 1 .. Last_Source loop
1496                      if (not Source_Indexes (S).Found)
1497                        and then
1498                          Project_Tree.Other_Sources.Table
1499                            (Source_Indexes (S).Id).Object_Name = Object_Name
1500                      then
1501                         --  We have found the object file: get the source
1502                         --  data, and mark it as found.
1503
1504                         Source_Id := Source_Indexes (S).Id;
1505                         Source := Project_Tree.Other_Sources.Table
1506                                     (Source_Id);
1507                         Source_Indexes (S).Found := True;
1508                         exit;
1509                      end if;
1510                   end loop;
1511
1512                   --  If it is not for a source of this project, then the
1513                   --  archive needs to be rebuilt.
1514
1515                   if Source_Id = No_Other_Source then
1516                      Need_To_Rebuild := True;
1517
1518                      if Verbose_Mode then
1519                         Write_Str  ("      -> ");
1520                         Write_Str  (Get_Name_String (Object_Name));
1521                         Write_Line (" is not an object of the project");
1522                      end if;
1523
1524                      exit;
1525                   end if;
1526
1527                   --  The second line is the time stamp of the object file.
1528                   --  If there is no next line, then the dependency file is
1529                   --  truncated, and the archive need to be rebuilt.
1530
1531                   if End_Of_File (File) then
1532                      Need_To_Rebuild := True;
1533
1534                      if Verbose_Mode then
1535                         Write_Str  ("      -> archive dependency file ");
1536                         Write_Line (" is truncated");
1537                      end if;
1538
1539                      exit;
1540                   end if;
1541
1542                   Get_Line (File, Name_Buffer, Name_Len);
1543
1544                   --  If the line has the wrong number of character, then
1545                   --  the dependency file is incorrectly formatted, and the
1546                   --  archive needs to be rebuilt.
1547
1548                   if Name_Len /= Time_Stamp_Length then
1549                      Need_To_Rebuild := True;
1550
1551                      if Verbose_Mode then
1552                         Write_Str  ("      -> archive dependency file ");
1553                         Write_Line (" is incorrectly formatted (time stamp)");
1554                      end if;
1555
1556                      exit;
1557                   end if;
1558
1559                   Time_Stamp := Time_Stamp_Type (Name_Buffer (1 .. Name_Len));
1560
1561                   --  If the time stamp in the dependency file is different
1562                   --  from the time stamp of the object file, then the archive
1563                   --  needs to be rebuilt.
1564
1565                   if Time_Stamp /= Source.Object_TS then
1566                      Need_To_Rebuild := True;
1567
1568                      if Verbose_Mode then
1569                         Write_Str  ("      -> time stamp of ");
1570                         Write_Str  (Get_Name_String (Object_Name));
1571                         Write_Str  (" is incorrect in the archive");
1572                         Write_Line (" dependency file");
1573                      end if;
1574
1575                      exit;
1576                   end if;
1577                end loop;
1578
1579                Close (File);
1580
1581                if not Need_To_Rebuild then
1582
1583                   --  Now, check if all object files of the project have been
1584                   --  accounted for. If any of them is not in the dependency
1585                   --  file, the archive needs to be rebuilt.
1586
1587                   for Index in 1 .. Last_Source loop
1588                      if not Source_Indexes (Index).Found then
1589                         Need_To_Rebuild := True;
1590
1591                         if Verbose_Mode then
1592                            Source_Id := Source_Indexes (Index).Id;
1593                            Source := Project_Tree.Other_Sources.Table
1594                                        (Source_Id);
1595                            Write_Str  ("      -> ");
1596                            Write_Str  (Get_Name_String (Source.Object_Name));
1597                            Write_Str  (" is not in the archive ");
1598                            Write_Line ("dependency file");
1599                         end if;
1600
1601                         exit;
1602                      end if;
1603                   end loop;
1604                end if;
1605
1606                if (not Need_To_Rebuild) and Verbose_Mode then
1607                   Write_Line ("      -> up to date");
1608                end if;
1609             end if;
1610          end if;
1611       end if;
1612
1613       --  Build the library if necessary
1614
1615       if Need_To_Rebuild then
1616
1617          --  If a library is built, then linking will need to occur
1618          --  unconditionally.
1619
1620          Need_To_Relink := True;
1621
1622          Last_Argument := 0;
1623
1624          --  If there are sources in Ada, then gnatmake will build the library,
1625          --  so nothing to do.
1626
1627          if not Data.Langs (Ada_Language_Index) then
1628
1629             --  Get all the object files of the project
1630
1631             Source_Id := Data.First_Other_Source;
1632             while Source_Id /= No_Other_Source loop
1633                Source := Project_Tree.Other_Sources.Table (Source_Id);
1634                Add_Argument
1635                  (Get_Name_String (Source.Object_Name), Verbose_Mode);
1636                Source_Id := Source.Next;
1637             end loop;
1638
1639             --  If it is a library, it need to be built it the same way Ada
1640             --  libraries are built.
1641
1642             if Data.Library_Kind = Static then
1643                MLib.Build_Library
1644                  (Ofiles      => Arguments (1 .. Last_Argument),
1645                   Output_File => Get_Name_String (Data.Library_Name),
1646                   Output_Dir  => Get_Name_String (Data.Display_Library_Dir));
1647
1648             else
1649                --  Link with g++ if C++ is one of the languages, otherwise
1650                --  building the library may fail with unresolved symbols.
1651
1652                if C_Plus_Plus_Is_Used then
1653                   if Compiler_Names (C_Plus_Plus_Language_Index) = null then
1654                      Get_Compiler (C_Plus_Plus_Language_Index);
1655                   end if;
1656
1657                   if Compiler_Is_Gcc (C_Plus_Plus_Language_Index) then
1658                      Name_Len := 0;
1659                      Add_Str_To_Name_Buffer
1660                        (Compiler_Names (C_Plus_Plus_Language_Index).all);
1661                      Driver_Name := Name_Find;
1662                   end if;
1663                end if;
1664
1665                --  If Library_Options is specified, add these options
1666
1667                declare
1668                   Library_Options : constant Variable_Value :=
1669                                       Value_Of
1670                                         (Name_Library_Options,
1671                                          Data.Decl.Attributes,
1672                                          Project_Tree);
1673
1674                begin
1675                   if not Library_Options.Default then
1676                      declare
1677                         Current : String_List_Id;
1678                         Element : String_Element;
1679
1680                      begin
1681                         Current := Library_Options.Values;
1682                         while Current /= Nil_String loop
1683                            Element :=
1684                              Project_Tree.String_Elements.Table (Current);
1685                            Get_Name_String (Element.Value);
1686
1687                            if Name_Len /= 0 then
1688                               Library_Opts.Increment_Last;
1689                               Library_Opts.Table (Library_Opts.Last) :=
1690                                 new String'(Name_Buffer (1 .. Name_Len));
1691                            end if;
1692
1693                            Current := Element.Next;
1694                         end loop;
1695                      end;
1696                   end if;
1697
1698                   Lib_Opts :=
1699                     new Argument_List'(Argument_List
1700                        (Library_Opts.Table (1 .. Library_Opts.Last)));
1701                end;
1702
1703                MLib.Tgt.Build_Dynamic_Library
1704                  (Ofiles       => Arguments (1 .. Last_Argument),
1705                   Options      => Lib_Opts.all,
1706                   Interfaces   => No_Argument,
1707                   Lib_Filename => Get_Name_String (Data.Library_Name),
1708                   Lib_Dir      => Get_Name_String (Data.Library_Dir),
1709                   Symbol_Data  => No_Symbols,
1710                   Driver_Name  => Driver_Name,
1711                   Lib_Version  => "",
1712                   Auto_Init    => False);
1713             end if;
1714          end if;
1715
1716          --  Create fake empty archive, so we can check its time stamp later
1717
1718          declare
1719             Archive : Ada.Text_IO.File_Type;
1720          begin
1721             Create (Archive, Out_File, Archive_Name);
1722             Close (Archive);
1723          end;
1724
1725          Create_Archive_Dependency_File
1726            (Archive_Dep_Name, Data.First_Other_Source);
1727       end if;
1728    end Build_Library;
1729
1730    -----------
1731    -- Check --
1732    -----------
1733
1734    procedure Check (Option : String) is
1735       First : Positive := Option'First;
1736       Last  : Natural;
1737
1738    begin
1739       for Index in Option'First + 1 .. Option'Last - 1 loop
1740          if Option (Index) = ' ' and then Option (Index + 1) = '-' then
1741             Write_Str ("warning: switch """);
1742             Write_Str (Option);
1743             Write_Str (""" is suspicious; consider using ");
1744
1745             Last := First;
1746             while Last <= Option'Last loop
1747                if Option (Last) = ' ' then
1748                   if First /= Option'First then
1749                      Write_Str (", ");
1750                   end if;
1751
1752                   Write_Char ('"');
1753                   Write_Str (Option (First .. Last - 1));
1754                   Write_Char ('"');
1755
1756                   while Last <= Option'Last and then Option (Last) = ' ' loop
1757                      Last := Last + 1;
1758                   end loop;
1759
1760                   First := Last;
1761
1762                else
1763                   if Last = Option'Last then
1764                      if First /= Option'First then
1765                         Write_Str (", ");
1766                      end if;
1767
1768                      Write_Char ('"');
1769                      Write_Str (Option (First .. Last));
1770                      Write_Char ('"');
1771                   end if;
1772
1773                   Last := Last + 1;
1774                end if;
1775             end loop;
1776
1777             Write_Line (" instead");
1778             exit;
1779          end if;
1780       end loop;
1781    end Check;
1782
1783    ---------------------------
1784    -- Check_Archive_Builder --
1785    ---------------------------
1786
1787    procedure Check_Archive_Builder is
1788    begin
1789       --  First, make sure that the archive builder (ar) is on the path
1790
1791       if Archive_Builder_Path = null then
1792          Archive_Builder_Path := Locate_Exec_On_Path (Archive_Builder);
1793
1794          if Archive_Builder_Path = null then
1795             Osint.Fail
1796               ("unable to locate archive builder """,
1797                Archive_Builder,
1798                """");
1799          end if;
1800
1801          --  If there is an archive indexer (ranlib), try to locate it on the
1802          --  path. Don't fail if it is not found.
1803
1804          if Archive_Indexer /= "" then
1805             Archive_Indexer_Path := Locate_Exec_On_Path (Archive_Indexer);
1806          end if;
1807       end if;
1808    end Check_Archive_Builder;
1809
1810    ------------------------------
1811    -- Check_Compilation_Needed --
1812    ------------------------------
1813
1814    procedure Check_Compilation_Needed
1815      (Source          : Other_Source;
1816       Need_To_Compile : out Boolean)
1817    is
1818       Source_Name   : constant String := Get_Name_String (Source.File_Name);
1819       Source_Path   : constant String := Get_Name_String (Source.Path_Name);
1820       Object_Name   : constant String := Get_Name_String (Source.Object_Name);
1821       C_Object_Name : String := Object_Name;
1822       Dep_Name      : constant String := Get_Name_String (Source.Dep_Name);
1823       C_Source_Path : String := Source_Path;
1824
1825       Source_In_Dependencies : Boolean := False;
1826       --  Set True if source was found in dependency file of its object file
1827
1828       Dep_File : Prj.Util.Text_File;
1829       Start    : Natural;
1830       Finish   : Natural;
1831
1832       Looping : Boolean := False;
1833       --  Set to True at the end of the first Big_Loop
1834
1835    begin
1836       Canonical_Case_File_Name (C_Source_Path);
1837       Canonical_Case_File_Name (C_Object_Name);
1838
1839       --  Assume the worst, so that statement "return;" may be used if there
1840       --  is any problem.
1841
1842       Need_To_Compile := True;
1843
1844       if Verbose_Mode then
1845          Write_Str  ("   Checking ");
1846          Write_Str  (Source_Name);
1847          Write_Line (" ... ");
1848       end if;
1849
1850       --  If object file does not exist, of course source need to be compiled
1851
1852       if Source.Object_TS = Empty_Time_Stamp then
1853          if Verbose_Mode then
1854             Write_Str  ("      -> object file ");
1855             Write_Str  (Object_Name);
1856             Write_Line (" does not exist");
1857          end if;
1858
1859          return;
1860       end if;
1861
1862       --  If the object file has been created before the last modification
1863       --  of the source, the source need to be recompiled.
1864
1865       if Source.Object_TS < Source.Source_TS then
1866          if Verbose_Mode then
1867             Write_Str  ("      -> object file ");
1868             Write_Str  (Object_Name);
1869             Write_Line (" has time stamp earlier than source");
1870          end if;
1871
1872          return;
1873       end if;
1874
1875       --  If there is no dependency file, then the source needs to be
1876       --  recompiled and the dependency file need to be created.
1877
1878       if Source.Dep_TS = Empty_Time_Stamp then
1879          if Verbose_Mode then
1880             Write_Str  ("      -> dependency file ");
1881             Write_Str  (Dep_Name);
1882             Write_Line (" does not exist");
1883          end if;
1884
1885          return;
1886       end if;
1887
1888       --  The source needs to be recompiled if the source has been modified
1889       --  after the dependency file has been created.
1890
1891       if Source.Dep_TS < Source.Source_TS then
1892          if Verbose_Mode then
1893             Write_Str  ("      -> dependency file ");
1894             Write_Str  (Dep_Name);
1895             Write_Line (" has time stamp earlier than source");
1896          end if;
1897
1898          return;
1899       end if;
1900
1901       --  Look for all dependencies
1902
1903       Open (Dep_File, Dep_Name);
1904
1905       --  If dependency file cannot be open, we need to recompile the source
1906
1907       if not Is_Valid (Dep_File) then
1908          if Verbose_Mode then
1909             Write_Str  ("      -> could not open dependency file ");
1910             Write_Line (Dep_Name);
1911          end if;
1912
1913          return;
1914       end if;
1915
1916       --  Loop Big_Loop is executed several times only when the dependency file
1917       --  contains several times
1918       --     <object file>: <source1> ...
1919       --  When there is only one of such occurence, Big_Loop is exited
1920       --  successfully at the beginning of the second loop.
1921
1922       Big_Loop :
1923       loop
1924          declare
1925             End_Of_File_Reached : Boolean := False;
1926
1927          begin
1928             loop
1929                if End_Of_File (Dep_File) then
1930                   End_Of_File_Reached := True;
1931                   exit;
1932                end if;
1933
1934                Get_Line (Dep_File, Name_Buffer, Name_Len);
1935
1936                exit when Name_Len > 0 and then Name_Buffer (1) /= '#';
1937             end loop;
1938
1939             --  If dependency file contains only empty lines or comments, then
1940             --  dependencies are unknown, and the source needs to be
1941             --  recompiled.
1942
1943             if End_Of_File_Reached then
1944                --  If we have reached the end of file after the first loop,
1945                --  there is nothing else to do.
1946
1947                exit Big_Loop when Looping;
1948
1949                if Verbose_Mode then
1950                   Write_Str  ("      -> dependency file ");
1951                   Write_Str  (Dep_Name);
1952                   Write_Line (" is empty");
1953                end if;
1954
1955                Close (Dep_File);
1956                return;
1957             end if;
1958          end;
1959
1960          Start  := 1;
1961          Finish := Index (Name_Buffer (1 .. Name_Len), ": ");
1962
1963          if Finish /= 0 then
1964             Canonical_Case_File_Name (Name_Buffer (1 .. Finish - 1));
1965          end if;
1966
1967          --  First line must start with name of object file, followed by colon
1968
1969          if Finish = 0 or else
1970             Name_Buffer (1 .. Finish - 1) /= C_Object_Name
1971          then
1972             if Verbose_Mode then
1973                Write_Str  ("      -> dependency file ");
1974                Write_Str  (Dep_Name);
1975                Write_Line (" has wrong format");
1976             end if;
1977
1978             Close (Dep_File);
1979             return;
1980
1981          else
1982             Start := Finish + 2;
1983
1984             --  Process each line
1985
1986             Line_Loop : loop
1987                declare
1988                   Line : String  := Name_Buffer (1 .. Name_Len);
1989                   Last : Natural := Name_Len;
1990
1991                begin
1992                   Name_Loop : loop
1993
1994                      --  Find the beginning of the next source path name
1995
1996                      while Start < Last and then Line (Start) = ' ' loop
1997                         Start := Start + 1;
1998                      end loop;
1999
2000                      --  Go to next line when there is a continuation character
2001                      --  \ at the end of the line.
2002
2003                      exit Name_Loop when Start = Last
2004                        and then Line (Start) = '\';
2005
2006                      --  We should not be at the end of the line, without
2007                      --  a continuation character \.
2008
2009                      if Start = Last then
2010                         if Verbose_Mode then
2011                            Write_Str  ("      -> dependency file ");
2012                            Write_Str  (Dep_Name);
2013                            Write_Line (" has wrong format");
2014                         end if;
2015
2016                         Close (Dep_File);
2017                         return;
2018                      end if;
2019
2020                      --  Look for the end of the source path name
2021
2022                      Finish := Start;
2023                      while Finish < Last loop
2024                         if Line (Finish) = '\' then
2025
2026                            --  On Windows, a '\' is part of the path name,
2027                            --  except when it is followed by another '\' or by
2028                            --  a space. On other platforms, when we are getting
2029                            --  a '\' that is not the last character of the
2030                            --  line, the next character is part of the path
2031                            --  name, even if it is a space.
2032
2033                            if On_Windows
2034                              and then Line (Finish + 1) /= '\'
2035                              and then Line (Finish + 1) /= ' '
2036                            then
2037                               Finish := Finish + 1;
2038
2039                            else
2040                               Line (Finish .. Last - 1) :=
2041                                 Line (Finish + 1 .. Last);
2042                               Last := Last - 1;
2043                            end if;
2044
2045                         else
2046                            --  A space that is not preceded by '\' indicates
2047                            --  the end of the path name.
2048
2049                            exit when Line (Finish + 1) = ' ';
2050
2051                            Finish := Finish + 1;
2052                         end if;
2053                      end loop;
2054
2055                      --  Check this source
2056
2057                      declare
2058                         Src_Name : constant String :=
2059                                      Normalize_Pathname
2060                                        (Name           =>
2061                                                        Line (Start .. Finish),
2062                                         Resolve_Links  => False,
2063                                         Case_Sensitive => False);
2064                         Src_TS   : Time_Stamp_Type;
2065
2066                      begin
2067                         --  If it is original source, set
2068                         --  Source_In_Dependencies.
2069
2070                         if Src_Name = C_Source_Path then
2071                            Source_In_Dependencies := True;
2072                         end if;
2073
2074                         Name_Len := 0;
2075                         Add_Str_To_Name_Buffer (Src_Name);
2076                         Src_TS := File_Stamp (File_Name_Type'(Name_Find));
2077
2078                         --  If the source does not exist, we need to recompile
2079
2080                         if Src_TS = Empty_Time_Stamp then
2081                            if Verbose_Mode then
2082                               Write_Str  ("      -> source ");
2083                               Write_Str  (Src_Name);
2084                               Write_Line (" does not exist");
2085                            end if;
2086
2087                            Close (Dep_File);
2088                            return;
2089
2090                            --  If the source has been modified after the object
2091                            --  file, we need to recompile.
2092
2093                         elsif Src_TS > Source.Object_TS then
2094                            if Verbose_Mode then
2095                               Write_Str  ("      -> source ");
2096                               Write_Str  (Src_Name);
2097                               Write_Line
2098                                 (" has time stamp later than object file");
2099                            end if;
2100
2101                            Close (Dep_File);
2102                            return;
2103                         end if;
2104                      end;
2105
2106                      --  If the source path name ends the line, we are done
2107
2108                      exit Line_Loop when Finish = Last;
2109
2110                      --  Go get the next source on the line
2111
2112                      Start := Finish + 1;
2113                   end loop Name_Loop;
2114                end;
2115
2116                --  If we are here, we had a continuation character \ at the end
2117                --  of the line, so we continue with the next line.
2118
2119                Get_Line (Dep_File, Name_Buffer, Name_Len);
2120                Start := 1;
2121             end loop Line_Loop;
2122          end if;
2123
2124          --  Set Looping at the end of the first loop
2125          Looping := True;
2126       end loop Big_Loop;
2127
2128       Close (Dep_File);
2129
2130       --  If the original sources were not in the dependency file, then we
2131       --  need to recompile. It may mean that we are using a different source
2132       --  (different variant) for this object file.
2133
2134       if not Source_In_Dependencies then
2135          if Verbose_Mode then
2136             Write_Str  ("      -> source ");
2137             Write_Str  (Source_Path);
2138             Write_Line (" is not in the dependencies");
2139          end if;
2140
2141          return;
2142       end if;
2143
2144       --  If we are here, then everything is OK, no need to recompile
2145
2146       if Verbose_Mode then
2147          Write_Line ("      -> up to date");
2148       end if;
2149
2150       Need_To_Compile := False;
2151    end Check_Compilation_Needed;
2152
2153    ---------------------------
2154    -- Check_For_C_Plus_Plus --
2155    ---------------------------
2156
2157    procedure Check_For_C_Plus_Plus is
2158    begin
2159       C_Plus_Plus_Is_Used := False;
2160
2161       for Project in Project_Table.First ..
2162                      Project_Table.Last (Project_Tree.Projects)
2163       loop
2164          if
2165            Project_Tree.Projects.Table (Project).Langs
2166                                            (C_Plus_Plus_Language_Index)
2167          then
2168             C_Plus_Plus_Is_Used := True;
2169             exit;
2170          end if;
2171       end loop;
2172    end Check_For_C_Plus_Plus;
2173
2174    -------------
2175    -- Compile --
2176    -------------
2177
2178    procedure Compile
2179      (Source_Id    : Other_Source_Id;
2180       Data         : Project_Data;
2181       Local_Errors : in out Boolean)
2182    is
2183       Source  : Other_Source :=
2184                   Project_Tree.Other_Sources.Table (Source_Id);
2185       Success : Boolean;
2186       CPATH   : String_Access := null;
2187
2188    begin
2189       --  If the compiler is not known yet, get its path name
2190
2191       if Compiler_Names (Source.Language) = null then
2192          Get_Compiler (Source.Language);
2193       end if;
2194
2195       --  For non GCC compilers, get the dependency file, first calling the
2196       --  compiler with the switch -M.
2197
2198       if not Compiler_Is_Gcc (Source.Language) then
2199          Last_Argument := 0;
2200
2201          --  Add the source name, preceded by -M
2202
2203          Add_Argument (Dash_M, True);
2204          Add_Argument (Get_Name_String (Source.Path_Name), True);
2205
2206          --  Add the compiling switches for this source found in
2207          --  package Compiler of the project file, if they exist.
2208
2209          Add_Switches
2210            (Data, Compiler, Source.Language, Source.File_Name);
2211
2212          --  Add the compiling switches for the language specified
2213          --  on the command line, if any.
2214
2215          for
2216            J in 1 .. Comp_Opts.Last (Options (Source.Language))
2217          loop
2218             Add_Argument (Options (Source.Language).Table (J), True);
2219          end loop;
2220
2221          --  Finally, add imported directory switches for this project file
2222
2223          Add_Search_Directories (Data, Source.Language);
2224
2225          --  And invoke the compiler using GNAT.Expect
2226
2227          Display_Command
2228            (Compiler_Names (Source.Language).all,
2229             Compiler_Paths (Source.Language));
2230
2231          begin
2232             Non_Blocking_Spawn
2233               (FD,
2234                Compiler_Paths (Source.Language).all,
2235                Arguments (1 .. Last_Argument),
2236                Buffer_Size => 0,
2237                Err_To_Out => True);
2238
2239             declare
2240                Dep_File : Ada.Text_IO.File_Type;
2241                Result   : Expect_Match;
2242                Status   : Integer;
2243
2244             begin
2245                --  Create the dependency file
2246
2247                Create (Dep_File, Out_File, Get_Name_String (Source.Dep_Name));
2248
2249                loop
2250                   Expect (FD, Result, Line_Matcher);
2251
2252                   exit when Result = Expect_Timeout;
2253
2254                   declare
2255                      S : constant String := Strip_CR_LF (Expect_Out (FD));
2256
2257                   begin
2258                      --  Each line of the output is put in the dependency
2259                      --  file, including errors. If there are errors, the
2260                      --  syntax of the dependency file will be incorrect and
2261                      --  recompilation will occur automatically the next time
2262                      --  the dependencies are checked.
2263
2264                      Put_Line (Dep_File, S);
2265                   end;
2266                end loop;
2267
2268                --  If we are here, it means we had a timeout, so the
2269                --  dependency file may be incomplete. It is safer to
2270                --  delete it, otherwise the dependencies may be wrong.
2271
2272                Close (FD, Status);
2273                Close (Dep_File);
2274                Delete_File (Get_Name_String (Source.Dep_Name), Success);
2275
2276             exception
2277                when Process_Died =>
2278
2279                   --  This is the normal outcome. Just close the file
2280
2281                   Close (FD, Status);
2282                   Close (Dep_File);
2283
2284                when others =>
2285
2286                   --  Something wrong happened. It is safer to delete the
2287                   --  dependency file, otherwise the dependencies may be wrong.
2288
2289                   Close (FD, Status);
2290
2291                   if Is_Open (Dep_File) then
2292                      Close (Dep_File);
2293                   end if;
2294
2295                   Delete_File (Get_Name_String (Source.Dep_Name), Success);
2296             end;
2297
2298          exception
2299                --  If we cannot spawn the compiler, then the dependencies are
2300                --  not updated. It is safer then to delete the dependency file,
2301                --  otherwise the dependencies may be wrong.
2302
2303             when Invalid_Process =>
2304                Delete_File (Get_Name_String (Source.Dep_Name), Success);
2305          end;
2306       end if;
2307
2308       Last_Argument := 0;
2309
2310       --  For GCC compilers, make sure the language is always specified to
2311       --  to the GCC driver, in case the extension is not recognized by the
2312       --  GCC driver as a source of the language.
2313
2314       if Compiler_Is_Gcc (Source.Language) then
2315          Add_Argument (Dash_x, Verbose_Mode);
2316          Add_Argument
2317            (Get_Name_String (Language_Names.Table (Source.Language)),
2318             Verbose_Mode);
2319       end if;
2320
2321       Add_Argument (Dash_c, True);
2322
2323       --  Add the compiling switches for this source found in package Compiler
2324       --  of the project file, if they exist.
2325
2326       Add_Switches
2327         (Data, Compiler, Source.Language, Source.File_Name);
2328
2329       --  Specify the source to be compiled
2330
2331       Add_Argument (Get_Name_String (Source.Path_Name), True);
2332
2333       --  If non static library project, compile with the PIC option if there
2334       --  is one (when there is no PIC option, MLib.Tgt.PIC_Option returns an
2335       --  empty string, and Add_Argument with an empty string has no effect).
2336
2337       if Data.Library and then Data.Library_Kind /= Static then
2338          Add_Argument (PIC_Option, True);
2339       end if;
2340
2341       --  Indicate the name of the object
2342
2343       Add_Argument (Dash_o, True);
2344       Add_Argument (Get_Name_String (Source.Object_Name), True);
2345
2346       --  When compiler is GCC, use the magic switch that creates the
2347       --  dependency file in the correct format.
2348
2349       if Compiler_Is_Gcc (Source.Language) then
2350          Add_Argument
2351            ("-Wp,-MD," & Get_Name_String (Source.Dep_Name),
2352             Verbose_Mode);
2353       end if;
2354
2355       --  Add the compiling switches for the language specified on the command
2356       --  line, if any.
2357
2358       for J in 1 .. Comp_Opts.Last (Options (Source.Language)) loop
2359          Add_Argument (Options (Source.Language).Table (J), True);
2360       end loop;
2361
2362       --  Finally, add the imported directory switches for this project file
2363       --  (or, for gcc compilers, set up the CPATH env var if needed).
2364
2365       Add_Search_Directories (Data, Source.Language);
2366
2367       --  Set CPATH, if compiler is GCC
2368
2369       if Compiler_Is_Gcc (Source.Language) then
2370          CPATH := Current_Include_Paths (Source.Language);
2371       end if;
2372
2373       --  And invoke the compiler
2374
2375       Display_Command
2376         (Name  => Compiler_Names (Source.Language).all,
2377          Path  => Compiler_Paths (Source.Language),
2378          CPATH => CPATH);
2379
2380       Spawn
2381         (Compiler_Paths (Source.Language).all,
2382          Arguments (1 .. Last_Argument),
2383          Success);
2384
2385       --  Case of successful compilation
2386
2387       if Success then
2388
2389          --  Update the time stamp of the object file
2390
2391          Source.Object_TS := File_Stamp (Source.Object_Name);
2392
2393          --  Do some sanity checks
2394
2395          if Source.Object_TS = Empty_Time_Stamp then
2396             Local_Errors := True;
2397             Report_Error
2398               ("object file ",
2399                Get_Name_String (Source.Object_Name),
2400                " has not been created");
2401
2402          elsif Source.Object_TS < Source.Source_TS then
2403             Local_Errors := True;
2404             Report_Error
2405               ("object file ",
2406                Get_Name_String (Source.Object_Name),
2407                " has not been modified");
2408
2409          else
2410             --  Everything looks fine, update the Other_Sources table
2411
2412             Project_Tree.Other_Sources.Table (Source_Id) := Source;
2413          end if;
2414
2415       --  Compilation failed
2416
2417       else
2418          Local_Errors := True;
2419          Report_Error
2420            ("compilation of ",
2421             Get_Name_String (Source.Path_Name),
2422             " failed");
2423       end if;
2424    end Compile;
2425
2426    --------------------------------
2427    -- Compile_Individual_Sources --
2428    --------------------------------
2429
2430    procedure Compile_Individual_Sources is
2431       Data         : Project_Data :=
2432                        Project_Tree.Projects.Table (Main_Project);
2433       Source_Id    : Other_Source_Id;
2434       Source       : Other_Source;
2435       Source_Name  : File_Name_Type;
2436       Project_Name : String := Get_Name_String (Data.Name);
2437       Dummy        : Boolean := False;
2438
2439       Ada_Is_A_Language : constant Boolean :=
2440                             Data.Langs (Ada_Language_Index);
2441
2442    begin
2443       Ada_Mains.Init;
2444       To_Mixed (Project_Name);
2445       Compile_Only := True;
2446
2447       Get_Imported_Directories (Main_Project, Data);
2448       Project_Tree.Projects.Table (Main_Project) := Data;
2449
2450       --  Compilation will occur in the object directory
2451
2452       if Project_Of_Current_Object_Directory /= Main_Project then
2453          Project_Of_Current_Object_Directory := Main_Project;
2454          Change_Dir (Get_Name_String (Data.Object_Directory));
2455
2456          if Verbose_Mode then
2457             Write_Str  ("Changing to object directory of """);
2458             Write_Name (Data.Name);
2459             Write_Str  (""": """);
2460             Write_Name (Data.Display_Object_Dir);
2461             Write_Line ("""");
2462          end if;
2463       end if;
2464
2465       if not Data.Other_Sources_Present then
2466          if Ada_Is_A_Language then
2467             Mains.Reset;
2468
2469             loop
2470                declare
2471                   Main : constant String := Mains.Next_Main;
2472                begin
2473                   exit when Main'Length = 0;
2474                   Ada_Mains.Increment_Last;
2475                   Ada_Mains.Table (Ada_Mains.Last) := new String'(Main);
2476                end;
2477             end loop;
2478
2479          else
2480             Osint.Fail ("project ", Project_Name, " contains no source");
2481          end if;
2482
2483       else
2484          Mains.Reset;
2485
2486          loop
2487             declare
2488                Main : constant String := Mains.Next_Main;
2489             begin
2490                Name_Len := Main'Length;
2491                exit when Name_Len = 0;
2492                Name_Buffer (1 .. Name_Len) := Main;
2493                Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2494                Source_Name := Name_Find;
2495
2496                if not Sources_Compiled.Get (Source_Name) then
2497                   Sources_Compiled.Set (Source_Name, True);
2498
2499                   Source_Id := Data.First_Other_Source;
2500                   while Source_Id /= No_Other_Source loop
2501                      Source := Project_Tree.Other_Sources.Table (Source_Id);
2502                      exit when Source.File_Name = Source_Name;
2503                      Source_Id := Source.Next;
2504                   end loop;
2505
2506                   if Source_Id = No_Other_Source then
2507                      if Ada_Is_A_Language then
2508                         Ada_Mains.Increment_Last;
2509                         Ada_Mains.Table (Ada_Mains.Last) := new String'(Main);
2510
2511                      else
2512                         Report_Error
2513                           (Main,
2514                            " is not a valid source of project ",
2515                            Project_Name);
2516                      end if;
2517
2518                   else
2519                      Compile (Source_Id, Data, Dummy);
2520                   end if;
2521                end if;
2522             end;
2523          end loop;
2524       end if;
2525
2526       if Ada_Mains.Last > 0 then
2527
2528          --  Invoke gnatmake for all Ada sources
2529
2530          Last_Argument := 0;
2531          Add_Argument (Dash_u, True);
2532
2533          for Index in 1 .. Ada_Mains.Last loop
2534             Add_Argument (Ada_Mains.Table (Index), True);
2535          end loop;
2536
2537          Compile_Link_With_Gnatmake (Mains_Specified => False);
2538       end if;
2539    end Compile_Individual_Sources;
2540
2541    --------------------------------
2542    -- Compile_Link_With_Gnatmake --
2543    --------------------------------
2544
2545    procedure Compile_Link_With_Gnatmake (Mains_Specified : Boolean) is
2546       Data    : constant Project_Data :=
2547                   Project_Tree.Projects.Table (Main_Project);
2548       Success : Boolean;
2549
2550    begin
2551       --  Array Arguments may already contain some arguments, so we don't
2552       --  set Last_Argument to 0.
2553
2554       --  Get the gnatmake to invoke
2555
2556       Get_Compiler (Ada_Language_Index);
2557
2558       --  Specify the project file
2559
2560       Add_Argument (Dash_P, True);
2561       Add_Argument (Get_Name_String (Data.Display_Path_Name), True);
2562
2563       --  Add the saved switches, if any
2564
2565       for Index in 1 .. Saved_Switches.Last loop
2566          Add_Argument (Saved_Switches.Table (Index), True);
2567       end loop;
2568
2569       --  If Mains_Specified is True, find the mains in package Mains
2570
2571       if Mains_Specified then
2572          Mains.Reset;
2573
2574          loop
2575             declare
2576                Main : constant String := Mains.Next_Main;
2577             begin
2578                exit when Main'Length = 0;
2579                Add_Argument (Main, True);
2580             end;
2581          end loop;
2582       end if;
2583
2584       --  Specify output file name, if any was specified on the command line
2585
2586       if Output_File_Name /= null then
2587          Add_Argument (Dash_o, True);
2588          Add_Argument (Output_File_Name, True);
2589       end if;
2590
2591       --  Transmit some switches to gnatmake
2592
2593       --  -c
2594
2595       if Compile_Only then
2596          Add_Argument (Dash_c, True);
2597       end if;
2598
2599       --  -d
2600
2601       if Display_Compilation_Progress then
2602          Add_Argument (Dash_d, True);
2603       end if;
2604
2605       --  -k
2606
2607       if Keep_Going then
2608          Add_Argument (Dash_k, True);
2609       end if;
2610
2611       --  -f
2612
2613       if Force_Compilations then
2614          Add_Argument (Dash_f, True);
2615       end if;
2616
2617       --  -v
2618
2619       if Verbose_Mode then
2620          Add_Argument (Dash_v, True);
2621       end if;
2622
2623       --  -q
2624
2625       if Quiet_Output then
2626          Add_Argument (Dash_q, True);
2627       end if;
2628
2629       --  -vP1 and -vP2
2630
2631       case Current_Verbosity is
2632          when Default =>
2633             null;
2634
2635          when Medium =>
2636             Add_Argument (Dash_vP1, True);
2637
2638          when High =>
2639             Add_Argument (Dash_vP2, True);
2640       end case;
2641
2642       --  If there are compiling options for Ada, transmit them to gnatmake
2643
2644       if Comp_Opts.Last (Options (Ada_Language_Index)) /= 0 then
2645          Add_Argument (Dash_cargs, True);
2646
2647          for Arg in 1 .. Comp_Opts.Last (Options (Ada_Language_Index)) loop
2648             Add_Argument (Options (Ada_Language_Index).Table (Arg), True);
2649          end loop;
2650       end if;
2651
2652       if not Compile_Only then
2653
2654          --  Linking options
2655
2656          if Linker_Options.Last /= 0 then
2657             Add_Argument (Dash_largs, True);
2658          else
2659             Add_Argument (Dash_largs, Verbose_Mode);
2660          end if;
2661
2662          --  Add the archives
2663
2664          Add_Archives (For_Gnatmake => True);
2665
2666          --  If there are linking options from the command line,
2667          --  transmit them to gnatmake.
2668
2669          for Arg in 1 .. Linker_Options.Last loop
2670             Add_Argument (Linker_Options.Table (Arg), True);
2671          end loop;
2672       end if;
2673
2674       --  And invoke gnatmake
2675
2676       Display_Command
2677         (Compiler_Names (Ada_Language_Index).all,
2678          Compiler_Paths (Ada_Language_Index));
2679
2680       Spawn
2681         (Compiler_Paths (Ada_Language_Index).all,
2682          Arguments (1 .. Last_Argument),
2683          Success);
2684
2685       --  Report an error if call to gnatmake failed
2686
2687       if not Success then
2688          Report_Error
2689            ("invocation of ",
2690             Compiler_Names (Ada_Language_Index).all,
2691             " failed");
2692       end if;
2693    end Compile_Link_With_Gnatmake;
2694
2695    ---------------------
2696    -- Compile_Sources --
2697    ---------------------
2698
2699    procedure Compile_Sources is
2700       Data         : Project_Data;
2701       Source_Id    : Other_Source_Id;
2702       Source       : Other_Source;
2703
2704       Local_Errors : Boolean := False;
2705       --  Set to True when there is a compilation error. Used only when
2706       --  Keep_Going is True, to inhibit the building of the archive.
2707
2708       Need_To_Compile : Boolean;
2709       --  Set to True when a source needs to be compiled/recompiled
2710
2711       Need_To_Rebuild_Archive : Boolean := Force_Compilations;
2712       --  True when the archive needs to be built/rebuilt unconditionally
2713
2714       Total_Number_Of_Sources : Int := 0;
2715
2716       Current_Source_Number : Int := 0;
2717
2718    begin
2719       --  First, get the number of sources
2720
2721       for Project in Project_Table.First ..
2722                      Project_Table.Last (Project_Tree.Projects)
2723       loop
2724          Data := Project_Tree.Projects.Table (Project);
2725
2726          if not Data.Virtual and then Data.Other_Sources_Present then
2727             Source_Id := Data.First_Other_Source;
2728             while Source_Id /= No_Other_Source loop
2729                Source := Project_Tree.Other_Sources.Table (Source_Id);
2730                Total_Number_Of_Sources := Total_Number_Of_Sources + 1;
2731                Source_Id := Source.Next;
2732             end loop;
2733          end if;
2734       end loop;
2735
2736       --  Loop through project files
2737
2738       for Project in Project_Table.First ..
2739                      Project_Table.Last (Project_Tree.Projects)
2740       loop
2741          Local_Errors := False;
2742          Data := Project_Tree.Projects.Table (Project);
2743
2744          --  Nothing to do when no sources of language other than Ada
2745
2746          if (not Data.Virtual) and then Data.Other_Sources_Present then
2747
2748             --  If the imported directory switches are unknown, compute them
2749
2750             if not Data.Include_Data_Set then
2751                Get_Imported_Directories (Project, Data);
2752                Data.Include_Data_Set := True;
2753                Project_Tree.Projects.Table (Project) := Data;
2754             end if;
2755
2756             Need_To_Rebuild_Archive := Force_Compilations;
2757
2758             --  Compilation will occur in the object directory
2759
2760             if Project_Of_Current_Object_Directory /= Project then
2761                Project_Of_Current_Object_Directory := Project;
2762                Change_Dir (Get_Name_String (Data.Object_Directory));
2763
2764                if Verbose_Mode then
2765                   Write_Str  ("Changing to object directory of """);
2766                   Write_Name (Data.Display_Name);
2767                   Write_Str  (""": """);
2768                   Write_Name (Data.Display_Object_Dir);
2769                   Write_Line ("""");
2770                end if;
2771             end if;
2772
2773             --  Process each source one by one
2774
2775             Source_Id := Data.First_Other_Source;
2776             while Source_Id /= No_Other_Source loop
2777                Source := Project_Tree.Other_Sources.Table (Source_Id);
2778                Current_Source_Number := Current_Source_Number + 1;
2779                Need_To_Compile := Force_Compilations;
2780
2781                --  Check if compilation is needed
2782
2783                if not Need_To_Compile then
2784                   Check_Compilation_Needed (Source, Need_To_Compile);
2785                end if;
2786
2787                --  Proceed, if compilation is needed
2788
2789                if Need_To_Compile then
2790
2791                   --  If a source is compiled/recompiled, of course the
2792                   --  archive will need to be built/rebuilt.
2793
2794                   Need_To_Rebuild_Archive := True;
2795                   Compile (Source_Id, Data, Local_Errors);
2796                end if;
2797
2798                if Display_Compilation_Progress then
2799                   Write_Str ("completed ");
2800                   Write_Int (Current_Source_Number);
2801                   Write_Str (" out of ");
2802                   Write_Int (Total_Number_Of_Sources);
2803                   Write_Str (" (");
2804                   Write_Int
2805                     ((Current_Source_Number * 100) / Total_Number_Of_Sources);
2806                   Write_Str ("%)...");
2807                   Write_Eol;
2808                end if;
2809
2810                --  Next source, if any
2811
2812                Source_Id := Source.Next;
2813             end loop;
2814
2815             if Need_To_Rebuild_Archive and then (not Data.Library) then
2816                Need_To_Rebuild_Global_Archive := True;
2817             end if;
2818
2819             --  If there was no compilation error and -c was not used,
2820             --  build / rebuild the archive if necessary.
2821
2822             if not Local_Errors
2823               and then Data.Library
2824               and then not Data.Langs (Ada_Language_Index)
2825               and then not Compile_Only
2826             then
2827                Build_Library (Project, Need_To_Rebuild_Archive);
2828             end if;
2829          end if;
2830       end loop;
2831    end Compile_Sources;
2832
2833    ---------------
2834    -- Copyright --
2835    ---------------
2836
2837    procedure Copyright is
2838    begin
2839       --  Only output the Copyright notice once
2840
2841       if not Copyright_Output then
2842          Copyright_Output := True;
2843          Write_Eol;
2844          Write_Str ("GPRMAKE ");
2845          Write_Str (Gnatvsn.Gnat_Version_String);
2846          Write_Str (" Copyright 2004-");
2847          Write_Str (Gnatvsn.Current_Year);
2848          Write_Str (" Free Software Foundation, Inc.");
2849          Write_Eol;
2850       end if;
2851    end Copyright;
2852
2853    ------------------------------------
2854    -- Create_Archive_Dependency_File --
2855    ------------------------------------
2856
2857    procedure Create_Archive_Dependency_File
2858      (Name         : String;
2859       First_Source : Other_Source_Id)
2860    is
2861       Source_Id : Other_Source_Id;
2862       Source    : Other_Source;
2863       Dep_File  : Ada.Text_IO.File_Type;
2864
2865    begin
2866       --  Create the file in Append mode, to avoid automatic insertion of
2867       --  an end of line if file is empty.
2868
2869       Create (Dep_File, Append_File, Name);
2870
2871       Source_Id := First_Source;
2872       while Source_Id /= No_Other_Source loop
2873          Source := Project_Tree.Other_Sources.Table (Source_Id);
2874          Put_Line (Dep_File, Get_Name_String (Source.Object_Name));
2875          Put_Line (Dep_File, String (Source.Object_TS));
2876          Source_Id := Source.Next;
2877       end loop;
2878
2879       Close (Dep_File);
2880
2881    exception
2882       when others =>
2883          if Is_Open (Dep_File) then
2884             Close (Dep_File);
2885          end if;
2886    end Create_Archive_Dependency_File;
2887
2888    -------------------------------------------
2889    -- Create_Global_Archive_Dependency_File --
2890    -------------------------------------------
2891
2892    procedure Create_Global_Archive_Dependency_File (Name : String) is
2893       Source_Id : Other_Source_Id;
2894       Source    : Other_Source;
2895       Dep_File  : Ada.Text_IO.File_Type;
2896
2897    begin
2898       --  Create the file in Append mode, to avoid automatic insertion of
2899       --  an end of line if file is empty.
2900
2901       Create (Dep_File, Append_File, Name);
2902
2903       --  Get all the object files of non-Ada sources in non-library projects
2904
2905       for Project in Project_Table.First ..
2906                      Project_Table.Last (Project_Tree.Projects)
2907       loop
2908          if not Project_Tree.Projects.Table (Project).Library then
2909             Source_Id :=
2910               Project_Tree.Projects.Table (Project).First_Other_Source;
2911             while Source_Id /= No_Other_Source loop
2912                Source := Project_Tree.Other_Sources.Table (Source_Id);
2913
2914                --  Put only those object files that are in the global archive
2915
2916                if Is_Included_In_Global_Archive
2917                     (Source.Object_Name, Project)
2918                then
2919                   Put_Line (Dep_File, Get_Name_String (Source.Object_Path));
2920                   Put_Line (Dep_File, String (Source.Object_TS));
2921                end if;
2922
2923                Source_Id := Source.Next;
2924             end loop;
2925          end if;
2926       end loop;
2927
2928       Close (Dep_File);
2929
2930    exception
2931       when others =>
2932          if Is_Open (Dep_File) then
2933             Close (Dep_File);
2934          end if;
2935    end Create_Global_Archive_Dependency_File;
2936
2937    ---------------------
2938    -- Display_Command --
2939    ---------------------
2940
2941    procedure Display_Command
2942      (Name    : String;
2943       Path    : String_Access;
2944       CPATH   : String_Access := null;
2945       Ellipse : Boolean := False)
2946    is
2947       Display_Ellipse : Boolean := Ellipse;
2948
2949    begin
2950       --  Only display the command in Verbose Mode (-v) or when
2951       --  not in Quiet Output (no -q).
2952
2953       if Verbose_Mode or (not Quiet_Output) then
2954
2955          --  In Verbose Mode output the full path of the spawned process
2956
2957          if Verbose_Mode then
2958             if CPATH /= null then
2959                Write_Str  ("CPATH = ");
2960                Write_Line (CPATH.all);
2961             end if;
2962
2963             Write_Str (Path.all);
2964
2965          else
2966             Write_Str (Name);
2967          end if;
2968
2969          --  Display only the arguments for which the display flag is set
2970          --  (in Verbose Mode, the display flag is set for all arguments)
2971
2972          for Arg in 1 .. Last_Argument loop
2973             if Arguments_Displayed (Arg) then
2974                Write_Char (' ');
2975                Write_Str (Arguments (Arg).all);
2976
2977             elsif Display_Ellipse then
2978                Write_Str (" ...");
2979                Display_Ellipse := False;
2980             end if;
2981          end loop;
2982
2983          Write_Eol;
2984       end if;
2985    end Display_Command;
2986
2987    ------------------
2988    -- Get_Compiler --
2989    ------------------
2990
2991    procedure Get_Compiler (For_Language : First_Language_Indexes) is
2992       Data : constant Project_Data :=
2993                Project_Tree.Projects.Table (Main_Project);
2994
2995       Ide : constant Package_Id :=
2996         Value_Of
2997           (Name_Ide,
2998            In_Packages => Data.Decl.Packages,
2999            In_Tree     => Project_Tree);
3000       --  The id of the package IDE in the project file
3001
3002       Compiler : constant Variable_Value :=
3003         Value_Of
3004           (Name                    => Language_Names.Table (For_Language),
3005            Index                   => 0,
3006            Attribute_Or_Array_Name => Name_Compiler_Command,
3007            In_Package              => Ide,
3008            In_Tree                 => Project_Tree);
3009       --  The value of Compiler_Command ("language") in package IDE, if defined
3010
3011    begin
3012       --  No need to do it again if the compiler is known for this language
3013
3014       if Compiler_Names (For_Language) = null then
3015
3016          --  If compiler command is not defined for this language in package
3017          --  IDE, use the default compiler for this language.
3018
3019          if Compiler = Nil_Variable_Value then
3020             if For_Language in Default_Compiler_Names'Range then
3021                Compiler_Names (For_Language) :=
3022                  Default_Compiler_Names (For_Language);
3023
3024             else
3025                Osint.Fail
3026                  ("unknow compiler name for language """,
3027                   Get_Name_String (Language_Names.Table (For_Language)),
3028                   """");
3029             end if;
3030
3031          else
3032             Compiler_Names (For_Language) :=
3033               new String'(Get_Name_String (Compiler.Value));
3034          end if;
3035
3036          --  Check we have a GCC compiler (name ends with "gcc" or "g++")
3037
3038          declare
3039             Comp_Name : constant String := Compiler_Names (For_Language).all;
3040             Last3     : String (1 .. 3);
3041          begin
3042             if Comp_Name'Length >= 3 then
3043                Last3 := Comp_Name (Comp_Name'Last - 2 .. Comp_Name'Last);
3044                Compiler_Is_Gcc (For_Language) :=
3045                  (Last3 = "gcc") or (Last3 = "g++");
3046             else
3047                Compiler_Is_Gcc (For_Language) := False;
3048             end if;
3049          end;
3050
3051          --  Locate the compiler on the path
3052
3053          Compiler_Paths (For_Language) :=
3054            Locate_Exec_On_Path (Compiler_Names (For_Language).all);
3055
3056          --  Fail if compiler cannot be found
3057
3058          if Compiler_Paths (For_Language) = null then
3059             if For_Language = Ada_Language_Index then
3060                Osint.Fail
3061                  ("unable to locate """,
3062                   Compiler_Names (For_Language).all,
3063                   """");
3064
3065             else
3066                Osint.Fail
3067                  ("unable to locate " &
3068                   Get_Name_String (Language_Names.Table (For_Language)),
3069                   " compiler """, Compiler_Names (For_Language).all & '"');
3070             end if;
3071          end if;
3072       end if;
3073    end Get_Compiler;
3074
3075    ------------------------------
3076    -- Get_Imported_Directories --
3077    ------------------------------
3078
3079    procedure Get_Imported_Directories
3080      (Project : Project_Id;
3081       Data    : in out Project_Data)
3082    is
3083       Imported_Projects : Project_List := Data.Imported_Projects;
3084
3085       Path_Length : Natural := 0;
3086       Position    : Natural := 0;
3087
3088       procedure Add (Source_Dirs : String_List_Id);
3089       --  Add a list of source directories
3090
3091       procedure Recursive_Get_Dirs (Prj : Project_Id);
3092       --  Recursive procedure to get the source directories of this project
3093       --  file and of the project files it imports, in the correct order.
3094
3095       ---------
3096       -- Add --
3097       ---------
3098
3099       procedure Add (Source_Dirs : String_List_Id) is
3100          Element_Id : String_List_Id;
3101          Element    : String_Element;
3102          Add_Arg    : Boolean := True;
3103
3104       begin
3105          --  Add each source directory path name, preceded by "-I" to Arguments
3106
3107          Element_Id := Source_Dirs;
3108          while Element_Id /= Nil_String loop
3109             Element := Project_Tree.String_Elements.Table (Element_Id);
3110
3111             if Element.Value /= No_Name then
3112                Get_Name_String (Element.Display_Value);
3113
3114                if Name_Len > 0 then
3115
3116                   --  Remove a trailing directory separator: this may cause
3117                   --  problems on Windows.
3118
3119                   if Name_Len > 1
3120                     and then Name_Buffer (Name_Len) = Directory_Separator
3121                   then
3122                      Name_Len := Name_Len - 1;
3123                   end if;
3124
3125                   declare
3126                      Arg : constant String :=
3127                              "-I" & Name_Buffer (1 .. Name_Len);
3128                   begin
3129                      --  Check if directory is already in the list. If it is,
3130                      --  no need to put it there again.
3131
3132                      Add_Arg := True;
3133
3134                      for Index in 1 .. Last_Argument loop
3135                         if Arguments (Index).all = Arg then
3136                            Add_Arg := False;
3137                            exit;
3138                         end if;
3139                      end loop;
3140
3141                      if Add_Arg then
3142                         if Path_Length /= 0 then
3143                            Path_Length := Path_Length + 1;
3144                         end if;
3145
3146                         Path_Length := Path_Length + Name_Len;
3147
3148                         Add_Argument (Arg, True);
3149                      end if;
3150                   end;
3151                end if;
3152             end if;
3153
3154             Element_Id := Element.Next;
3155          end loop;
3156       end Add;
3157
3158       ------------------------
3159       -- Recursive_Get_Dirs --
3160       ------------------------
3161
3162       procedure Recursive_Get_Dirs (Prj : Project_Id) is
3163          Data     : Project_Data;
3164          Imported : Project_List;
3165
3166       begin
3167          --  Nothing to do if project is undefined
3168
3169          if Prj /= No_Project then
3170             Data := Project_Tree.Projects.Table (Prj);
3171
3172             --  Nothing to do if project has already been processed
3173
3174             if not Data.Seen then
3175
3176                --  Mark the project as processed, to avoid multiple processing
3177                --  of the same project.
3178
3179                Project_Tree.Projects.Table (Prj).Seen := True;
3180
3181                --  Add the source directories of this project
3182
3183                if not Data.Virtual then
3184                   Add (Data.Source_Dirs);
3185                end if;
3186
3187                Recursive_Get_Dirs (Data.Extends);
3188
3189                --  Call itself for all imported projects, if any
3190
3191                Imported := Data.Imported_Projects;
3192                while Imported /= Empty_Project_List loop
3193                   Recursive_Get_Dirs
3194                     (Project_Tree.Project_Lists.Table (Imported).Project);
3195                   Imported :=
3196                     Project_Tree.Project_Lists.Table (Imported).Next;
3197                end loop;
3198             end if;
3199          end if;
3200       end Recursive_Get_Dirs;
3201
3202    --  Start of processing for Get_Imported_Directories
3203
3204    begin
3205       --  First, mark all project as not processed
3206
3207       for J in Project_Table.First ..
3208                Project_Table.Last (Project_Tree.Projects)
3209       loop
3210          Project_Tree.Projects.Table (J).Seen := False;
3211       end loop;
3212
3213       --  Empty Arguments
3214
3215       Last_Argument := 0;
3216
3217       --  Process this project individually, project data are already known
3218
3219       Project_Tree.Projects.Table (Project).Seen := True;
3220
3221       Add (Data.Source_Dirs);
3222
3223       Recursive_Get_Dirs (Data.Extends);
3224
3225       while Imported_Projects /= Empty_Project_List loop
3226          Recursive_Get_Dirs
3227            (Project_Tree.Project_Lists.Table
3228               (Imported_Projects).Project);
3229          Imported_Projects := Project_Tree.Project_Lists.Table
3230                                 (Imported_Projects).Next;
3231       end loop;
3232
3233       Data.Imported_Directories_Switches :=
3234         new Argument_List'(Arguments (1 .. Last_Argument));
3235
3236       --  Create the Include_Path, from the Arguments
3237
3238       Data.Include_Path := new String (1 .. Path_Length);
3239       Data.Include_Path (1 .. Arguments (1)'Length - 2) :=
3240         Arguments (1)(Arguments (1)'First + 2 .. Arguments (1)'Last);
3241       Position := Arguments (1)'Length - 2;
3242
3243       for Arg in 2 .. Last_Argument loop
3244          Position := Position + 1;
3245          Data.Include_Path (Position) := Path_Separator;
3246          Data.Include_Path
3247            (Position + 1 .. Position + Arguments (Arg)'Length - 2) :=
3248            Arguments (Arg)(Arguments (Arg)'First + 2 .. Arguments (Arg)'Last);
3249          Position := Position + Arguments (Arg)'Length - 2;
3250       end loop;
3251
3252       Last_Argument := 0;
3253    end Get_Imported_Directories;
3254
3255    -------------
3256    -- Gprmake --
3257    -------------
3258
3259    procedure Gprmake is
3260    begin
3261       Makegpr.Initialize;
3262
3263       if Verbose_Mode then
3264          Write_Eol;
3265          Write_Str ("Parsing project file """);
3266          Write_Str (Project_File_Name.all);
3267          Write_Str (""".");
3268          Write_Eol;
3269       end if;
3270
3271       --  Parse and process project files for other languages (not for Ada)
3272
3273       Prj.Pars.Parse
3274         (Project           => Main_Project,
3275          In_Tree           => Project_Tree,
3276          Project_File_Name => Project_File_Name.all,
3277          Packages_To_Check => Packages_To_Check);
3278
3279       --  Fail if parsing/processing was unsuccessful
3280
3281       if Main_Project = No_Project then
3282          Osint.Fail ("""", Project_File_Name.all, """ processing failed");
3283       end if;
3284
3285       if Verbose_Mode then
3286          Write_Eol;
3287          Write_Str ("Parsing of project file """);
3288          Write_Str (Project_File_Name.all);
3289          Write_Str (""" is finished.");
3290          Write_Eol;
3291       end if;
3292
3293       --  If -f was specified, we will certainly need to link (except when
3294       --  -u or -c were specified, of course).
3295
3296       Need_To_Relink := Force_Compilations;
3297
3298       if Unique_Compile then
3299          if Mains.Number_Of_Mains = 0 then
3300             Osint.Fail
3301               ("No source specified to compile in 'unique compile' mode");
3302          else
3303             Compile_Individual_Sources;
3304             Report_Total_Errors ("compilation");
3305          end if;
3306
3307       else
3308          declare
3309             Data : constant Prj.Project_Data :=
3310                      Project_Tree.Projects.Table (Main_Project);
3311          begin
3312             if Data.Library and then Mains.Number_Of_Mains /= 0 then
3313                Osint.Fail
3314                  ("Cannot specify mains on the command line " &
3315                   "for a Library Project");
3316             end if;
3317
3318             --  First check for C++, to link libraries with g++,
3319             --  rather than gcc.
3320
3321             Check_For_C_Plus_Plus;
3322
3323             --  Compile sources and build archives for library project,
3324             --  if necessary.
3325
3326             Compile_Sources;
3327
3328             --  When Keep_Going is True, if we had some errors, fail now,
3329             --  reporting the number of compilation errors.
3330             --  Do not attempt to link.
3331
3332             Report_Total_Errors ("compilation");
3333
3334             --  If -c was not specified, link the executables,
3335             --  if there are any.
3336
3337             if not Compile_Only
3338               and then not Data.Library
3339               and then Data.Object_Directory /= No_Path
3340             then
3341                Build_Global_Archive;
3342                Link_Executables;
3343             end if;
3344
3345             --  When Keep_Going is True, if we had some errors, fail, reporting
3346             --  the number of linking errors.
3347
3348             Report_Total_Errors ("linking");
3349          end;
3350       end if;
3351    end Gprmake;
3352
3353    ----------------
3354    -- Initialize --
3355    ----------------
3356
3357    procedure Initialize is
3358    begin
3359       Set_Mode (Ada_Only);
3360
3361       --  Do some necessary package initializations
3362
3363       Csets.Initialize;
3364       Namet.Initialize;
3365       Snames.Initialize;
3366       Prj.Initialize (Project_Tree);
3367       Mains.Delete;
3368
3369       --  Add the directory where gprmake is invoked in front of the path,
3370       --  if gprmake is invoked from a bin directory or with directory
3371       --  information. information. Only do this if the platform is not VMS,
3372       --  where the notion of path does not really exist.
3373
3374       --  Below code shares nasty code duplication with make.adb code???
3375
3376       if not OpenVMS then
3377          declare
3378             Prefix  : constant String := Executable_Prefix_Path;
3379             Command : constant String := Command_Name;
3380
3381          begin
3382             if Prefix'Length > 0 then
3383                declare
3384                   PATH : constant String :=
3385                            Prefix & Directory_Separator & "bin" &
3386                            Path_Separator &
3387                            Getenv ("PATH").all;
3388                begin
3389                   Setenv ("PATH", PATH);
3390                end;
3391
3392             else
3393                for Index in reverse Command'Range loop
3394                   if Command (Index) = Directory_Separator then
3395                      declare
3396                         Absolute_Dir : constant String :=
3397                                          Normalize_Pathname
3398                                            (Command (Command'First .. Index));
3399                         PATH         : constant String :=
3400                                          Absolute_Dir &
3401                                          Path_Separator &
3402                                          Getenv ("PATH").all;
3403                      begin
3404                         Setenv ("PATH", PATH);
3405                      end;
3406
3407                      exit;
3408                   end if;
3409                end loop;
3410             end if;
3411          end;
3412       end if;
3413
3414       --  Set Name_Ide and Name_Compiler_Command
3415
3416       Name_Len := 0;
3417       Add_Str_To_Name_Buffer ("ide");
3418       Name_Ide := Name_Find;
3419
3420       Name_Len := 0;
3421       Add_Str_To_Name_Buffer ("compiler_command");
3422       Name_Compiler_Command := Name_Find;
3423
3424       --  Make sure the Saved_Switches table is empty
3425
3426       Saved_Switches.Set_Last (0);
3427
3428       --  Get the command line arguments
3429
3430       Scan_Args : for Next_Arg in 1 .. Argument_Count loop
3431          Scan_Arg (Argument (Next_Arg));
3432       end loop Scan_Args;
3433
3434       --  Fail if command line ended with "-P"
3435
3436       if Project_File_Name_Expected then
3437          Osint.Fail ("project file name missing after -P");
3438
3439       --  Or if it ended with "-o"
3440
3441       elsif Output_File_Name_Expected then
3442          Osint.Fail ("output file name missing after -o");
3443       end if;
3444
3445       --  If no project file was specified, display the usage and fail
3446
3447       if Project_File_Name = null then
3448          Usage;
3449          Exit_Program (E_Success);
3450       end if;
3451
3452       --  To be able of finding libgnat.a in MLib.Tgt, we need to have the
3453       --  default search dirs established in Osint.
3454
3455       Osint.Add_Default_Search_Dirs;
3456    end Initialize;
3457
3458    -----------------------------------
3459    -- Is_Included_In_Global_Archive --
3460    -----------------------------------
3461
3462    function Is_Included_In_Global_Archive
3463      (Object_Name : File_Name_Type;
3464       Project     : Project_Id) return Boolean
3465    is
3466       Data   : Project_Data := Project_Tree.Projects.Table (Project);
3467       Source : Other_Source_Id;
3468
3469    begin
3470       while Data.Extended_By /= No_Project loop
3471          Data := Project_Tree.Projects.Table (Data.Extended_By);
3472
3473          Source := Data.First_Other_Source;
3474          while Source /= No_Other_Source loop
3475             if Project_Tree.Other_Sources.Table (Source).Object_Name =
3476                  Object_Name
3477             then
3478                return False;
3479             else
3480                Source :=
3481                  Project_Tree.Other_Sources.Table (Source).Next;
3482             end if;
3483          end loop;
3484       end loop;
3485
3486       return True;
3487    end Is_Included_In_Global_Archive;
3488
3489    ----------------------
3490    -- Link_Executables --
3491    ----------------------
3492
3493    procedure Link_Executables is
3494       Data : constant Project_Data :=
3495                Project_Tree.Projects.Table (Main_Project);
3496
3497       Mains_Specified : constant Boolean := Mains.Number_Of_Mains /= 0;
3498       --  True if main sources were specified on the command line
3499
3500       Object_Dir : constant String :=
3501                      Get_Name_String (Data.Display_Object_Dir);
3502       --  Path of the object directory of the main project
3503
3504       Source_Id : Other_Source_Id;
3505       Source    : Other_Source;
3506       Success   : Boolean;
3507
3508       Linker_Name : String_Access;
3509       Linker_Path : String_Access;
3510       --  The linker name and path, when linking is not done by gnatlink
3511
3512       Link_Done   : Boolean := False;
3513       --  Set to True when the linker is invoked directly (not through
3514       --  gnatmake) to be able to report if mains were up to date at the end
3515       --  of execution.
3516
3517       procedure Add_C_Plus_Plus_Link_For_Gnatmake;
3518       --  Add the --LINK= switch for gnatlink, depending on the C++ compiler
3519
3520       procedure Check_Time_Stamps (Exec_Time_Stamp : Time_Stamp_Type);
3521       --  Check if there is an archive that is more recent than the executable
3522       --  to decide if we need to relink.
3523
3524       procedure Choose_C_Plus_Plus_Link_Process;
3525       --  If the C++ compiler is not g++, create the correct script to link
3526
3527       procedure Link_Foreign
3528         (Main    : String;
3529          Main_Id : File_Name_Type;
3530          Source  : Other_Source);
3531       --  Link a non-Ada main, when there is no Ada code
3532
3533       ---------------------------------------
3534       -- Add_C_Plus_Plus_Link_For_Gnatmake --
3535       ---------------------------------------
3536
3537       procedure Add_C_Plus_Plus_Link_For_Gnatmake is
3538       begin
3539          Add_Argument
3540            ("--LINK=" & Compiler_Names (C_Plus_Plus_Language_Index).all,
3541             Verbose_Mode);
3542       end Add_C_Plus_Plus_Link_For_Gnatmake;
3543
3544       -----------------------
3545       -- Check_Time_Stamps --
3546       -----------------------
3547
3548       procedure Check_Time_Stamps (Exec_Time_Stamp : Time_Stamp_Type) is
3549          Prj_Data : Project_Data;
3550
3551       begin
3552          for Prj in Project_Table.First ..
3553                     Project_Table.Last (Project_Tree.Projects)
3554          loop
3555             Prj_Data := Project_Tree.Projects.Table (Prj);
3556
3557             --  There is an archive only in project
3558             --  files with sources other than Ada
3559             --  sources.
3560
3561             if Data.Other_Sources_Present then
3562                declare
3563                   Archive_Path : constant String := Get_Name_String
3564                     (Prj_Data.Display_Object_Dir) & Directory_Separator
3565                     & "lib" & Get_Name_String (Prj_Data.Display_Name)
3566                     & '.' & Archive_Ext;
3567                   Archive_TS   : Time_Stamp_Type;
3568                begin
3569                   Name_Len := 0;
3570                   Add_Str_To_Name_Buffer (Archive_Path);
3571                   Archive_TS := File_Stamp (File_Name_Type'(Name_Find));
3572
3573                   --  If the archive is later than the
3574                   --  executable, we need to relink.
3575
3576                   if Archive_TS /=  Empty_Time_Stamp
3577                     and then
3578                       Exec_Time_Stamp < Archive_TS
3579                   then
3580                      Need_To_Relink := True;
3581
3582                      if Verbose_Mode then
3583                         Write_Str ("      -> ");
3584                         Write_Str (Archive_Path);
3585                         Write_Str (" has time stamp ");
3586                         Write_Str ("later than ");
3587                         Write_Line ("executable");
3588                      end if;
3589
3590                      exit;
3591                   end if;
3592                end;
3593             end if;
3594          end loop;
3595       end Check_Time_Stamps;
3596
3597       -------------------------------------
3598       -- Choose_C_Plus_Plus_Link_Process --
3599       -------------------------------------
3600
3601       procedure Choose_C_Plus_Plus_Link_Process is
3602       begin
3603          if Compiler_Names (C_Plus_Plus_Language_Index) = null then
3604             Get_Compiler (C_Plus_Plus_Language_Index);
3605          end if;
3606       end Choose_C_Plus_Plus_Link_Process;
3607
3608       ------------------
3609       -- Link_Foreign --
3610       ------------------
3611
3612       procedure Link_Foreign
3613         (Main    : String;
3614          Main_Id : File_Name_Type;
3615          Source  : Other_Source)
3616       is
3617          Executable_Name : constant String :=
3618                              Get_Name_String
3619                                (Executable_Of
3620                                     (Project  => Main_Project,
3621                                      In_Tree  => Project_Tree,
3622                                      Main     => Main_Id,
3623                                      Index    => 0,
3624                                      Ada_Main => False));
3625          --  File name of the executable
3626
3627          Executable_Path : constant String :=
3628                              Get_Name_String
3629                                (Data.Display_Exec_Dir) &
3630                                 Directory_Separator & Executable_Name;
3631          --  Path name of the executable
3632
3633          Exec_Time_Stamp : Time_Stamp_Type;
3634
3635       begin
3636          --  Now, check if the executable is up to date. It is considered
3637          --  up to date if its time stamp is not earlier that the time stamp
3638          --  of any archive. Only do that if we don't know if we need to link.
3639
3640          if not Need_To_Relink then
3641
3642             --  Get the time stamp of the executable
3643
3644             Name_Len := 0;
3645             Add_Str_To_Name_Buffer (Executable_Path);
3646             Exec_Time_Stamp := File_Stamp (File_Name_Type'(Name_Find));
3647
3648             if Verbose_Mode then
3649                Write_Str  ("   Checking executable ");
3650                Write_Line (Executable_Name);
3651             end if;
3652
3653             --  If executable does not exist, we need to link
3654
3655             if Exec_Time_Stamp = Empty_Time_Stamp then
3656                Need_To_Relink := True;
3657
3658                if Verbose_Mode then
3659                   Write_Line ("      -> not found");
3660                end if;
3661
3662             --  Otherwise, get the time stamps of each archive. If one of
3663             --  them is found later than the executable, we need to relink.
3664
3665             else
3666                Check_Time_Stamps (Exec_Time_Stamp);
3667             end if;
3668
3669             --  If Need_To_Relink is False, we are done
3670
3671             if Verbose_Mode and (not Need_To_Relink) then
3672                Write_Line ("      -> up to date");
3673             end if;
3674          end if;
3675
3676          --  Prepare to link
3677
3678          if Need_To_Relink then
3679             Link_Done := True;
3680
3681             Last_Argument := 0;
3682
3683             --  Specify the executable path name
3684
3685             Add_Argument (Dash_o, True);
3686             Add_Argument
3687               (Get_Name_String (Data.Display_Exec_Dir) &
3688                Directory_Separator &
3689                Get_Name_String
3690                  (Executable_Of
3691                     (Project  => Main_Project,
3692                      In_Tree  => Project_Tree,
3693                      Main     => Main_Id,
3694                      Index    => 0,
3695                      Ada_Main => False)),
3696                True);
3697
3698             --  Specify the object file of the main source
3699
3700             Add_Argument
3701               (Object_Dir & Directory_Separator &
3702                Get_Name_String (Source.Object_Name),
3703                True);
3704
3705             --  Add all the archives, in a correct order
3706
3707             Add_Archives (For_Gnatmake => False);
3708
3709             --  Add the switches specified in package Linker of
3710             --  the main project.
3711
3712             Add_Switches
3713               (Data      => Data,
3714                Proc      => Linker,
3715                Language  => Source.Language,
3716                File_Name => Main_Id);
3717
3718             --  Add the switches specified in attribute
3719             --  Linker_Options of packages Linker.
3720
3721             if Link_Options_Switches = null then
3722                Link_Options_Switches :=
3723                  new Argument_List'
3724                    (Linker_Options_Switches (Main_Project, Project_Tree));
3725             end if;
3726
3727             Add_Arguments (Link_Options_Switches.all, True);
3728
3729             --  Add the linking options specified on the
3730             --  command line.
3731
3732             for Arg in 1 .. Linker_Options.Last loop
3733                Add_Argument (Linker_Options.Table (Arg), True);
3734             end loop;
3735
3736             --  If there are shared libraries and the run path
3737             --  option is supported, add the run path switch.
3738
3739             if Lib_Path.Last > 0 then
3740                Add_Argument
3741                  (Path_Option.all &
3742                   String (Lib_Path.Table (1 .. Lib_Path.Last)),
3743                   Verbose_Mode);
3744             end if;
3745
3746             --  And invoke the linker
3747
3748             Display_Command (Linker_Name.all, Linker_Path);
3749             Spawn
3750               (Linker_Path.all,
3751                Arguments (1 .. Last_Argument),
3752                Success);
3753
3754             if not Success then
3755                Report_Error ("could not link ", Main);
3756             end if;
3757          end if;
3758       end Link_Foreign;
3759
3760    --  Start of processing of Link_Executables
3761
3762    begin
3763       --  If no mains specified, get mains from attribute Main, if it exists
3764
3765       if not Mains_Specified then
3766          declare
3767             Element_Id : String_List_Id;
3768             Element    : String_Element;
3769
3770          begin
3771             Element_Id := Data.Mains;
3772             while Element_Id /= Nil_String loop
3773                Element := Project_Tree.String_Elements.Table (Element_Id);
3774
3775                if Element.Value /= No_Name then
3776                   Mains.Add_Main (Get_Name_String (Element.Value));
3777                end if;
3778
3779                Element_Id := Element.Next;
3780             end loop;
3781          end;
3782       end if;
3783
3784       if Mains.Number_Of_Mains = 0 then
3785
3786          --  If the attribute Main is an empty list or not specified,
3787          --  there is nothing to do.
3788
3789          if Verbose_Mode then
3790             Write_Line ("No main to link");
3791          end if;
3792          return;
3793       end if;
3794
3795       --  Check if -o was used for several mains
3796
3797       if Output_File_Name /= null and then Mains.Number_Of_Mains > 1 then
3798          Osint.Fail ("cannot specify an executable name for several mains");
3799       end if;
3800
3801       --  Check how we are going to do the link
3802
3803       if not Data.Other_Sources_Present then
3804
3805          --  Only Ada sources in the main project, and even maybe not
3806
3807          if not Data.Langs (Ada_Language_Index) then
3808
3809             --  Fail if the main project has no source of any language
3810
3811             Osint.Fail
3812               ("project """,
3813                Get_Name_String (Data.Name),
3814                """ has no sources, so no main can be linked");
3815
3816          else
3817             --  Only Ada sources in the main project, call gnatmake directly
3818
3819             Last_Argument := 0;
3820
3821             --  Choose correct linker if there is C++ code in other projects
3822
3823             if C_Plus_Plus_Is_Used then
3824                Choose_C_Plus_Plus_Link_Process;
3825                Add_Argument (Dash_largs, Verbose_Mode);
3826                Add_C_Plus_Plus_Link_For_Gnatmake;
3827                Add_Argument (Dash_margs, Verbose_Mode);
3828             end if;
3829
3830             Compile_Link_With_Gnatmake (Mains_Specified);
3831          end if;
3832
3833       else
3834          --  There are other language sources. First check if there are also
3835          --  sources in Ada.
3836
3837          if Data.Langs (Ada_Language_Index) then
3838
3839             --  There is a mix of Ada and other language sources in the main
3840             --  project. Any main that is not a source of the other languages
3841             --  will be deemed to be an Ada main.
3842
3843             --  Find the mains of the other languages and the Ada mains
3844
3845             Mains.Reset;
3846             Ada_Mains.Set_Last (0);
3847             Other_Mains.Set_Last (0);
3848
3849             --  For each main
3850
3851             loop
3852                declare
3853                   Main    : constant String := Mains.Next_Main;
3854                   Main_Id : File_Name_Type;
3855
3856                begin
3857                   exit when Main'Length = 0;
3858
3859                   --  Get the main file name
3860
3861                   Name_Len := 0;
3862                   Add_Str_To_Name_Buffer (Main);
3863                   Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
3864                   Main_Id := Name_Find;
3865
3866                   --  Check if it is a source of a language other than Ada
3867
3868                   Source_Id := Data.First_Other_Source;
3869                   while Source_Id /= No_Other_Source loop
3870                      Source :=
3871                        Project_Tree.Other_Sources.Table (Source_Id);
3872                      exit when Source.File_Name = Main_Id;
3873                      Source_Id := Source.Next;
3874                   end loop;
3875
3876                   --  If it is not, put it in the list of Ada mains
3877
3878                   if Source_Id = No_Other_Source then
3879                      Ada_Mains.Increment_Last;
3880                      Ada_Mains.Table (Ada_Mains.Last) := new String'(Main);
3881
3882                   --  Otherwise, put it in the list of other mains
3883
3884                   else
3885                      Other_Mains.Increment_Last;
3886                      Other_Mains.Table (Other_Mains.Last) := Source;
3887                   end if;
3888                end;
3889             end loop;
3890
3891             --  If C++ is one of the other language, create the shell script
3892             --  to do the link.
3893
3894             if C_Plus_Plus_Is_Used then
3895                Choose_C_Plus_Plus_Link_Process;
3896             end if;
3897
3898             --  Call gnatmake with the necessary switches for each non-Ada
3899             --  main, if there are some.
3900
3901             for Main in 1 .. Other_Mains.Last loop
3902                declare
3903                   Source : constant Other_Source := Other_Mains.Table (Main);
3904
3905                begin
3906                   Last_Argument := 0;
3907
3908                   --  Add -o if -o was specified
3909
3910                   if Output_File_Name = null then
3911                      Add_Argument (Dash_o, True);
3912                      Add_Argument
3913                        (Get_Name_String
3914                           (Executable_Of
3915                              (Project  => Main_Project,
3916                               In_Tree  => Project_Tree,
3917                               Main     => Other_Mains.Table (Main).File_Name,
3918                               Index    => 0,
3919                               Ada_Main => False)),
3920                         True);
3921                   end if;
3922
3923                   --  Call gnatmake with the -B switch
3924
3925                   Add_Argument (Dash_B, True);
3926
3927                   --  Add to the linking options the object file of the source
3928
3929                   Add_Argument (Dash_largs, Verbose_Mode);
3930                   Add_Argument
3931                     (Get_Name_String (Source.Object_Name), Verbose_Mode);
3932
3933                   --  If C++ is one of the language, add the --LINK switch
3934                   --  to the linking switches.
3935
3936                   if C_Plus_Plus_Is_Used then
3937                      Add_C_Plus_Plus_Link_For_Gnatmake;
3938                   end if;
3939
3940                   --  Add -margs so that the following switches are for
3941                   --  gnatmake
3942
3943                   Add_Argument (Dash_margs, Verbose_Mode);
3944
3945                   --  And link with gnatmake
3946
3947                   Compile_Link_With_Gnatmake (Mains_Specified => False);
3948                end;
3949             end loop;
3950
3951             --  If there are also Ada mains, call gnatmake for all these mains
3952
3953             if Ada_Mains.Last /= 0 then
3954                Last_Argument := 0;
3955
3956                --  Put all the Ada mains as the first arguments
3957
3958                for Main in 1 .. Ada_Mains.Last loop
3959                   Add_Argument (Ada_Mains.Table (Main).all, True);
3960                end loop;
3961
3962                --  If C++ is one of the languages, add the --LINK switch to
3963                --  the linking switches.
3964
3965                if Data.Langs (C_Plus_Plus_Language_Index) then
3966                   Add_Argument (Dash_largs, Verbose_Mode);
3967                   Add_C_Plus_Plus_Link_For_Gnatmake;
3968                   Add_Argument (Dash_margs, Verbose_Mode);
3969                end if;
3970
3971                --  And link with gnatmake
3972
3973                Compile_Link_With_Gnatmake (Mains_Specified => False);
3974             end if;
3975
3976          else
3977             --  No Ada source in main project
3978
3979             --  First, get the linker to invoke
3980
3981             if Data.Langs (C_Plus_Plus_Language_Index) then
3982                Get_Compiler (C_Plus_Plus_Language_Index);
3983                Linker_Name := Compiler_Names (C_Plus_Plus_Language_Index);
3984                Linker_Path := Compiler_Paths (C_Plus_Plus_Language_Index);
3985
3986             else
3987                Get_Compiler (C_Language_Index);
3988                Linker_Name := Compiler_Names (C_Language_Index);
3989                Linker_Path := Compiler_Paths (C_Language_Index);
3990             end if;
3991
3992             Link_Done := False;
3993
3994             Mains.Reset;
3995
3996             --  Get each main, check if it is a source of the main project,
3997             --  and if it is, invoke the linker.
3998
3999             loop
4000                declare
4001                   Main    : constant String := Mains.Next_Main;
4002                   Main_Id : File_Name_Type;
4003
4004                begin
4005                   exit when Main'Length = 0;
4006
4007                   --  Get the file name of the main
4008
4009                   Name_Len := 0;
4010                   Add_Str_To_Name_Buffer (Main);
4011                   Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
4012                   Main_Id := Name_Find;
4013
4014                   --  Check if it is a source of the main project file
4015
4016                   Source_Id := Data.First_Other_Source;
4017                   while Source_Id /= No_Other_Source loop
4018                      Source :=
4019                        Project_Tree.Other_Sources.Table (Source_Id);
4020                      exit when Source.File_Name = Main_Id;
4021                      Source_Id := Source.Next;
4022                   end loop;
4023
4024                   --  Report an error if it is not
4025
4026                   if Source_Id = No_Other_Source then
4027                      Report_Error
4028                        (Main, "is not a source of project ",
4029                         Get_Name_String (Data.Name));
4030
4031                   else
4032                      Link_Foreign (Main, Main_Id, Source);
4033                   end if;
4034                end;
4035             end loop;
4036
4037             --  If no linking was done, report it, except in Quiet Output
4038
4039             if (Verbose_Mode or (not Quiet_Output)) and (not Link_Done) then
4040                Osint.Write_Program_Name;
4041
4042                if Mains.Number_Of_Mains = 1 then
4043
4044                   --  If there is only one executable, report its name too
4045
4046                   Write_Str (": """);
4047                   Mains.Reset;
4048
4049                   declare
4050                      Main    : constant String := Mains.Next_Main;
4051                      Main_Id : File_Name_Type;
4052                   begin
4053                      Name_Len := 0;
4054                      Add_Str_To_Name_Buffer (Main);
4055                      Main_Id := Name_Find;
4056                      Write_Str
4057                        (Get_Name_String
4058                           (Executable_Of
4059                              (Project  => Main_Project,
4060                               In_Tree  => Project_Tree,
4061                               Main     => Main_Id,
4062                               Index    => 0,
4063                               Ada_Main => False)));
4064                      Write_Line (""" up to date");
4065                   end;
4066
4067                else
4068                   Write_Line (": all executables up to date");
4069                end if;
4070             end if;
4071          end if;
4072       end if;
4073    end Link_Executables;
4074
4075    ------------------
4076    -- Report_Error --
4077    ------------------
4078
4079    procedure Report_Error
4080      (S1 : String;
4081       S2 : String := "";
4082       S3 : String := "")
4083    is
4084    begin
4085       --  If Keep_Going is True, output error message preceded by error header
4086
4087       if Keep_Going then
4088          Total_Number_Of_Errors := Total_Number_Of_Errors + 1;
4089          Write_Str (Error_Header);
4090          Write_Str (S1);
4091          Write_Str (S2);
4092          Write_Str (S3);
4093          Write_Eol;
4094
4095       --  Otherwise just fail
4096
4097       else
4098          Osint.Fail (S1, S2, S3);
4099       end if;
4100    end Report_Error;
4101
4102    -------------------------
4103    -- Report_Total_Errors --
4104    -------------------------
4105
4106    procedure Report_Total_Errors (Kind : String) is
4107    begin
4108       if Total_Number_Of_Errors /= 0 then
4109          if Total_Number_Of_Errors = 1 then
4110             Osint.Fail
4111               ("One ", Kind, " error");
4112
4113          else
4114             Osint.Fail
4115               ("Total of" & Total_Number_Of_Errors'Img,
4116                ' ' & Kind & " errors");
4117          end if;
4118       end if;
4119    end Report_Total_Errors;
4120
4121    --------------
4122    -- Scan_Arg --
4123    --------------
4124
4125    procedure Scan_Arg (Arg : String) is
4126    begin
4127       pragma Assert (Arg'First = 1);
4128
4129       if Arg'Length = 0 then
4130          return;
4131       end if;
4132
4133       --  If preceding switch was -P, a project file name need to be
4134       --  specified, not a switch.
4135
4136       if Project_File_Name_Expected then
4137          if Arg (1) = '-' then
4138             Osint.Fail ("project file name missing after -P");
4139          else
4140             Project_File_Name_Expected := False;
4141             Project_File_Name := new String'(Arg);
4142          end if;
4143
4144       --  If preceding switch was -o, an executable name need to be
4145       --  specified, not a switch.
4146
4147       elsif Output_File_Name_Expected then
4148          if Arg (1) = '-' then
4149             Osint.Fail ("output file name missing after -o");
4150          else
4151             Output_File_Name_Expected := False;
4152             Output_File_Name := new String'(Arg);
4153          end if;
4154
4155       --  Set the processor/language for the following switches
4156
4157       --  -cargs: Ada compiler arguments
4158
4159       elsif Arg = "-cargs" then
4160          Current_Language  := Ada_Language_Index;
4161          Current_Processor := Compiler;
4162
4163       elsif Arg'Length > 7 and then Arg (1 .. 7) = "-cargs:" then
4164          Name_Len := 0;
4165          Add_Str_To_Name_Buffer (Arg (8 .. Arg'Last));
4166          To_Lower (Name_Buffer (1 .. Name_Len));
4167
4168          declare
4169             Lang : constant Name_Id := Name_Find;
4170          begin
4171             Current_Language := Language_Indexes.Get (Lang);
4172
4173             if Current_Language = No_Language_Index then
4174                Add_Language_Name (Lang);
4175                Current_Language := Last_Language_Index;
4176             end if;
4177
4178             Current_Processor := Compiler;
4179          end;
4180
4181       elsif Arg = "-largs" then
4182          Current_Processor := Linker;
4183
4184       --  -gargs: gprmake
4185
4186       elsif Arg = "-gargs" then
4187          Current_Processor := None;
4188
4189       --  A special test is needed for the -o switch within a -largs since
4190       --  that is another way to specify the name of the final executable.
4191
4192       elsif Current_Processor = Linker and then Arg = "-o" then
4193          Osint.Fail
4194            ("switch -o not allowed within a -largs. Use -o directly.");
4195
4196       --  If current processor is not gprmake directly, store the option in
4197       --  the appropriate table.
4198
4199       elsif Current_Processor /= None then
4200          Add_Option (Arg);
4201
4202       --  Switches start with '-'
4203
4204       elsif Arg (1) = '-' then
4205          if Arg'Length > 3 and then Arg (1 .. 3) = "-aP" then
4206             Add_Search_Project_Directory (Arg (4 .. Arg'Last));
4207
4208             --  Record the switch, so that it is passed to gnatmake, if
4209             --  gnatmake is called.
4210
4211             Saved_Switches.Append (new String'(Arg));
4212
4213          elsif Arg = "-c" then
4214             Compile_Only := True;
4215
4216             --  Make sure that when a main is specified and switch -c is used,
4217             --  only the main(s) is/are compiled.
4218
4219             if Mains.Number_Of_Mains > 0 then
4220                Unique_Compile := True;
4221             end if;
4222
4223          elsif Arg = "-d" then
4224             Display_Compilation_Progress := True;
4225
4226          elsif Arg = "-f" then
4227             Force_Compilations := True;
4228
4229          elsif Arg = "-h" then
4230             Usage;
4231
4232          elsif Arg = "-k" then
4233             Keep_Going := True;
4234
4235          elsif Arg = "-o" then
4236             if Output_File_Name /= null then
4237                Osint.Fail ("cannot specify several -o switches");
4238
4239             else
4240                Output_File_Name_Expected := True;
4241             end if;
4242
4243          elsif Arg'Length >= 2 and then Arg (2) = 'P' then
4244             if Project_File_Name /= null then
4245                Osint.Fail ("cannot have several project files specified");
4246
4247             elsif Arg'Length = 2 then
4248                Project_File_Name_Expected := True;
4249
4250             else
4251                Project_File_Name := new String'(Arg (3 .. Arg'Last));
4252             end if;
4253
4254          elsif Arg = "-p" or else Arg = "--create-missing-dirs" then
4255             Setup_Projects := True;
4256
4257          elsif Arg = "-q" then
4258             Quiet_Output := True;
4259
4260          elsif Arg = "-u" then
4261             Unique_Compile := True;
4262             Compile_Only   := True;
4263
4264          elsif Arg = "-v" then
4265             Verbose_Mode := True;
4266             Copyright;
4267
4268          elsif Arg'Length = 4 and then Arg (1 .. 3) = "-vP"
4269            and then Arg (4) in '0' .. '2'
4270          then
4271             case Arg (4) is
4272                when '0' =>
4273                   Current_Verbosity := Prj.Default;
4274                when '1' =>
4275                   Current_Verbosity := Prj.Medium;
4276                when '2' =>
4277                   Current_Verbosity := Prj.High;
4278                when others =>
4279                   null;
4280             end case;
4281
4282          elsif Arg'Length >= 3 and then Arg (2) = 'X'
4283            and then Is_External_Assignment (Arg)
4284          then
4285             --  Is_External_Assignment has side effects when it returns True
4286
4287             --  Record the -X switch, so that it will be passed to gnatmake,
4288             --  if gnatmake is called.
4289
4290             Saved_Switches.Append (new String'(Arg));
4291
4292          else
4293             Osint.Fail ("illegal option """, Arg, """");
4294          end if;
4295
4296       else
4297          --  Not a switch: must be a main
4298
4299          Mains.Add_Main (Arg);
4300
4301          --  Make sure that when a main is specified and switch -c is used,
4302          --  only the main(s) is/are compiled.
4303
4304          if Compile_Only then
4305             Unique_Compile := True;
4306          end if;
4307       end if;
4308    end Scan_Arg;
4309
4310    -----------------
4311    -- Strip_CR_LF --
4312    -----------------
4313
4314    function Strip_CR_LF (Text : String) return String is
4315       To       : String (1 .. Text'Length);
4316       Index_To : Natural := 0;
4317
4318    begin
4319       for Index in Text'Range loop
4320          if (Text (Index) /= ASCII.CR) and then (Text (Index) /= ASCII.LF) then
4321             Index_To := Index_To + 1;
4322             To (Index_To) := Text (Index);
4323          end if;
4324       end loop;
4325
4326       return To (1 .. Index_To);
4327    end Strip_CR_LF;
4328
4329    -----------
4330    -- Usage --
4331    -----------
4332
4333    procedure Usage is
4334    begin
4335       if not Usage_Output then
4336          Usage_Output := True;
4337          Copyright;
4338
4339          Write_Str ("Usage: ");
4340          Osint.Write_Program_Name;
4341          Write_Str (" -P<project file> [opts]  [name] {");
4342          Write_Str ("[-cargs:lang opts] ");
4343          Write_Str ("[-largs opts] [-gargs opts]}");
4344          Write_Eol;
4345          Write_Eol;
4346          Write_Str ("  name is zero or more file names");
4347          Write_Eol;
4348          Write_Eol;
4349
4350          --  GPRMAKE switches
4351
4352          Write_Str ("gprmake switches:");
4353          Write_Eol;
4354
4355          --  Line for -aP
4356
4357          Write_Str ("  -aPdir   Add directory dir to project search path");
4358          Write_Eol;
4359
4360          --  Line for -c
4361
4362          Write_Str ("  -c       Compile only");
4363          Write_Eol;
4364
4365          --  Line for -f
4366
4367          Write_Str ("  -f       Force recompilations");
4368          Write_Eol;
4369
4370          --  Line for -k
4371
4372          Write_Str ("  -k       Keep going after compilation errors");
4373          Write_Eol;
4374
4375          --  Line for -o
4376
4377          Write_Str ("  -o name  Choose an alternate executable name");
4378          Write_Eol;
4379
4380          --  Line for -p
4381
4382          Write_Str ("  -p       Create missing obj, lib and exec dirs");
4383          Write_Eol;
4384
4385          --  Line for -P
4386
4387          Write_Str ("  -Pproj   Use GNAT Project File proj");
4388          Write_Eol;
4389
4390          --  Line for -q
4391
4392          Write_Str ("  -q       Be quiet/terse");
4393          Write_Eol;
4394
4395          --  Line for -u
4396
4397          Write_Str
4398            ("  -u       Unique compilation. Only compile the given files");
4399          Write_Eol;
4400
4401          --  Line for -v
4402
4403          Write_Str ("  -v       Verbose output");
4404          Write_Eol;
4405
4406          --  Line for -vPx
4407
4408          Write_Str ("  -vPx     Specify verbosity when parsing Project Files");
4409          Write_Eol;
4410
4411          --  Line for -X
4412
4413          Write_Str ("  -Xnm=val Specify an external reference for " &
4414                     "Project Files");
4415          Write_Eol;
4416          Write_Eol;
4417
4418          --  Line for -cargs
4419
4420          Write_Line ("  -cargs opts     opts are passed to the Ada compiler");
4421
4422          --  Line for -cargs:lang
4423
4424          Write_Line ("  -cargs:<lang> opts");
4425          Write_Line ("     opts are passed to the compiler " &
4426                      "for language < lang > ");
4427
4428          --  Line for -largs
4429
4430          Write_Str ("  -largs opts    opts are passed to the linker");
4431          Write_Eol;
4432
4433          --  Line for -gargs
4434
4435          Write_Str ("  -gargs opts    opts directly interpreted by gprmake");
4436          Write_Eol;
4437          Write_Eol;
4438
4439       end if;
4440    end Usage;
4441
4442 begin
4443    Makeutl.Do_Fail := Report_Error'Access;
4444 end Makegpr;