1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
29 with Makeutl; use Makeutl;
30 with MLib.Tgt; use MLib.Tgt;
33 with Namet; use Namet;
35 with Osint; use Osint;
39 with Prj.Ext; use Prj.Ext;
41 with Prj.Tree; use Prj.Tree;
42 with Prj.Util; use Prj.Util;
44 with Snames; use Snames;
48 with Types; use Types;
49 with Hostparm; use Hostparm;
50 -- Used to determine if we are in VMS or not for error message purposes
52 with Ada.Characters.Handling; use Ada.Characters.Handling;
53 with Ada.Command_Line; use Ada.Command_Line;
54 with Ada.Text_IO; use Ada.Text_IO;
56 with GNAT.OS_Lib; use GNAT.OS_Lib;
58 with VMS_Conv; use VMS_Conv;
61 Project_Node_Tree : Project_Node_Tree_Ref;
62 Project_File : String_Access;
63 Project : Prj.Project_Id;
64 Current_Verbosity : Prj.Verbosity := Prj.Default;
65 Tool_Package_Name : Name_Id := No_Name;
67 B_Start : String_Ptr := new String'("b~");
68 -- Prefix of binder generated file, changed to b__ for VMS
70 Old_Project_File_Used : Boolean := False;
71 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
72 -- an old fashioned project file. -p cannot be used in conjunction
75 Temp_File_Name : Path_Name_Type := No_Path;
76 -- The name of the temporary text file to put a list of source/object
77 -- files to pass to a tool.
79 ASIS_Main : String_Access := null;
80 -- Main for commands Check, Metric and Pretty, when -U is used
82 package First_Switches is new Table.Table
83 (Table_Component_Type => String_Access,
84 Table_Index_Type => Integer,
87 Table_Increment => 100,
88 Table_Name => "Gnatcmd.First_Switches");
89 -- A table to keep the switches from the project file
91 package Carg_Switches is new Table.Table
92 (Table_Component_Type => String_Access,
93 Table_Index_Type => Integer,
96 Table_Increment => 100,
97 Table_Name => "Gnatcmd.Carg_Switches");
98 -- A table to keep the switches following -cargs for ASIS tools
100 package Rules_Switches is new Table.Table
101 (Table_Component_Type => String_Access,
102 Table_Index_Type => Integer,
103 Table_Low_Bound => 1,
105 Table_Increment => 100,
106 Table_Name => "Gnatcmd.Rules_Switches");
107 -- A table to keep the switches following -rules for gnatcheck
109 package Library_Paths is new Table.Table (
110 Table_Component_Type => String_Access,
111 Table_Index_Type => Integer,
112 Table_Low_Bound => 1,
114 Table_Increment => 100,
115 Table_Name => "Make.Library_Path");
117 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
118 -- tool. We allocate objects because we cannot declare aliased objects
119 -- as we are in a procedure, not a library level package.
121 subtype SA is String_Access;
123 Naming_String : constant SA := new String'("naming");
124 Binder_String : constant SA := new String'("binder");
125 Compiler_String : constant SA := new String'("compiler");
126 Check_String : constant SA := new String'("check");
127 Synchronize_String : constant SA := new String'("synchronize");
128 Eliminate_String : constant SA := new String'("eliminate");
129 Finder_String : constant SA := new String'("finder");
130 Linker_String : constant SA := new String'("linker");
131 Gnatls_String : constant SA := new String'("gnatls");
132 Pretty_String : constant SA := new String'("pretty_printer");
133 Stack_String : constant SA := new String'("stack");
134 Gnatstub_String : constant SA := new String'("gnatstub");
135 Metric_String : constant SA := new String'("metrics");
136 Xref_String : constant SA := new String'("cross_reference");
138 Packages_To_Check_By_Binder : constant String_List_Access :=
139 new String_List'((Naming_String, Binder_String));
141 Packages_To_Check_By_Check : constant String_List_Access :=
142 new String_List'((Naming_String, Check_String, Compiler_String));
144 Packages_To_Check_By_Sync : constant String_List_Access :=
145 new String_List'((Naming_String, Synchronize_String, Compiler_String));
147 Packages_To_Check_By_Eliminate : constant String_List_Access :=
148 new String_List'((Naming_String, Eliminate_String, Compiler_String));
150 Packages_To_Check_By_Finder : constant String_List_Access :=
151 new String_List'((Naming_String, Finder_String));
153 Packages_To_Check_By_Linker : constant String_List_Access :=
154 new String_List'((Naming_String, Linker_String));
156 Packages_To_Check_By_Gnatls : constant String_List_Access :=
157 new String_List'((Naming_String, Gnatls_String));
159 Packages_To_Check_By_Pretty : constant String_List_Access :=
160 new String_List'((Naming_String, Pretty_String, Compiler_String));
162 Packages_To_Check_By_Stack : constant String_List_Access :=
163 new String_List'((Naming_String, Stack_String));
165 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
166 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
168 Packages_To_Check_By_Metric : constant String_List_Access :=
169 new String_List'((Naming_String, Metric_String, Compiler_String));
171 Packages_To_Check_By_Xref : constant String_List_Access :=
172 new String_List'((Naming_String, Xref_String));
174 Packages_To_Check : String_List_Access := Prj.All_Packages;
176 ----------------------------------
177 -- Declarations for GNATCMD use --
178 ----------------------------------
180 The_Command : Command_Type;
181 -- The command specified in the invocation of the GNAT driver
183 Command_Arg : Positive := 1;
184 -- The index of the command in the arguments of the GNAT driver
186 My_Exit_Status : Exit_Status := Success;
187 -- The exit status of the spawned tool. Used to set the correct VMS
190 Current_Work_Dir : constant String := Get_Current_Dir;
191 -- The path of the working directory
193 All_Projects : Boolean := False;
194 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
195 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
196 -- should be invoked for all sources of all projects.
198 -----------------------
199 -- Local Subprograms --
200 -----------------------
202 procedure Add_To_Carg_Switches (Switch : String_Access);
203 -- Add a switch to the Carg_Switches table. If it is the first one, put the
204 -- switch "-cargs" at the beginning of the table.
206 procedure Add_To_Rules_Switches (Switch : String_Access);
207 -- Add a switch to the Rules_Switches table. If it is the first one, put
208 -- the switch "-crules" at the beginning of the table.
210 procedure Check_Files;
211 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
212 -- project file is specified, without any file arguments and without a
213 -- switch -files=. If it is the case, invoke the GNAT tool with the proper
214 -- list of files, derived from the sources of the project.
216 function Check_Project
217 (Project : Project_Id;
218 Root_Project : Project_Id) return Boolean;
219 -- Returns True if Project = Root_Project or if we want to consider all
220 -- sources of all projects. For GNAT METRIC, also returns True if Project
221 -- is extended by Root_Project.
223 procedure Check_Relative_Executable (Name : in out String_Access);
224 -- Check if an executable is specified as a relative path. If it is, and
225 -- the path contains directory information, fail. Otherwise, prepend the
226 -- exec directory. This procedure is only used for GNAT LINK when a project
227 -- file is specified.
229 function Configuration_Pragmas_File return Path_Name_Type;
230 -- Return an argument, if there is a configuration pragmas file to be
231 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
232 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
235 function Mapping_File return Path_Name_Type;
236 -- Create and return the path name of a mapping file. Used for gnatstub
237 -- (GNAT STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric
240 procedure Delete_Temp_Config_Files;
241 -- Delete all temporary config files. The caller is responsible for
242 -- ensuring that Keep_Temporary_Files is False.
244 procedure Get_Closure;
245 -- Get the sources in the closure of the ASIS_Main and add them to the
246 -- list of arguments.
248 function Index (Char : Character; Str : String) return Natural;
249 -- Returns first occurrence of Char in Str, returns 0 if Char not in Str
251 procedure Non_VMS_Usage;
252 -- Display usage for platforms other than VMS
254 procedure Process_Link;
255 -- Process GNAT LINK, when there is a project file specified
257 procedure Set_Library_For
258 (Project : Project_Id;
259 Libraries_Present : in out Boolean);
260 -- If Project is a library project, add the correct -L and -l switches to
261 -- the linker invocation.
263 procedure Set_Libraries is
264 new For_Every_Project_Imported (Boolean, Set_Library_For);
265 -- Add the -L and -l switches to the linker for all of the library
268 procedure Test_If_Relative_Path
269 (Switch : in out String_Access;
271 -- Test if Switch is a relative search path switch. If it is and it
272 -- includes directory information, prepend the path with Parent. This
273 -- subprogram is only called when using project files.
275 --------------------------
276 -- Add_To_Carg_Switches --
277 --------------------------
279 procedure Add_To_Carg_Switches (Switch : String_Access) is
281 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
283 if Carg_Switches.Last = 0 then
284 Carg_Switches.Increment_Last;
285 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
288 Carg_Switches.Increment_Last;
289 Carg_Switches.Table (Carg_Switches.Last) := Switch;
290 end Add_To_Carg_Switches;
292 ---------------------------
293 -- Add_To_Rules_Switches --
294 ---------------------------
296 procedure Add_To_Rules_Switches (Switch : String_Access) is
298 -- If the Rules_Switches table is empty, put "-rules" at the beginning
300 if Rules_Switches.Last = 0 then
301 Rules_Switches.Increment_Last;
302 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
305 Rules_Switches.Increment_Last;
306 Rules_Switches.Table (Rules_Switches.Last) := Switch;
307 end Add_To_Rules_Switches;
313 procedure Check_Files is
314 Add_Sources : Boolean := True;
315 Unit : Prj.Unit_Index;
316 Subunit : Boolean := False;
317 FD : File_Descriptor := Invalid_FD;
322 -- Check if there is at least one argument that is not a switch or if
323 -- there is a -files= switch.
325 for Index in 1 .. Last_Switches.Last loop
326 if Last_Switches.Table (Index).all'Length > 7
327 and then Last_Switches.Table (Index) (1 .. 7) = "-files="
329 Add_Sources := False;
332 elsif Last_Switches.Table (Index) (1) /= '-' then
337 Last_Switches.Table (Index - 1).all /= "-o")
339 (The_Command = Pretty
341 Last_Switches.Table (Index - 1).all /= "-o" and then
342 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
458 if All_Projects or else
459 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;
905 Prj.Env.Create_Mapping_File
907 Language => Name_Ada,
908 In_Tree => Project_Tree,
917 procedure Process_Link is
918 Look_For_Executable : Boolean := True;
919 Libraries_Present : Boolean := False;
920 Path_Option : constant String_Access :=
921 MLib.Linker_Library_Path_Option;
922 Prj : Project_Id := Project;
925 Skip_Executable : Boolean := False;
928 -- Add the default search directories, to be able to find
929 -- libgnat in call to MLib.Utl.Lib_Directory.
931 Add_Default_Search_Dirs;
933 Library_Paths.Set_Last (0);
935 -- Check if there are library project files
937 if MLib.Tgt.Support_For_Libraries /= None then
938 Set_Libraries (Project, Libraries_Present);
941 -- If there are, add the necessary additional switches
943 if Libraries_Present then
945 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
947 Last_Switches.Increment_Last;
948 Last_Switches.Table (Last_Switches.Last) :=
949 new String'("-L" & MLib.Utl.Lib_Directory);
950 Last_Switches.Increment_Last;
951 Last_Switches.Table (Last_Switches.Last) :=
952 new String'("-lgnarl");
953 Last_Switches.Increment_Last;
954 Last_Switches.Table (Last_Switches.Last) :=
955 new String'("-lgnat");
957 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
958 -- equivalent) with all the library dirs plus the standard GNAT
961 if Path_Option /= null then
963 Option : String_Access;
964 Length : Natural := Path_Option'Length;
968 if MLib.Separate_Run_Path_Options then
970 -- We are going to create one switch of the form
971 -- "-Wl,-rpath,dir_N" for each directory to consider.
973 -- One switch for each library directory
976 Library_Paths.First .. Library_Paths.Last
978 Last_Switches.Increment_Last;
980 (Last_Switches.Last) := new String'
982 Last_Switches.Table (Index).all);
985 -- One switch for the standard GNAT library dir
987 Last_Switches.Increment_Last;
989 (Last_Switches.Last) := new String'
990 (Path_Option.all & MLib.Utl.Lib_Directory);
993 -- First, compute the exact length for the switch
996 Library_Paths.First .. Library_Paths.Last
998 -- Add the length of the library dir plus one for the
999 -- directory separator.
1003 Library_Paths.Table (Index)'Length + 1;
1006 -- Finally, add the length of the standard GNAT library dir
1008 Length := Length + MLib.Utl.Lib_Directory'Length;
1009 Option := new String (1 .. Length);
1010 Option (1 .. Path_Option'Length) := Path_Option.all;
1011 Current := Path_Option'Length;
1013 -- Put each library dir followed by a dir separator
1016 Library_Paths.First .. Library_Paths.Last
1021 Library_Paths.Table (Index)'Length) :=
1022 Library_Paths.Table (Index).all;
1025 Library_Paths.Table (Index)'Length + 1;
1026 Option (Current) := Path_Separator;
1029 -- Finally put the standard GNAT library dir
1033 Current + MLib.Utl.Lib_Directory'Length) :=
1034 MLib.Utl.Lib_Directory;
1036 -- And add the switch to the last switches
1038 Last_Switches.Increment_Last;
1039 Last_Switches.Table (Last_Switches.Last) :=
1046 -- Check if the first ALI file specified can be found, either in the
1047 -- object directory of the main project or in an object directory of a
1048 -- project file extended by the main project. If the ALI file can be
1049 -- found, replace its name with its absolute path.
1051 Skip_Executable := False;
1053 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1055 -- If we have an executable just reset the flag
1057 if Skip_Executable then
1058 Skip_Executable := False;
1060 -- If -o, set flag so that next switch is not processed
1062 elsif Last_Switches.Table (J).all = "-o" then
1063 Skip_Executable := True;
1069 Switch : constant String :=
1070 Last_Switches.Table (J).all;
1071 ALI_File : constant String (1 .. Switch'Length + 4) :=
1074 Test_Existence : Boolean := False;
1077 Last := Switch'Length;
1079 -- Skip real switches
1081 if Switch'Length /= 0
1082 and then Switch (Switch'First) /= '-'
1084 -- Append ".ali" if file name does not end with it
1086 if Switch'Length <= 4
1087 or else Switch (Switch'Last - 3 .. Switch'Last)
1090 Last := ALI_File'Last;
1093 -- If file name includes directory information, stop if ALI
1096 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1097 Test_Existence := True;
1100 for K in Switch'Range loop
1101 if Switch (K) = '/' or else
1102 Switch (K) = Directory_Separator
1104 Test_Existence := True;
1110 if Test_Existence then
1111 if Is_Regular_File (ALI_File (1 .. Last)) then
1115 -- Look in object directories if ALI file exists
1120 Dir : constant String :=
1121 Get_Name_String (Prj.Object_Directory.Name);
1125 ALI_File (1 .. Last))
1127 -- We have found the correct project, so we
1128 -- replace the file with the absolute path.
1130 Last_Switches.Table (J) :=
1131 new String'(Dir & ALI_File (1 .. Last));
1139 -- Go to the project being extended, if any
1142 exit Project_Loop when Prj = No_Project;
1143 end loop Project_Loop;
1148 end loop Switch_Loop;
1150 -- If a relative path output file has been specified, we add the exec
1153 for J in reverse 1 .. Last_Switches.Last - 1 loop
1154 if Last_Switches.Table (J).all = "-o" then
1155 Check_Relative_Executable
1156 (Name => Last_Switches.Table (J + 1));
1157 Look_For_Executable := False;
1162 if Look_For_Executable then
1163 for J in reverse 1 .. First_Switches.Last - 1 loop
1164 if First_Switches.Table (J).all = "-o" then
1165 Look_For_Executable := False;
1166 Check_Relative_Executable
1167 (Name => First_Switches.Table (J + 1));
1173 -- If no executable is specified, then find the name of the first ALI
1174 -- file on the command line and issue a -o switch with the absolute path
1175 -- of the executable in the exec directory.
1177 if Look_For_Executable then
1178 for J in 1 .. Last_Switches.Last loop
1179 Arg := Last_Switches.Table (J);
1182 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1184 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1186 Last := Arg'Last - 4;
1188 elsif Is_Regular_File (Arg.all & ".ali") then
1193 Last_Switches.Increment_Last;
1194 Last_Switches.Table (Last_Switches.Last) :=
1196 Get_Name_String (Project.Exec_Directory.Name);
1197 Last_Switches.Increment_Last;
1198 Last_Switches.Table (Last_Switches.Last) :=
1199 new String'(Name_Buffer (1 .. Name_Len) &
1201 (Base_Name (Arg (Arg'First .. Last))));
1209 ---------------------
1210 -- Set_Library_For --
1211 ---------------------
1213 procedure Set_Library_For
1214 (Project : Project_Id;
1215 Libraries_Present : in out Boolean)
1217 Path_Option : constant String_Access :=
1218 MLib.Linker_Library_Path_Option;
1221 -- Case of library project
1223 if Project.Library then
1224 Libraries_Present := True;
1226 -- Add the -L switch
1228 Last_Switches.Increment_Last;
1229 Last_Switches.Table (Last_Switches.Last) :=
1230 new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1232 -- Add the -l switch
1234 Last_Switches.Increment_Last;
1235 Last_Switches.Table (Last_Switches.Last) :=
1236 new String'("-l" & Get_Name_String (Project.Library_Name));
1238 -- Add the directory to table Library_Paths, to be processed later
1239 -- if library is not static and if Path_Option is not null.
1241 if Project.Library_Kind /= Static
1242 and then Path_Option /= null
1244 Library_Paths.Increment_Last;
1245 Library_Paths.Table (Library_Paths.Last) :=
1246 new String'(Get_Name_String (Project.Library_Dir.Name));
1249 end Set_Library_For;
1251 ---------------------------
1252 -- Test_If_Relative_Path --
1253 ---------------------------
1255 procedure Test_If_Relative_Path
1256 (Switch : in out String_Access;
1260 Makeutl.Test_If_Relative_Path
1261 (Switch, Parent, Including_Non_Switch => False, Including_RTS => True);
1262 end Test_If_Relative_Path;
1268 procedure Non_VMS_Usage is
1272 Put_Line ("List of available commands");
1275 for C in Command_List'Range loop
1276 if not Command_List (C).VMS_Only then
1277 if Targparm.AAMP_On_Target then
1283 Put (To_Lower (Command_List (C).Cname.all));
1286 -- Never call gnatstack with a prefix
1289 Put (Command_List (C).Unixcmd.all);
1291 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
1295 Sws : Argument_List_Access renames Command_List (C).Unixsws;
1298 for J in Sws'Range loop
1310 Put_Line ("Commands find, list, metric, pretty, stack, stub and xref " &
1311 "accept project file switches -vPx, -Pprj and -Xnam=val");
1315 -------------------------------------
1316 -- Start of processing for GNATCmd --
1317 -------------------------------------
1327 Project_Node_Tree := new Project_Node_Tree_Data;
1328 Prj.Tree.Initialize (Project_Node_Tree);
1330 Prj.Initialize (Project_Tree);
1333 Last_Switches.Set_Last (0);
1335 First_Switches.Init;
1336 First_Switches.Set_Last (0);
1338 Carg_Switches.Set_Last (0);
1339 Rules_Switches.Init;
1340 Rules_Switches.Set_Last (0);
1342 VMS_Conv.Initialize;
1344 -- Add the default search directories, to be able to find system.ads in the
1345 -- subsequent call to Targparm.Get_Target_Parameters.
1347 Add_Default_Search_Dirs;
1349 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1350 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1352 Targparm.Get_Target_Parameters;
1354 -- Add the directory where the GNAT driver is invoked in front of the path,
1355 -- if the GNAT driver is invoked with directory information. Do not do this
1356 -- for VMS, where the notion of path does not really exist.
1360 Command : constant String := Command_Name;
1363 for Index in reverse Command'Range loop
1364 if Command (Index) = Directory_Separator then
1366 Absolute_Dir : constant String :=
1368 (Command (Command'First .. Index));
1370 PATH : constant String :=
1371 Absolute_Dir & Path_Separator & Getenv ("PATH").all;
1374 Setenv ("PATH", PATH);
1383 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1384 -- filenames and pathnames to Unix style.
1387 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1389 VMS_Conversion (The_Command);
1391 B_Start := new String'("b__");
1393 -- If not on VMS, scan the command line directly
1396 if Argument_Count = 0 then
1402 if Argument_Count > Command_Arg
1403 and then Argument (Command_Arg) = "-v"
1405 Verbose_Mode := True;
1406 Command_Arg := Command_Arg + 1;
1408 elsif Argument_Count > Command_Arg
1409 and then Argument (Command_Arg) = "-dn"
1411 Keep_Temporary_Files := True;
1412 Command_Arg := Command_Arg + 1;
1419 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1421 if Command_List (The_Command).VMS_Only then
1425 & Command_List (The_Command).Cname.all
1426 & """ can only be used on VMS");
1430 when Constraint_Error =>
1432 -- Check if it is an alternate command
1435 Alternate : Alternate_Command;
1438 Alternate := Alternate_Command'Value
1439 (Argument (Command_Arg));
1440 The_Command := Corresponding_To (Alternate);
1443 when Constraint_Error =>
1445 Fail ("Unknown command: " & Argument (Command_Arg));
1449 -- Get the arguments from the command line and from the eventual
1450 -- argument file(s) specified on the command line.
1452 for Arg in Command_Arg + 1 .. Argument_Count loop
1454 The_Arg : constant String := Argument (Arg);
1457 -- Check if an argument file is specified
1459 if The_Arg (The_Arg'First) = '@' then
1461 Arg_File : Ada.Text_IO.File_Type;
1462 Line : String (1 .. 256);
1466 -- Open the file and fail if the file cannot be found
1471 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1476 (Standard_Error, "Cannot open argument file """);
1479 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1481 Put_Line (Standard_Error, """");
1485 -- Read line by line and put the content of each non-
1486 -- empty line in the Last_Switches table.
1488 while not End_Of_File (Arg_File) loop
1489 Get_Line (Arg_File, Line, Last);
1492 Last_Switches.Increment_Last;
1493 Last_Switches.Table (Last_Switches.Last) :=
1494 new String'(Line (1 .. Last));
1502 -- It is not an argument file; just put the argument in
1503 -- the Last_Switches table.
1505 Last_Switches.Increment_Last;
1506 Last_Switches.Table (Last_Switches.Last) :=
1507 new String'(The_Arg);
1515 Program : String_Access;
1516 Exec_Path : String_Access;
1519 if The_Command = Stack then
1521 -- Never call gnatstack with a prefix
1523 Program := new String'(Command_List (The_Command).Unixcmd.all);
1527 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1530 -- Locate the executable for the command
1532 Exec_Path := Locate_Exec_On_Path (Program.all);
1534 if Exec_Path = null then
1535 Put_Line (Standard_Error, "could not locate " & Program.all);
1539 -- If there are switches for the executable, put them as first switches
1541 if Command_List (The_Command).Unixsws /= null then
1542 for J in Command_List (The_Command).Unixsws'Range loop
1543 First_Switches.Increment_Last;
1544 First_Switches.Table (First_Switches.Last) :=
1545 Command_List (The_Command).Unixsws (J);
1549 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, METRIC, PRETTY, STACK, STUB,
1550 -- SYNC and XREF, look for project file related switches.
1554 Tool_Package_Name := Name_Binder;
1555 Packages_To_Check := Packages_To_Check_By_Binder;
1557 Tool_Package_Name := Name_Check;
1558 Packages_To_Check := Packages_To_Check_By_Check;
1560 Tool_Package_Name := Name_Eliminate;
1561 Packages_To_Check := Packages_To_Check_By_Eliminate;
1563 Tool_Package_Name := Name_Finder;
1564 Packages_To_Check := Packages_To_Check_By_Finder;
1566 Tool_Package_Name := Name_Linker;
1567 Packages_To_Check := Packages_To_Check_By_Linker;
1569 Tool_Package_Name := Name_Gnatls;
1570 Packages_To_Check := Packages_To_Check_By_Gnatls;
1572 Tool_Package_Name := Name_Metrics;
1573 Packages_To_Check := Packages_To_Check_By_Metric;
1575 Tool_Package_Name := Name_Pretty_Printer;
1576 Packages_To_Check := Packages_To_Check_By_Pretty;
1578 Tool_Package_Name := Name_Stack;
1579 Packages_To_Check := Packages_To_Check_By_Stack;
1581 Tool_Package_Name := Name_Gnatstub;
1582 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1584 Tool_Package_Name := Name_Synchronize;
1585 Packages_To_Check := Packages_To_Check_By_Sync;
1587 Tool_Package_Name := Name_Cross_Reference;
1588 Packages_To_Check := Packages_To_Check_By_Xref;
1590 Tool_Package_Name := No_Name;
1593 if Tool_Package_Name /= No_Name then
1595 -- Check that the switches are consistent. Detect project file
1596 -- related switches.
1598 Inspect_Switches : declare
1599 Arg_Num : Positive := 1;
1600 Argv : String_Access;
1602 procedure Remove_Switch (Num : Positive);
1603 -- Remove a project related switch from table Last_Switches
1609 procedure Remove_Switch (Num : Positive) is
1611 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1612 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1613 Last_Switches.Decrement_Last;
1616 -- Start of processing for Inspect_Switches
1619 while Arg_Num <= Last_Switches.Last loop
1620 Argv := Last_Switches.Table (Arg_Num);
1622 if Argv (Argv'First) = '-' then
1623 if Argv'Length = 1 then
1625 ("switch character cannot be followed by a blank");
1628 -- The two style project files (-p and -P) cannot be used
1631 if (The_Command = Find or else The_Command = Xref)
1632 and then Argv (2) = 'p'
1634 Old_Project_File_Used := True;
1635 if Project_File /= null then
1636 Fail ("-P and -p cannot be used together");
1640 -- --subdirs=... Specify Subdirs
1642 if Argv'Length > Makeutl.Subdirs_Option'Length and then
1645 Argv'First + Makeutl.Subdirs_Option'Length - 1) =
1646 Makeutl.Subdirs_Option
1651 (Argv'First + Makeutl.Subdirs_Option'Length ..
1654 Remove_Switch (Arg_Num);
1656 -- -aPdir Add dir to the project search path
1658 elsif Argv'Length > 3
1659 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1661 Add_Search_Project_Directory
1662 (Project_Node_Tree, Argv (Argv'First + 3 .. Argv'Last));
1664 Remove_Switch (Arg_Num);
1666 -- -eL Follow links for files
1668 elsif Argv.all = "-eL" then
1669 Follow_Links_For_Files := True;
1670 Follow_Links_For_Dirs := True;
1672 Remove_Switch (Arg_Num);
1674 -- -vPx Specify verbosity while parsing project files
1676 elsif Argv'Length = 4
1677 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1679 case Argv (Argv'Last) is
1681 Current_Verbosity := Prj.Default;
1683 Current_Verbosity := Prj.Medium;
1685 Current_Verbosity := Prj.High;
1687 Fail ("Invalid switch: " & Argv.all);
1690 Remove_Switch (Arg_Num);
1692 -- -Pproject_file Specify project file to be used
1694 elsif Argv (Argv'First + 1) = 'P' then
1696 -- Only one -P switch can be used
1698 if Project_File /= null then
1701 & ": second project file forbidden (first is """
1705 -- The two style project files (-p and -P) cannot be
1708 elsif Old_Project_File_Used then
1709 Fail ("-p and -P cannot be used together");
1711 elsif Argv'Length = 2 then
1713 -- There is space between -P and the project file
1714 -- name. -P cannot be the last option.
1716 if Arg_Num = Last_Switches.Last then
1717 Fail ("project file name missing after -P");
1720 Remove_Switch (Arg_Num);
1721 Argv := Last_Switches.Table (Arg_Num);
1723 -- After -P, there must be a project file name,
1724 -- not another switch.
1726 if Argv (Argv'First) = '-' then
1727 Fail ("project file name missing after -P");
1730 Project_File := new String'(Argv.all);
1735 -- No space between -P and project file name
1738 new String'(Argv (Argv'First + 2 .. Argv'Last));
1741 Remove_Switch (Arg_Num);
1743 -- -Xexternal=value Specify an external reference to be
1744 -- used in project files
1746 elsif Argv'Length >= 5
1747 and then Argv (Argv'First + 1) = 'X'
1750 Equal_Pos : constant Natural :=
1753 Argv (Argv'First + 2 .. Argv'Last));
1755 if Equal_Pos >= Argv'First + 3 and then
1756 Equal_Pos /= Argv'Last then
1757 Add (Project_Node_Tree,
1759 Argv (Argv'First + 2 .. Equal_Pos - 1),
1760 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1764 & " is not a valid external assignment.");
1768 Remove_Switch (Arg_Num);
1771 (The_Command = Check or else
1772 The_Command = Sync or else
1773 The_Command = Pretty or else
1774 The_Command = Metric or else
1775 The_Command = Stack or else
1777 and then Argv'Length = 2
1778 and then Argv (2) = 'U'
1780 All_Projects := True;
1781 Remove_Switch (Arg_Num);
1784 Arg_Num := Arg_Num + 1;
1787 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1788 or else The_Command = Sync
1789 or else The_Command = Metric
1790 or else The_Command = Pretty)
1791 and then Project_File /= null
1792 and then All_Projects
1794 if ASIS_Main /= null then
1795 Fail ("cannot specify more than one main after -U");
1798 Remove_Switch (Arg_Num);
1802 Arg_Num := Arg_Num + 1;
1805 end Inspect_Switches;
1808 -- If there is a project file specified, parse it, get the switches
1809 -- for the tool and setup PATH environment variables.
1811 if Project_File /= null then
1812 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1815 (Project => Project,
1816 In_Tree => Project_Tree,
1817 In_Node_Tree => Project_Node_Tree,
1818 Project_File_Name => Project_File.all,
1819 Flags => Gnatmake_Flags,
1820 Packages_To_Check => Packages_To_Check);
1822 if Project = Prj.No_Project then
1823 Fail ("""" & Project_File.all & """ processing failed");
1826 -- Check if a package with the name of the tool is in the project
1827 -- file and if there is one, get the switches, if any, and scan them.
1830 Pkg : constant Prj.Package_Id :=
1832 (Name => Tool_Package_Name,
1833 In_Packages => Project.Decl.Packages,
1834 In_Tree => Project_Tree);
1836 Element : Package_Element;
1838 Switches_Array : Array_Element_Id;
1840 The_Switches : Prj.Variable_Value;
1841 Current : Prj.String_List_Id;
1842 The_String : String_Element;
1844 Main : String_Access := null;
1847 if Pkg /= No_Package then
1848 Element := Project_Tree.Packages.Table (Pkg);
1850 -- Packages Gnatls and Gnatstack have a single attribute
1851 -- Switches, that is not an associative array.
1853 if The_Command = List or else The_Command = Stack then
1856 (Variable_Name => Snames.Name_Switches,
1857 In_Variables => Element.Decl.Attributes,
1858 In_Tree => Project_Tree);
1860 -- Packages Binder (for gnatbind), Cross_Reference (for
1861 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1862 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1863 -- (for gnatcheck), and Metric (for gnatmetric) have an
1864 -- attributed Switches, an associative array, indexed by the
1865 -- name of the file.
1867 -- They also have an attribute Default_Switches, indexed by the
1868 -- name of the programming language.
1871 -- First check if there is a single main
1873 for J in 1 .. Last_Switches.Last loop
1874 if Last_Switches.Table (J) (1) /= '-' then
1876 Main := Last_Switches.Table (J);
1885 if Main /= null then
1888 (Name => Name_Switches,
1889 In_Arrays => Element.Decl.Arrays,
1890 In_Tree => Project_Tree);
1892 Add_Str_To_Name_Buffer (Main.all);
1893 The_Switches := Prj.Util.Value_Of
1894 (Index => Name_Find,
1896 In_Array => Switches_Array,
1897 In_Tree => Project_Tree);
1900 if The_Switches.Kind = Prj.Undefined then
1903 (Name => Name_Default_Switches,
1904 In_Arrays => Element.Decl.Arrays,
1905 In_Tree => Project_Tree);
1906 The_Switches := Prj.Util.Value_Of
1909 In_Array => Switches_Array,
1910 In_Tree => Project_Tree);
1914 -- If there are switches specified in the package of the
1915 -- project file corresponding to the tool, scan them.
1917 case The_Switches.Kind is
1918 when Prj.Undefined =>
1923 Switch : constant String :=
1924 Get_Name_String (The_Switches.Value);
1927 if Switch'Length > 0 then
1928 First_Switches.Increment_Last;
1929 First_Switches.Table (First_Switches.Last) :=
1930 new String'(Switch);
1935 Current := The_Switches.Values;
1936 while Current /= Prj.Nil_String loop
1937 The_String := Project_Tree.String_Elements.
1941 Switch : constant String :=
1942 Get_Name_String (The_String.Value);
1945 if Switch'Length > 0 then
1946 First_Switches.Increment_Last;
1947 First_Switches.Table (First_Switches.Last) :=
1948 new String'(Switch);
1952 Current := The_String.Next;
1958 if The_Command = Bind
1959 or else The_Command = Link
1960 or else The_Command = Elim
1962 Change_Dir (Get_Name_String (Project.Object_Directory.Name));
1965 -- Set up the env vars for project path files
1967 Prj.Env.Set_Ada_Paths
1968 (Project, Project_Tree, Including_Libraries => False);
1970 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
1971 -- a configuration pragmas file, if necessary.
1973 if The_Command = Pretty
1974 or else The_Command = Metric
1975 or else The_Command = Stub
1976 or else The_Command = Elim
1977 or else The_Command = Check
1978 or else The_Command = Sync
1980 -- If there are switches in package Compiler, put them in the
1981 -- Carg_Switches table.
1984 Pkg : constant Prj.Package_Id :=
1986 (Name => Name_Compiler,
1987 In_Packages => Project.Decl.Packages,
1988 In_Tree => Project_Tree);
1990 Element : Package_Element;
1992 Switches_Array : Array_Element_Id;
1994 The_Switches : Prj.Variable_Value;
1995 Current : Prj.String_List_Id;
1996 The_String : String_Element;
1998 Main : String_Access := null;
2002 if Pkg /= No_Package then
2004 -- First, check if there is a single main specified.
2006 for J in 1 .. Last_Switches.Last loop
2007 if Last_Switches.Table (J) (1) /= '-' then
2009 Main := Last_Switches.Table (J);
2018 Element := Project_Tree.Packages.Table (Pkg);
2020 -- If there is a single main and there is compilation
2021 -- switches specified in the project file, use them.
2023 if Main /= null and then not All_Projects then
2024 Name_Len := Main'Length;
2025 Name_Buffer (1 .. Name_Len) := Main.all;
2026 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2027 Main_Id := Name_Find;
2031 (Name => Name_Switches,
2032 In_Arrays => Element.Decl.Arrays,
2033 In_Tree => Project_Tree);
2034 The_Switches := Prj.Util.Value_Of
2037 In_Array => Switches_Array,
2038 In_Tree => Project_Tree);
2041 -- Otherwise, get the Default_Switches ("Ada")
2043 if The_Switches.Kind = Undefined then
2046 (Name => Name_Default_Switches,
2047 In_Arrays => Element.Decl.Arrays,
2048 In_Tree => Project_Tree);
2049 The_Switches := Prj.Util.Value_Of
2052 In_Array => Switches_Array,
2053 In_Tree => Project_Tree);
2056 -- If there are switches specified, put them in the
2057 -- Carg_Switches table.
2059 case The_Switches.Kind is
2060 when Prj.Undefined =>
2065 Switch : constant String :=
2066 Get_Name_String (The_Switches.Value);
2068 if Switch'Length > 0 then
2069 Add_To_Carg_Switches (new String'(Switch));
2074 Current := The_Switches.Values;
2075 while Current /= Prj.Nil_String loop
2077 Project_Tree.String_Elements.Table (Current);
2080 Switch : constant String :=
2081 Get_Name_String (The_String.Value);
2083 if Switch'Length > 0 then
2084 Add_To_Carg_Switches (new String'(Switch));
2088 Current := The_String.Next;
2094 -- If -cargs is one of the switches, move the following switches
2095 -- to the Carg_Switches table.
2097 for J in 1 .. First_Switches.Last loop
2098 if First_Switches.Table (J).all = "-cargs" then
2104 -- Move the switches that are before -rules when the
2105 -- command is CHECK.
2108 while K <= First_Switches.Last
2110 (The_Command /= Check
2111 or else First_Switches.Table (K).all /= "-rules")
2113 Add_To_Carg_Switches (First_Switches.Table (K));
2117 if K > First_Switches.Last then
2118 First_Switches.Set_Last (J - 1);
2122 while K <= First_Switches.Last loop
2124 First_Switches.Table (Last) :=
2125 First_Switches.Table (K);
2129 First_Switches.Set_Last (Last);
2137 for J in 1 .. Last_Switches.Last loop
2138 if Last_Switches.Table (J).all = "-cargs" then
2144 -- Move the switches that are before -rules when the
2145 -- command is CHECK.
2148 while K <= Last_Switches.Last
2150 (The_Command /= Check
2152 Last_Switches.Table (K).all /= "-rules")
2154 Add_To_Carg_Switches (Last_Switches.Table (K));
2158 if K > Last_Switches.Last then
2159 Last_Switches.Set_Last (J - 1);
2163 while K <= Last_Switches.Last loop
2165 Last_Switches.Table (Last) :=
2166 Last_Switches.Table (K);
2170 Last_Switches.Set_Last (Last);
2179 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2180 M_File : constant Path_Name_Type := Mapping_File;
2183 if CP_File /= No_Path then
2184 if The_Command = Elim then
2185 First_Switches.Increment_Last;
2186 First_Switches.Table (First_Switches.Last) :=
2187 new String'("-C" & Get_Name_String (CP_File));
2190 Add_To_Carg_Switches
2191 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2195 if M_File /= No_Path then
2196 Add_To_Carg_Switches
2197 (new String'("-gnatem=" & Get_Name_String (M_File)));
2202 if The_Command = Link then
2206 if The_Command = Link or else The_Command = Bind then
2208 -- For files that are specified as relative paths with directory
2209 -- information, we convert them to absolute paths, with parent
2210 -- being the current working directory if specified on the command
2211 -- line and the project directory if specified in the project
2212 -- file. This is what gnatmake is doing for linker and binder
2215 for J in 1 .. Last_Switches.Last loop
2216 GNATCmd.Test_If_Relative_Path
2217 (Last_Switches.Table (J), Current_Work_Dir);
2220 Get_Name_String (Project.Directory.Name);
2223 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2225 for J in 1 .. First_Switches.Last loop
2226 GNATCmd.Test_If_Relative_Path
2227 (First_Switches.Table (J), Project_Dir);
2231 elsif The_Command = Stub then
2233 File_Index : Integer := 0;
2234 Dir_Index : Integer := 0;
2235 Last : constant Integer := Last_Switches.Last;
2236 Lang : constant Language_Ptr :=
2237 Get_Language_From_Name (Project, "ada");
2240 for Index in 1 .. Last loop
2241 if Last_Switches.Table (Index)
2242 (Last_Switches.Table (Index)'First) /= '-'
2244 File_Index := Index;
2249 -- If the project file naming scheme is not standard, and if
2250 -- the file name ends with the spec suffix, then indicate to
2251 -- gnatstub the name of the body file with a -o switch.
2253 if not Is_Standard_GNAT_Naming (Lang.Config.Naming_Data) then
2254 if File_Index /= 0 then
2256 Spec : constant String :=
2258 (Last_Switches.Table (File_Index).all);
2259 Last : Natural := Spec'Last;
2262 Get_Name_String (Lang.Config.Naming_Data.Spec_Suffix);
2264 if Spec'Length > Name_Len
2265 and then Spec (Last - Name_Len + 1 .. Last) =
2266 Name_Buffer (1 .. Name_Len)
2268 Last := Last - Name_Len;
2270 (Lang.Config.Naming_Data.Body_Suffix);
2271 Last_Switches.Increment_Last;
2272 Last_Switches.Table (Last_Switches.Last) :=
2274 Last_Switches.Increment_Last;
2275 Last_Switches.Table (Last_Switches.Last) :=
2276 new String'(Spec (Spec'First .. Last) &
2277 Name_Buffer (1 .. Name_Len));
2283 -- Add the directory of the spec as the destination directory
2284 -- of the body, if there is no destination directory already
2287 if File_Index /= 0 then
2288 for Index in File_Index + 1 .. Last loop
2289 if Last_Switches.Table (Index)
2290 (Last_Switches.Table (Index)'First) /= '-'
2297 if Dir_Index = 0 then
2298 Last_Switches.Increment_Last;
2299 Last_Switches.Table (Last_Switches.Last) :=
2301 (Dir_Name (Last_Switches.Table (File_Index).all));
2307 -- For gnatmetric, the generated files should be put in the object
2308 -- directory. This must be the first switch, because it may be
2309 -- overridden by a switch in package Metrics in the project file or
2310 -- by a command line option. Note that we don't add the -d= switch
2311 -- if there is no object directory available.
2313 if The_Command = Metric
2314 and then Project.Object_Directory /= No_Path_Information
2316 First_Switches.Increment_Last;
2317 First_Switches.Table (2 .. First_Switches.Last) :=
2318 First_Switches.Table (1 .. First_Switches.Last - 1);
2319 First_Switches.Table (1) :=
2321 Get_Name_String (Project.Object_Directory.Name));
2324 -- For gnat check, -rules and the following switches need to be the
2325 -- last options, so move all these switches to table Rules_Switches.
2327 if The_Command = Check then
2330 -- Set to rank of options preceding "-rules"
2332 In_Rules_Switches : Boolean;
2333 -- Set to True when options "-rules" is found
2336 New_Last := First_Switches.Last;
2337 In_Rules_Switches := False;
2339 for J in 1 .. First_Switches.Last loop
2340 if In_Rules_Switches then
2341 Add_To_Rules_Switches (First_Switches.Table (J));
2343 elsif First_Switches.Table (J).all = "-rules" then
2345 In_Rules_Switches := True;
2349 if In_Rules_Switches then
2350 First_Switches.Set_Last (New_Last);
2353 New_Last := Last_Switches.Last;
2354 In_Rules_Switches := False;
2356 for J in 1 .. Last_Switches.Last loop
2357 if In_Rules_Switches then
2358 Add_To_Rules_Switches (Last_Switches.Table (J));
2360 elsif Last_Switches.Table (J).all = "-rules" then
2362 In_Rules_Switches := True;
2366 if In_Rules_Switches then
2367 Last_Switches.Set_Last (New_Last);
2372 -- For gnat check, sync, metric or pretty with -U + a main, get the
2373 -- list of sources from the closure and add them to the arguments.
2375 if ASIS_Main /= null then
2378 -- On VMS, set up the env var again for source dirs file. This is
2379 -- because the call to gnatmake has set this env var to another
2380 -- file that has now been deleted.
2382 if Hostparm.OpenVMS then
2384 -- First make sure that the recorded file names are empty
2386 Prj.Env.Initialize (Project_Tree);
2388 Prj.Env.Set_Ada_Paths
2389 (Project, Project_Tree, Including_Libraries => False);
2392 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2393 -- and gnat stack, if no file has been put on the command line, call
2394 -- tool with all the sources of the main project.
2396 elsif The_Command = Check or else
2397 The_Command = Sync or else
2398 The_Command = Pretty or else
2399 The_Command = Metric or else
2400 The_Command = List or else
2407 -- Gather all the arguments and invoke the executable
2410 The_Args : Argument_List
2411 (1 .. First_Switches.Last +
2412 Last_Switches.Last +
2413 Carg_Switches.Last +
2414 Rules_Switches.Last);
2415 Arg_Num : Natural := 0;
2418 for J in 1 .. First_Switches.Last loop
2419 Arg_Num := Arg_Num + 1;
2420 The_Args (Arg_Num) := First_Switches.Table (J);
2423 for J in 1 .. Last_Switches.Last loop
2424 Arg_Num := Arg_Num + 1;
2425 The_Args (Arg_Num) := Last_Switches.Table (J);
2428 for J in 1 .. Carg_Switches.Last loop
2429 Arg_Num := Arg_Num + 1;
2430 The_Args (Arg_Num) := Carg_Switches.Table (J);
2433 for J in 1 .. Rules_Switches.Last loop
2434 Arg_Num := Arg_Num + 1;
2435 The_Args (Arg_Num) := Rules_Switches.Table (J);
2438 -- If Display_Command is on, only display the generated command
2440 if Display_Command then
2441 Put (Standard_Error, "generated command -->");
2442 Put (Standard_Error, Exec_Path.all);
2444 for Arg in The_Args'Range loop
2445 Put (Standard_Error, " ");
2446 Put (Standard_Error, The_Args (Arg).all);
2449 Put (Standard_Error, "<--");
2450 New_Line (Standard_Error);
2454 if Verbose_Mode then
2455 Output.Write_Str (Exec_Path.all);
2457 for Arg in The_Args'Range loop
2458 Output.Write_Char (' ');
2459 Output.Write_Str (The_Args (Arg).all);
2466 Exit_Status (Spawn (Exec_Path.all, The_Args));
2473 if not Keep_Temporary_Files then
2474 Prj.Delete_All_Temp_Files (Project_Tree);
2475 Delete_Temp_Config_Files;
2478 Set_Exit_Status (Failure);
2481 if not Keep_Temporary_Files then
2482 Prj.Delete_All_Temp_Files (Project_Tree);
2483 Delete_Temp_Config_Files;
2486 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2487 -- return an understandable VMS exit status. However the exit status
2488 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2489 -- just a simple success or failure on VMS.
2491 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2492 Set_Exit_Status (Failure);
2494 Set_Exit_Status (My_Exit_Status);