OSDN Git Service

2010-09-10 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatcmd.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              G N A T C M D                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1996-2010, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
27
28 with Csets;
29 with Hostparm; use Hostparm;
30 with Makeutl;  use Makeutl;
31 with MLib.Tgt; use MLib.Tgt;
32 with MLib.Utl;
33 with MLib.Fil;
34 with Namet;    use Namet;
35 with Opt;      use Opt;
36 with Osint;    use Osint;
37 with Output;
38 with Prj;      use Prj;
39 with Prj.Env;
40 with Prj.Ext;  use Prj.Ext;
41 with Prj.Pars;
42 with Prj.Tree; use Prj.Tree;
43 with Prj.Util; use Prj.Util;
44 with Sinput.P;
45 with Snames;   use Snames;
46 with Table;
47 with Targparm;
48 with Tempdir;
49 with Types;    use Types;
50 with VMS_Conv; use VMS_Conv;
51 with VMS_Cmds; use VMS_Cmds;
52
53 with Ada.Characters.Handling; use Ada.Characters.Handling;
54 with Ada.Command_Line;        use Ada.Command_Line;
55 with Ada.Text_IO;             use Ada.Text_IO;
56
57 with GNAT.OS_Lib; use GNAT.OS_Lib;
58
59 procedure GNATCmd is
60    Project_Node_Tree : Project_Node_Tree_Ref;
61    Project_File      : String_Access;
62    Project           : Prj.Project_Id;
63    Current_Verbosity : Prj.Verbosity := Prj.Default;
64    Tool_Package_Name : Name_Id       := No_Name;
65
66    B_Start : String_Ptr    := new String'("b~");
67    --  Prefix of binder generated file, changed to b__ for VMS
68
69    Old_Project_File_Used : Boolean := False;
70    --  This flag indicates a switch -p (for gnatxref and gnatfind) for
71    --  an old fashioned project file. -p cannot be used in conjunction
72    --  with -P.
73
74    Temp_File_Name : Path_Name_Type := No_Path;
75    --  The name of the temporary text file to put a list of source/object
76    --  files to pass to a tool.
77
78    ASIS_Main : String_Access := null;
79    --  Main for commands Check, Metric and Pretty, when -U is used
80
81    package First_Switches is new Table.Table
82      (Table_Component_Type => String_Access,
83       Table_Index_Type     => Integer,
84       Table_Low_Bound      => 1,
85       Table_Initial        => 20,
86       Table_Increment      => 100,
87       Table_Name           => "Gnatcmd.First_Switches");
88    --  A table to keep the switches from the project file
89
90    package Carg_Switches is new Table.Table
91      (Table_Component_Type => String_Access,
92       Table_Index_Type     => Integer,
93       Table_Low_Bound      => 1,
94       Table_Initial        => 20,
95       Table_Increment      => 100,
96       Table_Name           => "Gnatcmd.Carg_Switches");
97    --  A table to keep the switches following -cargs for ASIS tools
98
99    package Rules_Switches is new Table.Table
100      (Table_Component_Type => String_Access,
101       Table_Index_Type     => Integer,
102       Table_Low_Bound      => 1,
103       Table_Initial        => 20,
104       Table_Increment      => 100,
105       Table_Name           => "Gnatcmd.Rules_Switches");
106    --  A table to keep the switches following -rules for gnatcheck
107
108    package Library_Paths is new Table.Table (
109      Table_Component_Type => String_Access,
110      Table_Index_Type     => Integer,
111      Table_Low_Bound      => 1,
112      Table_Initial        => 20,
113      Table_Increment      => 100,
114      Table_Name           => "Make.Library_Path");
115
116    --  Packages of project files to pass to Prj.Pars.Parse, depending on the
117    --  tool. We allocate objects because we cannot declare aliased objects
118    --  as we are in a procedure, not a library level package.
119
120    subtype SA is String_Access;
121
122    Naming_String      : constant SA := new String'("naming");
123    Binder_String      : constant SA := new String'("binder");
124    Builder_String     : constant SA := new String'("builder");
125    Compiler_String    : constant SA := new String'("compiler");
126    Check_String       : constant SA := new String'("check");
127    Synchronize_String : constant SA := new String'("synchronize");
128    Eliminate_String   : constant SA := new String'("eliminate");
129    Finder_String      : constant SA := new String'("finder");
130    Linker_String      : constant SA := new String'("linker");
131    Gnatls_String      : constant SA := new String'("gnatls");
132    Pretty_String      : constant SA := new String'("pretty_printer");
133    Stack_String       : constant SA := new String'("stack");
134    Gnatstub_String    : constant SA := new String'("gnatstub");
135    Metric_String      : constant SA := new String'("metrics");
136    Xref_String        : constant SA := new String'("cross_reference");
137
138    Packages_To_Check_By_Binder   : constant String_List_Access :=
139      new String_List'((Naming_String, Binder_String));
140
141    Packages_To_Check_By_Check : constant String_List_Access :=
142      new String_List'
143           ((Naming_String, Builder_String, Check_String, Compiler_String));
144
145    Packages_To_Check_By_Sync : constant String_List_Access :=
146      new String_List'((Naming_String, Synchronize_String, Compiler_String));
147
148    Packages_To_Check_By_Eliminate : constant String_List_Access :=
149      new String_List'((Naming_String, Eliminate_String, Compiler_String));
150
151    Packages_To_Check_By_Finder    : constant String_List_Access :=
152      new String_List'((Naming_String, Finder_String));
153
154    Packages_To_Check_By_Linker    : constant String_List_Access :=
155      new String_List'((Naming_String, Linker_String));
156
157    Packages_To_Check_By_Gnatls    : constant String_List_Access :=
158      new String_List'((Naming_String, Gnatls_String));
159
160    Packages_To_Check_By_Pretty    : constant String_List_Access :=
161      new String_List'((Naming_String, Pretty_String, Compiler_String));
162
163    Packages_To_Check_By_Stack     : constant String_List_Access :=
164      new String_List'((Naming_String, Stack_String));
165
166    Packages_To_Check_By_Gnatstub  : constant String_List_Access :=
167      new String_List'((Naming_String, Gnatstub_String, Compiler_String));
168
169    Packages_To_Check_By_Metric  : constant String_List_Access :=
170      new String_List'((Naming_String, Metric_String, Compiler_String));
171
172    Packages_To_Check_By_Xref      : constant String_List_Access :=
173      new String_List'((Naming_String, Xref_String));
174
175    Packages_To_Check : String_List_Access := Prj.All_Packages;
176
177    ----------------------------------
178    -- Declarations for GNATCMD use --
179    ----------------------------------
180
181    The_Command : Command_Type;
182    --  The command specified in the invocation of the GNAT driver
183
184    Command_Arg : Positive := 1;
185    --  The index of the command in the arguments of the GNAT driver
186
187    My_Exit_Status : Exit_Status := Success;
188    --  The exit status of the spawned tool. Used to set the correct VMS
189    --  exit status.
190
191    Current_Work_Dir : constant String := Get_Current_Dir;
192    --  The path of the working directory
193
194    All_Projects : Boolean := False;
195    --  Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
196    --  indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
197    --  should be invoked for all sources of all projects.
198
199    -----------------------
200    -- Local Subprograms --
201    -----------------------
202
203    procedure Add_To_Carg_Switches (Switch : String_Access);
204    --  Add a switch to the Carg_Switches table. If it is the first one, put the
205    --  switch "-cargs" at the beginning of the table.
206
207    procedure Add_To_Rules_Switches (Switch : String_Access);
208    --  Add a switch to the Rules_Switches table. If it is the first one, put
209    --  the switch "-crules" at the beginning of the table.
210
211    procedure Check_Files;
212    --  For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
213    --  project file is specified, without any file arguments and without a
214    --  switch -files=. If it is the case, invoke the GNAT tool with the proper
215    --  list of files, derived from the sources of the project.
216
217    function Check_Project
218      (Project      : Project_Id;
219       Root_Project : Project_Id) return Boolean;
220    --  Returns True if Project = Root_Project or if we want to consider all
221    --  sources of all projects. For GNAT METRIC, also returns True if Project
222    --  is extended by Root_Project.
223
224    procedure Check_Relative_Executable (Name : in out String_Access);
225    --  Check if an executable is specified as a relative path. If it is, and
226    --  the path contains directory information, fail. Otherwise, prepend the
227    --  exec directory. This procedure is only used for GNAT LINK when a project
228    --  file is specified.
229
230    function Configuration_Pragmas_File return Path_Name_Type;
231    --  Return an argument, if there is a configuration pragmas file to be
232    --  specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
233    --  STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
234    --  METRIC).
235
236    function Mapping_File return Path_Name_Type;
237    --  Create and return the path name of a mapping file. Used for gnatstub
238    --  (GNAT STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric
239    --  (GNAT METRIC).
240
241    procedure Delete_Temp_Config_Files;
242    --  Delete all temporary config files. The caller is responsible for
243    --  ensuring that Keep_Temporary_Files is False.
244
245    procedure Get_Closure;
246    --  Get the sources in the closure of the ASIS_Main and add them to the
247    --  list of arguments.
248
249    function Index (Char : Character; Str : String) return Natural;
250    --  Returns first occurrence of Char in Str, returns 0 if Char not in Str
251
252    procedure Non_VMS_Usage;
253    --  Display usage for platforms other than VMS
254
255    procedure Process_Link;
256    --  Process GNAT LINK, when there is a project file specified
257
258    procedure Set_Library_For
259      (Project           : Project_Id;
260       Libraries_Present : in out Boolean);
261    --  If Project is a library project, add the correct -L and -l switches to
262    --  the linker invocation.
263
264    procedure Set_Libraries is
265       new For_Every_Project_Imported (Boolean, Set_Library_For);
266    --  Add the -L and -l switches to the linker for all of the library
267    --  projects.
268
269    procedure Test_If_Relative_Path
270      (Switch : in out String_Access;
271       Parent : String);
272    --  Test if Switch is a relative search path switch. If it is and it
273    --  includes directory information, prepend the path with Parent. This
274    --  subprogram is only called when using project files.
275
276    --------------------------
277    -- Add_To_Carg_Switches --
278    --------------------------
279
280    procedure Add_To_Carg_Switches (Switch : String_Access) is
281    begin
282       --  If the Carg_Switches table is empty, put "-cargs" at the beginning
283
284       if Carg_Switches.Last = 0 then
285          Carg_Switches.Increment_Last;
286          Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
287       end if;
288
289       Carg_Switches.Increment_Last;
290       Carg_Switches.Table (Carg_Switches.Last) := Switch;
291    end Add_To_Carg_Switches;
292
293    ---------------------------
294    -- Add_To_Rules_Switches --
295    ---------------------------
296
297    procedure Add_To_Rules_Switches (Switch : String_Access) is
298    begin
299       --  If the Rules_Switches table is empty, put "-rules" at the beginning
300
301       if Rules_Switches.Last = 0 then
302          Rules_Switches.Increment_Last;
303          Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
304       end if;
305
306       Rules_Switches.Increment_Last;
307       Rules_Switches.Table (Rules_Switches.Last) := Switch;
308    end Add_To_Rules_Switches;
309
310    -----------------
311    -- Check_Files --
312    -----------------
313
314    procedure Check_Files is
315       Add_Sources : Boolean := True;
316       Unit        : Prj.Unit_Index;
317       Subunit     : Boolean := False;
318       FD          : File_Descriptor := Invalid_FD;
319       Status      : Integer;
320       Success     : Boolean;
321
322    begin
323       --  Check if there is at least one argument that is not a switch or if
324       --  there is a -files= switch.
325
326       for Index in 1 .. Last_Switches.Last loop
327          if Last_Switches.Table (Index).all'Length > 7
328            and then Last_Switches.Table (Index) (1 .. 7) = "-files="
329          then
330             Add_Sources := False;
331             exit;
332
333          elsif Last_Switches.Table (Index) (1) /= '-' then
334             if Index = 1
335               or else
336                 (The_Command = Check
337                    and then Last_Switches.Table (Index - 1).all /= "-o")
338               or else
339                 (The_Command = Pretty
340                    and then Last_Switches.Table (Index - 1).all /= "-o"
341                    and then Last_Switches.Table (Index - 1).all /= "-of")
342               or else
343                 (The_Command = Metric
344                    and then
345                      Last_Switches.Table (Index - 1).all /= "-o"  and then
346                      Last_Switches.Table (Index - 1).all /= "-og" and then
347                      Last_Switches.Table (Index - 1).all /= "-ox" and then
348                      Last_Switches.Table (Index - 1).all /= "-d")
349               or else
350                 (The_Command /= Check  and then
351                  The_Command /= Pretty and then
352                  The_Command /= Metric)
353             then
354                Add_Sources := False;
355                exit;
356             end if;
357          end if;
358       end loop;
359
360       --  If all arguments are switches and there is no switch -files=, add
361       --  the path names of all the sources of the main project.
362
363       if Add_Sources then
364
365          --  For gnatcheck, gnatpp, and gnatmetric, create a temporary file
366          --  and put the list of sources in it.
367
368          if The_Command = Check  or else
369             The_Command = Pretty or else
370             The_Command = Metric
371          then
372             Tempdir.Create_Temp_File (FD, Temp_File_Name);
373             Last_Switches.Increment_Last;
374             Last_Switches.Table (Last_Switches.Last) :=
375               new String'("-files=" & Get_Name_String (Temp_File_Name));
376          end if;
377
378          declare
379             Proj : Project_List;
380
381          begin
382             --  Gnatstack needs to add the .ci file for the binder generated
383             --  files corresponding to all of the library projects and main
384             --  units belonging to the application.
385
386             if The_Command = Stack then
387                Proj := Project_Tree.Projects;
388                while Proj /= null loop
389                   if Check_Project (Proj.Project, Project) then
390                      declare
391                         Main : String_List_Id;
392                         File : String_Access;
393
394                      begin
395                         --  Include binder generated files for main programs
396
397                         Main := Proj.Project.Mains;
398                         while Main /= Nil_String loop
399                            File :=
400                              new String'
401                                (Get_Name_String
402                                  (Proj.Project.Object_Directory.Name)        &
403                                 B_Start.all                                  &
404                                 MLib.Fil.Ext_To
405                                   (Get_Name_String
406                                      (Project_Tree.String_Elements.Table
407                                         (Main).Value),
408                                    "ci"));
409
410                            if Is_Regular_File (File.all) then
411                               Last_Switches.Increment_Last;
412                               Last_Switches.Table (Last_Switches.Last) := File;
413                            end if;
414
415                            Main :=
416                              Project_Tree.String_Elements.Table (Main).Next;
417                         end loop;
418
419                         if Proj.Project.Library then
420
421                            --  Include the .ci file for the binder generated
422                            --  files that contains the initialization and
423                            --  finalization of the library.
424
425                            File :=
426                              new String'
427                                (Get_Name_String
428                                  (Proj.Project.Object_Directory.Name)        &
429                                 B_Start.all                                  &
430                                 Get_Name_String (Proj.Project.Library_Name)  &
431                                 ".ci");
432
433                            if Is_Regular_File (File.all) then
434                               Last_Switches.Increment_Last;
435                               Last_Switches.Table (Last_Switches.Last) := File;
436                            end if;
437                         end if;
438                      end;
439                   end if;
440
441                   Proj := Proj.Next;
442                end loop;
443             end if;
444
445             Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
446             while Unit /= No_Unit_Index loop
447
448                --  For gnatls, we only need to put the library units, body or
449                --  spec, but not the subunits.
450
451                if The_Command = List then
452                   if Unit.File_Names (Impl) /= null
453                     and then not Unit.File_Names (Impl).Locally_Removed
454                   then
455                      --  There is a body, check if it is for this project
456
457                      if All_Projects
458                        or else Unit.File_Names (Impl).Project = Project
459                      then
460                         Subunit := False;
461
462                         if Unit.File_Names (Spec) = null
463                           or else Unit.File_Names (Spec).Locally_Removed
464                         then
465                            --  We have a body with no spec: we need to check if
466                            --  this is a subunit, because gnatls will complain
467                            --  about subunits.
468
469                            declare
470                               Src_Ind : constant Source_File_Index :=
471                                           Sinput.P.Load_Project_File
472                                             (Get_Name_String
473                                               (Unit.File_Names
474                                                 (Impl).Path.Name));
475                            begin
476                               Subunit :=
477                                 Sinput.P.Source_File_Is_Subunit (Src_Ind);
478                            end;
479                         end if;
480
481                         if not Subunit then
482                            Last_Switches.Increment_Last;
483                            Last_Switches.Table (Last_Switches.Last) :=
484                              new String'
485                                (Get_Name_String
486                                     (Unit.File_Names
487                                          (Impl).Display_File));
488                         end if;
489                      end if;
490
491                   elsif Unit.File_Names (Spec) /= null
492                     and then not Unit.File_Names (Spec).Locally_Removed
493                   then
494                      --  We have a spec with no body. Check if it is for this
495                      --  project.
496
497                      if All_Projects or else
498                         Unit.File_Names (Spec).Project = Project
499                      then
500                         Last_Switches.Increment_Last;
501                         Last_Switches.Table (Last_Switches.Last) :=
502                           new String'(Get_Name_String
503                                        (Unit.File_Names (Spec).Display_File));
504                      end if;
505                   end if;
506
507                --  For gnatstack, we put the .ci files corresponding to the
508                --  different units, including the binder generated files. We
509                --  only need to do that for the library units, body or spec,
510                --  but not the subunits.
511
512                elsif The_Command = Stack then
513                   if Unit.File_Names (Impl) /= null
514                     and then not Unit.File_Names (Impl).Locally_Removed
515                   then
516                      --  There is a body. Check if .ci files for this project
517                      --  must be added.
518
519                      if Check_Project
520                           (Unit.File_Names (Impl).Project, Project)
521                      then
522                         Subunit := False;
523
524                         if Unit.File_Names (Spec) = null
525                           or else Unit.File_Names (Spec).Locally_Removed
526                         then
527                            --  We have a body with no spec: we need to check
528                            --  if this is a subunit, because .ci files are not
529                            --  generated for subunits.
530
531                            declare
532                               Src_Ind : constant Source_File_Index :=
533                                           Sinput.P.Load_Project_File
534                                             (Get_Name_String
535                                               (Unit.File_Names
536                                                 (Impl).Path.Name));
537                            begin
538                               Subunit :=
539                                 Sinput.P.Source_File_Is_Subunit (Src_Ind);
540                            end;
541                         end if;
542
543                         if not Subunit then
544                            Last_Switches.Increment_Last;
545                            Last_Switches.Table (Last_Switches.Last) :=
546                              new String'
547                                (Get_Name_String
548                                  (Unit.File_Names
549                                    (Impl).Project. Object_Directory.Name)  &
550                                 MLib.Fil.Ext_To
551                                   (Get_Name_String
552                                      (Unit.File_Names (Impl).Display_File),
553                                    "ci"));
554                         end if;
555                      end if;
556
557                   elsif Unit.File_Names (Spec) /= null
558                     and then not Unit.File_Names (Spec).Locally_Removed
559                   then
560                      --  Spec with no body, check if it is for this project
561
562                      if Check_Project
563                           (Unit.File_Names (Spec).Project, Project)
564                      then
565                         Last_Switches.Increment_Last;
566                         Last_Switches.Table (Last_Switches.Last) :=
567                           new String'
568                             (Get_Name_String
569                               (Unit.File_Names
570                                 (Spec).Project. Object_Directory.Name)     &
571                              Dir_Separator                                 &
572                              MLib.Fil.Ext_To
573                                (Get_Name_String (Unit.File_Names (Spec).File),
574                                 "ci"));
575                      end if;
576                   end if;
577
578                else
579                   --  For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
580                   --  sources of the project, or of all projects if -U was
581                   --  specified.
582
583                   for Kind in Spec_Or_Body loop
584                      if Unit.File_Names (Kind) /= null
585                        and then Check_Project
586                                   (Unit.File_Names (Kind).Project, Project)
587                        and then not Unit.File_Names (Kind).Locally_Removed
588                      then
589                         Name_Len := 0;
590                         Add_Char_To_Name_Buffer ('"');
591                         Add_Str_To_Name_Buffer
592                           (Get_Name_String
593                             (Unit.File_Names (Kind).Path.Display_Name));
594                         Add_Char_To_Name_Buffer ('"');
595
596                         if FD /= Invalid_FD then
597                            Name_Len := Name_Len + 1;
598                            Name_Buffer (Name_Len) := ASCII.LF;
599                            Status :=
600                              Write (FD, Name_Buffer (1)'Address, Name_Len);
601
602                            if Status /= Name_Len then
603                               Osint.Fail ("disk full");
604                            end if;
605
606                         else
607                            Last_Switches.Increment_Last;
608                            Last_Switches.Table (Last_Switches.Last) :=
609                              new String'(Get_Name_String
610                                           (Unit.File_Names
611                                             (Kind).Path.Display_Name));
612                         end if;
613                      end if;
614                   end loop;
615                end if;
616
617                Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
618             end loop;
619          end;
620
621          if FD /= Invalid_FD then
622             Close (FD, Success);
623
624             if not Success then
625                Osint.Fail ("disk full");
626             end if;
627          end if;
628       end if;
629    end Check_Files;
630
631    -------------------
632    -- Check_Project --
633    -------------------
634
635    function Check_Project
636      (Project      : Project_Id;
637       Root_Project : Project_Id) return Boolean
638    is
639       Proj : Project_Id;
640
641    begin
642       if Project = No_Project then
643          return False;
644
645       elsif All_Projects or else Project = Root_Project then
646          return True;
647
648       elsif The_Command = Metric then
649          Proj := Root_Project;
650          while Proj.Extends /= No_Project loop
651             if Project = Proj.Extends then
652                return True;
653             end if;
654
655             Proj := Proj.Extends;
656          end loop;
657       end if;
658
659       return False;
660    end Check_Project;
661
662    -------------------------------
663    -- Check_Relative_Executable --
664    -------------------------------
665
666    procedure Check_Relative_Executable (Name : in out String_Access) is
667       Exec_File_Name : constant String := Name.all;
668
669    begin
670       if not Is_Absolute_Path (Exec_File_Name) then
671          for Index in Exec_File_Name'Range loop
672             if Exec_File_Name (Index) = Directory_Separator then
673                Fail ("relative executable (""" &
674                        Exec_File_Name &
675                        """) with directory part not allowed " &
676                        "when using project files");
677             end if;
678          end loop;
679
680          Get_Name_String (Project.Exec_Directory.Name);
681
682          if Name_Buffer (Name_Len) /= Directory_Separator then
683             Name_Len := Name_Len + 1;
684             Name_Buffer (Name_Len) := Directory_Separator;
685          end if;
686
687          Name_Buffer (Name_Len + 1 ..
688                         Name_Len + Exec_File_Name'Length) :=
689            Exec_File_Name;
690          Name_Len := Name_Len + Exec_File_Name'Length;
691          Name := new String'(Name_Buffer (1 .. Name_Len));
692       end if;
693    end Check_Relative_Executable;
694
695    --------------------------------
696    -- Configuration_Pragmas_File --
697    --------------------------------
698
699    function Configuration_Pragmas_File return Path_Name_Type is
700    begin
701       Prj.Env.Create_Config_Pragmas_File (Project, Project_Tree);
702       return Project.Config_File_Name;
703    end Configuration_Pragmas_File;
704
705    ------------------------------
706    -- Delete_Temp_Config_Files --
707    ------------------------------
708
709    procedure Delete_Temp_Config_Files is
710       Success : Boolean;
711       Proj    : Project_List;
712       pragma Warnings (Off, Success);
713
714    begin
715       --  This should only be called if Keep_Temporary_Files is False
716
717       pragma Assert (not Keep_Temporary_Files);
718
719       if Project /= No_Project then
720          Proj := Project_Tree.Projects;
721          while Proj /= null loop
722             if Proj.Project.Config_File_Temp then
723                Delete_Temporary_File
724                  (Project_Tree, Proj.Project.Config_File_Name);
725             end if;
726
727             Proj := Proj.Next;
728          end loop;
729       end if;
730
731       --  If a temporary text file that contains a list of files for a tool
732       --  has been created, delete this temporary file.
733
734       if Temp_File_Name /= No_Path then
735          Delete_Temporary_File (Project_Tree, Temp_File_Name);
736       end if;
737    end Delete_Temp_Config_Files;
738
739    -----------------
740    -- Get_Closure --
741    -----------------
742
743    procedure Get_Closure is
744       Args : constant Argument_List :=
745                (1 => new String'("-q"),
746                 2 => new String'("-b"),
747                 3 => new String'("-P"),
748                 4 => Project_File,
749                 5 => ASIS_Main,
750                 6 => new String'("-bargs"),
751                 7 => new String'("-R"),
752                 8 => new String'("-Z"));
753       --  Arguments for the invocation of gnatmake which are added to the
754       --  Last_Arguments list by this procedure.
755
756       FD : File_Descriptor;
757       --  File descriptor for the temp file that will get the output of the
758       --  invocation of gnatmake.
759
760       Name : Path_Name_Type;
761       --  Path of the file FD
762
763       GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
764       --  Name for gnatmake
765
766       GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
767       --  Path of gnatmake
768
769       Return_Code : Integer;
770
771       Unused : Boolean;
772       pragma Warnings (Off, Unused);
773
774       File : Ada.Text_IO.File_Type;
775       Line : String (1 .. 250);
776       Last : Natural;
777       --  Used to read file if there is an error, it is good enough to display
778       --  just 250 characters if the first line of the file is very long.
779
780       Unit  : Unit_Index;
781       Path  : Path_Name_Type;
782
783    begin
784       if GN_Path = null then
785          Put_Line (Standard_Error, "could not locate " & GN_Name);
786          raise Error_Exit;
787       end if;
788
789       --  Create the temp file
790
791       Tempdir.Create_Temp_File (FD, Name);
792
793       --  And close it, because on VMS Spawn with a file descriptor created
794       --  with Create_Temp_File does not redirect output.
795
796       Close (FD);
797
798       --  Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
799
800       Spawn
801         (Program_Name => GN_Path.all,
802          Args         => Args,
803          Output_File  => Get_Name_String (Name),
804          Success      => Unused,
805          Return_Code  => Return_Code,
806          Err_To_Out   => True);
807
808       --  Read the output of the invocation of gnatmake
809
810       Open (File, In_File, Get_Name_String (Name));
811
812       --  If it was unsuccessful, display the first line in the file and exit
813       --  with error.
814
815       if Return_Code /= 0 then
816          Get_Line (File, Line, Last);
817
818          if not Keep_Temporary_Files then
819             Delete (File);
820          else
821             Close (File);
822          end if;
823
824          Put_Line (Standard_Error, Line (1 .. Last));
825          Put_Line
826            (Standard_Error, "could not get closure of " & ASIS_Main.all);
827          raise Error_Exit;
828
829       else
830          --  Get each file name in the file, find its path and add it the
831          --  list of arguments.
832
833          while not End_Of_File (File) loop
834             Get_Line (File, Line, Last);
835             Path := No_Path;
836
837             Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
838             while Unit /= No_Unit_Index loop
839                if Unit.File_Names (Spec) /= null
840                  and then
841                    Get_Name_String (Unit.File_Names (Spec).File) =
842                       Line (1 .. Last)
843                then
844                   Path := Unit.File_Names (Spec).Path.Name;
845                   exit;
846
847                elsif Unit.File_Names (Impl) /= null
848                  and then
849                    Get_Name_String (Unit.File_Names (Impl).File) =
850                      Line (1 .. Last)
851                then
852                   Path := Unit.File_Names (Impl).Path.Name;
853                   exit;
854                end if;
855
856                Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
857             end loop;
858
859             Last_Switches.Increment_Last;
860
861             if Path /= No_Path then
862                Last_Switches.Table (Last_Switches.Last) :=
863                   new String'(Get_Name_String (Path));
864
865             else
866                Last_Switches.Table (Last_Switches.Last) :=
867                  new String'(Line (1 .. Last));
868             end if;
869          end loop;
870
871          if not Keep_Temporary_Files then
872             Delete (File);
873          else
874             Close (File);
875          end if;
876       end if;
877    end Get_Closure;
878
879    -----------
880    -- Index --
881    -----------
882
883    function Index (Char : Character; Str : String) return Natural is
884    begin
885       for Index in Str'Range loop
886          if Str (Index) = Char then
887             return Index;
888          end if;
889       end loop;
890
891       return 0;
892    end Index;
893
894    ------------------
895    -- Mapping_File --
896    ------------------
897
898    function Mapping_File return Path_Name_Type is
899       Result : Path_Name_Type;
900    begin
901       Prj.Env.Create_Mapping_File
902         (Project  => Project,
903          Language => Name_Ada,
904          In_Tree  => Project_Tree,
905          Name     => Result);
906       return Result;
907    end Mapping_File;
908
909    ------------------
910    -- Process_Link --
911    ------------------
912
913    procedure Process_Link is
914       Look_For_Executable : Boolean := True;
915       Libraries_Present   : Boolean := False;
916       Path_Option         : constant String_Access :=
917                               MLib.Linker_Library_Path_Option;
918       Prj                 : Project_Id := Project;
919       Arg                 : String_Access;
920       Last                : Natural := 0;
921       Skip_Executable     : Boolean := False;
922
923    begin
924       --  Add the default search directories, to be able to find
925       --  libgnat in call to MLib.Utl.Lib_Directory.
926
927       Add_Default_Search_Dirs;
928
929       Library_Paths.Set_Last (0);
930
931       --  Check if there are library project files
932
933       if MLib.Tgt.Support_For_Libraries /= None then
934          Set_Libraries (Project, Libraries_Present);
935       end if;
936
937       --  If there are, add the necessary additional switches
938
939       if Libraries_Present then
940
941          --  Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
942
943          Last_Switches.Increment_Last;
944          Last_Switches.Table (Last_Switches.Last) :=
945            new String'("-L" & MLib.Utl.Lib_Directory);
946          Last_Switches.Increment_Last;
947          Last_Switches.Table (Last_Switches.Last) :=
948            new String'("-lgnarl");
949          Last_Switches.Increment_Last;
950          Last_Switches.Table (Last_Switches.Last) :=
951            new String'("-lgnat");
952
953          --  If Path_Option is not null, create the switch ("-Wl,-rpath," or
954          --  equivalent) with all the library dirs plus the standard GNAT
955          --  library dir.
956
957          if Path_Option /= null then
958             declare
959                Option  : String_Access;
960                Length  : Natural := Path_Option'Length;
961                Current : Natural;
962
963             begin
964                if MLib.Separate_Run_Path_Options then
965
966                   --  We are going to create one switch of the form
967                   --  "-Wl,-rpath,dir_N" for each directory to consider.
968
969                   --  One switch for each library directory
970
971                   for Index in
972                     Library_Paths.First .. Library_Paths.Last
973                   loop
974                      Last_Switches.Increment_Last;
975                      Last_Switches.Table
976                        (Last_Switches.Last) := new String'
977                        (Path_Option.all &
978                         Last_Switches.Table (Index).all);
979                   end loop;
980
981                   --  One switch for the standard GNAT library dir
982
983                   Last_Switches.Increment_Last;
984                   Last_Switches.Table
985                     (Last_Switches.Last) := new String'
986                     (Path_Option.all & MLib.Utl.Lib_Directory);
987
988                else
989                   --  First, compute the exact length for the switch
990
991                   for Index in
992                     Library_Paths.First .. Library_Paths.Last
993                   loop
994                      --  Add the length of the library dir plus one for the
995                      --  directory separator.
996
997                      Length :=
998                        Length +
999                          Library_Paths.Table (Index)'Length + 1;
1000                   end loop;
1001
1002                   --  Finally, add the length of the standard GNAT library dir
1003
1004                   Length := Length + MLib.Utl.Lib_Directory'Length;
1005                   Option := new String (1 .. Length);
1006                   Option (1 .. Path_Option'Length) := Path_Option.all;
1007                   Current := Path_Option'Length;
1008
1009                   --  Put each library dir followed by a dir separator
1010
1011                   for Index in
1012                     Library_Paths.First .. Library_Paths.Last
1013                   loop
1014                      Option
1015                        (Current + 1 ..
1016                           Current +
1017                             Library_Paths.Table (Index)'Length) :=
1018                        Library_Paths.Table (Index).all;
1019                      Current :=
1020                        Current +
1021                          Library_Paths.Table (Index)'Length + 1;
1022                      Option (Current) := Path_Separator;
1023                   end loop;
1024
1025                   --  Finally put the standard GNAT library dir
1026
1027                   Option
1028                     (Current + 1 ..
1029                        Current + MLib.Utl.Lib_Directory'Length) :=
1030                       MLib.Utl.Lib_Directory;
1031
1032                   --  And add the switch to the last switches
1033
1034                   Last_Switches.Increment_Last;
1035                   Last_Switches.Table (Last_Switches.Last) :=
1036                     Option;
1037                end if;
1038             end;
1039          end if;
1040       end if;
1041
1042       --  Check if the first ALI file specified can be found, either in the
1043       --  object directory of the main project or in an object directory of a
1044       --  project file extended by the main project. If the ALI file can be
1045       --  found, replace its name with its absolute path.
1046
1047       Skip_Executable := False;
1048
1049       Switch_Loop : for J in 1 .. Last_Switches.Last loop
1050
1051          --  If we have an executable just reset the flag
1052
1053          if Skip_Executable then
1054             Skip_Executable := False;
1055
1056          --  If -o, set flag so that next switch is not processed
1057
1058          elsif Last_Switches.Table (J).all = "-o" then
1059             Skip_Executable := True;
1060
1061          --  Normal case
1062
1063          else
1064             declare
1065                Switch    : constant String :=
1066                              Last_Switches.Table (J).all;
1067                ALI_File  : constant String (1 .. Switch'Length + 4) :=
1068                              Switch & ".ali";
1069
1070                Test_Existence : Boolean := False;
1071
1072             begin
1073                Last := Switch'Length;
1074
1075                --  Skip real switches
1076
1077                if Switch'Length /= 0
1078                  and then Switch (Switch'First) /= '-'
1079                then
1080                   --  Append ".ali" if file name does not end with it
1081
1082                   if Switch'Length <= 4
1083                     or else Switch (Switch'Last - 3 .. Switch'Last) /= ".ali"
1084                   then
1085                      Last := ALI_File'Last;
1086                   end if;
1087
1088                   --  If file name includes directory information, stop if ALI
1089                   --  file exists.
1090
1091                   if Is_Absolute_Path (ALI_File (1 .. Last)) then
1092                      Test_Existence := True;
1093
1094                   else
1095                      for K in Switch'Range loop
1096                         if Switch (K) = '/'
1097                           or else Switch (K) = Directory_Separator
1098                         then
1099                            Test_Existence := True;
1100                            exit;
1101                         end if;
1102                      end loop;
1103                   end if;
1104
1105                   if Test_Existence then
1106                      if Is_Regular_File (ALI_File (1 .. Last)) then
1107                         exit Switch_Loop;
1108                      end if;
1109
1110                   --  Look in object directories if ALI file exists
1111
1112                   else
1113                      Project_Loop : loop
1114                         declare
1115                            Dir : constant String :=
1116                                    Get_Name_String (Prj.Object_Directory.Name);
1117                         begin
1118                            if Is_Regular_File
1119                                 (Dir &
1120                                  ALI_File (1 .. Last))
1121                            then
1122                               --  We have found the correct project, so we
1123                               --  replace the file with the absolute path.
1124
1125                               Last_Switches.Table (J) :=
1126                                 new String'(Dir & ALI_File (1 .. Last));
1127
1128                               --  And we are done
1129
1130                               exit Switch_Loop;
1131                            end if;
1132                         end;
1133
1134                         --  Go to the project being extended, if any
1135
1136                         Prj := Prj.Extends;
1137                         exit Project_Loop when Prj = No_Project;
1138                      end loop Project_Loop;
1139                   end if;
1140                end if;
1141             end;
1142          end if;
1143       end loop Switch_Loop;
1144
1145       --  If a relative path output file has been specified, we add the exec
1146       --  directory.
1147
1148       for J in reverse 1 .. Last_Switches.Last - 1 loop
1149          if Last_Switches.Table (J).all = "-o" then
1150             Check_Relative_Executable
1151               (Name => Last_Switches.Table (J + 1));
1152             Look_For_Executable := False;
1153             exit;
1154          end if;
1155       end loop;
1156
1157       if Look_For_Executable then
1158          for J in reverse 1 .. First_Switches.Last - 1 loop
1159             if First_Switches.Table (J).all = "-o" then
1160                Look_For_Executable := False;
1161                Check_Relative_Executable
1162                  (Name => First_Switches.Table (J + 1));
1163                exit;
1164             end if;
1165          end loop;
1166       end if;
1167
1168       --  If no executable is specified, then find the name of the first ALI
1169       --  file on the command line and issue a -o switch with the absolute path
1170       --  of the executable in the exec directory.
1171
1172       if Look_For_Executable then
1173          for J in 1 .. Last_Switches.Last loop
1174             Arg  := Last_Switches.Table (J);
1175             Last := 0;
1176
1177             if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1178                if Arg'Length > 4
1179                  and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1180                then
1181                   Last := Arg'Last - 4;
1182
1183                elsif Is_Regular_File (Arg.all & ".ali") then
1184                   Last := Arg'Last;
1185                end if;
1186
1187                if Last /= 0 then
1188                   Last_Switches.Increment_Last;
1189                   Last_Switches.Table (Last_Switches.Last) :=
1190                     new String'("-o");
1191                   Get_Name_String (Project.Exec_Directory.Name);
1192                   Last_Switches.Increment_Last;
1193                   Last_Switches.Table (Last_Switches.Last) :=
1194                     new String'(Name_Buffer (1 .. Name_Len) &
1195                                 Executable_Name
1196                                   (Base_Name (Arg (Arg'First .. Last))));
1197                   exit;
1198                end if;
1199             end if;
1200          end loop;
1201       end if;
1202    end Process_Link;
1203
1204    ---------------------
1205    -- Set_Library_For --
1206    ---------------------
1207
1208    procedure Set_Library_For
1209      (Project           : Project_Id;
1210       Libraries_Present : in out Boolean)
1211    is
1212       Path_Option : constant String_Access :=
1213                       MLib.Linker_Library_Path_Option;
1214
1215    begin
1216       --  Case of library project
1217
1218       if Project.Library then
1219          Libraries_Present := True;
1220
1221          --  Add the -L switch
1222
1223          Last_Switches.Increment_Last;
1224          Last_Switches.Table (Last_Switches.Last) :=
1225            new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1226
1227          --  Add the -l switch
1228
1229          Last_Switches.Increment_Last;
1230          Last_Switches.Table (Last_Switches.Last) :=
1231            new String'("-l" & Get_Name_String (Project.Library_Name));
1232
1233          --  Add the directory to table Library_Paths, to be processed later
1234          --  if library is not static and if Path_Option is not null.
1235
1236          if Project.Library_Kind /= Static
1237            and then Path_Option /= null
1238          then
1239             Library_Paths.Increment_Last;
1240             Library_Paths.Table (Library_Paths.Last) :=
1241               new String'(Get_Name_String (Project.Library_Dir.Name));
1242          end if;
1243       end if;
1244    end Set_Library_For;
1245
1246    ---------------------------
1247    -- Test_If_Relative_Path --
1248    ---------------------------
1249
1250    procedure Test_If_Relative_Path
1251      (Switch : in out String_Access;
1252       Parent : String)
1253    is
1254    begin
1255       Makeutl.Test_If_Relative_Path
1256         (Switch, Parent, Including_Non_Switch => False, Including_RTS => True);
1257    end Test_If_Relative_Path;
1258
1259    -------------------
1260    -- Non_VMS_Usage --
1261    -------------------
1262
1263    procedure Non_VMS_Usage is
1264    begin
1265       Output_Version;
1266       New_Line;
1267       Put_Line ("List of available commands");
1268       New_Line;
1269
1270       for C in Command_List'Range loop
1271
1272          --  No usage for VMS only command or for Sync
1273
1274          if not Command_List (C).VMS_Only and then C /= Sync then
1275             if Targparm.AAMP_On_Target then
1276                Put ("gnaampcmd ");
1277             else
1278                Put ("gnat ");
1279             end if;
1280
1281             Put (To_Lower (Command_List (C).Cname.all));
1282             Set_Col (25);
1283
1284             --  Never call gnatstack with a prefix
1285
1286             if C = Stack then
1287                Put (Command_List (C).Unixcmd.all);
1288             else
1289                Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1290             end if;
1291
1292             declare
1293                Sws : Argument_List_Access renames Command_List (C).Unixsws;
1294             begin
1295                if Sws /= null then
1296                   for J in Sws'Range loop
1297                      Put (' ');
1298                      Put (Sws (J).all);
1299                   end loop;
1300                end if;
1301             end;
1302
1303             New_Line;
1304          end if;
1305       end loop;
1306
1307       New_Line;
1308       Put_Line ("All commands except chop, krunch and preprocess " &
1309                 "accept project file switches -vPx, -Pprj and -Xnam=val");
1310       New_Line;
1311    end Non_VMS_Usage;
1312
1313    -------------------------------------
1314    -- Start of processing for GNATCmd --
1315    -------------------------------------
1316
1317 begin
1318    --  Initializations
1319
1320    Csets.Initialize;
1321    Snames.Initialize;
1322
1323    Project_Node_Tree := new Project_Node_Tree_Data;
1324    Prj.Tree.Initialize (Project_Node_Tree);
1325
1326    Prj.Initialize (Project_Tree);
1327
1328    Last_Switches.Init;
1329    Last_Switches.Set_Last (0);
1330
1331    First_Switches.Init;
1332    First_Switches.Set_Last (0);
1333    Carg_Switches.Init;
1334    Carg_Switches.Set_Last (0);
1335    Rules_Switches.Init;
1336    Rules_Switches.Set_Last (0);
1337
1338    VMS_Conv.Initialize;
1339
1340    --  Add the default search directories, to be able to find system.ads in the
1341    --  subsequent call to Targparm.Get_Target_Parameters.
1342
1343    Add_Default_Search_Dirs;
1344
1345    --  Get target parameters so that AAMP_On_Target will be set, for testing in
1346    --  Osint.Program_Name to handle the mapping of GNAAMP tool names.
1347
1348    Targparm.Get_Target_Parameters;
1349
1350    --  Put the command line in environment variable GNAT_DRIVER_COMMAND_LINE,
1351    --  so that the spawned tool may know the way the GNAT driver was invoked.
1352
1353    Name_Len := 0;
1354    Add_Str_To_Name_Buffer (Command_Name);
1355
1356    for J in 1 .. Argument_Count loop
1357       Add_Char_To_Name_Buffer (' ');
1358       Add_Str_To_Name_Buffer (Argument (J));
1359    end loop;
1360
1361    Setenv ("GNAT_DRIVER_COMMAND_LINE", Name_Buffer (1 .. Name_Len));
1362
1363    --  Add the directory where the GNAT driver is invoked in front of the path,
1364    --  if the GNAT driver is invoked with directory information. Do not do this
1365    --  for VMS, where the notion of path does not really exist.
1366
1367    if not OpenVMS then
1368       declare
1369          Command : constant String := Command_Name;
1370
1371       begin
1372          for Index in reverse Command'Range loop
1373             if Command (Index) = Directory_Separator then
1374                declare
1375                   Absolute_Dir : constant String :=
1376                                    Normalize_Pathname
1377                                      (Command (Command'First .. Index));
1378
1379                   PATH : constant String :=
1380                            Absolute_Dir & Path_Separator & Getenv ("PATH").all;
1381
1382                begin
1383                   Setenv ("PATH", PATH);
1384                end;
1385
1386                exit;
1387             end if;
1388          end loop;
1389       end;
1390    end if;
1391
1392    --  If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1393    --  filenames and pathnames to Unix style.
1394
1395    if Hostparm.OpenVMS
1396      or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1397    then
1398       VMS_Conversion (The_Command);
1399
1400       B_Start := new String'("b__");
1401
1402    --  If not on VMS, scan the command line directly
1403
1404    else
1405       if Argument_Count = 0 then
1406          Non_VMS_Usage;
1407          return;
1408       else
1409          begin
1410             loop
1411                if Argument_Count > Command_Arg
1412                  and then Argument (Command_Arg) = "-v"
1413                then
1414                   Verbose_Mode := True;
1415                   Command_Arg := Command_Arg + 1;
1416
1417                elsif Argument_Count > Command_Arg
1418                  and then Argument (Command_Arg) = "-dn"
1419                then
1420                   Keep_Temporary_Files := True;
1421                   Command_Arg := Command_Arg + 1;
1422
1423                else
1424                   exit;
1425                end if;
1426             end loop;
1427
1428             The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1429
1430             if Command_List (The_Command).VMS_Only then
1431                Non_VMS_Usage;
1432                Fail
1433                  ("Command """
1434                   & Command_List (The_Command).Cname.all
1435                   & """ can only be used on VMS");
1436             end if;
1437
1438          exception
1439             when Constraint_Error =>
1440
1441                --  Check if it is an alternate command
1442
1443                declare
1444                   Alternate : Alternate_Command;
1445
1446                begin
1447                   Alternate := Alternate_Command'Value
1448                                               (Argument (Command_Arg));
1449                   The_Command := Corresponding_To (Alternate);
1450
1451                exception
1452                   when Constraint_Error =>
1453                      Non_VMS_Usage;
1454                      Fail ("Unknown command: " & Argument (Command_Arg));
1455                end;
1456          end;
1457
1458          --  Get the arguments from the command line and from the eventual
1459          --  argument file(s) specified on the command line.
1460
1461          for Arg in Command_Arg + 1 .. Argument_Count loop
1462             declare
1463                The_Arg : constant String := Argument (Arg);
1464
1465             begin
1466                --  Check if an argument file is specified
1467
1468                if The_Arg (The_Arg'First) = '@' then
1469                   declare
1470                      Arg_File : Ada.Text_IO.File_Type;
1471                      Line     : String (1 .. 256);
1472                      Last     : Natural;
1473
1474                   begin
1475                      --  Open the file and fail if the file cannot be found
1476
1477                      begin
1478                         Open
1479                           (Arg_File, In_File,
1480                            The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1481
1482                      exception
1483                         when others =>
1484                            Put
1485                              (Standard_Error, "Cannot open argument file """);
1486                            Put
1487                              (Standard_Error,
1488                               The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1489
1490                            Put_Line (Standard_Error, """");
1491                            raise Error_Exit;
1492                      end;
1493
1494                      --  Read line by line and put the content of each non-
1495                      --  empty line in the Last_Switches table.
1496
1497                      while not End_Of_File (Arg_File) loop
1498                         Get_Line (Arg_File, Line, Last);
1499
1500                         if Last /= 0 then
1501                            Last_Switches.Increment_Last;
1502                            Last_Switches.Table (Last_Switches.Last) :=
1503                              new String'(Line (1 .. Last));
1504                         end if;
1505                      end loop;
1506
1507                      Close (Arg_File);
1508                   end;
1509
1510                else
1511                   --  It is not an argument file; just put the argument in
1512                   --  the Last_Switches table.
1513
1514                   Last_Switches.Increment_Last;
1515                   Last_Switches.Table (Last_Switches.Last) :=
1516                     new String'(The_Arg);
1517                end if;
1518             end;
1519          end loop;
1520       end if;
1521    end if;
1522
1523    declare
1524       Program   : String_Access;
1525       Exec_Path : String_Access;
1526
1527    begin
1528       if The_Command = Stack then
1529
1530          --  Never call gnatstack with a prefix
1531
1532          Program := new String'(Command_List (The_Command).Unixcmd.all);
1533
1534       else
1535          Program :=
1536            Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1537       end if;
1538
1539       --  Locate the executable for the command
1540
1541       Exec_Path := Locate_Exec_On_Path (Program.all);
1542
1543       if Exec_Path = null then
1544          Put_Line (Standard_Error, "could not locate " & Program.all);
1545          raise Error_Exit;
1546       end if;
1547
1548       --  If there are switches for the executable, put them as first switches
1549
1550       if Command_List (The_Command).Unixsws /= null then
1551          for J in Command_List (The_Command).Unixsws'Range loop
1552             First_Switches.Increment_Last;
1553             First_Switches.Table (First_Switches.Last) :=
1554               Command_List (The_Command).Unixsws (J);
1555          end loop;
1556       end if;
1557
1558       --  For BIND, CHECK, ELIM, FIND, LINK, LIST, METRIC, PRETTY, STACK, STUB,
1559       --  SYNC and XREF, look for project file related switches.
1560
1561       case The_Command is
1562          when Bind =>
1563             Tool_Package_Name := Name_Binder;
1564             Packages_To_Check := Packages_To_Check_By_Binder;
1565          when Check =>
1566             Tool_Package_Name := Name_Check;
1567             Packages_To_Check := Packages_To_Check_By_Check;
1568          when Elim =>
1569             Tool_Package_Name := Name_Eliminate;
1570             Packages_To_Check := Packages_To_Check_By_Eliminate;
1571          when Find =>
1572             Tool_Package_Name := Name_Finder;
1573             Packages_To_Check := Packages_To_Check_By_Finder;
1574          when Link =>
1575             Tool_Package_Name := Name_Linker;
1576             Packages_To_Check := Packages_To_Check_By_Linker;
1577          when List =>
1578             Tool_Package_Name := Name_Gnatls;
1579             Packages_To_Check := Packages_To_Check_By_Gnatls;
1580          when Metric =>
1581             Tool_Package_Name := Name_Metrics;
1582             Packages_To_Check := Packages_To_Check_By_Metric;
1583          when Pretty =>
1584             Tool_Package_Name := Name_Pretty_Printer;
1585             Packages_To_Check := Packages_To_Check_By_Pretty;
1586          when Stack =>
1587             Tool_Package_Name := Name_Stack;
1588             Packages_To_Check := Packages_To_Check_By_Stack;
1589          when Stub =>
1590             Tool_Package_Name := Name_Gnatstub;
1591             Packages_To_Check := Packages_To_Check_By_Gnatstub;
1592          when Sync =>
1593             Tool_Package_Name := Name_Synchronize;
1594             Packages_To_Check := Packages_To_Check_By_Sync;
1595          when Xref =>
1596             Tool_Package_Name := Name_Cross_Reference;
1597             Packages_To_Check := Packages_To_Check_By_Xref;
1598          when others =>
1599             Tool_Package_Name := No_Name;
1600       end case;
1601
1602       if Tool_Package_Name /= No_Name then
1603
1604          --  Check that the switches are consistent. Detect project file
1605          --  related switches.
1606
1607          Inspect_Switches : declare
1608             Arg_Num : Positive := 1;
1609             Argv    : String_Access;
1610
1611             procedure Remove_Switch (Num : Positive);
1612             --  Remove a project related switch from table Last_Switches
1613
1614             -------------------
1615             -- Remove_Switch --
1616             -------------------
1617
1618             procedure Remove_Switch (Num : Positive) is
1619             begin
1620                Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1621                  Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1622                Last_Switches.Decrement_Last;
1623             end Remove_Switch;
1624
1625          --  Start of processing for Inspect_Switches
1626
1627          begin
1628             while Arg_Num <= Last_Switches.Last loop
1629                Argv := Last_Switches.Table (Arg_Num);
1630
1631                if Argv (Argv'First) = '-' then
1632                   if Argv'Length = 1 then
1633                      Fail
1634                        ("switch character cannot be followed by a blank");
1635                   end if;
1636
1637                   --  The two style project files (-p and -P) cannot be used
1638                   --  together
1639
1640                   if (The_Command = Find or else The_Command = Xref)
1641                     and then Argv (2) = 'p'
1642                   then
1643                      Old_Project_File_Used := True;
1644                      if Project_File /= null then
1645                         Fail ("-P and -p cannot be used together");
1646                      end if;
1647                   end if;
1648
1649                   --  --subdirs=... Specify Subdirs
1650
1651                   if Argv'Length > Makeutl.Subdirs_Option'Length
1652                     and then
1653                       Argv
1654                        (Argv'First ..
1655                         Argv'First + Makeutl.Subdirs_Option'Length - 1) =
1656                           Makeutl.Subdirs_Option
1657                   then
1658                      Subdirs :=
1659                        new String'
1660                          (Argv
1661                            (Argv'First + Makeutl.Subdirs_Option'Length ..
1662                             Argv'Last));
1663
1664                      Remove_Switch (Arg_Num);
1665
1666                   --  -aPdir  Add dir to the project search path
1667
1668                   elsif Argv'Length > 3
1669                     and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1670                   then
1671                      Add_Search_Project_Directory
1672                        (Project_Node_Tree, Argv (Argv'First + 3 .. Argv'Last));
1673
1674                      Remove_Switch (Arg_Num);
1675
1676                   --  -eL  Follow links for files
1677
1678                   elsif Argv.all = "-eL" then
1679                      Follow_Links_For_Files := True;
1680                      Follow_Links_For_Dirs  := True;
1681
1682                      Remove_Switch (Arg_Num);
1683
1684                   --  -vPx  Specify verbosity while parsing project files
1685
1686                   elsif Argv'Length = 4
1687                     and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1688                   then
1689                      case Argv (Argv'Last) is
1690                         when '0' =>
1691                            Current_Verbosity := Prj.Default;
1692                         when '1' =>
1693                            Current_Verbosity := Prj.Medium;
1694                         when '2' =>
1695                            Current_Verbosity := Prj.High;
1696                         when others =>
1697                            Fail ("Invalid switch: " & Argv.all);
1698                      end case;
1699
1700                      Remove_Switch (Arg_Num);
1701
1702                   --  -Pproject_file  Specify project file to be used
1703
1704                   elsif Argv (Argv'First + 1) = 'P' then
1705
1706                      --  Only one -P switch can be used
1707
1708                      if Project_File /= null then
1709                         Fail
1710                           (Argv.all
1711                            & ": second project file forbidden (first is """
1712                            & Project_File.all
1713                            & """)");
1714
1715                      --  The two style project files (-p and -P) cannot be
1716                      --  used together.
1717
1718                      elsif Old_Project_File_Used then
1719                         Fail ("-p and -P cannot be used together");
1720
1721                      elsif Argv'Length = 2 then
1722
1723                         --  There is space between -P and the project file
1724                         --  name. -P cannot be the last option.
1725
1726                         if Arg_Num = Last_Switches.Last then
1727                            Fail ("project file name missing after -P");
1728
1729                         else
1730                            Remove_Switch (Arg_Num);
1731                            Argv := Last_Switches.Table (Arg_Num);
1732
1733                            --  After -P, there must be a project file name,
1734                            --  not another switch.
1735
1736                            if Argv (Argv'First) = '-' then
1737                               Fail ("project file name missing after -P");
1738
1739                            else
1740                               Project_File := new String'(Argv.all);
1741                            end if;
1742                         end if;
1743
1744                      else
1745                         --  No space between -P and project file name
1746
1747                         Project_File :=
1748                           new String'(Argv (Argv'First + 2 .. Argv'Last));
1749                      end if;
1750
1751                      Remove_Switch (Arg_Num);
1752
1753                   --  -Xexternal=value Specify an external reference to be
1754                   --                   used in project files
1755
1756                   elsif Argv'Length >= 5
1757                     and then Argv (Argv'First + 1) = 'X'
1758                   then
1759                      declare
1760                         Equal_Pos : constant Natural :=
1761                                       Index
1762                                         ('=',
1763                                          Argv (Argv'First + 2 .. Argv'Last));
1764                      begin
1765                         if Equal_Pos >= Argv'First + 3
1766                           and then Equal_Pos /= Argv'Last
1767                         then
1768                            Add (Project_Node_Tree,
1769                                 External_Name =>
1770                                   Argv (Argv'First + 2 .. Equal_Pos - 1),
1771                                 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1772                         else
1773                            Fail
1774                              (Argv.all
1775                               & " is not a valid external assignment.");
1776                         end if;
1777                      end;
1778
1779                      Remove_Switch (Arg_Num);
1780
1781                   elsif
1782                     (The_Command = Check  or else
1783                      The_Command = Sync   or else
1784                      The_Command = Pretty or else
1785                      The_Command = Metric or else
1786                      The_Command = Stack  or else
1787                      The_Command = List)
1788                     and then Argv'Length = 2
1789                     and then Argv (2) = 'U'
1790                   then
1791                      All_Projects := True;
1792                      Remove_Switch (Arg_Num);
1793
1794                   else
1795                      Arg_Num := Arg_Num + 1;
1796                   end if;
1797
1798                elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1799                         or else The_Command = Sync
1800                         or else The_Command = Metric
1801                         or else The_Command = Pretty)
1802                  and then Project_File /= null
1803                  and then All_Projects
1804                then
1805                   if ASIS_Main /= null then
1806                      Fail ("cannot specify more than one main after -U");
1807                   else
1808                      ASIS_Main := Argv;
1809                      Remove_Switch (Arg_Num);
1810                   end if;
1811
1812                else
1813                   Arg_Num := Arg_Num + 1;
1814                end if;
1815             end loop;
1816          end Inspect_Switches;
1817       end if;
1818
1819       --  If there is a project file specified, parse it, get the switches
1820       --  for the tool and setup PATH environment variables.
1821
1822       if Project_File /= null then
1823          Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1824
1825          Prj.Pars.Parse
1826            (Project           => Project,
1827             In_Tree           => Project_Tree,
1828             In_Node_Tree      => Project_Node_Tree,
1829             Project_File_Name => Project_File.all,
1830             Flags             => Gnatmake_Flags,
1831             Packages_To_Check => Packages_To_Check);
1832
1833          if Project = Prj.No_Project then
1834             Fail ("""" & Project_File.all & """ processing failed");
1835          end if;
1836
1837          --  Check if a package with the name of the tool is in the project
1838          --  file and if there is one, get the switches, if any, and scan them.
1839
1840          declare
1841             Pkg : constant Prj.Package_Id :=
1842                     Prj.Util.Value_Of
1843                       (Name        => Tool_Package_Name,
1844                        In_Packages => Project.Decl.Packages,
1845                        In_Tree     => Project_Tree);
1846
1847             Element : Package_Element;
1848
1849             Switches_Array : Array_Element_Id;
1850
1851             The_Switches : Prj.Variable_Value;
1852             Current      : Prj.String_List_Id;
1853             The_String   : String_Element;
1854
1855             Main : String_Access := null;
1856
1857          begin
1858             if Pkg /= No_Package then
1859                Element := Project_Tree.Packages.Table (Pkg);
1860
1861                --  Packages Gnatls and Gnatstack have a single attribute
1862                --  Switches, that is not an associative array.
1863
1864                if The_Command = List or else The_Command = Stack then
1865                   The_Switches :=
1866                     Prj.Util.Value_Of
1867                     (Variable_Name => Snames.Name_Switches,
1868                      In_Variables  => Element.Decl.Attributes,
1869                      In_Tree       => Project_Tree);
1870
1871                --  Packages Binder (for gnatbind), Cross_Reference (for
1872                --  gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1873                --  Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1874                --  (for gnatcheck), and Metric (for gnatmetric) have an
1875                --  attributed Switches, an associative array, indexed by the
1876                --  name of the file.
1877
1878                --  They also have an attribute Default_Switches, indexed by the
1879                --  name of the programming language.
1880
1881                else
1882                   --  First check if there is a single main
1883
1884                   for J in 1 .. Last_Switches.Last loop
1885                      if Last_Switches.Table (J) (1) /= '-' then
1886                         if Main = null then
1887                            Main := Last_Switches.Table (J);
1888
1889                         else
1890                            Main := null;
1891                            exit;
1892                         end if;
1893                      end if;
1894                   end loop;
1895
1896                   if Main /= null then
1897                      Switches_Array :=
1898                        Prj.Util.Value_Of
1899                          (Name      => Name_Switches,
1900                           In_Arrays => Element.Decl.Arrays,
1901                           In_Tree   => Project_Tree);
1902                      Name_Len := 0;
1903                      Add_Str_To_Name_Buffer (Main.all);
1904                      The_Switches := Prj.Util.Value_Of
1905                        (Index     => Name_Find,
1906                         Src_Index => 0,
1907                         In_Array  => Switches_Array,
1908                         In_Tree   => Project_Tree);
1909                   end if;
1910
1911                   if The_Switches.Kind = Prj.Undefined then
1912                      Switches_Array :=
1913                        Prj.Util.Value_Of
1914                          (Name      => Name_Default_Switches,
1915                           In_Arrays => Element.Decl.Arrays,
1916                           In_Tree   => Project_Tree);
1917                      The_Switches := Prj.Util.Value_Of
1918                        (Index     => Name_Ada,
1919                         Src_Index => 0,
1920                         In_Array  => Switches_Array,
1921                         In_Tree   => Project_Tree);
1922                   end if;
1923                end if;
1924
1925                --  If there are switches specified in the package of the
1926                --  project file corresponding to the tool, scan them.
1927
1928                case The_Switches.Kind is
1929                   when Prj.Undefined =>
1930                      null;
1931
1932                   when Prj.Single =>
1933                      declare
1934                         Switch : constant String :=
1935                                    Get_Name_String (The_Switches.Value);
1936
1937                      begin
1938                         if Switch'Length > 0 then
1939                            First_Switches.Increment_Last;
1940                            First_Switches.Table (First_Switches.Last) :=
1941                              new String'(Switch);
1942                         end if;
1943                      end;
1944
1945                   when Prj.List =>
1946                      Current := The_Switches.Values;
1947                      while Current /= Prj.Nil_String loop
1948                         The_String := Project_Tree.String_Elements.
1949                                         Table (Current);
1950
1951                         declare
1952                            Switch : constant String :=
1953                              Get_Name_String (The_String.Value);
1954
1955                         begin
1956                            if Switch'Length > 0 then
1957                               First_Switches.Increment_Last;
1958                               First_Switches.Table (First_Switches.Last) :=
1959                                 new String'(Switch);
1960                            end if;
1961                         end;
1962
1963                         Current := The_String.Next;
1964                      end loop;
1965                end case;
1966             end if;
1967          end;
1968
1969          if        The_Command = Bind
1970            or else The_Command = Link
1971            or else The_Command = Elim
1972          then
1973             Change_Dir (Get_Name_String (Project.Object_Directory.Name));
1974          end if;
1975
1976          --  Set up the env vars for project path files
1977
1978          Prj.Env.Set_Ada_Paths
1979            (Project, Project_Tree, Including_Libraries => False);
1980
1981          --  For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
1982          --  a configuration pragmas file, if necessary.
1983
1984          if        The_Command = Pretty
1985            or else The_Command = Metric
1986            or else The_Command = Stub
1987            or else The_Command = Elim
1988            or else The_Command = Check
1989            or else The_Command = Sync
1990          then
1991             --  If there are switches in package Compiler, put them in the
1992             --  Carg_Switches table.
1993
1994             declare
1995                Pkg  : constant Prj.Package_Id :=
1996                         Prj.Util.Value_Of
1997                           (Name        => Name_Compiler,
1998                            In_Packages => Project.Decl.Packages,
1999                            In_Tree     => Project_Tree);
2000
2001                Element : Package_Element;
2002
2003                Switches_Array : Array_Element_Id;
2004
2005                The_Switches : Prj.Variable_Value;
2006                Current      : Prj.String_List_Id;
2007                The_String   : String_Element;
2008
2009                Main    : String_Access := null;
2010                Main_Id : Name_Id;
2011
2012             begin
2013                if Pkg /= No_Package then
2014
2015                   --  First, check if there is a single main specified.
2016
2017                   for J in 1  .. Last_Switches.Last loop
2018                      if Last_Switches.Table (J) (1) /= '-' then
2019                         if Main = null then
2020                            Main := Last_Switches.Table (J);
2021
2022                         else
2023                            Main := null;
2024                            exit;
2025                         end if;
2026                      end if;
2027                   end loop;
2028
2029                   Element := Project_Tree.Packages.Table (Pkg);
2030
2031                   --  If there is a single main and there is compilation
2032                   --  switches specified in the project file, use them.
2033
2034                   if Main /= null and then not All_Projects then
2035                      Name_Len := Main'Length;
2036                      Name_Buffer (1 .. Name_Len) := Main.all;
2037                      Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2038                      Main_Id := Name_Find;
2039
2040                      Switches_Array :=
2041                        Prj.Util.Value_Of
2042                          (Name      => Name_Switches,
2043                           In_Arrays => Element.Decl.Arrays,
2044                           In_Tree   => Project_Tree);
2045                      The_Switches := Prj.Util.Value_Of
2046                        (Index     => Main_Id,
2047                         Src_Index => 0,
2048                         In_Array  => Switches_Array,
2049                         In_Tree   => Project_Tree);
2050                   end if;
2051
2052                   --  Otherwise, get the Default_Switches ("Ada")
2053
2054                   if The_Switches.Kind = Undefined then
2055                      Switches_Array :=
2056                        Prj.Util.Value_Of
2057                          (Name      => Name_Default_Switches,
2058                           In_Arrays => Element.Decl.Arrays,
2059                           In_Tree   => Project_Tree);
2060                      The_Switches := Prj.Util.Value_Of
2061                        (Index     => Name_Ada,
2062                         Src_Index => 0,
2063                         In_Array  => Switches_Array,
2064                         In_Tree   => Project_Tree);
2065                   end if;
2066
2067                   --  If there are switches specified, put them in the
2068                   --  Carg_Switches table.
2069
2070                   case The_Switches.Kind is
2071                      when Prj.Undefined =>
2072                         null;
2073
2074                      when Prj.Single =>
2075                         declare
2076                            Switch : constant String :=
2077                                       Get_Name_String (The_Switches.Value);
2078                         begin
2079                            if Switch'Length > 0 then
2080                               Add_To_Carg_Switches (new String'(Switch));
2081                            end if;
2082                         end;
2083
2084                      when Prj.List =>
2085                         Current := The_Switches.Values;
2086                         while Current /= Prj.Nil_String loop
2087                            The_String :=
2088                              Project_Tree.String_Elements.Table (Current);
2089
2090                            declare
2091                               Switch : constant String :=
2092                                          Get_Name_String (The_String.Value);
2093                            begin
2094                               if Switch'Length > 0 then
2095                                  Add_To_Carg_Switches (new String'(Switch));
2096                               end if;
2097                            end;
2098
2099                            Current := The_String.Next;
2100                         end loop;
2101                   end case;
2102                end if;
2103             end;
2104
2105             --  If -cargs is one of the switches, move the following switches
2106             --  to the Carg_Switches table.
2107
2108             for J in 1 .. First_Switches.Last loop
2109                if First_Switches.Table (J).all = "-cargs" then
2110                   declare
2111                      K    : Positive;
2112                      Last : Natural;
2113
2114                   begin
2115                      --  Move the switches that are before -rules when the
2116                      --  command is CHECK.
2117
2118                      K := J + 1;
2119                      while K <= First_Switches.Last
2120                        and then
2121                         (The_Command /= Check
2122                           or else First_Switches.Table (K).all /= "-rules")
2123                      loop
2124                         Add_To_Carg_Switches (First_Switches.Table (K));
2125                         K := K + 1;
2126                      end loop;
2127
2128                      if K > First_Switches.Last then
2129                         First_Switches.Set_Last (J - 1);
2130
2131                      else
2132                         Last := J - 1;
2133                         while K <= First_Switches.Last loop
2134                            Last := Last + 1;
2135                            First_Switches.Table (Last) :=
2136                              First_Switches.Table (K);
2137                            K := K + 1;
2138                         end loop;
2139
2140                         First_Switches.Set_Last (Last);
2141                      end if;
2142                   end;
2143
2144                   exit;
2145                end if;
2146             end loop;
2147
2148             for J in 1 .. Last_Switches.Last loop
2149                if Last_Switches.Table (J).all = "-cargs" then
2150                   declare
2151                      K    : Positive;
2152                      Last : Natural;
2153
2154                   begin
2155                      --  Move the switches that are before -rules when the
2156                      --  command is CHECK.
2157
2158                      K := J + 1;
2159                      while K <= Last_Switches.Last
2160                        and then
2161                         (The_Command /= Check
2162                           or else Last_Switches.Table (K).all /= "-rules")
2163                      loop
2164                         Add_To_Carg_Switches (Last_Switches.Table (K));
2165                         K := K + 1;
2166                      end loop;
2167
2168                      if K > Last_Switches.Last then
2169                         Last_Switches.Set_Last (J - 1);
2170
2171                      else
2172                         Last := J - 1;
2173                         while K <= Last_Switches.Last loop
2174                            Last := Last + 1;
2175                            Last_Switches.Table (Last) :=
2176                              Last_Switches.Table (K);
2177                            K := K + 1;
2178                         end loop;
2179
2180                         Last_Switches.Set_Last (Last);
2181                      end if;
2182                   end;
2183
2184                   exit;
2185                end if;
2186             end loop;
2187
2188             declare
2189                CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2190                M_File  : constant Path_Name_Type := Mapping_File;
2191
2192             begin
2193                if CP_File /= No_Path then
2194                   if The_Command = Elim then
2195                      First_Switches.Increment_Last;
2196                      First_Switches.Table (First_Switches.Last)  :=
2197                        new String'("-C" & Get_Name_String (CP_File));
2198
2199                   else
2200                      Add_To_Carg_Switches
2201                        (new String'("-gnatec=" & Get_Name_String (CP_File)));
2202                   end if;
2203                end if;
2204
2205                if M_File /= No_Path then
2206                   Add_To_Carg_Switches
2207                     (new String'("-gnatem=" & Get_Name_String (M_File)));
2208                end if;
2209
2210                --  For gnatcheck, also indicate a global configuration pragmas
2211                --  file and, if -U is not used, a local one.
2212
2213                if The_Command = Check then
2214                   declare
2215                      Pkg  : constant Prj.Package_Id :=
2216                               Prj.Util.Value_Of
2217                                 (Name        => Name_Builder,
2218                                  In_Packages => Project.Decl.Packages,
2219                                  In_Tree     => Project_Tree);
2220
2221                      Variable : Variable_Value :=
2222                                   Prj.Util.Value_Of
2223                                     (Name                    => No_Name,
2224                                      Attribute_Or_Array_Name =>
2225                                        Name_Global_Configuration_Pragmas,
2226                                      In_Package              => Pkg,
2227                                      In_Tree                 => Project_Tree);
2228
2229                   begin
2230                      if (Variable = Nil_Variable_Value
2231                           or else Length_Of_Name (Variable.Value) = 0)
2232                        and then Pkg /= No_Package
2233                      then
2234                         Variable :=
2235                           Prj.Util.Value_Of
2236                             (Name                    => Name_Ada,
2237                              Attribute_Or_Array_Name =>
2238                                Name_Global_Config_File,
2239                              In_Package              => Pkg,
2240                              In_Tree                 => Project_Tree);
2241                      end if;
2242
2243                      if Variable /= Nil_Variable_Value
2244                        and then Length_Of_Name (Variable.Value) /= 0
2245                      then
2246                         Add_To_Carg_Switches
2247                           (new String'
2248                              ("-gnatec=" & Get_Name_String (Variable.Value)));
2249                      end if;
2250                   end;
2251
2252                   if not All_Projects then
2253                      declare
2254                         Pkg : constant Prj.Package_Id :=
2255                                 Prj.Util.Value_Of
2256                                   (Name        => Name_Compiler,
2257                                    In_Packages => Project.Decl.Packages,
2258                                    In_Tree     => Project_Tree);
2259
2260                         Variable : Variable_Value :=
2261                                      Prj.Util.Value_Of
2262                                        (Name        => No_Name,
2263                                         Attribute_Or_Array_Name =>
2264                                           Name_Local_Configuration_Pragmas,
2265                                         In_Package  => Pkg,
2266                                         In_Tree     => Project_Tree);
2267
2268                      begin
2269                         if (Variable = Nil_Variable_Value
2270                              or else Length_Of_Name (Variable.Value) = 0)
2271                           and then Pkg /= No_Package
2272                         then
2273                            Variable :=
2274                              Prj.Util.Value_Of
2275                                (Name                    => Name_Ada,
2276                                 Attribute_Or_Array_Name =>
2277                                   Name_Local_Config_File,
2278                                 In_Package              => Pkg,
2279                                 In_Tree                 => Project_Tree);
2280                         end if;
2281
2282                         if Variable /= Nil_Variable_Value
2283                           and then Length_Of_Name (Variable.Value) /= 0
2284                         then
2285                            Add_To_Carg_Switches
2286                              (new String'
2287                                 ("-gnatec=" &
2288                                  Get_Name_String (Variable.Value)));
2289                         end if;
2290                      end;
2291                   end if;
2292                end if;
2293             end;
2294          end if;
2295
2296          if The_Command = Link then
2297             Process_Link;
2298          end if;
2299
2300          if The_Command = Link or else The_Command = Bind then
2301
2302             --  For files that are specified as relative paths with directory
2303             --  information, we convert them to absolute paths, with parent
2304             --  being the current working directory if specified on the command
2305             --  line and the project directory if specified in the project
2306             --  file. This is what gnatmake is doing for linker and binder
2307             --  arguments.
2308
2309             for J in 1 .. Last_Switches.Last loop
2310                GNATCmd.Test_If_Relative_Path
2311                  (Last_Switches.Table (J), Current_Work_Dir);
2312             end loop;
2313
2314             Get_Name_String (Project.Directory.Name);
2315
2316             declare
2317                Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2318             begin
2319                for J in 1 .. First_Switches.Last loop
2320                   GNATCmd.Test_If_Relative_Path
2321                     (First_Switches.Table (J), Project_Dir);
2322                end loop;
2323             end;
2324
2325          elsif The_Command = Stub then
2326             declare
2327                File_Index : Integer := 0;
2328                Dir_Index  : Integer := 0;
2329                Last       : constant Integer := Last_Switches.Last;
2330                Lang       : constant Language_Ptr :=
2331                               Get_Language_From_Name (Project, "ada");
2332
2333             begin
2334                for Index in 1 .. Last loop
2335                   if Last_Switches.Table (Index)
2336                     (Last_Switches.Table (Index)'First) /= '-'
2337                   then
2338                      File_Index := Index;
2339                      exit;
2340                   end if;
2341                end loop;
2342
2343                --  If the project file naming scheme is not standard, and if
2344                --  the file name ends with the spec suffix, then indicate to
2345                --  gnatstub the name of the body file with a -o switch.
2346
2347                if not Is_Standard_GNAT_Naming (Lang.Config.Naming_Data) then
2348                   if File_Index /= 0 then
2349                      declare
2350                         Spec : constant String :=
2351                                  Base_Name
2352                                    (Last_Switches.Table (File_Index).all);
2353                         Last : Natural := Spec'Last;
2354
2355                      begin
2356                         Get_Name_String (Lang.Config.Naming_Data.Spec_Suffix);
2357
2358                         if Spec'Length > Name_Len
2359                           and then Spec (Last - Name_Len + 1 .. Last) =
2360                                                   Name_Buffer (1 .. Name_Len)
2361                         then
2362                            Last := Last - Name_Len;
2363                            Get_Name_String
2364                              (Lang.Config.Naming_Data.Body_Suffix);
2365                            Last_Switches.Increment_Last;
2366                            Last_Switches.Table (Last_Switches.Last) :=
2367                              new String'("-o");
2368                            Last_Switches.Increment_Last;
2369                            Last_Switches.Table (Last_Switches.Last) :=
2370                              new String'(Spec (Spec'First .. Last) &
2371                                            Name_Buffer (1 .. Name_Len));
2372                         end if;
2373                      end;
2374                   end if;
2375                end if;
2376
2377                --  Add the directory of the spec as the destination directory
2378                --  of the body, if there is no destination directory already
2379                --  specified.
2380
2381                if File_Index /= 0 then
2382                   for Index in File_Index + 1 .. Last loop
2383                      if Last_Switches.Table (Index)
2384                          (Last_Switches.Table (Index)'First) /= '-'
2385                      then
2386                         Dir_Index := Index;
2387                         exit;
2388                      end if;
2389                   end loop;
2390
2391                   if Dir_Index = 0 then
2392                      Last_Switches.Increment_Last;
2393                      Last_Switches.Table (Last_Switches.Last) :=
2394                        new String'
2395                              (Dir_Name (Last_Switches.Table (File_Index).all));
2396                   end if;
2397                end if;
2398             end;
2399          end if;
2400
2401          --  For gnatmetric, the generated files should be put in the object
2402          --  directory. This must be the first switch, because it may be
2403          --  overridden by a switch in package Metrics in the project file or
2404          --  by a command line option. Note that we don't add the -d= switch
2405          --  if there is no object directory available.
2406
2407          if The_Command = Metric
2408            and then Project.Object_Directory /= No_Path_Information
2409          then
2410             First_Switches.Increment_Last;
2411             First_Switches.Table (2 .. First_Switches.Last) :=
2412               First_Switches.Table (1 .. First_Switches.Last - 1);
2413             First_Switches.Table (1) :=
2414               new String'("-d=" &
2415                           Get_Name_String (Project.Object_Directory.Name));
2416          end if;
2417
2418          --  For gnat check, -rules and the following switches need to be the
2419          --  last options, so move all these switches to table Rules_Switches.
2420
2421          if The_Command = Check then
2422             declare
2423                New_Last : Natural;
2424                --  Set to rank of options preceding "-rules"
2425
2426                In_Rules_Switches : Boolean;
2427                --  Set to True when options "-rules" is found
2428
2429             begin
2430                New_Last := First_Switches.Last;
2431                In_Rules_Switches := False;
2432
2433                for J in 1 .. First_Switches.Last loop
2434                   if In_Rules_Switches then
2435                      Add_To_Rules_Switches (First_Switches.Table (J));
2436
2437                   elsif First_Switches.Table (J).all = "-rules" then
2438                      New_Last := J - 1;
2439                      In_Rules_Switches := True;
2440                   end if;
2441                end loop;
2442
2443                if In_Rules_Switches then
2444                   First_Switches.Set_Last (New_Last);
2445                end if;
2446
2447                New_Last := Last_Switches.Last;
2448                In_Rules_Switches := False;
2449
2450                for J in 1 .. Last_Switches.Last loop
2451                   if In_Rules_Switches then
2452                      Add_To_Rules_Switches (Last_Switches.Table (J));
2453
2454                   elsif Last_Switches.Table (J).all = "-rules" then
2455                      New_Last := J - 1;
2456                      In_Rules_Switches := True;
2457                   end if;
2458                end loop;
2459
2460                if In_Rules_Switches then
2461                   Last_Switches.Set_Last (New_Last);
2462                end if;
2463             end;
2464          end if;
2465
2466          --  For gnat check, sync, metric or pretty with -U + a main, get the
2467          --  list of sources from the closure and add them to the arguments.
2468
2469          if ASIS_Main /= null then
2470             Get_Closure;
2471
2472             --  On VMS, set up the env var again for source dirs file. This is
2473             --  because the call to gnatmake has set this env var to another
2474             --  file that has now been deleted.
2475
2476             if Hostparm.OpenVMS then
2477
2478                --  First make sure that the recorded file names are empty
2479
2480                Prj.Env.Initialize (Project_Tree);
2481
2482                Prj.Env.Set_Ada_Paths
2483                  (Project, Project_Tree, Including_Libraries => False);
2484             end if;
2485
2486          --  For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2487          --  and gnat stack, if no file has been put on the command line, call
2488          --  tool with all the sources of the main project.
2489
2490          elsif The_Command = Check  or else
2491                The_Command = Sync   or else
2492                The_Command = Pretty or else
2493                The_Command = Metric or else
2494                The_Command = List   or else
2495                The_Command = Stack
2496          then
2497             Check_Files;
2498          end if;
2499       end if;
2500
2501       --  Gather all the arguments and invoke the executable
2502
2503       declare
2504          The_Args : Argument_List
2505                       (1 .. First_Switches.Last +
2506                             Last_Switches.Last +
2507                             Carg_Switches.Last +
2508                             Rules_Switches.Last);
2509          Arg_Num  : Natural := 0;
2510
2511       begin
2512          for J in 1 .. First_Switches.Last loop
2513             Arg_Num := Arg_Num + 1;
2514             The_Args (Arg_Num) := First_Switches.Table (J);
2515          end loop;
2516
2517          for J in 1 .. Last_Switches.Last loop
2518             Arg_Num := Arg_Num + 1;
2519             The_Args (Arg_Num) := Last_Switches.Table (J);
2520          end loop;
2521
2522          for J in 1 .. Carg_Switches.Last loop
2523             Arg_Num := Arg_Num + 1;
2524             The_Args (Arg_Num) := Carg_Switches.Table (J);
2525          end loop;
2526
2527          for J in 1 .. Rules_Switches.Last loop
2528             Arg_Num := Arg_Num + 1;
2529             The_Args (Arg_Num) := Rules_Switches.Table (J);
2530          end loop;
2531
2532          --  If Display_Command is on, only display the generated command
2533
2534          if Display_Command then
2535             Put (Standard_Error, "generated command -->");
2536             Put (Standard_Error, Exec_Path.all);
2537
2538             for Arg in The_Args'Range loop
2539                Put (Standard_Error, " ");
2540                Put (Standard_Error, The_Args (Arg).all);
2541             end loop;
2542
2543             Put (Standard_Error, "<--");
2544             New_Line (Standard_Error);
2545             raise Normal_Exit;
2546          end if;
2547
2548          if Verbose_Mode then
2549             Output.Write_Str (Exec_Path.all);
2550
2551             for Arg in The_Args'Range loop
2552                Output.Write_Char (' ');
2553                Output.Write_Str (The_Args (Arg).all);
2554             end loop;
2555
2556             Output.Write_Eol;
2557          end if;
2558
2559          My_Exit_Status :=
2560            Exit_Status (Spawn (Exec_Path.all, The_Args));
2561          raise Normal_Exit;
2562       end;
2563    end;
2564
2565 exception
2566    when Error_Exit =>
2567       if not Keep_Temporary_Files then
2568          Prj.Delete_All_Temp_Files (Project_Tree);
2569          Delete_Temp_Config_Files;
2570       end if;
2571
2572       Set_Exit_Status (Failure);
2573
2574    when Normal_Exit =>
2575       if not Keep_Temporary_Files then
2576          Prj.Delete_All_Temp_Files (Project_Tree);
2577          Delete_Temp_Config_Files;
2578       end if;
2579
2580       --  Since GNATCmd is normally called from DCL (the VMS shell), it must
2581       --  return an understandable VMS exit status. However the exit status
2582       --  returned *to* GNATCmd is a Posix style code, so we test it and return
2583       --  just a simple success or failure on VMS.
2584
2585       if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2586          Set_Exit_Status (Failure);
2587       else
2588          Set_Exit_Status (My_Exit_Status);
2589       end if;
2590 end GNATCmd;