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;
29 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;
46 with Types; use Types;
47 with Hostparm; use Hostparm;
48 -- Used to determine if we are in VMS or not for error message purposes
50 with Ada.Characters.Handling; use Ada.Characters.Handling;
51 with Ada.Command_Line; use Ada.Command_Line;
52 with Ada.Text_IO; use Ada.Text_IO;
54 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 B_Start : String_Ptr := new String'("b~");
66 -- Prefix of binder generated file, changed to b__ for VMS
68 Old_Project_File_Used : Boolean := False;
69 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
70 -- an old fashioned project file. -p cannot be used in conjunction
73 Max_Files_On_The_Command_Line : constant := 30; -- Arbitrary
75 Temp_File_Name : String_Access := null;
76 -- The name of the temporary text file to put a list of source/object
77 -- files to pass to a tool, when there are more than
78 -- Max_Files_On_The_Command_Line files.
80 ASIS_Main : String_Access := null;
81 -- Main for commands Check, Metric and Pretty, when -U is used
83 package First_Switches is new Table.Table
84 (Table_Component_Type => String_Access,
85 Table_Index_Type => Integer,
88 Table_Increment => 100,
89 Table_Name => "Gnatcmd.First_Switches");
90 -- A table to keep the switches from the project file
92 package Carg_Switches is new Table.Table
93 (Table_Component_Type => String_Access,
94 Table_Index_Type => Integer,
97 Table_Increment => 100,
98 Table_Name => "Gnatcmd.Carg_Switches");
99 -- A table to keep the switches following -cargs for ASIS tools
101 package Rules_Switches is new Table.Table
102 (Table_Component_Type => String_Access,
103 Table_Index_Type => Integer,
104 Table_Low_Bound => 1,
106 Table_Increment => 100,
107 Table_Name => "Gnatcmd.Rules_Switches");
108 -- A table to keep the switches following -rules for gnatcheck
110 package Library_Paths is new Table.Table (
111 Table_Component_Type => String_Access,
112 Table_Index_Type => Integer,
113 Table_Low_Bound => 1,
115 Table_Increment => 100,
116 Table_Name => "Make.Library_Path");
118 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
119 -- tool. We allocate objects because we cannot declare aliased objects
120 -- as we are in a procedure, not a library level package.
122 subtype SA is String_Access;
124 Naming_String : constant SA := new String'("naming");
125 Binder_String : constant SA := new String'("binder");
126 Compiler_String : constant SA := new String'("compiler");
127 Check_String : constant SA := new String'("check");
128 Synchronize_String : constant SA := new String'("synchronize");
129 Eliminate_String : constant SA := new String'("eliminate");
130 Finder_String : constant SA := new String'("finder");
131 Linker_String : constant SA := new String'("linker");
132 Gnatls_String : constant SA := new String'("gnatls");
133 Pretty_String : constant SA := new String'("pretty_printer");
134 Stack_String : constant SA := new String'("stack");
135 Gnatstub_String : constant SA := new String'("gnatstub");
136 Metric_String : constant SA := new String'("metrics");
137 Xref_String : constant SA := new String'("cross_reference");
139 Packages_To_Check_By_Binder : constant String_List_Access :=
140 new String_List'((Naming_String, Binder_String));
142 Packages_To_Check_By_Check : constant String_List_Access :=
143 new String_List'((Naming_String, Check_String, Compiler_String));
145 Packages_To_Check_By_Sync : constant String_List_Access :=
146 new String_List'((Naming_String, Synchronize_String, Compiler_String));
148 Packages_To_Check_By_Eliminate : constant String_List_Access :=
149 new String_List'((Naming_String, Eliminate_String, Compiler_String));
151 Packages_To_Check_By_Finder : constant String_List_Access :=
152 new String_List'((Naming_String, Finder_String));
154 Packages_To_Check_By_Linker : constant String_List_Access :=
155 new String_List'((Naming_String, Linker_String));
157 Packages_To_Check_By_Gnatls : constant String_List_Access :=
158 new String_List'((Naming_String, Gnatls_String));
160 Packages_To_Check_By_Pretty : constant String_List_Access :=
161 new String_List'((Naming_String, Pretty_String, Compiler_String));
163 Packages_To_Check_By_Stack : constant String_List_Access :=
164 new String_List'((Naming_String, Stack_String));
166 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
167 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
169 Packages_To_Check_By_Metric : constant String_List_Access :=
170 new String_List'((Naming_String, Metric_String, Compiler_String));
172 Packages_To_Check_By_Xref : constant String_List_Access :=
173 new String_List'((Naming_String, Xref_String));
175 Packages_To_Check : String_List_Access := Prj.All_Packages;
177 ----------------------------------
178 -- Declarations for GNATCMD use --
179 ----------------------------------
181 The_Command : Command_Type;
182 -- The command specified in the invocation of the GNAT driver
184 Command_Arg : Positive := 1;
185 -- The index of the command in the arguments of the GNAT driver
187 My_Exit_Status : Exit_Status := Success;
188 -- The exit status of the spawned tool. Used to set the correct VMS
191 Current_Work_Dir : constant String := Get_Current_Dir;
192 -- The path of the working directory
194 All_Projects : Boolean := False;
195 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
196 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
197 -- should be invoked for all sources of all projects.
199 -----------------------
200 -- Local Subprograms --
201 -----------------------
203 procedure Add_To_Carg_Switches (Switch : String_Access);
204 -- Add a switch to the Carg_Switches table. If it is the first one, put the
205 -- switch "-cargs" at the beginning of the table.
207 procedure Add_To_Rules_Switches (Switch : String_Access);
208 -- Add a switch to the Rules_Switches table. If it is the first one, put
209 -- the switch "-crules" at the beginning of the table.
211 procedure Check_Files;
212 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
213 -- project file is specified, without any file arguments. If it is the
214 -- case, invoke the GNAT tool with the proper list of files, derived from
215 -- the sources of the project.
217 function Check_Project
218 (Project : Project_Id;
219 Root_Project : Project_Id) return Boolean;
220 -- Returns True if Project = Root_Project or if we want to consider all
221 -- sources of all projects. For GNAT METRIC, also returns True if Project
222 -- is extended by Root_Project.
224 procedure Check_Relative_Executable (Name : in out String_Access);
225 -- Check if an executable is specified as a relative path. If it is, and
226 -- the path contains directory information, fail. Otherwise, prepend the
227 -- exec directory. This procedure is only used for GNAT LINK when a project
228 -- file is specified.
230 function Configuration_Pragmas_File return Path_Name_Type;
231 -- Return an argument, if there is a configuration pragmas file to be
232 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
233 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
236 procedure Delete_Temp_Config_Files;
237 -- Delete all temporary config files. The caller is responsible for
238 -- ensuring that Keep_Temporary_Files is False.
240 procedure Get_Closure;
241 -- Get the sources in the closure of the ASIS_Main and add them to the
242 -- list of arguments.
244 function Index (Char : Character; Str : String) return Natural;
245 -- Returns first occurrence of Char in Str, returns 0 if Char not in Str
247 procedure Non_VMS_Usage;
248 -- Display usage for platforms other than VMS
250 procedure Process_Link;
251 -- Process GNAT LINK, when there is a project file specified
253 procedure Set_Library_For
254 (Project : Project_Id;
255 There_Are_Libraries : in out Boolean);
256 -- If Project is a library project, add the correct -L and -l switches to
257 -- the linker invocation.
259 procedure Set_Libraries is
260 new For_Every_Project_Imported (Boolean, Set_Library_For);
261 -- Add the -L and -l switches to the linker for all of the library
264 procedure Test_If_Relative_Path
265 (Switch : in out String_Access;
267 -- Test if Switch is a relative search path switch. If it is and it
268 -- includes directory information, prepend the path with Parent. This
269 -- subprogram is only called when using project files.
271 --------------------------
272 -- Add_To_Carg_Switches --
273 --------------------------
275 procedure Add_To_Carg_Switches (Switch : String_Access) is
277 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
279 if Carg_Switches.Last = 0 then
280 Carg_Switches.Increment_Last;
281 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
284 Carg_Switches.Increment_Last;
285 Carg_Switches.Table (Carg_Switches.Last) := Switch;
286 end Add_To_Carg_Switches;
288 ---------------------------
289 -- Add_To_Rules_Switches --
290 ---------------------------
292 procedure Add_To_Rules_Switches (Switch : String_Access) is
294 -- If the Rules_Switches table is empty, put "-rules" at the beginning
296 if Rules_Switches.Last = 0 then
297 Rules_Switches.Increment_Last;
298 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
301 Rules_Switches.Increment_Last;
302 Rules_Switches.Table (Rules_Switches.Last) := Switch;
303 end Add_To_Rules_Switches;
309 procedure Check_Files is
310 Add_Sources : Boolean := True;
311 Unit_Data : Prj.Unit_Data;
312 Subunit : Boolean := False;
315 -- Check if there is at least one argument that is not a switch
317 for Index in 1 .. Last_Switches.Last loop
318 if Last_Switches.Table (Index) (1) /= '-' then
319 Add_Sources := False;
324 -- If all arguments were switches, add the path names of all the sources
325 -- of the main project.
329 Current_Last : constant Integer := Last_Switches.Last;
331 -- Gnatstack needs to add the .ci file for the binder
332 -- generated files corresponding to all of the library projects
333 -- and main units belonging to the application.
335 if The_Command = Stack then
336 for Proj in Project_Table.First ..
337 Project_Table.Last (Project_Tree.Projects)
339 if Check_Project (Proj, Project) then
341 Data : Project_Data renames
342 Project_Tree.Projects.Table (Proj);
343 Main : String_List_Id := Data.Mains;
344 File : String_Access;
347 -- Include binder generated files for main programs
349 while Main /= Nil_String loop
352 (Get_Name_String (Data.Object_Directory.Name) &
353 Directory_Separator &
357 (Project_Tree.String_Elements.Table
361 if Is_Regular_File (File.all) then
362 Last_Switches.Increment_Last;
363 Last_Switches.Table (Last_Switches.Last) := File;
367 Project_Tree.String_Elements.Table (Main).Next;
372 -- Include the .ci file for the binder generated
373 -- files that contains the initialization and
374 -- finalization of the library.
378 (Get_Name_String (Data.Object_Directory.Name) &
379 Directory_Separator &
381 Get_Name_String (Data.Library_Name) &
384 if Is_Regular_File (File.all) then
385 Last_Switches.Increment_Last;
386 Last_Switches.Table (Last_Switches.Last) := File;
394 for Unit in Unit_Table.First ..
395 Unit_Table.Last (Project_Tree.Units)
397 Unit_Data := Project_Tree.Units.Table (Unit);
399 -- For gnatls, we only need to put the library units, body or
400 -- spec, but not the subunits.
402 if The_Command = List then
404 Unit_Data.File_Names (Body_Part).Name /= No_File
406 Unit_Data.File_Names (Body_Part).Path.Name /= Slash
408 -- There is a body, check if it is for this project
410 if All_Projects or else
411 Unit_Data.File_Names (Body_Part).Project = Project
416 Unit_Data.File_Names (Specification).Name = No_File
419 (Specification).Path.Name = Slash
421 -- We have a body with no spec: we need to check if
422 -- this is a subunit, because gnatls will complain
426 Src_Ind : Source_File_Index;
429 Src_Ind := Sinput.P.Load_Project_File
431 (Unit_Data.File_Names
432 (Body_Part).Path.Name));
435 Sinput.P.Source_File_Is_Subunit
441 Last_Switches.Increment_Last;
442 Last_Switches.Table (Last_Switches.Last) :=
445 (Unit_Data.File_Names
446 (Body_Part).Display_Name));
451 Unit_Data.File_Names (Specification).Name /= No_File
453 Unit_Data.File_Names (Specification).Path.Name /= Slash
455 -- We have a spec with no body; check if it is for this
458 if All_Projects or else
459 Unit_Data.File_Names (Specification).Project = Project
461 Last_Switches.Increment_Last;
462 Last_Switches.Table (Last_Switches.Last) :=
465 (Unit_Data.File_Names
466 (Specification).Display_Name));
470 -- For gnatstack, we put the .ci files corresponding to the
471 -- different units, including the binder generated files. We
472 -- only need to do that for the library units, body or spec,
473 -- but not the subunits.
475 elsif The_Command = Stack then
477 Unit_Data.File_Names (Body_Part).Name /= No_File
479 Unit_Data.File_Names (Body_Part).Path.Name /= Slash
481 -- There is a body. Check if .ci files for this project
486 (Unit_Data.File_Names (Body_Part).Project, Project)
491 Unit_Data.File_Names (Specification).Name = No_File
494 (Specification).Path.Name = Slash
496 -- We have a body with no spec: we need to check
497 -- if this is a subunit, because .ci files are not
498 -- generated for subunits.
501 Src_Ind : Source_File_Index;
504 Src_Ind := Sinput.P.Load_Project_File
506 (Unit_Data.File_Names
507 (Body_Part).Path.Name));
510 Sinput.P.Source_File_Is_Subunit (Src_Ind);
515 Last_Switches.Increment_Last;
516 Last_Switches.Table (Last_Switches.Last) :=
519 (Project_Tree.Projects.Table
520 (Unit_Data.File_Names
521 (Body_Part).Project).
522 Object_Directory.Name) &
523 Directory_Separator &
526 (Unit_Data.File_Names
527 (Body_Part).Display_Name),
533 Unit_Data.File_Names (Specification).Name /= No_File
535 Unit_Data.File_Names (Specification).Path.Name /= Slash
537 -- We have a spec with no body. Check if it is for this
542 (Unit_Data.File_Names (Specification).Project,
545 Last_Switches.Increment_Last;
546 Last_Switches.Table (Last_Switches.Last) :=
549 (Project_Tree.Projects.Table
550 (Unit_Data.File_Names
551 (Specification).Project).
552 Object_Directory.Name) &
556 (Unit_Data.File_Names
557 (Specification).Name),
563 -- For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
564 -- sources of the project, or of all projects if -U was
567 for Kind in Spec_Or_Body loop
569 (Unit_Data.File_Names (Kind).Project, Project)
570 and then Unit_Data.File_Names (Kind).Name /= No_File
571 and then Unit_Data.File_Names (Kind).Path.Name /= Slash
573 Last_Switches.Increment_Last;
574 Last_Switches.Table (Last_Switches.Last) :=
577 (Unit_Data.File_Names
578 (Kind).Path.Display_Name));
584 -- If the list of files is too long, create a temporary text file
585 -- that lists these files, and pass this temp file to gnatcheck,
586 -- gnatpp or gnatmetric using switch -files=.
588 if Last_Switches.Last - Current_Last >
589 Max_Files_On_The_Command_Line
592 Temp_File_FD : File_Descriptor;
593 Buffer : String (1 .. 1_000);
595 OK : Boolean := True;
598 Create_Temp_File (Temp_File_FD, Temp_File_Name);
600 if Temp_File_Name /= null then
601 for Index in Current_Last + 1 ..
604 Len := Last_Switches.Table (Index)'Length;
605 Buffer (1 .. Len) := Last_Switches.Table (Index).all;
607 Buffer (Len) := ASCII.LF;
608 Buffer (Len + 1) := ASCII.NUL;
617 Close (Temp_File_FD, OK);
619 Close (Temp_File_FD, OK);
623 -- If there were any problem creating the temp file, then
624 -- pass the list of files.
628 -- Replace list of files with -files=<temp file name>
630 Last_Switches.Set_Last (Current_Last + 1);
631 Last_Switches.Table (Last_Switches.Last) :=
632 new String'("-files=" & Temp_File_Name.all);
645 function Check_Project
646 (Project : Project_Id;
647 Root_Project : Project_Id) return Boolean
650 if Project = No_Project then
653 elsif All_Projects or Project = Root_Project then
656 elsif The_Command = Metric then
661 Data := Project_Tree.Projects.Table (Root_Project);
662 while Data.Extends /= No_Project loop
663 if Project = Data.Extends then
667 Data := Project_Tree.Projects.Table (Data.Extends);
675 -------------------------------
676 -- Check_Relative_Executable --
677 -------------------------------
679 procedure Check_Relative_Executable (Name : in out String_Access) is
680 Exec_File_Name : constant String := Name.all;
683 if not Is_Absolute_Path (Exec_File_Name) then
684 for Index in Exec_File_Name'Range loop
685 if Exec_File_Name (Index) = Directory_Separator then
686 Fail ("relative executable (""" &
688 """) with directory part not allowed " &
689 "when using project files");
693 Get_Name_String (Project_Tree.Projects.Table
694 (Project).Exec_Directory.Name);
696 if Name_Buffer (Name_Len) /= Directory_Separator then
697 Name_Len := Name_Len + 1;
698 Name_Buffer (Name_Len) := Directory_Separator;
701 Name_Buffer (Name_Len + 1 ..
702 Name_Len + Exec_File_Name'Length) :=
704 Name_Len := Name_Len + Exec_File_Name'Length;
705 Name := new String'(Name_Buffer (1 .. Name_Len));
707 end Check_Relative_Executable;
709 --------------------------------
710 -- Configuration_Pragmas_File --
711 --------------------------------
713 function Configuration_Pragmas_File return Path_Name_Type is
715 Prj.Env.Create_Config_Pragmas_File
716 (Project, Project, Project_Tree, Include_Config_Files => False);
717 return Project_Tree.Projects.Table (Project).Config_File_Name;
718 end Configuration_Pragmas_File;
720 ------------------------------
721 -- Delete_Temp_Config_Files --
722 ------------------------------
724 procedure Delete_Temp_Config_Files is
726 pragma Warnings (Off, Success);
729 -- This should only be called if Keep_Temporary_Files is False
731 pragma Assert (not Keep_Temporary_Files);
733 if Project /= No_Project then
734 for Prj in Project_Table.First ..
735 Project_Table.Last (Project_Tree.Projects)
738 Project_Tree.Projects.Table (Prj).Config_File_Temp
741 Output.Write_Str ("Deleting temp configuration file """);
744 (Project_Tree.Projects.Table
745 (Prj).Config_File_Name));
746 Output.Write_Line ("""");
752 (Project_Tree.Projects.Table (Prj).Config_File_Name),
758 -- If a temporary text file that contains a list of files for a tool
759 -- has been created, delete this temporary file.
761 if Temp_File_Name /= null then
762 Delete_File (Temp_File_Name.all, Success);
764 end Delete_Temp_Config_Files;
770 procedure Get_Closure is
771 Args : constant Argument_List :=
772 (1 => new String'("-q"),
773 2 => new String'("-b"),
774 3 => new String'("-P"),
777 6 => new String'("-bargs"),
778 7 => new String'("-R"),
779 8 => new String'("-Z"));
780 -- Arguments for the invocation of gnatmake which are added to the
781 -- Last_Arguments list by this procedure.
783 FD : File_Descriptor;
784 -- File descriptor for the temp file that will get the output of the
785 -- invocation of gnatmake.
787 Name : Path_Name_Type;
788 -- Path of the file FD
790 GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
793 GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
796 Return_Code : Integer;
799 pragma Warnings (Off, Unused);
801 File : Ada.Text_IO.File_Type;
802 Line : String (1 .. 250);
804 -- Used to read file if there is an error, it is good enough to display
805 -- just 250 characters if the first line of the file is very long.
808 Path : Path_Name_Type;
811 if GN_Path = null then
812 Put_Line (Standard_Error, "could not locate " & GN_Name);
816 -- Create the temp file
818 Tempdir.Create_Temp_File (FD, Name);
820 -- And close it, because on VMS Spawn with a file descriptor created
821 -- with Create_Temp_File does not redirect output.
825 -- Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
828 (Program_Name => GN_Path.all,
830 Output_File => Get_Name_String (Name),
832 Return_Code => Return_Code,
837 -- Read the output of the invocation of gnatmake
839 Open (File, In_File, Get_Name_String (Name));
841 -- If it was unsuccessful, display the first line in the file and exit
844 if Return_Code /= 0 then
845 Get_Line (File, Line, Last);
847 if not Keep_Temporary_Files then
853 Put_Line (Standard_Error, Line (1 .. Last));
855 (Standard_Error, "could not get closure of " & ASIS_Main.all);
859 -- Get each file name in the file, find its path and add it the
860 -- list of arguments.
862 while not End_Of_File (File) loop
863 Get_Line (File, Line, Last);
866 for Unit in Unit_Table.First ..
867 Unit_Table.Last (Project_Tree.Units)
869 Udata := Project_Tree.Units.Table (Unit);
871 if Udata.File_Names (Specification).Name /= No_File
873 Get_Name_String (Udata.File_Names (Specification).Name) =
876 Path := Udata.File_Names (Specification).Path.Name;
879 elsif Udata.File_Names (Body_Part).Name /= No_File
881 Get_Name_String (Udata.File_Names (Body_Part).Name) =
884 Path := Udata.File_Names (Body_Part).Path.Name;
889 Last_Switches.Increment_Last;
891 if Path /= No_Path then
892 Last_Switches.Table (Last_Switches.Last) :=
893 new String'(Get_Name_String (Path));
896 Last_Switches.Table (Last_Switches.Last) :=
897 new String'(Line (1 .. Last));
901 if not Keep_Temporary_Files then
913 function Index (Char : Character; Str : String) return Natural is
915 for Index in Str'Range loop
916 if Str (Index) = Char then
928 procedure Process_Link is
929 Look_For_Executable : Boolean := True;
930 There_Are_Libraries : Boolean := False;
931 Path_Option : constant String_Access :=
932 MLib.Linker_Library_Path_Option;
933 Prj : Project_Id := Project;
936 Skip_Executable : Boolean := False;
939 -- Add the default search directories, to be able to find
940 -- libgnat in call to MLib.Utl.Lib_Directory.
942 Add_Default_Search_Dirs;
944 Library_Paths.Set_Last (0);
946 -- Check if there are library project files
948 if MLib.Tgt.Support_For_Libraries /= None then
949 Set_Libraries (Project, Project_Tree, There_Are_Libraries);
952 -- If there are, add the necessary additional switches
954 if There_Are_Libraries then
956 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
958 Last_Switches.Increment_Last;
959 Last_Switches.Table (Last_Switches.Last) :=
960 new String'("-L" & MLib.Utl.Lib_Directory);
961 Last_Switches.Increment_Last;
962 Last_Switches.Table (Last_Switches.Last) :=
963 new String'("-lgnarl");
964 Last_Switches.Increment_Last;
965 Last_Switches.Table (Last_Switches.Last) :=
966 new String'("-lgnat");
968 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
969 -- equivalent) with all the library dirs plus the standard GNAT
972 if Path_Option /= null then
974 Option : String_Access;
975 Length : Natural := Path_Option'Length;
979 if MLib.Separate_Run_Path_Options then
981 -- We are going to create one switch of the form
982 -- "-Wl,-rpath,dir_N" for each directory to consider.
984 -- One switch for each library directory
987 Library_Paths.First .. Library_Paths.Last
989 Last_Switches.Increment_Last;
991 (Last_Switches.Last) := new String'
993 Last_Switches.Table (Index).all);
996 -- One switch for the standard GNAT library dir.
998 Last_Switches.Increment_Last;
1000 (Last_Switches.Last) := new String'
1001 (Path_Option.all & MLib.Utl.Lib_Directory);
1004 -- First, compute the exact length for the switch
1007 Library_Paths.First .. Library_Paths.Last
1009 -- Add the length of the library dir plus one for the
1010 -- directory separator.
1014 Library_Paths.Table (Index)'Length + 1;
1017 -- Finally, add the length of the standard GNAT library dir
1019 Length := Length + MLib.Utl.Lib_Directory'Length;
1020 Option := new String (1 .. Length);
1021 Option (1 .. Path_Option'Length) := Path_Option.all;
1022 Current := Path_Option'Length;
1024 -- Put each library dir followed by a dir separator
1027 Library_Paths.First .. Library_Paths.Last
1032 Library_Paths.Table (Index)'Length) :=
1033 Library_Paths.Table (Index).all;
1036 Library_Paths.Table (Index)'Length + 1;
1037 Option (Current) := Path_Separator;
1040 -- Finally put the standard GNAT library dir
1044 Current + MLib.Utl.Lib_Directory'Length) :=
1045 MLib.Utl.Lib_Directory;
1047 -- And add the switch to the last switches
1049 Last_Switches.Increment_Last;
1050 Last_Switches.Table (Last_Switches.Last) :=
1057 -- Check if the first ALI file specified can be found, either in the
1058 -- object directory of the main project or in an object directory of a
1059 -- project file extended by the main project. If the ALI file can be
1060 -- found, replace its name with its absolute path.
1062 Skip_Executable := False;
1064 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1066 -- If we have an executable just reset the flag
1068 if Skip_Executable then
1069 Skip_Executable := False;
1071 -- If -o, set flag so that next switch is not processed
1073 elsif Last_Switches.Table (J).all = "-o" then
1074 Skip_Executable := True;
1080 Switch : constant String :=
1081 Last_Switches.Table (J).all;
1082 ALI_File : constant String (1 .. Switch'Length + 4) :=
1085 Test_Existence : Boolean := False;
1088 Last := Switch'Length;
1090 -- Skip real switches
1092 if Switch'Length /= 0
1093 and then Switch (Switch'First) /= '-'
1095 -- Append ".ali" if file name does not end with it
1097 if Switch'Length <= 4
1098 or else Switch (Switch'Last - 3 .. Switch'Last)
1101 Last := ALI_File'Last;
1104 -- If file name includes directory information, stop if ALI
1107 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1108 Test_Existence := True;
1111 for K in Switch'Range loop
1112 if Switch (K) = '/' or else
1113 Switch (K) = Directory_Separator
1115 Test_Existence := True;
1121 if Test_Existence then
1122 if Is_Regular_File (ALI_File (1 .. Last)) then
1126 -- Look in object directories if ALI file exists
1131 Dir : constant String :=
1133 (Project_Tree.Projects.Table
1134 (Prj).Object_Directory.Name);
1138 Directory_Separator &
1139 ALI_File (1 .. Last))
1141 -- We have found the correct project, so we
1142 -- replace the file with the absolute path.
1144 Last_Switches.Table (J) :=
1146 (Dir & Directory_Separator &
1147 ALI_File (1 .. Last));
1155 -- Go to the project being extended, if any
1158 Project_Tree.Projects.Table (Prj).Extends;
1159 exit Project_Loop when Prj = No_Project;
1160 end loop Project_Loop;
1165 end loop Switch_Loop;
1167 -- If a relative path output file has been specified, we add the exec
1170 for J in reverse 1 .. Last_Switches.Last - 1 loop
1171 if Last_Switches.Table (J).all = "-o" then
1172 Check_Relative_Executable
1173 (Name => Last_Switches.Table (J + 1));
1174 Look_For_Executable := False;
1179 if Look_For_Executable then
1180 for J in reverse 1 .. First_Switches.Last - 1 loop
1181 if First_Switches.Table (J).all = "-o" then
1182 Look_For_Executable := False;
1183 Check_Relative_Executable
1184 (Name => First_Switches.Table (J + 1));
1190 -- If no executable is specified, then find the name of the first ALI
1191 -- file on the command line and issue a -o switch with the absolute path
1192 -- of the executable in the exec directory.
1194 if Look_For_Executable then
1195 for J in 1 .. Last_Switches.Last loop
1196 Arg := Last_Switches.Table (J);
1199 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1201 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1203 Last := Arg'Last - 4;
1205 elsif Is_Regular_File (Arg.all & ".ali") then
1210 Last_Switches.Increment_Last;
1211 Last_Switches.Table (Last_Switches.Last) :=
1214 (Project_Tree.Projects.Table
1215 (Project).Exec_Directory.Name);
1216 Last_Switches.Increment_Last;
1217 Last_Switches.Table (Last_Switches.Last) :=
1218 new String'(Name_Buffer (1 .. Name_Len) &
1219 Directory_Separator &
1221 (Base_Name (Arg (Arg'First .. Last))));
1229 ---------------------
1230 -- Set_Library_For --
1231 ---------------------
1233 procedure Set_Library_For
1234 (Project : Project_Id;
1235 There_Are_Libraries : in out Boolean)
1237 Path_Option : constant String_Access :=
1238 MLib.Linker_Library_Path_Option;
1241 -- Case of library project
1243 if Project_Tree.Projects.Table (Project).Library then
1244 There_Are_Libraries := True;
1246 -- Add the -L switch
1248 Last_Switches.Increment_Last;
1249 Last_Switches.Table (Last_Switches.Last) :=
1252 (Project_Tree.Projects.Table
1253 (Project).Library_Dir.Name));
1255 -- Add the -l switch
1257 Last_Switches.Increment_Last;
1258 Last_Switches.Table (Last_Switches.Last) :=
1261 (Project_Tree.Projects.Table
1262 (Project).Library_Name));
1264 -- Add the directory to table Library_Paths, to be processed later
1265 -- if library is not static and if Path_Option is not null.
1267 if Project_Tree.Projects.Table (Project).Library_Kind /=
1269 and then Path_Option /= null
1271 Library_Paths.Increment_Last;
1272 Library_Paths.Table (Library_Paths.Last) :=
1273 new String'(Get_Name_String
1274 (Project_Tree.Projects.Table
1275 (Project).Library_Dir.Name));
1278 end Set_Library_For;
1280 ---------------------------
1281 -- Test_If_Relative_Path --
1282 ---------------------------
1284 procedure Test_If_Relative_Path
1285 (Switch : in out String_Access;
1289 if Switch /= null then
1292 Sw : String (1 .. Switch'Length);
1293 Start : Positive := 1;
1298 if Sw (1) = '-' then
1300 and then (Sw (2) = 'A' or else
1301 Sw (2) = 'I' or else
1310 elsif Sw'Length >= 4
1311 and then (Sw (2 .. 3) = "aL" or else
1312 Sw (2 .. 3) = "aO" or else
1317 elsif Sw'Length >= 7
1318 and then Sw (2 .. 6) = "-RTS="
1326 -- If the path is relative, test if it includes directory
1327 -- information. If it does, prepend Parent to the path.
1329 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1330 for J in Start .. Sw'Last loop
1331 if Sw (J) = Directory_Separator then
1334 (Sw (1 .. Start - 1) &
1336 Directory_Separator &
1337 Sw (Start .. Sw'Last));
1344 end Test_If_Relative_Path;
1350 procedure Non_VMS_Usage is
1354 Put_Line ("List of available commands");
1357 for C in Command_List'Range loop
1358 if not Command_List (C).VMS_Only then
1359 if Targparm.AAMP_On_Target then
1365 Put (To_Lower (Command_List (C).Cname.all));
1368 -- Never call gnatstack with a prefix
1371 Put (Command_List (C).Unixcmd.all);
1373 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1377 Sws : Argument_List_Access renames Command_List (C).Unixsws;
1380 for J in Sws'Range loop
1392 Put_Line ("Commands find, list, metric, pretty, stack, stub and xref " &
1393 "accept project file switches -vPx, -Pprj and -Xnam=val");
1397 -------------------------------------
1398 -- Start of processing for GNATCmd --
1399 -------------------------------------
1409 Prj.Initialize (Project_Tree);
1412 Last_Switches.Set_Last (0);
1414 First_Switches.Init;
1415 First_Switches.Set_Last (0);
1417 Carg_Switches.Set_Last (0);
1418 Rules_Switches.Init;
1419 Rules_Switches.Set_Last (0);
1421 VMS_Conv.Initialize;
1423 Set_Mode (Ada_Only);
1425 -- Add the default search directories, to be able to find system.ads in the
1426 -- subsequent call to Targparm.Get_Target_Parameters.
1428 Add_Default_Search_Dirs;
1430 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1431 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1433 Targparm.Get_Target_Parameters;
1435 -- Add the directory where the GNAT driver is invoked in front of the path,
1436 -- if the GNAT driver is invoked with directory information. Do not do this
1437 -- for VMS, where the notion of path does not really exist.
1441 Command : constant String := Command_Name;
1444 for Index in reverse Command'Range loop
1445 if Command (Index) = Directory_Separator then
1447 Absolute_Dir : constant String :=
1449 (Command (Command'First .. Index));
1451 PATH : constant String :=
1454 Getenv ("PATH").all;
1457 Setenv ("PATH", PATH);
1466 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1467 -- filenames and pathnames to Unix style.
1470 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1472 VMS_Conversion (The_Command);
1474 B_Start := new String'("b__");
1476 -- If not on VMS, scan the command line directly
1479 if Argument_Count = 0 then
1485 if Argument_Count > Command_Arg
1486 and then Argument (Command_Arg) = "-v"
1488 Verbose_Mode := True;
1489 Command_Arg := Command_Arg + 1;
1491 elsif Argument_Count > Command_Arg
1492 and then Argument (Command_Arg) = "-dn"
1494 Keep_Temporary_Files := True;
1495 Command_Arg := Command_Arg + 1;
1502 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1504 if Command_List (The_Command).VMS_Only then
1508 & Command_List (The_Command).Cname.all
1509 & """ can only be used on VMS");
1513 when Constraint_Error =>
1515 -- Check if it is an alternate command
1518 Alternate : Alternate_Command;
1521 Alternate := Alternate_Command'Value
1522 (Argument (Command_Arg));
1523 The_Command := Corresponding_To (Alternate);
1526 when Constraint_Error =>
1528 Fail ("Unknown command: " & Argument (Command_Arg));
1532 -- Get the arguments from the command line and from the eventual
1533 -- argument file(s) specified on the command line.
1535 for Arg in Command_Arg + 1 .. Argument_Count loop
1537 The_Arg : constant String := Argument (Arg);
1540 -- Check if an argument file is specified
1542 if The_Arg (The_Arg'First) = '@' then
1544 Arg_File : Ada.Text_IO.File_Type;
1545 Line : String (1 .. 256);
1549 -- Open the file and fail if the file cannot be found
1554 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1559 (Standard_Error, "Cannot open argument file """);
1562 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1564 Put_Line (Standard_Error, """");
1568 -- Read line by line and put the content of each non-
1569 -- empty line in the Last_Switches table.
1571 while not End_Of_File (Arg_File) loop
1572 Get_Line (Arg_File, Line, Last);
1575 Last_Switches.Increment_Last;
1576 Last_Switches.Table (Last_Switches.Last) :=
1577 new String'(Line (1 .. Last));
1585 -- It is not an argument file; just put the argument in
1586 -- the Last_Switches table.
1588 Last_Switches.Increment_Last;
1589 Last_Switches.Table (Last_Switches.Last) :=
1590 new String'(The_Arg);
1598 Program : String_Access;
1599 Exec_Path : String_Access;
1602 if The_Command = Stack then
1603 -- Never call gnatstack with a prefix
1605 Program := new String'(Command_List (The_Command).Unixcmd.all);
1609 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1612 -- Locate the executable for the command
1614 Exec_Path := Locate_Exec_On_Path (Program.all);
1616 if Exec_Path = null then
1617 Put_Line (Standard_Error, "could not locate " & Program.all);
1621 -- If there are switches for the executable, put them as first switches
1623 if Command_List (The_Command).Unixsws /= null then
1624 for J in Command_List (The_Command).Unixsws'Range loop
1625 First_Switches.Increment_Last;
1626 First_Switches.Table (First_Switches.Last) :=
1627 Command_List (The_Command).Unixsws (J);
1631 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, PRETTY, STACK, STUB,
1632 -- METRIC ad XREF, look for project file related switches.
1634 if The_Command = Bind
1635 or else The_Command = Check
1636 or else The_Command = Sync
1637 or else The_Command = Elim
1638 or else The_Command = Find
1639 or else The_Command = Link
1640 or else The_Command = List
1641 or else The_Command = Xref
1642 or else The_Command = Pretty
1643 or else The_Command = Stack
1644 or else The_Command = Stub
1645 or else The_Command = Metric
1649 Tool_Package_Name := Name_Binder;
1650 Packages_To_Check := Packages_To_Check_By_Binder;
1652 Tool_Package_Name := Name_Check;
1653 Packages_To_Check := Packages_To_Check_By_Check;
1655 Tool_Package_Name := Name_Synchronize;
1656 Packages_To_Check := Packages_To_Check_By_Sync;
1658 Tool_Package_Name := Name_Eliminate;
1659 Packages_To_Check := Packages_To_Check_By_Eliminate;
1661 Tool_Package_Name := Name_Finder;
1662 Packages_To_Check := Packages_To_Check_By_Finder;
1664 Tool_Package_Name := Name_Linker;
1665 Packages_To_Check := Packages_To_Check_By_Linker;
1667 Tool_Package_Name := Name_Gnatls;
1668 Packages_To_Check := Packages_To_Check_By_Gnatls;
1670 Tool_Package_Name := Name_Metrics;
1671 Packages_To_Check := Packages_To_Check_By_Metric;
1673 Tool_Package_Name := Name_Pretty_Printer;
1674 Packages_To_Check := Packages_To_Check_By_Pretty;
1676 Tool_Package_Name := Name_Stack;
1677 Packages_To_Check := Packages_To_Check_By_Stack;
1679 Tool_Package_Name := Name_Gnatstub;
1680 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1682 Tool_Package_Name := Name_Cross_Reference;
1683 Packages_To_Check := Packages_To_Check_By_Xref;
1688 -- Check that the switches are consistent. Detect project file
1689 -- related switches.
1693 Arg_Num : Positive := 1;
1694 Argv : String_Access;
1696 procedure Remove_Switch (Num : Positive);
1697 -- Remove a project related switch from table Last_Switches
1703 procedure Remove_Switch (Num : Positive) is
1705 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1706 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1707 Last_Switches.Decrement_Last;
1710 -- Start of processing for Inspect_Switches
1713 while Arg_Num <= Last_Switches.Last loop
1714 Argv := Last_Switches.Table (Arg_Num);
1716 if Argv (Argv'First) = '-' then
1717 if Argv'Length = 1 then
1719 ("switch character cannot be followed by a blank");
1722 -- The two style project files (-p and -P) cannot be used
1725 if (The_Command = Find or else The_Command = Xref)
1726 and then Argv (2) = 'p'
1728 Old_Project_File_Used := True;
1729 if Project_File /= null then
1730 Fail ("-P and -p cannot be used together");
1734 -- --subdirs=... Specify Subdirs
1736 if Argv'Length > Subdirs_Option'Length and then
1738 (Argv'First .. Argv'First + Subdirs_Option'Length - 1) =
1744 (Argv'First + Subdirs_Option'Length .. Argv'Last));
1746 Remove_Switch (Arg_Num);
1748 -- -aPdir Add dir to the project search path
1750 elsif Argv'Length > 3
1751 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1753 Add_Search_Project_Directory
1754 (Argv (Argv'First + 3 .. Argv'Last));
1756 Remove_Switch (Arg_Num);
1758 -- -eL Follow links for files
1760 elsif Argv.all = "-eL" then
1761 Follow_Links_For_Files := True;
1763 Remove_Switch (Arg_Num);
1765 -- -vPx Specify verbosity while parsing project files
1767 elsif Argv'Length = 4
1768 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1770 case Argv (Argv'Last) is
1772 Current_Verbosity := Prj.Default;
1774 Current_Verbosity := Prj.Medium;
1776 Current_Verbosity := Prj.High;
1778 Fail ("Invalid switch: " & Argv.all);
1781 Remove_Switch (Arg_Num);
1783 -- -Pproject_file Specify project file to be used
1785 elsif Argv (Argv'First + 1) = 'P' then
1787 -- Only one -P switch can be used
1789 if Project_File /= null then
1792 & ": second project file forbidden (first is """
1796 -- The two style project files (-p and -P) cannot be
1799 elsif Old_Project_File_Used then
1800 Fail ("-p and -P cannot be used together");
1802 elsif Argv'Length = 2 then
1804 -- There is space between -P and the project file
1805 -- name. -P cannot be the last option.
1807 if Arg_Num = Last_Switches.Last then
1808 Fail ("project file name missing after -P");
1811 Remove_Switch (Arg_Num);
1812 Argv := Last_Switches.Table (Arg_Num);
1814 -- After -P, there must be a project file name,
1815 -- not another switch.
1817 if Argv (Argv'First) = '-' then
1818 Fail ("project file name missing after -P");
1821 Project_File := new String'(Argv.all);
1826 -- No space between -P and project file name
1829 new String'(Argv (Argv'First + 2 .. Argv'Last));
1832 Remove_Switch (Arg_Num);
1834 -- -Xexternal=value Specify an external reference to be
1835 -- used in project files
1837 elsif Argv'Length >= 5
1838 and then Argv (Argv'First + 1) = 'X'
1841 Equal_Pos : constant Natural :=
1844 Argv (Argv'First + 2 .. Argv'Last));
1846 if Equal_Pos >= Argv'First + 3 and then
1847 Equal_Pos /= Argv'Last then
1848 Add (External_Name =>
1849 Argv (Argv'First + 2 .. Equal_Pos - 1),
1850 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1854 & " is not a valid external assignment.");
1858 Remove_Switch (Arg_Num);
1861 (The_Command = Check or else
1862 The_Command = Sync or else
1863 The_Command = Pretty or else
1864 The_Command = Metric or else
1865 The_Command = Stack or else
1867 and then Argv'Length = 2
1868 and then Argv (2) = 'U'
1870 All_Projects := True;
1871 Remove_Switch (Arg_Num);
1874 Arg_Num := Arg_Num + 1;
1877 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1878 or else The_Command = Sync
1879 or else The_Command = Metric
1880 or else The_Command = Pretty)
1881 and then Project_File /= null
1882 and then All_Projects
1884 if ASIS_Main /= null then
1885 Fail ("cannot specify more than one main after -U");
1888 Remove_Switch (Arg_Num);
1892 Arg_Num := Arg_Num + 1;
1895 end Inspect_Switches;
1898 -- If there is a project file specified, parse it, get the switches
1899 -- for the tool and setup PATH environment variables.
1901 if Project_File /= null then
1902 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1905 (Project => Project,
1906 In_Tree => Project_Tree,
1907 Project_File_Name => Project_File.all,
1908 Packages_To_Check => Packages_To_Check);
1910 if Project = Prj.No_Project then
1911 Fail ("""" & Project_File.all & """ processing failed");
1914 -- Check if a package with the name of the tool is in the project
1915 -- file and if there is one, get the switches, if any, and scan them.
1918 Data : constant Prj.Project_Data :=
1919 Project_Tree.Projects.Table (Project);
1921 Pkg : constant Prj.Package_Id :=
1923 (Name => Tool_Package_Name,
1924 In_Packages => Data.Decl.Packages,
1925 In_Tree => Project_Tree);
1927 Element : Package_Element;
1929 Default_Switches_Array : Array_Element_Id;
1931 The_Switches : Prj.Variable_Value;
1932 Current : Prj.String_List_Id;
1933 The_String : String_Element;
1936 if Pkg /= No_Package then
1937 Element := Project_Tree.Packages.Table (Pkg);
1939 -- Packages Gnatls and Gnatstack have a single attribute
1940 -- Switches, that is not an associative array.
1942 if The_Command = List or else The_Command = Stack then
1945 (Variable_Name => Snames.Name_Switches,
1946 In_Variables => Element.Decl.Attributes,
1947 In_Tree => Project_Tree);
1949 -- Packages Binder (for gnatbind), Cross_Reference (for
1950 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1951 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1952 -- (for gnatcheck), and Metric (for gnatmetric) have an
1953 -- attributed Switches, an associative array, indexed by the
1954 -- name of the file.
1956 -- They also have an attribute Default_Switches, indexed by the
1957 -- name of the programming language.
1960 if The_Switches.Kind = Prj.Undefined then
1961 Default_Switches_Array :=
1963 (Name => Name_Default_Switches,
1964 In_Arrays => Element.Decl.Arrays,
1965 In_Tree => Project_Tree);
1966 The_Switches := Prj.Util.Value_Of
1969 In_Array => Default_Switches_Array,
1970 In_Tree => Project_Tree);
1974 -- If there are switches specified in the package of the
1975 -- project file corresponding to the tool, scan them.
1977 case The_Switches.Kind is
1978 when Prj.Undefined =>
1983 Switch : constant String :=
1984 Get_Name_String (The_Switches.Value);
1987 if Switch'Length > 0 then
1988 First_Switches.Increment_Last;
1989 First_Switches.Table (First_Switches.Last) :=
1990 new String'(Switch);
1995 Current := The_Switches.Values;
1996 while Current /= Prj.Nil_String loop
1997 The_String := Project_Tree.String_Elements.
2001 Switch : constant String :=
2002 Get_Name_String (The_String.Value);
2005 if Switch'Length > 0 then
2006 First_Switches.Increment_Last;
2007 First_Switches.Table (First_Switches.Last) :=
2008 new String'(Switch);
2012 Current := The_String.Next;
2018 if The_Command = Bind
2019 or else The_Command = Link
2020 or else The_Command = Elim
2024 (Project_Tree.Projects.Table
2025 (Project).Object_Directory.Name));
2028 -- Set up the env vars for project path files
2030 Prj.Env.Set_Ada_Paths
2031 (Project, Project_Tree, Including_Libraries => False);
2033 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
2034 -- a configuration pragmas file, if necessary.
2036 if The_Command = Pretty
2037 or else The_Command = Metric
2038 or else The_Command = Stub
2039 or else The_Command = Elim
2040 or else The_Command = Check
2041 or else The_Command = Sync
2043 -- If there are switches in package Compiler, put them in the
2044 -- Carg_Switches table.
2047 Data : constant Prj.Project_Data :=
2048 Project_Tree.Projects.Table (Project);
2050 Pkg : constant Prj.Package_Id :=
2052 (Name => Name_Compiler,
2053 In_Packages => Data.Decl.Packages,
2054 In_Tree => Project_Tree);
2056 Element : Package_Element;
2058 Default_Switches_Array : Array_Element_Id;
2060 The_Switches : Prj.Variable_Value;
2061 Current : Prj.String_List_Id;
2062 The_String : String_Element;
2065 if Pkg /= No_Package then
2066 Element := Project_Tree.Packages.Table (Pkg);
2068 Default_Switches_Array :=
2070 (Name => Name_Default_Switches,
2071 In_Arrays => Element.Decl.Arrays,
2072 In_Tree => Project_Tree);
2073 The_Switches := Prj.Util.Value_Of
2076 In_Array => Default_Switches_Array,
2077 In_Tree => Project_Tree);
2079 -- If there are switches specified in the package of the
2080 -- project file corresponding to the tool, scan them.
2082 case The_Switches.Kind is
2083 when Prj.Undefined =>
2088 Switch : constant String :=
2089 Get_Name_String (The_Switches.Value);
2091 if Switch'Length > 0 then
2092 Add_To_Carg_Switches (new String'(Switch));
2097 Current := The_Switches.Values;
2098 while Current /= Prj.Nil_String loop
2100 Project_Tree.String_Elements.Table (Current);
2103 Switch : constant String :=
2104 Get_Name_String (The_String.Value);
2106 if Switch'Length > 0 then
2107 Add_To_Carg_Switches (new String'(Switch));
2111 Current := The_String.Next;
2117 -- If -cargs is one of the switches, move the following switches
2118 -- to the Carg_Switches table.
2120 for J in 1 .. First_Switches.Last loop
2121 if First_Switches.Table (J).all = "-cargs" then
2127 -- Move the switches that are before -rules when the
2128 -- command is CHECK.
2131 while K <= First_Switches.Last
2133 (The_Command /= Check
2134 or else First_Switches.Table (K).all /= "-rules")
2136 Add_To_Carg_Switches (First_Switches.Table (K));
2140 if K > First_Switches.Last then
2141 First_Switches.Set_Last (J - 1);
2145 while K <= First_Switches.Last loop
2147 First_Switches.Table (Last) :=
2148 First_Switches.Table (K);
2152 First_Switches.Set_Last (Last);
2160 for J in 1 .. Last_Switches.Last loop
2161 if Last_Switches.Table (J).all = "-cargs" then
2167 -- Move the switches that are before -rules when the
2168 -- command is CHECK.
2171 while K <= Last_Switches.Last
2173 (The_Command /= Check
2175 Last_Switches.Table (K).all /= "-rules")
2177 Add_To_Carg_Switches (Last_Switches.Table (K));
2181 if K > Last_Switches.Last then
2182 Last_Switches.Set_Last (J - 1);
2186 while K <= Last_Switches.Last loop
2188 Last_Switches.Table (Last) :=
2189 Last_Switches.Table (K);
2193 Last_Switches.Set_Last (Last);
2202 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2205 if CP_File /= No_Path then
2206 if The_Command = Elim then
2207 First_Switches.Increment_Last;
2208 First_Switches.Table (First_Switches.Last) :=
2209 new String'("-C" & Get_Name_String (CP_File));
2212 Add_To_Carg_Switches
2213 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2219 if The_Command = Link then
2223 if The_Command = Link or The_Command = Bind then
2225 -- For files that are specified as relative paths with directory
2226 -- information, we convert them to absolute paths, with parent
2227 -- being the current working directory if specified on the command
2228 -- line and the project directory if specified in the project
2229 -- file. This is what gnatmake is doing for linker and binder
2232 for J in 1 .. Last_Switches.Last loop
2233 Test_If_Relative_Path
2234 (Last_Switches.Table (J), Current_Work_Dir);
2238 (Project_Tree.Projects.Table (Project).Directory.Name);
2241 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2243 for J in 1 .. First_Switches.Last loop
2244 Test_If_Relative_Path
2245 (First_Switches.Table (J), Project_Dir);
2249 elsif The_Command = Stub then
2251 Data : constant Prj.Project_Data :=
2252 Project_Tree.Projects.Table (Project);
2253 File_Index : Integer := 0;
2254 Dir_Index : Integer := 0;
2255 Last : constant Integer := Last_Switches.Last;
2258 for Index in 1 .. Last loop
2259 if Last_Switches.Table (Index)
2260 (Last_Switches.Table (Index)'First) /= '-'
2262 File_Index := Index;
2267 -- If the naming scheme of the project file is not standard,
2268 -- and if the file name ends with the spec suffix, then
2269 -- indicate to gnatstub the name of the body file with
2272 if Body_Suffix_Id_Of (Project_Tree, Name_Ada, Data.Naming) /=
2273 Prj.Default_Ada_Spec_Suffix
2275 if File_Index /= 0 then
2277 Spec : constant String :=
2278 Base_Name (Last_Switches.Table (File_Index).all);
2279 Last : Natural := Spec'Last;
2284 (Project_Tree, Name_Ada, Data.Naming));
2286 if Spec'Length > Name_Len
2287 and then Spec (Last - Name_Len + 1 .. Last) =
2288 Name_Buffer (1 .. Name_Len)
2290 Last := Last - Name_Len;
2293 (Project_Tree, Name_Ada, Data.Naming));
2294 Last_Switches.Increment_Last;
2295 Last_Switches.Table (Last_Switches.Last) :=
2297 Last_Switches.Increment_Last;
2298 Last_Switches.Table (Last_Switches.Last) :=
2299 new String'(Spec (Spec'First .. Last) &
2300 Name_Buffer (1 .. Name_Len));
2306 -- Add the directory of the spec as the destination directory
2307 -- of the body, if there is no destination directory already
2310 if File_Index /= 0 then
2311 for Index in File_Index + 1 .. Last loop
2312 if Last_Switches.Table (Index)
2313 (Last_Switches.Table (Index)'First) /= '-'
2320 if Dir_Index = 0 then
2321 Last_Switches.Increment_Last;
2322 Last_Switches.Table (Last_Switches.Last) :=
2324 (Dir_Name (Last_Switches.Table (File_Index).all));
2330 -- For gnatmetric, the generated files should be put in the object
2331 -- directory. This must be the first switch, because it may be
2332 -- overridden by a switch in package Metrics in the project file or
2333 -- by a command line option. Note that we don't add the -d= switch
2334 -- if there is no object directory available.
2336 if The_Command = Metric
2338 Project_Tree.Projects.Table (Project).Object_Directory /=
2341 First_Switches.Increment_Last;
2342 First_Switches.Table (2 .. First_Switches.Last) :=
2343 First_Switches.Table (1 .. First_Switches.Last - 1);
2344 First_Switches.Table (1) :=
2347 (Project_Tree.Projects.Table
2348 (Project).Object_Directory.Name));
2351 -- For gnat check, -rules and the following switches need to be the
2352 -- last options. So, we move all these switches to table
2355 if The_Command = Check then
2358 -- Set to rank of options preceding "-rules"
2360 In_Rules_Switches : Boolean;
2361 -- Set to True when options "-rules" is found
2364 New_Last := First_Switches.Last;
2365 In_Rules_Switches := False;
2367 for J in 1 .. First_Switches.Last loop
2368 if In_Rules_Switches then
2369 Add_To_Rules_Switches (First_Switches.Table (J));
2371 elsif First_Switches.Table (J).all = "-rules" then
2373 In_Rules_Switches := True;
2377 if In_Rules_Switches then
2378 First_Switches.Set_Last (New_Last);
2381 New_Last := Last_Switches.Last;
2382 In_Rules_Switches := False;
2384 for J in 1 .. Last_Switches.Last loop
2385 if In_Rules_Switches then
2386 Add_To_Rules_Switches (Last_Switches.Table (J));
2388 elsif Last_Switches.Table (J).all = "-rules" then
2390 In_Rules_Switches := True;
2394 if In_Rules_Switches then
2395 Last_Switches.Set_Last (New_Last);
2400 -- For gnat check, sync, metric or pretty with -U + a main, get the
2401 -- list of sources from the closure and add them to the arguments.
2403 if ASIS_Main /= null then
2406 -- On VMS, set up the env var again for source dirs file. This is
2407 -- because the call to gnatmake has set this env var to another
2408 -- file that has now been deleted.
2410 if Hostparm.OpenVMS then
2412 -- First make sure that the recorded file names are empty
2414 Prj.Env.Initialize (Project_Tree);
2416 Prj.Env.Set_Ada_Paths
2417 (Project, Project_Tree, Including_Libraries => False);
2420 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2421 -- and gnat stack, if no file has been put on the command line, call
2422 -- tool with all the sources of the main project.
2424 elsif The_Command = Check or else
2425 The_Command = Sync or else
2426 The_Command = Pretty or else
2427 The_Command = Metric or else
2428 The_Command = List or else
2435 -- Gather all the arguments and invoke the executable
2438 The_Args : Argument_List
2439 (1 .. First_Switches.Last +
2440 Last_Switches.Last +
2441 Carg_Switches.Last +
2442 Rules_Switches.Last);
2443 Arg_Num : Natural := 0;
2446 for J in 1 .. First_Switches.Last loop
2447 Arg_Num := Arg_Num + 1;
2448 The_Args (Arg_Num) := First_Switches.Table (J);
2451 for J in 1 .. Last_Switches.Last loop
2452 Arg_Num := Arg_Num + 1;
2453 The_Args (Arg_Num) := Last_Switches.Table (J);
2456 for J in 1 .. Carg_Switches.Last loop
2457 Arg_Num := Arg_Num + 1;
2458 The_Args (Arg_Num) := Carg_Switches.Table (J);
2461 for J in 1 .. Rules_Switches.Last loop
2462 Arg_Num := Arg_Num + 1;
2463 The_Args (Arg_Num) := Rules_Switches.Table (J);
2466 -- If Display_Command is on, only display the generated command
2468 if Display_Command then
2469 Put (Standard_Error, "generated command -->");
2470 Put (Standard_Error, Exec_Path.all);
2472 for Arg in The_Args'Range loop
2473 Put (Standard_Error, " ");
2474 Put (Standard_Error, The_Args (Arg).all);
2477 Put (Standard_Error, "<--");
2478 New_Line (Standard_Error);
2482 if Verbose_Mode then
2483 Output.Write_Str (Exec_Path.all);
2485 for Arg in The_Args'Range loop
2486 Output.Write_Char (' ');
2487 Output.Write_Str (The_Args (Arg).all);
2494 Exit_Status (Spawn (Exec_Path.all, The_Args));
2501 if not Keep_Temporary_Files then
2502 Prj.Env.Delete_All_Path_Files (Project_Tree);
2503 Delete_Temp_Config_Files;
2506 Set_Exit_Status (Failure);
2509 if not Keep_Temporary_Files then
2510 Prj.Env.Delete_All_Path_Files (Project_Tree);
2511 Delete_Temp_Config_Files;
2514 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2515 -- return an understandable VMS exit status. However the exit status
2516 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2517 -- just a simple success or failure on VMS.
2519 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2520 Set_Exit_Status (Failure);
2522 Set_Exit_Status (My_Exit_Status);