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;
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.Util; use Prj.Util;
43 with Snames; use Snames;
47 with Types; use Types;
48 with Hostparm; use Hostparm;
49 -- Used to determine if we are in VMS or not for error message purposes
51 with Ada.Characters.Handling; use Ada.Characters.Handling;
52 with Ada.Command_Line; use Ada.Command_Line;
53 with Ada.Text_IO; use Ada.Text_IO;
55 with GNAT.OS_Lib; use GNAT.OS_Lib;
57 with VMS_Conv; use VMS_Conv;
60 Project_Tree : constant Project_Tree_Ref := new Project_Tree_Data;
61 Project_File : String_Access;
62 Project : Prj.Project_Id;
63 Current_Verbosity : Prj.Verbosity := Prj.Default;
64 Tool_Package_Name : Name_Id := No_Name;
66 B_Start : String_Ptr := new String'("b~");
67 -- Prefix of binder generated file, changed to b__ for VMS
69 Old_Project_File_Used : Boolean := False;
70 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
71 -- an old fashioned project file. -p cannot be used in conjunction
74 Temp_File_Name : Path_Name_Type := No_Path;
75 -- The name of the temporary text file to put a list of source/object
76 -- files to pass to a tool.
78 ASIS_Main : String_Access := null;
79 -- Main for commands Check, Metric and Pretty, when -U is used
81 package First_Switches is new Table.Table
82 (Table_Component_Type => String_Access,
83 Table_Index_Type => Integer,
86 Table_Increment => 100,
87 Table_Name => "Gnatcmd.First_Switches");
88 -- A table to keep the switches from the project file
90 package Carg_Switches is new Table.Table
91 (Table_Component_Type => String_Access,
92 Table_Index_Type => Integer,
95 Table_Increment => 100,
96 Table_Name => "Gnatcmd.Carg_Switches");
97 -- A table to keep the switches following -cargs for ASIS tools
99 package Rules_Switches is new Table.Table
100 (Table_Component_Type => String_Access,
101 Table_Index_Type => Integer,
102 Table_Low_Bound => 1,
104 Table_Increment => 100,
105 Table_Name => "Gnatcmd.Rules_Switches");
106 -- A table to keep the switches following -rules for gnatcheck
108 package Library_Paths is new Table.Table (
109 Table_Component_Type => String_Access,
110 Table_Index_Type => Integer,
111 Table_Low_Bound => 1,
113 Table_Increment => 100,
114 Table_Name => "Make.Library_Path");
116 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
117 -- tool. We allocate objects because we cannot declare aliased objects
118 -- as we are in a procedure, not a library level package.
120 subtype SA is String_Access;
122 Naming_String : constant SA := new String'("naming");
123 Binder_String : constant SA := new String'("binder");
124 Compiler_String : constant SA := new String'("compiler");
125 Check_String : constant SA := new String'("check");
126 Synchronize_String : constant SA := new String'("synchronize");
127 Eliminate_String : constant SA := new String'("eliminate");
128 Finder_String : constant SA := new String'("finder");
129 Linker_String : constant SA := new String'("linker");
130 Gnatls_String : constant SA := new String'("gnatls");
131 Pretty_String : constant SA := new String'("pretty_printer");
132 Stack_String : constant SA := new String'("stack");
133 Gnatstub_String : constant SA := new String'("gnatstub");
134 Metric_String : constant SA := new String'("metrics");
135 Xref_String : constant SA := new String'("cross_reference");
137 Packages_To_Check_By_Binder : constant String_List_Access :=
138 new String_List'((Naming_String, Binder_String));
140 Packages_To_Check_By_Check : constant String_List_Access :=
141 new String_List'((Naming_String, Check_String, Compiler_String));
143 Packages_To_Check_By_Sync : constant String_List_Access :=
144 new String_List'((Naming_String, Synchronize_String, Compiler_String));
146 Packages_To_Check_By_Eliminate : constant String_List_Access :=
147 new String_List'((Naming_String, Eliminate_String, Compiler_String));
149 Packages_To_Check_By_Finder : constant String_List_Access :=
150 new String_List'((Naming_String, Finder_String));
152 Packages_To_Check_By_Linker : constant String_List_Access :=
153 new String_List'((Naming_String, Linker_String));
155 Packages_To_Check_By_Gnatls : constant String_List_Access :=
156 new String_List'((Naming_String, Gnatls_String));
158 Packages_To_Check_By_Pretty : constant String_List_Access :=
159 new String_List'((Naming_String, Pretty_String, Compiler_String));
161 Packages_To_Check_By_Stack : constant String_List_Access :=
162 new String_List'((Naming_String, Stack_String));
164 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
165 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
167 Packages_To_Check_By_Metric : constant String_List_Access :=
168 new String_List'((Naming_String, Metric_String, Compiler_String));
170 Packages_To_Check_By_Xref : constant String_List_Access :=
171 new String_List'((Naming_String, Xref_String));
173 Packages_To_Check : String_List_Access := Prj.All_Packages;
175 ----------------------------------
176 -- Declarations for GNATCMD use --
177 ----------------------------------
179 The_Command : Command_Type;
180 -- The command specified in the invocation of the GNAT driver
182 Command_Arg : Positive := 1;
183 -- The index of the command in the arguments of the GNAT driver
185 My_Exit_Status : Exit_Status := Success;
186 -- The exit status of the spawned tool. Used to set the correct VMS
189 Current_Work_Dir : constant String := Get_Current_Dir;
190 -- The path of the working directory
192 All_Projects : Boolean := False;
193 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
194 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
195 -- should be invoked for all sources of all projects.
197 -----------------------
198 -- Local Subprograms --
199 -----------------------
201 procedure Add_To_Carg_Switches (Switch : String_Access);
202 -- Add a switch to the Carg_Switches table. If it is the first one, put the
203 -- switch "-cargs" at the beginning of the table.
205 procedure Add_To_Rules_Switches (Switch : String_Access);
206 -- Add a switch to the Rules_Switches table. If it is the first one, put
207 -- the switch "-crules" at the beginning of the table.
209 procedure Check_Files;
210 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
211 -- project file is specified, without any file arguments. If it is the
212 -- case, invoke the GNAT tool with the proper list of files, derived from
213 -- the sources of the project.
215 function Check_Project
216 (Project : Project_Id;
217 Root_Project : Project_Id) return Boolean;
218 -- Returns True if Project = Root_Project or if we want to consider all
219 -- sources of all projects. For GNAT METRIC, also returns True if Project
220 -- is extended by Root_Project.
222 procedure Check_Relative_Executable (Name : in out String_Access);
223 -- Check if an executable is specified as a relative path. If it is, and
224 -- the path contains directory information, fail. Otherwise, prepend the
225 -- exec directory. This procedure is only used for GNAT LINK when a project
226 -- file is specified.
228 function Configuration_Pragmas_File return Path_Name_Type;
229 -- Return an argument, if there is a configuration pragmas file to be
230 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
231 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
234 procedure Delete_Temp_Config_Files;
235 -- Delete all temporary config files. The caller is responsible for
236 -- ensuring that Keep_Temporary_Files is False.
238 procedure Get_Closure;
239 -- Get the sources in the closure of the ASIS_Main and add them to the
240 -- list of arguments.
242 function Index (Char : Character; Str : String) return Natural;
243 -- Returns first occurrence of Char in Str, returns 0 if Char not in Str
245 procedure Non_VMS_Usage;
246 -- Display usage for platforms other than VMS
248 procedure Process_Link;
249 -- Process GNAT LINK, when there is a project file specified
251 procedure Set_Library_For
252 (Project : Project_Id;
253 Libraries_Present : in out Boolean);
254 -- If Project is a library project, add the correct -L and -l switches to
255 -- the linker invocation.
257 procedure Set_Libraries is
258 new For_Every_Project_Imported (Boolean, Set_Library_For);
259 -- Add the -L and -l switches to the linker for all of the library
262 procedure Test_If_Relative_Path
263 (Switch : in out String_Access;
265 -- Test if Switch is a relative search path switch. If it is and it
266 -- includes directory information, prepend the path with Parent. This
267 -- subprogram is only called when using project files.
269 --------------------------
270 -- Add_To_Carg_Switches --
271 --------------------------
273 procedure Add_To_Carg_Switches (Switch : String_Access) is
275 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
277 if Carg_Switches.Last = 0 then
278 Carg_Switches.Increment_Last;
279 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
282 Carg_Switches.Increment_Last;
283 Carg_Switches.Table (Carg_Switches.Last) := Switch;
284 end Add_To_Carg_Switches;
286 ---------------------------
287 -- Add_To_Rules_Switches --
288 ---------------------------
290 procedure Add_To_Rules_Switches (Switch : String_Access) is
292 -- If the Rules_Switches table is empty, put "-rules" at the beginning
294 if Rules_Switches.Last = 0 then
295 Rules_Switches.Increment_Last;
296 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
299 Rules_Switches.Increment_Last;
300 Rules_Switches.Table (Rules_Switches.Last) := Switch;
301 end Add_To_Rules_Switches;
307 procedure Check_Files is
308 Add_Sources : Boolean := True;
309 Unit : Prj.Unit_Index;
310 Subunit : Boolean := False;
311 FD : File_Descriptor := Invalid_FD;
316 -- Check if there is at least one argument that is not a switch
318 for Index in 1 .. Last_Switches.Last loop
319 if Last_Switches.Table (Index) (1) /= '-' then
320 Add_Sources := False;
325 -- If all arguments were switches, add the path names of all the sources
326 -- of the main project.
330 -- For gnatcheck, gnatpp and gnatmetric , create a temporary file and
331 -- put the list of sources in it.
333 if The_Command = Check or else
334 The_Command = Pretty or else
337 Tempdir.Create_Temp_File (FD, Temp_File_Name);
338 Last_Switches.Increment_Last;
339 Last_Switches.Table (Last_Switches.Last) :=
340 new String'("-files=" & Get_Name_String (Temp_File_Name));
347 -- Gnatstack needs to add the .ci file for the binder generated
348 -- files corresponding to all of the library projects and main
349 -- units belonging to the application.
351 if The_Command = Stack then
352 Proj := Project_Tree.Projects;
353 while Proj /= null loop
354 if Check_Project (Proj.Project, Project) then
356 Main : String_List_Id;
357 File : String_Access;
360 -- Include binder generated files for main programs
362 Main := Proj.Project.Mains;
363 while Main /= Nil_String loop
367 (Proj.Project.Object_Directory.Name) &
368 Directory_Separator &
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) &
395 Directory_Separator &
397 Get_Name_String (Proj.Project.Library_Name) &
400 if Is_Regular_File (File.all) then
401 Last_Switches.Increment_Last;
402 Last_Switches.Table (Last_Switches.Last) := File;
412 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
413 while Unit /= No_Unit_Index loop
415 -- For gnatls, we only need to put the library units, body or
416 -- spec, but not the subunits.
418 if The_Command = List then
419 if Unit.File_Names (Impl) /= null
420 and then not Unit.File_Names (Impl).Locally_Removed
422 -- There is a body, check if it is for this project
424 if All_Projects or else
425 Unit.File_Names (Impl).Project = Project
429 if Unit.File_Names (Spec) = null
430 or else Unit.File_Names (Spec).Locally_Removed
432 -- We have a body with no spec: we need to check if
433 -- this is a subunit, because gnatls will complain
437 Src_Ind : constant Source_File_Index :=
438 Sinput.P.Load_Project_File
444 Sinput.P.Source_File_Is_Subunit (Src_Ind);
449 Last_Switches.Increment_Last;
450 Last_Switches.Table (Last_Switches.Last) :=
454 (Impl).Display_File));
458 elsif Unit.File_Names (Spec) /= null
459 and then not Unit.File_Names (Spec).Locally_Removed
461 -- We have a spec with no body. Check if it is for this
464 if All_Projects or else
465 Unit.File_Names (Spec).Project = Project
467 Last_Switches.Increment_Last;
468 Last_Switches.Table (Last_Switches.Last) :=
469 new String'(Get_Name_String
470 (Unit.File_Names (Spec).Display_File));
474 -- For gnatstack, we put the .ci files corresponding to the
475 -- different units, including the binder generated files. We
476 -- only need to do that for the library units, body or spec,
477 -- but not the subunits.
479 elsif The_Command = Stack then
480 if Unit.File_Names (Impl) /= null
481 and then not Unit.File_Names (Impl).Locally_Removed
483 -- There is a body. Check if .ci files for this project
487 (Unit.File_Names (Impl).Project, Project)
491 if Unit.File_Names (Spec) = null
492 or else Unit.File_Names (Spec).Locally_Removed
494 -- We have a body with no spec: we need to check
495 -- if this is a subunit, because .ci files are not
496 -- generated for subunits.
499 Src_Ind : constant Source_File_Index :=
500 Sinput.P.Load_Project_File
506 Sinput.P.Source_File_Is_Subunit (Src_Ind);
511 Last_Switches.Increment_Last;
512 Last_Switches.Table (Last_Switches.Last) :=
516 (Impl).Project. Object_Directory.Name) &
517 Directory_Separator &
520 (Unit.File_Names (Impl).Display_File),
525 elsif Unit.File_Names (Spec) /= null
526 and then not Unit.File_Names (Spec).Locally_Removed
528 -- Spec with no body, check if it is for this project
531 (Unit.File_Names (Spec).Project, Project)
533 Last_Switches.Increment_Last;
534 Last_Switches.Table (Last_Switches.Last) :=
538 (Spec).Project. Object_Directory.Name) &
541 (Get_Name_String (Unit.File_Names (Spec).File),
547 -- For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
548 -- sources of the project, or of all projects if -U was
551 for Kind in Spec_Or_Body loop
552 if Unit.File_Names (Kind) /= null
553 and then Check_Project
554 (Unit.File_Names (Kind).Project, Project)
555 and then not Unit.File_Names (Kind).Locally_Removed
558 (Unit.File_Names (Kind).Path.Display_Name);
560 if FD /= Invalid_FD then
561 Name_Len := Name_Len + 1;
562 Name_Buffer (Name_Len) := ASCII.LF;
564 Write (FD, Name_Buffer (1)'Address, Name_Len);
566 if Status /= Name_Len then
567 Osint.Fail ("disk full");
571 Last_Switches.Increment_Last;
572 Last_Switches.Table (Last_Switches.Last) :=
573 new String'(Get_Name_String
575 (Kind).Path.Display_Name));
581 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
585 if FD /= Invalid_FD then
589 Osint.Fail ("disk full");
599 function Check_Project
600 (Project : Project_Id;
601 Root_Project : Project_Id) return Boolean
606 if Project = No_Project then
609 elsif All_Projects or Project = Root_Project then
612 elsif The_Command = Metric then
613 Proj := Root_Project;
614 while Proj.Extends /= No_Project loop
615 if Project = Proj.Extends then
619 Proj := Proj.Extends;
626 -------------------------------
627 -- Check_Relative_Executable --
628 -------------------------------
630 procedure Check_Relative_Executable (Name : in out String_Access) is
631 Exec_File_Name : constant String := Name.all;
634 if not Is_Absolute_Path (Exec_File_Name) then
635 for Index in Exec_File_Name'Range loop
636 if Exec_File_Name (Index) = Directory_Separator then
637 Fail ("relative executable (""" &
639 """) with directory part not allowed " &
640 "when using project files");
644 Get_Name_String (Project.Exec_Directory.Name);
646 if Name_Buffer (Name_Len) /= Directory_Separator then
647 Name_Len := Name_Len + 1;
648 Name_Buffer (Name_Len) := Directory_Separator;
651 Name_Buffer (Name_Len + 1 ..
652 Name_Len + Exec_File_Name'Length) :=
654 Name_Len := Name_Len + Exec_File_Name'Length;
655 Name := new String'(Name_Buffer (1 .. Name_Len));
657 end Check_Relative_Executable;
659 --------------------------------
660 -- Configuration_Pragmas_File --
661 --------------------------------
663 function Configuration_Pragmas_File return Path_Name_Type is
665 Prj.Env.Create_Config_Pragmas_File (Project, Project_Tree);
666 return Project.Config_File_Name;
667 end Configuration_Pragmas_File;
669 ------------------------------
670 -- Delete_Temp_Config_Files --
671 ------------------------------
673 procedure Delete_Temp_Config_Files is
676 pragma Warnings (Off, Success);
679 -- This should only be called if Keep_Temporary_Files is False
681 pragma Assert (not Keep_Temporary_Files);
683 if Project /= No_Project then
684 Proj := Project_Tree.Projects;
685 while Proj /= null loop
686 if Proj.Project.Config_File_Temp then
688 Output.Write_Str ("Deleting temp configuration file """);
690 (Get_Name_String (Proj.Project.Config_File_Name));
691 Output.Write_Line ("""");
695 (Name => Get_Name_String (Proj.Project.Config_File_Name),
703 -- If a temporary text file that contains a list of files for a tool
704 -- has been created, delete this temporary file.
706 if Temp_File_Name /= No_Path then
707 Delete_File (Get_Name_String (Temp_File_Name), Success);
709 end Delete_Temp_Config_Files;
715 procedure Get_Closure is
716 Args : constant Argument_List :=
717 (1 => new String'("-q"),
718 2 => new String'("-b"),
719 3 => new String'("-P"),
722 6 => new String'("-bargs"),
723 7 => new String'("-R"),
724 8 => new String'("-Z"));
725 -- Arguments for the invocation of gnatmake which are added to the
726 -- Last_Arguments list by this procedure.
728 FD : File_Descriptor;
729 -- File descriptor for the temp file that will get the output of the
730 -- invocation of gnatmake.
732 Name : Path_Name_Type;
733 -- Path of the file FD
735 GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
738 GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
741 Return_Code : Integer;
744 pragma Warnings (Off, Unused);
746 File : Ada.Text_IO.File_Type;
747 Line : String (1 .. 250);
749 -- Used to read file if there is an error, it is good enough to display
750 -- just 250 characters if the first line of the file is very long.
753 Path : Path_Name_Type;
756 if GN_Path = null then
757 Put_Line (Standard_Error, "could not locate " & GN_Name);
761 -- Create the temp file
763 Tempdir.Create_Temp_File (FD, Name);
765 -- And close it, because on VMS Spawn with a file descriptor created
766 -- with Create_Temp_File does not redirect output.
770 -- Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
773 (Program_Name => GN_Path.all,
775 Output_File => Get_Name_String (Name),
777 Return_Code => Return_Code,
782 -- Read the output of the invocation of gnatmake
784 Open (File, In_File, Get_Name_String (Name));
786 -- If it was unsuccessful, display the first line in the file and exit
789 if Return_Code /= 0 then
790 Get_Line (File, Line, Last);
792 if not Keep_Temporary_Files then
798 Put_Line (Standard_Error, Line (1 .. Last));
800 (Standard_Error, "could not get closure of " & ASIS_Main.all);
804 -- Get each file name in the file, find its path and add it the
805 -- list of arguments.
807 while not End_Of_File (File) loop
808 Get_Line (File, Line, Last);
811 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
812 while Unit /= No_Unit_Index loop
813 if Unit.File_Names (Spec) /= null
815 Get_Name_String (Unit.File_Names (Spec).File) =
818 Path := Unit.File_Names (Spec).Path.Name;
821 elsif Unit.File_Names (Impl) /= null
823 Get_Name_String (Unit.File_Names (Impl).File) =
826 Path := Unit.File_Names (Impl).Path.Name;
830 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
833 Last_Switches.Increment_Last;
835 if Path /= No_Path then
836 Last_Switches.Table (Last_Switches.Last) :=
837 new String'(Get_Name_String (Path));
840 Last_Switches.Table (Last_Switches.Last) :=
841 new String'(Line (1 .. Last));
845 if not Keep_Temporary_Files then
857 function Index (Char : Character; Str : String) return Natural is
859 for Index in Str'Range loop
860 if Str (Index) = Char then
872 procedure Process_Link is
873 Look_For_Executable : Boolean := True;
874 Libraries_Present : Boolean := False;
875 Path_Option : constant String_Access :=
876 MLib.Linker_Library_Path_Option;
877 Prj : Project_Id := Project;
880 Skip_Executable : Boolean := False;
883 -- Add the default search directories, to be able to find
884 -- libgnat in call to MLib.Utl.Lib_Directory.
886 Add_Default_Search_Dirs;
888 Library_Paths.Set_Last (0);
890 -- Check if there are library project files
892 if MLib.Tgt.Support_For_Libraries /= None then
893 Set_Libraries (Project, Libraries_Present);
896 -- If there are, add the necessary additional switches
898 if Libraries_Present then
900 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
902 Last_Switches.Increment_Last;
903 Last_Switches.Table (Last_Switches.Last) :=
904 new String'("-L" & MLib.Utl.Lib_Directory);
905 Last_Switches.Increment_Last;
906 Last_Switches.Table (Last_Switches.Last) :=
907 new String'("-lgnarl");
908 Last_Switches.Increment_Last;
909 Last_Switches.Table (Last_Switches.Last) :=
910 new String'("-lgnat");
912 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
913 -- equivalent) with all the library dirs plus the standard GNAT
916 if Path_Option /= null then
918 Option : String_Access;
919 Length : Natural := Path_Option'Length;
923 if MLib.Separate_Run_Path_Options then
925 -- We are going to create one switch of the form
926 -- "-Wl,-rpath,dir_N" for each directory to consider.
928 -- One switch for each library directory
931 Library_Paths.First .. Library_Paths.Last
933 Last_Switches.Increment_Last;
935 (Last_Switches.Last) := new String'
937 Last_Switches.Table (Index).all);
940 -- One switch for the standard GNAT library dir
942 Last_Switches.Increment_Last;
944 (Last_Switches.Last) := new String'
945 (Path_Option.all & MLib.Utl.Lib_Directory);
948 -- First, compute the exact length for the switch
951 Library_Paths.First .. Library_Paths.Last
953 -- Add the length of the library dir plus one for the
954 -- directory separator.
958 Library_Paths.Table (Index)'Length + 1;
961 -- Finally, add the length of the standard GNAT library dir
963 Length := Length + MLib.Utl.Lib_Directory'Length;
964 Option := new String (1 .. Length);
965 Option (1 .. Path_Option'Length) := Path_Option.all;
966 Current := Path_Option'Length;
968 -- Put each library dir followed by a dir separator
971 Library_Paths.First .. Library_Paths.Last
976 Library_Paths.Table (Index)'Length) :=
977 Library_Paths.Table (Index).all;
980 Library_Paths.Table (Index)'Length + 1;
981 Option (Current) := Path_Separator;
984 -- Finally put the standard GNAT library dir
988 Current + MLib.Utl.Lib_Directory'Length) :=
989 MLib.Utl.Lib_Directory;
991 -- And add the switch to the last switches
993 Last_Switches.Increment_Last;
994 Last_Switches.Table (Last_Switches.Last) :=
1001 -- Check if the first ALI file specified can be found, either in the
1002 -- object directory of the main project or in an object directory of a
1003 -- project file extended by the main project. If the ALI file can be
1004 -- found, replace its name with its absolute path.
1006 Skip_Executable := False;
1008 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1010 -- If we have an executable just reset the flag
1012 if Skip_Executable then
1013 Skip_Executable := False;
1015 -- If -o, set flag so that next switch is not processed
1017 elsif Last_Switches.Table (J).all = "-o" then
1018 Skip_Executable := True;
1024 Switch : constant String :=
1025 Last_Switches.Table (J).all;
1026 ALI_File : constant String (1 .. Switch'Length + 4) :=
1029 Test_Existence : Boolean := False;
1032 Last := Switch'Length;
1034 -- Skip real switches
1036 if Switch'Length /= 0
1037 and then Switch (Switch'First) /= '-'
1039 -- Append ".ali" if file name does not end with it
1041 if Switch'Length <= 4
1042 or else Switch (Switch'Last - 3 .. Switch'Last)
1045 Last := ALI_File'Last;
1048 -- If file name includes directory information, stop if ALI
1051 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1052 Test_Existence := True;
1055 for K in Switch'Range loop
1056 if Switch (K) = '/' or else
1057 Switch (K) = Directory_Separator
1059 Test_Existence := True;
1065 if Test_Existence then
1066 if Is_Regular_File (ALI_File (1 .. Last)) then
1070 -- Look in object directories if ALI file exists
1075 Dir : constant String :=
1076 Get_Name_String (Prj.Object_Directory.Name);
1080 Directory_Separator &
1081 ALI_File (1 .. Last))
1083 -- We have found the correct project, so we
1084 -- replace the file with the absolute path.
1086 Last_Switches.Table (J) :=
1088 (Dir & Directory_Separator &
1089 ALI_File (1 .. Last));
1097 -- Go to the project being extended, if any
1100 exit Project_Loop when Prj = No_Project;
1101 end loop Project_Loop;
1106 end loop Switch_Loop;
1108 -- If a relative path output file has been specified, we add the exec
1111 for J in reverse 1 .. Last_Switches.Last - 1 loop
1112 if Last_Switches.Table (J).all = "-o" then
1113 Check_Relative_Executable
1114 (Name => Last_Switches.Table (J + 1));
1115 Look_For_Executable := False;
1120 if Look_For_Executable then
1121 for J in reverse 1 .. First_Switches.Last - 1 loop
1122 if First_Switches.Table (J).all = "-o" then
1123 Look_For_Executable := False;
1124 Check_Relative_Executable
1125 (Name => First_Switches.Table (J + 1));
1131 -- If no executable is specified, then find the name of the first ALI
1132 -- file on the command line and issue a -o switch with the absolute path
1133 -- of the executable in the exec directory.
1135 if Look_For_Executable then
1136 for J in 1 .. Last_Switches.Last loop
1137 Arg := Last_Switches.Table (J);
1140 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1142 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1144 Last := Arg'Last - 4;
1146 elsif Is_Regular_File (Arg.all & ".ali") then
1151 Last_Switches.Increment_Last;
1152 Last_Switches.Table (Last_Switches.Last) :=
1154 Get_Name_String (Project.Exec_Directory.Name);
1155 Last_Switches.Increment_Last;
1156 Last_Switches.Table (Last_Switches.Last) :=
1157 new String'(Name_Buffer (1 .. Name_Len) &
1158 Directory_Separator &
1160 (Base_Name (Arg (Arg'First .. Last))));
1168 ---------------------
1169 -- Set_Library_For --
1170 ---------------------
1172 procedure Set_Library_For
1173 (Project : Project_Id;
1174 Libraries_Present : in out Boolean)
1176 Path_Option : constant String_Access :=
1177 MLib.Linker_Library_Path_Option;
1180 -- Case of library project
1182 if Project.Library then
1183 Libraries_Present := True;
1185 -- Add the -L switch
1187 Last_Switches.Increment_Last;
1188 Last_Switches.Table (Last_Switches.Last) :=
1189 new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1191 -- Add the -l switch
1193 Last_Switches.Increment_Last;
1194 Last_Switches.Table (Last_Switches.Last) :=
1195 new String'("-l" & Get_Name_String (Project.Library_Name));
1197 -- Add the directory to table Library_Paths, to be processed later
1198 -- if library is not static and if Path_Option is not null.
1200 if Project.Library_Kind /= Static
1201 and then Path_Option /= null
1203 Library_Paths.Increment_Last;
1204 Library_Paths.Table (Library_Paths.Last) :=
1205 new String'(Get_Name_String (Project.Library_Dir.Name));
1208 end Set_Library_For;
1210 ---------------------------
1211 -- Test_If_Relative_Path --
1212 ---------------------------
1214 procedure Test_If_Relative_Path
1215 (Switch : in out String_Access;
1219 Makeutl.Test_If_Relative_Path
1220 (Switch, Parent, Including_Non_Switch => False, Including_RTS => True);
1221 end Test_If_Relative_Path;
1227 procedure Non_VMS_Usage is
1231 Put_Line ("List of available commands");
1234 for C in Command_List'Range loop
1235 if not Command_List (C).VMS_Only then
1236 if Targparm.AAMP_On_Target then
1242 Put (To_Lower (Command_List (C).Cname.all));
1245 -- Never call gnatstack with a prefix
1248 Put (Command_List (C).Unixcmd.all);
1250 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1254 Sws : Argument_List_Access renames Command_List (C).Unixsws;
1257 for J in Sws'Range loop
1269 Put_Line ("Commands find, list, metric, pretty, stack, stub and xref " &
1270 "accept project file switches -vPx, -Pprj and -Xnam=val");
1274 -------------------------------------
1275 -- Start of processing for GNATCmd --
1276 -------------------------------------
1286 Prj.Initialize (Project_Tree);
1289 Last_Switches.Set_Last (0);
1291 First_Switches.Init;
1292 First_Switches.Set_Last (0);
1294 Carg_Switches.Set_Last (0);
1295 Rules_Switches.Init;
1296 Rules_Switches.Set_Last (0);
1298 VMS_Conv.Initialize;
1300 Set_Mode (Ada_Only);
1302 -- Add the default search directories, to be able to find system.ads in the
1303 -- subsequent call to Targparm.Get_Target_Parameters.
1305 Add_Default_Search_Dirs;
1307 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1308 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1310 Targparm.Get_Target_Parameters;
1312 -- Add the directory where the GNAT driver is invoked in front of the path,
1313 -- if the GNAT driver is invoked with directory information. Do not do this
1314 -- for VMS, where the notion of path does not really exist.
1318 Command : constant String := Command_Name;
1321 for Index in reverse Command'Range loop
1322 if Command (Index) = Directory_Separator then
1324 Absolute_Dir : constant String :=
1326 (Command (Command'First .. Index));
1328 PATH : constant String :=
1331 Getenv ("PATH").all;
1334 Setenv ("PATH", PATH);
1343 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1344 -- filenames and pathnames to Unix style.
1347 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1349 VMS_Conversion (The_Command);
1351 B_Start := new String'("b__");
1353 -- If not on VMS, scan the command line directly
1356 if Argument_Count = 0 then
1362 if Argument_Count > Command_Arg
1363 and then Argument (Command_Arg) = "-v"
1365 Verbose_Mode := True;
1366 Command_Arg := Command_Arg + 1;
1368 elsif Argument_Count > Command_Arg
1369 and then Argument (Command_Arg) = "-dn"
1371 Keep_Temporary_Files := True;
1372 Command_Arg := Command_Arg + 1;
1379 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1381 if Command_List (The_Command).VMS_Only then
1385 & Command_List (The_Command).Cname.all
1386 & """ can only be used on VMS");
1390 when Constraint_Error =>
1392 -- Check if it is an alternate command
1395 Alternate : Alternate_Command;
1398 Alternate := Alternate_Command'Value
1399 (Argument (Command_Arg));
1400 The_Command := Corresponding_To (Alternate);
1403 when Constraint_Error =>
1405 Fail ("Unknown command: " & Argument (Command_Arg));
1409 -- Get the arguments from the command line and from the eventual
1410 -- argument file(s) specified on the command line.
1412 for Arg in Command_Arg + 1 .. Argument_Count loop
1414 The_Arg : constant String := Argument (Arg);
1417 -- Check if an argument file is specified
1419 if The_Arg (The_Arg'First) = '@' then
1421 Arg_File : Ada.Text_IO.File_Type;
1422 Line : String (1 .. 256);
1426 -- Open the file and fail if the file cannot be found
1431 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1436 (Standard_Error, "Cannot open argument file """);
1439 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1441 Put_Line (Standard_Error, """");
1445 -- Read line by line and put the content of each non-
1446 -- empty line in the Last_Switches table.
1448 while not End_Of_File (Arg_File) loop
1449 Get_Line (Arg_File, Line, Last);
1452 Last_Switches.Increment_Last;
1453 Last_Switches.Table (Last_Switches.Last) :=
1454 new String'(Line (1 .. Last));
1462 -- It is not an argument file; just put the argument in
1463 -- the Last_Switches table.
1465 Last_Switches.Increment_Last;
1466 Last_Switches.Table (Last_Switches.Last) :=
1467 new String'(The_Arg);
1475 Program : String_Access;
1476 Exec_Path : String_Access;
1479 if The_Command = Stack then
1480 -- Never call gnatstack with a prefix
1482 Program := new String'(Command_List (The_Command).Unixcmd.all);
1486 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1489 -- Locate the executable for the command
1491 Exec_Path := Locate_Exec_On_Path (Program.all);
1493 if Exec_Path = null then
1494 Put_Line (Standard_Error, "could not locate " & Program.all);
1498 -- If there are switches for the executable, put them as first switches
1500 if Command_List (The_Command).Unixsws /= null then
1501 for J in Command_List (The_Command).Unixsws'Range loop
1502 First_Switches.Increment_Last;
1503 First_Switches.Table (First_Switches.Last) :=
1504 Command_List (The_Command).Unixsws (J);
1508 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, PRETTY, STACK, STUB,
1509 -- METRIC ad XREF, look for project file related switches.
1511 if The_Command = Bind
1512 or else The_Command = Check
1513 or else The_Command = Sync
1514 or else The_Command = Elim
1515 or else The_Command = Find
1516 or else The_Command = Link
1517 or else The_Command = List
1518 or else The_Command = Xref
1519 or else The_Command = Pretty
1520 or else The_Command = Stack
1521 or else The_Command = Stub
1522 or else The_Command = Metric
1526 Tool_Package_Name := Name_Binder;
1527 Packages_To_Check := Packages_To_Check_By_Binder;
1529 Tool_Package_Name := Name_Check;
1530 Packages_To_Check := Packages_To_Check_By_Check;
1532 Tool_Package_Name := Name_Synchronize;
1533 Packages_To_Check := Packages_To_Check_By_Sync;
1535 Tool_Package_Name := Name_Eliminate;
1536 Packages_To_Check := Packages_To_Check_By_Eliminate;
1538 Tool_Package_Name := Name_Finder;
1539 Packages_To_Check := Packages_To_Check_By_Finder;
1541 Tool_Package_Name := Name_Linker;
1542 Packages_To_Check := Packages_To_Check_By_Linker;
1544 Tool_Package_Name := Name_Gnatls;
1545 Packages_To_Check := Packages_To_Check_By_Gnatls;
1547 Tool_Package_Name := Name_Metrics;
1548 Packages_To_Check := Packages_To_Check_By_Metric;
1550 Tool_Package_Name := Name_Pretty_Printer;
1551 Packages_To_Check := Packages_To_Check_By_Pretty;
1553 Tool_Package_Name := Name_Stack;
1554 Packages_To_Check := Packages_To_Check_By_Stack;
1556 Tool_Package_Name := Name_Gnatstub;
1557 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1559 Tool_Package_Name := Name_Cross_Reference;
1560 Packages_To_Check := Packages_To_Check_By_Xref;
1565 -- Check that the switches are consistent. Detect project file
1566 -- related switches.
1570 Arg_Num : Positive := 1;
1571 Argv : String_Access;
1573 procedure Remove_Switch (Num : Positive);
1574 -- Remove a project related switch from table Last_Switches
1580 procedure Remove_Switch (Num : Positive) is
1582 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1583 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1584 Last_Switches.Decrement_Last;
1587 -- Start of processing for Inspect_Switches
1590 while Arg_Num <= Last_Switches.Last loop
1591 Argv := Last_Switches.Table (Arg_Num);
1593 if Argv (Argv'First) = '-' then
1594 if Argv'Length = 1 then
1596 ("switch character cannot be followed by a blank");
1599 -- The two style project files (-p and -P) cannot be used
1602 if (The_Command = Find or else The_Command = Xref)
1603 and then Argv (2) = 'p'
1605 Old_Project_File_Used := True;
1606 if Project_File /= null then
1607 Fail ("-P and -p cannot be used together");
1611 -- --subdirs=... Specify Subdirs
1613 if Argv'Length > Makeutl.Subdirs_Option'Length and then
1616 Argv'First + Makeutl.Subdirs_Option'Length - 1) =
1617 Makeutl.Subdirs_Option
1622 (Argv'First + Makeutl.Subdirs_Option'Length ..
1625 Remove_Switch (Arg_Num);
1627 -- -aPdir Add dir to the project search path
1629 elsif Argv'Length > 3
1630 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1632 Add_Search_Project_Directory
1633 (Argv (Argv'First + 3 .. Argv'Last));
1635 Remove_Switch (Arg_Num);
1637 -- -eL Follow links for files
1639 elsif Argv.all = "-eL" then
1640 Follow_Links_For_Files := True;
1642 Remove_Switch (Arg_Num);
1644 -- -vPx Specify verbosity while parsing project files
1646 elsif Argv'Length = 4
1647 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1649 case Argv (Argv'Last) is
1651 Current_Verbosity := Prj.Default;
1653 Current_Verbosity := Prj.Medium;
1655 Current_Verbosity := Prj.High;
1657 Fail ("Invalid switch: " & Argv.all);
1660 Remove_Switch (Arg_Num);
1662 -- -Pproject_file Specify project file to be used
1664 elsif Argv (Argv'First + 1) = 'P' then
1666 -- Only one -P switch can be used
1668 if Project_File /= null then
1671 & ": second project file forbidden (first is """
1675 -- The two style project files (-p and -P) cannot be
1678 elsif Old_Project_File_Used then
1679 Fail ("-p and -P cannot be used together");
1681 elsif Argv'Length = 2 then
1683 -- There is space between -P and the project file
1684 -- name. -P cannot be the last option.
1686 if Arg_Num = Last_Switches.Last then
1687 Fail ("project file name missing after -P");
1690 Remove_Switch (Arg_Num);
1691 Argv := Last_Switches.Table (Arg_Num);
1693 -- After -P, there must be a project file name,
1694 -- not another switch.
1696 if Argv (Argv'First) = '-' then
1697 Fail ("project file name missing after -P");
1700 Project_File := new String'(Argv.all);
1705 -- No space between -P and project file name
1708 new String'(Argv (Argv'First + 2 .. Argv'Last));
1711 Remove_Switch (Arg_Num);
1713 -- -Xexternal=value Specify an external reference to be
1714 -- used in project files
1716 elsif Argv'Length >= 5
1717 and then Argv (Argv'First + 1) = 'X'
1720 Equal_Pos : constant Natural :=
1723 Argv (Argv'First + 2 .. Argv'Last));
1725 if Equal_Pos >= Argv'First + 3 and then
1726 Equal_Pos /= Argv'Last then
1727 Add (External_Name =>
1728 Argv (Argv'First + 2 .. Equal_Pos - 1),
1729 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1733 & " is not a valid external assignment.");
1737 Remove_Switch (Arg_Num);
1740 (The_Command = Check or else
1741 The_Command = Sync or else
1742 The_Command = Pretty or else
1743 The_Command = Metric or else
1744 The_Command = Stack or else
1746 and then Argv'Length = 2
1747 and then Argv (2) = 'U'
1749 All_Projects := True;
1750 Remove_Switch (Arg_Num);
1753 Arg_Num := Arg_Num + 1;
1756 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1757 or else The_Command = Sync
1758 or else The_Command = Metric
1759 or else The_Command = Pretty)
1760 and then Project_File /= null
1761 and then All_Projects
1763 if ASIS_Main /= null then
1764 Fail ("cannot specify more than one main after -U");
1767 Remove_Switch (Arg_Num);
1771 Arg_Num := Arg_Num + 1;
1774 end Inspect_Switches;
1777 -- If there is a project file specified, parse it, get the switches
1778 -- for the tool and setup PATH environment variables.
1780 if Project_File /= null then
1781 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1784 (Project => Project,
1785 In_Tree => Project_Tree,
1786 Project_File_Name => Project_File.all,
1787 Packages_To_Check => Packages_To_Check,
1788 Is_Config_File => False);
1790 if Project = Prj.No_Project then
1791 Fail ("""" & Project_File.all & """ processing failed");
1794 -- Check if a package with the name of the tool is in the project
1795 -- file and if there is one, get the switches, if any, and scan them.
1798 Pkg : constant Prj.Package_Id :=
1800 (Name => Tool_Package_Name,
1801 In_Packages => Project.Decl.Packages,
1802 In_Tree => Project_Tree);
1804 Element : Package_Element;
1806 Default_Switches_Array : Array_Element_Id;
1808 The_Switches : Prj.Variable_Value;
1809 Current : Prj.String_List_Id;
1810 The_String : String_Element;
1813 if Pkg /= No_Package then
1814 Element := Project_Tree.Packages.Table (Pkg);
1816 -- Packages Gnatls and Gnatstack have a single attribute
1817 -- Switches, that is not an associative array.
1819 if The_Command = List or else The_Command = Stack then
1822 (Variable_Name => Snames.Name_Switches,
1823 In_Variables => Element.Decl.Attributes,
1824 In_Tree => Project_Tree);
1826 -- Packages Binder (for gnatbind), Cross_Reference (for
1827 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1828 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1829 -- (for gnatcheck), and Metric (for gnatmetric) have an
1830 -- attributed Switches, an associative array, indexed by the
1831 -- name of the file.
1833 -- They also have an attribute Default_Switches, indexed by the
1834 -- name of the programming language.
1837 if The_Switches.Kind = Prj.Undefined then
1838 Default_Switches_Array :=
1840 (Name => Name_Default_Switches,
1841 In_Arrays => Element.Decl.Arrays,
1842 In_Tree => Project_Tree);
1843 The_Switches := Prj.Util.Value_Of
1846 In_Array => Default_Switches_Array,
1847 In_Tree => Project_Tree);
1851 -- If there are switches specified in the package of the
1852 -- project file corresponding to the tool, scan them.
1854 case The_Switches.Kind is
1855 when Prj.Undefined =>
1860 Switch : constant String :=
1861 Get_Name_String (The_Switches.Value);
1864 if Switch'Length > 0 then
1865 First_Switches.Increment_Last;
1866 First_Switches.Table (First_Switches.Last) :=
1867 new String'(Switch);
1872 Current := The_Switches.Values;
1873 while Current /= Prj.Nil_String loop
1874 The_String := Project_Tree.String_Elements.
1878 Switch : constant String :=
1879 Get_Name_String (The_String.Value);
1882 if Switch'Length > 0 then
1883 First_Switches.Increment_Last;
1884 First_Switches.Table (First_Switches.Last) :=
1885 new String'(Switch);
1889 Current := The_String.Next;
1895 if The_Command = Bind
1896 or else The_Command = Link
1897 or else The_Command = Elim
1899 Change_Dir (Get_Name_String (Project.Object_Directory.Name));
1902 -- Set up the env vars for project path files
1904 Prj.Env.Set_Ada_Paths
1905 (Project, Project_Tree, Including_Libraries => False);
1907 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
1908 -- a configuration pragmas file, if necessary.
1910 if The_Command = Pretty
1911 or else The_Command = Metric
1912 or else The_Command = Stub
1913 or else The_Command = Elim
1914 or else The_Command = Check
1915 or else The_Command = Sync
1917 -- If there are switches in package Compiler, put them in the
1918 -- Carg_Switches table.
1921 Pkg : constant Prj.Package_Id :=
1923 (Name => Name_Compiler,
1924 In_Packages => Project.Decl.Packages,
1925 In_Tree => Project_Tree);
1927 Element : Package_Element;
1929 Default_Switches_Array : Array_Element_Id;
1931 The_Switches : Prj.Variable_Value;
1932 Current : Prj.String_List_Id;
1933 The_String : String_Element;
1936 if Pkg /= No_Package then
1937 Element := Project_Tree.Packages.Table (Pkg);
1939 Default_Switches_Array :=
1941 (Name => Name_Default_Switches,
1942 In_Arrays => Element.Decl.Arrays,
1943 In_Tree => Project_Tree);
1944 The_Switches := Prj.Util.Value_Of
1947 In_Array => Default_Switches_Array,
1948 In_Tree => Project_Tree);
1950 -- If there are switches specified in the package of the
1951 -- project file corresponding to the tool, scan them.
1953 case The_Switches.Kind is
1954 when Prj.Undefined =>
1959 Switch : constant String :=
1960 Get_Name_String (The_Switches.Value);
1962 if Switch'Length > 0 then
1963 Add_To_Carg_Switches (new String'(Switch));
1968 Current := The_Switches.Values;
1969 while Current /= Prj.Nil_String loop
1971 Project_Tree.String_Elements.Table (Current);
1974 Switch : constant String :=
1975 Get_Name_String (The_String.Value);
1977 if Switch'Length > 0 then
1978 Add_To_Carg_Switches (new String'(Switch));
1982 Current := The_String.Next;
1988 -- If -cargs is one of the switches, move the following switches
1989 -- to the Carg_Switches table.
1991 for J in 1 .. First_Switches.Last loop
1992 if First_Switches.Table (J).all = "-cargs" then
1998 -- Move the switches that are before -rules when the
1999 -- command is CHECK.
2002 while K <= First_Switches.Last
2004 (The_Command /= Check
2005 or else First_Switches.Table (K).all /= "-rules")
2007 Add_To_Carg_Switches (First_Switches.Table (K));
2011 if K > First_Switches.Last then
2012 First_Switches.Set_Last (J - 1);
2016 while K <= First_Switches.Last loop
2018 First_Switches.Table (Last) :=
2019 First_Switches.Table (K);
2023 First_Switches.Set_Last (Last);
2031 for J in 1 .. Last_Switches.Last loop
2032 if Last_Switches.Table (J).all = "-cargs" then
2038 -- Move the switches that are before -rules when the
2039 -- command is CHECK.
2042 while K <= Last_Switches.Last
2044 (The_Command /= Check
2046 Last_Switches.Table (K).all /= "-rules")
2048 Add_To_Carg_Switches (Last_Switches.Table (K));
2052 if K > Last_Switches.Last then
2053 Last_Switches.Set_Last (J - 1);
2057 while K <= Last_Switches.Last loop
2059 Last_Switches.Table (Last) :=
2060 Last_Switches.Table (K);
2064 Last_Switches.Set_Last (Last);
2073 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2076 if CP_File /= No_Path then
2077 if The_Command = Elim then
2078 First_Switches.Increment_Last;
2079 First_Switches.Table (First_Switches.Last) :=
2080 new String'("-C" & Get_Name_String (CP_File));
2083 Add_To_Carg_Switches
2084 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2090 if The_Command = Link then
2094 if The_Command = Link or The_Command = Bind then
2096 -- For files that are specified as relative paths with directory
2097 -- information, we convert them to absolute paths, with parent
2098 -- being the current working directory if specified on the command
2099 -- line and the project directory if specified in the project
2100 -- file. This is what gnatmake is doing for linker and binder
2103 for J in 1 .. Last_Switches.Last loop
2104 Test_If_Relative_Path
2105 (Last_Switches.Table (J), Current_Work_Dir);
2108 Get_Name_String (Project.Directory.Name);
2111 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2113 for J in 1 .. First_Switches.Last loop
2114 Test_If_Relative_Path
2115 (First_Switches.Table (J), Project_Dir);
2119 elsif The_Command = Stub then
2121 File_Index : Integer := 0;
2122 Dir_Index : Integer := 0;
2123 Last : constant Integer := Last_Switches.Last;
2124 Lang : constant Language_Ptr :=
2125 Get_Language_From_Name (Project, "ada");
2128 for Index in 1 .. Last loop
2129 if Last_Switches.Table (Index)
2130 (Last_Switches.Table (Index)'First) /= '-'
2132 File_Index := Index;
2137 -- If the naming scheme of the project file is not standard,
2138 -- and if the file name ends with the spec suffix, then
2139 -- indicate to gnatstub the name of the body file with
2142 if Lang.Config.Naming_Data.Body_Suffix /=
2143 Prj.Default_Ada_Spec_Suffix
2145 if File_Index /= 0 then
2147 Spec : constant String :=
2148 Base_Name (Last_Switches.Table (File_Index).all);
2149 Last : Natural := Spec'Last;
2152 Get_Name_String (Lang.Config.Naming_Data.Spec_Suffix);
2154 if Spec'Length > Name_Len
2155 and then Spec (Last - Name_Len + 1 .. Last) =
2156 Name_Buffer (1 .. Name_Len)
2158 Last := Last - Name_Len;
2160 (Lang.Config.Naming_Data.Body_Suffix);
2161 Last_Switches.Increment_Last;
2162 Last_Switches.Table (Last_Switches.Last) :=
2164 Last_Switches.Increment_Last;
2165 Last_Switches.Table (Last_Switches.Last) :=
2166 new String'(Spec (Spec'First .. Last) &
2167 Name_Buffer (1 .. Name_Len));
2173 -- Add the directory of the spec as the destination directory
2174 -- of the body, if there is no destination directory already
2177 if File_Index /= 0 then
2178 for Index in File_Index + 1 .. Last loop
2179 if Last_Switches.Table (Index)
2180 (Last_Switches.Table (Index)'First) /= '-'
2187 if Dir_Index = 0 then
2188 Last_Switches.Increment_Last;
2189 Last_Switches.Table (Last_Switches.Last) :=
2191 (Dir_Name (Last_Switches.Table (File_Index).all));
2197 -- For gnatmetric, the generated files should be put in the object
2198 -- directory. This must be the first switch, because it may be
2199 -- overridden by a switch in package Metrics in the project file or
2200 -- by a command line option. Note that we don't add the -d= switch
2201 -- if there is no object directory available.
2203 if The_Command = Metric
2204 and then Project.Object_Directory /= No_Path_Information
2206 First_Switches.Increment_Last;
2207 First_Switches.Table (2 .. First_Switches.Last) :=
2208 First_Switches.Table (1 .. First_Switches.Last - 1);
2209 First_Switches.Table (1) :=
2211 Get_Name_String (Project.Object_Directory.Name));
2214 -- For gnat check, -rules and the following switches need to be the
2215 -- last options. So, we move all these switches to table
2218 if The_Command = Check then
2221 -- Set to rank of options preceding "-rules"
2223 In_Rules_Switches : Boolean;
2224 -- Set to True when options "-rules" is found
2227 New_Last := First_Switches.Last;
2228 In_Rules_Switches := False;
2230 for J in 1 .. First_Switches.Last loop
2231 if In_Rules_Switches then
2232 Add_To_Rules_Switches (First_Switches.Table (J));
2234 elsif First_Switches.Table (J).all = "-rules" then
2236 In_Rules_Switches := True;
2240 if In_Rules_Switches then
2241 First_Switches.Set_Last (New_Last);
2244 New_Last := Last_Switches.Last;
2245 In_Rules_Switches := False;
2247 for J in 1 .. Last_Switches.Last loop
2248 if In_Rules_Switches then
2249 Add_To_Rules_Switches (Last_Switches.Table (J));
2251 elsif Last_Switches.Table (J).all = "-rules" then
2253 In_Rules_Switches := True;
2257 if In_Rules_Switches then
2258 Last_Switches.Set_Last (New_Last);
2263 -- For gnat check, sync, metric or pretty with -U + a main, get the
2264 -- list of sources from the closure and add them to the arguments.
2266 if ASIS_Main /= null then
2269 -- On VMS, set up the env var again for source dirs file. This is
2270 -- because the call to gnatmake has set this env var to another
2271 -- file that has now been deleted.
2273 if Hostparm.OpenVMS then
2275 -- First make sure that the recorded file names are empty
2277 Prj.Env.Initialize (Project_Tree);
2279 Prj.Env.Set_Ada_Paths
2280 (Project, Project_Tree, Including_Libraries => False);
2283 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2284 -- and gnat stack, if no file has been put on the command line, call
2285 -- tool with all the sources of the main project.
2287 elsif The_Command = Check or else
2288 The_Command = Sync or else
2289 The_Command = Pretty or else
2290 The_Command = Metric or else
2291 The_Command = List or else
2298 -- Gather all the arguments and invoke the executable
2301 The_Args : Argument_List
2302 (1 .. First_Switches.Last +
2303 Last_Switches.Last +
2304 Carg_Switches.Last +
2305 Rules_Switches.Last);
2306 Arg_Num : Natural := 0;
2309 for J in 1 .. First_Switches.Last loop
2310 Arg_Num := Arg_Num + 1;
2311 The_Args (Arg_Num) := First_Switches.Table (J);
2314 for J in 1 .. Last_Switches.Last loop
2315 Arg_Num := Arg_Num + 1;
2316 The_Args (Arg_Num) := Last_Switches.Table (J);
2319 for J in 1 .. Carg_Switches.Last loop
2320 Arg_Num := Arg_Num + 1;
2321 The_Args (Arg_Num) := Carg_Switches.Table (J);
2324 for J in 1 .. Rules_Switches.Last loop
2325 Arg_Num := Arg_Num + 1;
2326 The_Args (Arg_Num) := Rules_Switches.Table (J);
2329 -- If Display_Command is on, only display the generated command
2331 if Display_Command then
2332 Put (Standard_Error, "generated command -->");
2333 Put (Standard_Error, Exec_Path.all);
2335 for Arg in The_Args'Range loop
2336 Put (Standard_Error, " ");
2337 Put (Standard_Error, The_Args (Arg).all);
2340 Put (Standard_Error, "<--");
2341 New_Line (Standard_Error);
2345 if Verbose_Mode then
2346 Output.Write_Str (Exec_Path.all);
2348 for Arg in The_Args'Range loop
2349 Output.Write_Char (' ');
2350 Output.Write_Str (The_Args (Arg).all);
2357 Exit_Status (Spawn (Exec_Path.all, The_Args));
2364 if not Keep_Temporary_Files then
2365 Prj.Env.Delete_All_Path_Files (Project_Tree);
2366 Delete_Temp_Config_Files;
2369 Set_Exit_Status (Failure);
2372 if not Keep_Temporary_Files then
2373 Prj.Env.Delete_All_Path_Files (Project_Tree);
2374 Delete_Temp_Config_Files;
2377 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2378 -- return an understandable VMS exit status. However the exit status
2379 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2380 -- just a simple success or failure on VMS.
2382 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2383 Set_Exit_Status (Failure);
2385 Set_Exit_Status (My_Exit_Status);