OSDN Git Service

2008-05-27 Vincent Celier <celier@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").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
1057                ALI_File       : constant String (1 .. Switch'Length + 4) :=
1058                                   Switch & ".ali";
1059
1060                Test_Existence : Boolean := False;
1061
1062             begin
1063                Last := Switch'Length;
1064
1065                --  Skip real switches
1066
1067                if Switch'Length /= 0
1068                  and then Switch (Switch'First) /= '-'
1069                then
1070                   --  Append ".ali" if file name does not end with it
1071
1072                   if Switch'Length <= 4
1073                     or else Switch (Switch'Last - 3 .. Switch'Last)
1074                     /= ".ali"
1075                   then
1076                      Last := ALI_File'Last;
1077                   end if;
1078
1079                   --  If file name includes directory information, stop if ALI
1080                   --  file exists.
1081
1082                   if Is_Absolute_Path (ALI_File (1 .. Last)) then
1083                      Test_Existence := True;
1084
1085                   else
1086                      for K in Switch'Range loop
1087                         if Switch (K) = '/' or else
1088                           Switch (K) = Directory_Separator
1089                         then
1090                            Test_Existence := True;
1091                            exit;
1092                         end if;
1093                      end loop;
1094                   end if;
1095
1096                   if Test_Existence then
1097                      if Is_Regular_File (ALI_File (1 .. Last)) then
1098                         exit Switch_Loop;
1099                      end if;
1100
1101                   --  Look in object directories if ALI file exists
1102
1103                   else
1104                      Project_Loop : loop
1105                         declare
1106                            Dir : constant String :=
1107                                    Get_Name_String
1108                                      (Project_Tree.Projects.Table
1109                                         (Prj).Object_Directory.Name);
1110                         begin
1111                            if Is_Regular_File
1112                                 (Dir &
1113                                  Directory_Separator &
1114                                  ALI_File (1 .. Last))
1115                            then
1116                               --  We have found the correct project, so we
1117                               --  replace the file with the absolute path.
1118
1119                               Last_Switches.Table (J) :=
1120                                 new String'
1121                                   (Dir & Directory_Separator &
1122                                    ALI_File (1 .. Last));
1123
1124                               --  And we are done
1125
1126                               exit Switch_Loop;
1127                            end if;
1128                         end;
1129
1130                         --  Go to the project being extended, if any
1131
1132                         Prj :=
1133                           Project_Tree.Projects.Table (Prj).Extends;
1134                         exit Project_Loop when Prj = No_Project;
1135                      end loop Project_Loop;
1136                   end if;
1137                end if;
1138             end;
1139          end if;
1140       end loop Switch_Loop;
1141
1142       --  If a relative path output file has been specified, we add the exec
1143       --  directory.
1144
1145       for J in reverse 1 .. Last_Switches.Last - 1 loop
1146          if Last_Switches.Table (J).all = "-o" then
1147             Check_Relative_Executable
1148               (Name => Last_Switches.Table (J + 1));
1149             Look_For_Executable := False;
1150             exit;
1151          end if;
1152       end loop;
1153
1154       if Look_For_Executable then
1155          for J in reverse 1 .. First_Switches.Last - 1 loop
1156             if First_Switches.Table (J).all = "-o" then
1157                Look_For_Executable := False;
1158                Check_Relative_Executable
1159                  (Name => First_Switches.Table (J + 1));
1160                exit;
1161             end if;
1162          end loop;
1163       end if;
1164
1165       --  If no executable is specified, then find the name of the first ALI
1166       --  file on the command line and issue a -o switch with the absolute path
1167       --  of the executable in the exec directory.
1168
1169       if Look_For_Executable then
1170          for J in 1 .. Last_Switches.Last loop
1171             Arg  := Last_Switches.Table (J);
1172             Last := 0;
1173
1174             if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1175                if Arg'Length > 4
1176                  and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1177                then
1178                   Last := Arg'Last - 4;
1179
1180                elsif Is_Regular_File (Arg.all & ".ali") then
1181                   Last := Arg'Last;
1182                end if;
1183
1184                if Last /= 0 then
1185                   Last_Switches.Increment_Last;
1186                   Last_Switches.Table (Last_Switches.Last) :=
1187                     new String'("-o");
1188                   Get_Name_String
1189                     (Project_Tree.Projects.Table
1190                        (Project).Exec_Directory.Name);
1191                   Last_Switches.Increment_Last;
1192                   Last_Switches.Table (Last_Switches.Last) :=
1193                     new String'(Name_Buffer (1 .. Name_Len) &
1194                                 Directory_Separator &
1195                                 Executable_Name
1196                                   (Base_Name (Arg (Arg'First .. Last))));
1197                   exit;
1198                end if;
1199             end if;
1200          end loop;
1201       end if;
1202    end Process_Link;
1203
1204    ---------------------
1205    -- Set_Library_For --
1206    ---------------------
1207
1208    procedure Set_Library_For
1209      (Project             : Project_Id;
1210       There_Are_Libraries : in out Boolean)
1211    is
1212       Path_Option : constant String_Access :=
1213                       MLib.Linker_Library_Path_Option;
1214
1215    begin
1216       --  Case of library project
1217
1218       if Project_Tree.Projects.Table (Project).Library then
1219          There_Are_Libraries := True;
1220
1221          --  Add the -L switch
1222
1223          Last_Switches.Increment_Last;
1224          Last_Switches.Table (Last_Switches.Last) :=
1225            new String'("-L" &
1226                        Get_Name_String
1227                          (Project_Tree.Projects.Table
1228                             (Project).Library_Dir.Name));
1229
1230          --  Add the -l switch
1231
1232          Last_Switches.Increment_Last;
1233          Last_Switches.Table (Last_Switches.Last) :=
1234            new String'("-l" &
1235                        Get_Name_String
1236                          (Project_Tree.Projects.Table
1237                             (Project).Library_Name));
1238
1239          --  Add the directory to table Library_Paths, to be processed later
1240          --  if library is not static and if Path_Option is not null.
1241
1242          if Project_Tree.Projects.Table (Project).Library_Kind /=
1243               Static
1244            and then Path_Option /= null
1245          then
1246             Library_Paths.Increment_Last;
1247             Library_Paths.Table (Library_Paths.Last) :=
1248               new String'(Get_Name_String
1249                             (Project_Tree.Projects.Table
1250                                (Project).Library_Dir.Name));
1251          end if;
1252       end if;
1253    end Set_Library_For;
1254
1255    ---------------------------
1256    -- Test_If_Relative_Path --
1257    ---------------------------
1258
1259    procedure Test_If_Relative_Path
1260      (Switch : in out String_Access;
1261       Parent : String)
1262    is
1263    begin
1264       if Switch /= null then
1265
1266          declare
1267             Sw : String (1 .. Switch'Length);
1268             Start : Positive := 1;
1269
1270          begin
1271             Sw := Switch.all;
1272
1273             if Sw (1) = '-' then
1274                if Sw'Length >= 3
1275                  and then (Sw (2) = 'A' or else
1276                            Sw (2) = 'I' or else
1277                            Sw (2) = 'L')
1278                then
1279                   Start := 3;
1280
1281                   if Sw = "-I-" then
1282                      return;
1283                   end if;
1284
1285                elsif Sw'Length >= 4
1286                  and then (Sw (2 .. 3) = "aL" or else
1287                            Sw (2 .. 3) = "aO" or else
1288                            Sw (2 .. 3) = "aI")
1289                then
1290                   Start := 4;
1291
1292                elsif Sw'Length >= 7
1293                  and then Sw (2 .. 6) = "-RTS="
1294                then
1295                   Start := 7;
1296                else
1297                   return;
1298                end if;
1299             end if;
1300
1301             --  If the path is relative, test if it includes directory
1302             --  information. If it does, prepend Parent to the path.
1303
1304             if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1305                for J in Start .. Sw'Last loop
1306                   if Sw (J) = Directory_Separator then
1307                      Switch :=
1308                         new String'
1309                               (Sw (1 .. Start - 1) &
1310                                Parent &
1311                                Directory_Separator &
1312                                Sw (Start .. Sw'Last));
1313                      return;
1314                   end if;
1315                end loop;
1316             end if;
1317          end;
1318       end if;
1319    end Test_If_Relative_Path;
1320
1321    -------------------
1322    -- Non_VMS_Usage --
1323    -------------------
1324
1325    procedure Non_VMS_Usage is
1326    begin
1327       Output_Version;
1328       New_Line;
1329       Put_Line ("List of available commands");
1330       New_Line;
1331
1332       for C in Command_List'Range loop
1333          if not Command_List (C).VMS_Only then
1334             if Targparm.AAMP_On_Target then
1335                Put ("gnaampcmd ");
1336             else
1337                Put ("gnat ");
1338             end if;
1339
1340             Put (To_Lower (Command_List (C).Cname.all));
1341             Set_Col (25);
1342
1343             --  Never call gnatstack with a prefix
1344
1345             if C = Stack then
1346                Put (Command_List (C).Unixcmd.all);
1347             else
1348                Put (Program_Name (Command_List (C).Unixcmd.all).all);
1349             end if;
1350
1351             declare
1352                Sws : Argument_List_Access renames Command_List (C).Unixsws;
1353             begin
1354                if Sws /= null then
1355                   for J in Sws'Range loop
1356                      Put (' ');
1357                      Put (Sws (J).all);
1358                   end loop;
1359                end if;
1360             end;
1361
1362             New_Line;
1363          end if;
1364       end loop;
1365
1366       New_Line;
1367       Put_Line ("Commands find, list, metric, pretty, stack, stub and xref " &
1368                 "accept project file switches -vPx, -Pprj and -Xnam=val");
1369       New_Line;
1370    end Non_VMS_Usage;
1371
1372    -------------------------------------
1373    -- Start of processing for GNATCmd --
1374    -------------------------------------
1375
1376 begin
1377    --  Initializations
1378
1379    Namet.Initialize;
1380    Csets.Initialize;
1381
1382    Snames.Initialize;
1383
1384    Prj.Initialize (Project_Tree);
1385
1386    Last_Switches.Init;
1387    Last_Switches.Set_Last (0);
1388
1389    First_Switches.Init;
1390    First_Switches.Set_Last (0);
1391    Carg_Switches.Init;
1392    Carg_Switches.Set_Last (0);
1393    Rules_Switches.Init;
1394    Rules_Switches.Set_Last (0);
1395
1396    VMS_Conv.Initialize;
1397
1398    Set_Mode (Ada_Only);
1399
1400    --  Add the default search directories, to be able to find system.ads in the
1401    --  subsequent call to Targparm.Get_Target_Parameters.
1402
1403    Add_Default_Search_Dirs;
1404
1405    --  Get target parameters so that AAMP_On_Target will be set, for testing in
1406    --  Osint.Program_Name to handle the mapping of GNAAMP tool names.
1407
1408    Targparm.Get_Target_Parameters;
1409
1410    --  Add the directory where the GNAT driver is invoked in front of the path,
1411    --  if the GNAT driver is invoked with directory information. Do not do this
1412    --  for VMS, where the notion of path does not really exist.
1413
1414    if not OpenVMS then
1415       declare
1416          Command : constant String := Command_Name;
1417
1418       begin
1419          for Index in reverse Command'Range loop
1420             if Command (Index) = Directory_Separator then
1421                declare
1422                   Absolute_Dir : constant String :=
1423                                    Normalize_Pathname
1424                                      (Command (Command'First .. Index));
1425
1426                   PATH         : constant String :=
1427                                    Absolute_Dir &
1428                   Path_Separator &
1429                   Getenv ("PATH").all;
1430
1431                begin
1432                   Setenv ("PATH", PATH);
1433                end;
1434
1435                exit;
1436             end if;
1437          end loop;
1438       end;
1439    end if;
1440
1441    --  If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1442    --  filenames and pathnames to Unix style.
1443
1444    if Hostparm.OpenVMS
1445      or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1446    then
1447       VMS_Conversion (The_Command);
1448
1449       B_Start := new String'("b__");
1450
1451    --  If not on VMS, scan the command line directly
1452
1453    else
1454       if Argument_Count = 0 then
1455          Non_VMS_Usage;
1456          return;
1457       else
1458          begin
1459             loop
1460                if Argument_Count > Command_Arg
1461                  and then Argument (Command_Arg) = "-v"
1462                then
1463                   Verbose_Mode := True;
1464                   Command_Arg := Command_Arg + 1;
1465
1466                elsif Argument_Count > Command_Arg
1467                  and then Argument (Command_Arg) = "-dn"
1468                then
1469                   Keep_Temporary_Files := True;
1470                   Command_Arg := Command_Arg + 1;
1471
1472                else
1473                   exit;
1474                end if;
1475             end loop;
1476
1477             The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1478
1479             if Command_List (The_Command).VMS_Only then
1480                Non_VMS_Usage;
1481                Fail
1482                  ("Command """,
1483                   Command_List (The_Command).Cname.all,
1484                   """ can only be used on VMS");
1485             end if;
1486
1487          exception
1488             when Constraint_Error =>
1489
1490                --  Check if it is an alternate command
1491
1492                declare
1493                   Alternate : Alternate_Command;
1494
1495                begin
1496                   Alternate := Alternate_Command'Value
1497                                               (Argument (Command_Arg));
1498                   The_Command := Corresponding_To (Alternate);
1499
1500                exception
1501                   when Constraint_Error =>
1502                      Non_VMS_Usage;
1503                      Fail ("Unknown command: ", Argument (Command_Arg));
1504                end;
1505          end;
1506
1507          --  Get the arguments from the command line and from the eventual
1508          --  argument file(s) specified on the command line.
1509
1510          for Arg in Command_Arg + 1 .. Argument_Count loop
1511             declare
1512                The_Arg : constant String := Argument (Arg);
1513
1514             begin
1515                --  Check if an argument file is specified
1516
1517                if The_Arg (The_Arg'First) = '@' then
1518                   declare
1519                      Arg_File : Ada.Text_IO.File_Type;
1520                      Line     : String (1 .. 256);
1521                      Last     : Natural;
1522
1523                   begin
1524                      --  Open the file and fail if the file cannot be found
1525
1526                      begin
1527                         Open
1528                           (Arg_File, In_File,
1529                            The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1530
1531                      exception
1532                         when others =>
1533                            Put
1534                              (Standard_Error, "Cannot open argument file """);
1535                            Put
1536                              (Standard_Error,
1537                               The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1538
1539                            Put_Line (Standard_Error, """");
1540                            raise Error_Exit;
1541                      end;
1542
1543                      --  Read line by line and put the content of each non-
1544                      --  empty line in the Last_Switches table.
1545
1546                      while not End_Of_File (Arg_File) loop
1547                         Get_Line (Arg_File, Line, Last);
1548
1549                         if Last /= 0 then
1550                            Last_Switches.Increment_Last;
1551                            Last_Switches.Table (Last_Switches.Last) :=
1552                              new String'(Line (1 .. Last));
1553                         end if;
1554                      end loop;
1555
1556                      Close (Arg_File);
1557                   end;
1558
1559                else
1560                   --  It is not an argument file; just put the argument in
1561                   --  the Last_Switches table.
1562
1563                   Last_Switches.Increment_Last;
1564                   Last_Switches.Table (Last_Switches.Last) :=
1565                     new String'(The_Arg);
1566                end if;
1567             end;
1568          end loop;
1569       end if;
1570    end if;
1571
1572    declare
1573       Program   : String_Access;
1574       Exec_Path : String_Access;
1575
1576    begin
1577       if The_Command = Stack then
1578          --  Never call gnatstack with a prefix
1579
1580          Program := new String'(Command_List (The_Command).Unixcmd.all);
1581
1582       else
1583          Program :=
1584            Program_Name (Command_List (The_Command).Unixcmd.all);
1585       end if;
1586
1587       --  Locate the executable for the command
1588
1589       Exec_Path := Locate_Exec_On_Path (Program.all);
1590
1591       if Exec_Path = null then
1592          Put_Line (Standard_Error, "could not locate " & Program.all);
1593          raise Error_Exit;
1594       end if;
1595
1596       --  If there are switches for the executable, put them as first switches
1597
1598       if Command_List (The_Command).Unixsws /= null then
1599          for J in Command_List (The_Command).Unixsws'Range loop
1600             First_Switches.Increment_Last;
1601             First_Switches.Table (First_Switches.Last) :=
1602               Command_List (The_Command).Unixsws (J);
1603          end loop;
1604       end if;
1605
1606       --  For BIND, CHECK, ELIM, FIND, LINK, LIST, PRETTY, STACK, STUB,
1607       --  METRIC ad  XREF, look for project file related switches.
1608
1609       if The_Command = Bind
1610         or else The_Command = Check
1611         or else The_Command = Sync
1612         or else The_Command = Elim
1613         or else The_Command = Find
1614         or else The_Command = Link
1615         or else The_Command = List
1616         or else The_Command = Xref
1617         or else The_Command = Pretty
1618         or else The_Command = Stack
1619         or else The_Command = Stub
1620         or else The_Command = Metric
1621       then
1622          case The_Command is
1623             when Bind =>
1624                Tool_Package_Name := Name_Binder;
1625                Packages_To_Check := Packages_To_Check_By_Binder;
1626             when Check =>
1627                Tool_Package_Name := Name_Check;
1628                Packages_To_Check := Packages_To_Check_By_Check;
1629             when Sync =>
1630                Tool_Package_Name := Name_Synchronize;
1631                Packages_To_Check := Packages_To_Check_By_Sync;
1632             when Elim =>
1633                Tool_Package_Name := Name_Eliminate;
1634                Packages_To_Check := Packages_To_Check_By_Eliminate;
1635             when Find =>
1636                Tool_Package_Name := Name_Finder;
1637                Packages_To_Check := Packages_To_Check_By_Finder;
1638             when Link =>
1639                Tool_Package_Name := Name_Linker;
1640                Packages_To_Check := Packages_To_Check_By_Linker;
1641             when List =>
1642                Tool_Package_Name := Name_Gnatls;
1643                Packages_To_Check := Packages_To_Check_By_Gnatls;
1644             when Metric =>
1645                Tool_Package_Name := Name_Metrics;
1646                Packages_To_Check := Packages_To_Check_By_Metric;
1647             when Pretty =>
1648                Tool_Package_Name := Name_Pretty_Printer;
1649                Packages_To_Check := Packages_To_Check_By_Pretty;
1650             when Stack =>
1651                Tool_Package_Name := Name_Stack;
1652                Packages_To_Check := Packages_To_Check_By_Stack;
1653             when Stub =>
1654                Tool_Package_Name := Name_Gnatstub;
1655                Packages_To_Check := Packages_To_Check_By_Gnatstub;
1656             when Xref =>
1657                Tool_Package_Name := Name_Cross_Reference;
1658                Packages_To_Check := Packages_To_Check_By_Xref;
1659             when others =>
1660                null;
1661          end case;
1662
1663          --  Check that the switches are consistent. Detect project file
1664          --  related switches.
1665
1666          Inspect_Switches :
1667          declare
1668             Arg_Num : Positive := 1;
1669             Argv    : String_Access;
1670
1671             procedure Remove_Switch (Num : Positive);
1672             --  Remove a project related switch from table Last_Switches
1673
1674             -------------------
1675             -- Remove_Switch --
1676             -------------------
1677
1678             procedure Remove_Switch (Num : Positive) is
1679             begin
1680                Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1681                  Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1682                Last_Switches.Decrement_Last;
1683             end Remove_Switch;
1684
1685          --  Start of processing for Inspect_Switches
1686
1687          begin
1688             while Arg_Num <= Last_Switches.Last loop
1689                Argv := Last_Switches.Table (Arg_Num);
1690
1691                if Argv (Argv'First) = '-' then
1692                   if Argv'Length = 1 then
1693                      Fail
1694                        ("switch character cannot be followed by a blank");
1695                   end if;
1696
1697                   --  The two style project files (-p and -P) cannot be used
1698                   --  together
1699
1700                   if (The_Command = Find or else The_Command = Xref)
1701                     and then Argv (2) = 'p'
1702                   then
1703                      Old_Project_File_Used := True;
1704                      if Project_File /= null then
1705                         Fail ("-P and -p cannot be used together");
1706                      end if;
1707                   end if;
1708
1709                   --  --subdirs=... Specify Subdirs
1710
1711                   if Argv'Length > Subdirs_Option'Length and then
1712                     Argv
1713                       (Argv'First .. Argv'First + Subdirs_Option'Length - 1) =
1714                       Subdirs_Option
1715                   then
1716                      Subdirs :=
1717                        new String'
1718                          (Argv
1719                             (Argv'First + Subdirs_Option'Length .. Argv'Last));
1720
1721                      Remove_Switch (Arg_Num);
1722
1723                   --  -aPdir  Add dir to the project search path
1724
1725                   elsif Argv'Length > 3
1726                     and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1727                   then
1728                      Add_Search_Project_Directory
1729                        (Argv (Argv'First + 3 .. Argv'Last));
1730
1731                      Remove_Switch (Arg_Num);
1732
1733                   --  -eL  Follow links for files
1734
1735                   elsif Argv.all = "-eL" then
1736                      Follow_Links_For_Files := True;
1737
1738                      Remove_Switch (Arg_Num);
1739
1740                   --  -vPx  Specify verbosity while parsing project files
1741
1742                   elsif Argv'Length = 4
1743                     and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1744                   then
1745                      case Argv (Argv'Last) is
1746                         when '0' =>
1747                            Current_Verbosity := Prj.Default;
1748                         when '1' =>
1749                            Current_Verbosity := Prj.Medium;
1750                         when '2' =>
1751                            Current_Verbosity := Prj.High;
1752                         when others =>
1753                            Fail ("Invalid switch: ", Argv.all);
1754                      end case;
1755
1756                      Remove_Switch (Arg_Num);
1757
1758                   --  -Pproject_file  Specify project file to be used
1759
1760                   elsif Argv (Argv'First + 1) = 'P' then
1761
1762                      --  Only one -P switch can be used
1763
1764                      if Project_File /= null then
1765                         Fail
1766                           (Argv.all,
1767                            ": second project file forbidden (first is """,
1768                            Project_File.all & """)");
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;