1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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;
41 with Snames; use Snames;
43 with Types; use Types;
44 with Hostparm; use Hostparm;
45 -- Used to determine if we are in VMS or not for error message purposes
47 with Ada.Characters.Handling; use Ada.Characters.Handling;
48 with Ada.Command_Line; use Ada.Command_Line;
49 with Ada.Text_IO; use Ada.Text_IO;
51 with GNAT.OS_Lib; use GNAT.OS_Lib;
55 with VMS_Conv; use VMS_Conv;
58 Project_File : String_Access;
59 Project : Prj.Project_Id;
60 Current_Verbosity : Prj.Verbosity := Prj.Default;
61 Tool_Package_Name : Name_Id := No_Name;
63 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
64 -- an old fashioned project file. -p cannot be used in conjonction
67 Old_Project_File_Used : Boolean := False;
69 -- A table to keep the switches from the project file
71 package First_Switches is new Table.Table
72 (Table_Component_Type => String_Access,
73 Table_Index_Type => Integer,
76 Table_Increment => 100,
77 Table_Name => "Gnatcmd.First_Switches");
79 package Library_Paths is new Table.Table (
80 Table_Component_Type => String_Access,
81 Table_Index_Type => Integer,
84 Table_Increment => 100,
85 Table_Name => "Make.Library_Path");
87 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
88 -- tool. We allocate objects because we cannot declare aliased objects
89 -- as we are in a procedure, not a library level package.
91 Naming_String : constant String_Access := new String'("naming");
92 Binder_String : constant String_Access := new String'("binder");
93 Eliminate_String : constant String_Access := new String'("eliminate");
94 Finder_String : constant String_Access := new String'("finder");
95 Linker_String : constant String_Access := new String'("linker");
96 Gnatls_String : constant String_Access := new String'("gnatls");
97 Pretty_String : constant String_Access := new String'("pretty_printer");
98 Gnatstub_String : constant String_Access := new String'("gnatstub");
99 Xref_String : constant String_Access := new String'("cross_reference");
101 Packages_To_Check_By_Binder : constant String_List_Access :=
102 new String_List'((Naming_String, Binder_String));
104 Packages_To_Check_By_Eliminate : constant String_List_Access :=
105 new String_List'((Naming_String, Eliminate_String));
107 Packages_To_Check_By_Finder : constant String_List_Access :=
108 new String_List'((Naming_String, Finder_String));
110 Packages_To_Check_By_Linker : constant String_List_Access :=
111 new String_List'((Naming_String, Linker_String));
113 Packages_To_Check_By_Gnatls : constant String_List_Access :=
114 new String_List'((Naming_String, Gnatls_String));
116 Packages_To_Check_By_Pretty : constant String_List_Access :=
117 new String_List'((Naming_String, Pretty_String));
119 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
120 new String_List'((Naming_String, Gnatstub_String));
122 Packages_To_Check_By_Xref : constant String_List_Access :=
123 new String_List'((Naming_String, Xref_String));
125 Packages_To_Check : String_List_Access := Prj.All_Packages;
127 ----------------------------------
128 -- Declarations for GNATCMD use --
129 ----------------------------------
131 The_Command : Command_Type;
133 Command_Arg : Positive := 1;
135 My_Exit_Status : Exit_Status := Success;
137 Current_Work_Dir : constant String := Get_Current_Dir;
139 -----------------------
140 -- Local Subprograms --
141 -----------------------
143 procedure Check_Relative_Executable (Name : in out String_Access);
144 -- Check if an executable is specified as a relative path.
145 -- If it is, and the path contains directory information, fail.
146 -- Otherwise, prepend the exec directory.
147 -- This procedure is only used for GNAT LINK when a project file
150 function Configuration_Pragmas_File return Name_Id;
151 -- Return an argument, if there is a configuration pragmas file to be
152 -- specified for Project, otherwise return No_Name.
153 -- Used for gnatstub (GNAT STUB), gnatpp (GNAT PRETTY) and gnatelim
156 procedure Delete_Temp_Config_Files;
157 -- Delete all temporary config files
159 function Index (Char : Character; Str : String) return Natural;
160 -- Returns the first occurrence of Char in Str.
161 -- Returns 0 if Char is not in Str.
163 procedure Non_VMS_Usage;
164 -- Display usage for platforms other than VMS
166 procedure Set_Library_For
167 (Project : Project_Id;
168 There_Are_Libraries : in out Boolean);
169 -- If Project is a library project, add the correct
170 -- -L and -l switches to the linker invocation.
172 procedure Set_Libraries is
173 new For_Every_Project_Imported (Boolean, Set_Library_For);
174 -- Add the -L and -l switches to the linker for all
175 -- of the library projects.
177 procedure Test_If_Relative_Path
178 (Switch : in out String_Access;
180 -- Test if Switch is a relative search path switch.
181 -- If it is and it includes directory information, prepend the path with
182 -- Parent.This subprogram is only called when using project files.
184 -------------------------------
185 -- Check_Relative_Executable --
186 -------------------------------
188 procedure Check_Relative_Executable (Name : in out String_Access) is
189 Exec_File_Name : constant String := Name.all;
192 if not Is_Absolute_Path (Exec_File_Name) then
193 for Index in Exec_File_Name'Range loop
194 if Exec_File_Name (Index) = Directory_Separator then
195 Fail ("relative executable (""" &
197 """) with directory part not allowed " &
198 "when using project files");
202 Get_Name_String (Projects.Table
203 (Project).Exec_Directory);
205 if Name_Buffer (Name_Len) /= Directory_Separator then
206 Name_Len := Name_Len + 1;
207 Name_Buffer (Name_Len) := Directory_Separator;
210 Name_Buffer (Name_Len + 1 ..
211 Name_Len + Exec_File_Name'Length) :=
213 Name_Len := Name_Len + Exec_File_Name'Length;
214 Name := new String'(Name_Buffer (1 .. Name_Len));
216 end Check_Relative_Executable;
218 --------------------------------
219 -- Configuration_Pragmas_File --
220 --------------------------------
222 function Configuration_Pragmas_File return Name_Id is
224 Prj.Env.Create_Config_Pragmas_File
225 (Project, Project, Include_Config_Files => False);
226 return Projects.Table (Project).Config_File_Name;
227 end Configuration_Pragmas_File;
229 ------------------------------
230 -- Delete_Temp_Config_Files --
231 ------------------------------
233 procedure Delete_Temp_Config_Files is
237 if Project /= No_Project then
238 for Prj in 1 .. Projects.Last loop
239 if Projects.Table (Prj).Config_File_Temp then
240 if Opt.Verbose_Mode then
241 Output.Write_Str ("Deleting temp configuration file """);
242 Output.Write_Str (Get_Name_String
243 (Projects.Table (Prj).Config_File_Name));
244 Output.Write_Line ("""");
248 (Name => Get_Name_String
249 (Projects.Table (Prj).Config_File_Name),
254 end Delete_Temp_Config_Files;
260 function Index (Char : Character; Str : String) return Natural is
262 for Index in Str'Range loop
263 if Str (Index) = Char then
271 ---------------------
272 -- Set_Library_For --
273 ---------------------
275 procedure Set_Library_For
276 (Project : Project_Id;
277 There_Are_Libraries : in out Boolean)
279 Path_Option : constant String_Access :=
280 MLib.Tgt.Linker_Library_Path_Option;
283 -- Case of library project
285 if Projects.Table (Project).Library then
286 There_Are_Libraries := True;
290 Last_Switches.Increment_Last;
291 Last_Switches.Table (Last_Switches.Last) :=
294 (Projects.Table (Project).Library_Dir));
298 Last_Switches.Increment_Last;
299 Last_Switches.Table (Last_Switches.Last) :=
302 (Projects.Table (Project).Library_Name));
304 -- Add the directory to table Library_Paths, to be processed later
305 -- if library is not static and if Path_Option is not null.
307 if Projects.Table (Project).Library_Kind /= Static
308 and then Path_Option /= null
310 Library_Paths.Increment_Last;
311 Library_Paths.Table (Library_Paths.Last) :=
312 new String'(Get_Name_String
313 (Projects.Table (Project).Library_Dir));
319 ---------------------------
320 -- Test_If_Relative_Path --
321 ---------------------------
323 procedure Test_If_Relative_Path
324 (Switch : in out String_Access;
328 if Switch /= null then
331 Sw : String (1 .. Switch'Length);
332 Start : Positive := 1;
339 and then (Sw (2) = 'A'
341 or else Sw (2) = 'L')
350 and then (Sw (2 .. 3) = "aL"
351 or else Sw (2 .. 3) = "aO"
352 or else Sw (2 .. 3) = "aI")
357 and then Sw (2 .. 6) = "-RTS="
365 -- If the path is relative, test if it includes directory
366 -- information. If it does, prepend Parent to the path.
368 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
369 for J in Start .. Sw'Last loop
370 if Sw (J) = Directory_Separator then
373 (Sw (1 .. Start - 1) &
375 Directory_Separator &
376 Sw (Start .. Sw'Last));
383 end Test_If_Relative_Path;
389 procedure Non_VMS_Usage is
393 Put_Line ("List of available commands");
396 for C in Command_List'Range loop
397 if not Command_List (C).VMS_Only then
398 Put ("GNAT " & Command_List (C).Cname.all);
400 Put (Command_List (C).Unixcmd.all);
403 Sws : Argument_List_Access renames Command_List (C).Unixsws;
406 for J in Sws'Range loop
418 Put_Line ("Commands FIND, LIST, PRETTY, STUB and XREF accept " &
419 "project file switches -vPx, -Pprj and -Xnam=val");
423 -------------------------------------
424 -- Start of processing for GNATCmd --
425 -------------------------------------
438 Last_Switches.Set_Last (0);
441 First_Switches.Set_Last (0);
445 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
446 -- filenames and pathnames to Unix style.
449 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
451 VMS_Conversion (The_Command);
453 -- If not on VMS, scan the command line directly
456 if Argument_Count = 0 then
461 if Argument_Count > 1 and then Argument (1) = "-v" then
462 Opt.Verbose_Mode := True;
466 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
468 if Command_List (The_Command).VMS_Only then
472 Command_List (The_Command).Cname.all,
473 """ can only be used on VMS");
477 when Constraint_Error =>
479 -- Check if it is an alternate command
482 Alternate : Alternate_Command;
485 Alternate := Alternate_Command'Value
486 (Argument (Command_Arg));
487 The_Command := Corresponding_To (Alternate);
490 when Constraint_Error =>
492 Fail ("Unknown command: ", Argument (Command_Arg));
496 -- Get the arguments from the command line and from the eventual
497 -- argument file(s) specified on the command line.
499 for Arg in Command_Arg + 1 .. Argument_Count loop
501 The_Arg : constant String := Argument (Arg);
504 -- Check if an argument file is specified
506 if The_Arg (The_Arg'First) = '@' then
508 Arg_File : Ada.Text_IO.File_Type;
509 Line : String (1 .. 256);
513 -- Open the file and fail if the file cannot be found
518 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
523 (Standard_Error, "Cannot open argument file """);
526 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
528 Put_Line (Standard_Error, """");
532 -- Read line by line and put the content of each
533 -- non empty line in the Last_Switches table.
535 while not End_Of_File (Arg_File) loop
536 Get_Line (Arg_File, Line, Last);
539 Last_Switches.Increment_Last;
540 Last_Switches.Table (Last_Switches.Last) :=
541 new String'(Line (1 .. Last));
549 -- It is not an argument file; just put the argument in
550 -- the Last_Switches table.
552 Last_Switches.Increment_Last;
553 Last_Switches.Table (Last_Switches.Last) :=
554 new String'(The_Arg);
562 Program : constant String :=
563 Program_Name (Command_List (The_Command).Unixcmd.all).all;
565 Exec_Path : String_Access;
568 -- Locate the executable for the command
570 Exec_Path := Locate_Exec_On_Path (Program);
572 if Exec_Path = null then
573 Put_Line (Standard_Error, "Couldn't locate " & Program);
577 -- If there are switches for the executable, put them as first switches
579 if Command_List (The_Command).Unixsws /= null then
580 for J in Command_List (The_Command).Unixsws'Range loop
581 First_Switches.Increment_Last;
582 First_Switches.Table (First_Switches.Last) :=
583 Command_List (The_Command).Unixsws (J);
587 -- For BIND, FIND, LINK, LIST, PRETTY ad XREF, look for project file
590 if The_Command = Bind
591 or else The_Command = Elim
592 or else The_Command = Find
593 or else The_Command = Link
594 or else The_Command = List
595 or else The_Command = Xref
596 or else The_Command = Pretty
597 or else The_Command = Stub
601 Tool_Package_Name := Name_Binder;
602 Packages_To_Check := Packages_To_Check_By_Binder;
604 Tool_Package_Name := Name_Eliminate;
605 Packages_To_Check := Packages_To_Check_By_Eliminate;
607 Tool_Package_Name := Name_Finder;
608 Packages_To_Check := Packages_To_Check_By_Finder;
610 Tool_Package_Name := Name_Linker;
611 Packages_To_Check := Packages_To_Check_By_Linker;
613 Tool_Package_Name := Name_Gnatls;
614 Packages_To_Check := Packages_To_Check_By_Gnatls;
616 Tool_Package_Name := Name_Pretty_Printer;
617 Packages_To_Check := Packages_To_Check_By_Pretty;
619 Tool_Package_Name := Name_Gnatstub;
620 Packages_To_Check := Packages_To_Check_By_Gnatstub;
622 Tool_Package_Name := Name_Cross_Reference;
623 Packages_To_Check := Packages_To_Check_By_Xref;
628 -- Check that the switches are consistent.
629 -- Detect project file related switches.
633 Arg_Num : Positive := 1;
634 Argv : String_Access;
636 procedure Remove_Switch (Num : Positive);
637 -- Remove a project related switch from table Last_Switches
643 procedure Remove_Switch (Num : Positive) is
645 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
646 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
647 Last_Switches.Decrement_Last;
650 -- Start of processing for Inspect_Switches
653 while Arg_Num <= Last_Switches.Last loop
654 Argv := Last_Switches.Table (Arg_Num);
656 if Argv (Argv'First) = '-' then
657 if Argv'Length = 1 then
659 ("switch character cannot be followed by a blank");
662 -- The two style project files (-p and -P) cannot be used
665 if (The_Command = Find or else The_Command = Xref)
666 and then Argv (2) = 'p'
668 Old_Project_File_Used := True;
669 if Project_File /= null then
670 Fail ("-P and -p cannot be used together");
674 -- -vPx Specify verbosity while parsing project files
677 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
679 case Argv (Argv'Last) is
681 Current_Verbosity := Prj.Default;
683 Current_Verbosity := Prj.Medium;
685 Current_Verbosity := Prj.High;
687 Fail ("Invalid switch: ", Argv.all);
690 Remove_Switch (Arg_Num);
692 -- -Pproject_file Specify project file to be used
694 elsif Argv (Argv'First + 1) = 'P' then
696 -- Only one -P switch can be used
698 if Project_File /= null then
701 ": second project file forbidden (first is """,
702 Project_File.all & """)");
704 -- The two style project files (-p and -P) cannot be
707 elsif Old_Project_File_Used then
708 Fail ("-p and -P cannot be used together");
710 elsif Argv'Length = 2 then
712 -- There is space between -P and the project file
713 -- name. -P cannot be the last option.
715 if Arg_Num = Last_Switches.Last then
716 Fail ("project file name missing after -P");
719 Remove_Switch (Arg_Num);
720 Argv := Last_Switches.Table (Arg_Num);
722 -- After -P, there must be a project file name,
723 -- not another switch.
725 if Argv (Argv'First) = '-' then
726 Fail ("project file name missing after -P");
729 Project_File := new String'(Argv.all);
734 -- No space between -P and project file name
737 new String'(Argv (Argv'First + 2 .. Argv'Last));
740 Remove_Switch (Arg_Num);
742 -- -Xexternal=value Specify an external reference to be
743 -- used in project files
745 elsif Argv'Length >= 5
746 and then Argv (Argv'First + 1) = 'X'
749 Equal_Pos : constant Natural :=
750 Index ('=', Argv (Argv'First + 2 .. Argv'Last));
752 if Equal_Pos >= Argv'First + 3 and then
753 Equal_Pos /= Argv'Last then
754 Add (External_Name =>
755 Argv (Argv'First + 2 .. Equal_Pos - 1),
756 Value => Argv (Equal_Pos + 1 .. Argv'Last));
760 " is not a valid external assignment.");
764 Remove_Switch (Arg_Num);
767 Arg_Num := Arg_Num + 1;
771 Arg_Num := Arg_Num + 1;
774 end Inspect_Switches;
777 -- If there is a project file specified, parse it, get the switches
778 -- for the tool and setup PATH environment variables.
780 if Project_File /= null then
781 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
785 Project_File_Name => Project_File.all,
786 Packages_To_Check => Packages_To_Check);
788 if Project = Prj.No_Project then
789 Fail ("""", Project_File.all, """ processing failed");
792 -- Check if a package with the name of the tool is in the project
793 -- file and if there is one, get the switches, if any, and scan them.
796 Data : constant Prj.Project_Data :=
797 Prj.Projects.Table (Project);
799 Pkg : constant Prj.Package_Id :=
801 (Name => Tool_Package_Name,
802 In_Packages => Data.Decl.Packages);
804 Element : Package_Element;
806 Default_Switches_Array : Array_Element_Id;
808 The_Switches : Prj.Variable_Value;
809 Current : Prj.String_List_Id;
810 The_String : String_Element;
813 if Pkg /= No_Package then
814 Element := Packages.Table (Pkg);
816 -- Packages Gnatls has a single attribute Switches, that is
817 -- not an associative array.
819 if The_Command = List then
822 (Variable_Name => Snames.Name_Switches,
823 In_Variables => Element.Decl.Attributes);
825 -- Packages Binder (for gnatbind), Cross_Reference (for
826 -- gnatxref), Linker (for gnatlink) Finder (for gnatfind),
827 -- Pretty_Printer (for gnatpp) and Eliminate (for gnatelim)
828 -- have an attributed Switches, an associative array, indexed
829 -- by the name of the file.
831 -- They also have an attribute Default_Switches, indexed
832 -- by the name of the programming language.
835 if The_Switches.Kind = Prj.Undefined then
836 Default_Switches_Array :=
838 (Name => Name_Default_Switches,
839 In_Arrays => Packages.Table (Pkg).Decl.Arrays);
840 The_Switches := Prj.Util.Value_Of
842 In_Array => Default_Switches_Array);
846 -- If there are switches specified in the package of the
847 -- project file corresponding to the tool, scan them.
849 case The_Switches.Kind is
850 when Prj.Undefined =>
855 Switch : constant String :=
856 Get_Name_String (The_Switches.Value);
859 if Switch'Length > 0 then
860 First_Switches.Increment_Last;
861 First_Switches.Table (First_Switches.Last) :=
867 Current := The_Switches.Values;
868 while Current /= Prj.Nil_String loop
869 The_String := String_Elements.Table (Current);
872 Switch : constant String :=
873 Get_Name_String (The_String.Value);
876 if Switch'Length > 0 then
877 First_Switches.Increment_Last;
878 First_Switches.Table (First_Switches.Last) :=
883 Current := The_String.Next;
889 if The_Command = Bind
890 or else The_Command = Link
891 or else The_Command = Elim
895 (Projects.Table (Project).Object_Directory));
898 -- Set up the env vars for project path files
900 Prj.Env.Set_Ada_Paths (Project, Including_Libraries => False);
902 -- For gnatstub, gnatpp and gnatelim, create a configuration pragmas
903 -- file, if necessary.
905 if The_Command = Pretty
906 or else The_Command = Stub
907 or else The_Command = Elim
910 CP_File : constant Name_Id := Configuration_Pragmas_File;
913 if CP_File /= No_Name then
914 First_Switches.Increment_Last;
916 if The_Command = Elim then
917 First_Switches.Table (First_Switches.Last) :=
918 new String'("-C" & Get_Name_String (CP_File));
921 First_Switches.Table (First_Switches.Last) :=
922 new String'("-gnatec=" & Get_Name_String (CP_File));
928 if The_Command = Link then
930 -- Add the default search directories, to be able to find
931 -- libgnat in call to MLib.Utl.Lib_Directory.
933 Add_Default_Search_Dirs;
936 There_Are_Libraries : Boolean := False;
937 Path_Option : constant String_Access :=
938 MLib.Tgt.Linker_Library_Path_Option;
941 Library_Paths.Set_Last (0);
943 -- Check if there are library project files
945 if MLib.Tgt.Support_For_Libraries /= MLib.Tgt.None then
946 Set_Libraries (Project, There_Are_Libraries);
949 -- If there are, add the necessary additional switches
951 if There_Are_Libraries then
953 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
955 Last_Switches.Increment_Last;
956 Last_Switches.Table (Last_Switches.Last) :=
957 new String'("-L" & MLib.Utl.Lib_Directory);
958 Last_Switches.Increment_Last;
959 Last_Switches.Table (Last_Switches.Last) :=
960 new String'("-lgnarl");
961 Last_Switches.Increment_Last;
962 Last_Switches.Table (Last_Switches.Last) :=
963 new String'("-lgnat");
965 -- If Path_Option is not null, create the switch
966 -- ("-Wl,-rpath," or equivalent) with all the library dirs
967 -- plus the standard GNAT library dir.
969 if Path_Option /= null then
971 Option : String_Access;
972 Length : Natural := Path_Option'Length;
976 -- First, compute the exact length for the switch
979 Library_Paths.First .. Library_Paths.Last
981 -- Add the length of the library dir plus one
982 -- for the directory separator.
986 Library_Paths.Table (Index)'Length + 1;
989 -- Finally, add the length of the standard GNAT
992 Length := Length + MLib.Utl.Lib_Directory'Length;
993 Option := new String (1 .. Length);
994 Option (1 .. Path_Option'Length) := Path_Option.all;
995 Current := Path_Option'Length;
997 -- Put each library dir followed by a dir separator
1000 Library_Paths.First .. Library_Paths.Last
1005 Library_Paths.Table (Index)'Length) :=
1006 Library_Paths.Table (Index).all;
1009 Library_Paths.Table (Index)'Length + 1;
1010 Option (Current) := Path_Separator;
1013 -- Finally put the standard GNAT library dir
1017 Current + MLib.Utl.Lib_Directory'Length) :=
1018 MLib.Utl.Lib_Directory;
1020 -- And add the switch to the last switches
1022 Last_Switches.Increment_Last;
1023 Last_Switches.Table (Last_Switches.Last) :=
1030 -- Check if the first ALI file specified can be found, either
1031 -- in the object directory of the main project or in an object
1032 -- directory of a project file extended by the main project.
1033 -- If the ALI file can be found, replace its name with its
1037 Skip_Executable : Boolean := False;
1040 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1042 -- If we have an executable just reset the flag
1044 if Skip_Executable then
1045 Skip_Executable := False;
1047 -- If -o, set flag so that next switch is not processed
1049 elsif Last_Switches.Table (J).all = "-o" then
1050 Skip_Executable := True;
1056 Switch : constant String :=
1057 Last_Switches.Table (J).all;
1059 ALI_File : constant String (1 .. Switch'Length + 4) :=
1062 Last : Natural := Switch'Length;
1063 Test_Existence : Boolean := False;
1066 -- Skip real switches
1068 if Switch'Length /= 0 and then
1069 Switch (Switch'First) /= '-'
1071 -- Append ".ali" if file name does not end with it
1073 if Switch'Length <= 4 or else
1074 Switch (Switch'Last - 3 .. Switch'Last) /= ".ali"
1076 Last := ALI_File'Last;
1079 -- If file name includes directory information,
1080 -- stop if ALI file exists.
1082 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1083 Test_Existence := True;
1086 for K in Switch'Range loop
1087 if Switch (K) = '/' or else
1088 Switch (K) = Directory_Separator
1090 Test_Existence := True;
1096 if Test_Existence then
1097 if Is_Regular_File (ALI_File (1 .. Last)) then
1102 -- Look in the object directories if the ALI
1106 Prj : Project_Id := Project;
1111 Dir : constant String :=
1113 (Projects.Table (Prj).
1117 (Dir & Directory_Separator &
1118 ALI_File (1 .. Last))
1120 -- We have found the correct
1121 -- project, so we replace the file
1122 -- with the absolute path.
1124 Last_Switches.Table (J) :=
1126 (Dir & Directory_Separator &
1127 ALI_File (1 .. Last));
1135 -- Go to the project being extended,
1138 Prj := Projects.Table (Prj).Extends;
1139 exit Project_Loop when Prj = No_Project;
1140 end loop Project_Loop;
1146 end loop Switch_Loop;
1149 -- If a relative path output file has been specified, we add
1150 -- the exec directory.
1153 Look_For_Executable : Boolean := True;
1157 for J in reverse 1 .. Last_Switches.Last - 1 loop
1158 if Last_Switches.Table (J).all = "-o" then
1159 Check_Relative_Executable
1160 (Name => Last_Switches.Table (J + 1));
1161 Look_For_Executable := False;
1166 if Look_For_Executable then
1167 for J in reverse 1 .. First_Switches.Last - 1 loop
1168 if First_Switches.Table (J).all = "-o" then
1169 Look_For_Executable := False;
1170 Check_Relative_Executable
1171 (Name => First_Switches.Table (J + 1));
1177 -- If no executable is specified, then find the name
1178 -- of the first ALI file on the command line and issue
1179 -- a -o switch with the absolute path of the executable
1180 -- in the exec directory.
1182 if Look_For_Executable then
1183 for J in 1 .. Last_Switches.Last loop
1185 Arg : constant String_Access :=
1186 Last_Switches.Table (J);
1187 Last : Natural := 0;
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
1202 Executable_Name : constant String :=
1203 Base_Name (Arg (Arg'First .. Last));
1205 Last_Switches.Increment_Last;
1206 Last_Switches.Table (Last_Switches.Last) :=
1209 (Projects.Table (Project).Exec_Directory);
1210 Last_Switches.Increment_Last;
1211 Last_Switches.Table (Last_Switches.Last) :=
1212 new String'(Name_Buffer (1 .. Name_Len) &
1213 Directory_Separator &
1215 Get_Executable_Suffix.all);
1226 if The_Command = Link or The_Command = Bind then
1228 -- For files that are specified as relative paths with directory
1229 -- information, we convert them to absolute paths, with parent
1230 -- being the current working directory if specified on the command
1231 -- line and the project directory if specified in the project
1232 -- file. This is what gnatmake is doing for linker and binder
1235 for J in 1 .. Last_Switches.Last loop
1236 Test_If_Relative_Path
1237 (Last_Switches.Table (J), Current_Work_Dir);
1240 Get_Name_String (Projects.Table (Project).Directory);
1243 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
1246 for J in 1 .. First_Switches.Last loop
1247 Test_If_Relative_Path
1248 (First_Switches.Table (J), Project_Dir);
1252 elsif The_Command = Stub then
1254 Data : constant Prj.Project_Data :=
1255 Prj.Projects.Table (Project);
1256 File_Index : Integer := 0;
1257 Dir_Index : Integer := 0;
1258 Last : constant Integer := Last_Switches.Last;
1261 for Index in 1 .. Last loop
1262 if Last_Switches.Table (Index)
1263 (Last_Switches.Table (Index)'First) /= '-'
1265 File_Index := Index;
1270 -- If the naming scheme of the project file is not standard,
1271 -- and if the file name ends with the spec suffix, then
1272 -- indicate to gnatstub the name of the body file with
1275 if Data.Naming.Current_Spec_Suffix /=
1276 Prj.Default_Ada_Spec_Suffix
1278 if File_Index /= 0 then
1280 Spec : constant String :=
1281 Base_Name (Last_Switches.Table (File_Index).all);
1282 Last : Natural := Spec'Last;
1285 Get_Name_String (Data.Naming.Current_Spec_Suffix);
1287 if Spec'Length > Name_Len
1288 and then Spec (Last - Name_Len + 1 .. Last) =
1289 Name_Buffer (1 .. Name_Len)
1291 Last := Last - Name_Len;
1292 Get_Name_String (Data.Naming.Current_Body_Suffix);
1293 Last_Switches.Increment_Last;
1294 Last_Switches.Table (Last_Switches.Last) :=
1296 Last_Switches.Increment_Last;
1297 Last_Switches.Table (Last_Switches.Last) :=
1298 new String'(Spec (Spec'First .. Last) &
1299 Name_Buffer (1 .. Name_Len));
1305 -- Add the directory of the spec as the destination directory
1306 -- of the body, if there is no destination directory already
1309 if File_Index /= 0 then
1310 for Index in File_Index + 1 .. Last loop
1311 if Last_Switches.Table (Index)
1312 (Last_Switches.Table (Index)'First) /= '-'
1319 if Dir_Index = 0 then
1320 Last_Switches.Increment_Last;
1321 Last_Switches.Table (Last_Switches.Last) :=
1323 (Dir_Name (Last_Switches.Table (File_Index).all));
1330 -- Gather all the arguments and invoke the executable
1333 The_Args : Argument_List
1334 (1 .. First_Switches.Last + Last_Switches.Last);
1335 Arg_Num : Natural := 0;
1337 for J in 1 .. First_Switches.Last loop
1338 Arg_Num := Arg_Num + 1;
1339 The_Args (Arg_Num) := First_Switches.Table (J);
1342 for J in 1 .. Last_Switches.Last loop
1343 Arg_Num := Arg_Num + 1;
1344 The_Args (Arg_Num) := Last_Switches.Table (J);
1347 -- If Display_Command is on, only display the generated command
1349 if Display_Command then
1350 Put (Standard_Error, "generated command -->");
1351 Put (Standard_Error, Exec_Path.all);
1353 for Arg in The_Args'Range loop
1354 Put (Standard_Error, " ");
1355 Put (Standard_Error, The_Args (Arg).all);
1358 Put (Standard_Error, "<--");
1359 New_Line (Standard_Error);
1363 if Opt.Verbose_Mode then
1364 Output.Write_Str (Exec_Path.all);
1366 for Arg in The_Args'Range loop
1367 Output.Write_Char (' ');
1368 Output.Write_Str (The_Args (Arg).all);
1375 Exit_Status (Spawn (Exec_Path.all, The_Args));
1382 Prj.Env.Delete_All_Path_Files;
1383 Delete_Temp_Config_Files;
1384 Set_Exit_Status (Failure);
1387 Prj.Env.Delete_All_Path_Files;
1388 Delete_Temp_Config_Files;
1390 -- Since GNATCmd is normally called from DCL (the VMS shell),
1391 -- it must return an understandable VMS exit status. However
1392 -- the exit status returned *to* GNATCmd is a Posix style code,
1393 -- so we test it and return just a simple success or failure on VMS.
1395 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
1396 Set_Exit_Status (Failure);
1398 Set_Exit_Status (My_Exit_Status);