1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
29 with Makeutl; use Makeutl;
30 with MLib.Tgt; use MLib.Tgt;
33 with Namet; use Namet;
35 with Osint; use Osint;
39 with Prj.Ext; use Prj.Ext;
41 with Prj.Tree; use Prj.Tree;
42 with Prj.Util; use Prj.Util;
44 with Snames; use Snames;
48 with Types; use Types;
49 with Hostparm; use Hostparm;
50 -- Used to determine if we are in VMS or not for error message purposes
52 with Ada.Characters.Handling; use Ada.Characters.Handling;
53 with Ada.Command_Line; use Ada.Command_Line;
54 with Ada.Text_IO; use Ada.Text_IO;
56 with GNAT.OS_Lib; use GNAT.OS_Lib;
58 with VMS_Conv; use VMS_Conv;
61 Project_Node_Tree : Project_Node_Tree_Ref;
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 Old_Project_File_Used : Boolean := False;
71 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
72 -- an old fashioned project file. -p cannot be used in conjunction
75 Temp_File_Name : Path_Name_Type := No_Path;
76 -- The name of the temporary text file to put a list of source/object
77 -- files to pass to a tool.
79 ASIS_Main : String_Access := null;
80 -- Main for commands Check, Metric and Pretty, when -U is used
82 package First_Switches is new Table.Table
83 (Table_Component_Type => String_Access,
84 Table_Index_Type => Integer,
87 Table_Increment => 100,
88 Table_Name => "Gnatcmd.First_Switches");
89 -- A table to keep the switches from the project file
91 package Carg_Switches is new Table.Table
92 (Table_Component_Type => String_Access,
93 Table_Index_Type => Integer,
96 Table_Increment => 100,
97 Table_Name => "Gnatcmd.Carg_Switches");
98 -- A table to keep the switches following -cargs for ASIS tools
100 package Rules_Switches is new Table.Table
101 (Table_Component_Type => String_Access,
102 Table_Index_Type => Integer,
103 Table_Low_Bound => 1,
105 Table_Increment => 100,
106 Table_Name => "Gnatcmd.Rules_Switches");
107 -- A table to keep the switches following -rules for gnatcheck
109 package Library_Paths is new Table.Table (
110 Table_Component_Type => String_Access,
111 Table_Index_Type => Integer,
112 Table_Low_Bound => 1,
114 Table_Increment => 100,
115 Table_Name => "Make.Library_Path");
117 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
118 -- tool. We allocate objects because we cannot declare aliased objects
119 -- as we are in a procedure, not a library level package.
121 subtype SA is String_Access;
123 Naming_String : constant SA := new String'("naming");
124 Binder_String : constant SA := new String'("binder");
125 Compiler_String : constant SA := new String'("compiler");
126 Check_String : constant SA := new String'("check");
127 Synchronize_String : constant SA := new String'("synchronize");
128 Eliminate_String : constant SA := new String'("eliminate");
129 Finder_String : constant SA := new String'("finder");
130 Linker_String : constant SA := new String'("linker");
131 Gnatls_String : constant SA := new String'("gnatls");
132 Pretty_String : constant SA := new String'("pretty_printer");
133 Stack_String : constant SA := new String'("stack");
134 Gnatstub_String : constant SA := new String'("gnatstub");
135 Metric_String : constant SA := new String'("metrics");
136 Xref_String : constant SA := new String'("cross_reference");
138 Packages_To_Check_By_Binder : constant String_List_Access :=
139 new String_List'((Naming_String, Binder_String));
141 Packages_To_Check_By_Check : constant String_List_Access :=
142 new String_List'((Naming_String, Check_String, Compiler_String));
144 Packages_To_Check_By_Sync : constant String_List_Access :=
145 new String_List'((Naming_String, Synchronize_String, Compiler_String));
147 Packages_To_Check_By_Eliminate : constant String_List_Access :=
148 new String_List'((Naming_String, Eliminate_String, Compiler_String));
150 Packages_To_Check_By_Finder : constant String_List_Access :=
151 new String_List'((Naming_String, Finder_String));
153 Packages_To_Check_By_Linker : constant String_List_Access :=
154 new String_List'((Naming_String, Linker_String));
156 Packages_To_Check_By_Gnatls : constant String_List_Access :=
157 new String_List'((Naming_String, Gnatls_String));
159 Packages_To_Check_By_Pretty : constant String_List_Access :=
160 new String_List'((Naming_String, Pretty_String, Compiler_String));
162 Packages_To_Check_By_Stack : constant String_List_Access :=
163 new String_List'((Naming_String, Stack_String));
165 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
166 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
168 Packages_To_Check_By_Metric : constant String_List_Access :=
169 new String_List'((Naming_String, Metric_String, Compiler_String));
171 Packages_To_Check_By_Xref : constant String_List_Access :=
172 new String_List'((Naming_String, Xref_String));
174 Packages_To_Check : String_List_Access := Prj.All_Packages;
176 ----------------------------------
177 -- Declarations for GNATCMD use --
178 ----------------------------------
180 The_Command : Command_Type;
181 -- The command specified in the invocation of the GNAT driver
183 Command_Arg : Positive := 1;
184 -- The index of the command in the arguments of the GNAT driver
186 My_Exit_Status : Exit_Status := Success;
187 -- The exit status of the spawned tool. Used to set the correct VMS
190 Current_Work_Dir : constant String := Get_Current_Dir;
191 -- The path of the working directory
193 All_Projects : Boolean := False;
194 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
195 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
196 -- should be invoked for all sources of all projects.
198 -----------------------
199 -- Local Subprograms --
200 -----------------------
202 procedure Add_To_Carg_Switches (Switch : String_Access);
203 -- Add a switch to the Carg_Switches table. If it is the first one, put the
204 -- switch "-cargs" at the beginning of the table.
206 procedure Add_To_Rules_Switches (Switch : String_Access);
207 -- Add a switch to the Rules_Switches table. If it is the first one, put
208 -- the switch "-crules" at the beginning of the table.
210 procedure Check_Files;
211 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
212 -- project file is specified, without any file arguments. If it is the
213 -- case, invoke the GNAT tool with the proper list of files, derived from
214 -- the sources of the project.
216 function Check_Project
217 (Project : Project_Id;
218 Root_Project : Project_Id) return Boolean;
219 -- Returns True if Project = Root_Project or if we want to consider all
220 -- sources of all projects. For GNAT METRIC, also returns True if Project
221 -- is extended by Root_Project.
223 procedure Check_Relative_Executable (Name : in out String_Access);
224 -- Check if an executable is specified as a relative path. If it is, and
225 -- the path contains directory information, fail. Otherwise, prepend the
226 -- exec directory. This procedure is only used for GNAT LINK when a project
227 -- file is specified.
229 function Configuration_Pragmas_File return Path_Name_Type;
230 -- Return an argument, if there is a configuration pragmas file to be
231 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
232 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
235 procedure Delete_Temp_Config_Files;
236 -- Delete all temporary config files. The caller is responsible for
237 -- ensuring that Keep_Temporary_Files is False.
239 procedure Get_Closure;
240 -- Get the sources in the closure of the ASIS_Main and add them to the
241 -- list of arguments.
243 function Index (Char : Character; Str : String) return Natural;
244 -- Returns first occurrence of Char in Str, returns 0 if Char not in Str
246 procedure Non_VMS_Usage;
247 -- Display usage for platforms other than VMS
249 procedure Process_Link;
250 -- Process GNAT LINK, when there is a project file specified
252 procedure Set_Library_For
253 (Project : Project_Id;
254 Libraries_Present : in out Boolean);
255 -- If Project is a library project, add the correct -L and -l switches to
256 -- the linker invocation.
258 procedure Set_Libraries is
259 new For_Every_Project_Imported (Boolean, Set_Library_For);
260 -- Add the -L and -l switches to the linker for all of the library
263 procedure Test_If_Relative_Path
264 (Switch : in out String_Access;
266 -- Test if Switch is a relative search path switch. If it is and it
267 -- includes directory information, prepend the path with Parent. This
268 -- subprogram is only called when using project files.
270 --------------------------
271 -- Add_To_Carg_Switches --
272 --------------------------
274 procedure Add_To_Carg_Switches (Switch : String_Access) is
276 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
278 if Carg_Switches.Last = 0 then
279 Carg_Switches.Increment_Last;
280 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
283 Carg_Switches.Increment_Last;
284 Carg_Switches.Table (Carg_Switches.Last) := Switch;
285 end Add_To_Carg_Switches;
287 ---------------------------
288 -- Add_To_Rules_Switches --
289 ---------------------------
291 procedure Add_To_Rules_Switches (Switch : String_Access) is
293 -- If the Rules_Switches table is empty, put "-rules" at the beginning
295 if Rules_Switches.Last = 0 then
296 Rules_Switches.Increment_Last;
297 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
300 Rules_Switches.Increment_Last;
301 Rules_Switches.Table (Rules_Switches.Last) := Switch;
302 end Add_To_Rules_Switches;
308 procedure Check_Files is
309 Add_Sources : Boolean := True;
310 Unit : Prj.Unit_Index;
311 Subunit : Boolean := False;
312 FD : File_Descriptor := Invalid_FD;
317 -- Check if there is at least one argument that is not a switch
319 for Index in 1 .. Last_Switches.Last loop
320 if Last_Switches.Table (Index) (1) /= '-' then
321 Add_Sources := False;
326 -- If all arguments were switches, add the path names of all the sources
327 -- of the main project.
331 -- For gnatcheck, gnatpp and gnatmetric , create a temporary file and
332 -- put the list of sources in it.
334 if The_Command = Check or else
335 The_Command = Pretty or else
338 Tempdir.Create_Temp_File (FD, Temp_File_Name);
339 Last_Switches.Increment_Last;
340 Last_Switches.Table (Last_Switches.Last) :=
341 new String'("-files=" & Get_Name_String (Temp_File_Name));
348 -- Gnatstack needs to add the .ci file for the binder generated
349 -- files corresponding to all of the library projects and main
350 -- units belonging to the application.
352 if The_Command = Stack then
353 Proj := Project_Tree.Projects;
354 while Proj /= null loop
355 if Check_Project (Proj.Project, Project) then
357 Main : String_List_Id;
358 File : String_Access;
361 -- Include binder generated files for main programs
363 Main := Proj.Project.Mains;
364 while Main /= Nil_String loop
368 (Proj.Project.Object_Directory.Name) &
372 (Project_Tree.String_Elements.Table
376 if Is_Regular_File (File.all) then
377 Last_Switches.Increment_Last;
378 Last_Switches.Table (Last_Switches.Last) := File;
382 Project_Tree.String_Elements.Table (Main).Next;
385 if Proj.Project.Library then
387 -- Include the .ci file for the binder generated
388 -- files that contains the initialization and
389 -- finalization of the library.
394 (Proj.Project.Object_Directory.Name) &
396 Get_Name_String (Proj.Project.Library_Name) &
399 if Is_Regular_File (File.all) then
400 Last_Switches.Increment_Last;
401 Last_Switches.Table (Last_Switches.Last) := File;
411 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
412 while Unit /= No_Unit_Index loop
414 -- For gnatls, we only need to put the library units, body or
415 -- spec, but not the subunits.
417 if The_Command = List then
418 if Unit.File_Names (Impl) /= null
419 and then not Unit.File_Names (Impl).Locally_Removed
421 -- There is a body, check if it is for this project
423 if All_Projects or else
424 Unit.File_Names (Impl).Project = Project
428 if Unit.File_Names (Spec) = null
429 or else Unit.File_Names (Spec).Locally_Removed
431 -- We have a body with no spec: we need to check if
432 -- this is a subunit, because gnatls will complain
436 Src_Ind : constant Source_File_Index :=
437 Sinput.P.Load_Project_File
443 Sinput.P.Source_File_Is_Subunit (Src_Ind);
448 Last_Switches.Increment_Last;
449 Last_Switches.Table (Last_Switches.Last) :=
453 (Impl).Display_File));
457 elsif Unit.File_Names (Spec) /= null
458 and then not Unit.File_Names (Spec).Locally_Removed
460 -- We have a spec with no body. Check if it is for this
463 if All_Projects or else
464 Unit.File_Names (Spec).Project = Project
466 Last_Switches.Increment_Last;
467 Last_Switches.Table (Last_Switches.Last) :=
468 new String'(Get_Name_String
469 (Unit.File_Names (Spec).Display_File));
473 -- For gnatstack, we put the .ci files corresponding to the
474 -- different units, including the binder generated files. We
475 -- only need to do that for the library units, body or spec,
476 -- but not the subunits.
478 elsif The_Command = Stack then
479 if Unit.File_Names (Impl) /= null
480 and then not Unit.File_Names (Impl).Locally_Removed
482 -- There is a body. Check if .ci files for this project
486 (Unit.File_Names (Impl).Project, Project)
490 if Unit.File_Names (Spec) = null
491 or else Unit.File_Names (Spec).Locally_Removed
493 -- We have a body with no spec: we need to check
494 -- if this is a subunit, because .ci files are not
495 -- generated for subunits.
498 Src_Ind : constant Source_File_Index :=
499 Sinput.P.Load_Project_File
505 Sinput.P.Source_File_Is_Subunit (Src_Ind);
510 Last_Switches.Increment_Last;
511 Last_Switches.Table (Last_Switches.Last) :=
515 (Impl).Project. Object_Directory.Name) &
518 (Unit.File_Names (Impl).Display_File),
523 elsif Unit.File_Names (Spec) /= null
524 and then not Unit.File_Names (Spec).Locally_Removed
526 -- Spec with no body, check if it is for this project
529 (Unit.File_Names (Spec).Project, Project)
531 Last_Switches.Increment_Last;
532 Last_Switches.Table (Last_Switches.Last) :=
536 (Spec).Project. Object_Directory.Name) &
539 (Get_Name_String (Unit.File_Names (Spec).File),
545 -- For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
546 -- sources of the project, or of all projects if -U was
549 for Kind in Spec_Or_Body loop
550 if Unit.File_Names (Kind) /= null
551 and then Check_Project
552 (Unit.File_Names (Kind).Project, Project)
553 and then not Unit.File_Names (Kind).Locally_Removed
556 (Unit.File_Names (Kind).Path.Display_Name);
558 if FD /= Invalid_FD then
559 Name_Len := Name_Len + 1;
560 Name_Buffer (Name_Len) := ASCII.LF;
562 Write (FD, Name_Buffer (1)'Address, Name_Len);
564 if Status /= Name_Len then
565 Osint.Fail ("disk full");
569 Last_Switches.Increment_Last;
570 Last_Switches.Table (Last_Switches.Last) :=
571 new String'(Get_Name_String
573 (Kind).Path.Display_Name));
579 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
583 if FD /= Invalid_FD then
587 Osint.Fail ("disk full");
597 function Check_Project
598 (Project : Project_Id;
599 Root_Project : Project_Id) return Boolean
604 if Project = No_Project then
607 elsif All_Projects or else Project = Root_Project then
610 elsif The_Command = Metric then
611 Proj := Root_Project;
612 while Proj.Extends /= No_Project loop
613 if Project = Proj.Extends then
617 Proj := Proj.Extends;
624 -------------------------------
625 -- Check_Relative_Executable --
626 -------------------------------
628 procedure Check_Relative_Executable (Name : in out String_Access) is
629 Exec_File_Name : constant String := Name.all;
632 if not Is_Absolute_Path (Exec_File_Name) then
633 for Index in Exec_File_Name'Range loop
634 if Exec_File_Name (Index) = Directory_Separator then
635 Fail ("relative executable (""" &
637 """) with directory part not allowed " &
638 "when using project files");
642 Get_Name_String (Project.Exec_Directory.Name);
644 if Name_Buffer (Name_Len) /= Directory_Separator then
645 Name_Len := Name_Len + 1;
646 Name_Buffer (Name_Len) := Directory_Separator;
649 Name_Buffer (Name_Len + 1 ..
650 Name_Len + Exec_File_Name'Length) :=
652 Name_Len := Name_Len + Exec_File_Name'Length;
653 Name := new String'(Name_Buffer (1 .. Name_Len));
655 end Check_Relative_Executable;
657 --------------------------------
658 -- Configuration_Pragmas_File --
659 --------------------------------
661 function Configuration_Pragmas_File return Path_Name_Type is
663 Prj.Env.Create_Config_Pragmas_File (Project, Project_Tree);
664 return Project.Config_File_Name;
665 end Configuration_Pragmas_File;
667 ------------------------------
668 -- Delete_Temp_Config_Files --
669 ------------------------------
671 procedure Delete_Temp_Config_Files is
674 pragma Warnings (Off, Success);
677 -- This should only be called if Keep_Temporary_Files is False
679 pragma Assert (not Keep_Temporary_Files);
681 if Project /= No_Project then
682 Proj := Project_Tree.Projects;
683 while Proj /= null loop
684 if Proj.Project.Config_File_Temp then
685 Delete_Temporary_File
686 (Project_Tree, Proj.Project.Config_File_Name);
693 -- If a temporary text file that contains a list of files for a tool
694 -- has been created, delete this temporary file.
696 if Temp_File_Name /= No_Path then
697 Delete_Temporary_File (Project_Tree, Temp_File_Name);
699 end Delete_Temp_Config_Files;
705 procedure Get_Closure is
706 Args : constant Argument_List :=
707 (1 => new String'("-q"),
708 2 => new String'("-b"),
709 3 => new String'("-P"),
712 6 => new String'("-bargs"),
713 7 => new String'("-R"),
714 8 => new String'("-Z"));
715 -- Arguments for the invocation of gnatmake which are added to the
716 -- Last_Arguments list by this procedure.
718 FD : File_Descriptor;
719 -- File descriptor for the temp file that will get the output of the
720 -- invocation of gnatmake.
722 Name : Path_Name_Type;
723 -- Path of the file FD
725 GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
728 GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
731 Return_Code : Integer;
734 pragma Warnings (Off, Unused);
736 File : Ada.Text_IO.File_Type;
737 Line : String (1 .. 250);
739 -- Used to read file if there is an error, it is good enough to display
740 -- just 250 characters if the first line of the file is very long.
743 Path : Path_Name_Type;
746 if GN_Path = null then
747 Put_Line (Standard_Error, "could not locate " & GN_Name);
751 -- Create the temp file
753 Tempdir.Create_Temp_File (FD, Name);
755 -- And close it, because on VMS Spawn with a file descriptor created
756 -- with Create_Temp_File does not redirect output.
760 -- Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
763 (Program_Name => GN_Path.all,
765 Output_File => Get_Name_String (Name),
767 Return_Code => Return_Code,
772 -- Read the output of the invocation of gnatmake
774 Open (File, In_File, Get_Name_String (Name));
776 -- If it was unsuccessful, display the first line in the file and exit
779 if Return_Code /= 0 then
780 Get_Line (File, Line, Last);
782 if not Keep_Temporary_Files then
788 Put_Line (Standard_Error, Line (1 .. Last));
790 (Standard_Error, "could not get closure of " & ASIS_Main.all);
794 -- Get each file name in the file, find its path and add it the
795 -- list of arguments.
797 while not End_Of_File (File) loop
798 Get_Line (File, Line, Last);
801 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
802 while Unit /= No_Unit_Index loop
803 if Unit.File_Names (Spec) /= null
805 Get_Name_String (Unit.File_Names (Spec).File) =
808 Path := Unit.File_Names (Spec).Path.Name;
811 elsif Unit.File_Names (Impl) /= null
813 Get_Name_String (Unit.File_Names (Impl).File) =
816 Path := Unit.File_Names (Impl).Path.Name;
820 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
823 Last_Switches.Increment_Last;
825 if Path /= No_Path then
826 Last_Switches.Table (Last_Switches.Last) :=
827 new String'(Get_Name_String (Path));
830 Last_Switches.Table (Last_Switches.Last) :=
831 new String'(Line (1 .. Last));
835 if not Keep_Temporary_Files then
847 function Index (Char : Character; Str : String) return Natural is
849 for Index in Str'Range loop
850 if Str (Index) = Char then
862 procedure Process_Link is
863 Look_For_Executable : Boolean := True;
864 Libraries_Present : Boolean := False;
865 Path_Option : constant String_Access :=
866 MLib.Linker_Library_Path_Option;
867 Prj : Project_Id := Project;
870 Skip_Executable : Boolean := False;
873 -- Add the default search directories, to be able to find
874 -- libgnat in call to MLib.Utl.Lib_Directory.
876 Add_Default_Search_Dirs;
878 Library_Paths.Set_Last (0);
880 -- Check if there are library project files
882 if MLib.Tgt.Support_For_Libraries /= None then
883 Set_Libraries (Project, Libraries_Present);
886 -- If there are, add the necessary additional switches
888 if Libraries_Present then
890 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
892 Last_Switches.Increment_Last;
893 Last_Switches.Table (Last_Switches.Last) :=
894 new String'("-L" & MLib.Utl.Lib_Directory);
895 Last_Switches.Increment_Last;
896 Last_Switches.Table (Last_Switches.Last) :=
897 new String'("-lgnarl");
898 Last_Switches.Increment_Last;
899 Last_Switches.Table (Last_Switches.Last) :=
900 new String'("-lgnat");
902 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
903 -- equivalent) with all the library dirs plus the standard GNAT
906 if Path_Option /= null then
908 Option : String_Access;
909 Length : Natural := Path_Option'Length;
913 if MLib.Separate_Run_Path_Options then
915 -- We are going to create one switch of the form
916 -- "-Wl,-rpath,dir_N" for each directory to consider.
918 -- One switch for each library directory
921 Library_Paths.First .. Library_Paths.Last
923 Last_Switches.Increment_Last;
925 (Last_Switches.Last) := new String'
927 Last_Switches.Table (Index).all);
930 -- One switch for the standard GNAT library dir
932 Last_Switches.Increment_Last;
934 (Last_Switches.Last) := new String'
935 (Path_Option.all & MLib.Utl.Lib_Directory);
938 -- First, compute the exact length for the switch
941 Library_Paths.First .. Library_Paths.Last
943 -- Add the length of the library dir plus one for the
944 -- directory separator.
948 Library_Paths.Table (Index)'Length + 1;
951 -- Finally, add the length of the standard GNAT library dir
953 Length := Length + MLib.Utl.Lib_Directory'Length;
954 Option := new String (1 .. Length);
955 Option (1 .. Path_Option'Length) := Path_Option.all;
956 Current := Path_Option'Length;
958 -- Put each library dir followed by a dir separator
961 Library_Paths.First .. Library_Paths.Last
966 Library_Paths.Table (Index)'Length) :=
967 Library_Paths.Table (Index).all;
970 Library_Paths.Table (Index)'Length + 1;
971 Option (Current) := Path_Separator;
974 -- Finally put the standard GNAT library dir
978 Current + MLib.Utl.Lib_Directory'Length) :=
979 MLib.Utl.Lib_Directory;
981 -- And add the switch to the last switches
983 Last_Switches.Increment_Last;
984 Last_Switches.Table (Last_Switches.Last) :=
991 -- Check if the first ALI file specified can be found, either in the
992 -- object directory of the main project or in an object directory of a
993 -- project file extended by the main project. If the ALI file can be
994 -- found, replace its name with its absolute path.
996 Skip_Executable := False;
998 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1000 -- If we have an executable just reset the flag
1002 if Skip_Executable then
1003 Skip_Executable := False;
1005 -- If -o, set flag so that next switch is not processed
1007 elsif Last_Switches.Table (J).all = "-o" then
1008 Skip_Executable := True;
1014 Switch : constant String :=
1015 Last_Switches.Table (J).all;
1016 ALI_File : constant String (1 .. Switch'Length + 4) :=
1019 Test_Existence : Boolean := False;
1022 Last := Switch'Length;
1024 -- Skip real switches
1026 if Switch'Length /= 0
1027 and then Switch (Switch'First) /= '-'
1029 -- Append ".ali" if file name does not end with it
1031 if Switch'Length <= 4
1032 or else Switch (Switch'Last - 3 .. Switch'Last)
1035 Last := ALI_File'Last;
1038 -- If file name includes directory information, stop if ALI
1041 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1042 Test_Existence := True;
1045 for K in Switch'Range loop
1046 if Switch (K) = '/' or else
1047 Switch (K) = Directory_Separator
1049 Test_Existence := True;
1055 if Test_Existence then
1056 if Is_Regular_File (ALI_File (1 .. Last)) then
1060 -- Look in object directories if ALI file exists
1065 Dir : constant String :=
1066 Get_Name_String (Prj.Object_Directory.Name);
1070 ALI_File (1 .. Last))
1072 -- We have found the correct project, so we
1073 -- replace the file with the absolute path.
1075 Last_Switches.Table (J) :=
1076 new String'(Dir & ALI_File (1 .. Last));
1084 -- Go to the project being extended, if any
1087 exit Project_Loop when Prj = No_Project;
1088 end loop Project_Loop;
1093 end loop Switch_Loop;
1095 -- If a relative path output file has been specified, we add the exec
1098 for J in reverse 1 .. Last_Switches.Last - 1 loop
1099 if Last_Switches.Table (J).all = "-o" then
1100 Check_Relative_Executable
1101 (Name => Last_Switches.Table (J + 1));
1102 Look_For_Executable := False;
1107 if Look_For_Executable then
1108 for J in reverse 1 .. First_Switches.Last - 1 loop
1109 if First_Switches.Table (J).all = "-o" then
1110 Look_For_Executable := False;
1111 Check_Relative_Executable
1112 (Name => First_Switches.Table (J + 1));
1118 -- If no executable is specified, then find the name of the first ALI
1119 -- file on the command line and issue a -o switch with the absolute path
1120 -- of the executable in the exec directory.
1122 if Look_For_Executable then
1123 for J in 1 .. Last_Switches.Last loop
1124 Arg := Last_Switches.Table (J);
1127 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1129 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1131 Last := Arg'Last - 4;
1133 elsif Is_Regular_File (Arg.all & ".ali") then
1138 Last_Switches.Increment_Last;
1139 Last_Switches.Table (Last_Switches.Last) :=
1141 Get_Name_String (Project.Exec_Directory.Name);
1142 Last_Switches.Increment_Last;
1143 Last_Switches.Table (Last_Switches.Last) :=
1144 new String'(Name_Buffer (1 .. Name_Len) &
1146 (Base_Name (Arg (Arg'First .. Last))));
1154 ---------------------
1155 -- Set_Library_For --
1156 ---------------------
1158 procedure Set_Library_For
1159 (Project : Project_Id;
1160 Libraries_Present : in out Boolean)
1162 Path_Option : constant String_Access :=
1163 MLib.Linker_Library_Path_Option;
1166 -- Case of library project
1168 if Project.Library then
1169 Libraries_Present := True;
1171 -- Add the -L switch
1173 Last_Switches.Increment_Last;
1174 Last_Switches.Table (Last_Switches.Last) :=
1175 new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1177 -- Add the -l switch
1179 Last_Switches.Increment_Last;
1180 Last_Switches.Table (Last_Switches.Last) :=
1181 new String'("-l" & Get_Name_String (Project.Library_Name));
1183 -- Add the directory to table Library_Paths, to be processed later
1184 -- if library is not static and if Path_Option is not null.
1186 if Project.Library_Kind /= Static
1187 and then Path_Option /= null
1189 Library_Paths.Increment_Last;
1190 Library_Paths.Table (Library_Paths.Last) :=
1191 new String'(Get_Name_String (Project.Library_Dir.Name));
1194 end Set_Library_For;
1196 ---------------------------
1197 -- Test_If_Relative_Path --
1198 ---------------------------
1200 procedure Test_If_Relative_Path
1201 (Switch : in out String_Access;
1205 Makeutl.Test_If_Relative_Path
1206 (Switch, Parent, Including_Non_Switch => False, Including_RTS => True);
1207 end Test_If_Relative_Path;
1213 procedure Non_VMS_Usage is
1217 Put_Line ("List of available commands");
1220 for C in Command_List'Range loop
1221 if not Command_List (C).VMS_Only then
1222 if Targparm.AAMP_On_Target then
1228 Put (To_Lower (Command_List (C).Cname.all));
1231 -- Never call gnatstack with a prefix
1234 Put (Command_List (C).Unixcmd.all);
1236 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1240 Sws : Argument_List_Access renames Command_List (C).Unixsws;
1243 for J in Sws'Range loop
1255 Put_Line ("Commands find, list, metric, pretty, stack, stub and xref " &
1256 "accept project file switches -vPx, -Pprj and -Xnam=val");
1260 -------------------------------------
1261 -- Start of processing for GNATCmd --
1262 -------------------------------------
1272 Project_Node_Tree := new Project_Node_Tree_Data;
1273 Prj.Tree.Initialize (Project_Node_Tree);
1275 Prj.Initialize (Project_Tree);
1278 Last_Switches.Set_Last (0);
1280 First_Switches.Init;
1281 First_Switches.Set_Last (0);
1283 Carg_Switches.Set_Last (0);
1284 Rules_Switches.Init;
1285 Rules_Switches.Set_Last (0);
1287 VMS_Conv.Initialize;
1289 -- Add the default search directories, to be able to find system.ads in the
1290 -- subsequent call to Targparm.Get_Target_Parameters.
1292 Add_Default_Search_Dirs;
1294 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1295 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1297 Targparm.Get_Target_Parameters;
1299 -- Add the directory where the GNAT driver is invoked in front of the path,
1300 -- if the GNAT driver is invoked with directory information. Do not do this
1301 -- for VMS, where the notion of path does not really exist.
1305 Command : constant String := Command_Name;
1308 for Index in reverse Command'Range loop
1309 if Command (Index) = Directory_Separator then
1311 Absolute_Dir : constant String :=
1313 (Command (Command'First .. Index));
1315 PATH : constant String :=
1316 Absolute_Dir & Path_Separator & Getenv ("PATH").all;
1319 Setenv ("PATH", PATH);
1328 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1329 -- filenames and pathnames to Unix style.
1332 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1334 VMS_Conversion (The_Command);
1336 B_Start := new String'("b__");
1338 -- If not on VMS, scan the command line directly
1341 if Argument_Count = 0 then
1347 if Argument_Count > Command_Arg
1348 and then Argument (Command_Arg) = "-v"
1350 Verbose_Mode := True;
1351 Command_Arg := Command_Arg + 1;
1353 elsif Argument_Count > Command_Arg
1354 and then Argument (Command_Arg) = "-dn"
1356 Keep_Temporary_Files := True;
1357 Command_Arg := Command_Arg + 1;
1364 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1366 if Command_List (The_Command).VMS_Only then
1370 & Command_List (The_Command).Cname.all
1371 & """ can only be used on VMS");
1375 when Constraint_Error =>
1377 -- Check if it is an alternate command
1380 Alternate : Alternate_Command;
1383 Alternate := Alternate_Command'Value
1384 (Argument (Command_Arg));
1385 The_Command := Corresponding_To (Alternate);
1388 when Constraint_Error =>
1390 Fail ("Unknown command: " & Argument (Command_Arg));
1394 -- Get the arguments from the command line and from the eventual
1395 -- argument file(s) specified on the command line.
1397 for Arg in Command_Arg + 1 .. Argument_Count loop
1399 The_Arg : constant String := Argument (Arg);
1402 -- Check if an argument file is specified
1404 if The_Arg (The_Arg'First) = '@' then
1406 Arg_File : Ada.Text_IO.File_Type;
1407 Line : String (1 .. 256);
1411 -- Open the file and fail if the file cannot be found
1416 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1421 (Standard_Error, "Cannot open argument file """);
1424 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1426 Put_Line (Standard_Error, """");
1430 -- Read line by line and put the content of each non-
1431 -- empty line in the Last_Switches table.
1433 while not End_Of_File (Arg_File) loop
1434 Get_Line (Arg_File, Line, Last);
1437 Last_Switches.Increment_Last;
1438 Last_Switches.Table (Last_Switches.Last) :=
1439 new String'(Line (1 .. Last));
1447 -- It is not an argument file; just put the argument in
1448 -- the Last_Switches table.
1450 Last_Switches.Increment_Last;
1451 Last_Switches.Table (Last_Switches.Last) :=
1452 new String'(The_Arg);
1460 Program : String_Access;
1461 Exec_Path : String_Access;
1464 if The_Command = Stack then
1466 -- Never call gnatstack with a prefix
1468 Program := new String'(Command_List (The_Command).Unixcmd.all);
1472 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1475 -- Locate the executable for the command
1477 Exec_Path := Locate_Exec_On_Path (Program.all);
1479 if Exec_Path = null then
1480 Put_Line (Standard_Error, "could not locate " & Program.all);
1484 -- If there are switches for the executable, put them as first switches
1486 if Command_List (The_Command).Unixsws /= null then
1487 for J in Command_List (The_Command).Unixsws'Range loop
1488 First_Switches.Increment_Last;
1489 First_Switches.Table (First_Switches.Last) :=
1490 Command_List (The_Command).Unixsws (J);
1494 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, METRIC, PRETTY, STACK, STUB,
1495 -- SYNC and XREF, look for project file related switches.
1499 Tool_Package_Name := Name_Binder;
1500 Packages_To_Check := Packages_To_Check_By_Binder;
1502 Tool_Package_Name := Name_Check;
1503 Packages_To_Check := Packages_To_Check_By_Check;
1505 Tool_Package_Name := Name_Eliminate;
1506 Packages_To_Check := Packages_To_Check_By_Eliminate;
1508 Tool_Package_Name := Name_Finder;
1509 Packages_To_Check := Packages_To_Check_By_Finder;
1511 Tool_Package_Name := Name_Linker;
1512 Packages_To_Check := Packages_To_Check_By_Linker;
1514 Tool_Package_Name := Name_Gnatls;
1515 Packages_To_Check := Packages_To_Check_By_Gnatls;
1517 Tool_Package_Name := Name_Metrics;
1518 Packages_To_Check := Packages_To_Check_By_Metric;
1520 Tool_Package_Name := Name_Pretty_Printer;
1521 Packages_To_Check := Packages_To_Check_By_Pretty;
1523 Tool_Package_Name := Name_Stack;
1524 Packages_To_Check := Packages_To_Check_By_Stack;
1526 Tool_Package_Name := Name_Gnatstub;
1527 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1529 Tool_Package_Name := Name_Synchronize;
1530 Packages_To_Check := Packages_To_Check_By_Sync;
1532 Tool_Package_Name := Name_Cross_Reference;
1533 Packages_To_Check := Packages_To_Check_By_Xref;
1535 Tool_Package_Name := No_Name;
1538 if Tool_Package_Name /= No_Name then
1540 -- Check that the switches are consistent. Detect project file
1541 -- related switches.
1543 Inspect_Switches : declare
1544 Arg_Num : Positive := 1;
1545 Argv : String_Access;
1547 procedure Remove_Switch (Num : Positive);
1548 -- Remove a project related switch from table Last_Switches
1554 procedure Remove_Switch (Num : Positive) is
1556 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1557 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1558 Last_Switches.Decrement_Last;
1561 -- Start of processing for Inspect_Switches
1564 while Arg_Num <= Last_Switches.Last loop
1565 Argv := Last_Switches.Table (Arg_Num);
1567 if Argv (Argv'First) = '-' then
1568 if Argv'Length = 1 then
1570 ("switch character cannot be followed by a blank");
1573 -- The two style project files (-p and -P) cannot be used
1576 if (The_Command = Find or else The_Command = Xref)
1577 and then Argv (2) = 'p'
1579 Old_Project_File_Used := True;
1580 if Project_File /= null then
1581 Fail ("-P and -p cannot be used together");
1585 -- --subdirs=... Specify Subdirs
1587 if Argv'Length > Makeutl.Subdirs_Option'Length and then
1590 Argv'First + Makeutl.Subdirs_Option'Length - 1) =
1591 Makeutl.Subdirs_Option
1596 (Argv'First + Makeutl.Subdirs_Option'Length ..
1599 Remove_Switch (Arg_Num);
1601 -- -aPdir Add dir to the project search path
1603 elsif Argv'Length > 3
1604 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1606 Add_Search_Project_Directory
1607 (Project_Node_Tree, Argv (Argv'First + 3 .. Argv'Last));
1609 Remove_Switch (Arg_Num);
1611 -- -eL Follow links for files
1613 elsif Argv.all = "-eL" then
1614 Follow_Links_For_Files := True;
1615 Follow_Links_For_Dirs := True;
1617 Remove_Switch (Arg_Num);
1619 -- -vPx Specify verbosity while parsing project files
1621 elsif Argv'Length = 4
1622 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1624 case Argv (Argv'Last) is
1626 Current_Verbosity := Prj.Default;
1628 Current_Verbosity := Prj.Medium;
1630 Current_Verbosity := Prj.High;
1632 Fail ("Invalid switch: " & Argv.all);
1635 Remove_Switch (Arg_Num);
1637 -- -Pproject_file Specify project file to be used
1639 elsif Argv (Argv'First + 1) = 'P' then
1641 -- Only one -P switch can be used
1643 if Project_File /= null then
1646 & ": second project file forbidden (first is """
1650 -- The two style project files (-p and -P) cannot be
1653 elsif Old_Project_File_Used then
1654 Fail ("-p and -P cannot be used together");
1656 elsif Argv'Length = 2 then
1658 -- There is space between -P and the project file
1659 -- name. -P cannot be the last option.
1661 if Arg_Num = Last_Switches.Last then
1662 Fail ("project file name missing after -P");
1665 Remove_Switch (Arg_Num);
1666 Argv := Last_Switches.Table (Arg_Num);
1668 -- After -P, there must be a project file name,
1669 -- not another switch.
1671 if Argv (Argv'First) = '-' then
1672 Fail ("project file name missing after -P");
1675 Project_File := new String'(Argv.all);
1680 -- No space between -P and project file name
1683 new String'(Argv (Argv'First + 2 .. Argv'Last));
1686 Remove_Switch (Arg_Num);
1688 -- -Xexternal=value Specify an external reference to be
1689 -- used in project files
1691 elsif Argv'Length >= 5
1692 and then Argv (Argv'First + 1) = 'X'
1695 Equal_Pos : constant Natural :=
1698 Argv (Argv'First + 2 .. Argv'Last));
1700 if Equal_Pos >= Argv'First + 3 and then
1701 Equal_Pos /= Argv'Last then
1702 Add (Project_Node_Tree,
1704 Argv (Argv'First + 2 .. Equal_Pos - 1),
1705 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1709 & " is not a valid external assignment.");
1713 Remove_Switch (Arg_Num);
1716 (The_Command = Check or else
1717 The_Command = Sync or else
1718 The_Command = Pretty or else
1719 The_Command = Metric or else
1720 The_Command = Stack or else
1722 and then Argv'Length = 2
1723 and then Argv (2) = 'U'
1725 All_Projects := True;
1726 Remove_Switch (Arg_Num);
1729 Arg_Num := Arg_Num + 1;
1732 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1733 or else The_Command = Sync
1734 or else The_Command = Metric
1735 or else The_Command = Pretty)
1736 and then Project_File /= null
1737 and then All_Projects
1739 if ASIS_Main /= null then
1740 Fail ("cannot specify more than one main after -U");
1743 Remove_Switch (Arg_Num);
1747 Arg_Num := Arg_Num + 1;
1750 end Inspect_Switches;
1753 -- If there is a project file specified, parse it, get the switches
1754 -- for the tool and setup PATH environment variables.
1756 if Project_File /= null then
1757 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1760 (Project => Project,
1761 In_Tree => Project_Tree,
1762 In_Node_Tree => Project_Node_Tree,
1763 Project_File_Name => Project_File.all,
1764 Flags => Gnatmake_Flags,
1765 Packages_To_Check => Packages_To_Check);
1767 if Project = Prj.No_Project then
1768 Fail ("""" & Project_File.all & """ processing failed");
1771 -- Check if a package with the name of the tool is in the project
1772 -- file and if there is one, get the switches, if any, and scan them.
1775 Pkg : constant Prj.Package_Id :=
1777 (Name => Tool_Package_Name,
1778 In_Packages => Project.Decl.Packages,
1779 In_Tree => Project_Tree);
1781 Element : Package_Element;
1783 Default_Switches_Array : Array_Element_Id;
1785 The_Switches : Prj.Variable_Value;
1786 Current : Prj.String_List_Id;
1787 The_String : String_Element;
1790 if Pkg /= No_Package then
1791 Element := Project_Tree.Packages.Table (Pkg);
1793 -- Packages Gnatls and Gnatstack have a single attribute
1794 -- Switches, that is not an associative array.
1796 if The_Command = List or else The_Command = Stack then
1799 (Variable_Name => Snames.Name_Switches,
1800 In_Variables => Element.Decl.Attributes,
1801 In_Tree => Project_Tree);
1803 -- Packages Binder (for gnatbind), Cross_Reference (for
1804 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1805 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1806 -- (for gnatcheck), and Metric (for gnatmetric) have an
1807 -- attributed Switches, an associative array, indexed by the
1808 -- name of the file.
1810 -- They also have an attribute Default_Switches, indexed by the
1811 -- name of the programming language.
1814 if The_Switches.Kind = Prj.Undefined then
1815 Default_Switches_Array :=
1817 (Name => Name_Default_Switches,
1818 In_Arrays => Element.Decl.Arrays,
1819 In_Tree => Project_Tree);
1820 The_Switches := Prj.Util.Value_Of
1823 In_Array => Default_Switches_Array,
1824 In_Tree => Project_Tree);
1828 -- If there are switches specified in the package of the
1829 -- project file corresponding to the tool, scan them.
1831 case The_Switches.Kind is
1832 when Prj.Undefined =>
1837 Switch : constant String :=
1838 Get_Name_String (The_Switches.Value);
1841 if Switch'Length > 0 then
1842 First_Switches.Increment_Last;
1843 First_Switches.Table (First_Switches.Last) :=
1844 new String'(Switch);
1849 Current := The_Switches.Values;
1850 while Current /= Prj.Nil_String loop
1851 The_String := Project_Tree.String_Elements.
1855 Switch : constant String :=
1856 Get_Name_String (The_String.Value);
1859 if Switch'Length > 0 then
1860 First_Switches.Increment_Last;
1861 First_Switches.Table (First_Switches.Last) :=
1862 new String'(Switch);
1866 Current := The_String.Next;
1872 if The_Command = Bind
1873 or else The_Command = Link
1874 or else The_Command = Elim
1876 Change_Dir (Get_Name_String (Project.Object_Directory.Name));
1879 -- Set up the env vars for project path files
1881 Prj.Env.Set_Ada_Paths
1882 (Project, Project_Tree, Including_Libraries => False);
1884 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
1885 -- a configuration pragmas file, if necessary.
1887 if The_Command = Pretty
1888 or else The_Command = Metric
1889 or else The_Command = Stub
1890 or else The_Command = Elim
1891 or else The_Command = Check
1892 or else The_Command = Sync
1894 -- If there are switches in package Compiler, put them in the
1895 -- Carg_Switches table.
1898 Pkg : constant Prj.Package_Id :=
1900 (Name => Name_Compiler,
1901 In_Packages => Project.Decl.Packages,
1902 In_Tree => Project_Tree);
1904 Element : Package_Element;
1906 Switches_Array : Array_Element_Id;
1908 The_Switches : Prj.Variable_Value;
1909 Current : Prj.String_List_Id;
1910 The_String : String_Element;
1912 Main : String_Access := null;
1916 if Pkg /= No_Package then
1918 -- First, check if there is a single main specified.
1920 for J in 1 .. Last_Switches.Last loop
1921 if Last_Switches.Table (J) (1) /= '-' then
1923 Main := Last_Switches.Table (J);
1932 Element := Project_Tree.Packages.Table (Pkg);
1934 -- If there is a single main and there is compilation
1935 -- switches specified in the project file, use them.
1937 if Main /= null and then not All_Projects then
1938 Name_Len := Main'Length;
1939 Name_Buffer (1 .. Name_Len) := Main.all;
1940 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1941 Main_Id := Name_Find;
1945 (Name => Name_Switches,
1946 In_Arrays => Element.Decl.Arrays,
1947 In_Tree => Project_Tree);
1948 The_Switches := Prj.Util.Value_Of
1951 In_Array => Switches_Array,
1952 In_Tree => Project_Tree);
1955 -- Otherwise, get the Default_Switches ("Ada")
1957 if The_Switches.Kind = Undefined then
1960 (Name => Name_Default_Switches,
1961 In_Arrays => Element.Decl.Arrays,
1962 In_Tree => Project_Tree);
1963 The_Switches := Prj.Util.Value_Of
1966 In_Array => Switches_Array,
1967 In_Tree => Project_Tree);
1970 -- If there are switches specified, put them in the
1971 -- Carg_Switches table.
1973 case The_Switches.Kind is
1974 when Prj.Undefined =>
1979 Switch : constant String :=
1980 Get_Name_String (The_Switches.Value);
1982 if Switch'Length > 0 then
1983 Add_To_Carg_Switches (new String'(Switch));
1988 Current := The_Switches.Values;
1989 while Current /= Prj.Nil_String loop
1991 Project_Tree.String_Elements.Table (Current);
1994 Switch : constant String :=
1995 Get_Name_String (The_String.Value);
1997 if Switch'Length > 0 then
1998 Add_To_Carg_Switches (new String'(Switch));
2002 Current := The_String.Next;
2008 -- If -cargs is one of the switches, move the following switches
2009 -- to the Carg_Switches table.
2011 for J in 1 .. First_Switches.Last loop
2012 if First_Switches.Table (J).all = "-cargs" then
2018 -- Move the switches that are before -rules when the
2019 -- command is CHECK.
2022 while K <= First_Switches.Last
2024 (The_Command /= Check
2025 or else First_Switches.Table (K).all /= "-rules")
2027 Add_To_Carg_Switches (First_Switches.Table (K));
2031 if K > First_Switches.Last then
2032 First_Switches.Set_Last (J - 1);
2036 while K <= First_Switches.Last loop
2038 First_Switches.Table (Last) :=
2039 First_Switches.Table (K);
2043 First_Switches.Set_Last (Last);
2051 for J in 1 .. Last_Switches.Last loop
2052 if Last_Switches.Table (J).all = "-cargs" then
2058 -- Move the switches that are before -rules when the
2059 -- command is CHECK.
2062 while K <= Last_Switches.Last
2064 (The_Command /= Check
2066 Last_Switches.Table (K).all /= "-rules")
2068 Add_To_Carg_Switches (Last_Switches.Table (K));
2072 if K > Last_Switches.Last then
2073 Last_Switches.Set_Last (J - 1);
2077 while K <= Last_Switches.Last loop
2079 Last_Switches.Table (Last) :=
2080 Last_Switches.Table (K);
2084 Last_Switches.Set_Last (Last);
2093 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2096 if CP_File /= No_Path then
2097 if The_Command = Elim then
2098 First_Switches.Increment_Last;
2099 First_Switches.Table (First_Switches.Last) :=
2100 new String'("-C" & Get_Name_String (CP_File));
2103 Add_To_Carg_Switches
2104 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2110 if The_Command = Link then
2114 if The_Command = Link or else The_Command = Bind then
2116 -- For files that are specified as relative paths with directory
2117 -- information, we convert them to absolute paths, with parent
2118 -- being the current working directory if specified on the command
2119 -- line and the project directory if specified in the project
2120 -- file. This is what gnatmake is doing for linker and binder
2123 for J in 1 .. Last_Switches.Last loop
2124 GNATCmd.Test_If_Relative_Path
2125 (Last_Switches.Table (J), Current_Work_Dir);
2128 Get_Name_String (Project.Directory.Name);
2131 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2133 for J in 1 .. First_Switches.Last loop
2134 GNATCmd.Test_If_Relative_Path
2135 (First_Switches.Table (J), Project_Dir);
2139 elsif The_Command = Stub then
2141 File_Index : Integer := 0;
2142 Dir_Index : Integer := 0;
2143 Last : constant Integer := Last_Switches.Last;
2144 Lang : constant Language_Ptr :=
2145 Get_Language_From_Name (Project, "ada");
2148 for Index in 1 .. Last loop
2149 if Last_Switches.Table (Index)
2150 (Last_Switches.Table (Index)'First) /= '-'
2152 File_Index := Index;
2157 -- If the project file naming scheme is not standard, and if
2158 -- the file name ends with the spec suffix, then indicate to
2159 -- gnatstub the name of the body file with a -o switch.
2161 if not Is_Standard_GNAT_Naming (Lang.Config.Naming_Data) then
2162 if File_Index /= 0 then
2164 Spec : constant String :=
2166 (Last_Switches.Table (File_Index).all);
2167 Last : Natural := Spec'Last;
2170 Get_Name_String (Lang.Config.Naming_Data.Spec_Suffix);
2172 if Spec'Length > Name_Len
2173 and then Spec (Last - Name_Len + 1 .. Last) =
2174 Name_Buffer (1 .. Name_Len)
2176 Last := Last - Name_Len;
2178 (Lang.Config.Naming_Data.Body_Suffix);
2179 Last_Switches.Increment_Last;
2180 Last_Switches.Table (Last_Switches.Last) :=
2182 Last_Switches.Increment_Last;
2183 Last_Switches.Table (Last_Switches.Last) :=
2184 new String'(Spec (Spec'First .. Last) &
2185 Name_Buffer (1 .. Name_Len));
2191 -- Add the directory of the spec as the destination directory
2192 -- of the body, if there is no destination directory already
2195 if File_Index /= 0 then
2196 for Index in File_Index + 1 .. Last loop
2197 if Last_Switches.Table (Index)
2198 (Last_Switches.Table (Index)'First) /= '-'
2205 if Dir_Index = 0 then
2206 Last_Switches.Increment_Last;
2207 Last_Switches.Table (Last_Switches.Last) :=
2209 (Dir_Name (Last_Switches.Table (File_Index).all));
2215 -- For gnatmetric, the generated files should be put in the object
2216 -- directory. This must be the first switch, because it may be
2217 -- overridden by a switch in package Metrics in the project file or
2218 -- by a command line option. Note that we don't add the -d= switch
2219 -- if there is no object directory available.
2221 if The_Command = Metric
2222 and then Project.Object_Directory /= No_Path_Information
2224 First_Switches.Increment_Last;
2225 First_Switches.Table (2 .. First_Switches.Last) :=
2226 First_Switches.Table (1 .. First_Switches.Last - 1);
2227 First_Switches.Table (1) :=
2229 Get_Name_String (Project.Object_Directory.Name));
2232 -- For gnat check, -rules and the following switches need to be the
2233 -- last options, so move all these switches to table Rules_Switches.
2235 if The_Command = Check then
2238 -- Set to rank of options preceding "-rules"
2240 In_Rules_Switches : Boolean;
2241 -- Set to True when options "-rules" is found
2244 New_Last := First_Switches.Last;
2245 In_Rules_Switches := False;
2247 for J in 1 .. First_Switches.Last loop
2248 if In_Rules_Switches then
2249 Add_To_Rules_Switches (First_Switches.Table (J));
2251 elsif First_Switches.Table (J).all = "-rules" then
2253 In_Rules_Switches := True;
2257 if In_Rules_Switches then
2258 First_Switches.Set_Last (New_Last);
2261 New_Last := Last_Switches.Last;
2262 In_Rules_Switches := False;
2264 for J in 1 .. Last_Switches.Last loop
2265 if In_Rules_Switches then
2266 Add_To_Rules_Switches (Last_Switches.Table (J));
2268 elsif Last_Switches.Table (J).all = "-rules" then
2270 In_Rules_Switches := True;
2274 if In_Rules_Switches then
2275 Last_Switches.Set_Last (New_Last);
2280 -- For gnat check, sync, metric or pretty with -U + a main, get the
2281 -- list of sources from the closure and add them to the arguments.
2283 if ASIS_Main /= null then
2286 -- On VMS, set up the env var again for source dirs file. This is
2287 -- because the call to gnatmake has set this env var to another
2288 -- file that has now been deleted.
2290 if Hostparm.OpenVMS then
2292 -- First make sure that the recorded file names are empty
2294 Prj.Env.Initialize (Project_Tree);
2296 Prj.Env.Set_Ada_Paths
2297 (Project, Project_Tree, Including_Libraries => False);
2300 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2301 -- and gnat stack, if no file has been put on the command line, call
2302 -- tool with all the sources of the main project.
2304 elsif The_Command = Check or else
2305 The_Command = Sync or else
2306 The_Command = Pretty or else
2307 The_Command = Metric or else
2308 The_Command = List or else
2315 -- Gather all the arguments and invoke the executable
2318 The_Args : Argument_List
2319 (1 .. First_Switches.Last +
2320 Last_Switches.Last +
2321 Carg_Switches.Last +
2322 Rules_Switches.Last);
2323 Arg_Num : Natural := 0;
2326 for J in 1 .. First_Switches.Last loop
2327 Arg_Num := Arg_Num + 1;
2328 The_Args (Arg_Num) := First_Switches.Table (J);
2331 for J in 1 .. Last_Switches.Last loop
2332 Arg_Num := Arg_Num + 1;
2333 The_Args (Arg_Num) := Last_Switches.Table (J);
2336 for J in 1 .. Carg_Switches.Last loop
2337 Arg_Num := Arg_Num + 1;
2338 The_Args (Arg_Num) := Carg_Switches.Table (J);
2341 for J in 1 .. Rules_Switches.Last loop
2342 Arg_Num := Arg_Num + 1;
2343 The_Args (Arg_Num) := Rules_Switches.Table (J);
2346 -- If Display_Command is on, only display the generated command
2348 if Display_Command then
2349 Put (Standard_Error, "generated command -->");
2350 Put (Standard_Error, Exec_Path.all);
2352 for Arg in The_Args'Range loop
2353 Put (Standard_Error, " ");
2354 Put (Standard_Error, The_Args (Arg).all);
2357 Put (Standard_Error, "<--");
2358 New_Line (Standard_Error);
2362 if Verbose_Mode then
2363 Output.Write_Str (Exec_Path.all);
2365 for Arg in The_Args'Range loop
2366 Output.Write_Char (' ');
2367 Output.Write_Str (The_Args (Arg).all);
2374 Exit_Status (Spawn (Exec_Path.all, The_Args));
2381 if not Keep_Temporary_Files then
2382 Prj.Delete_All_Temp_Files (Project_Tree);
2383 Delete_Temp_Config_Files;
2386 Set_Exit_Status (Failure);
2389 if not Keep_Temporary_Files then
2390 Prj.Delete_All_Temp_Files (Project_Tree);
2391 Delete_Temp_Config_Files;
2394 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2395 -- return an understandable VMS exit status. However the exit status
2396 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2397 -- just a simple success or failure on VMS.
2399 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2400 Set_Exit_Status (Failure);
2402 Set_Exit_Status (My_Exit_Status);