1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2011, 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 Hostparm; use Hostparm;
30 with Makeutl; use Makeutl;
31 with MLib.Tgt; use MLib.Tgt;
34 with Namet; use Namet;
36 with Osint; use Osint;
40 with Prj.Ext; use Prj.Ext;
42 with Prj.Tree; use Prj.Tree;
43 with Prj.Util; use Prj.Util;
45 with Snames; use Snames;
49 with Types; use Types;
50 with VMS_Conv; use VMS_Conv;
51 with VMS_Cmds; use VMS_Cmds;
53 with Ada.Characters.Handling; use Ada.Characters.Handling;
54 with Ada.Command_Line; use Ada.Command_Line;
55 with Ada.Text_IO; use Ada.Text_IO;
57 with GNAT.OS_Lib; use GNAT.OS_Lib;
60 Project_Node_Tree : Project_Node_Tree_Ref;
61 Root_Environment : Prj.Tree.Environment;
62 Project_File : String_Access;
63 Project : Prj.Project_Id;
64 Current_Verbosity : Prj.Verbosity := Prj.Default;
65 Tool_Package_Name : Name_Id := No_Name;
67 B_Start : String_Ptr := new String'("b~");
68 -- Prefix of binder generated file, changed to b__ for VMS
70 Project_Tree : constant Project_Tree_Ref :=
71 new Project_Tree_Data (Is_Root_Tree => True);
74 Old_Project_File_Used : Boolean := False;
75 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
76 -- an old fashioned project file. -p cannot be used in conjunction
79 Temp_File_Name : Path_Name_Type := No_Path;
80 -- The name of the temporary text file to put a list of source/object
81 -- files to pass to a tool.
83 ASIS_Main : String_Access := null;
84 -- Main for commands Check, Metric and Pretty, when -U is used
86 package First_Switches is new Table.Table
87 (Table_Component_Type => String_Access,
88 Table_Index_Type => Integer,
91 Table_Increment => 100,
92 Table_Name => "Gnatcmd.First_Switches");
93 -- A table to keep the switches from the project file
95 package Carg_Switches is new Table.Table
96 (Table_Component_Type => String_Access,
97 Table_Index_Type => Integer,
100 Table_Increment => 100,
101 Table_Name => "Gnatcmd.Carg_Switches");
102 -- A table to keep the switches following -cargs for ASIS tools
104 package Rules_Switches is new Table.Table
105 (Table_Component_Type => String_Access,
106 Table_Index_Type => Integer,
107 Table_Low_Bound => 1,
109 Table_Increment => 100,
110 Table_Name => "Gnatcmd.Rules_Switches");
111 -- A table to keep the switches following -rules for gnatcheck
113 package Library_Paths is new Table.Table (
114 Table_Component_Type => String_Access,
115 Table_Index_Type => Integer,
116 Table_Low_Bound => 1,
118 Table_Increment => 100,
119 Table_Name => "Make.Library_Path");
121 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
122 -- tool. We allocate objects because we cannot declare aliased objects
123 -- as we are in a procedure, not a library level package.
125 subtype SA is String_Access;
127 Naming_String : constant SA := new String'("naming");
128 Binder_String : constant SA := new String'("binder");
129 Builder_String : constant SA := new String'("builder");
130 Compiler_String : constant SA := new String'("compiler");
131 Check_String : constant SA := new String'("check");
132 Synchronize_String : constant SA := new String'("synchronize");
133 Eliminate_String : constant SA := new String'("eliminate");
134 Finder_String : constant SA := new String'("finder");
135 Linker_String : constant SA := new String'("linker");
136 Gnatls_String : constant SA := new String'("gnatls");
137 Pretty_String : constant SA := new String'("pretty_printer");
138 Stack_String : constant SA := new String'("stack");
139 Gnatstub_String : constant SA := new String'("gnatstub");
140 Metric_String : constant SA := new String'("metrics");
141 Xref_String : constant SA := new String'("cross_reference");
143 Packages_To_Check_By_Binder : constant String_List_Access :=
144 new String_List'((Naming_String, Binder_String));
146 Packages_To_Check_By_Check : constant String_List_Access :=
148 ((Naming_String, Builder_String, Check_String, Compiler_String));
150 Packages_To_Check_By_Sync : constant String_List_Access :=
151 new String_List'((Naming_String, Synchronize_String, Compiler_String));
153 Packages_To_Check_By_Eliminate : constant String_List_Access :=
154 new String_List'((Naming_String, Eliminate_String, Compiler_String));
156 Packages_To_Check_By_Finder : constant String_List_Access :=
157 new String_List'((Naming_String, Finder_String));
159 Packages_To_Check_By_Linker : constant String_List_Access :=
160 new String_List'((Naming_String, Linker_String));
162 Packages_To_Check_By_Gnatls : constant String_List_Access :=
163 new String_List'((Naming_String, Gnatls_String));
165 Packages_To_Check_By_Pretty : constant String_List_Access :=
166 new String_List'((Naming_String, Pretty_String, Compiler_String));
168 Packages_To_Check_By_Stack : constant String_List_Access :=
169 new String_List'((Naming_String, Stack_String));
171 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
172 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
174 Packages_To_Check_By_Metric : constant String_List_Access :=
175 new String_List'((Naming_String, Metric_String, Compiler_String));
177 Packages_To_Check_By_Xref : constant String_List_Access :=
178 new String_List'((Naming_String, Xref_String));
180 Packages_To_Check : String_List_Access := Prj.All_Packages;
182 ----------------------------------
183 -- Declarations for GNATCMD use --
184 ----------------------------------
186 The_Command : Command_Type;
187 -- The command specified in the invocation of the GNAT driver
189 Command_Arg : Positive := 1;
190 -- The index of the command in the arguments of the GNAT driver
192 My_Exit_Status : Exit_Status := Success;
193 -- The exit status of the spawned tool. Used to set the correct VMS
196 Current_Work_Dir : constant String := Get_Current_Dir;
197 -- The path of the working directory
199 All_Projects : Boolean := False;
200 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
201 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
202 -- should be invoked for all sources of all projects.
204 -----------------------
205 -- Local Subprograms --
206 -----------------------
208 procedure Add_To_Carg_Switches (Switch : String_Access);
209 -- Add a switch to the Carg_Switches table. If it is the first one, put the
210 -- switch "-cargs" at the beginning of the table.
212 procedure Add_To_Rules_Switches (Switch : String_Access);
213 -- Add a switch to the Rules_Switches table. If it is the first one, put
214 -- the switch "-crules" at the beginning of the table.
216 procedure Check_Files;
217 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
218 -- project file is specified, without any file arguments and without a
219 -- switch -files=. If it is the case, invoke the GNAT tool with the proper
220 -- list of files, derived from the sources of the project.
222 function Check_Project
223 (Project : Project_Id;
224 Root_Project : Project_Id) return Boolean;
225 -- Returns True if Project = Root_Project or if we want to consider all
226 -- sources of all projects. For GNAT METRIC, also returns True if Project
227 -- is extended by Root_Project.
229 procedure Check_Relative_Executable (Name : in out String_Access);
230 -- Check if an executable is specified as a relative path. If it is, and
231 -- the path contains directory information, fail. Otherwise, prepend the
232 -- exec directory. This procedure is only used for GNAT LINK when a project
233 -- file is specified.
235 function Configuration_Pragmas_File return Path_Name_Type;
236 -- Return an argument, if there is a configuration pragmas file to be
237 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
238 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
241 function Mapping_File return Path_Name_Type;
242 -- Create and return the path name of a mapping file. Used for gnatstub
243 -- (GNAT STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric
246 procedure Delete_Temp_Config_Files;
247 -- Delete all temporary config files. The caller is responsible for
248 -- ensuring that Keep_Temporary_Files is False.
250 procedure Get_Closure;
251 -- Get the sources in the closure of the ASIS_Main and add them to the
252 -- list of arguments.
254 procedure Non_VMS_Usage;
255 -- Display usage for platforms other than VMS
257 procedure Process_Link;
258 -- Process GNAT LINK, when there is a project file specified
260 procedure Set_Library_For
261 (Project : Project_Id;
262 Tree : Project_Tree_Ref;
263 Libraries_Present : in out Boolean);
264 -- If Project is a library project, add the correct -L and -l switches to
265 -- the linker invocation.
267 procedure Set_Libraries is
268 new For_Every_Project_Imported (Boolean, Set_Library_For);
269 -- Add the -L and -l switches to the linker for all of the library
272 procedure Test_If_Relative_Path
273 (Switch : in out String_Access;
275 -- Test if Switch is a relative search path switch. If it is and it
276 -- includes directory information, prepend the path with Parent. This
277 -- subprogram is only called when using project files.
279 --------------------------
280 -- Add_To_Carg_Switches --
281 --------------------------
283 procedure Add_To_Carg_Switches (Switch : String_Access) is
285 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
287 if Carg_Switches.Last = 0 then
288 Carg_Switches.Increment_Last;
289 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
292 Carg_Switches.Increment_Last;
293 Carg_Switches.Table (Carg_Switches.Last) := Switch;
294 end Add_To_Carg_Switches;
296 ---------------------------
297 -- Add_To_Rules_Switches --
298 ---------------------------
300 procedure Add_To_Rules_Switches (Switch : String_Access) is
302 -- If the Rules_Switches table is empty, put "-rules" at the beginning
304 if Rules_Switches.Last = 0 then
305 Rules_Switches.Increment_Last;
306 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
309 Rules_Switches.Increment_Last;
310 Rules_Switches.Table (Rules_Switches.Last) := Switch;
311 end Add_To_Rules_Switches;
317 procedure Check_Files is
318 Add_Sources : Boolean := True;
319 Unit : Prj.Unit_Index;
320 Subunit : Boolean := False;
321 FD : File_Descriptor := Invalid_FD;
325 procedure Add_To_Response_File
327 Check_File : Boolean := True);
328 -- Include the file name passed as parameter in the response file for
329 -- the tool being called. If the response file can not be written then
330 -- the file name is passed in the parameter list of the tool. If the
331 -- Check_File parameter is True then the procedure verifies the
332 -- existence of the file before adding it to the response file.
334 --------------------------
335 -- Add_To_Response_File --
336 --------------------------
338 procedure Add_To_Response_File
340 Check_File : Boolean := True)
345 Add_Str_To_Name_Buffer (File_Name);
347 if not Check_File or else
348 Is_Regular_File (Name_Buffer (1 .. Name_Len))
350 if FD /= Invalid_FD then
351 Name_Len := Name_Len + 1;
352 Name_Buffer (Name_Len) := ASCII.LF;
354 Status := Write (FD, Name_Buffer (1)'Address, Name_Len);
356 if Status /= Name_Len then
357 Osint.Fail ("disk full");
360 Last_Switches.Increment_Last;
361 Last_Switches.Table (Last_Switches.Last) :=
362 new String'(File_Name);
365 end Add_To_Response_File;
367 -- Start of processing for Check_Files
370 -- Check if there is at least one argument that is not a switch or if
371 -- there is a -files= switch.
373 for Index in 1 .. Last_Switches.Last loop
374 if Last_Switches.Table (Index).all'Length > 7
375 and then Last_Switches.Table (Index) (1 .. 7) = "-files="
377 Add_Sources := False;
380 elsif Last_Switches.Table (Index) (1) /= '-' then
384 and then Last_Switches.Table (Index - 1).all /= "-o")
386 (The_Command = Pretty
387 and then Last_Switches.Table (Index - 1).all /= "-o"
388 and then Last_Switches.Table (Index - 1).all /= "-of")
390 (The_Command = Metric
392 Last_Switches.Table (Index - 1).all /= "-o" and then
393 Last_Switches.Table (Index - 1).all /= "-og" and then
394 Last_Switches.Table (Index - 1).all /= "-ox" and then
395 Last_Switches.Table (Index - 1).all /= "-d")
397 (The_Command /= Check and then
398 The_Command /= Pretty and then
399 The_Command /= Metric)
401 Add_Sources := False;
407 -- If all arguments are switches and there is no switch -files=, add
408 -- the path names of all the sources of the main project.
412 -- For gnatcheck, gnatpp, and gnatmetric, create a temporary file
413 -- and put the list of sources in it. For gnatstack create a
414 -- temporary file with the list of .ci files.
416 if The_Command = Check or else
417 The_Command = Pretty or else
418 The_Command = Metric or else
421 Tempdir.Create_Temp_File (FD, Temp_File_Name);
422 Last_Switches.Increment_Last;
423 Last_Switches.Table (Last_Switches.Last) :=
424 new String'("-files=" & Get_Name_String (Temp_File_Name));
431 -- Gnatstack needs to add the .ci file for the binder generated
432 -- files corresponding to all of the library projects and main
433 -- units belonging to the application.
435 if The_Command = Stack then
436 Proj := Project_Tree.Projects;
437 while Proj /= null loop
438 if Check_Project (Proj.Project, Project) then
440 Main : String_List_Id;
443 -- Include binder generated files for main programs
445 Main := Proj.Project.Mains;
446 while Main /= Nil_String loop
449 (Proj.Project.Object_Directory.Name) &
453 (Project_Tree.Shared.String_Elements.Table
457 -- When looking for the .ci file for a binder
458 -- generated file, look for both b~xxx and b__xxx
459 -- as gprbuild always uses b__ as the prefix of
462 if not Is_Regular_File (Name_Buffer (1 .. Name_Len))
463 and then B_Start.all /= "b__"
467 (Proj.Project.Object_Directory.Name) &
472 .String_Elements.Table (Main).Value),
476 Main := Project_Tree.Shared.String_Elements.Table
480 if Proj.Project.Library then
482 -- Include the .ci file for the binder generated
483 -- files that contains the initialization and
484 -- finalization of the library.
488 (Proj.Project.Object_Directory.Name) &
490 Get_Name_String (Proj.Project.Library_Name) &
493 -- When looking for the .ci file for a binder
494 -- generated file, look for both b~xxx and b__xxx
495 -- as gprbuild always uses b__ as the prefix of
498 if not Is_Regular_File (Name_Buffer (1 .. Name_Len))
499 and then B_Start.all /= "b__"
503 (Proj.Project.Object_Directory.Name) &
505 Get_Name_String (Proj.Project.Library_Name) &
516 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
517 while Unit /= No_Unit_Index loop
519 -- For gnatls, we only need to put the library units, body or
520 -- spec, but not the subunits.
522 if The_Command = List then
523 if Unit.File_Names (Impl) /= null
524 and then not Unit.File_Names (Impl).Locally_Removed
526 -- There is a body, check if it is for this project
529 or else Unit.File_Names (Impl).Project = Project
533 if Unit.File_Names (Spec) = null
534 or else Unit.File_Names (Spec).Locally_Removed
536 -- We have a body with no spec: we need to check if
537 -- this is a subunit, because gnatls will complain
541 Src_Ind : constant Source_File_Index :=
542 Sinput.P.Load_Project_File
548 Sinput.P.Source_File_Is_Subunit (Src_Ind);
553 Last_Switches.Increment_Last;
554 Last_Switches.Table (Last_Switches.Last) :=
558 (Impl).Display_File));
562 elsif Unit.File_Names (Spec) /= null
563 and then not Unit.File_Names (Spec).Locally_Removed
565 -- We have a spec with no body. Check if it is for this
568 if All_Projects or else
569 Unit.File_Names (Spec).Project = Project
571 Last_Switches.Increment_Last;
572 Last_Switches.Table (Last_Switches.Last) :=
573 new String'(Get_Name_String
574 (Unit.File_Names (Spec).Display_File));
578 -- For gnatstack, we put the .ci files corresponding to the
579 -- different units, including the binder generated files. We
580 -- only need to do that for the library units, body or spec,
581 -- but not the subunits.
583 elsif The_Command = Stack then
584 if Unit.File_Names (Impl) /= null
585 and then not Unit.File_Names (Impl).Locally_Removed
587 -- There is a body. Check if .ci files for this project
591 (Unit.File_Names (Impl).Project, Project)
595 if Unit.File_Names (Spec) = null
596 or else Unit.File_Names (Spec).Locally_Removed
598 -- We have a body with no spec: we need to check
599 -- if this is a subunit, because .ci files are not
600 -- generated for subunits.
603 Src_Ind : constant Source_File_Index :=
604 Sinput.P.Load_Project_File
610 Sinput.P.Source_File_Is_Subunit (Src_Ind);
618 (Impl).Project. Object_Directory.Name) &
621 (Unit.File_Names (Impl).Display_File),
626 elsif Unit.File_Names (Spec) /= null
627 and then not Unit.File_Names (Spec).Locally_Removed
629 -- Spec with no body, check if it is for this project
632 (Unit.File_Names (Spec).Project, Project)
637 (Spec).Project. Object_Directory.Name) &
640 (Get_Name_String (Unit.File_Names (Spec).File),
646 -- For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
647 -- sources of the project, or of all projects if -U was
650 for Kind in Spec_Or_Body loop
651 if Unit.File_Names (Kind) /= null
652 and then Check_Project
653 (Unit.File_Names (Kind).Project, Project)
654 and then not Unit.File_Names (Kind).Locally_Removed
659 (Unit.File_Names (Kind).Path.Display_Name) &
661 Check_File => False);
666 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
670 if FD /= Invalid_FD then
674 Osint.Fail ("disk full");
684 function Check_Project
685 (Project : Project_Id;
686 Root_Project : Project_Id) return Boolean
691 if Project = No_Project then
694 elsif All_Projects or else Project = Root_Project then
697 elsif The_Command = Metric then
698 Proj := Root_Project;
699 while Proj.Extends /= No_Project loop
700 if Project = Proj.Extends then
704 Proj := Proj.Extends;
711 -------------------------------
712 -- Check_Relative_Executable --
713 -------------------------------
715 procedure Check_Relative_Executable (Name : in out String_Access) is
716 Exec_File_Name : constant String := Name.all;
719 if not Is_Absolute_Path (Exec_File_Name) then
720 for Index in Exec_File_Name'Range loop
721 if Exec_File_Name (Index) = Directory_Separator then
722 Fail ("relative executable (""" &
724 """) with directory part not allowed " &
725 "when using project files");
729 Get_Name_String (Project.Exec_Directory.Name);
731 if Name_Buffer (Name_Len) /= Directory_Separator then
732 Name_Len := Name_Len + 1;
733 Name_Buffer (Name_Len) := Directory_Separator;
736 Name_Buffer (Name_Len + 1 ..
737 Name_Len + Exec_File_Name'Length) :=
739 Name_Len := Name_Len + Exec_File_Name'Length;
740 Name := new String'(Name_Buffer (1 .. Name_Len));
742 end Check_Relative_Executable;
744 --------------------------------
745 -- Configuration_Pragmas_File --
746 --------------------------------
748 function Configuration_Pragmas_File return Path_Name_Type is
750 Prj.Env.Create_Config_Pragmas_File (Project, Project_Tree);
751 return Project.Config_File_Name;
752 end Configuration_Pragmas_File;
754 ------------------------------
755 -- Delete_Temp_Config_Files --
756 ------------------------------
758 procedure Delete_Temp_Config_Files is
761 pragma Warnings (Off, Success);
764 -- This should only be called if Keep_Temporary_Files is False
766 pragma Assert (not Keep_Temporary_Files);
768 if Project /= No_Project then
769 Proj := Project_Tree.Projects;
770 while Proj /= null loop
771 if Proj.Project.Config_File_Temp then
772 Delete_Temporary_File
773 (Project_Tree.Shared, Proj.Project.Config_File_Name);
780 -- If a temporary text file that contains a list of files for a tool
781 -- has been created, delete this temporary file.
783 if Temp_File_Name /= No_Path then
784 Delete_Temporary_File (Project_Tree.Shared, Temp_File_Name);
786 end Delete_Temp_Config_Files;
792 procedure Get_Closure is
793 Args : constant Argument_List :=
794 (1 => new String'("-q"),
795 2 => new String'("-b"),
796 3 => new String'("-P"),
799 6 => new String'("-bargs"),
800 7 => new String'("-R"),
801 8 => new String'("-Z"));
802 -- Arguments for the invocation of gnatmake which are added to the
803 -- Last_Arguments list by this procedure.
805 FD : File_Descriptor;
806 -- File descriptor for the temp file that will get the output of the
807 -- invocation of gnatmake.
809 Name : Path_Name_Type;
810 -- Path of the file FD
812 GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
815 GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
818 Return_Code : Integer;
821 pragma Warnings (Off, Unused);
823 File : Ada.Text_IO.File_Type;
824 Line : String (1 .. 250);
826 -- Used to read file if there is an error, it is good enough to display
827 -- just 250 characters if the first line of the file is very long.
830 Path : Path_Name_Type;
833 if GN_Path = null then
834 Put_Line (Standard_Error, "could not locate " & GN_Name);
838 -- Create the temp file
840 Tempdir.Create_Temp_File (FD, Name);
842 -- And close it, because on VMS Spawn with a file descriptor created
843 -- with Create_Temp_File does not redirect output.
847 -- Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
850 (Program_Name => GN_Path.all,
852 Output_File => Get_Name_String (Name),
854 Return_Code => Return_Code,
857 -- Read the output of the invocation of gnatmake
859 Open (File, In_File, Get_Name_String (Name));
861 -- If it was unsuccessful, display the first line in the file and exit
864 if Return_Code /= 0 then
865 Get_Line (File, Line, Last);
867 if not Keep_Temporary_Files then
873 Put_Line (Standard_Error, Line (1 .. Last));
875 (Standard_Error, "could not get closure of " & ASIS_Main.all);
879 -- Get each file name in the file, find its path and add it the
880 -- list of arguments.
882 while not End_Of_File (File) loop
883 Get_Line (File, Line, Last);
886 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
887 while Unit /= No_Unit_Index loop
888 if Unit.File_Names (Spec) /= null
890 Get_Name_String (Unit.File_Names (Spec).File) =
893 Path := Unit.File_Names (Spec).Path.Name;
896 elsif Unit.File_Names (Impl) /= null
898 Get_Name_String (Unit.File_Names (Impl).File) =
901 Path := Unit.File_Names (Impl).Path.Name;
905 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
908 Last_Switches.Increment_Last;
910 if Path /= No_Path then
911 Last_Switches.Table (Last_Switches.Last) :=
912 new String'(Get_Name_String (Path));
915 Last_Switches.Table (Last_Switches.Last) :=
916 new String'(Line (1 .. Last));
920 if not Keep_Temporary_Files then
932 function Mapping_File return Path_Name_Type is
933 Result : Path_Name_Type;
935 Prj.Env.Create_Mapping_File
937 Language => Name_Ada,
938 In_Tree => Project_Tree,
947 procedure Process_Link is
948 Look_For_Executable : Boolean := True;
949 Libraries_Present : Boolean := False;
950 Path_Option : constant String_Access :=
951 MLib.Linker_Library_Path_Option;
952 Prj : Project_Id := Project;
955 Skip_Executable : Boolean := False;
958 -- Add the default search directories, to be able to find
959 -- libgnat in call to MLib.Utl.Lib_Directory.
961 Add_Default_Search_Dirs;
963 Library_Paths.Set_Last (0);
965 -- Check if there are library project files
967 if MLib.Tgt.Support_For_Libraries /= None then
968 Set_Libraries (Project, Project_Tree, Libraries_Present);
971 -- If there are, add the necessary additional switches
973 if Libraries_Present then
975 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
977 Last_Switches.Increment_Last;
978 Last_Switches.Table (Last_Switches.Last) :=
979 new String'("-L" & MLib.Utl.Lib_Directory);
980 Last_Switches.Increment_Last;
981 Last_Switches.Table (Last_Switches.Last) :=
982 new String'("-lgnarl");
983 Last_Switches.Increment_Last;
984 Last_Switches.Table (Last_Switches.Last) :=
985 new String'("-lgnat");
987 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
988 -- equivalent) with all the library dirs plus the standard GNAT
991 if Path_Option /= null then
993 Option : String_Access;
994 Length : Natural := Path_Option'Length;
998 if MLib.Separate_Run_Path_Options then
1000 -- We are going to create one switch of the form
1001 -- "-Wl,-rpath,dir_N" for each directory to consider.
1003 -- One switch for each library directory
1006 Library_Paths.First .. Library_Paths.Last
1008 Last_Switches.Increment_Last;
1010 (Last_Switches.Last) := new String'
1012 Last_Switches.Table (Index).all);
1015 -- One switch for the standard GNAT library dir
1017 Last_Switches.Increment_Last;
1019 (Last_Switches.Last) := new String'
1020 (Path_Option.all & MLib.Utl.Lib_Directory);
1023 -- First, compute the exact length for the switch
1026 Library_Paths.First .. Library_Paths.Last
1028 -- Add the length of the library dir plus one for the
1029 -- directory separator.
1033 Library_Paths.Table (Index)'Length + 1;
1036 -- Finally, add the length of the standard GNAT library dir
1038 Length := Length + MLib.Utl.Lib_Directory'Length;
1039 Option := new String (1 .. Length);
1040 Option (1 .. Path_Option'Length) := Path_Option.all;
1041 Current := Path_Option'Length;
1043 -- Put each library dir followed by a dir separator
1046 Library_Paths.First .. Library_Paths.Last
1051 Library_Paths.Table (Index)'Length) :=
1052 Library_Paths.Table (Index).all;
1055 Library_Paths.Table (Index)'Length + 1;
1056 Option (Current) := Path_Separator;
1059 -- Finally put the standard GNAT library dir
1063 Current + MLib.Utl.Lib_Directory'Length) :=
1064 MLib.Utl.Lib_Directory;
1066 -- And add the switch to the last switches
1068 Last_Switches.Increment_Last;
1069 Last_Switches.Table (Last_Switches.Last) :=
1076 -- Check if the first ALI file specified can be found, either in the
1077 -- object directory of the main project or in an object directory of a
1078 -- project file extended by the main project. If the ALI file can be
1079 -- found, replace its name with its absolute path.
1081 Skip_Executable := False;
1083 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1085 -- If we have an executable just reset the flag
1087 if Skip_Executable then
1088 Skip_Executable := False;
1090 -- If -o, set flag so that next switch is not processed
1092 elsif Last_Switches.Table (J).all = "-o" then
1093 Skip_Executable := True;
1099 Switch : constant String :=
1100 Last_Switches.Table (J).all;
1101 ALI_File : constant String (1 .. Switch'Length + 4) :=
1104 Test_Existence : Boolean := False;
1107 Last := Switch'Length;
1109 -- Skip real switches
1111 if Switch'Length /= 0
1112 and then Switch (Switch'First) /= '-'
1114 -- Append ".ali" if file name does not end with it
1116 if Switch'Length <= 4
1117 or else Switch (Switch'Last - 3 .. Switch'Last) /= ".ali"
1119 Last := ALI_File'Last;
1122 -- If file name includes directory information, stop if ALI
1125 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1126 Test_Existence := True;
1129 for K in Switch'Range loop
1131 or else Switch (K) = Directory_Separator
1133 Test_Existence := True;
1139 if Test_Existence then
1140 if Is_Regular_File (ALI_File (1 .. Last)) then
1144 -- Look in object directories if ALI file exists
1149 Dir : constant String :=
1150 Get_Name_String (Prj.Object_Directory.Name);
1154 ALI_File (1 .. Last))
1156 -- We have found the correct project, so we
1157 -- replace the file with the absolute path.
1159 Last_Switches.Table (J) :=
1160 new String'(Dir & ALI_File (1 .. Last));
1168 -- Go to the project being extended, if any
1171 exit Project_Loop when Prj = No_Project;
1172 end loop Project_Loop;
1177 end loop Switch_Loop;
1179 -- If a relative path output file has been specified, we add the exec
1182 for J in reverse 1 .. Last_Switches.Last - 1 loop
1183 if Last_Switches.Table (J).all = "-o" then
1184 Check_Relative_Executable
1185 (Name => Last_Switches.Table (J + 1));
1186 Look_For_Executable := False;
1191 if Look_For_Executable then
1192 for J in reverse 1 .. First_Switches.Last - 1 loop
1193 if First_Switches.Table (J).all = "-o" then
1194 Look_For_Executable := False;
1195 Check_Relative_Executable
1196 (Name => First_Switches.Table (J + 1));
1202 -- If no executable is specified, then find the name of the first ALI
1203 -- file on the command line and issue a -o switch with the absolute path
1204 -- of the executable in the exec directory.
1206 if Look_For_Executable then
1207 for J in 1 .. Last_Switches.Last loop
1208 Arg := Last_Switches.Table (J);
1211 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1213 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1215 Last := Arg'Last - 4;
1217 elsif Is_Regular_File (Arg.all & ".ali") then
1222 Last_Switches.Increment_Last;
1223 Last_Switches.Table (Last_Switches.Last) :=
1225 Get_Name_String (Project.Exec_Directory.Name);
1226 Last_Switches.Increment_Last;
1227 Last_Switches.Table (Last_Switches.Last) :=
1228 new String'(Name_Buffer (1 .. Name_Len) &
1230 (Base_Name (Arg (Arg'First .. Last))));
1238 ---------------------
1239 -- Set_Library_For --
1240 ---------------------
1242 procedure Set_Library_For
1243 (Project : Project_Id;
1244 Tree : Project_Tree_Ref;
1245 Libraries_Present : in out Boolean)
1247 pragma Unreferenced (Tree);
1249 Path_Option : constant String_Access :=
1250 MLib.Linker_Library_Path_Option;
1253 -- Case of library project
1255 if Project.Library then
1256 Libraries_Present := True;
1258 -- Add the -L switch
1260 Last_Switches.Increment_Last;
1261 Last_Switches.Table (Last_Switches.Last) :=
1262 new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1264 -- Add the -l switch
1266 Last_Switches.Increment_Last;
1267 Last_Switches.Table (Last_Switches.Last) :=
1268 new String'("-l" & Get_Name_String (Project.Library_Name));
1270 -- Add the directory to table Library_Paths, to be processed later
1271 -- if library is not static and if Path_Option is not null.
1273 if Project.Library_Kind /= Static
1274 and then Path_Option /= null
1276 Library_Paths.Increment_Last;
1277 Library_Paths.Table (Library_Paths.Last) :=
1278 new String'(Get_Name_String (Project.Library_Dir.Name));
1281 end Set_Library_For;
1283 ---------------------------
1284 -- Test_If_Relative_Path --
1285 ---------------------------
1287 procedure Test_If_Relative_Path
1288 (Switch : in out String_Access;
1292 Makeutl.Test_If_Relative_Path
1294 Do_Fail => Osint.Fail'Access,
1295 Including_Non_Switch => False,
1296 Including_RTS => True);
1297 end Test_If_Relative_Path;
1303 procedure Non_VMS_Usage is
1307 Put_Line ("List of available commands");
1310 for C in Command_List'Range loop
1312 -- No usage for VMS only command or for Sync
1314 if not Command_List (C).VMS_Only and then C /= Sync then
1315 if Targparm.AAMP_On_Target then
1321 Put (To_Lower (Command_List (C).Cname.all));
1324 -- Never call gnatstack with a prefix
1327 Put (Command_List (C).Unixcmd.all);
1329 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1333 Sws : Argument_List_Access renames Command_List (C).Unixsws;
1336 for J in Sws'Range loop
1348 Put_Line ("All commands except chop, krunch and preprocess " &
1349 "accept project file switches -vPx, -Pprj and -Xnam=val");
1353 -- Start of processing for GNATCmd
1361 Prj.Tree.Initialize (Root_Environment, Gnatmake_Flags);
1362 Prj.Env.Initialize_Default_Project_Path
1363 (Root_Environment.Project_Path, Target_Name => "");
1365 Project_Node_Tree := new Project_Node_Tree_Data;
1366 Prj.Tree.Initialize (Project_Node_Tree);
1368 Prj.Initialize (Project_Tree);
1371 Last_Switches.Set_Last (0);
1373 First_Switches.Init;
1374 First_Switches.Set_Last (0);
1376 Carg_Switches.Set_Last (0);
1377 Rules_Switches.Init;
1378 Rules_Switches.Set_Last (0);
1380 VMS_Conv.Initialize;
1382 -- Add the default search directories, to be able to find system.ads in the
1383 -- subsequent call to Targparm.Get_Target_Parameters.
1385 Add_Default_Search_Dirs;
1387 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1388 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1390 Targparm.Get_Target_Parameters;
1392 -- Put the command line in environment variable GNAT_DRIVER_COMMAND_LINE,
1393 -- so that the spawned tool may know the way the GNAT driver was invoked.
1396 Add_Str_To_Name_Buffer (Command_Name);
1398 for J in 1 .. Argument_Count loop
1399 Add_Char_To_Name_Buffer (' ');
1400 Add_Str_To_Name_Buffer (Argument (J));
1403 Setenv ("GNAT_DRIVER_COMMAND_LINE", Name_Buffer (1 .. Name_Len));
1405 -- Add the directory where the GNAT driver is invoked in front of the path,
1406 -- if the GNAT driver is invoked with directory information. Do not do this
1407 -- for VMS, where the notion of path does not really exist.
1411 Command : constant String := Command_Name;
1414 for Index in reverse Command'Range loop
1415 if Command (Index) = Directory_Separator then
1417 Absolute_Dir : constant String :=
1419 (Command (Command'First .. Index));
1421 PATH : constant String :=
1422 Absolute_Dir & Path_Separator & Getenv ("PATH").all;
1425 Setenv ("PATH", PATH);
1434 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1435 -- filenames and pathnames to Unix style.
1438 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1440 VMS_Conversion (The_Command);
1442 B_Start := new String'("b__");
1444 -- If not on VMS, scan the command line directly
1447 if Argument_Count = 0 then
1453 if Argument_Count > Command_Arg
1454 and then Argument (Command_Arg) = "-v"
1456 Verbose_Mode := True;
1457 Command_Arg := Command_Arg + 1;
1459 elsif Argument_Count > Command_Arg
1460 and then Argument (Command_Arg) = "-dn"
1462 Keep_Temporary_Files := True;
1463 Command_Arg := Command_Arg + 1;
1470 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1472 if Command_List (The_Command).VMS_Only then
1476 & Command_List (The_Command).Cname.all
1477 & """ can only be used on VMS");
1481 when Constraint_Error =>
1483 -- Check if it is an alternate command
1486 Alternate : Alternate_Command;
1489 Alternate := Alternate_Command'Value
1490 (Argument (Command_Arg));
1491 The_Command := Corresponding_To (Alternate);
1494 when Constraint_Error =>
1496 Fail ("Unknown command: " & Argument (Command_Arg));
1500 -- Get the arguments from the command line and from the eventual
1501 -- argument file(s) specified on the command line.
1503 for Arg in Command_Arg + 1 .. Argument_Count loop
1505 The_Arg : constant String := Argument (Arg);
1508 -- Check if an argument file is specified
1510 if The_Arg (The_Arg'First) = '@' then
1512 Arg_File : Ada.Text_IO.File_Type;
1513 Line : String (1 .. 256);
1517 -- Open the file and fail if the file cannot be found
1522 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1527 (Standard_Error, "Cannot open argument file """);
1530 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1532 Put_Line (Standard_Error, """");
1536 -- Read line by line and put the content of each non-
1537 -- empty line in the Last_Switches table.
1539 while not End_Of_File (Arg_File) loop
1540 Get_Line (Arg_File, Line, Last);
1543 Last_Switches.Increment_Last;
1544 Last_Switches.Table (Last_Switches.Last) :=
1545 new String'(Line (1 .. Last));
1553 -- It is not an argument file; just put the argument in
1554 -- the Last_Switches table.
1556 Last_Switches.Increment_Last;
1557 Last_Switches.Table (Last_Switches.Last) :=
1558 new String'(The_Arg);
1566 Program : String_Access;
1567 Exec_Path : String_Access;
1570 if The_Command = Stack then
1572 -- Never call gnatstack with a prefix
1574 Program := new String'(Command_List (The_Command).Unixcmd.all);
1578 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1581 -- For the tools where the GNAT driver processes the project files,
1582 -- allow shared library projects to import projects that are not shared
1583 -- library projects, to avoid adding a switch for these tools. For the
1584 -- builder (gnatmake), if a shared library project imports a project
1585 -- that is not a shared library project and the appropriate switch is
1586 -- not specified, the invocation of gnatmake will fail.
1588 Opt.Unchecked_Shared_Lib_Imports := True;
1590 -- Locate the executable for the command
1592 Exec_Path := Locate_Exec_On_Path (Program.all);
1594 if Exec_Path = null then
1595 Put_Line (Standard_Error, "could not locate " & Program.all);
1599 -- If there are switches for the executable, put them as first switches
1601 if Command_List (The_Command).Unixsws /= null then
1602 for J in Command_List (The_Command).Unixsws'Range loop
1603 First_Switches.Increment_Last;
1604 First_Switches.Table (First_Switches.Last) :=
1605 Command_List (The_Command).Unixsws (J);
1609 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, METRIC, PRETTY, STACK, STUB,
1610 -- SYNC and XREF, look for project file related switches.
1614 Tool_Package_Name := Name_Binder;
1615 Packages_To_Check := Packages_To_Check_By_Binder;
1617 Tool_Package_Name := Name_Check;
1618 Packages_To_Check := Packages_To_Check_By_Check;
1620 Tool_Package_Name := Name_Eliminate;
1621 Packages_To_Check := Packages_To_Check_By_Eliminate;
1623 Tool_Package_Name := Name_Finder;
1624 Packages_To_Check := Packages_To_Check_By_Finder;
1626 Tool_Package_Name := Name_Linker;
1627 Packages_To_Check := Packages_To_Check_By_Linker;
1629 Tool_Package_Name := Name_Gnatls;
1630 Packages_To_Check := Packages_To_Check_By_Gnatls;
1632 Tool_Package_Name := Name_Metrics;
1633 Packages_To_Check := Packages_To_Check_By_Metric;
1635 Tool_Package_Name := Name_Pretty_Printer;
1636 Packages_To_Check := Packages_To_Check_By_Pretty;
1638 Tool_Package_Name := Name_Stack;
1639 Packages_To_Check := Packages_To_Check_By_Stack;
1641 Tool_Package_Name := Name_Gnatstub;
1642 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1644 Tool_Package_Name := Name_Synchronize;
1645 Packages_To_Check := Packages_To_Check_By_Sync;
1647 Tool_Package_Name := Name_Cross_Reference;
1648 Packages_To_Check := Packages_To_Check_By_Xref;
1650 Tool_Package_Name := No_Name;
1653 if Tool_Package_Name /= No_Name then
1655 -- Check that the switches are consistent. Detect project file
1656 -- related switches.
1658 Inspect_Switches : declare
1659 Arg_Num : Positive := 1;
1660 Argv : String_Access;
1662 procedure Remove_Switch (Num : Positive);
1663 -- Remove a project related switch from table Last_Switches
1669 procedure Remove_Switch (Num : Positive) is
1671 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1672 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1673 Last_Switches.Decrement_Last;
1676 -- Start of processing for Inspect_Switches
1679 while Arg_Num <= Last_Switches.Last loop
1680 Argv := Last_Switches.Table (Arg_Num);
1682 if Argv (Argv'First) = '-' then
1683 if Argv'Length = 1 then
1685 ("switch character cannot be followed by a blank");
1688 -- The two style project files (-p and -P) cannot be used
1691 if (The_Command = Find or else The_Command = Xref)
1692 and then Argv (2) = 'p'
1694 Old_Project_File_Used := True;
1695 if Project_File /= null then
1696 Fail ("-P and -p cannot be used together");
1700 -- --subdirs=... Specify Subdirs
1702 if Argv'Length > Makeutl.Subdirs_Option'Length
1706 Argv'First + Makeutl.Subdirs_Option'Length - 1) =
1707 Makeutl.Subdirs_Option
1712 (Argv'First + Makeutl.Subdirs_Option'Length ..
1715 Remove_Switch (Arg_Num);
1717 -- -aPdir Add dir to the project search path
1719 elsif Argv'Length > 3
1720 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1722 Prj.Env.Add_Directories
1723 (Root_Environment.Project_Path,
1724 Argv (Argv'First + 3 .. Argv'Last));
1726 Remove_Switch (Arg_Num);
1728 -- -eL Follow links for files
1730 elsif Argv.all = "-eL" then
1731 Follow_Links_For_Files := True;
1732 Follow_Links_For_Dirs := True;
1734 Remove_Switch (Arg_Num);
1736 -- -vPx Specify verbosity while parsing project files
1738 elsif Argv'Length = 4
1739 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1741 case Argv (Argv'Last) is
1743 Current_Verbosity := Prj.Default;
1745 Current_Verbosity := Prj.Medium;
1747 Current_Verbosity := Prj.High;
1749 Fail ("Invalid switch: " & Argv.all);
1752 Remove_Switch (Arg_Num);
1754 -- -Pproject_file Specify project file to be used
1756 elsif Argv (Argv'First + 1) = 'P' then
1758 -- Only one -P switch can be used
1760 if Project_File /= null then
1763 & ": second project file forbidden (first is """
1767 -- The two style project files (-p and -P) cannot be
1770 elsif Old_Project_File_Used then
1771 Fail ("-p and -P cannot be used together");
1773 elsif Argv'Length = 2 then
1775 -- There is space between -P and the project file
1776 -- name. -P cannot be the last option.
1778 if Arg_Num = Last_Switches.Last then
1779 Fail ("project file name missing after -P");
1782 Remove_Switch (Arg_Num);
1783 Argv := Last_Switches.Table (Arg_Num);
1785 -- After -P, there must be a project file name,
1786 -- not another switch.
1788 if Argv (Argv'First) = '-' then
1789 Fail ("project file name missing after -P");
1792 Project_File := new String'(Argv.all);
1797 -- No space between -P and project file name
1800 new String'(Argv (Argv'First + 2 .. Argv'Last));
1803 Remove_Switch (Arg_Num);
1805 -- -Xexternal=value Specify an external reference to be
1806 -- used in project files
1808 elsif Argv'Length >= 5
1809 and then Argv (Argv'First + 1) = 'X'
1811 if not Check (Root_Environment.External,
1812 Argv (Argv'First + 2 .. Argv'Last))
1815 & " is not a valid external assignment.");
1818 Remove_Switch (Arg_Num);
1821 (The_Command = Check or else
1822 The_Command = Sync or else
1823 The_Command = Pretty or else
1824 The_Command = Metric or else
1825 The_Command = Stack or else
1827 and then Argv'Length = 2
1828 and then Argv (2) = 'U'
1830 All_Projects := True;
1831 Remove_Switch (Arg_Num);
1834 Arg_Num := Arg_Num + 1;
1837 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1838 or else The_Command = Sync
1839 or else The_Command = Metric
1840 or else The_Command = Pretty)
1841 and then Project_File /= null
1842 and then All_Projects
1844 if ASIS_Main /= null then
1845 Fail ("cannot specify more than one main after -U");
1848 Remove_Switch (Arg_Num);
1852 Arg_Num := Arg_Num + 1;
1855 end Inspect_Switches;
1858 -- If there is a project file specified, parse it, get the switches
1859 -- for the tool and setup PATH environment variables.
1861 if Project_File /= null then
1862 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1865 (Project => Project,
1866 In_Tree => Project_Tree,
1867 In_Node_Tree => Project_Node_Tree,
1868 Project_File_Name => Project_File.all,
1869 Env => Root_Environment,
1870 Packages_To_Check => Packages_To_Check);
1872 if Project = Prj.No_Project then
1873 Fail ("""" & Project_File.all & """ processing failed");
1876 -- Check if a package with the name of the tool is in the project
1877 -- file and if there is one, get the switches, if any, and scan them.
1880 Pkg : constant Prj.Package_Id :=
1882 (Name => Tool_Package_Name,
1883 In_Packages => Project.Decl.Packages,
1884 Shared => Project_Tree.Shared);
1886 Element : Package_Element;
1888 Switches_Array : Array_Element_Id;
1890 The_Switches : Prj.Variable_Value;
1891 Current : Prj.String_List_Id;
1892 The_String : String_Element;
1894 Main : String_Access := null;
1897 if Pkg /= No_Package then
1898 Element := Project_Tree.Shared.Packages.Table (Pkg);
1900 -- Packages Gnatls and Gnatstack have a single attribute
1901 -- Switches, that is not an associative array.
1903 if The_Command = List or else The_Command = Stack then
1906 (Variable_Name => Snames.Name_Switches,
1907 In_Variables => Element.Decl.Attributes,
1908 Shared => Project_Tree.Shared);
1910 -- Packages Binder (for gnatbind), Cross_Reference (for
1911 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1912 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1913 -- (for gnatcheck), and Metric (for gnatmetric) have an
1914 -- attributed Switches, an associative array, indexed by the
1915 -- name of the file.
1917 -- They also have an attribute Default_Switches, indexed by the
1918 -- name of the programming language.
1921 -- First check if there is a single main
1923 for J in 1 .. Last_Switches.Last loop
1924 if Last_Switches.Table (J) (1) /= '-' then
1926 Main := Last_Switches.Table (J);
1935 if Main /= null then
1938 (Name => Name_Switches,
1939 In_Arrays => Element.Decl.Arrays,
1940 Shared => Project_Tree.Shared);
1942 Add_Str_To_Name_Buffer (Main.all);
1943 The_Switches := Prj.Util.Value_Of
1944 (Index => Name_Find,
1946 In_Array => Switches_Array,
1947 Shared => Project_Tree.Shared);
1950 if The_Switches.Kind = Prj.Undefined then
1953 (Name => Name_Default_Switches,
1954 In_Arrays => Element.Decl.Arrays,
1955 Shared => Project_Tree.Shared);
1956 The_Switches := Prj.Util.Value_Of
1959 In_Array => Switches_Array,
1960 Shared => Project_Tree.Shared);
1964 -- If there are switches specified in the package of the
1965 -- project file corresponding to the tool, scan them.
1967 case The_Switches.Kind is
1968 when Prj.Undefined =>
1973 Switch : constant String :=
1974 Get_Name_String (The_Switches.Value);
1977 if Switch'Length > 0 then
1978 First_Switches.Increment_Last;
1979 First_Switches.Table (First_Switches.Last) :=
1980 new String'(Switch);
1985 Current := The_Switches.Values;
1986 while Current /= Prj.Nil_String loop
1987 The_String := Project_Tree.Shared.String_Elements.
1991 Switch : constant String :=
1992 Get_Name_String (The_String.Value);
1995 if Switch'Length > 0 then
1996 First_Switches.Increment_Last;
1997 First_Switches.Table (First_Switches.Last) :=
1998 new String'(Switch);
2002 Current := The_String.Next;
2008 if The_Command = Bind
2009 or else The_Command = Link
2010 or else The_Command = Elim
2012 Change_Dir (Get_Name_String (Project.Object_Directory.Name));
2015 -- Set up the env vars for project path files
2017 Prj.Env.Set_Ada_Paths
2018 (Project, Project_Tree, Including_Libraries => False);
2020 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
2021 -- a configuration pragmas file, if necessary.
2023 if The_Command = Pretty
2024 or else The_Command = Metric
2025 or else The_Command = Stub
2026 or else The_Command = Elim
2027 or else The_Command = Check
2028 or else The_Command = Sync
2030 -- If there are switches in package Compiler, put them in the
2031 -- Carg_Switches table.
2034 Pkg : constant Prj.Package_Id :=
2036 (Name => Name_Compiler,
2037 In_Packages => Project.Decl.Packages,
2038 Shared => Project_Tree.Shared);
2040 Element : Package_Element;
2042 Switches_Array : Array_Element_Id;
2044 The_Switches : Prj.Variable_Value;
2045 Current : Prj.String_List_Id;
2046 The_String : String_Element;
2048 Main : String_Access := null;
2052 if Pkg /= No_Package then
2054 -- First, check if there is a single main specified.
2056 for J in 1 .. Last_Switches.Last loop
2057 if Last_Switches.Table (J) (1) /= '-' then
2059 Main := Last_Switches.Table (J);
2068 Element := Project_Tree.Shared.Packages.Table (Pkg);
2070 -- If there is a single main and there is compilation
2071 -- switches specified in the project file, use them.
2073 if Main /= null and then not All_Projects then
2074 Name_Len := Main'Length;
2075 Name_Buffer (1 .. Name_Len) := Main.all;
2076 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2077 Main_Id := Name_Find;
2081 (Name => Name_Switches,
2082 In_Arrays => Element.Decl.Arrays,
2083 Shared => Project_Tree.Shared);
2084 The_Switches := Prj.Util.Value_Of
2087 In_Array => Switches_Array,
2088 Shared => Project_Tree.Shared);
2091 -- Otherwise, get the Default_Switches ("Ada")
2093 if The_Switches.Kind = Undefined then
2096 (Name => Name_Default_Switches,
2097 In_Arrays => Element.Decl.Arrays,
2098 Shared => Project_Tree.Shared);
2099 The_Switches := Prj.Util.Value_Of
2102 In_Array => Switches_Array,
2103 Shared => Project_Tree.Shared);
2106 -- If there are switches specified, put them in the
2107 -- Carg_Switches table.
2109 case The_Switches.Kind is
2110 when Prj.Undefined =>
2115 Switch : constant String :=
2116 Get_Name_String (The_Switches.Value);
2118 if Switch'Length > 0 then
2119 Add_To_Carg_Switches (new String'(Switch));
2124 Current := The_Switches.Values;
2125 while Current /= Prj.Nil_String loop
2126 The_String := Project_Tree.Shared.String_Elements
2130 Switch : constant String :=
2131 Get_Name_String (The_String.Value);
2133 if Switch'Length > 0 then
2134 Add_To_Carg_Switches (new String'(Switch));
2138 Current := The_String.Next;
2144 -- If -cargs is one of the switches, move the following switches
2145 -- to the Carg_Switches table.
2147 for J in 1 .. First_Switches.Last loop
2148 if First_Switches.Table (J).all = "-cargs" then
2154 -- Move the switches that are before -rules when the
2155 -- command is CHECK.
2158 while K <= First_Switches.Last
2160 (The_Command /= Check
2161 or else First_Switches.Table (K).all /= "-rules")
2163 Add_To_Carg_Switches (First_Switches.Table (K));
2167 if K > First_Switches.Last then
2168 First_Switches.Set_Last (J - 1);
2172 while K <= First_Switches.Last loop
2174 First_Switches.Table (Last) :=
2175 First_Switches.Table (K);
2179 First_Switches.Set_Last (Last);
2187 for J in 1 .. Last_Switches.Last loop
2188 if Last_Switches.Table (J).all = "-cargs" then
2194 -- Move the switches that are before -rules when the
2195 -- command is CHECK.
2198 while K <= Last_Switches.Last
2200 (The_Command /= Check
2201 or else Last_Switches.Table (K).all /= "-rules")
2203 Add_To_Carg_Switches (Last_Switches.Table (K));
2207 if K > Last_Switches.Last then
2208 Last_Switches.Set_Last (J - 1);
2212 while K <= Last_Switches.Last loop
2214 Last_Switches.Table (Last) :=
2215 Last_Switches.Table (K);
2219 Last_Switches.Set_Last (Last);
2228 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2229 M_File : constant Path_Name_Type := Mapping_File;
2232 if CP_File /= No_Path then
2233 if The_Command = Elim then
2234 First_Switches.Increment_Last;
2235 First_Switches.Table (First_Switches.Last) :=
2236 new String'("-C" & Get_Name_String (CP_File));
2239 Add_To_Carg_Switches
2240 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2244 if M_File /= No_Path then
2245 Add_To_Carg_Switches
2246 (new String'("-gnatem=" & Get_Name_String (M_File)));
2249 -- For gnatcheck, also indicate a global configuration pragmas
2250 -- file and, if -U is not used, a local one.
2252 if The_Command = Check then
2254 Pkg : constant Prj.Package_Id :=
2256 (Name => Name_Builder,
2257 In_Packages => Project.Decl.Packages,
2258 Shared => Project_Tree.Shared);
2260 Variable : Variable_Value :=
2263 Attribute_Or_Array_Name =>
2264 Name_Global_Configuration_Pragmas,
2266 Shared => Project_Tree.Shared);
2269 if (Variable = Nil_Variable_Value
2270 or else Length_Of_Name (Variable.Value) = 0)
2271 and then Pkg /= No_Package
2276 Attribute_Or_Array_Name =>
2277 Name_Global_Config_File,
2279 Shared => Project_Tree.Shared);
2282 if Variable /= Nil_Variable_Value
2283 and then Length_Of_Name (Variable.Value) /= 0
2285 Add_To_Carg_Switches
2287 ("-gnatec=" & Get_Name_String (Variable.Value)));
2291 if not All_Projects then
2293 Pkg : constant Prj.Package_Id :=
2295 (Name => Name_Compiler,
2296 In_Packages => Project.Decl.Packages,
2297 Shared => Project_Tree.Shared);
2299 Variable : Variable_Value :=
2302 Attribute_Or_Array_Name =>
2303 Name_Local_Configuration_Pragmas,
2305 Shared => Project_Tree.Shared);
2308 if (Variable = Nil_Variable_Value
2309 or else Length_Of_Name (Variable.Value) = 0)
2310 and then Pkg /= No_Package
2315 Attribute_Or_Array_Name =>
2316 Name_Local_Config_File,
2319 Project_Tree.Shared);
2322 if Variable /= Nil_Variable_Value
2323 and then Length_Of_Name (Variable.Value) /= 0
2325 Add_To_Carg_Switches
2328 Get_Name_String (Variable.Value)));
2336 if The_Command = Link then
2340 if The_Command = Link or else The_Command = Bind then
2342 -- For files that are specified as relative paths with directory
2343 -- information, we convert them to absolute paths, with parent
2344 -- being the current working directory if specified on the command
2345 -- line and the project directory if specified in the project
2346 -- file. This is what gnatmake is doing for linker and binder
2349 for J in 1 .. Last_Switches.Last loop
2350 GNATCmd.Test_If_Relative_Path
2351 (Last_Switches.Table (J), Current_Work_Dir);
2354 Get_Name_String (Project.Directory.Name);
2357 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2359 for J in 1 .. First_Switches.Last loop
2360 GNATCmd.Test_If_Relative_Path
2361 (First_Switches.Table (J), Project_Dir);
2365 elsif The_Command = Stub then
2367 File_Index : Integer := 0;
2368 Dir_Index : Integer := 0;
2369 Last : constant Integer := Last_Switches.Last;
2370 Lang : constant Language_Ptr :=
2371 Get_Language_From_Name (Project, "ada");
2374 for Index in 1 .. Last loop
2375 if Last_Switches.Table (Index)
2376 (Last_Switches.Table (Index)'First) /= '-'
2378 File_Index := Index;
2383 -- If the project file naming scheme is not standard, and if
2384 -- the file name ends with the spec suffix, then indicate to
2385 -- gnatstub the name of the body file with a -o switch.
2387 if not Is_Standard_GNAT_Naming (Lang.Config.Naming_Data) then
2388 if File_Index /= 0 then
2390 Spec : constant String :=
2392 (Last_Switches.Table (File_Index).all);
2393 Last : Natural := Spec'Last;
2396 Get_Name_String (Lang.Config.Naming_Data.Spec_Suffix);
2398 if Spec'Length > Name_Len
2399 and then Spec (Last - Name_Len + 1 .. Last) =
2400 Name_Buffer (1 .. Name_Len)
2402 Last := Last - Name_Len;
2404 (Lang.Config.Naming_Data.Body_Suffix);
2405 Last_Switches.Increment_Last;
2406 Last_Switches.Table (Last_Switches.Last) :=
2408 Last_Switches.Increment_Last;
2409 Last_Switches.Table (Last_Switches.Last) :=
2410 new String'(Spec (Spec'First .. Last) &
2411 Name_Buffer (1 .. Name_Len));
2417 -- Add the directory of the spec as the destination directory
2418 -- of the body, if there is no destination directory already
2421 if File_Index /= 0 then
2422 for Index in File_Index + 1 .. Last loop
2423 if Last_Switches.Table (Index)
2424 (Last_Switches.Table (Index)'First) /= '-'
2431 if Dir_Index = 0 then
2432 Last_Switches.Increment_Last;
2433 Last_Switches.Table (Last_Switches.Last) :=
2435 (Dir_Name (Last_Switches.Table (File_Index).all));
2441 -- For gnatmetric, the generated files should be put in the object
2442 -- directory. This must be the first switch, because it may be
2443 -- overridden by a switch in package Metrics in the project file or
2444 -- by a command line option. Note that we don't add the -d= switch
2445 -- if there is no object directory available.
2447 if The_Command = Metric
2448 and then Project.Object_Directory /= No_Path_Information
2450 First_Switches.Increment_Last;
2451 First_Switches.Table (2 .. First_Switches.Last) :=
2452 First_Switches.Table (1 .. First_Switches.Last - 1);
2453 First_Switches.Table (1) :=
2455 Get_Name_String (Project.Object_Directory.Name));
2458 -- For gnat check, -rules and the following switches need to be the
2459 -- last options, so move all these switches to table Rules_Switches.
2461 if The_Command = Check then
2464 -- Set to rank of options preceding "-rules"
2466 In_Rules_Switches : Boolean;
2467 -- Set to True when options "-rules" is found
2470 New_Last := First_Switches.Last;
2471 In_Rules_Switches := False;
2473 for J in 1 .. First_Switches.Last loop
2474 if In_Rules_Switches then
2475 Add_To_Rules_Switches (First_Switches.Table (J));
2477 elsif First_Switches.Table (J).all = "-rules" then
2479 In_Rules_Switches := True;
2483 if In_Rules_Switches then
2484 First_Switches.Set_Last (New_Last);
2487 New_Last := Last_Switches.Last;
2488 In_Rules_Switches := False;
2490 for J in 1 .. Last_Switches.Last loop
2491 if In_Rules_Switches then
2492 Add_To_Rules_Switches (Last_Switches.Table (J));
2494 elsif Last_Switches.Table (J).all = "-rules" then
2496 In_Rules_Switches := True;
2500 if In_Rules_Switches then
2501 Last_Switches.Set_Last (New_Last);
2506 -- For gnat check, sync, metric or pretty with -U + a main, get the
2507 -- list of sources from the closure and add them to the arguments.
2509 if ASIS_Main /= null then
2512 -- On VMS, set up the env var again for source dirs file. This is
2513 -- because the call to gnatmake has set this env var to another
2514 -- file that has now been deleted.
2516 if Hostparm.OpenVMS then
2518 -- First make sure that the recorded file names are empty
2520 Prj.Env.Initialize (Project_Tree);
2522 Prj.Env.Set_Ada_Paths
2523 (Project, Project_Tree, Including_Libraries => False);
2526 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2527 -- and gnat stack, if no file has been put on the command line, call
2528 -- tool with all the sources of the main project.
2530 elsif The_Command = Check or else
2531 The_Command = Sync or else
2532 The_Command = Pretty or else
2533 The_Command = Metric or else
2534 The_Command = List or else
2541 -- Gather all the arguments and invoke the executable
2544 The_Args : Argument_List
2545 (1 .. First_Switches.Last +
2546 Last_Switches.Last +
2547 Carg_Switches.Last +
2548 Rules_Switches.Last);
2549 Arg_Num : Natural := 0;
2552 for J in 1 .. First_Switches.Last loop
2553 Arg_Num := Arg_Num + 1;
2554 The_Args (Arg_Num) := First_Switches.Table (J);
2557 for J in 1 .. Last_Switches.Last loop
2558 Arg_Num := Arg_Num + 1;
2559 The_Args (Arg_Num) := Last_Switches.Table (J);
2562 for J in 1 .. Carg_Switches.Last loop
2563 Arg_Num := Arg_Num + 1;
2564 The_Args (Arg_Num) := Carg_Switches.Table (J);
2567 for J in 1 .. Rules_Switches.Last loop
2568 Arg_Num := Arg_Num + 1;
2569 The_Args (Arg_Num) := Rules_Switches.Table (J);
2572 -- If Display_Command is on, only display the generated command
2574 if Display_Command then
2575 Put (Standard_Error, "generated command -->");
2576 Put (Standard_Error, Exec_Path.all);
2578 for Arg in The_Args'Range loop
2579 Put (Standard_Error, " ");
2580 Put (Standard_Error, The_Args (Arg).all);
2583 Put (Standard_Error, "<--");
2584 New_Line (Standard_Error);
2588 if Verbose_Mode then
2589 Output.Write_Str (Exec_Path.all);
2591 for Arg in The_Args'Range loop
2592 Output.Write_Char (' ');
2593 Output.Write_Str (The_Args (Arg).all);
2600 Exit_Status (Spawn (Exec_Path.all, The_Args));
2607 if not Keep_Temporary_Files then
2608 Prj.Delete_All_Temp_Files (Project_Tree.Shared);
2609 Delete_Temp_Config_Files;
2612 Set_Exit_Status (Failure);
2615 if not Keep_Temporary_Files then
2616 Prj.Delete_All_Temp_Files (Project_Tree.Shared);
2617 Delete_Temp_Config_Files;
2620 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2621 -- return an understandable VMS exit status. However the exit status
2622 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2623 -- just a simple success or failure on VMS.
2625 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2626 Set_Exit_Status (Failure);
2628 Set_Exit_Status (My_Exit_Status);