1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
30 with MLib.Tgt; use MLib.Tgt;
32 with Namet; use Namet;
34 with Osint; use Osint;
38 with Prj.Ext; use Prj.Ext;
40 with Prj.Util; use Prj.Util;
42 with Snames; use Snames;
44 with Types; use Types;
45 with Hostparm; use Hostparm;
46 -- Used to determine if we are in VMS or not for error message purposes
48 with Ada.Characters.Handling; use Ada.Characters.Handling;
49 with Ada.Command_Line; use Ada.Command_Line;
50 with Ada.Text_IO; use Ada.Text_IO;
52 with GNAT.OS_Lib; use GNAT.OS_Lib;
56 with VMS_Conv; use VMS_Conv;
59 Project_Tree : constant Project_Tree_Ref := new Project_Tree_Data;
60 Project_File : String_Access;
61 Project : Prj.Project_Id;
62 Current_Verbosity : Prj.Verbosity := Prj.Default;
63 Tool_Package_Name : Name_Id := No_Name;
65 Old_Project_File_Used : Boolean := False;
66 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
67 -- an old fashioned project file. -p cannot be used in conjonction
70 Max_Files_On_The_Command_Line : constant := 30; -- Arbitrary
72 Temp_File_Name : String_Access := null;
73 -- The name of the temporary text file to put a list of source/object
74 -- files to pass to a tool, when there are more than
75 -- Max_Files_On_The_Command_Line files.
77 package First_Switches is new Table.Table
78 (Table_Component_Type => String_Access,
79 Table_Index_Type => Integer,
82 Table_Increment => 100,
83 Table_Name => "Gnatcmd.First_Switches");
84 -- A table to keep the switches from the project file
86 package Carg_Switches is new Table.Table
87 (Table_Component_Type => String_Access,
88 Table_Index_Type => Integer,
91 Table_Increment => 100,
92 Table_Name => "Gnatcmd.Carg_Switches");
93 -- A table to keep the switches following -cargs for ASIS tools
95 package Library_Paths is new Table.Table (
96 Table_Component_Type => String_Access,
97 Table_Index_Type => Integer,
100 Table_Increment => 100,
101 Table_Name => "Make.Library_Path");
103 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
104 -- tool. We allocate objects because we cannot declare aliased objects
105 -- as we are in a procedure, not a library level package.
107 Naming_String : constant String_Access := new String'("naming");
108 Binder_String : constant String_Access := new String'("binder");
109 Eliminate_String : constant String_Access := new String'("eliminate");
110 Finder_String : constant String_Access := new String'("finder");
111 Linker_String : constant String_Access := new String'("linker");
112 Gnatls_String : constant String_Access := new String'("gnatls");
113 Pretty_String : constant String_Access := new String'("pretty_printer");
114 Gnatstub_String : constant String_Access := new String'("gnatstub");
115 Metric_String : constant String_Access := new String'("metrics");
116 Xref_String : constant String_Access := new String'("cross_reference");
118 Packages_To_Check_By_Binder : constant String_List_Access :=
119 new String_List'((Naming_String, Binder_String));
121 Packages_To_Check_By_Eliminate : constant String_List_Access :=
122 new String_List'((Naming_String, Eliminate_String));
124 Packages_To_Check_By_Finder : constant String_List_Access :=
125 new String_List'((Naming_String, Finder_String));
127 Packages_To_Check_By_Linker : constant String_List_Access :=
128 new String_List'((Naming_String, Linker_String));
130 Packages_To_Check_By_Gnatls : constant String_List_Access :=
131 new String_List'((Naming_String, Gnatls_String));
133 Packages_To_Check_By_Pretty : constant String_List_Access :=
134 new String_List'((Naming_String, Pretty_String));
136 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
137 new String_List'((Naming_String, Gnatstub_String));
139 Packages_To_Check_By_Metric : constant String_List_Access :=
140 new String_List'((Naming_String, Metric_String));
142 Packages_To_Check_By_Xref : constant String_List_Access :=
143 new String_List'((Naming_String, Xref_String));
145 Packages_To_Check : String_List_Access := Prj.All_Packages;
147 ----------------------------------
148 -- Declarations for GNATCMD use --
149 ----------------------------------
151 The_Command : Command_Type;
152 -- The command specified in the invocation of the GNAT driver
154 Command_Arg : Positive := 1;
155 -- The index of the command in the arguments of the GNAT driver
157 My_Exit_Status : Exit_Status := Success;
158 -- The exit status of the spawned tool. Used to set the correct VMS
161 Current_Work_Dir : constant String := Get_Current_Dir;
162 -- The path of the working directory
164 All_Projects : Boolean := False;
165 -- Flag used for GNAT PRETTY and GNAT METRIC to indicate that
166 -- the underlying tool (gnatpp or gnatmetric) should be invoked for all
167 -- sources of all projects.
169 -----------------------
170 -- Local Subprograms --
171 -----------------------
173 procedure Add_To_Carg_Switches (Switch : String_Access);
174 -- Add a switch to the Carg_Switches table. If it is the first one,
175 -- put the switch "-cargs" at the beginning of the table.
177 procedure Check_Files;
178 -- For GNAT LIST, GNAT PRETTY and GNAT METRIC, check if a project
179 -- file is specified, without any file arguments. If it is the case,
180 -- invoke the GNAT tool with the proper list of files, derived from
181 -- the sources of the project.
183 function Check_Project
184 (Project : Project_Id;
185 Root_Project : Project_Id) return Boolean;
186 -- Returns True if Project = Root_Project.
187 -- For GNAT METRIC, also returns True if Project is extended by
190 procedure Check_Relative_Executable (Name : in out String_Access);
191 -- Check if an executable is specified as a relative path.
192 -- If it is, and the path contains directory information, fail.
193 -- Otherwise, prepend the exec directory.
194 -- This procedure is only used for GNAT LINK when a project file
197 function Configuration_Pragmas_File return Name_Id;
198 -- Return an argument, if there is a configuration pragmas file to be
199 -- specified for Project, otherwise return No_Name.
200 -- Used for gnatstub (GNAT STUB), gnatpp (GNAT PRETTY), gnatelim
201 -- (GNAT ELIM), and gnatmetric (GNAT METRIC).
203 procedure Delete_Temp_Config_Files;
204 -- Delete all temporary config files
206 function Index (Char : Character; Str : String) return Natural;
207 -- Returns the first occurrence of Char in Str.
208 -- Returns 0 if Char is not in Str.
210 procedure Non_VMS_Usage;
211 -- Display usage for platforms other than VMS
213 procedure Process_Link;
214 -- Process GNAT LINK, when there is a project file specified
216 procedure Set_Library_For
217 (Project : Project_Id;
218 There_Are_Libraries : in out Boolean);
219 -- If Project is a library project, add the correct
220 -- -L and -l switches to the linker invocation.
222 procedure Set_Libraries is
223 new For_Every_Project_Imported (Boolean, Set_Library_For);
224 -- Add the -L and -l switches to the linker for all
225 -- of the library projects.
227 procedure Test_If_Relative_Path
228 (Switch : in out String_Access;
230 -- Test if Switch is a relative search path switch.
231 -- If it is and it includes directory information, prepend the path with
232 -- Parent.This subprogram is only called when using project files.
234 --------------------------
235 -- Add_To_Carg_Switches --
236 --------------------------
238 procedure Add_To_Carg_Switches (Switch : String_Access) is
240 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
242 if Carg_Switches.Last = 0 then
243 Carg_Switches.Increment_Last;
244 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
247 Carg_Switches.Increment_Last;
248 Carg_Switches.Table (Carg_Switches.Last) := Switch;
249 end Add_To_Carg_Switches;
255 procedure Check_Files is
256 Add_Sources : Boolean := True;
257 Unit_Data : Prj.Unit_Data;
258 Subunit : Boolean := False;
261 -- Check if there is at least one argument that is not a switch
263 for Index in 1 .. Last_Switches.Last loop
264 if Last_Switches.Table (Index) (1) /= '-' then
265 Add_Sources := False;
270 -- If all arguments were switches, add the path names of
271 -- all the sources of the main project.
275 Current_Last : constant Integer := Last_Switches.Last;
277 for Unit in Unit_Table.First ..
278 Unit_Table.Last (Project_Tree.Units)
280 Unit_Data := Project_Tree.Units.Table (Unit);
282 -- For gnatls, we only need to put the library units,
283 -- body or spec, but not the subunits.
285 if The_Command = List then
287 Unit_Data.File_Names (Body_Part).Name /= No_Name
289 -- There is a body; check if it is for this
292 if Unit_Data.File_Names (Body_Part).Project =
297 if Unit_Data.File_Names (Specification).Name =
300 -- We have a body with no spec: we need
301 -- to check if this is a subunit, because
302 -- gnatls will complain about subunits.
305 Src_Ind : Source_File_Index;
308 Src_Ind := Sinput.P.Load_Project_File
310 (Unit_Data.File_Names
314 Sinput.P.Source_File_Is_Subunit
320 Last_Switches.Increment_Last;
321 Last_Switches.Table (Last_Switches.Last) :=
324 (Unit_Data.File_Names
325 (Body_Part).Display_Name));
329 elsif Unit_Data.File_Names (Specification).Name /=
332 -- We have a spec with no body; check if it is
335 if Unit_Data.File_Names (Specification).Project =
338 Last_Switches.Increment_Last;
339 Last_Switches.Table (Last_Switches.Last) :=
342 (Unit_Data.File_Names
343 (Specification).Display_Name));
348 -- For gnatpp and gnatmetric, put all sources
349 -- of the project, or of all projects if -U was specified.
351 for Kind in Spec_Or_Body loop
353 -- Put only sources that belong to the main
357 (Unit_Data.File_Names (Kind).Project, Project)
359 Last_Switches.Increment_Last;
360 Last_Switches.Table (Last_Switches.Last) :=
363 (Unit_Data.File_Names
364 (Kind).Display_Path));
370 -- If the list of files is too long, create a temporary
371 -- text file that lists these files, and pass this temp
372 -- file to gnatpp or gnatmetric using switch -files=.
374 if Last_Switches.Last - Current_Last >
375 Max_Files_On_The_Command_Line
378 Temp_File_FD : File_Descriptor;
379 Buffer : String (1 .. 1_000);
381 OK : Boolean := True;
384 Create_Temp_File (Temp_File_FD, Temp_File_Name);
386 if Temp_File_Name /= null then
387 for Index in Current_Last + 1 ..
390 Len := Last_Switches.Table (Index)'Length;
392 Last_Switches.Table (Index).all;
394 Buffer (Len) := ASCII.LF;
395 Buffer (Len + 1) := ASCII.NUL;
404 Close (Temp_File_FD, OK);
406 Close (Temp_File_FD, OK);
410 -- If there were any problem creating the temp
411 -- file, then pass the list of files.
415 -- Replace the list of files with
416 -- "-files=<temp file name>".
418 Last_Switches.Set_Last (Current_Last + 1);
419 Last_Switches.Table (Last_Switches.Last) :=
420 new String'("-files=" & Temp_File_Name.all);
433 function Check_Project
434 (Project : Project_Id;
435 Root_Project : Project_Id) return Boolean
438 if Project = No_Project then
441 elsif All_Projects or Project = Root_Project then
444 elsif The_Command = Metric then
446 Data : Project_Data :=
447 Project_Tree.Projects.Table (Root_Project);
450 while Data.Extends /= No_Project loop
451 if Project = Data.Extends then
455 Data := Project_Tree.Projects.Table (Data.Extends);
463 -------------------------------
464 -- Check_Relative_Executable --
465 -------------------------------
467 procedure Check_Relative_Executable (Name : in out String_Access) is
468 Exec_File_Name : constant String := Name.all;
471 if not Is_Absolute_Path (Exec_File_Name) then
472 for Index in Exec_File_Name'Range loop
473 if Exec_File_Name (Index) = Directory_Separator then
474 Fail ("relative executable (""" &
476 """) with directory part not allowed " &
477 "when using project files");
481 Get_Name_String (Project_Tree.Projects.Table
482 (Project).Exec_Directory);
484 if Name_Buffer (Name_Len) /= Directory_Separator then
485 Name_Len := Name_Len + 1;
486 Name_Buffer (Name_Len) := Directory_Separator;
489 Name_Buffer (Name_Len + 1 ..
490 Name_Len + Exec_File_Name'Length) :=
492 Name_Len := Name_Len + Exec_File_Name'Length;
493 Name := new String'(Name_Buffer (1 .. Name_Len));
495 end Check_Relative_Executable;
497 --------------------------------
498 -- Configuration_Pragmas_File --
499 --------------------------------
501 function Configuration_Pragmas_File return Name_Id is
503 Prj.Env.Create_Config_Pragmas_File
504 (Project, Project, Project_Tree, Include_Config_Files => False);
505 return Project_Tree.Projects.Table (Project).Config_File_Name;
506 end Configuration_Pragmas_File;
508 ------------------------------
509 -- Delete_Temp_Config_Files --
510 ------------------------------
512 procedure Delete_Temp_Config_Files is
516 if not Keep_Temporary_Files then
517 if Project /= No_Project then
518 for Prj in Project_Table.First ..
519 Project_Table.Last (Project_Tree.Projects)
522 Project_Tree.Projects.Table (Prj).Config_File_Temp
525 Output.Write_Str ("Deleting temp configuration file """);
528 (Project_Tree.Projects.Table
529 (Prj).Config_File_Name));
530 Output.Write_Line ("""");
534 (Name => Get_Name_String
535 (Project_Tree.Projects.Table
536 (Prj).Config_File_Name),
542 -- If a temporary text file that contains a list of files for a tool
543 -- has been created, delete this temporary file.
545 if Temp_File_Name /= null then
546 Delete_File (Temp_File_Name.all, Success);
549 end Delete_Temp_Config_Files;
555 function Index (Char : Character; Str : String) return Natural is
557 for Index in Str'Range loop
558 if Str (Index) = Char then
570 procedure Process_Link is
571 Look_For_Executable : Boolean := True;
572 There_Are_Libraries : Boolean := False;
573 Path_Option : constant String_Access :=
574 MLib.Linker_Library_Path_Option;
575 Prj : Project_Id := Project;
578 Skip_Executable : Boolean := False;
581 -- Add the default search directories, to be able to find
582 -- libgnat in call to MLib.Utl.Lib_Directory.
584 Add_Default_Search_Dirs;
586 Library_Paths.Set_Last (0);
588 -- Check if there are library project files
590 if MLib.Tgt.Support_For_Libraries /= MLib.Tgt.None then
591 Set_Libraries (Project, Project_Tree, There_Are_Libraries);
594 -- If there are, add the necessary additional switches
596 if There_Are_Libraries then
598 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
600 Last_Switches.Increment_Last;
601 Last_Switches.Table (Last_Switches.Last) :=
602 new String'("-L" & MLib.Utl.Lib_Directory);
603 Last_Switches.Increment_Last;
604 Last_Switches.Table (Last_Switches.Last) :=
605 new String'("-lgnarl");
606 Last_Switches.Increment_Last;
607 Last_Switches.Table (Last_Switches.Last) :=
608 new String'("-lgnat");
610 -- If Path_Option is not null, create the switch
611 -- ("-Wl,-rpath," or equivalent) with all the library dirs
612 -- plus the standard GNAT library dir.
614 if Path_Option /= null then
616 Option : String_Access;
617 Length : Natural := Path_Option'Length;
621 -- First, compute the exact length for the switch
624 Library_Paths.First .. Library_Paths.Last
626 -- Add the length of the library dir plus one
627 -- for the directory separator.
631 Library_Paths.Table (Index)'Length + 1;
634 -- Finally, add the length of the standard GNAT
637 Length := Length + MLib.Utl.Lib_Directory'Length;
638 Option := new String (1 .. Length);
639 Option (1 .. Path_Option'Length) := Path_Option.all;
640 Current := Path_Option'Length;
642 -- Put each library dir followed by a dir separator
645 Library_Paths.First .. Library_Paths.Last
650 Library_Paths.Table (Index)'Length) :=
651 Library_Paths.Table (Index).all;
654 Library_Paths.Table (Index)'Length + 1;
655 Option (Current) := Path_Separator;
658 -- Finally put the standard GNAT library dir
662 Current + MLib.Utl.Lib_Directory'Length) :=
663 MLib.Utl.Lib_Directory;
665 -- And add the switch to the last switches
667 Last_Switches.Increment_Last;
668 Last_Switches.Table (Last_Switches.Last) :=
674 -- Check if the first ALI file specified can be found, either
675 -- in the object directory of the main project or in an object
676 -- directory of a project file extended by the main project.
677 -- If the ALI file can be found, replace its name with its
680 Skip_Executable := False;
682 Switch_Loop : for J in 1 .. Last_Switches.Last loop
684 -- If we have an executable just reset the flag
686 if Skip_Executable then
687 Skip_Executable := False;
689 -- If -o, set flag so that next switch is not processed
691 elsif Last_Switches.Table (J).all = "-o" then
692 Skip_Executable := True;
698 Switch : constant String :=
699 Last_Switches.Table (J).all;
701 ALI_File : constant String (1 .. Switch'Length + 4) :=
704 Test_Existence : Boolean := False;
707 Last := Switch'Length;
709 -- Skip real switches
711 if Switch'Length /= 0
712 and then Switch (Switch'First) /= '-'
714 -- Append ".ali" if file name does not end with it
716 if Switch'Length <= 4
717 or else Switch (Switch'Last - 3 .. Switch'Last)
720 Last := ALI_File'Last;
723 -- If file name includes directory information,
724 -- stop if ALI file exists.
726 if Is_Absolute_Path (ALI_File (1 .. Last)) then
727 Test_Existence := True;
730 for K in Switch'Range loop
731 if Switch (K) = '/' or else
732 Switch (K) = Directory_Separator
734 Test_Existence := True;
740 if Test_Existence then
741 if Is_Regular_File (ALI_File (1 .. Last)) then
745 -- Look in object directories if ALI file exists
750 Dir : constant String :=
752 (Project_Tree.Projects.Table
753 (Prj).Object_Directory);
757 Directory_Separator &
758 ALI_File (1 .. Last))
760 -- We have found the correct project, so we
761 -- replace the file with the absolute path.
763 Last_Switches.Table (J) :=
765 (Dir & Directory_Separator &
766 ALI_File (1 .. Last));
774 -- Go to the project being extended,
778 Project_Tree.Projects.Table (Prj).Extends;
779 exit Project_Loop when Prj = No_Project;
780 end loop Project_Loop;
785 end loop Switch_Loop;
787 -- If a relative path output file has been specified, we add
788 -- the exec directory.
790 for J in reverse 1 .. Last_Switches.Last - 1 loop
791 if Last_Switches.Table (J).all = "-o" then
792 Check_Relative_Executable
793 (Name => Last_Switches.Table (J + 1));
794 Look_For_Executable := False;
799 if Look_For_Executable then
800 for J in reverse 1 .. First_Switches.Last - 1 loop
801 if First_Switches.Table (J).all = "-o" then
802 Look_For_Executable := False;
803 Check_Relative_Executable
804 (Name => First_Switches.Table (J + 1));
810 -- If no executable is specified, then find the name
811 -- of the first ALI file on the command line and issue
812 -- a -o switch with the absolute path of the executable
813 -- in the exec directory.
815 if Look_For_Executable then
816 for J in 1 .. Last_Switches.Last loop
817 Arg := Last_Switches.Table (J);
820 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
822 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
824 Last := Arg'Last - 4;
826 elsif Is_Regular_File (Arg.all & ".ali") then
831 Last_Switches.Increment_Last;
832 Last_Switches.Table (Last_Switches.Last) :=
835 (Project_Tree.Projects.Table
836 (Project).Exec_Directory);
837 Last_Switches.Increment_Last;
838 Last_Switches.Table (Last_Switches.Last) :=
839 new String'(Name_Buffer (1 .. Name_Len) &
840 Directory_Separator &
841 Base_Name (Arg (Arg'First .. Last)) &
842 Get_Executable_Suffix.all);
850 ---------------------
851 -- Set_Library_For --
852 ---------------------
854 procedure Set_Library_For
855 (Project : Project_Id;
856 There_Are_Libraries : in out Boolean)
858 Path_Option : constant String_Access :=
859 MLib.Linker_Library_Path_Option;
862 -- Case of library project
864 if Project_Tree.Projects.Table (Project).Library then
865 There_Are_Libraries := True;
869 Last_Switches.Increment_Last;
870 Last_Switches.Table (Last_Switches.Last) :=
873 (Project_Tree.Projects.Table
874 (Project).Library_Dir));
878 Last_Switches.Increment_Last;
879 Last_Switches.Table (Last_Switches.Last) :=
882 (Project_Tree.Projects.Table
883 (Project).Library_Name));
885 -- Add the directory to table Library_Paths, to be processed later
886 -- if library is not static and if Path_Option is not null.
888 if Project_Tree.Projects.Table (Project).Library_Kind /=
890 and then Path_Option /= null
892 Library_Paths.Increment_Last;
893 Library_Paths.Table (Library_Paths.Last) :=
894 new String'(Get_Name_String
895 (Project_Tree.Projects.Table
896 (Project).Library_Dir));
901 ---------------------------
902 -- Test_If_Relative_Path --
903 ---------------------------
905 procedure Test_If_Relative_Path
906 (Switch : in out String_Access;
910 if Switch /= null then
913 Sw : String (1 .. Switch'Length);
914 Start : Positive := 1;
921 and then (Sw (2) = 'A' or else
932 and then (Sw (2 .. 3) = "aL" or else
933 Sw (2 .. 3) = "aO" or else
939 and then Sw (2 .. 6) = "-RTS="
947 -- If the path is relative, test if it includes directory
948 -- information. If it does, prepend Parent to the path.
950 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
951 for J in Start .. Sw'Last loop
952 if Sw (J) = Directory_Separator then
955 (Sw (1 .. Start - 1) &
957 Directory_Separator &
958 Sw (Start .. Sw'Last));
965 end Test_If_Relative_Path;
971 procedure Non_VMS_Usage is
975 Put_Line ("List of available commands");
978 for C in Command_List'Range loop
979 if not Command_List (C).VMS_Only then
980 Put ("gnat " & To_Lower (Command_List (C).Cname.all));
982 Put (Command_List (C).Unixcmd.all);
985 Sws : Argument_List_Access renames Command_List (C).Unixsws;
988 for J in Sws'Range loop
1000 Put_Line ("Commands find, list, metric, pretty, stub and xref accept " &
1001 "project file switches -vPx, -Pprj and -Xnam=val");
1005 -------------------------------------
1006 -- Start of processing for GNATCmd --
1007 -------------------------------------
1017 Prj.Initialize (Project_Tree);
1020 Last_Switches.Set_Last (0);
1022 First_Switches.Init;
1023 First_Switches.Set_Last (0);
1025 Carg_Switches.Set_Last (0);
1027 VMS_Conv.Initialize;
1029 -- Add the directory where the GNAT driver is invoked in front of the
1030 -- path, if the GNAT driver is invoked with directory information.
1031 -- Only do this if the platform is not VMS, where the notion of path
1032 -- does not really exist.
1036 Command : constant String := Command_Name;
1039 for Index in reverse Command'Range loop
1040 if Command (Index) = Directory_Separator then
1042 Absolute_Dir : constant String :=
1044 (Command (Command'First .. Index));
1046 PATH : constant String :=
1049 Getenv ("PATH").all;
1052 Setenv ("PATH", PATH);
1061 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1062 -- filenames and pathnames to Unix style.
1065 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1067 VMS_Conversion (The_Command);
1069 -- If not on VMS, scan the command line directly
1072 if Argument_Count = 0 then
1078 if Argument_Count > Command_Arg
1079 and then Argument (Command_Arg) = "-v"
1081 Verbose_Mode := True;
1082 Command_Arg := Command_Arg + 1;
1084 elsif Argument_Count > Command_Arg
1085 and then Argument (Command_Arg) = "-dn"
1087 Keep_Temporary_Files := True;
1088 Command_Arg := Command_Arg + 1;
1095 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1097 if Command_List (The_Command).VMS_Only then
1101 Command_List (The_Command).Cname.all,
1102 """ can only be used on VMS");
1106 when Constraint_Error =>
1108 -- Check if it is an alternate command
1111 Alternate : Alternate_Command;
1114 Alternate := Alternate_Command'Value
1115 (Argument (Command_Arg));
1116 The_Command := Corresponding_To (Alternate);
1119 when Constraint_Error =>
1121 Fail ("Unknown command: ", Argument (Command_Arg));
1125 -- Get the arguments from the command line and from the eventual
1126 -- argument file(s) specified on the command line.
1128 for Arg in Command_Arg + 1 .. Argument_Count loop
1130 The_Arg : constant String := Argument (Arg);
1133 -- Check if an argument file is specified
1135 if The_Arg (The_Arg'First) = '@' then
1137 Arg_File : Ada.Text_IO.File_Type;
1138 Line : String (1 .. 256);
1142 -- Open the file and fail if the file cannot be found
1147 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1152 (Standard_Error, "Cannot open argument file """);
1155 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1157 Put_Line (Standard_Error, """");
1161 -- Read line by line and put the content of each
1162 -- non empty line in the Last_Switches table.
1164 while not End_Of_File (Arg_File) loop
1165 Get_Line (Arg_File, Line, Last);
1168 Last_Switches.Increment_Last;
1169 Last_Switches.Table (Last_Switches.Last) :=
1170 new String'(Line (1 .. Last));
1178 -- It is not an argument file; just put the argument in
1179 -- the Last_Switches table.
1181 Last_Switches.Increment_Last;
1182 Last_Switches.Table (Last_Switches.Last) :=
1183 new String'(The_Arg);
1191 Program : constant String :=
1192 Program_Name (Command_List (The_Command).Unixcmd.all).all;
1194 Exec_Path : String_Access;
1197 -- First deal with built-in command(s)
1199 if The_Command = Setup then
1202 Arg_Num : Positive := 1;
1203 Argv : String_Access;
1206 while Arg_Num <= Last_Switches.Last loop
1207 Argv := Last_Switches.Table (Arg_Num);
1209 if Argv (Argv'First) /= '-' then
1210 Fail ("invalid parameter """, Argv.all, """");
1213 if Argv'Length = 1 then
1215 ("switch character cannot be followed by a blank");
1218 -- -vPx Specify verbosity while parsing project files
1221 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1223 case Argv (Argv'Last) is
1225 Current_Verbosity := Prj.Default;
1227 Current_Verbosity := Prj.Medium;
1229 Current_Verbosity := Prj.High;
1231 Fail ("Invalid switch: ", Argv.all);
1234 -- -Pproject_file Specify project file to be used
1236 elsif Argv (Argv'First + 1) = 'P' then
1238 -- Only one -P switch can be used
1240 if Project_File /= null then
1243 ": second project file forbidden (first is """,
1244 Project_File.all & """)");
1246 elsif Argv'Length = 2 then
1248 -- There is space between -P and the project file
1249 -- name. -P cannot be the last option.
1251 if Arg_Num = Last_Switches.Last then
1252 Fail ("project file name missing after -P");
1255 Arg_Num := Arg_Num + 1;
1256 Argv := Last_Switches.Table (Arg_Num);
1258 -- After -P, there must be a project file name,
1259 -- not another switch.
1261 if Argv (Argv'First) = '-' then
1262 Fail ("project file name missing after -P");
1265 Project_File := new String'(Argv.all);
1270 -- No space between -P and project file name
1273 new String'(Argv (Argv'First + 2 .. Argv'Last));
1276 -- -Xexternal=value Specify an external reference to be
1277 -- used in project files
1279 elsif Argv'Length >= 5
1280 and then Argv (Argv'First + 1) = 'X'
1283 Equal_Pos : constant Natural :=
1284 Index ('=', Argv (Argv'First + 2 .. Argv'Last));
1286 if Equal_Pos >= Argv'First + 3 and then
1287 Equal_Pos /= Argv'Last then
1290 Argv (Argv'First + 2 .. Equal_Pos - 1),
1291 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1295 " is not a valid external assignment.");
1299 elsif Argv.all = "-v" then
1300 Verbose_Mode := True;
1302 elsif Argv.all = "-q" then
1303 Quiet_Output := True;
1306 Fail ("invalid parameter """, Argv.all, """");
1310 Arg_Num := Arg_Num + 1;
1313 if Project_File = null then
1314 Fail ("no project file specified");
1317 Setup_Projects := True;
1319 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1321 -- Missing directories are created during processing of the
1325 (Project => Project,
1326 In_Tree => Project_Tree,
1327 Project_File_Name => Project_File.all,
1328 Packages_To_Check => All_Packages);
1330 if Project = Prj.No_Project then
1331 Fail ("""", Project_File.all, """ processing failed");
1334 -- Processing is done
1340 -- Locate the executable for the command
1342 Exec_Path := Locate_Exec_On_Path (Program);
1344 if Exec_Path = null then
1345 Put_Line (Standard_Error, "Couldn't locate " & Program);
1349 -- If there are switches for the executable, put them as first switches
1351 if Command_List (The_Command).Unixsws /= null then
1352 for J in Command_List (The_Command).Unixsws'Range loop
1353 First_Switches.Increment_Last;
1354 First_Switches.Table (First_Switches.Last) :=
1355 Command_List (The_Command).Unixsws (J);
1359 -- For BIND, FIND, LINK, LIST, PRETTY ad XREF, look for project file
1360 -- related switches.
1362 if The_Command = Bind
1363 or else The_Command = Elim
1364 or else The_Command = Find
1365 or else The_Command = Link
1366 or else The_Command = List
1367 or else The_Command = Xref
1368 or else The_Command = Pretty
1369 or else The_Command = Stub
1370 or else The_Command = Metric
1374 Tool_Package_Name := Name_Binder;
1375 Packages_To_Check := Packages_To_Check_By_Binder;
1377 Tool_Package_Name := Name_Eliminate;
1378 Packages_To_Check := Packages_To_Check_By_Eliminate;
1380 Tool_Package_Name := Name_Finder;
1381 Packages_To_Check := Packages_To_Check_By_Finder;
1383 Tool_Package_Name := Name_Linker;
1384 Packages_To_Check := Packages_To_Check_By_Linker;
1386 Tool_Package_Name := Name_Gnatls;
1387 Packages_To_Check := Packages_To_Check_By_Gnatls;
1389 Tool_Package_Name := Name_Metrics;
1390 Packages_To_Check := Packages_To_Check_By_Metric;
1392 Tool_Package_Name := Name_Pretty_Printer;
1393 Packages_To_Check := Packages_To_Check_By_Pretty;
1395 Tool_Package_Name := Name_Gnatstub;
1396 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1398 Tool_Package_Name := Name_Cross_Reference;
1399 Packages_To_Check := Packages_To_Check_By_Xref;
1404 -- Check that the switches are consistent.
1405 -- Detect project file related switches.
1409 Arg_Num : Positive := 1;
1410 Argv : String_Access;
1412 procedure Remove_Switch (Num : Positive);
1413 -- Remove a project related switch from table Last_Switches
1419 procedure Remove_Switch (Num : Positive) is
1421 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1422 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1423 Last_Switches.Decrement_Last;
1426 -- Start of processing for Inspect_Switches
1429 while Arg_Num <= Last_Switches.Last loop
1430 Argv := Last_Switches.Table (Arg_Num);
1432 if Argv (Argv'First) = '-' then
1433 if Argv'Length = 1 then
1435 ("switch character cannot be followed by a blank");
1438 -- The two style project files (-p and -P) cannot be used
1441 if (The_Command = Find or else The_Command = Xref)
1442 and then Argv (2) = 'p'
1444 Old_Project_File_Used := True;
1445 if Project_File /= null then
1446 Fail ("-P and -p cannot be used together");
1450 -- -vPx Specify verbosity while parsing project files
1453 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1455 case Argv (Argv'Last) is
1457 Current_Verbosity := Prj.Default;
1459 Current_Verbosity := Prj.Medium;
1461 Current_Verbosity := Prj.High;
1463 Fail ("Invalid switch: ", Argv.all);
1466 Remove_Switch (Arg_Num);
1468 -- -Pproject_file Specify project file to be used
1470 elsif Argv (Argv'First + 1) = 'P' then
1472 -- Only one -P switch can be used
1474 if Project_File /= null then
1477 ": second project file forbidden (first is """,
1478 Project_File.all & """)");
1480 -- The two style project files (-p and -P) cannot be
1483 elsif Old_Project_File_Used then
1484 Fail ("-p and -P cannot be used together");
1486 elsif Argv'Length = 2 then
1488 -- There is space between -P and the project file
1489 -- name. -P cannot be the last option.
1491 if Arg_Num = Last_Switches.Last then
1492 Fail ("project file name missing after -P");
1495 Remove_Switch (Arg_Num);
1496 Argv := Last_Switches.Table (Arg_Num);
1498 -- After -P, there must be a project file name,
1499 -- not another switch.
1501 if Argv (Argv'First) = '-' then
1502 Fail ("project file name missing after -P");
1505 Project_File := new String'(Argv.all);
1510 -- No space between -P and project file name
1513 new String'(Argv (Argv'First + 2 .. Argv'Last));
1516 Remove_Switch (Arg_Num);
1518 -- -Xexternal=value Specify an external reference to be
1519 -- used in project files
1521 elsif Argv'Length >= 5
1522 and then Argv (Argv'First + 1) = 'X'
1525 Equal_Pos : constant Natural :=
1526 Index ('=', Argv (Argv'First + 2 .. Argv'Last));
1528 if Equal_Pos >= Argv'First + 3 and then
1529 Equal_Pos /= Argv'Last then
1530 Add (External_Name =>
1531 Argv (Argv'First + 2 .. Equal_Pos - 1),
1532 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1536 " is not a valid external assignment.");
1540 Remove_Switch (Arg_Num);
1542 elsif (The_Command = Pretty or else The_Command = Metric)
1543 and then Argv'Length = 2
1544 and then Argv (2) = 'U'
1546 All_Projects := True;
1547 Remove_Switch (Arg_Num);
1550 Arg_Num := Arg_Num + 1;
1554 Arg_Num := Arg_Num + 1;
1557 end Inspect_Switches;
1560 -- If there is a project file specified, parse it, get the switches
1561 -- for the tool and setup PATH environment variables.
1563 if Project_File /= null then
1564 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1567 (Project => Project,
1568 In_Tree => Project_Tree,
1569 Project_File_Name => Project_File.all,
1570 Packages_To_Check => Packages_To_Check);
1572 if Project = Prj.No_Project then
1573 Fail ("""", Project_File.all, """ processing failed");
1576 -- Check if a package with the name of the tool is in the project
1577 -- file and if there is one, get the switches, if any, and scan them.
1580 Data : constant Prj.Project_Data :=
1581 Project_Tree.Projects.Table (Project);
1583 Pkg : constant Prj.Package_Id :=
1585 (Name => Tool_Package_Name,
1586 In_Packages => Data.Decl.Packages,
1587 In_Tree => Project_Tree);
1589 Element : Package_Element;
1591 Default_Switches_Array : Array_Element_Id;
1593 The_Switches : Prj.Variable_Value;
1594 Current : Prj.String_List_Id;
1595 The_String : String_Element;
1598 if Pkg /= No_Package then
1599 Element := Project_Tree.Packages.Table (Pkg);
1601 -- Packages Gnatls has a single attribute Switches, that is
1602 -- not an associative array.
1604 if The_Command = List then
1607 (Variable_Name => Snames.Name_Switches,
1608 In_Variables => Element.Decl.Attributes,
1609 In_Tree => Project_Tree);
1611 -- Packages Binder (for gnatbind), Cross_Reference (for
1612 -- gnatxref), Linker (for gnatlink) Finder (for gnatfind),
1613 -- Pretty_Printer (for gnatpp) Eliminate (for gnatelim) and
1614 -- Metric (for gnatmetric) have an attributed Switches,
1615 -- an associative array, indexed by the name of the file.
1617 -- They also have an attribute Default_Switches, indexed
1618 -- by the name of the programming language.
1621 if The_Switches.Kind = Prj.Undefined then
1622 Default_Switches_Array :=
1624 (Name => Name_Default_Switches,
1625 In_Arrays => Element.Decl.Arrays,
1626 In_Tree => Project_Tree);
1627 The_Switches := Prj.Util.Value_Of
1630 In_Array => Default_Switches_Array,
1631 In_Tree => Project_Tree);
1635 -- If there are switches specified in the package of the
1636 -- project file corresponding to the tool, scan them.
1638 case The_Switches.Kind is
1639 when Prj.Undefined =>
1644 Switch : constant String :=
1645 Get_Name_String (The_Switches.Value);
1648 if Switch'Length > 0 then
1649 First_Switches.Increment_Last;
1650 First_Switches.Table (First_Switches.Last) :=
1651 new String'(Switch);
1656 Current := The_Switches.Values;
1657 while Current /= Prj.Nil_String loop
1658 The_String := Project_Tree.String_Elements.
1662 Switch : constant String :=
1663 Get_Name_String (The_String.Value);
1666 if Switch'Length > 0 then
1667 First_Switches.Increment_Last;
1668 First_Switches.Table (First_Switches.Last) :=
1669 new String'(Switch);
1673 Current := The_String.Next;
1679 if The_Command = Bind
1680 or else The_Command = Link
1681 or else The_Command = Elim
1685 (Project_Tree.Projects.Table
1686 (Project).Object_Directory));
1689 -- Set up the env vars for project path files
1691 Prj.Env.Set_Ada_Paths
1692 (Project, Project_Tree, Including_Libraries => False);
1694 -- For gnatstub, gnatmetric, gnatpp and gnatelim, create
1695 -- a configuration pragmas file, if necessary.
1697 if The_Command = Pretty
1698 or else The_Command = Metric
1699 or else The_Command = Stub
1700 or else The_Command = Elim
1702 -- If -cargs is one of the switches, move the following
1703 -- switches to the Carg_Switches table.
1705 for J in 1 .. First_Switches.Last loop
1706 if First_Switches.Table (J).all = "-cargs" then
1707 for K in J + 1 .. First_Switches.Last loop
1708 Add_To_Carg_Switches (First_Switches.Table (K));
1710 First_Switches.Set_Last (J - 1);
1715 for J in 1 .. Last_Switches.Last loop
1716 if Last_Switches.Table (J).all = "-cargs" then
1717 for K in J + 1 .. Last_Switches.Last loop
1718 Add_To_Carg_Switches (Last_Switches.Table (K));
1720 Last_Switches.Set_Last (J - 1);
1726 CP_File : constant Name_Id := Configuration_Pragmas_File;
1728 if CP_File /= No_Name then
1729 if The_Command = Elim then
1730 First_Switches.Increment_Last;
1731 First_Switches.Table (First_Switches.Last) :=
1732 new String'("-C" & Get_Name_String (CP_File));
1735 Add_To_Carg_Switches
1736 (new String'("-gnatec=" & Get_Name_String (CP_File)));
1742 if The_Command = Link then
1746 if The_Command = Link or The_Command = Bind then
1748 -- For files that are specified as relative paths with directory
1749 -- information, we convert them to absolute paths, with parent
1750 -- being the current working directory if specified on the command
1751 -- line and the project directory if specified in the project
1752 -- file. This is what gnatmake is doing for linker and binder
1755 for J in 1 .. Last_Switches.Last loop
1756 Test_If_Relative_Path
1757 (Last_Switches.Table (J), Current_Work_Dir);
1761 (Project_Tree.Projects.Table (Project).Directory);
1764 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
1767 for J in 1 .. First_Switches.Last loop
1768 Test_If_Relative_Path
1769 (First_Switches.Table (J), Project_Dir);
1773 elsif The_Command = Stub then
1775 Data : constant Prj.Project_Data :=
1776 Project_Tree.Projects.Table (Project);
1777 File_Index : Integer := 0;
1778 Dir_Index : Integer := 0;
1779 Last : constant Integer := Last_Switches.Last;
1782 for Index in 1 .. Last loop
1783 if Last_Switches.Table (Index)
1784 (Last_Switches.Table (Index)'First) /= '-'
1786 File_Index := Index;
1791 -- If the naming scheme of the project file is not standard,
1792 -- and if the file name ends with the spec suffix, then
1793 -- indicate to gnatstub the name of the body file with
1796 if Data.Naming.Ada_Spec_Suffix /=
1797 Prj.Default_Ada_Spec_Suffix
1799 if File_Index /= 0 then
1801 Spec : constant String :=
1802 Base_Name (Last_Switches.Table (File_Index).all);
1803 Last : Natural := Spec'Last;
1806 Get_Name_String (Data.Naming.Ada_Spec_Suffix);
1808 if Spec'Length > Name_Len
1809 and then Spec (Last - Name_Len + 1 .. Last) =
1810 Name_Buffer (1 .. Name_Len)
1812 Last := Last - Name_Len;
1813 Get_Name_String (Data.Naming.Ada_Body_Suffix);
1814 Last_Switches.Increment_Last;
1815 Last_Switches.Table (Last_Switches.Last) :=
1817 Last_Switches.Increment_Last;
1818 Last_Switches.Table (Last_Switches.Last) :=
1819 new String'(Spec (Spec'First .. Last) &
1820 Name_Buffer (1 .. Name_Len));
1826 -- Add the directory of the spec as the destination directory
1827 -- of the body, if there is no destination directory already
1830 if File_Index /= 0 then
1831 for Index in File_Index + 1 .. Last loop
1832 if Last_Switches.Table (Index)
1833 (Last_Switches.Table (Index)'First) /= '-'
1840 if Dir_Index = 0 then
1841 Last_Switches.Increment_Last;
1842 Last_Switches.Table (Last_Switches.Last) :=
1844 (Dir_Name (Last_Switches.Table (File_Index).all));
1850 -- For gnatmetric, the generated files should be put in the
1851 -- object directory. This must be the first switch, because it may
1852 -- be overriden by a switch in package Metrics in the project file
1853 -- or by a command line option.
1855 if The_Command = Metric then
1856 First_Switches.Increment_Last;
1857 First_Switches.Table (2 .. First_Switches.Last) :=
1858 First_Switches.Table (1 .. First_Switches.Last - 1);
1859 First_Switches.Table (1) :=
1862 (Project_Tree.Projects.Table
1863 (Project).Object_Directory));
1866 -- For gnat pretty and gnat metric, if no file has been put on the
1867 -- command line, call the tool with all the sources of the main
1870 if The_Command = Pretty or else
1871 The_Command = Metric or else
1878 -- Gather all the arguments and invoke the executable
1881 The_Args : Argument_List
1882 (1 .. First_Switches.Last +
1883 Last_Switches.Last +
1884 Carg_Switches.Last);
1885 Arg_Num : Natural := 0;
1888 for J in 1 .. First_Switches.Last loop
1889 Arg_Num := Arg_Num + 1;
1890 The_Args (Arg_Num) := First_Switches.Table (J);
1893 for J in 1 .. Last_Switches.Last loop
1894 Arg_Num := Arg_Num + 1;
1895 The_Args (Arg_Num) := Last_Switches.Table (J);
1898 for J in 1 .. Carg_Switches.Last loop
1899 Arg_Num := Arg_Num + 1;
1900 The_Args (Arg_Num) := Carg_Switches.Table (J);
1903 -- If Display_Command is on, only display the generated command
1905 if Display_Command then
1906 Put (Standard_Error, "generated command -->");
1907 Put (Standard_Error, Exec_Path.all);
1909 for Arg in The_Args'Range loop
1910 Put (Standard_Error, " ");
1911 Put (Standard_Error, The_Args (Arg).all);
1914 Put (Standard_Error, "<--");
1915 New_Line (Standard_Error);
1919 if Verbose_Mode then
1920 Output.Write_Str (Exec_Path.all);
1922 for Arg in The_Args'Range loop
1923 Output.Write_Char (' ');
1924 Output.Write_Str (The_Args (Arg).all);
1931 Exit_Status (Spawn (Exec_Path.all, The_Args));
1938 Prj.Env.Delete_All_Path_Files (Project_Tree);
1939 Delete_Temp_Config_Files;
1940 Set_Exit_Status (Failure);
1943 Prj.Env.Delete_All_Path_Files (Project_Tree);
1944 Delete_Temp_Config_Files;
1946 -- Since GNATCmd is normally called from DCL (the VMS shell),
1947 -- it must return an understandable VMS exit status. However
1948 -- the exit status returned *to* GNATCmd is a Posix style code,
1949 -- so we test it and return just a simple success or failure on VMS.
1951 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
1952 Set_Exit_Status (Failure);
1954 Set_Exit_Status (My_Exit_Status);