1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2009, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
30 with MLib.Tgt; use MLib.Tgt;
33 with Namet; use Namet;
35 with Osint; use Osint;
39 with Prj.Ext; use Prj.Ext;
41 with Prj.Util; use Prj.Util;
43 with Snames; use Snames;
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
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;
55 with GNAT.OS_Lib; use GNAT.OS_Lib;
57 with VMS_Conv; use VMS_Conv;
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;
66 B_Start : String_Ptr := new String'("b~");
67 -- Prefix of binder generated file, changed to b__ for VMS
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
74 Max_Files_On_The_Command_Line : constant := 30; -- Arbitrary
76 Temp_File_Name : String_Access := null;
77 -- The name of the temporary text file to put a list of source/object
78 -- files to pass to a tool, when there are more than
79 -- Max_Files_On_The_Command_Line files.
81 ASIS_Main : String_Access := null;
82 -- Main for commands Check, Metric and Pretty, when -U is used
84 package First_Switches is new Table.Table
85 (Table_Component_Type => String_Access,
86 Table_Index_Type => Integer,
89 Table_Increment => 100,
90 Table_Name => "Gnatcmd.First_Switches");
91 -- A table to keep the switches from the project file
93 package Carg_Switches is new Table.Table
94 (Table_Component_Type => String_Access,
95 Table_Index_Type => Integer,
98 Table_Increment => 100,
99 Table_Name => "Gnatcmd.Carg_Switches");
100 -- A table to keep the switches following -cargs for ASIS tools
102 package Rules_Switches is new Table.Table
103 (Table_Component_Type => String_Access,
104 Table_Index_Type => Integer,
105 Table_Low_Bound => 1,
107 Table_Increment => 100,
108 Table_Name => "Gnatcmd.Rules_Switches");
109 -- A table to keep the switches following -rules for gnatcheck
111 package Library_Paths is new Table.Table (
112 Table_Component_Type => String_Access,
113 Table_Index_Type => Integer,
114 Table_Low_Bound => 1,
116 Table_Increment => 100,
117 Table_Name => "Make.Library_Path");
119 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
120 -- tool. We allocate objects because we cannot declare aliased objects
121 -- as we are in a procedure, not a library level package.
123 subtype SA is String_Access;
125 Naming_String : constant SA := new String'("naming");
126 Binder_String : constant SA := new String'("binder");
127 Compiler_String : constant SA := new String'("compiler");
128 Check_String : constant SA := new String'("check");
129 Synchronize_String : constant SA := new String'("synchronize");
130 Eliminate_String : constant SA := new String'("eliminate");
131 Finder_String : constant SA := new String'("finder");
132 Linker_String : constant SA := new String'("linker");
133 Gnatls_String : constant SA := new String'("gnatls");
134 Pretty_String : constant SA := new String'("pretty_printer");
135 Stack_String : constant SA := new String'("stack");
136 Gnatstub_String : constant SA := new String'("gnatstub");
137 Metric_String : constant SA := new String'("metrics");
138 Xref_String : constant SA := new String'("cross_reference");
140 Packages_To_Check_By_Binder : constant String_List_Access :=
141 new String_List'((Naming_String, Binder_String));
143 Packages_To_Check_By_Check : constant String_List_Access :=
144 new String_List'((Naming_String, Check_String, Compiler_String));
146 Packages_To_Check_By_Sync : constant String_List_Access :=
147 new String_List'((Naming_String, Synchronize_String, Compiler_String));
149 Packages_To_Check_By_Eliminate : constant String_List_Access :=
150 new String_List'((Naming_String, Eliminate_String, Compiler_String));
152 Packages_To_Check_By_Finder : constant String_List_Access :=
153 new String_List'((Naming_String, Finder_String));
155 Packages_To_Check_By_Linker : constant String_List_Access :=
156 new String_List'((Naming_String, Linker_String));
158 Packages_To_Check_By_Gnatls : constant String_List_Access :=
159 new String_List'((Naming_String, Gnatls_String));
161 Packages_To_Check_By_Pretty : constant String_List_Access :=
162 new String_List'((Naming_String, Pretty_String, Compiler_String));
164 Packages_To_Check_By_Stack : constant String_List_Access :=
165 new String_List'((Naming_String, Stack_String));
167 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
168 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
170 Packages_To_Check_By_Metric : constant String_List_Access :=
171 new String_List'((Naming_String, Metric_String, Compiler_String));
173 Packages_To_Check_By_Xref : constant String_List_Access :=
174 new String_List'((Naming_String, Xref_String));
176 Packages_To_Check : String_List_Access := Prj.All_Packages;
178 ----------------------------------
179 -- Declarations for GNATCMD use --
180 ----------------------------------
182 The_Command : Command_Type;
183 -- The command specified in the invocation of the GNAT driver
185 Command_Arg : Positive := 1;
186 -- The index of the command in the arguments of the GNAT driver
188 My_Exit_Status : Exit_Status := Success;
189 -- The exit status of the spawned tool. Used to set the correct VMS
192 Current_Work_Dir : constant String := Get_Current_Dir;
193 -- The path of the working directory
195 All_Projects : Boolean := False;
196 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
197 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
198 -- should be invoked for all sources of all projects.
200 -----------------------
201 -- Local Subprograms --
202 -----------------------
204 procedure Add_To_Carg_Switches (Switch : String_Access);
205 -- Add a switch to the Carg_Switches table. If it is the first one, put the
206 -- switch "-cargs" at the beginning of the table.
208 procedure Add_To_Rules_Switches (Switch : String_Access);
209 -- Add a switch to the Rules_Switches table. If it is the first one, put
210 -- the switch "-crules" at the beginning of the table.
212 procedure Check_Files;
213 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
214 -- project file is specified, without any file arguments. If it is the
215 -- case, invoke the GNAT tool with the proper list of files, derived from
216 -- the sources of the project.
218 function Check_Project
219 (Project : Project_Id;
220 Root_Project : Project_Id) return Boolean;
221 -- Returns True if Project = Root_Project or if we want to consider all
222 -- sources of all projects. For GNAT METRIC, also returns True if Project
223 -- is extended by Root_Project.
225 procedure Check_Relative_Executable (Name : in out String_Access);
226 -- Check if an executable is specified as a relative path. If it is, and
227 -- the path contains directory information, fail. Otherwise, prepend the
228 -- exec directory. This procedure is only used for GNAT LINK when a project
229 -- file is specified.
231 function Configuration_Pragmas_File return Path_Name_Type;
232 -- Return an argument, if there is a configuration pragmas file to be
233 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
234 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
237 procedure Delete_Temp_Config_Files;
238 -- Delete all temporary config files. The caller is responsible for
239 -- ensuring that Keep_Temporary_Files is False.
241 procedure Get_Closure;
242 -- Get the sources in the closure of the ASIS_Main and add them to the
243 -- list of arguments.
245 function Index (Char : Character; Str : String) return Natural;
246 -- Returns first occurrence of Char in Str, returns 0 if Char not in Str
248 procedure Non_VMS_Usage;
249 -- Display usage for platforms other than VMS
251 procedure Process_Link;
252 -- Process GNAT LINK, when there is a project file specified
254 procedure Set_Library_For
255 (Project : Project_Id;
256 Libraries_Present : in out Boolean);
257 -- If Project is a library project, add the correct -L and -l switches to
258 -- the linker invocation.
260 procedure Set_Libraries is
261 new For_Every_Project_Imported (Boolean, Set_Library_For);
262 -- Add the -L and -l switches to the linker for all of the library
265 procedure Test_If_Relative_Path
266 (Switch : in out String_Access;
268 -- Test if Switch is a relative search path switch. If it is and it
269 -- includes directory information, prepend the path with Parent. This
270 -- subprogram is only called when using project files.
272 --------------------------
273 -- Add_To_Carg_Switches --
274 --------------------------
276 procedure Add_To_Carg_Switches (Switch : String_Access) is
278 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
280 if Carg_Switches.Last = 0 then
281 Carg_Switches.Increment_Last;
282 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
285 Carg_Switches.Increment_Last;
286 Carg_Switches.Table (Carg_Switches.Last) := Switch;
287 end Add_To_Carg_Switches;
289 ---------------------------
290 -- Add_To_Rules_Switches --
291 ---------------------------
293 procedure Add_To_Rules_Switches (Switch : String_Access) is
295 -- If the Rules_Switches table is empty, put "-rules" at the beginning
297 if Rules_Switches.Last = 0 then
298 Rules_Switches.Increment_Last;
299 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
302 Rules_Switches.Increment_Last;
303 Rules_Switches.Table (Rules_Switches.Last) := Switch;
304 end Add_To_Rules_Switches;
310 procedure Check_Files is
311 Add_Sources : Boolean := True;
312 Unit_Data : Prj.Unit_Data;
313 Subunit : Boolean := False;
316 -- Check if there is at least one argument that is not a switch
318 for Index in 1 .. Last_Switches.Last loop
319 if Last_Switches.Table (Index) (1) /= '-' then
320 Add_Sources := False;
325 -- If all arguments were switches, add the path names of all the sources
326 -- of the main project.
330 Current_Last : constant Integer := Last_Switches.Last;
334 -- Gnatstack needs to add the .ci file for the binder
335 -- generated files corresponding to all of the library projects
336 -- and main units belonging to the application.
338 if The_Command = Stack then
339 Proj := Project_Tree.Projects;
340 while Proj /= null loop
341 if Check_Project (Proj.Project, Project) then
343 Main : String_List_Id := Proj.Project.Mains;
344 File : String_Access;
347 -- Include binder generated files for main programs
349 while Main /= Nil_String loop
353 (Proj.Project.Object_Directory.Name) &
354 Directory_Separator &
358 (Project_Tree.String_Elements.Table
362 if Is_Regular_File (File.all) then
363 Last_Switches.Increment_Last;
364 Last_Switches.Table (Last_Switches.Last) := File;
368 Project_Tree.String_Elements.Table (Main).Next;
371 if Proj.Project.Library then
373 -- Include the .ci file for the binder generated
374 -- files that contains the initialization and
375 -- finalization of the library.
380 (Proj.Project.Object_Directory.Name) &
381 Directory_Separator &
383 Get_Name_String (Proj.Project.Library_Name) &
386 if Is_Regular_File (File.all) then
387 Last_Switches.Increment_Last;
388 Last_Switches.Table (Last_Switches.Last) := File;
398 for Unit in Unit_Table.First ..
399 Unit_Table.Last (Project_Tree.Units)
401 Unit_Data := Project_Tree.Units.Table (Unit);
403 -- For gnatls, we only need to put the library units, body or
404 -- spec, but not the subunits.
406 if The_Command = List then
408 Unit_Data.File_Names (Body_Part).Name /= No_File
410 Unit_Data.File_Names (Body_Part).Path.Name /= Slash
412 -- There is a body, check if it is for this project
414 if All_Projects or else
415 Unit_Data.File_Names (Body_Part).Project = Project
420 Unit_Data.File_Names (Specification).Name = No_File
423 (Specification).Path.Name = Slash
425 -- We have a body with no spec: we need to check if
426 -- this is a subunit, because gnatls will complain
430 Src_Ind : Source_File_Index;
433 Src_Ind := Sinput.P.Load_Project_File
435 (Unit_Data.File_Names
436 (Body_Part).Path.Name));
439 Sinput.P.Source_File_Is_Subunit
445 Last_Switches.Increment_Last;
446 Last_Switches.Table (Last_Switches.Last) :=
449 (Unit_Data.File_Names
450 (Body_Part).Display_Name));
455 Unit_Data.File_Names (Specification).Name /= No_File
457 Unit_Data.File_Names (Specification).Path.Name /= Slash
459 -- We have a spec with no body; check if it is for this
462 if All_Projects or else
463 Unit_Data.File_Names (Specification).Project = Project
465 Last_Switches.Increment_Last;
466 Last_Switches.Table (Last_Switches.Last) :=
469 (Unit_Data.File_Names
470 (Specification).Display_Name));
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.
479 elsif The_Command = Stack then
481 Unit_Data.File_Names (Body_Part).Name /= No_File
483 Unit_Data.File_Names (Body_Part).Path.Name /= Slash
485 -- There is a body. Check if .ci files for this project
490 (Unit_Data.File_Names (Body_Part).Project, Project)
495 Unit_Data.File_Names (Specification).Name = No_File
498 (Specification).Path.Name = Slash
500 -- We have a body with no spec: we need to check
501 -- if this is a subunit, because .ci files are not
502 -- generated for subunits.
505 Src_Ind : Source_File_Index;
508 Src_Ind := Sinput.P.Load_Project_File
510 (Unit_Data.File_Names
511 (Body_Part).Path.Name));
514 Sinput.P.Source_File_Is_Subunit (Src_Ind);
519 Last_Switches.Increment_Last;
520 Last_Switches.Table (Last_Switches.Last) :=
523 (Unit_Data.File_Names
525 Object_Directory.Name) &
526 Directory_Separator &
529 (Unit_Data.File_Names
530 (Body_Part).Display_Name),
536 Unit_Data.File_Names (Specification).Name /= No_File
538 Unit_Data.File_Names (Specification).Path.Name /= Slash
540 -- We have a spec with no body. Check if it is for this
545 (Unit_Data.File_Names (Specification).Project,
548 Last_Switches.Increment_Last;
549 Last_Switches.Table (Last_Switches.Last) :=
552 (Unit_Data.File_Names
553 (Specification).Project.
554 Object_Directory.Name) &
558 (Unit_Data.File_Names
559 (Specification).Name),
565 -- For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
566 -- sources of the project, or of all projects if -U was
569 for Kind in Spec_Or_Body loop
571 (Unit_Data.File_Names (Kind).Project, Project)
572 and then Unit_Data.File_Names (Kind).Name /= No_File
573 and then Unit_Data.File_Names (Kind).Path.Name /= Slash
575 Last_Switches.Increment_Last;
576 Last_Switches.Table (Last_Switches.Last) :=
579 (Unit_Data.File_Names
580 (Kind).Path.Display_Name));
586 -- If the list of files is too long, create a temporary text file
587 -- that lists these files, and pass this temp file to gnatcheck,
588 -- gnatpp or gnatmetric using switch -files=.
590 if Last_Switches.Last - Current_Last >
591 Max_Files_On_The_Command_Line
594 Temp_File_FD : File_Descriptor;
595 Buffer : String (1 .. 1_000);
597 OK : Boolean := True;
600 Create_Temp_File (Temp_File_FD, Temp_File_Name);
602 if Temp_File_Name /= null then
603 for Index in Current_Last + 1 ..
606 Len := Last_Switches.Table (Index)'Length;
607 Buffer (1 .. Len) := Last_Switches.Table (Index).all;
609 Buffer (Len) := ASCII.LF;
610 Buffer (Len + 1) := ASCII.NUL;
619 Close (Temp_File_FD, OK);
621 Close (Temp_File_FD, OK);
625 -- If there were any problem creating the temp file, then
626 -- pass the list of files.
630 -- Replace list of files with -files=<temp file name>
632 Last_Switches.Set_Last (Current_Last + 1);
633 Last_Switches.Table (Last_Switches.Last) :=
634 new String'("-files=" & Temp_File_Name.all);
647 function Check_Project
648 (Project : Project_Id;
649 Root_Project : Project_Id) return Boolean
654 if Project = No_Project then
657 elsif All_Projects or Project = Root_Project then
660 elsif The_Command = Metric then
661 Proj := Root_Project;
662 while Proj.Extends /= No_Project loop
663 if Project = Proj.Extends then
667 Proj := Proj.Extends;
674 -------------------------------
675 -- Check_Relative_Executable --
676 -------------------------------
678 procedure Check_Relative_Executable (Name : in out String_Access) is
679 Exec_File_Name : constant String := Name.all;
682 if not Is_Absolute_Path (Exec_File_Name) then
683 for Index in Exec_File_Name'Range loop
684 if Exec_File_Name (Index) = Directory_Separator then
685 Fail ("relative executable (""" &
687 """) with directory part not allowed " &
688 "when using project files");
692 Get_Name_String (Project.Exec_Directory.Name);
694 if Name_Buffer (Name_Len) /= Directory_Separator then
695 Name_Len := Name_Len + 1;
696 Name_Buffer (Name_Len) := Directory_Separator;
699 Name_Buffer (Name_Len + 1 ..
700 Name_Len + Exec_File_Name'Length) :=
702 Name_Len := Name_Len + Exec_File_Name'Length;
703 Name := new String'(Name_Buffer (1 .. Name_Len));
705 end Check_Relative_Executable;
707 --------------------------------
708 -- Configuration_Pragmas_File --
709 --------------------------------
711 function Configuration_Pragmas_File return Path_Name_Type is
713 Prj.Env.Create_Config_Pragmas_File
714 (Project, Project, Project_Tree, Include_Config_Files => False);
715 return Project.Config_File_Name;
716 end Configuration_Pragmas_File;
718 ------------------------------
719 -- Delete_Temp_Config_Files --
720 ------------------------------
722 procedure Delete_Temp_Config_Files is
725 pragma Warnings (Off, Success);
728 -- This should only be called if Keep_Temporary_Files is False
730 pragma Assert (not Keep_Temporary_Files);
732 if Project /= No_Project then
733 Proj := Project_Tree.Projects;
734 while Proj /= null loop
735 if Proj.Project.Config_File_Temp then
737 Output.Write_Str ("Deleting temp configuration file """);
739 (Get_Name_String (Proj.Project.Config_File_Name));
740 Output.Write_Line ("""");
744 (Name => Get_Name_String (Proj.Project.Config_File_Name),
752 -- If a temporary text file that contains a list of files for a tool
753 -- has been created, delete this temporary file.
755 if Temp_File_Name /= null then
756 Delete_File (Temp_File_Name.all, Success);
758 end Delete_Temp_Config_Files;
764 procedure Get_Closure is
765 Args : constant Argument_List :=
766 (1 => new String'("-q"),
767 2 => new String'("-b"),
768 3 => new String'("-P"),
771 6 => new String'("-bargs"),
772 7 => new String'("-R"),
773 8 => new String'("-Z"));
774 -- Arguments for the invocation of gnatmake which are added to the
775 -- Last_Arguments list by this procedure.
777 FD : File_Descriptor;
778 -- File descriptor for the temp file that will get the output of the
779 -- invocation of gnatmake.
781 Name : Path_Name_Type;
782 -- Path of the file FD
784 GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
787 GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
790 Return_Code : Integer;
793 pragma Warnings (Off, Unused);
795 File : Ada.Text_IO.File_Type;
796 Line : String (1 .. 250);
798 -- Used to read file if there is an error, it is good enough to display
799 -- just 250 characters if the first line of the file is very long.
802 Path : Path_Name_Type;
805 if GN_Path = null then
806 Put_Line (Standard_Error, "could not locate " & GN_Name);
810 -- Create the temp file
812 Tempdir.Create_Temp_File (FD, Name);
814 -- And close it, because on VMS Spawn with a file descriptor created
815 -- with Create_Temp_File does not redirect output.
819 -- Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
822 (Program_Name => GN_Path.all,
824 Output_File => Get_Name_String (Name),
826 Return_Code => Return_Code,
831 -- Read the output of the invocation of gnatmake
833 Open (File, In_File, Get_Name_String (Name));
835 -- If it was unsuccessful, display the first line in the file and exit
838 if Return_Code /= 0 then
839 Get_Line (File, Line, Last);
841 if not Keep_Temporary_Files then
847 Put_Line (Standard_Error, Line (1 .. Last));
849 (Standard_Error, "could not get closure of " & ASIS_Main.all);
853 -- Get each file name in the file, find its path and add it the
854 -- list of arguments.
856 while not End_Of_File (File) loop
857 Get_Line (File, Line, Last);
860 for Unit in Unit_Table.First ..
861 Unit_Table.Last (Project_Tree.Units)
863 Udata := Project_Tree.Units.Table (Unit);
865 if Udata.File_Names (Specification).Name /= No_File
867 Get_Name_String (Udata.File_Names (Specification).Name) =
870 Path := Udata.File_Names (Specification).Path.Name;
873 elsif Udata.File_Names (Body_Part).Name /= No_File
875 Get_Name_String (Udata.File_Names (Body_Part).Name) =
878 Path := Udata.File_Names (Body_Part).Path.Name;
883 Last_Switches.Increment_Last;
885 if Path /= No_Path then
886 Last_Switches.Table (Last_Switches.Last) :=
887 new String'(Get_Name_String (Path));
890 Last_Switches.Table (Last_Switches.Last) :=
891 new String'(Line (1 .. Last));
895 if not Keep_Temporary_Files then
907 function Index (Char : Character; Str : String) return Natural is
909 for Index in Str'Range loop
910 if Str (Index) = Char then
922 procedure Process_Link is
923 Look_For_Executable : Boolean := True;
924 Libraries_Present : Boolean := False;
925 Path_Option : constant String_Access :=
926 MLib.Linker_Library_Path_Option;
927 Prj : Project_Id := Project;
930 Skip_Executable : Boolean := False;
933 -- Add the default search directories, to be able to find
934 -- libgnat in call to MLib.Utl.Lib_Directory.
936 Add_Default_Search_Dirs;
938 Library_Paths.Set_Last (0);
940 -- Check if there are library project files
942 if MLib.Tgt.Support_For_Libraries /= None then
943 Set_Libraries (Project, Libraries_Present);
946 -- If there are, add the necessary additional switches
948 if Libraries_Present then
950 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
952 Last_Switches.Increment_Last;
953 Last_Switches.Table (Last_Switches.Last) :=
954 new String'("-L" & MLib.Utl.Lib_Directory);
955 Last_Switches.Increment_Last;
956 Last_Switches.Table (Last_Switches.Last) :=
957 new String'("-lgnarl");
958 Last_Switches.Increment_Last;
959 Last_Switches.Table (Last_Switches.Last) :=
960 new String'("-lgnat");
962 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
963 -- equivalent) with all the library dirs plus the standard GNAT
966 if Path_Option /= null then
968 Option : String_Access;
969 Length : Natural := Path_Option'Length;
973 if MLib.Separate_Run_Path_Options then
975 -- We are going to create one switch of the form
976 -- "-Wl,-rpath,dir_N" for each directory to consider.
978 -- One switch for each library directory
981 Library_Paths.First .. Library_Paths.Last
983 Last_Switches.Increment_Last;
985 (Last_Switches.Last) := new String'
987 Last_Switches.Table (Index).all);
990 -- One switch for the standard GNAT library dir.
992 Last_Switches.Increment_Last;
994 (Last_Switches.Last) := new String'
995 (Path_Option.all & MLib.Utl.Lib_Directory);
998 -- First, compute the exact length for the switch
1001 Library_Paths.First .. Library_Paths.Last
1003 -- Add the length of the library dir plus one for the
1004 -- directory separator.
1008 Library_Paths.Table (Index)'Length + 1;
1011 -- Finally, add the length of the standard GNAT library dir
1013 Length := Length + MLib.Utl.Lib_Directory'Length;
1014 Option := new String (1 .. Length);
1015 Option (1 .. Path_Option'Length) := Path_Option.all;
1016 Current := Path_Option'Length;
1018 -- Put each library dir followed by a dir separator
1021 Library_Paths.First .. Library_Paths.Last
1026 Library_Paths.Table (Index)'Length) :=
1027 Library_Paths.Table (Index).all;
1030 Library_Paths.Table (Index)'Length + 1;
1031 Option (Current) := Path_Separator;
1034 -- Finally put the standard GNAT library dir
1038 Current + MLib.Utl.Lib_Directory'Length) :=
1039 MLib.Utl.Lib_Directory;
1041 -- And add the switch to the last switches
1043 Last_Switches.Increment_Last;
1044 Last_Switches.Table (Last_Switches.Last) :=
1051 -- Check if the first ALI file specified can be found, either in the
1052 -- object directory of the main project or in an object directory of a
1053 -- project file extended by the main project. If the ALI file can be
1054 -- found, replace its name with its absolute path.
1056 Skip_Executable := False;
1058 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1060 -- If we have an executable just reset the flag
1062 if Skip_Executable then
1063 Skip_Executable := False;
1065 -- If -o, set flag so that next switch is not processed
1067 elsif Last_Switches.Table (J).all = "-o" then
1068 Skip_Executable := True;
1074 Switch : constant String :=
1075 Last_Switches.Table (J).all;
1076 ALI_File : constant String (1 .. Switch'Length + 4) :=
1079 Test_Existence : Boolean := False;
1082 Last := Switch'Length;
1084 -- Skip real switches
1086 if Switch'Length /= 0
1087 and then Switch (Switch'First) /= '-'
1089 -- Append ".ali" if file name does not end with it
1091 if Switch'Length <= 4
1092 or else Switch (Switch'Last - 3 .. Switch'Last)
1095 Last := ALI_File'Last;
1098 -- If file name includes directory information, stop if ALI
1101 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1102 Test_Existence := True;
1105 for K in Switch'Range loop
1106 if Switch (K) = '/' or else
1107 Switch (K) = Directory_Separator
1109 Test_Existence := True;
1115 if Test_Existence then
1116 if Is_Regular_File (ALI_File (1 .. Last)) then
1120 -- Look in object directories if ALI file exists
1125 Dir : constant String :=
1126 Get_Name_String (Prj.Object_Directory.Name);
1130 Directory_Separator &
1131 ALI_File (1 .. Last))
1133 -- We have found the correct project, so we
1134 -- replace the file with the absolute path.
1136 Last_Switches.Table (J) :=
1138 (Dir & Directory_Separator &
1139 ALI_File (1 .. Last));
1147 -- Go to the project being extended, if any
1150 exit Project_Loop when Prj = No_Project;
1151 end loop Project_Loop;
1156 end loop Switch_Loop;
1158 -- If a relative path output file has been specified, we add the exec
1161 for J in reverse 1 .. Last_Switches.Last - 1 loop
1162 if Last_Switches.Table (J).all = "-o" then
1163 Check_Relative_Executable
1164 (Name => Last_Switches.Table (J + 1));
1165 Look_For_Executable := False;
1170 if Look_For_Executable then
1171 for J in reverse 1 .. First_Switches.Last - 1 loop
1172 if First_Switches.Table (J).all = "-o" then
1173 Look_For_Executable := False;
1174 Check_Relative_Executable
1175 (Name => First_Switches.Table (J + 1));
1181 -- If no executable is specified, then find the name of the first ALI
1182 -- file on the command line and issue a -o switch with the absolute path
1183 -- of the executable in the exec directory.
1185 if Look_For_Executable then
1186 for J in 1 .. Last_Switches.Last loop
1187 Arg := Last_Switches.Table (J);
1190 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1192 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1194 Last := Arg'Last - 4;
1196 elsif Is_Regular_File (Arg.all & ".ali") then
1201 Last_Switches.Increment_Last;
1202 Last_Switches.Table (Last_Switches.Last) :=
1204 Get_Name_String (Project.Exec_Directory.Name);
1205 Last_Switches.Increment_Last;
1206 Last_Switches.Table (Last_Switches.Last) :=
1207 new String'(Name_Buffer (1 .. Name_Len) &
1208 Directory_Separator &
1210 (Base_Name (Arg (Arg'First .. Last))));
1218 ---------------------
1219 -- Set_Library_For --
1220 ---------------------
1222 procedure Set_Library_For
1223 (Project : Project_Id;
1224 Libraries_Present : in out Boolean)
1226 Path_Option : constant String_Access :=
1227 MLib.Linker_Library_Path_Option;
1230 -- Case of library project
1232 if Project.Library then
1233 Libraries_Present := True;
1235 -- Add the -L switch
1237 Last_Switches.Increment_Last;
1238 Last_Switches.Table (Last_Switches.Last) :=
1239 new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1241 -- Add the -l switch
1243 Last_Switches.Increment_Last;
1244 Last_Switches.Table (Last_Switches.Last) :=
1245 new String'("-l" & Get_Name_String (Project.Library_Name));
1247 -- Add the directory to table Library_Paths, to be processed later
1248 -- if library is not static and if Path_Option is not null.
1250 if Project.Library_Kind /= Static
1251 and then Path_Option /= null
1253 Library_Paths.Increment_Last;
1254 Library_Paths.Table (Library_Paths.Last) :=
1255 new String'(Get_Name_String (Project.Library_Dir.Name));
1258 end Set_Library_For;
1260 ---------------------------
1261 -- Test_If_Relative_Path --
1262 ---------------------------
1264 procedure Test_If_Relative_Path
1265 (Switch : in out String_Access;
1269 Makeutl.Test_If_Relative_Path
1270 (Switch, Parent, Including_Non_Switch => False, Including_RTS => True);
1271 end Test_If_Relative_Path;
1277 procedure Non_VMS_Usage is
1281 Put_Line ("List of available commands");
1284 for C in Command_List'Range loop
1285 if not Command_List (C).VMS_Only then
1286 if Targparm.AAMP_On_Target then
1292 Put (To_Lower (Command_List (C).Cname.all));
1295 -- Never call gnatstack with a prefix
1298 Put (Command_List (C).Unixcmd.all);
1300 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1304 Sws : Argument_List_Access renames Command_List (C).Unixsws;
1307 for J in Sws'Range loop
1319 Put_Line ("Commands find, list, metric, pretty, stack, stub and xref " &
1320 "accept project file switches -vPx, -Pprj and -Xnam=val");
1324 -------------------------------------
1325 -- Start of processing for GNATCmd --
1326 -------------------------------------
1336 Prj.Initialize (Project_Tree);
1339 Last_Switches.Set_Last (0);
1341 First_Switches.Init;
1342 First_Switches.Set_Last (0);
1344 Carg_Switches.Set_Last (0);
1345 Rules_Switches.Init;
1346 Rules_Switches.Set_Last (0);
1348 VMS_Conv.Initialize;
1350 Set_Mode (Ada_Only);
1352 -- Add the default search directories, to be able to find system.ads in the
1353 -- subsequent call to Targparm.Get_Target_Parameters.
1355 Add_Default_Search_Dirs;
1357 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1358 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1360 Targparm.Get_Target_Parameters;
1362 -- Add the directory where the GNAT driver is invoked in front of the path,
1363 -- if the GNAT driver is invoked with directory information. Do not do this
1364 -- for VMS, where the notion of path does not really exist.
1368 Command : constant String := Command_Name;
1371 for Index in reverse Command'Range loop
1372 if Command (Index) = Directory_Separator then
1374 Absolute_Dir : constant String :=
1376 (Command (Command'First .. Index));
1378 PATH : constant String :=
1381 Getenv ("PATH").all;
1384 Setenv ("PATH", PATH);
1393 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1394 -- filenames and pathnames to Unix style.
1397 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1399 VMS_Conversion (The_Command);
1401 B_Start := new String'("b__");
1403 -- If not on VMS, scan the command line directly
1406 if Argument_Count = 0 then
1412 if Argument_Count > Command_Arg
1413 and then Argument (Command_Arg) = "-v"
1415 Verbose_Mode := True;
1416 Command_Arg := Command_Arg + 1;
1418 elsif Argument_Count > Command_Arg
1419 and then Argument (Command_Arg) = "-dn"
1421 Keep_Temporary_Files := True;
1422 Command_Arg := Command_Arg + 1;
1429 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1431 if Command_List (The_Command).VMS_Only then
1435 & Command_List (The_Command).Cname.all
1436 & """ can only be used on VMS");
1440 when Constraint_Error =>
1442 -- Check if it is an alternate command
1445 Alternate : Alternate_Command;
1448 Alternate := Alternate_Command'Value
1449 (Argument (Command_Arg));
1450 The_Command := Corresponding_To (Alternate);
1453 when Constraint_Error =>
1455 Fail ("Unknown command: " & Argument (Command_Arg));
1459 -- Get the arguments from the command line and from the eventual
1460 -- argument file(s) specified on the command line.
1462 for Arg in Command_Arg + 1 .. Argument_Count loop
1464 The_Arg : constant String := Argument (Arg);
1467 -- Check if an argument file is specified
1469 if The_Arg (The_Arg'First) = '@' then
1471 Arg_File : Ada.Text_IO.File_Type;
1472 Line : String (1 .. 256);
1476 -- Open the file and fail if the file cannot be found
1481 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1486 (Standard_Error, "Cannot open argument file """);
1489 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1491 Put_Line (Standard_Error, """");
1495 -- Read line by line and put the content of each non-
1496 -- empty line in the Last_Switches table.
1498 while not End_Of_File (Arg_File) loop
1499 Get_Line (Arg_File, Line, Last);
1502 Last_Switches.Increment_Last;
1503 Last_Switches.Table (Last_Switches.Last) :=
1504 new String'(Line (1 .. Last));
1512 -- It is not an argument file; just put the argument in
1513 -- the Last_Switches table.
1515 Last_Switches.Increment_Last;
1516 Last_Switches.Table (Last_Switches.Last) :=
1517 new String'(The_Arg);
1525 Program : String_Access;
1526 Exec_Path : String_Access;
1529 if The_Command = Stack then
1530 -- Never call gnatstack with a prefix
1532 Program := new String'(Command_List (The_Command).Unixcmd.all);
1536 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1539 -- Locate the executable for the command
1541 Exec_Path := Locate_Exec_On_Path (Program.all);
1543 if Exec_Path = null then
1544 Put_Line (Standard_Error, "could not locate " & Program.all);
1548 -- If there are switches for the executable, put them as first switches
1550 if Command_List (The_Command).Unixsws /= null then
1551 for J in Command_List (The_Command).Unixsws'Range loop
1552 First_Switches.Increment_Last;
1553 First_Switches.Table (First_Switches.Last) :=
1554 Command_List (The_Command).Unixsws (J);
1558 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, PRETTY, STACK, STUB,
1559 -- METRIC ad XREF, look for project file related switches.
1561 if The_Command = Bind
1562 or else The_Command = Check
1563 or else The_Command = Sync
1564 or else The_Command = Elim
1565 or else The_Command = Find
1566 or else The_Command = Link
1567 or else The_Command = List
1568 or else The_Command = Xref
1569 or else The_Command = Pretty
1570 or else The_Command = Stack
1571 or else The_Command = Stub
1572 or else The_Command = Metric
1576 Tool_Package_Name := Name_Binder;
1577 Packages_To_Check := Packages_To_Check_By_Binder;
1579 Tool_Package_Name := Name_Check;
1580 Packages_To_Check := Packages_To_Check_By_Check;
1582 Tool_Package_Name := Name_Synchronize;
1583 Packages_To_Check := Packages_To_Check_By_Sync;
1585 Tool_Package_Name := Name_Eliminate;
1586 Packages_To_Check := Packages_To_Check_By_Eliminate;
1588 Tool_Package_Name := Name_Finder;
1589 Packages_To_Check := Packages_To_Check_By_Finder;
1591 Tool_Package_Name := Name_Linker;
1592 Packages_To_Check := Packages_To_Check_By_Linker;
1594 Tool_Package_Name := Name_Gnatls;
1595 Packages_To_Check := Packages_To_Check_By_Gnatls;
1597 Tool_Package_Name := Name_Metrics;
1598 Packages_To_Check := Packages_To_Check_By_Metric;
1600 Tool_Package_Name := Name_Pretty_Printer;
1601 Packages_To_Check := Packages_To_Check_By_Pretty;
1603 Tool_Package_Name := Name_Stack;
1604 Packages_To_Check := Packages_To_Check_By_Stack;
1606 Tool_Package_Name := Name_Gnatstub;
1607 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1609 Tool_Package_Name := Name_Cross_Reference;
1610 Packages_To_Check := Packages_To_Check_By_Xref;
1615 -- Check that the switches are consistent. Detect project file
1616 -- related switches.
1620 Arg_Num : Positive := 1;
1621 Argv : String_Access;
1623 procedure Remove_Switch (Num : Positive);
1624 -- Remove a project related switch from table Last_Switches
1630 procedure Remove_Switch (Num : Positive) is
1632 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1633 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1634 Last_Switches.Decrement_Last;
1637 -- Start of processing for Inspect_Switches
1640 while Arg_Num <= Last_Switches.Last loop
1641 Argv := Last_Switches.Table (Arg_Num);
1643 if Argv (Argv'First) = '-' then
1644 if Argv'Length = 1 then
1646 ("switch character cannot be followed by a blank");
1649 -- The two style project files (-p and -P) cannot be used
1652 if (The_Command = Find or else The_Command = Xref)
1653 and then Argv (2) = 'p'
1655 Old_Project_File_Used := True;
1656 if Project_File /= null then
1657 Fail ("-P and -p cannot be used together");
1661 -- --subdirs=... Specify Subdirs
1663 if Argv'Length > Subdirs_Option'Length and then
1665 (Argv'First .. Argv'First + Subdirs_Option'Length - 1) =
1671 (Argv'First + Subdirs_Option'Length .. Argv'Last));
1673 Remove_Switch (Arg_Num);
1675 -- -aPdir Add dir to the project search path
1677 elsif Argv'Length > 3
1678 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1680 Add_Search_Project_Directory
1681 (Argv (Argv'First + 3 .. Argv'Last));
1683 Remove_Switch (Arg_Num);
1685 -- -eL Follow links for files
1687 elsif Argv.all = "-eL" then
1688 Follow_Links_For_Files := True;
1690 Remove_Switch (Arg_Num);
1692 -- -vPx Specify verbosity while parsing project files
1694 elsif Argv'Length = 4
1695 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1697 case Argv (Argv'Last) is
1699 Current_Verbosity := Prj.Default;
1701 Current_Verbosity := Prj.Medium;
1703 Current_Verbosity := Prj.High;
1705 Fail ("Invalid switch: " & Argv.all);
1708 Remove_Switch (Arg_Num);
1710 -- -Pproject_file Specify project file to be used
1712 elsif Argv (Argv'First + 1) = 'P' then
1714 -- Only one -P switch can be used
1716 if Project_File /= null then
1719 & ": second project file forbidden (first is """
1723 -- The two style project files (-p and -P) cannot be
1726 elsif Old_Project_File_Used then
1727 Fail ("-p and -P cannot be used together");
1729 elsif Argv'Length = 2 then
1731 -- There is space between -P and the project file
1732 -- name. -P cannot be the last option.
1734 if Arg_Num = Last_Switches.Last then
1735 Fail ("project file name missing after -P");
1738 Remove_Switch (Arg_Num);
1739 Argv := Last_Switches.Table (Arg_Num);
1741 -- After -P, there must be a project file name,
1742 -- not another switch.
1744 if Argv (Argv'First) = '-' then
1745 Fail ("project file name missing after -P");
1748 Project_File := new String'(Argv.all);
1753 -- No space between -P and project file name
1756 new String'(Argv (Argv'First + 2 .. Argv'Last));
1759 Remove_Switch (Arg_Num);
1761 -- -Xexternal=value Specify an external reference to be
1762 -- used in project files
1764 elsif Argv'Length >= 5
1765 and then Argv (Argv'First + 1) = 'X'
1768 Equal_Pos : constant Natural :=
1771 Argv (Argv'First + 2 .. Argv'Last));
1773 if Equal_Pos >= Argv'First + 3 and then
1774 Equal_Pos /= Argv'Last then
1775 Add (External_Name =>
1776 Argv (Argv'First + 2 .. Equal_Pos - 1),
1777 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1781 & " is not a valid external assignment.");
1785 Remove_Switch (Arg_Num);
1788 (The_Command = Check or else
1789 The_Command = Sync or else
1790 The_Command = Pretty or else
1791 The_Command = Metric or else
1792 The_Command = Stack or else
1794 and then Argv'Length = 2
1795 and then Argv (2) = 'U'
1797 All_Projects := True;
1798 Remove_Switch (Arg_Num);
1801 Arg_Num := Arg_Num + 1;
1804 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1805 or else The_Command = Sync
1806 or else The_Command = Metric
1807 or else The_Command = Pretty)
1808 and then Project_File /= null
1809 and then All_Projects
1811 if ASIS_Main /= null then
1812 Fail ("cannot specify more than one main after -U");
1815 Remove_Switch (Arg_Num);
1819 Arg_Num := Arg_Num + 1;
1822 end Inspect_Switches;
1825 -- If there is a project file specified, parse it, get the switches
1826 -- for the tool and setup PATH environment variables.
1828 if Project_File /= null then
1829 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1832 (Project => Project,
1833 In_Tree => Project_Tree,
1834 Project_File_Name => Project_File.all,
1835 Packages_To_Check => Packages_To_Check,
1836 Is_Config_File => False);
1838 if Project = Prj.No_Project then
1839 Fail ("""" & Project_File.all & """ processing failed");
1842 -- Check if a package with the name of the tool is in the project
1843 -- file and if there is one, get the switches, if any, and scan them.
1846 Pkg : constant Prj.Package_Id :=
1848 (Name => Tool_Package_Name,
1849 In_Packages => Project.Decl.Packages,
1850 In_Tree => Project_Tree);
1852 Element : Package_Element;
1854 Default_Switches_Array : Array_Element_Id;
1856 The_Switches : Prj.Variable_Value;
1857 Current : Prj.String_List_Id;
1858 The_String : String_Element;
1861 if Pkg /= No_Package then
1862 Element := Project_Tree.Packages.Table (Pkg);
1864 -- Packages Gnatls and Gnatstack have a single attribute
1865 -- Switches, that is not an associative array.
1867 if The_Command = List or else The_Command = Stack then
1870 (Variable_Name => Snames.Name_Switches,
1871 In_Variables => Element.Decl.Attributes,
1872 In_Tree => Project_Tree);
1874 -- Packages Binder (for gnatbind), Cross_Reference (for
1875 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1876 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1877 -- (for gnatcheck), and Metric (for gnatmetric) have an
1878 -- attributed Switches, an associative array, indexed by the
1879 -- name of the file.
1881 -- They also have an attribute Default_Switches, indexed by the
1882 -- name of the programming language.
1885 if The_Switches.Kind = Prj.Undefined then
1886 Default_Switches_Array :=
1888 (Name => Name_Default_Switches,
1889 In_Arrays => Element.Decl.Arrays,
1890 In_Tree => Project_Tree);
1891 The_Switches := Prj.Util.Value_Of
1894 In_Array => Default_Switches_Array,
1895 In_Tree => Project_Tree);
1899 -- If there are switches specified in the package of the
1900 -- project file corresponding to the tool, scan them.
1902 case The_Switches.Kind is
1903 when Prj.Undefined =>
1908 Switch : constant String :=
1909 Get_Name_String (The_Switches.Value);
1912 if Switch'Length > 0 then
1913 First_Switches.Increment_Last;
1914 First_Switches.Table (First_Switches.Last) :=
1915 new String'(Switch);
1920 Current := The_Switches.Values;
1921 while Current /= Prj.Nil_String loop
1922 The_String := Project_Tree.String_Elements.
1926 Switch : constant String :=
1927 Get_Name_String (The_String.Value);
1930 if Switch'Length > 0 then
1931 First_Switches.Increment_Last;
1932 First_Switches.Table (First_Switches.Last) :=
1933 new String'(Switch);
1937 Current := The_String.Next;
1943 if The_Command = Bind
1944 or else The_Command = Link
1945 or else The_Command = Elim
1947 Change_Dir (Get_Name_String (Project.Object_Directory.Name));
1950 -- Set up the env vars for project path files
1952 Prj.Env.Set_Ada_Paths
1953 (Project, Project_Tree, Including_Libraries => False);
1955 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
1956 -- a configuration pragmas file, if necessary.
1958 if The_Command = Pretty
1959 or else The_Command = Metric
1960 or else The_Command = Stub
1961 or else The_Command = Elim
1962 or else The_Command = Check
1963 or else The_Command = Sync
1965 -- If there are switches in package Compiler, put them in the
1966 -- Carg_Switches table.
1969 Pkg : constant Prj.Package_Id :=
1971 (Name => Name_Compiler,
1972 In_Packages => Project.Decl.Packages,
1973 In_Tree => Project_Tree);
1975 Element : Package_Element;
1977 Default_Switches_Array : Array_Element_Id;
1979 The_Switches : Prj.Variable_Value;
1980 Current : Prj.String_List_Id;
1981 The_String : String_Element;
1984 if Pkg /= No_Package then
1985 Element := Project_Tree.Packages.Table (Pkg);
1987 Default_Switches_Array :=
1989 (Name => Name_Default_Switches,
1990 In_Arrays => Element.Decl.Arrays,
1991 In_Tree => Project_Tree);
1992 The_Switches := Prj.Util.Value_Of
1995 In_Array => Default_Switches_Array,
1996 In_Tree => Project_Tree);
1998 -- If there are switches specified in the package of the
1999 -- project file corresponding to the tool, scan them.
2001 case The_Switches.Kind is
2002 when Prj.Undefined =>
2007 Switch : constant String :=
2008 Get_Name_String (The_Switches.Value);
2010 if Switch'Length > 0 then
2011 Add_To_Carg_Switches (new String'(Switch));
2016 Current := The_Switches.Values;
2017 while Current /= Prj.Nil_String loop
2019 Project_Tree.String_Elements.Table (Current);
2022 Switch : constant String :=
2023 Get_Name_String (The_String.Value);
2025 if Switch'Length > 0 then
2026 Add_To_Carg_Switches (new String'(Switch));
2030 Current := The_String.Next;
2036 -- If -cargs is one of the switches, move the following switches
2037 -- to the Carg_Switches table.
2039 for J in 1 .. First_Switches.Last loop
2040 if First_Switches.Table (J).all = "-cargs" then
2046 -- Move the switches that are before -rules when the
2047 -- command is CHECK.
2050 while K <= First_Switches.Last
2052 (The_Command /= Check
2053 or else First_Switches.Table (K).all /= "-rules")
2055 Add_To_Carg_Switches (First_Switches.Table (K));
2059 if K > First_Switches.Last then
2060 First_Switches.Set_Last (J - 1);
2064 while K <= First_Switches.Last loop
2066 First_Switches.Table (Last) :=
2067 First_Switches.Table (K);
2071 First_Switches.Set_Last (Last);
2079 for J in 1 .. Last_Switches.Last loop
2080 if Last_Switches.Table (J).all = "-cargs" then
2086 -- Move the switches that are before -rules when the
2087 -- command is CHECK.
2090 while K <= Last_Switches.Last
2092 (The_Command /= Check
2094 Last_Switches.Table (K).all /= "-rules")
2096 Add_To_Carg_Switches (Last_Switches.Table (K));
2100 if K > Last_Switches.Last then
2101 Last_Switches.Set_Last (J - 1);
2105 while K <= Last_Switches.Last loop
2107 Last_Switches.Table (Last) :=
2108 Last_Switches.Table (K);
2112 Last_Switches.Set_Last (Last);
2121 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2124 if CP_File /= No_Path then
2125 if The_Command = Elim then
2126 First_Switches.Increment_Last;
2127 First_Switches.Table (First_Switches.Last) :=
2128 new String'("-C" & Get_Name_String (CP_File));
2131 Add_To_Carg_Switches
2132 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2138 if The_Command = Link then
2142 if The_Command = Link or The_Command = Bind then
2144 -- For files that are specified as relative paths with directory
2145 -- information, we convert them to absolute paths, with parent
2146 -- being the current working directory if specified on the command
2147 -- line and the project directory if specified in the project
2148 -- file. This is what gnatmake is doing for linker and binder
2151 for J in 1 .. Last_Switches.Last loop
2152 Test_If_Relative_Path
2153 (Last_Switches.Table (J), Current_Work_Dir);
2156 Get_Name_String (Project.Directory.Name);
2159 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2161 for J in 1 .. First_Switches.Last loop
2162 Test_If_Relative_Path
2163 (First_Switches.Table (J), Project_Dir);
2167 elsif The_Command = Stub then
2169 File_Index : Integer := 0;
2170 Dir_Index : Integer := 0;
2171 Last : constant Integer := Last_Switches.Last;
2174 for Index in 1 .. Last loop
2175 if Last_Switches.Table (Index)
2176 (Last_Switches.Table (Index)'First) /= '-'
2178 File_Index := Index;
2183 -- If the naming scheme of the project file is not standard,
2184 -- and if the file name ends with the spec suffix, then
2185 -- indicate to gnatstub the name of the body file with
2188 if Body_Suffix_Id_Of (Project_Tree, Name_Ada, Project.Naming) /=
2189 Prj.Default_Ada_Spec_Suffix
2191 if File_Index /= 0 then
2193 Spec : constant String :=
2194 Base_Name (Last_Switches.Table (File_Index).all);
2195 Last : Natural := Spec'Last;
2200 (Project_Tree, Name_Ada, Project.Naming));
2202 if Spec'Length > Name_Len
2203 and then Spec (Last - Name_Len + 1 .. Last) =
2204 Name_Buffer (1 .. Name_Len)
2206 Last := Last - Name_Len;
2209 (Project_Tree, Name_Ada, Project.Naming));
2210 Last_Switches.Increment_Last;
2211 Last_Switches.Table (Last_Switches.Last) :=
2213 Last_Switches.Increment_Last;
2214 Last_Switches.Table (Last_Switches.Last) :=
2215 new String'(Spec (Spec'First .. Last) &
2216 Name_Buffer (1 .. Name_Len));
2222 -- Add the directory of the spec as the destination directory
2223 -- of the body, if there is no destination directory already
2226 if File_Index /= 0 then
2227 for Index in File_Index + 1 .. Last loop
2228 if Last_Switches.Table (Index)
2229 (Last_Switches.Table (Index)'First) /= '-'
2236 if Dir_Index = 0 then
2237 Last_Switches.Increment_Last;
2238 Last_Switches.Table (Last_Switches.Last) :=
2240 (Dir_Name (Last_Switches.Table (File_Index).all));
2246 -- For gnatmetric, the generated files should be put in the object
2247 -- directory. This must be the first switch, because it may be
2248 -- overridden by a switch in package Metrics in the project file or
2249 -- by a command line option. Note that we don't add the -d= switch
2250 -- if there is no object directory available.
2252 if The_Command = Metric
2253 and then Project.Object_Directory /= No_Path_Information
2255 First_Switches.Increment_Last;
2256 First_Switches.Table (2 .. First_Switches.Last) :=
2257 First_Switches.Table (1 .. First_Switches.Last - 1);
2258 First_Switches.Table (1) :=
2260 Get_Name_String (Project.Object_Directory.Name));
2263 -- For gnat check, -rules and the following switches need to be the
2264 -- last options. So, we move all these switches to table
2267 if The_Command = Check then
2270 -- Set to rank of options preceding "-rules"
2272 In_Rules_Switches : Boolean;
2273 -- Set to True when options "-rules" is found
2276 New_Last := First_Switches.Last;
2277 In_Rules_Switches := False;
2279 for J in 1 .. First_Switches.Last loop
2280 if In_Rules_Switches then
2281 Add_To_Rules_Switches (First_Switches.Table (J));
2283 elsif First_Switches.Table (J).all = "-rules" then
2285 In_Rules_Switches := True;
2289 if In_Rules_Switches then
2290 First_Switches.Set_Last (New_Last);
2293 New_Last := Last_Switches.Last;
2294 In_Rules_Switches := False;
2296 for J in 1 .. Last_Switches.Last loop
2297 if In_Rules_Switches then
2298 Add_To_Rules_Switches (Last_Switches.Table (J));
2300 elsif Last_Switches.Table (J).all = "-rules" then
2302 In_Rules_Switches := True;
2306 if In_Rules_Switches then
2307 Last_Switches.Set_Last (New_Last);
2312 -- For gnat check, sync, metric or pretty with -U + a main, get the
2313 -- list of sources from the closure and add them to the arguments.
2315 if ASIS_Main /= null then
2318 -- On VMS, set up the env var again for source dirs file. This is
2319 -- because the call to gnatmake has set this env var to another
2320 -- file that has now been deleted.
2322 if Hostparm.OpenVMS then
2324 -- First make sure that the recorded file names are empty
2326 Prj.Env.Initialize (Project_Tree);
2328 Prj.Env.Set_Ada_Paths
2329 (Project, Project_Tree, Including_Libraries => False);
2332 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2333 -- and gnat stack, if no file has been put on the command line, call
2334 -- tool with all the sources of the main project.
2336 elsif The_Command = Check or else
2337 The_Command = Sync or else
2338 The_Command = Pretty or else
2339 The_Command = Metric or else
2340 The_Command = List or else
2347 -- Gather all the arguments and invoke the executable
2350 The_Args : Argument_List
2351 (1 .. First_Switches.Last +
2352 Last_Switches.Last +
2353 Carg_Switches.Last +
2354 Rules_Switches.Last);
2355 Arg_Num : Natural := 0;
2358 for J in 1 .. First_Switches.Last loop
2359 Arg_Num := Arg_Num + 1;
2360 The_Args (Arg_Num) := First_Switches.Table (J);
2363 for J in 1 .. Last_Switches.Last loop
2364 Arg_Num := Arg_Num + 1;
2365 The_Args (Arg_Num) := Last_Switches.Table (J);
2368 for J in 1 .. Carg_Switches.Last loop
2369 Arg_Num := Arg_Num + 1;
2370 The_Args (Arg_Num) := Carg_Switches.Table (J);
2373 for J in 1 .. Rules_Switches.Last loop
2374 Arg_Num := Arg_Num + 1;
2375 The_Args (Arg_Num) := Rules_Switches.Table (J);
2378 -- If Display_Command is on, only display the generated command
2380 if Display_Command then
2381 Put (Standard_Error, "generated command -->");
2382 Put (Standard_Error, Exec_Path.all);
2384 for Arg in The_Args'Range loop
2385 Put (Standard_Error, " ");
2386 Put (Standard_Error, The_Args (Arg).all);
2389 Put (Standard_Error, "<--");
2390 New_Line (Standard_Error);
2394 if Verbose_Mode then
2395 Output.Write_Str (Exec_Path.all);
2397 for Arg in The_Args'Range loop
2398 Output.Write_Char (' ');
2399 Output.Write_Str (The_Args (Arg).all);
2406 Exit_Status (Spawn (Exec_Path.all, The_Args));
2413 if not Keep_Temporary_Files then
2414 Prj.Env.Delete_All_Path_Files (Project_Tree);
2415 Delete_Temp_Config_Files;
2418 Set_Exit_Status (Failure);
2421 if not Keep_Temporary_Files then
2422 Prj.Env.Delete_All_Path_Files (Project_Tree);
2423 Delete_Temp_Config_Files;
2426 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2427 -- return an understandable VMS exit status. However the exit status
2428 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2429 -- just a simple success or failure on VMS.
2431 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2432 Set_Exit_Status (Failure);
2434 Set_Exit_Status (My_Exit_Status);