OSDN Git Service

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