OSDN Git Service

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