1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2010, 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 Builder_String : constant SA := new String'("builder");
126 Compiler_String : constant SA := new String'("compiler");
127 Check_String : constant SA := new String'("check");
128 Synchronize_String : constant SA := new String'("synchronize");
129 Eliminate_String : constant SA := new String'("eliminate");
130 Finder_String : constant SA := new String'("finder");
131 Linker_String : constant SA := new String'("linker");
132 Gnatls_String : constant SA := new String'("gnatls");
133 Pretty_String : constant SA := new String'("pretty_printer");
134 Stack_String : constant SA := new String'("stack");
135 Gnatstub_String : constant SA := new String'("gnatstub");
136 Metric_String : constant SA := new String'("metrics");
137 Xref_String : constant SA := new String'("cross_reference");
139 Packages_To_Check_By_Binder : constant String_List_Access :=
140 new String_List'((Naming_String, Binder_String));
142 Packages_To_Check_By_Check : constant String_List_Access :=
144 ((Naming_String, Builder_String, Check_String, Compiler_String));
146 Packages_To_Check_By_Sync : constant String_List_Access :=
147 new String_List'((Naming_String, Synchronize_String, Compiler_String));
149 Packages_To_Check_By_Eliminate : constant String_List_Access :=
150 new String_List'((Naming_String, Eliminate_String, Compiler_String));
152 Packages_To_Check_By_Finder : constant String_List_Access :=
153 new String_List'((Naming_String, Finder_String));
155 Packages_To_Check_By_Linker : constant String_List_Access :=
156 new String_List'((Naming_String, Linker_String));
158 Packages_To_Check_By_Gnatls : constant String_List_Access :=
159 new String_List'((Naming_String, Gnatls_String));
161 Packages_To_Check_By_Pretty : constant String_List_Access :=
162 new String_List'((Naming_String, Pretty_String, Compiler_String));
164 Packages_To_Check_By_Stack : constant String_List_Access :=
165 new String_List'((Naming_String, Stack_String));
167 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
168 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
170 Packages_To_Check_By_Metric : constant String_List_Access :=
171 new String_List'((Naming_String, Metric_String, Compiler_String));
173 Packages_To_Check_By_Xref : constant String_List_Access :=
174 new String_List'((Naming_String, Xref_String));
176 Packages_To_Check : String_List_Access := Prj.All_Packages;
178 ----------------------------------
179 -- Declarations for GNATCMD use --
180 ----------------------------------
182 The_Command : Command_Type;
183 -- The command specified in the invocation of the GNAT driver
185 Command_Arg : Positive := 1;
186 -- The index of the command in the arguments of the GNAT driver
188 My_Exit_Status : Exit_Status := Success;
189 -- The exit status of the spawned tool. Used to set the correct VMS
192 Current_Work_Dir : constant String := Get_Current_Dir;
193 -- The path of the working directory
195 All_Projects : Boolean := False;
196 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
197 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
198 -- should be invoked for all sources of all projects.
200 -----------------------
201 -- Local Subprograms --
202 -----------------------
204 procedure Add_To_Carg_Switches (Switch : String_Access);
205 -- Add a switch to the Carg_Switches table. If it is the first one, put the
206 -- switch "-cargs" at the beginning of the table.
208 procedure Add_To_Rules_Switches (Switch : String_Access);
209 -- Add a switch to the Rules_Switches table. If it is the first one, put
210 -- the switch "-crules" at the beginning of the table.
212 procedure Check_Files;
213 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
214 -- project file is specified, without any file arguments and without a
215 -- switch -files=. If it is the case, invoke the GNAT tool with the proper
216 -- list of files, derived from the sources of the project.
218 function Check_Project
219 (Project : Project_Id;
220 Root_Project : Project_Id) return Boolean;
221 -- Returns True if Project = Root_Project or if we want to consider all
222 -- sources of all projects. For GNAT METRIC, also returns True if Project
223 -- is extended by Root_Project.
225 procedure Check_Relative_Executable (Name : in out String_Access);
226 -- Check if an executable is specified as a relative path. If it is, and
227 -- the path contains directory information, fail. Otherwise, prepend the
228 -- exec directory. This procedure is only used for GNAT LINK when a project
229 -- file is specified.
231 function Configuration_Pragmas_File return Path_Name_Type;
232 -- Return an argument, if there is a configuration pragmas file to be
233 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
234 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
237 function Mapping_File return Path_Name_Type;
238 -- Create and return the path name of a mapping file. Used for gnatstub
239 -- (GNAT STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric
242 procedure Delete_Temp_Config_Files;
243 -- Delete all temporary config files. The caller is responsible for
244 -- ensuring that Keep_Temporary_Files is False.
246 procedure Get_Closure;
247 -- Get the sources in the closure of the ASIS_Main and add them to the
248 -- list of arguments.
250 function Index (Char : Character; Str : String) return Natural;
251 -- Returns first occurrence of Char in Str, returns 0 if Char not in Str
253 procedure Non_VMS_Usage;
254 -- Display usage for platforms other than VMS
256 procedure Process_Link;
257 -- Process GNAT LINK, when there is a project file specified
259 procedure Set_Library_For
260 (Project : Project_Id;
261 Libraries_Present : in out Boolean);
262 -- If Project is a library project, add the correct -L and -l switches to
263 -- the linker invocation.
265 procedure Set_Libraries is
266 new For_Every_Project_Imported (Boolean, Set_Library_For);
267 -- Add the -L and -l switches to the linker for all of the library
270 procedure Test_If_Relative_Path
271 (Switch : in out String_Access;
273 -- Test if Switch is a relative search path switch. If it is and it
274 -- includes directory information, prepend the path with Parent. This
275 -- subprogram is only called when using project files.
277 --------------------------
278 -- Add_To_Carg_Switches --
279 --------------------------
281 procedure Add_To_Carg_Switches (Switch : String_Access) is
283 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
285 if Carg_Switches.Last = 0 then
286 Carg_Switches.Increment_Last;
287 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
290 Carg_Switches.Increment_Last;
291 Carg_Switches.Table (Carg_Switches.Last) := Switch;
292 end Add_To_Carg_Switches;
294 ---------------------------
295 -- Add_To_Rules_Switches --
296 ---------------------------
298 procedure Add_To_Rules_Switches (Switch : String_Access) is
300 -- If the Rules_Switches table is empty, put "-rules" at the beginning
302 if Rules_Switches.Last = 0 then
303 Rules_Switches.Increment_Last;
304 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
307 Rules_Switches.Increment_Last;
308 Rules_Switches.Table (Rules_Switches.Last) := Switch;
309 end Add_To_Rules_Switches;
315 procedure Check_Files is
316 Add_Sources : Boolean := True;
317 Unit : Prj.Unit_Index;
318 Subunit : Boolean := False;
319 FD : File_Descriptor := Invalid_FD;
324 -- Check if there is at least one argument that is not a switch or if
325 -- there is a -files= switch.
327 for Index in 1 .. Last_Switches.Last loop
328 if Last_Switches.Table (Index).all'Length > 7
329 and then Last_Switches.Table (Index) (1 .. 7) = "-files="
331 Add_Sources := False;
334 elsif Last_Switches.Table (Index) (1) /= '-' then
338 and then Last_Switches.Table (Index - 1).all /= "-o")
340 (The_Command = Pretty
341 and then Last_Switches.Table (Index - 1).all /= "-o"
342 and then Last_Switches.Table (Index - 1).all /= "-of")
344 (The_Command = Metric
346 Last_Switches.Table (Index - 1).all /= "-o" and then
347 Last_Switches.Table (Index - 1).all /= "-og" and then
348 Last_Switches.Table (Index - 1).all /= "-ox" and then
349 Last_Switches.Table (Index - 1).all /= "-d")
351 (The_Command /= Check and then
352 The_Command /= Pretty and then
353 The_Command /= Metric)
355 Add_Sources := False;
361 -- If all arguments are switches and there is no switch -files=, add
362 -- the path names of all the sources of the main project.
366 -- For gnatcheck, gnatpp, and gnatmetric, create a temporary file
367 -- and put the list of sources in it.
369 if The_Command = Check or else
370 The_Command = Pretty or else
373 Tempdir.Create_Temp_File (FD, Temp_File_Name);
374 Last_Switches.Increment_Last;
375 Last_Switches.Table (Last_Switches.Last) :=
376 new String'("-files=" & Get_Name_String (Temp_File_Name));
383 -- Gnatstack needs to add the .ci file for the binder generated
384 -- files corresponding to all of the library projects and main
385 -- units belonging to the application.
387 if The_Command = Stack then
388 Proj := Project_Tree.Projects;
389 while Proj /= null loop
390 if Check_Project (Proj.Project, Project) then
392 Main : String_List_Id;
393 File : String_Access;
396 -- Include binder generated files for main programs
398 Main := Proj.Project.Mains;
399 while Main /= Nil_String loop
403 (Proj.Project.Object_Directory.Name) &
407 (Project_Tree.String_Elements.Table
411 if Is_Regular_File (File.all) then
412 Last_Switches.Increment_Last;
413 Last_Switches.Table (Last_Switches.Last) := File;
417 Project_Tree.String_Elements.Table (Main).Next;
420 if Proj.Project.Library then
422 -- Include the .ci file for the binder generated
423 -- files that contains the initialization and
424 -- finalization of the library.
429 (Proj.Project.Object_Directory.Name) &
431 Get_Name_String (Proj.Project.Library_Name) &
434 if Is_Regular_File (File.all) then
435 Last_Switches.Increment_Last;
436 Last_Switches.Table (Last_Switches.Last) := File;
446 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
447 while Unit /= No_Unit_Index loop
449 -- For gnatls, we only need to put the library units, body or
450 -- spec, but not the subunits.
452 if The_Command = List then
453 if Unit.File_Names (Impl) /= null
454 and then not Unit.File_Names (Impl).Locally_Removed
456 -- There is a body, check if it is for this project
459 or else Unit.File_Names (Impl).Project = Project
463 if Unit.File_Names (Spec) = null
464 or else Unit.File_Names (Spec).Locally_Removed
466 -- We have a body with no spec: we need to check if
467 -- this is a subunit, because gnatls will complain
471 Src_Ind : constant Source_File_Index :=
472 Sinput.P.Load_Project_File
478 Sinput.P.Source_File_Is_Subunit (Src_Ind);
483 Last_Switches.Increment_Last;
484 Last_Switches.Table (Last_Switches.Last) :=
488 (Impl).Display_File));
492 elsif Unit.File_Names (Spec) /= null
493 and then not Unit.File_Names (Spec).Locally_Removed
495 -- We have a spec with no body. Check if it is for this
498 if All_Projects or else
499 Unit.File_Names (Spec).Project = Project
501 Last_Switches.Increment_Last;
502 Last_Switches.Table (Last_Switches.Last) :=
503 new String'(Get_Name_String
504 (Unit.File_Names (Spec).Display_File));
508 -- For gnatstack, we put the .ci files corresponding to the
509 -- different units, including the binder generated files. We
510 -- only need to do that for the library units, body or spec,
511 -- but not the subunits.
513 elsif The_Command = Stack then
514 if Unit.File_Names (Impl) /= null
515 and then not Unit.File_Names (Impl).Locally_Removed
517 -- There is a body. Check if .ci files for this project
521 (Unit.File_Names (Impl).Project, Project)
525 if Unit.File_Names (Spec) = null
526 or else Unit.File_Names (Spec).Locally_Removed
528 -- We have a body with no spec: we need to check
529 -- if this is a subunit, because .ci files are not
530 -- generated for subunits.
533 Src_Ind : constant Source_File_Index :=
534 Sinput.P.Load_Project_File
540 Sinput.P.Source_File_Is_Subunit (Src_Ind);
545 Last_Switches.Increment_Last;
546 Last_Switches.Table (Last_Switches.Last) :=
550 (Impl).Project. Object_Directory.Name) &
553 (Unit.File_Names (Impl).Display_File),
558 elsif Unit.File_Names (Spec) /= null
559 and then not Unit.File_Names (Spec).Locally_Removed
561 -- Spec with no body, check if it is for this project
564 (Unit.File_Names (Spec).Project, Project)
566 Last_Switches.Increment_Last;
567 Last_Switches.Table (Last_Switches.Last) :=
571 (Spec).Project. Object_Directory.Name) &
574 (Get_Name_String (Unit.File_Names (Spec).File),
580 -- For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
581 -- sources of the project, or of all projects if -U was
584 for Kind in Spec_Or_Body loop
585 if Unit.File_Names (Kind) /= null
586 and then Check_Project
587 (Unit.File_Names (Kind).Project, Project)
588 and then not Unit.File_Names (Kind).Locally_Removed
591 Add_Char_To_Name_Buffer ('"');
592 Add_Str_To_Name_Buffer
594 (Unit.File_Names (Kind).Path.Display_Name));
595 Add_Char_To_Name_Buffer ('"');
597 if FD /= Invalid_FD then
598 Name_Len := Name_Len + 1;
599 Name_Buffer (Name_Len) := ASCII.LF;
601 Write (FD, Name_Buffer (1)'Address, Name_Len);
603 if Status /= Name_Len then
604 Osint.Fail ("disk full");
608 Last_Switches.Increment_Last;
609 Last_Switches.Table (Last_Switches.Last) :=
610 new String'(Get_Name_String
612 (Kind).Path.Display_Name));
618 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
622 if FD /= Invalid_FD then
626 Osint.Fail ("disk full");
636 function Check_Project
637 (Project : Project_Id;
638 Root_Project : Project_Id) return Boolean
643 if Project = No_Project then
646 elsif All_Projects or else Project = Root_Project then
649 elsif The_Command = Metric then
650 Proj := Root_Project;
651 while Proj.Extends /= No_Project loop
652 if Project = Proj.Extends then
656 Proj := Proj.Extends;
663 -------------------------------
664 -- Check_Relative_Executable --
665 -------------------------------
667 procedure Check_Relative_Executable (Name : in out String_Access) is
668 Exec_File_Name : constant String := Name.all;
671 if not Is_Absolute_Path (Exec_File_Name) then
672 for Index in Exec_File_Name'Range loop
673 if Exec_File_Name (Index) = Directory_Separator then
674 Fail ("relative executable (""" &
676 """) with directory part not allowed " &
677 "when using project files");
681 Get_Name_String (Project.Exec_Directory.Name);
683 if Name_Buffer (Name_Len) /= Directory_Separator then
684 Name_Len := Name_Len + 1;
685 Name_Buffer (Name_Len) := Directory_Separator;
688 Name_Buffer (Name_Len + 1 ..
689 Name_Len + Exec_File_Name'Length) :=
691 Name_Len := Name_Len + Exec_File_Name'Length;
692 Name := new String'(Name_Buffer (1 .. Name_Len));
694 end Check_Relative_Executable;
696 --------------------------------
697 -- Configuration_Pragmas_File --
698 --------------------------------
700 function Configuration_Pragmas_File return Path_Name_Type is
702 Prj.Env.Create_Config_Pragmas_File (Project, Project_Tree);
703 return Project.Config_File_Name;
704 end Configuration_Pragmas_File;
706 ------------------------------
707 -- Delete_Temp_Config_Files --
708 ------------------------------
710 procedure Delete_Temp_Config_Files is
713 pragma Warnings (Off, Success);
716 -- This should only be called if Keep_Temporary_Files is False
718 pragma Assert (not Keep_Temporary_Files);
720 if Project /= No_Project then
721 Proj := Project_Tree.Projects;
722 while Proj /= null loop
723 if Proj.Project.Config_File_Temp then
724 Delete_Temporary_File
725 (Project_Tree, Proj.Project.Config_File_Name);
732 -- If a temporary text file that contains a list of files for a tool
733 -- has been created, delete this temporary file.
735 if Temp_File_Name /= No_Path then
736 Delete_Temporary_File (Project_Tree, Temp_File_Name);
738 end Delete_Temp_Config_Files;
744 procedure Get_Closure is
745 Args : constant Argument_List :=
746 (1 => new String'("-q"),
747 2 => new String'("-b"),
748 3 => new String'("-P"),
751 6 => new String'("-bargs"),
752 7 => new String'("-R"),
753 8 => new String'("-Z"));
754 -- Arguments for the invocation of gnatmake which are added to the
755 -- Last_Arguments list by this procedure.
757 FD : File_Descriptor;
758 -- File descriptor for the temp file that will get the output of the
759 -- invocation of gnatmake.
761 Name : Path_Name_Type;
762 -- Path of the file FD
764 GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
767 GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
770 Return_Code : Integer;
773 pragma Warnings (Off, Unused);
775 File : Ada.Text_IO.File_Type;
776 Line : String (1 .. 250);
778 -- Used to read file if there is an error, it is good enough to display
779 -- just 250 characters if the first line of the file is very long.
782 Path : Path_Name_Type;
785 if GN_Path = null then
786 Put_Line (Standard_Error, "could not locate " & GN_Name);
790 -- Create the temp file
792 Tempdir.Create_Temp_File (FD, Name);
794 -- And close it, because on VMS Spawn with a file descriptor created
795 -- with Create_Temp_File does not redirect output.
799 -- Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
802 (Program_Name => GN_Path.all,
804 Output_File => Get_Name_String (Name),
806 Return_Code => Return_Code,
811 -- Read the output of the invocation of gnatmake
813 Open (File, In_File, Get_Name_String (Name));
815 -- If it was unsuccessful, display the first line in the file and exit
818 if Return_Code /= 0 then
819 Get_Line (File, Line, Last);
821 if not Keep_Temporary_Files then
827 Put_Line (Standard_Error, Line (1 .. Last));
829 (Standard_Error, "could not get closure of " & ASIS_Main.all);
833 -- Get each file name in the file, find its path and add it the
834 -- list of arguments.
836 while not End_Of_File (File) loop
837 Get_Line (File, Line, Last);
840 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
841 while Unit /= No_Unit_Index loop
842 if Unit.File_Names (Spec) /= null
844 Get_Name_String (Unit.File_Names (Spec).File) =
847 Path := Unit.File_Names (Spec).Path.Name;
850 elsif Unit.File_Names (Impl) /= null
852 Get_Name_String (Unit.File_Names (Impl).File) =
855 Path := Unit.File_Names (Impl).Path.Name;
859 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
862 Last_Switches.Increment_Last;
864 if Path /= No_Path then
865 Last_Switches.Table (Last_Switches.Last) :=
866 new String'(Get_Name_String (Path));
869 Last_Switches.Table (Last_Switches.Last) :=
870 new String'(Line (1 .. Last));
874 if not Keep_Temporary_Files then
886 function Index (Char : Character; Str : String) return Natural is
888 for Index in Str'Range loop
889 if Str (Index) = Char then
901 function Mapping_File return Path_Name_Type is
902 Result : Path_Name_Type;
904 Prj.Env.Create_Mapping_File
906 Language => Name_Ada,
907 In_Tree => Project_Tree,
916 procedure Process_Link is
917 Look_For_Executable : Boolean := True;
918 Libraries_Present : Boolean := False;
919 Path_Option : constant String_Access :=
920 MLib.Linker_Library_Path_Option;
921 Prj : Project_Id := Project;
924 Skip_Executable : Boolean := False;
927 -- Add the default search directories, to be able to find
928 -- libgnat in call to MLib.Utl.Lib_Directory.
930 Add_Default_Search_Dirs;
932 Library_Paths.Set_Last (0);
934 -- Check if there are library project files
936 if MLib.Tgt.Support_For_Libraries /= None then
937 Set_Libraries (Project, Libraries_Present);
940 -- If there are, add the necessary additional switches
942 if Libraries_Present then
944 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
946 Last_Switches.Increment_Last;
947 Last_Switches.Table (Last_Switches.Last) :=
948 new String'("-L" & MLib.Utl.Lib_Directory);
949 Last_Switches.Increment_Last;
950 Last_Switches.Table (Last_Switches.Last) :=
951 new String'("-lgnarl");
952 Last_Switches.Increment_Last;
953 Last_Switches.Table (Last_Switches.Last) :=
954 new String'("-lgnat");
956 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
957 -- equivalent) with all the library dirs plus the standard GNAT
960 if Path_Option /= null then
962 Option : String_Access;
963 Length : Natural := Path_Option'Length;
967 if MLib.Separate_Run_Path_Options then
969 -- We are going to create one switch of the form
970 -- "-Wl,-rpath,dir_N" for each directory to consider.
972 -- One switch for each library directory
975 Library_Paths.First .. Library_Paths.Last
977 Last_Switches.Increment_Last;
979 (Last_Switches.Last) := new String'
981 Last_Switches.Table (Index).all);
984 -- One switch for the standard GNAT library dir
986 Last_Switches.Increment_Last;
988 (Last_Switches.Last) := new String'
989 (Path_Option.all & MLib.Utl.Lib_Directory);
992 -- First, compute the exact length for the switch
995 Library_Paths.First .. Library_Paths.Last
997 -- Add the length of the library dir plus one for the
998 -- directory separator.
1002 Library_Paths.Table (Index)'Length + 1;
1005 -- Finally, add the length of the standard GNAT library dir
1007 Length := Length + MLib.Utl.Lib_Directory'Length;
1008 Option := new String (1 .. Length);
1009 Option (1 .. Path_Option'Length) := Path_Option.all;
1010 Current := Path_Option'Length;
1012 -- Put each library dir followed by a dir separator
1015 Library_Paths.First .. Library_Paths.Last
1020 Library_Paths.Table (Index)'Length) :=
1021 Library_Paths.Table (Index).all;
1024 Library_Paths.Table (Index)'Length + 1;
1025 Option (Current) := Path_Separator;
1028 -- Finally put the standard GNAT library dir
1032 Current + MLib.Utl.Lib_Directory'Length) :=
1033 MLib.Utl.Lib_Directory;
1035 -- And add the switch to the last switches
1037 Last_Switches.Increment_Last;
1038 Last_Switches.Table (Last_Switches.Last) :=
1045 -- Check if the first ALI file specified can be found, either in the
1046 -- object directory of the main project or in an object directory of a
1047 -- project file extended by the main project. If the ALI file can be
1048 -- found, replace its name with its absolute path.
1050 Skip_Executable := False;
1052 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1054 -- If we have an executable just reset the flag
1056 if Skip_Executable then
1057 Skip_Executable := False;
1059 -- If -o, set flag so that next switch is not processed
1061 elsif Last_Switches.Table (J).all = "-o" then
1062 Skip_Executable := True;
1068 Switch : constant String :=
1069 Last_Switches.Table (J).all;
1070 ALI_File : constant String (1 .. Switch'Length + 4) :=
1073 Test_Existence : Boolean := False;
1076 Last := Switch'Length;
1078 -- Skip real switches
1080 if Switch'Length /= 0
1081 and then Switch (Switch'First) /= '-'
1083 -- Append ".ali" if file name does not end with it
1085 if Switch'Length <= 4
1086 or else Switch (Switch'Last - 3 .. Switch'Last) /= ".ali"
1088 Last := ALI_File'Last;
1091 -- If file name includes directory information, stop if ALI
1094 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1095 Test_Existence := True;
1098 for K in Switch'Range loop
1100 or else Switch (K) = Directory_Separator
1102 Test_Existence := True;
1108 if Test_Existence then
1109 if Is_Regular_File (ALI_File (1 .. Last)) then
1113 -- Look in object directories if ALI file exists
1118 Dir : constant String :=
1119 Get_Name_String (Prj.Object_Directory.Name);
1123 ALI_File (1 .. Last))
1125 -- We have found the correct project, so we
1126 -- replace the file with the absolute path.
1128 Last_Switches.Table (J) :=
1129 new String'(Dir & ALI_File (1 .. Last));
1137 -- Go to the project being extended, if any
1140 exit Project_Loop when Prj = No_Project;
1141 end loop Project_Loop;
1146 end loop Switch_Loop;
1148 -- If a relative path output file has been specified, we add the exec
1151 for J in reverse 1 .. Last_Switches.Last - 1 loop
1152 if Last_Switches.Table (J).all = "-o" then
1153 Check_Relative_Executable
1154 (Name => Last_Switches.Table (J + 1));
1155 Look_For_Executable := False;
1160 if Look_For_Executable then
1161 for J in reverse 1 .. First_Switches.Last - 1 loop
1162 if First_Switches.Table (J).all = "-o" then
1163 Look_For_Executable := False;
1164 Check_Relative_Executable
1165 (Name => First_Switches.Table (J + 1));
1171 -- If no executable is specified, then find the name of the first ALI
1172 -- file on the command line and issue a -o switch with the absolute path
1173 -- of the executable in the exec directory.
1175 if Look_For_Executable then
1176 for J in 1 .. Last_Switches.Last loop
1177 Arg := Last_Switches.Table (J);
1180 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1182 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1184 Last := Arg'Last - 4;
1186 elsif Is_Regular_File (Arg.all & ".ali") then
1191 Last_Switches.Increment_Last;
1192 Last_Switches.Table (Last_Switches.Last) :=
1194 Get_Name_String (Project.Exec_Directory.Name);
1195 Last_Switches.Increment_Last;
1196 Last_Switches.Table (Last_Switches.Last) :=
1197 new String'(Name_Buffer (1 .. Name_Len) &
1199 (Base_Name (Arg (Arg'First .. Last))));
1207 ---------------------
1208 -- Set_Library_For --
1209 ---------------------
1211 procedure Set_Library_For
1212 (Project : Project_Id;
1213 Libraries_Present : in out Boolean)
1215 Path_Option : constant String_Access :=
1216 MLib.Linker_Library_Path_Option;
1219 -- Case of library project
1221 if Project.Library then
1222 Libraries_Present := True;
1224 -- Add the -L switch
1226 Last_Switches.Increment_Last;
1227 Last_Switches.Table (Last_Switches.Last) :=
1228 new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1230 -- Add the -l switch
1232 Last_Switches.Increment_Last;
1233 Last_Switches.Table (Last_Switches.Last) :=
1234 new String'("-l" & Get_Name_String (Project.Library_Name));
1236 -- Add the directory to table Library_Paths, to be processed later
1237 -- if library is not static and if Path_Option is not null.
1239 if Project.Library_Kind /= Static
1240 and then Path_Option /= null
1242 Library_Paths.Increment_Last;
1243 Library_Paths.Table (Library_Paths.Last) :=
1244 new String'(Get_Name_String (Project.Library_Dir.Name));
1247 end Set_Library_For;
1249 ---------------------------
1250 -- Test_If_Relative_Path --
1251 ---------------------------
1253 procedure Test_If_Relative_Path
1254 (Switch : in out String_Access;
1258 Makeutl.Test_If_Relative_Path
1259 (Switch, Parent, Including_Non_Switch => False, Including_RTS => True);
1260 end Test_If_Relative_Path;
1266 procedure Non_VMS_Usage is
1270 Put_Line ("List of available commands");
1273 for C in Command_List'Range loop
1275 -- No usage for VMS only command or for Sync
1277 if not Command_List (C).VMS_Only and then C /= Sync then
1278 if Targparm.AAMP_On_Target then
1284 Put (To_Lower (Command_List (C).Cname.all));
1287 -- Never call gnatstack with a prefix
1290 Put (Command_List (C).Unixcmd.all);
1292 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1296 Sws : Argument_List_Access renames Command_List (C).Unixsws;
1299 for J in Sws'Range loop
1311 Put_Line ("All commands except chop, krunch and preprocess " &
1312 "accept project file switches -vPx, -Pprj and -Xnam=val");
1316 -------------------------------------
1317 -- Start of processing for GNATCmd --
1318 -------------------------------------
1328 Project_Node_Tree := new Project_Node_Tree_Data;
1329 Prj.Tree.Initialize (Project_Node_Tree);
1331 Prj.Initialize (Project_Tree);
1334 Last_Switches.Set_Last (0);
1336 First_Switches.Init;
1337 First_Switches.Set_Last (0);
1339 Carg_Switches.Set_Last (0);
1340 Rules_Switches.Init;
1341 Rules_Switches.Set_Last (0);
1343 VMS_Conv.Initialize;
1345 -- Add the default search directories, to be able to find system.ads in the
1346 -- subsequent call to Targparm.Get_Target_Parameters.
1348 Add_Default_Search_Dirs;
1350 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1351 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1353 Targparm.Get_Target_Parameters;
1355 -- Add the directory where the GNAT driver is invoked in front of the path,
1356 -- if the GNAT driver is invoked with directory information. Do not do this
1357 -- for VMS, where the notion of path does not really exist.
1361 Command : constant String := Command_Name;
1364 for Index in reverse Command'Range loop
1365 if Command (Index) = Directory_Separator then
1367 Absolute_Dir : constant String :=
1369 (Command (Command'First .. Index));
1371 PATH : constant String :=
1372 Absolute_Dir & Path_Separator & Getenv ("PATH").all;
1375 Setenv ("PATH", PATH);
1384 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1385 -- filenames and pathnames to Unix style.
1388 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1390 VMS_Conversion (The_Command);
1392 B_Start := new String'("b__");
1394 -- If not on VMS, scan the command line directly
1397 if Argument_Count = 0 then
1403 if Argument_Count > Command_Arg
1404 and then Argument (Command_Arg) = "-v"
1406 Verbose_Mode := True;
1407 Command_Arg := Command_Arg + 1;
1409 elsif Argument_Count > Command_Arg
1410 and then Argument (Command_Arg) = "-dn"
1412 Keep_Temporary_Files := True;
1413 Command_Arg := Command_Arg + 1;
1420 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1422 if Command_List (The_Command).VMS_Only then
1426 & Command_List (The_Command).Cname.all
1427 & """ can only be used on VMS");
1431 when Constraint_Error =>
1433 -- Check if it is an alternate command
1436 Alternate : Alternate_Command;
1439 Alternate := Alternate_Command'Value
1440 (Argument (Command_Arg));
1441 The_Command := Corresponding_To (Alternate);
1444 when Constraint_Error =>
1446 Fail ("Unknown command: " & Argument (Command_Arg));
1450 -- Get the arguments from the command line and from the eventual
1451 -- argument file(s) specified on the command line.
1453 for Arg in Command_Arg + 1 .. Argument_Count loop
1455 The_Arg : constant String := Argument (Arg);
1458 -- Check if an argument file is specified
1460 if The_Arg (The_Arg'First) = '@' then
1462 Arg_File : Ada.Text_IO.File_Type;
1463 Line : String (1 .. 256);
1467 -- Open the file and fail if the file cannot be found
1472 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1477 (Standard_Error, "Cannot open argument file """);
1480 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1482 Put_Line (Standard_Error, """");
1486 -- Read line by line and put the content of each non-
1487 -- empty line in the Last_Switches table.
1489 while not End_Of_File (Arg_File) loop
1490 Get_Line (Arg_File, Line, Last);
1493 Last_Switches.Increment_Last;
1494 Last_Switches.Table (Last_Switches.Last) :=
1495 new String'(Line (1 .. Last));
1503 -- It is not an argument file; just put the argument in
1504 -- the Last_Switches table.
1506 Last_Switches.Increment_Last;
1507 Last_Switches.Table (Last_Switches.Last) :=
1508 new String'(The_Arg);
1516 Program : String_Access;
1517 Exec_Path : String_Access;
1520 if The_Command = Stack then
1522 -- Never call gnatstack with a prefix
1524 Program := new String'(Command_List (The_Command).Unixcmd.all);
1528 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1531 -- Locate the executable for the command
1533 Exec_Path := Locate_Exec_On_Path (Program.all);
1535 if Exec_Path = null then
1536 Put_Line (Standard_Error, "could not locate " & Program.all);
1540 -- If there are switches for the executable, put them as first switches
1542 if Command_List (The_Command).Unixsws /= null then
1543 for J in Command_List (The_Command).Unixsws'Range loop
1544 First_Switches.Increment_Last;
1545 First_Switches.Table (First_Switches.Last) :=
1546 Command_List (The_Command).Unixsws (J);
1550 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, METRIC, PRETTY, STACK, STUB,
1551 -- SYNC and XREF, look for project file related switches.
1555 Tool_Package_Name := Name_Binder;
1556 Packages_To_Check := Packages_To_Check_By_Binder;
1558 Tool_Package_Name := Name_Check;
1559 Packages_To_Check := Packages_To_Check_By_Check;
1561 Tool_Package_Name := Name_Eliminate;
1562 Packages_To_Check := Packages_To_Check_By_Eliminate;
1564 Tool_Package_Name := Name_Finder;
1565 Packages_To_Check := Packages_To_Check_By_Finder;
1567 Tool_Package_Name := Name_Linker;
1568 Packages_To_Check := Packages_To_Check_By_Linker;
1570 Tool_Package_Name := Name_Gnatls;
1571 Packages_To_Check := Packages_To_Check_By_Gnatls;
1573 Tool_Package_Name := Name_Metrics;
1574 Packages_To_Check := Packages_To_Check_By_Metric;
1576 Tool_Package_Name := Name_Pretty_Printer;
1577 Packages_To_Check := Packages_To_Check_By_Pretty;
1579 Tool_Package_Name := Name_Stack;
1580 Packages_To_Check := Packages_To_Check_By_Stack;
1582 Tool_Package_Name := Name_Gnatstub;
1583 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1585 Tool_Package_Name := Name_Synchronize;
1586 Packages_To_Check := Packages_To_Check_By_Sync;
1588 Tool_Package_Name := Name_Cross_Reference;
1589 Packages_To_Check := Packages_To_Check_By_Xref;
1591 Tool_Package_Name := No_Name;
1594 if Tool_Package_Name /= No_Name then
1596 -- Check that the switches are consistent. Detect project file
1597 -- related switches.
1599 Inspect_Switches : declare
1600 Arg_Num : Positive := 1;
1601 Argv : String_Access;
1603 procedure Remove_Switch (Num : Positive);
1604 -- Remove a project related switch from table Last_Switches
1610 procedure Remove_Switch (Num : Positive) is
1612 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1613 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1614 Last_Switches.Decrement_Last;
1617 -- Start of processing for Inspect_Switches
1620 while Arg_Num <= Last_Switches.Last loop
1621 Argv := Last_Switches.Table (Arg_Num);
1623 if Argv (Argv'First) = '-' then
1624 if Argv'Length = 1 then
1626 ("switch character cannot be followed by a blank");
1629 -- The two style project files (-p and -P) cannot be used
1632 if (The_Command = Find or else The_Command = Xref)
1633 and then Argv (2) = 'p'
1635 Old_Project_File_Used := True;
1636 if Project_File /= null then
1637 Fail ("-P and -p cannot be used together");
1641 -- --subdirs=... Specify Subdirs
1643 if Argv'Length > Makeutl.Subdirs_Option'Length
1647 Argv'First + Makeutl.Subdirs_Option'Length - 1) =
1648 Makeutl.Subdirs_Option
1653 (Argv'First + Makeutl.Subdirs_Option'Length ..
1656 Remove_Switch (Arg_Num);
1658 -- -aPdir Add dir to the project search path
1660 elsif Argv'Length > 3
1661 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1663 Add_Search_Project_Directory
1664 (Project_Node_Tree, Argv (Argv'First + 3 .. Argv'Last));
1666 Remove_Switch (Arg_Num);
1668 -- -eL Follow links for files
1670 elsif Argv.all = "-eL" then
1671 Follow_Links_For_Files := True;
1672 Follow_Links_For_Dirs := True;
1674 Remove_Switch (Arg_Num);
1676 -- -vPx Specify verbosity while parsing project files
1678 elsif Argv'Length = 4
1679 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1681 case Argv (Argv'Last) is
1683 Current_Verbosity := Prj.Default;
1685 Current_Verbosity := Prj.Medium;
1687 Current_Verbosity := Prj.High;
1689 Fail ("Invalid switch: " & Argv.all);
1692 Remove_Switch (Arg_Num);
1694 -- -Pproject_file Specify project file to be used
1696 elsif Argv (Argv'First + 1) = 'P' then
1698 -- Only one -P switch can be used
1700 if Project_File /= null then
1703 & ": second project file forbidden (first is """
1707 -- The two style project files (-p and -P) cannot be
1710 elsif Old_Project_File_Used then
1711 Fail ("-p and -P cannot be used together");
1713 elsif Argv'Length = 2 then
1715 -- There is space between -P and the project file
1716 -- name. -P cannot be the last option.
1718 if Arg_Num = Last_Switches.Last then
1719 Fail ("project file name missing after -P");
1722 Remove_Switch (Arg_Num);
1723 Argv := Last_Switches.Table (Arg_Num);
1725 -- After -P, there must be a project file name,
1726 -- not another switch.
1728 if Argv (Argv'First) = '-' then
1729 Fail ("project file name missing after -P");
1732 Project_File := new String'(Argv.all);
1737 -- No space between -P and project file name
1740 new String'(Argv (Argv'First + 2 .. Argv'Last));
1743 Remove_Switch (Arg_Num);
1745 -- -Xexternal=value Specify an external reference to be
1746 -- used in project files
1748 elsif Argv'Length >= 5
1749 and then Argv (Argv'First + 1) = 'X'
1752 Equal_Pos : constant Natural :=
1755 Argv (Argv'First + 2 .. Argv'Last));
1757 if Equal_Pos >= Argv'First + 3
1758 and then Equal_Pos /= Argv'Last
1760 Add (Project_Node_Tree,
1762 Argv (Argv'First + 2 .. Equal_Pos - 1),
1763 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1767 & " is not a valid external assignment.");
1771 Remove_Switch (Arg_Num);
1774 (The_Command = Check or else
1775 The_Command = Sync or else
1776 The_Command = Pretty or else
1777 The_Command = Metric or else
1778 The_Command = Stack or else
1780 and then Argv'Length = 2
1781 and then Argv (2) = 'U'
1783 All_Projects := True;
1784 Remove_Switch (Arg_Num);
1787 Arg_Num := Arg_Num + 1;
1790 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1791 or else The_Command = Sync
1792 or else The_Command = Metric
1793 or else The_Command = Pretty)
1794 and then Project_File /= null
1795 and then All_Projects
1797 if ASIS_Main /= null then
1798 Fail ("cannot specify more than one main after -U");
1801 Remove_Switch (Arg_Num);
1805 Arg_Num := Arg_Num + 1;
1808 end Inspect_Switches;
1811 -- If there is a project file specified, parse it, get the switches
1812 -- for the tool and setup PATH environment variables.
1814 if Project_File /= null then
1815 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1818 (Project => Project,
1819 In_Tree => Project_Tree,
1820 In_Node_Tree => Project_Node_Tree,
1821 Project_File_Name => Project_File.all,
1822 Flags => Gnatmake_Flags,
1823 Packages_To_Check => Packages_To_Check);
1825 if Project = Prj.No_Project then
1826 Fail ("""" & Project_File.all & """ processing failed");
1829 -- Check if a package with the name of the tool is in the project
1830 -- file and if there is one, get the switches, if any, and scan them.
1833 Pkg : constant Prj.Package_Id :=
1835 (Name => Tool_Package_Name,
1836 In_Packages => Project.Decl.Packages,
1837 In_Tree => Project_Tree);
1839 Element : Package_Element;
1841 Switches_Array : Array_Element_Id;
1843 The_Switches : Prj.Variable_Value;
1844 Current : Prj.String_List_Id;
1845 The_String : String_Element;
1847 Main : String_Access := null;
1850 if Pkg /= No_Package then
1851 Element := Project_Tree.Packages.Table (Pkg);
1853 -- Packages Gnatls and Gnatstack have a single attribute
1854 -- Switches, that is not an associative array.
1856 if The_Command = List or else The_Command = Stack then
1859 (Variable_Name => Snames.Name_Switches,
1860 In_Variables => Element.Decl.Attributes,
1861 In_Tree => Project_Tree);
1863 -- Packages Binder (for gnatbind), Cross_Reference (for
1864 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1865 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1866 -- (for gnatcheck), and Metric (for gnatmetric) have an
1867 -- attributed Switches, an associative array, indexed by the
1868 -- name of the file.
1870 -- They also have an attribute Default_Switches, indexed by the
1871 -- name of the programming language.
1874 -- First check if there is a single main
1876 for J in 1 .. Last_Switches.Last loop
1877 if Last_Switches.Table (J) (1) /= '-' then
1879 Main := Last_Switches.Table (J);
1888 if Main /= null then
1891 (Name => Name_Switches,
1892 In_Arrays => Element.Decl.Arrays,
1893 In_Tree => Project_Tree);
1895 Add_Str_To_Name_Buffer (Main.all);
1896 The_Switches := Prj.Util.Value_Of
1897 (Index => Name_Find,
1899 In_Array => Switches_Array,
1900 In_Tree => Project_Tree);
1903 if The_Switches.Kind = Prj.Undefined then
1906 (Name => Name_Default_Switches,
1907 In_Arrays => Element.Decl.Arrays,
1908 In_Tree => Project_Tree);
1909 The_Switches := Prj.Util.Value_Of
1912 In_Array => Switches_Array,
1913 In_Tree => Project_Tree);
1917 -- If there are switches specified in the package of the
1918 -- project file corresponding to the tool, scan them.
1920 case The_Switches.Kind is
1921 when Prj.Undefined =>
1926 Switch : constant String :=
1927 Get_Name_String (The_Switches.Value);
1930 if Switch'Length > 0 then
1931 First_Switches.Increment_Last;
1932 First_Switches.Table (First_Switches.Last) :=
1933 new String'(Switch);
1938 Current := The_Switches.Values;
1939 while Current /= Prj.Nil_String loop
1940 The_String := Project_Tree.String_Elements.
1944 Switch : constant String :=
1945 Get_Name_String (The_String.Value);
1948 if Switch'Length > 0 then
1949 First_Switches.Increment_Last;
1950 First_Switches.Table (First_Switches.Last) :=
1951 new String'(Switch);
1955 Current := The_String.Next;
1961 if The_Command = Bind
1962 or else The_Command = Link
1963 or else The_Command = Elim
1965 Change_Dir (Get_Name_String (Project.Object_Directory.Name));
1968 -- Set up the env vars for project path files
1970 Prj.Env.Set_Ada_Paths
1971 (Project, Project_Tree, Including_Libraries => False);
1973 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
1974 -- a configuration pragmas file, if necessary.
1976 if The_Command = Pretty
1977 or else The_Command = Metric
1978 or else The_Command = Stub
1979 or else The_Command = Elim
1980 or else The_Command = Check
1981 or else The_Command = Sync
1983 -- If there are switches in package Compiler, put them in the
1984 -- Carg_Switches table.
1987 Pkg : constant Prj.Package_Id :=
1989 (Name => Name_Compiler,
1990 In_Packages => Project.Decl.Packages,
1991 In_Tree => Project_Tree);
1993 Element : Package_Element;
1995 Switches_Array : Array_Element_Id;
1997 The_Switches : Prj.Variable_Value;
1998 Current : Prj.String_List_Id;
1999 The_String : String_Element;
2001 Main : String_Access := null;
2005 if Pkg /= No_Package then
2007 -- First, check if there is a single main specified.
2009 for J in 1 .. Last_Switches.Last loop
2010 if Last_Switches.Table (J) (1) /= '-' then
2012 Main := Last_Switches.Table (J);
2021 Element := Project_Tree.Packages.Table (Pkg);
2023 -- If there is a single main and there is compilation
2024 -- switches specified in the project file, use them.
2026 if Main /= null and then not All_Projects then
2027 Name_Len := Main'Length;
2028 Name_Buffer (1 .. Name_Len) := Main.all;
2029 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2030 Main_Id := Name_Find;
2034 (Name => Name_Switches,
2035 In_Arrays => Element.Decl.Arrays,
2036 In_Tree => Project_Tree);
2037 The_Switches := Prj.Util.Value_Of
2040 In_Array => Switches_Array,
2041 In_Tree => Project_Tree);
2044 -- Otherwise, get the Default_Switches ("Ada")
2046 if The_Switches.Kind = Undefined then
2049 (Name => Name_Default_Switches,
2050 In_Arrays => Element.Decl.Arrays,
2051 In_Tree => Project_Tree);
2052 The_Switches := Prj.Util.Value_Of
2055 In_Array => Switches_Array,
2056 In_Tree => Project_Tree);
2059 -- If there are switches specified, put them in the
2060 -- Carg_Switches table.
2062 case The_Switches.Kind is
2063 when Prj.Undefined =>
2068 Switch : constant String :=
2069 Get_Name_String (The_Switches.Value);
2071 if Switch'Length > 0 then
2072 Add_To_Carg_Switches (new String'(Switch));
2077 Current := The_Switches.Values;
2078 while Current /= Prj.Nil_String loop
2080 Project_Tree.String_Elements.Table (Current);
2083 Switch : constant String :=
2084 Get_Name_String (The_String.Value);
2086 if Switch'Length > 0 then
2087 Add_To_Carg_Switches (new String'(Switch));
2091 Current := The_String.Next;
2097 -- If -cargs is one of the switches, move the following switches
2098 -- to the Carg_Switches table.
2100 for J in 1 .. First_Switches.Last loop
2101 if First_Switches.Table (J).all = "-cargs" then
2107 -- Move the switches that are before -rules when the
2108 -- command is CHECK.
2111 while K <= First_Switches.Last
2113 (The_Command /= Check
2114 or else First_Switches.Table (K).all /= "-rules")
2116 Add_To_Carg_Switches (First_Switches.Table (K));
2120 if K > First_Switches.Last then
2121 First_Switches.Set_Last (J - 1);
2125 while K <= First_Switches.Last loop
2127 First_Switches.Table (Last) :=
2128 First_Switches.Table (K);
2132 First_Switches.Set_Last (Last);
2140 for J in 1 .. Last_Switches.Last loop
2141 if Last_Switches.Table (J).all = "-cargs" then
2147 -- Move the switches that are before -rules when the
2148 -- command is CHECK.
2151 while K <= Last_Switches.Last
2153 (The_Command /= Check
2154 or else Last_Switches.Table (K).all /= "-rules")
2156 Add_To_Carg_Switches (Last_Switches.Table (K));
2160 if K > Last_Switches.Last then
2161 Last_Switches.Set_Last (J - 1);
2165 while K <= Last_Switches.Last loop
2167 Last_Switches.Table (Last) :=
2168 Last_Switches.Table (K);
2172 Last_Switches.Set_Last (Last);
2181 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2182 M_File : constant Path_Name_Type := Mapping_File;
2185 if CP_File /= No_Path then
2186 if The_Command = Elim then
2187 First_Switches.Increment_Last;
2188 First_Switches.Table (First_Switches.Last) :=
2189 new String'("-C" & Get_Name_String (CP_File));
2192 Add_To_Carg_Switches
2193 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2197 if M_File /= No_Path then
2198 Add_To_Carg_Switches
2199 (new String'("-gnatem=" & Get_Name_String (M_File)));
2202 -- For gnatcheck, also indicate a global configuration pragmas
2203 -- file and, if -U is not used, a local one.
2205 if The_Command = Check then
2207 Pkg : constant Prj.Package_Id :=
2209 (Name => Name_Builder,
2210 In_Packages => Project.Decl.Packages,
2211 In_Tree => Project_Tree);
2213 Variable : Variable_Value :=
2216 Attribute_Or_Array_Name =>
2217 Name_Global_Configuration_Pragmas,
2219 In_Tree => Project_Tree);
2222 if (Variable = Nil_Variable_Value
2223 or else Length_Of_Name (Variable.Value) = 0)
2224 and then Pkg /= No_Package
2229 Attribute_Or_Array_Name =>
2230 Name_Global_Config_File,
2232 In_Tree => Project_Tree);
2235 if Variable /= Nil_Variable_Value
2236 and then Length_Of_Name (Variable.Value) /= 0
2238 Add_To_Carg_Switches
2240 ("-gnatec=" & Get_Name_String (Variable.Value)));
2244 if not All_Projects then
2246 Pkg : constant Prj.Package_Id :=
2248 (Name => Name_Compiler,
2249 In_Packages => Project.Decl.Packages,
2250 In_Tree => Project_Tree);
2252 Variable : Variable_Value :=
2255 Attribute_Or_Array_Name =>
2256 Name_Local_Configuration_Pragmas,
2258 In_Tree => Project_Tree);
2261 if (Variable = Nil_Variable_Value
2262 or else Length_Of_Name (Variable.Value) = 0)
2263 and then Pkg /= No_Package
2268 Attribute_Or_Array_Name =>
2269 Name_Local_Config_File,
2271 In_Tree => Project_Tree);
2274 if Variable /= Nil_Variable_Value
2275 and then Length_Of_Name (Variable.Value) /= 0
2277 Add_To_Carg_Switches
2280 Get_Name_String (Variable.Value)));
2288 if The_Command = Link then
2292 if The_Command = Link or else The_Command = Bind then
2294 -- For files that are specified as relative paths with directory
2295 -- information, we convert them to absolute paths, with parent
2296 -- being the current working directory if specified on the command
2297 -- line and the project directory if specified in the project
2298 -- file. This is what gnatmake is doing for linker and binder
2301 for J in 1 .. Last_Switches.Last loop
2302 GNATCmd.Test_If_Relative_Path
2303 (Last_Switches.Table (J), Current_Work_Dir);
2306 Get_Name_String (Project.Directory.Name);
2309 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2311 for J in 1 .. First_Switches.Last loop
2312 GNATCmd.Test_If_Relative_Path
2313 (First_Switches.Table (J), Project_Dir);
2317 elsif The_Command = Stub then
2319 File_Index : Integer := 0;
2320 Dir_Index : Integer := 0;
2321 Last : constant Integer := Last_Switches.Last;
2322 Lang : constant Language_Ptr :=
2323 Get_Language_From_Name (Project, "ada");
2326 for Index in 1 .. Last loop
2327 if Last_Switches.Table (Index)
2328 (Last_Switches.Table (Index)'First) /= '-'
2330 File_Index := Index;
2335 -- If the project file naming scheme is not standard, and if
2336 -- the file name ends with the spec suffix, then indicate to
2337 -- gnatstub the name of the body file with a -o switch.
2339 if not Is_Standard_GNAT_Naming (Lang.Config.Naming_Data) then
2340 if File_Index /= 0 then
2342 Spec : constant String :=
2344 (Last_Switches.Table (File_Index).all);
2345 Last : Natural := Spec'Last;
2348 Get_Name_String (Lang.Config.Naming_Data.Spec_Suffix);
2350 if Spec'Length > Name_Len
2351 and then Spec (Last - Name_Len + 1 .. Last) =
2352 Name_Buffer (1 .. Name_Len)
2354 Last := Last - Name_Len;
2356 (Lang.Config.Naming_Data.Body_Suffix);
2357 Last_Switches.Increment_Last;
2358 Last_Switches.Table (Last_Switches.Last) :=
2360 Last_Switches.Increment_Last;
2361 Last_Switches.Table (Last_Switches.Last) :=
2362 new String'(Spec (Spec'First .. Last) &
2363 Name_Buffer (1 .. Name_Len));
2369 -- Add the directory of the spec as the destination directory
2370 -- of the body, if there is no destination directory already
2373 if File_Index /= 0 then
2374 for Index in File_Index + 1 .. Last loop
2375 if Last_Switches.Table (Index)
2376 (Last_Switches.Table (Index)'First) /= '-'
2383 if Dir_Index = 0 then
2384 Last_Switches.Increment_Last;
2385 Last_Switches.Table (Last_Switches.Last) :=
2387 (Dir_Name (Last_Switches.Table (File_Index).all));
2393 -- For gnatmetric, the generated files should be put in the object
2394 -- directory. This must be the first switch, because it may be
2395 -- overridden by a switch in package Metrics in the project file or
2396 -- by a command line option. Note that we don't add the -d= switch
2397 -- if there is no object directory available.
2399 if The_Command = Metric
2400 and then Project.Object_Directory /= No_Path_Information
2402 First_Switches.Increment_Last;
2403 First_Switches.Table (2 .. First_Switches.Last) :=
2404 First_Switches.Table (1 .. First_Switches.Last - 1);
2405 First_Switches.Table (1) :=
2407 Get_Name_String (Project.Object_Directory.Name));
2410 -- For gnat check, -rules and the following switches need to be the
2411 -- last options, so move all these switches to table Rules_Switches.
2413 if The_Command = Check then
2416 -- Set to rank of options preceding "-rules"
2418 In_Rules_Switches : Boolean;
2419 -- Set to True when options "-rules" is found
2422 New_Last := First_Switches.Last;
2423 In_Rules_Switches := False;
2425 for J in 1 .. First_Switches.Last loop
2426 if In_Rules_Switches then
2427 Add_To_Rules_Switches (First_Switches.Table (J));
2429 elsif First_Switches.Table (J).all = "-rules" then
2431 In_Rules_Switches := True;
2435 if In_Rules_Switches then
2436 First_Switches.Set_Last (New_Last);
2439 New_Last := Last_Switches.Last;
2440 In_Rules_Switches := False;
2442 for J in 1 .. Last_Switches.Last loop
2443 if In_Rules_Switches then
2444 Add_To_Rules_Switches (Last_Switches.Table (J));
2446 elsif Last_Switches.Table (J).all = "-rules" then
2448 In_Rules_Switches := True;
2452 if In_Rules_Switches then
2453 Last_Switches.Set_Last (New_Last);
2458 -- For gnat check, sync, metric or pretty with -U + a main, get the
2459 -- list of sources from the closure and add them to the arguments.
2461 if ASIS_Main /= null then
2464 -- On VMS, set up the env var again for source dirs file. This is
2465 -- because the call to gnatmake has set this env var to another
2466 -- file that has now been deleted.
2468 if Hostparm.OpenVMS then
2470 -- First make sure that the recorded file names are empty
2472 Prj.Env.Initialize (Project_Tree);
2474 Prj.Env.Set_Ada_Paths
2475 (Project, Project_Tree, Including_Libraries => False);
2478 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2479 -- and gnat stack, if no file has been put on the command line, call
2480 -- tool with all the sources of the main project.
2482 elsif The_Command = Check or else
2483 The_Command = Sync or else
2484 The_Command = Pretty or else
2485 The_Command = Metric or else
2486 The_Command = List or else
2493 -- Gather all the arguments and invoke the executable
2496 The_Args : Argument_List
2497 (1 .. First_Switches.Last +
2498 Last_Switches.Last +
2499 Carg_Switches.Last +
2500 Rules_Switches.Last);
2501 Arg_Num : Natural := 0;
2504 for J in 1 .. First_Switches.Last loop
2505 Arg_Num := Arg_Num + 1;
2506 The_Args (Arg_Num) := First_Switches.Table (J);
2509 for J in 1 .. Last_Switches.Last loop
2510 Arg_Num := Arg_Num + 1;
2511 The_Args (Arg_Num) := Last_Switches.Table (J);
2514 for J in 1 .. Carg_Switches.Last loop
2515 Arg_Num := Arg_Num + 1;
2516 The_Args (Arg_Num) := Carg_Switches.Table (J);
2519 for J in 1 .. Rules_Switches.Last loop
2520 Arg_Num := Arg_Num + 1;
2521 The_Args (Arg_Num) := Rules_Switches.Table (J);
2524 -- If Display_Command is on, only display the generated command
2526 if Display_Command then
2527 Put (Standard_Error, "generated command -->");
2528 Put (Standard_Error, Exec_Path.all);
2530 for Arg in The_Args'Range loop
2531 Put (Standard_Error, " ");
2532 Put (Standard_Error, The_Args (Arg).all);
2535 Put (Standard_Error, "<--");
2536 New_Line (Standard_Error);
2540 if Verbose_Mode then
2541 Output.Write_Str (Exec_Path.all);
2543 for Arg in The_Args'Range loop
2544 Output.Write_Char (' ');
2545 Output.Write_Str (The_Args (Arg).all);
2552 Exit_Status (Spawn (Exec_Path.all, The_Args));
2559 if not Keep_Temporary_Files then
2560 Prj.Delete_All_Temp_Files (Project_Tree);
2561 Delete_Temp_Config_Files;
2564 Set_Exit_Status (Failure);
2567 if not Keep_Temporary_Files then
2568 Prj.Delete_All_Temp_Files (Project_Tree);
2569 Delete_Temp_Config_Files;
2572 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2573 -- return an understandable VMS exit status. However the exit status
2574 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2575 -- just a simple success or failure on VMS.
2577 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2578 Set_Exit_Status (Failure);
2580 Set_Exit_Status (My_Exit_Status);