OSDN Git Service

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