1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2008, 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 -- First, compute the exact length for the switch
982 Library_Paths.First .. Library_Paths.Last
984 -- Add the length of the library dir plus one for the
985 -- directory separator.
989 Library_Paths.Table (Index)'Length + 1;
992 -- Finally, add the length of the standard GNAT library dir
994 Length := Length + MLib.Utl.Lib_Directory'Length;
995 Option := new String (1 .. Length);
996 Option (1 .. Path_Option'Length) := Path_Option.all;
997 Current := Path_Option'Length;
999 -- Put each library dir followed by a dir separator
1002 Library_Paths.First .. Library_Paths.Last
1007 Library_Paths.Table (Index)'Length) :=
1008 Library_Paths.Table (Index).all;
1011 Library_Paths.Table (Index)'Length + 1;
1012 Option (Current) := Path_Separator;
1015 -- Finally put the standard GNAT library dir
1019 Current + MLib.Utl.Lib_Directory'Length) :=
1020 MLib.Utl.Lib_Directory;
1022 -- And add the switch to the last switches
1024 Last_Switches.Increment_Last;
1025 Last_Switches.Table (Last_Switches.Last) :=
1031 -- Check if the first ALI file specified can be found, either in the
1032 -- object directory of the main project or in an object directory of a
1033 -- project file extended by the main project. If the ALI file can be
1034 -- found, replace its name with its absolute path.
1036 Skip_Executable := False;
1038 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1040 -- If we have an executable just reset the flag
1042 if Skip_Executable then
1043 Skip_Executable := False;
1045 -- If -o, set flag so that next switch is not processed
1047 elsif Last_Switches.Table (J).all = "-o" then
1048 Skip_Executable := True;
1054 Switch : constant String :=
1055 Last_Switches.Table (J).all;
1057 ALI_File : constant String (1 .. Switch'Length + 4) :=
1060 Test_Existence : Boolean := False;
1063 Last := Switch'Length;
1065 -- Skip real switches
1067 if Switch'Length /= 0
1068 and then Switch (Switch'First) /= '-'
1070 -- Append ".ali" if file name does not end with it
1072 if Switch'Length <= 4
1073 or else Switch (Switch'Last - 3 .. Switch'Last)
1076 Last := ALI_File'Last;
1079 -- If file name includes directory information, stop if ALI
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
1101 -- Look in object directories if ALI file exists
1106 Dir : constant String :=
1108 (Project_Tree.Projects.Table
1109 (Prj).Object_Directory.Name);
1113 Directory_Separator &
1114 ALI_File (1 .. Last))
1116 -- We have found the correct project, so we
1117 -- replace the file with the absolute path.
1119 Last_Switches.Table (J) :=
1121 (Dir
& Directory_Separator
&
1122 ALI_File
(1 .. Last
));
1130 -- Go to the project being extended, if any
1133 Project_Tree
.Projects
.Table
(Prj
).Extends
;
1134 exit Project_Loop
when Prj
= No_Project
;
1135 end loop Project_Loop
;
1140 end loop Switch_Loop
;
1142 -- If a relative path output file has been specified, we add the exec
1145 for J
in reverse 1 .. Last_Switches
.Last
- 1 loop
1146 if Last_Switches
.Table
(J
).all = "-o" then
1147 Check_Relative_Executable
1148 (Name
=> Last_Switches
.Table
(J
+ 1));
1149 Look_For_Executable
:= False;
1154 if Look_For_Executable
then
1155 for J
in reverse 1 .. First_Switches
.Last
- 1 loop
1156 if First_Switches
.Table
(J
).all = "-o" then
1157 Look_For_Executable
:= False;
1158 Check_Relative_Executable
1159 (Name
=> First_Switches
.Table
(J
+ 1));
1165 -- If no executable is specified, then find the name of the first ALI
1166 -- file on the command line and issue a -o switch with the absolute path
1167 -- of the executable in the exec directory.
1169 if Look_For_Executable
then
1170 for J
in 1 .. Last_Switches
.Last
loop
1171 Arg
:= Last_Switches
.Table
(J
);
1174 if Arg
'Length /= 0 and then Arg
(Arg
'First) /= '-' then
1176 and then Arg
(Arg
'Last - 3 .. Arg
'Last) = ".ali"
1178 Last
:= Arg
'Last - 4;
1180 elsif Is_Regular_File
(Arg
.all & ".ali") then
1185 Last_Switches
.Increment_Last
;
1186 Last_Switches
.Table
(Last_Switches
.Last
) :=
1189 (Project_Tree.Projects.Table
1190 (Project).Exec_Directory.Name);
1191 Last_Switches.Increment_Last;
1192 Last_Switches.Table (Last_Switches.Last) :=
1193 new String'(Name_Buffer
(1 .. Name_Len
) &
1194 Directory_Separator
&
1196 (Base_Name
(Arg
(Arg
'First .. Last
))));
1204 ---------------------
1205 -- Set_Library_For --
1206 ---------------------
1208 procedure Set_Library_For
1209 (Project
: Project_Id
;
1210 There_Are_Libraries
: in out Boolean)
1212 Path_Option
: constant String_Access
:=
1213 MLib
.Linker_Library_Path_Option
;
1216 -- Case of library project
1218 if Project_Tree
.Projects
.Table
(Project
).Library
then
1219 There_Are_Libraries
:= True;
1221 -- Add the -L switch
1223 Last_Switches
.Increment_Last
;
1224 Last_Switches
.Table
(Last_Switches
.Last
) :=
1227 (Project_Tree.Projects.Table
1228 (Project).Library_Dir.Name));
1230 -- Add the -l switch
1232 Last_Switches.Increment_Last;
1233 Last_Switches.Table (Last_Switches.Last) :=
1236 (Project_Tree
.Projects
.Table
1237 (Project
).Library_Name
));
1239 -- Add the directory to table Library_Paths, to be processed later
1240 -- if library is not static and if Path_Option is not null.
1242 if Project_Tree
.Projects
.Table
(Project
).Library_Kind
/=
1244 and then Path_Option
/= null
1246 Library_Paths
.Increment_Last
;
1247 Library_Paths
.Table
(Library_Paths
.Last
) :=
1248 new String'(Get_Name_String
1249 (Project_Tree.Projects.Table
1250 (Project).Library_Dir.Name));
1253 end Set_Library_For;
1255 ---------------------------
1256 -- Test_If_Relative_Path --
1257 ---------------------------
1259 procedure Test_If_Relative_Path
1260 (Switch : in out String_Access;
1264 if Switch /= null then
1267 Sw : String (1 .. Switch'Length);
1268 Start : Positive := 1;
1273 if Sw (1) = '-' then
1275 and then (Sw (2) = 'A
' or else
1276 Sw (2) = 'I
' or else
1285 elsif Sw'Length >= 4
1286 and then (Sw (2 .. 3) = "aL" or else
1287 Sw (2 .. 3) = "aO" or else
1292 elsif Sw'Length >= 7
1293 and then Sw (2 .. 6) = "-RTS="
1301 -- If the path is relative, test if it includes directory
1302 -- information. If it does, prepend Parent to the path.
1304 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1305 for J in Start .. Sw'Last loop
1306 if Sw (J) = Directory_Separator then
1309 (Sw
(1 .. Start
- 1) &
1311 Directory_Separator
&
1312 Sw
(Start
.. Sw
'Last));
1319 end Test_If_Relative_Path
;
1325 procedure Non_VMS_Usage
is
1329 Put_Line
("List of available commands");
1332 for C
in Command_List
'Range loop
1333 if not Command_List
(C
).VMS_Only
then
1334 if Targparm
.AAMP_On_Target
then
1340 Put
(To_Lower
(Command_List
(C
).Cname
.all));
1343 -- Never call gnatstack with a prefix
1346 Put
(Command_List
(C
).Unixcmd
.all);
1348 Put
(Program_Name
(Command_List
(C
).Unixcmd
.all, "gnat").all);
1352 Sws
: Argument_List_Access
renames Command_List
(C
).Unixsws
;
1355 for J
in Sws
'Range loop
1367 Put_Line
("Commands find, list, metric, pretty, stack, stub and xref " &
1368 "accept project file switches -vPx, -Pprj and -Xnam=val");
1372 -------------------------------------
1373 -- Start of processing for GNATCmd --
1374 -------------------------------------
1384 Prj
.Initialize
(Project_Tree
);
1387 Last_Switches
.Set_Last
(0);
1389 First_Switches
.Init
;
1390 First_Switches
.Set_Last
(0);
1392 Carg_Switches
.Set_Last
(0);
1393 Rules_Switches
.Init
;
1394 Rules_Switches
.Set_Last
(0);
1396 VMS_Conv
.Initialize
;
1398 Set_Mode
(Ada_Only
);
1400 -- Add the default search directories, to be able to find system.ads in the
1401 -- subsequent call to Targparm.Get_Target_Parameters.
1403 Add_Default_Search_Dirs
;
1405 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1406 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1408 Targparm
.Get_Target_Parameters
;
1410 -- Add the directory where the GNAT driver is invoked in front of the path,
1411 -- if the GNAT driver is invoked with directory information. Do not do this
1412 -- for VMS, where the notion of path does not really exist.
1416 Command
: constant String := Command_Name
;
1419 for Index
in reverse Command
'Range loop
1420 if Command
(Index
) = Directory_Separator
then
1422 Absolute_Dir
: constant String :=
1424 (Command
(Command
'First .. Index
));
1426 PATH
: constant String :=
1429 Getenv
("PATH").all;
1432 Setenv
("PATH", PATH
);
1441 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1442 -- filenames and pathnames to Unix style.
1445 or else To_Lower
(Getenv
("EMULATE_VMS").all) = "true"
1447 VMS_Conversion
(The_Command
);
1449 B_Start
:= new String'("b__");
1451 -- If not on VMS, scan the command line directly
1454 if Argument_Count = 0 then
1460 if Argument_Count > Command_Arg
1461 and then Argument (Command_Arg) = "-v"
1463 Verbose_Mode := True;
1464 Command_Arg := Command_Arg + 1;
1466 elsif Argument_Count > Command_Arg
1467 and then Argument (Command_Arg) = "-dn"
1469 Keep_Temporary_Files := True;
1470 Command_Arg := Command_Arg + 1;
1477 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1479 if Command_List (The_Command).VMS_Only then
1483 Command_List (The_Command).Cname.all,
1484 """ can only be used on VMS");
1488 when Constraint_Error =>
1490 -- Check if it is an alternate command
1493 Alternate : Alternate_Command;
1496 Alternate := Alternate_Command'Value
1497 (Argument (Command_Arg));
1498 The_Command := Corresponding_To (Alternate);
1501 when Constraint_Error =>
1503 Fail ("Unknown command: ", Argument (Command_Arg));
1507 -- Get the arguments from the command line and from the eventual
1508 -- argument file(s) specified on the command line.
1510 for Arg in Command_Arg + 1 .. Argument_Count loop
1512 The_Arg : constant String := Argument (Arg);
1515 -- Check if an argument file is specified
1517 if The_Arg (The_Arg'First) = '@
' then
1519 Arg_File : Ada.Text_IO.File_Type;
1520 Line : String (1 .. 256);
1524 -- Open the file and fail if the file cannot be found
1529 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1534 (Standard_Error, "Cannot open argument file """);
1537 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1539 Put_Line (Standard_Error, """");
1543 -- Read line by line and put the content of each non-
1544 -- empty line in the Last_Switches table.
1546 while not End_Of_File (Arg_File) loop
1547 Get_Line (Arg_File, Line, Last);
1550 Last_Switches.Increment_Last;
1551 Last_Switches.Table (Last_Switches.Last) :=
1552 new String'(Line
(1 .. Last
));
1560 -- It is not an argument file; just put the argument in
1561 -- the Last_Switches table.
1563 Last_Switches
.Increment_Last
;
1564 Last_Switches
.Table
(Last_Switches
.Last
) :=
1565 new String'(The_Arg);
1573 Program : String_Access;
1574 Exec_Path : String_Access;
1577 if The_Command = Stack then
1578 -- Never call gnatstack with a prefix
1580 Program := new String'(Command_List
(The_Command
).Unixcmd
.all);
1584 Program_Name
(Command_List
(The_Command
).Unixcmd
.all, "gnat");
1587 -- Locate the executable for the command
1589 Exec_Path
:= Locate_Exec_On_Path
(Program
.all);
1591 if Exec_Path
= null then
1592 Put_Line
(Standard_Error
, "could not locate " & Program
.all);
1596 -- If there are switches for the executable, put them as first switches
1598 if Command_List
(The_Command
).Unixsws
/= null then
1599 for J
in Command_List
(The_Command
).Unixsws
'Range loop
1600 First_Switches
.Increment_Last
;
1601 First_Switches
.Table
(First_Switches
.Last
) :=
1602 Command_List
(The_Command
).Unixsws
(J
);
1606 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, PRETTY, STACK, STUB,
1607 -- METRIC ad XREF, look for project file related switches.
1609 if The_Command
= Bind
1610 or else The_Command
= Check
1611 or else The_Command
= Sync
1612 or else The_Command
= Elim
1613 or else The_Command
= Find
1614 or else The_Command
= Link
1615 or else The_Command
= List
1616 or else The_Command
= Xref
1617 or else The_Command
= Pretty
1618 or else The_Command
= Stack
1619 or else The_Command
= Stub
1620 or else The_Command
= Metric
1624 Tool_Package_Name
:= Name_Binder
;
1625 Packages_To_Check
:= Packages_To_Check_By_Binder
;
1627 Tool_Package_Name
:= Name_Check
;
1628 Packages_To_Check
:= Packages_To_Check_By_Check
;
1630 Tool_Package_Name
:= Name_Synchronize
;
1631 Packages_To_Check
:= Packages_To_Check_By_Sync
;
1633 Tool_Package_Name
:= Name_Eliminate
;
1634 Packages_To_Check
:= Packages_To_Check_By_Eliminate
;
1636 Tool_Package_Name
:= Name_Finder
;
1637 Packages_To_Check
:= Packages_To_Check_By_Finder
;
1639 Tool_Package_Name
:= Name_Linker
;
1640 Packages_To_Check
:= Packages_To_Check_By_Linker
;
1642 Tool_Package_Name
:= Name_Gnatls
;
1643 Packages_To_Check
:= Packages_To_Check_By_Gnatls
;
1645 Tool_Package_Name
:= Name_Metrics
;
1646 Packages_To_Check
:= Packages_To_Check_By_Metric
;
1648 Tool_Package_Name
:= Name_Pretty_Printer
;
1649 Packages_To_Check
:= Packages_To_Check_By_Pretty
;
1651 Tool_Package_Name
:= Name_Stack
;
1652 Packages_To_Check
:= Packages_To_Check_By_Stack
;
1654 Tool_Package_Name
:= Name_Gnatstub
;
1655 Packages_To_Check
:= Packages_To_Check_By_Gnatstub
;
1657 Tool_Package_Name
:= Name_Cross_Reference
;
1658 Packages_To_Check
:= Packages_To_Check_By_Xref
;
1663 -- Check that the switches are consistent. Detect project file
1664 -- related switches.
1668 Arg_Num
: Positive := 1;
1669 Argv
: String_Access
;
1671 procedure Remove_Switch
(Num
: Positive);
1672 -- Remove a project related switch from table Last_Switches
1678 procedure Remove_Switch
(Num
: Positive) is
1680 Last_Switches
.Table
(Num
.. Last_Switches
.Last
- 1) :=
1681 Last_Switches
.Table
(Num
+ 1 .. Last_Switches
.Last
);
1682 Last_Switches
.Decrement_Last
;
1685 -- Start of processing for Inspect_Switches
1688 while Arg_Num
<= Last_Switches
.Last
loop
1689 Argv
:= Last_Switches
.Table
(Arg_Num
);
1691 if Argv
(Argv
'First) = '-' then
1692 if Argv
'Length = 1 then
1694 ("switch character cannot be followed by a blank");
1697 -- The two style project files (-p and -P) cannot be used
1700 if (The_Command
= Find
or else The_Command
= Xref
)
1701 and then Argv
(2) = 'p'
1703 Old_Project_File_Used
:= True;
1704 if Project_File
/= null then
1705 Fail
("-P and -p cannot be used together");
1709 -- --subdirs=... Specify Subdirs
1711 if Argv
'Length > Subdirs_Option
'Length and then
1713 (Argv
'First .. Argv
'First + Subdirs_Option
'Length - 1) =
1719 (Argv'First + Subdirs_Option'Length .. Argv'Last));
1721 Remove_Switch (Arg_Num);
1723 -- -aPdir Add dir to the project search path
1725 elsif Argv'Length > 3
1726 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1728 Add_Search_Project_Directory
1729 (Argv (Argv'First + 3 .. Argv'Last));
1731 Remove_Switch (Arg_Num);
1733 -- -eL Follow links for files
1735 elsif Argv.all = "-eL" then
1736 Follow_Links_For_Files := True;
1738 Remove_Switch (Arg_Num);
1740 -- -vPx Specify verbosity while parsing project files
1742 elsif Argv'Length = 4
1743 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1745 case Argv (Argv'Last) is
1747 Current_Verbosity := Prj.Default;
1749 Current_Verbosity := Prj.Medium;
1751 Current_Verbosity := Prj.High;
1753 Fail ("Invalid switch: ", Argv.all);
1756 Remove_Switch (Arg_Num);
1758 -- -Pproject_file Specify project file to be used
1760 elsif Argv (Argv'First + 1) = 'P
' then
1762 -- Only one -P switch can be used
1764 if Project_File /= null then
1767 ": second project file forbidden (first is """,
1768 Project_File.all & """)");
1770 -- The two style project files (-p and -P) cannot be
1773 elsif Old_Project_File_Used then
1774 Fail ("-p and -P cannot be used together");
1776 elsif Argv'Length = 2 then
1778 -- There is space between -P and the project file
1779 -- name. -P cannot be the last option.
1781 if Arg_Num = Last_Switches.Last then
1782 Fail ("project file name missing after -P");
1785 Remove_Switch (Arg_Num);
1786 Argv := Last_Switches.Table (Arg_Num);
1788 -- After -P, there must be a project file name,
1789 -- not another switch.
1791 if Argv (Argv'First) = '-' then
1792 Fail ("project file name missing after -P");
1795 Project_File := new String'(Argv
.all);
1800 -- No space between -P and project file name
1803 new String'(Argv (Argv'First + 2 .. Argv'Last));
1806 Remove_Switch (Arg_Num);
1808 -- -Xexternal=value Specify an external reference to be
1809 -- used in project files
1811 elsif Argv'Length >= 5
1812 and then Argv (Argv'First + 1) = 'X
'
1815 Equal_Pos : constant Natural :=
1818 Argv (Argv'First + 2 .. Argv'Last));
1820 if Equal_Pos >= Argv'First + 3 and then
1821 Equal_Pos /= Argv'Last then
1822 Add (External_Name =>
1823 Argv (Argv'First + 2 .. Equal_Pos - 1),
1824 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1828 " is not a valid external assignment.");
1832 Remove_Switch (Arg_Num);
1835 (The_Command = Check or else
1836 The_Command = Sync or else
1837 The_Command = Pretty or else
1838 The_Command = Metric or else
1839 The_Command = Stack or else
1841 and then Argv'Length = 2
1842 and then Argv (2) = 'U
'
1844 All_Projects := True;
1845 Remove_Switch (Arg_Num);
1848 Arg_Num := Arg_Num + 1;
1851 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1852 or else The_Command = Sync
1853 or else The_Command = Metric
1854 or else The_Command = Pretty)
1855 and then Project_File /= null
1856 and then All_Projects
1858 if ASIS_Main /= null then
1859 Fail ("cannot specify more than one main after -U");
1862 Remove_Switch (Arg_Num);
1866 Arg_Num := Arg_Num + 1;
1869 end Inspect_Switches;
1872 -- If there is a project file specified, parse it, get the switches
1873 -- for the tool and setup PATH environment variables.
1875 if Project_File /= null then
1876 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1879 (Project => Project,
1880 In_Tree => Project_Tree,
1881 Project_File_Name => Project_File.all,
1882 Packages_To_Check => Packages_To_Check);
1884 if Project = Prj.No_Project then
1885 Fail ("""", Project_File.all, """ processing failed");
1888 -- Check if a package with the name of the tool is in the project
1889 -- file and if there is one, get the switches, if any, and scan them.
1892 Data : constant Prj.Project_Data :=
1893 Project_Tree.Projects.Table (Project);
1895 Pkg : constant Prj.Package_Id :=
1897 (Name => Tool_Package_Name,
1898 In_Packages => Data.Decl.Packages,
1899 In_Tree => Project_Tree);
1901 Element : Package_Element;
1903 Default_Switches_Array : Array_Element_Id;
1905 The_Switches : Prj.Variable_Value;
1906 Current : Prj.String_List_Id;
1907 The_String : String_Element;
1910 if Pkg /= No_Package then
1911 Element := Project_Tree.Packages.Table (Pkg);
1913 -- Packages Gnatls and Gnatstack have a single attribute
1914 -- Switches, that is not an associative array.
1916 if The_Command = List or else The_Command = Stack then
1919 (Variable_Name => Snames.Name_Switches,
1920 In_Variables => Element.Decl.Attributes,
1921 In_Tree => Project_Tree);
1923 -- Packages Binder (for gnatbind), Cross_Reference (for
1924 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1925 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1926 -- (for gnatcheck), and Metric (for gnatmetric) have an
1927 -- attributed Switches, an associative array, indexed by the
1928 -- name of the file.
1930 -- They also have an attribute Default_Switches, indexed by the
1931 -- name of the programming language.
1934 if The_Switches.Kind = Prj.Undefined then
1935 Default_Switches_Array :=
1937 (Name => Name_Default_Switches,
1938 In_Arrays => Element.Decl.Arrays,
1939 In_Tree => Project_Tree);
1940 The_Switches := Prj.Util.Value_Of
1943 In_Array => Default_Switches_Array,
1944 In_Tree => Project_Tree);
1948 -- If there are switches specified in the package of the
1949 -- project file corresponding to the tool, scan them.
1951 case The_Switches.Kind is
1952 when Prj.Undefined =>
1957 Switch : constant String :=
1958 Get_Name_String (The_Switches.Value);
1961 if Switch'Length > 0 then
1962 First_Switches.Increment_Last;
1963 First_Switches.Table (First_Switches.Last) :=
1964 new String'(Switch
);
1969 Current
:= The_Switches
.Values
;
1970 while Current
/= Prj
.Nil_String
loop
1971 The_String
:= Project_Tree
.String_Elements
.
1975 Switch
: constant String :=
1976 Get_Name_String
(The_String
.Value
);
1979 if Switch
'Length > 0 then
1980 First_Switches
.Increment_Last
;
1981 First_Switches
.Table
(First_Switches
.Last
) :=
1982 new String'(Switch);
1986 Current := The_String.Next;
1992 if The_Command = Bind
1993 or else The_Command = Link
1994 or else The_Command = Elim
1998 (Project_Tree.Projects.Table
1999 (Project).Object_Directory.Name));
2002 -- Set up the env vars for project path files
2004 Prj.Env.Set_Ada_Paths
2005 (Project, Project_Tree, Including_Libraries => False);
2007 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
2008 -- a configuration pragmas file, if necessary.
2010 if The_Command = Pretty
2011 or else The_Command = Metric
2012 or else The_Command = Stub
2013 or else The_Command = Elim
2014 or else The_Command = Check
2015 or else The_Command = Sync
2017 -- If there are switches in package Compiler, put them in the
2018 -- Carg_Switches table.
2021 Data : constant Prj.Project_Data :=
2022 Project_Tree.Projects.Table (Project);
2024 Pkg : constant Prj.Package_Id :=
2026 (Name => Name_Compiler,
2027 In_Packages => Data.Decl.Packages,
2028 In_Tree => Project_Tree);
2030 Element : Package_Element;
2032 Default_Switches_Array : Array_Element_Id;
2034 The_Switches : Prj.Variable_Value;
2035 Current : Prj.String_List_Id;
2036 The_String : String_Element;
2039 if Pkg /= No_Package then
2040 Element := Project_Tree.Packages.Table (Pkg);
2042 Default_Switches_Array :=
2044 (Name => Name_Default_Switches,
2045 In_Arrays => Element.Decl.Arrays,
2046 In_Tree => Project_Tree);
2047 The_Switches := Prj.Util.Value_Of
2050 In_Array => Default_Switches_Array,
2051 In_Tree => Project_Tree);
2053 -- If there are switches specified in the package of the
2054 -- project file corresponding to the tool, scan them.
2056 case The_Switches.Kind is
2057 when Prj.Undefined =>
2062 Switch : constant String :=
2063 Get_Name_String (The_Switches.Value);
2065 if Switch'Length > 0 then
2066 Add_To_Carg_Switches (new String'(Switch
));
2071 Current
:= The_Switches
.Values
;
2072 while Current
/= Prj
.Nil_String
loop
2074 Project_Tree
.String_Elements
.Table
(Current
);
2077 Switch
: constant String :=
2078 Get_Name_String
(The_String
.Value
);
2080 if Switch
'Length > 0 then
2081 Add_To_Carg_Switches
(new String'(Switch));
2085 Current := The_String.Next;
2091 -- If -cargs is one of the switches, move the following switches
2092 -- to the Carg_Switches table.
2094 for J in 1 .. First_Switches.Last loop
2095 if First_Switches.Table (J).all = "-cargs" then
2101 -- Move the switches that are before -rules when the
2102 -- command is CHECK.
2105 while K <= First_Switches.Last
2107 (The_Command /= Check
2108 or else First_Switches.Table (K).all /= "-rules")
2110 Add_To_Carg_Switches (First_Switches.Table (K));
2114 if K > First_Switches.Last then
2115 First_Switches.Set_Last (J - 1);
2119 while K <= First_Switches.Last loop
2121 First_Switches.Table (Last) :=
2122 First_Switches.Table (K);
2126 First_Switches.Set_Last (Last);
2134 for J in 1 .. Last_Switches.Last loop
2135 if Last_Switches.Table (J).all = "-cargs" then
2141 -- Move the switches that are before -rules when the
2142 -- command is CHECK.
2145 while K <= Last_Switches.Last
2147 (The_Command /= Check
2149 Last_Switches.Table (K).all /= "-rules")
2151 Add_To_Carg_Switches (Last_Switches.Table (K));
2155 if K > Last_Switches.Last then
2156 Last_Switches.Set_Last (J - 1);
2160 while K <= Last_Switches.Last loop
2162 Last_Switches.Table (Last) :=
2163 Last_Switches.Table (K);
2167 Last_Switches.Set_Last (Last);
2176 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2179 if CP_File /= No_Path then
2180 if The_Command = Elim then
2181 First_Switches.Increment_Last;
2182 First_Switches.Table (First_Switches.Last) :=
2183 new String'("-C" & Get_Name_String
(CP_File
));
2186 Add_To_Carg_Switches
2187 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2193 if The_Command = Link then
2197 if The_Command = Link or The_Command = Bind then
2199 -- For files that are specified as relative paths with directory
2200 -- information, we convert them to absolute paths, with parent
2201 -- being the current working directory if specified on the command
2202 -- line and the project directory if specified in the project
2203 -- file. This is what gnatmake is doing for linker and binder
2206 for J in 1 .. Last_Switches.Last loop
2207 Test_If_Relative_Path
2208 (Last_Switches.Table (J), Current_Work_Dir);
2212 (Project_Tree.Projects.Table (Project).Directory.Name);
2215 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2217 for J in 1 .. First_Switches.Last loop
2218 Test_If_Relative_Path
2219 (First_Switches.Table (J), Project_Dir);
2223 elsif The_Command = Stub then
2225 Data : constant Prj.Project_Data :=
2226 Project_Tree.Projects.Table (Project);
2227 File_Index : Integer := 0;
2228 Dir_Index : Integer := 0;
2229 Last : constant Integer := Last_Switches.Last;
2232 for Index in 1 .. Last loop
2233 if Last_Switches.Table (Index)
2234 (Last_Switches.Table (Index)'First) /= '-'
2236 File_Index := Index;
2241 -- If the naming scheme of the project file is not standard,
2242 -- and if the file name ends with the spec suffix, then
2243 -- indicate to gnatstub the name of the body file with
2246 if Body_Suffix_Id_Of (Project_Tree, "ada", Data.Naming) /=
2247 Prj.Default_Ada_Spec_Suffix
2249 if File_Index /= 0 then
2251 Spec : constant String :=
2252 Base_Name (Last_Switches.Table (File_Index).all);
2253 Last : Natural := Spec'Last;
2258 (Project_Tree, "ada", Data.Naming));
2260 if Spec'Length > Name_Len
2261 and then Spec (Last - Name_Len + 1 .. Last) =
2262 Name_Buffer (1 .. Name_Len)
2264 Last := Last - Name_Len;
2267 (Project_Tree, "ada", Data.Naming));
2268 Last_Switches.Increment_Last;
2269 Last_Switches.Table (Last_Switches.Last) :=
2271 Last_Switches
.Increment_Last
;
2272 Last_Switches
.Table
(Last_Switches
.Last
) :=
2273 new String'(Spec (Spec'First .. Last) &
2274 Name_Buffer (1 .. Name_Len));
2280 -- Add the directory of the spec as the destination directory
2281 -- of the body, if there is no destination directory already
2284 if File_Index /= 0 then
2285 for Index in File_Index + 1 .. Last loop
2286 if Last_Switches.Table (Index)
2287 (Last_Switches.Table (Index)'First) /= '-'
2294 if Dir_Index = 0 then
2295 Last_Switches.Increment_Last;
2296 Last_Switches.Table (Last_Switches.Last) :=
2298 (Dir_Name
(Last_Switches
.Table
(File_Index
).all));
2304 -- For gnatmetric, the generated files should be put in the object
2305 -- directory. This must be the first switch, because it may be
2306 -- overridden by a switch in package Metrics in the project file or
2307 -- by a command line option. Note that we don't add the -d= switch
2308 -- if there is no object directory available.
2310 if The_Command
= Metric
2312 Project_Tree
.Projects
.Table
(Project
).Object_Directory
/=
2315 First_Switches
.Increment_Last
;
2316 First_Switches
.Table
(2 .. First_Switches
.Last
) :=
2317 First_Switches
.Table
(1 .. First_Switches
.Last
- 1);
2318 First_Switches
.Table
(1) :=
2321 (Project_Tree.Projects.Table
2322 (Project).Object_Directory.Name));
2325 -- For gnat check, -rules and the following switches need to be the
2326 -- last options. So, we move all these switches to table
2329 if The_Command = Check then
2332 -- Set to rank of options preceding "-rules"
2334 In_Rules_Switches : Boolean;
2335 -- Set to True when options "-rules" is found
2338 New_Last := First_Switches.Last;
2339 In_Rules_Switches := False;
2341 for J in 1 .. First_Switches.Last loop
2342 if In_Rules_Switches then
2343 Add_To_Rules_Switches (First_Switches.Table (J));
2345 elsif First_Switches.Table (J).all = "-rules" then
2347 In_Rules_Switches := True;
2351 if In_Rules_Switches then
2352 First_Switches.Set_Last (New_Last);
2355 New_Last := Last_Switches.Last;
2356 In_Rules_Switches := False;
2358 for J in 1 .. Last_Switches.Last loop
2359 if In_Rules_Switches then
2360 Add_To_Rules_Switches (Last_Switches.Table (J));
2362 elsif Last_Switches.Table (J).all = "-rules" then
2364 In_Rules_Switches := True;
2368 if In_Rules_Switches then
2369 Last_Switches.Set_Last (New_Last);
2374 -- For gnat check, sync, metric or pretty with -U + a main, get the
2375 -- list of sources from the closure and add them to the arguments.
2377 if ASIS_Main /= null then
2380 -- On VMS, set up the env var again for source dirs file. This is
2381 -- because the call to gnatmake has set this env var to another
2382 -- file that has now been deleted.
2384 if Hostparm.OpenVMS then
2386 -- First make sure that the recorded file names are empty
2390 Prj.Env.Set_Ada_Paths
2391 (Project, Project_Tree, Including_Libraries => False);
2394 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2395 -- and gnat stack, if no file has been put on the command line, call
2396 -- tool with all the sources of the main project.
2398 elsif The_Command = Check or else
2399 The_Command = Sync or else
2400 The_Command = Pretty or else
2401 The_Command = Metric or else
2402 The_Command = List or else
2409 -- Gather all the arguments and invoke the executable
2412 The_Args : Argument_List
2413 (1 .. First_Switches.Last +
2414 Last_Switches.Last +
2415 Carg_Switches.Last +
2416 Rules_Switches.Last);
2417 Arg_Num : Natural := 0;
2420 for J in 1 .. First_Switches.Last loop
2421 Arg_Num := Arg_Num + 1;
2422 The_Args (Arg_Num) := First_Switches.Table (J);
2425 for J in 1 .. Last_Switches.Last loop
2426 Arg_Num := Arg_Num + 1;
2427 The_Args (Arg_Num) := Last_Switches.Table (J);
2430 for J in 1 .. Carg_Switches.Last loop
2431 Arg_Num := Arg_Num + 1;
2432 The_Args (Arg_Num) := Carg_Switches.Table (J);
2435 for J in 1 .. Rules_Switches.Last loop
2436 Arg_Num := Arg_Num + 1;
2437 The_Args (Arg_Num) := Rules_Switches.Table (J);
2440 -- If Display_Command is on, only display the generated command
2442 if Display_Command then
2443 Put (Standard_Error, "generated command -->");
2444 Put (Standard_Error, Exec_Path.all);
2446 for Arg in The_Args'Range loop
2447 Put (Standard_Error, " ");
2448 Put (Standard_Error, The_Args (Arg).all);
2451 Put (Standard_Error, "<--");
2452 New_Line (Standard_Error);
2456 if Verbose_Mode then
2457 Output.Write_Str (Exec_Path.all);
2459 for Arg in The_Args'Range loop
2460 Output.Write_Char (' ');
2461 Output.Write_Str (The_Args (Arg).all);
2468 Exit_Status (Spawn (Exec_Path.all, The_Args));
2475 if not Keep_Temporary_Files then
2476 Prj.Env.Delete_All_Path_Files (Project_Tree);
2477 Delete_Temp_Config_Files;
2480 Set_Exit_Status (Failure);
2483 if not Keep_Temporary_Files then
2484 Prj.Env.Delete_All_Path_Files (Project_Tree);
2485 Delete_Temp_Config_Files;
2488 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2489 -- return an understandable VMS exit status. However the exit status
2490 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2491 -- just a simple success or failure on VMS.
2493 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2494 Set_Exit_Status (Failure);
2496 Set_Exit_Status (My_Exit_Status);