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 Hostparm; use Hostparm;
30 with Makeutl; use Makeutl;
31 with MLib.Tgt; use MLib.Tgt;
34 with Namet; use Namet;
36 with Osint; use Osint;
40 with Prj.Ext; use Prj.Ext;
42 with Prj.Tree; use Prj.Tree;
43 with Prj.Util; use Prj.Util;
45 with Snames; use Snames;
49 with Types; use Types;
50 with VMS_Conv; use VMS_Conv;
51 with VMS_Cmds; use VMS_Cmds;
53 with Ada.Characters.Handling; use Ada.Characters.Handling;
54 with Ada.Command_Line; use Ada.Command_Line;
55 with Ada.Text_IO; use Ada.Text_IO;
57 with GNAT.OS_Lib; use GNAT.OS_Lib;
60 Project_Node_Tree : Project_Node_Tree_Ref;
61 Project_File : String_Access;
62 Project : Prj.Project_Id;
63 Current_Verbosity : Prj.Verbosity := Prj.Default;
64 Tool_Package_Name : Name_Id := No_Name;
66 B_Start : String_Ptr := new String'("b~");
67 -- Prefix of binder generated file, changed to b__ for VMS
69 Old_Project_File_Used : Boolean := False;
70 -- This flag indicates a switch -p (for gnatxref and gnatfind) for
71 -- an old fashioned project file. -p cannot be used in conjunction
74 Temp_File_Name : Path_Name_Type := No_Path;
75 -- The name of the temporary text file to put a list of source/object
76 -- files to pass to a tool.
78 ASIS_Main : String_Access := null;
79 -- Main for commands Check, Metric and Pretty, when -U is used
81 package First_Switches is new Table.Table
82 (Table_Component_Type => String_Access,
83 Table_Index_Type => Integer,
86 Table_Increment => 100,
87 Table_Name => "Gnatcmd.First_Switches");
88 -- A table to keep the switches from the project file
90 package Carg_Switches is new Table.Table
91 (Table_Component_Type => String_Access,
92 Table_Index_Type => Integer,
95 Table_Increment => 100,
96 Table_Name => "Gnatcmd.Carg_Switches");
97 -- A table to keep the switches following -cargs for ASIS tools
99 package Rules_Switches is new Table.Table
100 (Table_Component_Type => String_Access,
101 Table_Index_Type => Integer,
102 Table_Low_Bound => 1,
104 Table_Increment => 100,
105 Table_Name => "Gnatcmd.Rules_Switches");
106 -- A table to keep the switches following -rules for gnatcheck
108 package Library_Paths is new Table.Table (
109 Table_Component_Type => String_Access,
110 Table_Index_Type => Integer,
111 Table_Low_Bound => 1,
113 Table_Increment => 100,
114 Table_Name => "Make.Library_Path");
116 -- Packages of project files to pass to Prj.Pars.Parse, depending on the
117 -- tool. We allocate objects because we cannot declare aliased objects
118 -- as we are in a procedure, not a library level package.
120 subtype SA is String_Access;
122 Naming_String : constant SA := new String'("naming");
123 Binder_String : constant SA := new String'("binder");
124 Builder_String : constant SA := new String'("builder");
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 :=
143 ((Naming_String, Builder_String, Check_String, Compiler_String));
145 Packages_To_Check_By_Sync : constant String_List_Access :=
146 new String_List'((Naming_String, Synchronize_String, Compiler_String));
148 Packages_To_Check_By_Eliminate : constant String_List_Access :=
149 new String_List'((Naming_String, Eliminate_String, Compiler_String));
151 Packages_To_Check_By_Finder : constant String_List_Access :=
152 new String_List'((Naming_String, Finder_String));
154 Packages_To_Check_By_Linker : constant String_List_Access :=
155 new String_List'((Naming_String, Linker_String));
157 Packages_To_Check_By_Gnatls : constant String_List_Access :=
158 new String_List'((Naming_String, Gnatls_String));
160 Packages_To_Check_By_Pretty : constant String_List_Access :=
161 new String_List'((Naming_String, Pretty_String, Compiler_String));
163 Packages_To_Check_By_Stack : constant String_List_Access :=
164 new String_List'((Naming_String, Stack_String));
166 Packages_To_Check_By_Gnatstub : constant String_List_Access :=
167 new String_List'((Naming_String, Gnatstub_String, Compiler_String));
169 Packages_To_Check_By_Metric : constant String_List_Access :=
170 new String_List'((Naming_String, Metric_String, Compiler_String));
172 Packages_To_Check_By_Xref : constant String_List_Access :=
173 new String_List'((Naming_String, Xref_String));
175 Packages_To_Check : String_List_Access := Prj.All_Packages;
177 ----------------------------------
178 -- Declarations for GNATCMD use --
179 ----------------------------------
181 The_Command : Command_Type;
182 -- The command specified in the invocation of the GNAT driver
184 Command_Arg : Positive := 1;
185 -- The index of the command in the arguments of the GNAT driver
187 My_Exit_Status : Exit_Status := Success;
188 -- The exit status of the spawned tool. Used to set the correct VMS
191 Current_Work_Dir : constant String := Get_Current_Dir;
192 -- The path of the working directory
194 All_Projects : Boolean := False;
195 -- Flag used for GNAT CHECK, GNAT PRETTY, GNAT METRIC, and GNAT STACK to
196 -- indicate that the underlying tool (gnatcheck, gnatpp or gnatmetric)
197 -- should be invoked for all sources of all projects.
199 -----------------------
200 -- Local Subprograms --
201 -----------------------
203 procedure Add_To_Carg_Switches (Switch : String_Access);
204 -- Add a switch to the Carg_Switches table. If it is the first one, put the
205 -- switch "-cargs" at the beginning of the table.
207 procedure Add_To_Rules_Switches (Switch : String_Access);
208 -- Add a switch to the Rules_Switches table. If it is the first one, put
209 -- the switch "-crules" at the beginning of the table.
211 procedure Check_Files;
212 -- For GNAT LIST, GNAT PRETTY, GNAT METRIC, and GNAT STACK, check if a
213 -- project file is specified, without any file arguments and without a
214 -- switch -files=. If it is the case, invoke the GNAT tool with the proper
215 -- list of files, derived from the sources of the project.
217 function Check_Project
218 (Project : Project_Id;
219 Root_Project : Project_Id) return Boolean;
220 -- Returns True if Project = Root_Project or if we want to consider all
221 -- sources of all projects. For GNAT METRIC, also returns True if Project
222 -- is extended by Root_Project.
224 procedure Check_Relative_Executable (Name : in out String_Access);
225 -- Check if an executable is specified as a relative path. If it is, and
226 -- the path contains directory information, fail. Otherwise, prepend the
227 -- exec directory. This procedure is only used for GNAT LINK when a project
228 -- file is specified.
230 function Configuration_Pragmas_File return Path_Name_Type;
231 -- Return an argument, if there is a configuration pragmas file to be
232 -- specified for Project, otherwise return No_Name. Used for gnatstub (GNAT
233 -- STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric (GNAT
236 function Mapping_File return Path_Name_Type;
237 -- Create and return the path name of a mapping file. Used for gnatstub
238 -- (GNAT STUB), gnatpp (GNAT PRETTY), gnatelim (GNAT ELIM), and gnatmetric
241 procedure Delete_Temp_Config_Files;
242 -- Delete all temporary config files. The caller is responsible for
243 -- ensuring that Keep_Temporary_Files is False.
245 procedure Get_Closure;
246 -- Get the sources in the closure of the ASIS_Main and add them to the
247 -- list of arguments.
249 function Index (Char : Character; Str : String) return Natural;
250 -- Returns first occurrence of Char in Str, returns 0 if Char not in Str
252 procedure Non_VMS_Usage;
253 -- Display usage for platforms other than VMS
255 procedure Process_Link;
256 -- Process GNAT LINK, when there is a project file specified
258 procedure Set_Library_For
259 (Project : Project_Id;
260 Libraries_Present : in out Boolean);
261 -- If Project is a library project, add the correct -L and -l switches to
262 -- the linker invocation.
264 procedure Set_Libraries is
265 new For_Every_Project_Imported (Boolean, Set_Library_For);
266 -- Add the -L and -l switches to the linker for all of the library
269 procedure Test_If_Relative_Path
270 (Switch : in out String_Access;
272 -- Test if Switch is a relative search path switch. If it is and it
273 -- includes directory information, prepend the path with Parent. This
274 -- subprogram is only called when using project files.
276 --------------------------
277 -- Add_To_Carg_Switches --
278 --------------------------
280 procedure Add_To_Carg_Switches (Switch : String_Access) is
282 -- If the Carg_Switches table is empty, put "-cargs" at the beginning
284 if Carg_Switches.Last = 0 then
285 Carg_Switches.Increment_Last;
286 Carg_Switches.Table (Carg_Switches.Last) := new String'("-cargs");
289 Carg_Switches.Increment_Last;
290 Carg_Switches.Table (Carg_Switches.Last) := Switch;
291 end Add_To_Carg_Switches;
293 ---------------------------
294 -- Add_To_Rules_Switches --
295 ---------------------------
297 procedure Add_To_Rules_Switches (Switch : String_Access) is
299 -- If the Rules_Switches table is empty, put "-rules" at the beginning
301 if Rules_Switches.Last = 0 then
302 Rules_Switches.Increment_Last;
303 Rules_Switches.Table (Rules_Switches.Last) := new String'("-rules");
306 Rules_Switches.Increment_Last;
307 Rules_Switches.Table (Rules_Switches.Last) := Switch;
308 end Add_To_Rules_Switches;
314 procedure Check_Files is
315 Add_Sources : Boolean := True;
316 Unit : Prj.Unit_Index;
317 Subunit : Boolean := False;
318 FD : File_Descriptor := Invalid_FD;
323 -- Check if there is at least one argument that is not a switch or if
324 -- there is a -files= switch.
326 for Index in 1 .. Last_Switches.Last loop
327 if Last_Switches.Table (Index).all'Length > 7
328 and then Last_Switches.Table (Index) (1 .. 7) = "-files="
330 Add_Sources := False;
333 elsif Last_Switches.Table (Index) (1) /= '-' then
337 and then Last_Switches.Table (Index - 1).all /= "-o")
339 (The_Command = Pretty
340 and then Last_Switches.Table (Index - 1).all /= "-o"
341 and then Last_Switches.Table (Index - 1).all /= "-of")
343 (The_Command = Metric
345 Last_Switches.Table (Index - 1).all /= "-o" and then
346 Last_Switches.Table (Index - 1).all /= "-og" and then
347 Last_Switches.Table (Index - 1).all /= "-ox" and then
348 Last_Switches.Table (Index - 1).all /= "-d")
350 (The_Command /= Check and then
351 The_Command /= Pretty and then
352 The_Command /= Metric)
354 Add_Sources := False;
360 -- If all arguments are switches and there is no switch -files=, add
361 -- the path names of all the sources of the main project.
365 -- For gnatcheck, gnatpp, and gnatmetric, create a temporary file
366 -- and put the list of sources in it.
368 if The_Command = Check or else
369 The_Command = Pretty or else
372 Tempdir.Create_Temp_File (FD, Temp_File_Name);
373 Last_Switches.Increment_Last;
374 Last_Switches.Table (Last_Switches.Last) :=
375 new String'("-files=" & Get_Name_String (Temp_File_Name));
382 -- Gnatstack needs to add the .ci file for the binder generated
383 -- files corresponding to all of the library projects and main
384 -- units belonging to the application.
386 if The_Command = Stack then
387 Proj := Project_Tree.Projects;
388 while Proj /= null loop
389 if Check_Project (Proj.Project, Project) then
391 Main : String_List_Id;
392 File : String_Access;
395 -- Include binder generated files for main programs
397 Main := Proj.Project.Mains;
398 while Main /= Nil_String loop
402 (Proj.Project.Object_Directory.Name) &
406 (Project_Tree.String_Elements.Table
410 if Is_Regular_File (File.all) then
411 Last_Switches.Increment_Last;
412 Last_Switches.Table (Last_Switches.Last) := File;
416 Project_Tree.String_Elements.Table (Main).Next;
419 if Proj.Project.Library then
421 -- Include the .ci file for the binder generated
422 -- files that contains the initialization and
423 -- finalization of the library.
428 (Proj.Project.Object_Directory.Name) &
430 Get_Name_String (Proj.Project.Library_Name) &
433 if Is_Regular_File (File.all) then
434 Last_Switches.Increment_Last;
435 Last_Switches.Table (Last_Switches.Last) := File;
445 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
446 while Unit /= No_Unit_Index loop
448 -- For gnatls, we only need to put the library units, body or
449 -- spec, but not the subunits.
451 if The_Command = List then
452 if Unit.File_Names (Impl) /= null
453 and then not Unit.File_Names (Impl).Locally_Removed
455 -- There is a body, check if it is for this project
458 or else Unit.File_Names (Impl).Project = Project
462 if Unit.File_Names (Spec) = null
463 or else Unit.File_Names (Spec).Locally_Removed
465 -- We have a body with no spec: we need to check if
466 -- this is a subunit, because gnatls will complain
470 Src_Ind : constant Source_File_Index :=
471 Sinput.P.Load_Project_File
477 Sinput.P.Source_File_Is_Subunit (Src_Ind);
482 Last_Switches.Increment_Last;
483 Last_Switches.Table (Last_Switches.Last) :=
487 (Impl).Display_File));
491 elsif Unit.File_Names (Spec) /= null
492 and then not Unit.File_Names (Spec).Locally_Removed
494 -- We have a spec with no body. Check if it is for this
497 if All_Projects or else
498 Unit.File_Names (Spec).Project = Project
500 Last_Switches.Increment_Last;
501 Last_Switches.Table (Last_Switches.Last) :=
502 new String'(Get_Name_String
503 (Unit.File_Names (Spec).Display_File));
507 -- For gnatstack, we put the .ci files corresponding to the
508 -- different units, including the binder generated files. We
509 -- only need to do that for the library units, body or spec,
510 -- but not the subunits.
512 elsif The_Command = Stack then
513 if Unit.File_Names (Impl) /= null
514 and then not Unit.File_Names (Impl).Locally_Removed
516 -- There is a body. Check if .ci files for this project
520 (Unit.File_Names (Impl).Project, Project)
524 if Unit.File_Names (Spec) = null
525 or else Unit.File_Names (Spec).Locally_Removed
527 -- We have a body with no spec: we need to check
528 -- if this is a subunit, because .ci files are not
529 -- generated for subunits.
532 Src_Ind : constant Source_File_Index :=
533 Sinput.P.Load_Project_File
539 Sinput.P.Source_File_Is_Subunit (Src_Ind);
544 Last_Switches.Increment_Last;
545 Last_Switches.Table (Last_Switches.Last) :=
549 (Impl).Project. Object_Directory.Name) &
552 (Unit.File_Names (Impl).Display_File),
557 elsif Unit.File_Names (Spec) /= null
558 and then not Unit.File_Names (Spec).Locally_Removed
560 -- Spec with no body, check if it is for this project
563 (Unit.File_Names (Spec).Project, Project)
565 Last_Switches.Increment_Last;
566 Last_Switches.Table (Last_Switches.Last) :=
570 (Spec).Project. Object_Directory.Name) &
573 (Get_Name_String (Unit.File_Names (Spec).File),
579 -- For gnatcheck, gnatsync, gnatpp and gnatmetric, put all
580 -- sources of the project, or of all projects if -U was
583 for Kind in Spec_Or_Body loop
584 if Unit.File_Names (Kind) /= null
585 and then Check_Project
586 (Unit.File_Names (Kind).Project, Project)
587 and then not Unit.File_Names (Kind).Locally_Removed
590 Add_Char_To_Name_Buffer ('"');
591 Add_Str_To_Name_Buffer
593 (Unit.File_Names (Kind).Path.Display_Name));
594 Add_Char_To_Name_Buffer ('"');
596 if FD /= Invalid_FD then
597 Name_Len := Name_Len + 1;
598 Name_Buffer (Name_Len) := ASCII.LF;
600 Write (FD, Name_Buffer (1)'Address, Name_Len);
602 if Status /= Name_Len then
603 Osint.Fail ("disk full");
607 Last_Switches.Increment_Last;
608 Last_Switches.Table (Last_Switches.Last) :=
609 new String'(Get_Name_String
611 (Kind).Path.Display_Name));
617 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
621 if FD /= Invalid_FD then
625 Osint.Fail ("disk full");
635 function Check_Project
636 (Project : Project_Id;
637 Root_Project : Project_Id) return Boolean
642 if Project = No_Project then
645 elsif All_Projects or else Project = Root_Project then
648 elsif The_Command = Metric then
649 Proj := Root_Project;
650 while Proj.Extends /= No_Project loop
651 if Project = Proj.Extends then
655 Proj := Proj.Extends;
662 -------------------------------
663 -- Check_Relative_Executable --
664 -------------------------------
666 procedure Check_Relative_Executable (Name : in out String_Access) is
667 Exec_File_Name : constant String := Name.all;
670 if not Is_Absolute_Path (Exec_File_Name) then
671 for Index in Exec_File_Name'Range loop
672 if Exec_File_Name (Index) = Directory_Separator then
673 Fail ("relative executable (""" &
675 """) with directory part not allowed " &
676 "when using project files");
680 Get_Name_String (Project.Exec_Directory.Name);
682 if Name_Buffer (Name_Len) /= Directory_Separator then
683 Name_Len := Name_Len + 1;
684 Name_Buffer (Name_Len) := Directory_Separator;
687 Name_Buffer (Name_Len + 1 ..
688 Name_Len + Exec_File_Name'Length) :=
690 Name_Len := Name_Len + Exec_File_Name'Length;
691 Name := new String'(Name_Buffer (1 .. Name_Len));
693 end Check_Relative_Executable;
695 --------------------------------
696 -- Configuration_Pragmas_File --
697 --------------------------------
699 function Configuration_Pragmas_File return Path_Name_Type is
701 Prj.Env.Create_Config_Pragmas_File (Project, Project_Tree);
702 return Project.Config_File_Name;
703 end Configuration_Pragmas_File;
705 ------------------------------
706 -- Delete_Temp_Config_Files --
707 ------------------------------
709 procedure Delete_Temp_Config_Files is
712 pragma Warnings (Off, Success);
715 -- This should only be called if Keep_Temporary_Files is False
717 pragma Assert (not Keep_Temporary_Files);
719 if Project /= No_Project then
720 Proj := Project_Tree.Projects;
721 while Proj /= null loop
722 if Proj.Project.Config_File_Temp then
723 Delete_Temporary_File
724 (Project_Tree, Proj.Project.Config_File_Name);
731 -- If a temporary text file that contains a list of files for a tool
732 -- has been created, delete this temporary file.
734 if Temp_File_Name /= No_Path then
735 Delete_Temporary_File (Project_Tree, Temp_File_Name);
737 end Delete_Temp_Config_Files;
743 procedure Get_Closure is
744 Args : constant Argument_List :=
745 (1 => new String'("-q"),
746 2 => new String'("-b"),
747 3 => new String'("-P"),
750 6 => new String'("-bargs"),
751 7 => new String'("-R"),
752 8 => new String'("-Z"));
753 -- Arguments for the invocation of gnatmake which are added to the
754 -- Last_Arguments list by this procedure.
756 FD : File_Descriptor;
757 -- File descriptor for the temp file that will get the output of the
758 -- invocation of gnatmake.
760 Name : Path_Name_Type;
761 -- Path of the file FD
763 GN_Name : constant String := Program_Name ("gnatmake", "gnat").all;
766 GN_Path : constant String_Access := Locate_Exec_On_Path (GN_Name);
769 Return_Code : Integer;
772 pragma Warnings (Off, Unused);
774 File : Ada.Text_IO.File_Type;
775 Line : String (1 .. 250);
777 -- Used to read file if there is an error, it is good enough to display
778 -- just 250 characters if the first line of the file is very long.
781 Path : Path_Name_Type;
784 if GN_Path = null then
785 Put_Line (Standard_Error, "could not locate " & GN_Name);
789 -- Create the temp file
791 Tempdir.Create_Temp_File (FD, Name);
793 -- And close it, because on VMS Spawn with a file descriptor created
794 -- with Create_Temp_File does not redirect output.
798 -- Spawn "gnatmake -q -b -P <project> <main> -bargs -R -Z"
801 (Program_Name => GN_Path.all,
803 Output_File => Get_Name_String (Name),
805 Return_Code => Return_Code,
810 -- Read the output of the invocation of gnatmake
812 Open (File, In_File, Get_Name_String (Name));
814 -- If it was unsuccessful, display the first line in the file and exit
817 if Return_Code /= 0 then
818 Get_Line (File, Line, Last);
820 if not Keep_Temporary_Files then
826 Put_Line (Standard_Error, Line (1 .. Last));
828 (Standard_Error, "could not get closure of " & ASIS_Main.all);
832 -- Get each file name in the file, find its path and add it the
833 -- list of arguments.
835 while not End_Of_File (File) loop
836 Get_Line (File, Line, Last);
839 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
840 while Unit /= No_Unit_Index loop
841 if Unit.File_Names (Spec) /= null
843 Get_Name_String (Unit.File_Names (Spec).File) =
846 Path := Unit.File_Names (Spec).Path.Name;
849 elsif Unit.File_Names (Impl) /= null
851 Get_Name_String (Unit.File_Names (Impl).File) =
854 Path := Unit.File_Names (Impl).Path.Name;
858 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
861 Last_Switches.Increment_Last;
863 if Path /= No_Path then
864 Last_Switches.Table (Last_Switches.Last) :=
865 new String'(Get_Name_String (Path));
868 Last_Switches.Table (Last_Switches.Last) :=
869 new String'(Line (1 .. Last));
873 if not Keep_Temporary_Files then
885 function Index (Char : Character; Str : String) return Natural is
887 for Index in Str'Range loop
888 if Str (Index) = Char then
900 function Mapping_File return Path_Name_Type is
901 Result : Path_Name_Type;
903 Prj.Env.Create_Mapping_File
905 Language => Name_Ada,
906 In_Tree => Project_Tree,
915 procedure Process_Link is
916 Look_For_Executable : Boolean := True;
917 Libraries_Present : Boolean := False;
918 Path_Option : constant String_Access :=
919 MLib.Linker_Library_Path_Option;
920 Prj : Project_Id := Project;
923 Skip_Executable : Boolean := False;
926 -- Add the default search directories, to be able to find
927 -- libgnat in call to MLib.Utl.Lib_Directory.
929 Add_Default_Search_Dirs;
931 Library_Paths.Set_Last (0);
933 -- Check if there are library project files
935 if MLib.Tgt.Support_For_Libraries /= None then
936 Set_Libraries (Project, Libraries_Present);
939 -- If there are, add the necessary additional switches
941 if Libraries_Present then
943 -- Add -L<lib_dir> -lgnarl -lgnat -Wl,-rpath,<lib_dir>
945 Last_Switches.Increment_Last;
946 Last_Switches.Table (Last_Switches.Last) :=
947 new String'("-L" & MLib.Utl.Lib_Directory);
948 Last_Switches.Increment_Last;
949 Last_Switches.Table (Last_Switches.Last) :=
950 new String'("-lgnarl");
951 Last_Switches.Increment_Last;
952 Last_Switches.Table (Last_Switches.Last) :=
953 new String'("-lgnat");
955 -- If Path_Option is not null, create the switch ("-Wl,-rpath," or
956 -- equivalent) with all the library dirs plus the standard GNAT
959 if Path_Option /= null then
961 Option : String_Access;
962 Length : Natural := Path_Option'Length;
966 if MLib.Separate_Run_Path_Options then
968 -- We are going to create one switch of the form
969 -- "-Wl,-rpath,dir_N" for each directory to consider.
971 -- One switch for each library directory
974 Library_Paths.First .. Library_Paths.Last
976 Last_Switches.Increment_Last;
978 (Last_Switches.Last) := new String'
980 Last_Switches.Table (Index).all);
983 -- One switch for the standard GNAT library dir
985 Last_Switches.Increment_Last;
987 (Last_Switches.Last) := new String'
988 (Path_Option.all & MLib.Utl.Lib_Directory);
991 -- First, compute the exact length for the switch
994 Library_Paths.First .. Library_Paths.Last
996 -- Add the length of the library dir plus one for the
997 -- directory separator.
1001 Library_Paths.Table (Index)'Length + 1;
1004 -- Finally, add the length of the standard GNAT library dir
1006 Length := Length + MLib.Utl.Lib_Directory'Length;
1007 Option := new String (1 .. Length);
1008 Option (1 .. Path_Option'Length) := Path_Option.all;
1009 Current := Path_Option'Length;
1011 -- Put each library dir followed by a dir separator
1014 Library_Paths.First .. Library_Paths.Last
1019 Library_Paths.Table (Index)'Length) :=
1020 Library_Paths.Table (Index).all;
1023 Library_Paths.Table (Index)'Length + 1;
1024 Option (Current) := Path_Separator;
1027 -- Finally put the standard GNAT library dir
1031 Current + MLib.Utl.Lib_Directory'Length) :=
1032 MLib.Utl.Lib_Directory;
1034 -- And add the switch to the last switches
1036 Last_Switches.Increment_Last;
1037 Last_Switches.Table (Last_Switches.Last) :=
1044 -- Check if the first ALI file specified can be found, either in the
1045 -- object directory of the main project or in an object directory of a
1046 -- project file extended by the main project. If the ALI file can be
1047 -- found, replace its name with its absolute path.
1049 Skip_Executable := False;
1051 Switch_Loop : for J in 1 .. Last_Switches.Last loop
1053 -- If we have an executable just reset the flag
1055 if Skip_Executable then
1056 Skip_Executable := False;
1058 -- If -o, set flag so that next switch is not processed
1060 elsif Last_Switches.Table (J).all = "-o" then
1061 Skip_Executable := True;
1067 Switch : constant String :=
1068 Last_Switches.Table (J).all;
1069 ALI_File : constant String (1 .. Switch'Length + 4) :=
1072 Test_Existence : Boolean := False;
1075 Last := Switch'Length;
1077 -- Skip real switches
1079 if Switch'Length /= 0
1080 and then Switch (Switch'First) /= '-'
1082 -- Append ".ali" if file name does not end with it
1084 if Switch'Length <= 4
1085 or else Switch (Switch'Last - 3 .. Switch'Last) /= ".ali"
1087 Last := ALI_File'Last;
1090 -- If file name includes directory information, stop if ALI
1093 if Is_Absolute_Path (ALI_File (1 .. Last)) then
1094 Test_Existence := True;
1097 for K in Switch'Range loop
1099 or else Switch (K) = Directory_Separator
1101 Test_Existence := True;
1107 if Test_Existence then
1108 if Is_Regular_File (ALI_File (1 .. Last)) then
1112 -- Look in object directories if ALI file exists
1117 Dir : constant String :=
1118 Get_Name_String (Prj.Object_Directory.Name);
1122 ALI_File (1 .. Last))
1124 -- We have found the correct project, so we
1125 -- replace the file with the absolute path.
1127 Last_Switches.Table (J) :=
1128 new String'(Dir & ALI_File (1 .. Last));
1136 -- Go to the project being extended, if any
1139 exit Project_Loop when Prj = No_Project;
1140 end loop Project_Loop;
1145 end loop Switch_Loop;
1147 -- If a relative path output file has been specified, we add the exec
1150 for J in reverse 1 .. Last_Switches.Last - 1 loop
1151 if Last_Switches.Table (J).all = "-o" then
1152 Check_Relative_Executable
1153 (Name => Last_Switches.Table (J + 1));
1154 Look_For_Executable := False;
1159 if Look_For_Executable then
1160 for J in reverse 1 .. First_Switches.Last - 1 loop
1161 if First_Switches.Table (J).all = "-o" then
1162 Look_For_Executable := False;
1163 Check_Relative_Executable
1164 (Name => First_Switches.Table (J + 1));
1170 -- If no executable is specified, then find the name of the first ALI
1171 -- file on the command line and issue a -o switch with the absolute path
1172 -- of the executable in the exec directory.
1174 if Look_For_Executable then
1175 for J in 1 .. Last_Switches.Last loop
1176 Arg := Last_Switches.Table (J);
1179 if Arg'Length /= 0 and then Arg (Arg'First) /= '-' then
1181 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
1183 Last := Arg'Last - 4;
1185 elsif Is_Regular_File (Arg.all & ".ali") then
1190 Last_Switches.Increment_Last;
1191 Last_Switches.Table (Last_Switches.Last) :=
1193 Get_Name_String (Project.Exec_Directory.Name);
1194 Last_Switches.Increment_Last;
1195 Last_Switches.Table (Last_Switches.Last) :=
1196 new String'(Name_Buffer (1 .. Name_Len) &
1198 (Base_Name (Arg (Arg'First .. Last))));
1206 ---------------------
1207 -- Set_Library_For --
1208 ---------------------
1210 procedure Set_Library_For
1211 (Project : Project_Id;
1212 Libraries_Present : in out Boolean)
1214 Path_Option : constant String_Access :=
1215 MLib.Linker_Library_Path_Option;
1218 -- Case of library project
1220 if Project.Library then
1221 Libraries_Present := True;
1223 -- Add the -L switch
1225 Last_Switches.Increment_Last;
1226 Last_Switches.Table (Last_Switches.Last) :=
1227 new String'("-L" & Get_Name_String (Project.Library_Dir.Name));
1229 -- Add the -l switch
1231 Last_Switches.Increment_Last;
1232 Last_Switches.Table (Last_Switches.Last) :=
1233 new String'("-l" & Get_Name_String (Project.Library_Name));
1235 -- Add the directory to table Library_Paths, to be processed later
1236 -- if library is not static and if Path_Option is not null.
1238 if Project.Library_Kind /= Static
1239 and then Path_Option /= null
1241 Library_Paths.Increment_Last;
1242 Library_Paths.Table (Library_Paths.Last) :=
1243 new String'(Get_Name_String (Project.Library_Dir.Name));
1246 end Set_Library_For;
1248 ---------------------------
1249 -- Test_If_Relative_Path --
1250 ---------------------------
1252 procedure Test_If_Relative_Path
1253 (Switch : in out String_Access;
1257 Makeutl.Test_If_Relative_Path
1258 (Switch, Parent, Including_Non_Switch => False, Including_RTS => True);
1259 end Test_If_Relative_Path;
1265 procedure Non_VMS_Usage is
1269 Put_Line ("List of available commands");
1272 for C in Command_List'Range loop
1274 -- No usage for VMS only command or for Sync
1276 if not Command_List (C).VMS_Only and then C /= Sync 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 ("All commands except chop, krunch and preprocess " &
1311 "accept project file switches -vPx, -Pprj and -Xnam=val");
1315 -------------------------------------
1316 -- Start of processing for GNATCmd --
1317 -------------------------------------
1325 Project_Node_Tree := new Project_Node_Tree_Data;
1326 Prj.Tree.Initialize (Project_Node_Tree);
1328 Prj.Initialize (Project_Tree);
1331 Last_Switches.Set_Last (0);
1333 First_Switches.Init;
1334 First_Switches.Set_Last (0);
1336 Carg_Switches.Set_Last (0);
1337 Rules_Switches.Init;
1338 Rules_Switches.Set_Last (0);
1340 VMS_Conv.Initialize;
1342 -- Add the default search directories, to be able to find system.ads in the
1343 -- subsequent call to Targparm.Get_Target_Parameters.
1345 Add_Default_Search_Dirs;
1347 -- Get target parameters so that AAMP_On_Target will be set, for testing in
1348 -- Osint.Program_Name to handle the mapping of GNAAMP tool names.
1350 Targparm.Get_Target_Parameters;
1352 -- Put the command line in environment variable GNAT_DRIVER_COMMAND_LINE,
1353 -- so that the spawned tool may know the way the GNAT driver was invoked.
1356 Add_Str_To_Name_Buffer (Command_Name);
1358 for J in 1 .. Argument_Count loop
1359 Add_Char_To_Name_Buffer (' ');
1360 Add_Str_To_Name_Buffer (Argument (J));
1363 Setenv ("GNAT_DRIVER_COMMAND_LINE", Name_Buffer (1 .. Name_Len));
1365 -- Add the directory where the GNAT driver is invoked in front of the path,
1366 -- if the GNAT driver is invoked with directory information. Do not do this
1367 -- for VMS, where the notion of path does not really exist.
1371 Command : constant String := Command_Name;
1374 for Index in reverse Command'Range loop
1375 if Command (Index) = Directory_Separator then
1377 Absolute_Dir : constant String :=
1379 (Command (Command'First .. Index));
1381 PATH : constant String :=
1382 Absolute_Dir & Path_Separator & Getenv ("PATH").all;
1385 Setenv ("PATH", PATH);
1394 -- If on VMS, or if VMS emulation is on, convert VMS style /qualifiers,
1395 -- filenames and pathnames to Unix style.
1398 or else To_Lower (Getenv ("EMULATE_VMS").all) = "true"
1400 VMS_Conversion (The_Command);
1402 B_Start := new String'("b__");
1404 -- If not on VMS, scan the command line directly
1407 if Argument_Count = 0 then
1413 if Argument_Count > Command_Arg
1414 and then Argument (Command_Arg) = "-v"
1416 Verbose_Mode := True;
1417 Command_Arg := Command_Arg + 1;
1419 elsif Argument_Count > Command_Arg
1420 and then Argument (Command_Arg) = "-dn"
1422 Keep_Temporary_Files := True;
1423 Command_Arg := Command_Arg + 1;
1430 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
1432 if Command_List (The_Command).VMS_Only then
1436 & Command_List (The_Command).Cname.all
1437 & """ can only be used on VMS");
1441 when Constraint_Error =>
1443 -- Check if it is an alternate command
1446 Alternate : Alternate_Command;
1449 Alternate := Alternate_Command'Value
1450 (Argument (Command_Arg));
1451 The_Command := Corresponding_To (Alternate);
1454 when Constraint_Error =>
1456 Fail ("Unknown command: " & Argument (Command_Arg));
1460 -- Get the arguments from the command line and from the eventual
1461 -- argument file(s) specified on the command line.
1463 for Arg in Command_Arg + 1 .. Argument_Count loop
1465 The_Arg : constant String := Argument (Arg);
1468 -- Check if an argument file is specified
1470 if The_Arg (The_Arg'First) = '@' then
1472 Arg_File : Ada.Text_IO.File_Type;
1473 Line : String (1 .. 256);
1477 -- Open the file and fail if the file cannot be found
1482 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1487 (Standard_Error, "Cannot open argument file """);
1490 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
1492 Put_Line (Standard_Error, """");
1496 -- Read line by line and put the content of each non-
1497 -- empty line in the Last_Switches table.
1499 while not End_Of_File (Arg_File) loop
1500 Get_Line (Arg_File, Line, Last);
1503 Last_Switches.Increment_Last;
1504 Last_Switches.Table (Last_Switches.Last) :=
1505 new String'(Line (1 .. Last));
1513 -- It is not an argument file; just put the argument in
1514 -- the Last_Switches table.
1516 Last_Switches.Increment_Last;
1517 Last_Switches.Table (Last_Switches.Last) :=
1518 new String'(The_Arg);
1526 Program : String_Access;
1527 Exec_Path : String_Access;
1530 if The_Command = Stack then
1532 -- Never call gnatstack with a prefix
1534 Program := new String'(Command_List (The_Command).Unixcmd.all);
1538 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
1541 -- Locate the executable for the command
1543 Exec_Path := Locate_Exec_On_Path (Program.all);
1545 if Exec_Path = null then
1546 Put_Line (Standard_Error, "could not locate " & Program.all);
1550 -- If there are switches for the executable, put them as first switches
1552 if Command_List (The_Command).Unixsws /= null then
1553 for J in Command_List (The_Command).Unixsws'Range loop
1554 First_Switches.Increment_Last;
1555 First_Switches.Table (First_Switches.Last) :=
1556 Command_List (The_Command).Unixsws (J);
1560 -- For BIND, CHECK, ELIM, FIND, LINK, LIST, METRIC, PRETTY, STACK, STUB,
1561 -- SYNC and XREF, look for project file related switches.
1565 Tool_Package_Name := Name_Binder;
1566 Packages_To_Check := Packages_To_Check_By_Binder;
1568 Tool_Package_Name := Name_Check;
1569 Packages_To_Check := Packages_To_Check_By_Check;
1571 Tool_Package_Name := Name_Eliminate;
1572 Packages_To_Check := Packages_To_Check_By_Eliminate;
1574 Tool_Package_Name := Name_Finder;
1575 Packages_To_Check := Packages_To_Check_By_Finder;
1577 Tool_Package_Name := Name_Linker;
1578 Packages_To_Check := Packages_To_Check_By_Linker;
1580 Tool_Package_Name := Name_Gnatls;
1581 Packages_To_Check := Packages_To_Check_By_Gnatls;
1583 Tool_Package_Name := Name_Metrics;
1584 Packages_To_Check := Packages_To_Check_By_Metric;
1586 Tool_Package_Name := Name_Pretty_Printer;
1587 Packages_To_Check := Packages_To_Check_By_Pretty;
1589 Tool_Package_Name := Name_Stack;
1590 Packages_To_Check := Packages_To_Check_By_Stack;
1592 Tool_Package_Name := Name_Gnatstub;
1593 Packages_To_Check := Packages_To_Check_By_Gnatstub;
1595 Tool_Package_Name := Name_Synchronize;
1596 Packages_To_Check := Packages_To_Check_By_Sync;
1598 Tool_Package_Name := Name_Cross_Reference;
1599 Packages_To_Check := Packages_To_Check_By_Xref;
1601 Tool_Package_Name := No_Name;
1604 if Tool_Package_Name /= No_Name then
1606 -- Check that the switches are consistent. Detect project file
1607 -- related switches.
1609 Inspect_Switches : declare
1610 Arg_Num : Positive := 1;
1611 Argv : String_Access;
1613 procedure Remove_Switch (Num : Positive);
1614 -- Remove a project related switch from table Last_Switches
1620 procedure Remove_Switch (Num : Positive) is
1622 Last_Switches.Table (Num .. Last_Switches.Last - 1) :=
1623 Last_Switches.Table (Num + 1 .. Last_Switches.Last);
1624 Last_Switches.Decrement_Last;
1627 -- Start of processing for Inspect_Switches
1630 while Arg_Num <= Last_Switches.Last loop
1631 Argv := Last_Switches.Table (Arg_Num);
1633 if Argv (Argv'First) = '-' then
1634 if Argv'Length = 1 then
1636 ("switch character cannot be followed by a blank");
1639 -- The two style project files (-p and -P) cannot be used
1642 if (The_Command = Find or else The_Command = Xref)
1643 and then Argv (2) = 'p'
1645 Old_Project_File_Used := True;
1646 if Project_File /= null then
1647 Fail ("-P and -p cannot be used together");
1651 -- --subdirs=... Specify Subdirs
1653 if Argv'Length > Makeutl.Subdirs_Option'Length
1657 Argv'First + Makeutl.Subdirs_Option'Length - 1) =
1658 Makeutl.Subdirs_Option
1663 (Argv'First + Makeutl.Subdirs_Option'Length ..
1666 Remove_Switch (Arg_Num);
1668 -- -aPdir Add dir to the project search path
1670 elsif Argv'Length > 3
1671 and then Argv (Argv'First + 1 .. Argv'First + 2) = "aP"
1673 Add_Search_Project_Directory
1674 (Project_Node_Tree, Argv (Argv'First + 3 .. Argv'Last));
1676 Remove_Switch (Arg_Num);
1678 -- -eL Follow links for files
1680 elsif Argv.all = "-eL" then
1681 Follow_Links_For_Files := True;
1682 Follow_Links_For_Dirs := True;
1684 Remove_Switch (Arg_Num);
1686 -- -vPx Specify verbosity while parsing project files
1688 elsif Argv'Length = 4
1689 and then Argv (Argv'First + 1 .. Argv'First + 2) = "vP"
1691 case Argv (Argv'Last) is
1693 Current_Verbosity := Prj.Default;
1695 Current_Verbosity := Prj.Medium;
1697 Current_Verbosity := Prj.High;
1699 Fail ("Invalid switch: " & Argv.all);
1702 Remove_Switch (Arg_Num);
1704 -- -Pproject_file Specify project file to be used
1706 elsif Argv (Argv'First + 1) = 'P' then
1708 -- Only one -P switch can be used
1710 if Project_File /= null then
1713 & ": second project file forbidden (first is """
1717 -- The two style project files (-p and -P) cannot be
1720 elsif Old_Project_File_Used then
1721 Fail ("-p and -P cannot be used together");
1723 elsif Argv'Length = 2 then
1725 -- There is space between -P and the project file
1726 -- name. -P cannot be the last option.
1728 if Arg_Num = Last_Switches.Last then
1729 Fail ("project file name missing after -P");
1732 Remove_Switch (Arg_Num);
1733 Argv := Last_Switches.Table (Arg_Num);
1735 -- After -P, there must be a project file name,
1736 -- not another switch.
1738 if Argv (Argv'First) = '-' then
1739 Fail ("project file name missing after -P");
1742 Project_File := new String'(Argv.all);
1747 -- No space between -P and project file name
1750 new String'(Argv (Argv'First + 2 .. Argv'Last));
1753 Remove_Switch (Arg_Num);
1755 -- -Xexternal=value Specify an external reference to be
1756 -- used in project files
1758 elsif Argv'Length >= 5
1759 and then Argv (Argv'First + 1) = 'X'
1762 Equal_Pos : constant Natural :=
1765 Argv (Argv'First + 2 .. Argv'Last));
1767 if Equal_Pos >= Argv'First + 3
1768 and then Equal_Pos /= Argv'Last
1770 Add (Project_Node_Tree,
1772 Argv (Argv'First + 2 .. Equal_Pos - 1),
1773 Value => Argv (Equal_Pos + 1 .. Argv'Last));
1777 & " is not a valid external assignment.");
1781 Remove_Switch (Arg_Num);
1784 (The_Command = Check or else
1785 The_Command = Sync or else
1786 The_Command = Pretty or else
1787 The_Command = Metric or else
1788 The_Command = Stack or else
1790 and then Argv'Length = 2
1791 and then Argv (2) = 'U'
1793 All_Projects := True;
1794 Remove_Switch (Arg_Num);
1797 Arg_Num := Arg_Num + 1;
1800 elsif ((The_Command = Check and then Argv (Argv'First) /= '+')
1801 or else The_Command = Sync
1802 or else The_Command = Metric
1803 or else The_Command = Pretty)
1804 and then Project_File /= null
1805 and then All_Projects
1807 if ASIS_Main /= null then
1808 Fail ("cannot specify more than one main after -U");
1811 Remove_Switch (Arg_Num);
1815 Arg_Num := Arg_Num + 1;
1818 end Inspect_Switches;
1821 -- If there is a project file specified, parse it, get the switches
1822 -- for the tool and setup PATH environment variables.
1824 if Project_File /= null then
1825 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1828 (Project => Project,
1829 In_Tree => Project_Tree,
1830 In_Node_Tree => Project_Node_Tree,
1831 Project_File_Name => Project_File.all,
1832 Flags => Gnatmake_Flags,
1833 Packages_To_Check => Packages_To_Check);
1835 if Project = Prj.No_Project then
1836 Fail ("""" & Project_File.all & """ processing failed");
1839 -- Check if a package with the name of the tool is in the project
1840 -- file and if there is one, get the switches, if any, and scan them.
1843 Pkg : constant Prj.Package_Id :=
1845 (Name => Tool_Package_Name,
1846 In_Packages => Project.Decl.Packages,
1847 In_Tree => Project_Tree);
1849 Element : Package_Element;
1851 Switches_Array : Array_Element_Id;
1853 The_Switches : Prj.Variable_Value;
1854 Current : Prj.String_List_Id;
1855 The_String : String_Element;
1857 Main : String_Access := null;
1860 if Pkg /= No_Package then
1861 Element := Project_Tree.Packages.Table (Pkg);
1863 -- Packages Gnatls and Gnatstack have a single attribute
1864 -- Switches, that is not an associative array.
1866 if The_Command = List or else The_Command = Stack then
1869 (Variable_Name => Snames.Name_Switches,
1870 In_Variables => Element.Decl.Attributes,
1871 In_Tree => Project_Tree);
1873 -- Packages Binder (for gnatbind), Cross_Reference (for
1874 -- gnatxref), Linker (for gnatlink), Finder (for gnatfind),
1875 -- Pretty_Printer (for gnatpp), Eliminate (for gnatelim), Check
1876 -- (for gnatcheck), and Metric (for gnatmetric) have an
1877 -- attributed Switches, an associative array, indexed by the
1878 -- name of the file.
1880 -- They also have an attribute Default_Switches, indexed by the
1881 -- name of the programming language.
1884 -- First check if there is a single main
1886 for J in 1 .. Last_Switches.Last loop
1887 if Last_Switches.Table (J) (1) /= '-' then
1889 Main := Last_Switches.Table (J);
1898 if Main /= null then
1901 (Name => Name_Switches,
1902 In_Arrays => Element.Decl.Arrays,
1903 In_Tree => Project_Tree);
1905 Add_Str_To_Name_Buffer (Main.all);
1906 The_Switches := Prj.Util.Value_Of
1907 (Index => Name_Find,
1909 In_Array => Switches_Array,
1910 In_Tree => Project_Tree);
1913 if The_Switches.Kind = Prj.Undefined then
1916 (Name => Name_Default_Switches,
1917 In_Arrays => Element.Decl.Arrays,
1918 In_Tree => Project_Tree);
1919 The_Switches := Prj.Util.Value_Of
1922 In_Array => Switches_Array,
1923 In_Tree => Project_Tree);
1927 -- If there are switches specified in the package of the
1928 -- project file corresponding to the tool, scan them.
1930 case The_Switches.Kind is
1931 when Prj.Undefined =>
1936 Switch : constant String :=
1937 Get_Name_String (The_Switches.Value);
1940 if Switch'Length > 0 then
1941 First_Switches.Increment_Last;
1942 First_Switches.Table (First_Switches.Last) :=
1943 new String'(Switch);
1948 Current := The_Switches.Values;
1949 while Current /= Prj.Nil_String loop
1950 The_String := Project_Tree.String_Elements.
1954 Switch : constant String :=
1955 Get_Name_String (The_String.Value);
1958 if Switch'Length > 0 then
1959 First_Switches.Increment_Last;
1960 First_Switches.Table (First_Switches.Last) :=
1961 new String'(Switch);
1965 Current := The_String.Next;
1971 if The_Command = Bind
1972 or else The_Command = Link
1973 or else The_Command = Elim
1975 Change_Dir (Get_Name_String (Project.Object_Directory.Name));
1978 -- Set up the env vars for project path files
1980 Prj.Env.Set_Ada_Paths
1981 (Project, Project_Tree, Including_Libraries => False);
1983 -- For gnatcheck, gnatstub, gnatmetric, gnatpp and gnatelim, create
1984 -- a configuration pragmas file, if necessary.
1986 if The_Command = Pretty
1987 or else The_Command = Metric
1988 or else The_Command = Stub
1989 or else The_Command = Elim
1990 or else The_Command = Check
1991 or else The_Command = Sync
1993 -- If there are switches in package Compiler, put them in the
1994 -- Carg_Switches table.
1997 Pkg : constant Prj.Package_Id :=
1999 (Name => Name_Compiler,
2000 In_Packages => Project.Decl.Packages,
2001 In_Tree => Project_Tree);
2003 Element : Package_Element;
2005 Switches_Array : Array_Element_Id;
2007 The_Switches : Prj.Variable_Value;
2008 Current : Prj.String_List_Id;
2009 The_String : String_Element;
2011 Main : String_Access := null;
2015 if Pkg /= No_Package then
2017 -- First, check if there is a single main specified.
2019 for J in 1 .. Last_Switches.Last loop
2020 if Last_Switches.Table (J) (1) /= '-' then
2022 Main := Last_Switches.Table (J);
2031 Element := Project_Tree.Packages.Table (Pkg);
2033 -- If there is a single main and there is compilation
2034 -- switches specified in the project file, use them.
2036 if Main /= null and then not All_Projects then
2037 Name_Len := Main'Length;
2038 Name_Buffer (1 .. Name_Len) := Main.all;
2039 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2040 Main_Id := Name_Find;
2044 (Name => Name_Switches,
2045 In_Arrays => Element.Decl.Arrays,
2046 In_Tree => Project_Tree);
2047 The_Switches := Prj.Util.Value_Of
2050 In_Array => Switches_Array,
2051 In_Tree => Project_Tree);
2054 -- Otherwise, get the Default_Switches ("Ada")
2056 if The_Switches.Kind = Undefined then
2059 (Name => Name_Default_Switches,
2060 In_Arrays => Element.Decl.Arrays,
2061 In_Tree => Project_Tree);
2062 The_Switches := Prj.Util.Value_Of
2065 In_Array => Switches_Array,
2066 In_Tree => Project_Tree);
2069 -- If there are switches specified, put them in the
2070 -- Carg_Switches table.
2072 case The_Switches.Kind is
2073 when Prj.Undefined =>
2078 Switch : constant String :=
2079 Get_Name_String (The_Switches.Value);
2081 if Switch'Length > 0 then
2082 Add_To_Carg_Switches (new String'(Switch));
2087 Current := The_Switches.Values;
2088 while Current /= Prj.Nil_String loop
2090 Project_Tree.String_Elements.Table (Current);
2093 Switch : constant String :=
2094 Get_Name_String (The_String.Value);
2096 if Switch'Length > 0 then
2097 Add_To_Carg_Switches (new String'(Switch));
2101 Current := The_String.Next;
2107 -- If -cargs is one of the switches, move the following switches
2108 -- to the Carg_Switches table.
2110 for J in 1 .. First_Switches.Last loop
2111 if First_Switches.Table (J).all = "-cargs" then
2117 -- Move the switches that are before -rules when the
2118 -- command is CHECK.
2121 while K <= First_Switches.Last
2123 (The_Command /= Check
2124 or else First_Switches.Table (K).all /= "-rules")
2126 Add_To_Carg_Switches (First_Switches.Table (K));
2130 if K > First_Switches.Last then
2131 First_Switches.Set_Last (J - 1);
2135 while K <= First_Switches.Last loop
2137 First_Switches.Table (Last) :=
2138 First_Switches.Table (K);
2142 First_Switches.Set_Last (Last);
2150 for J in 1 .. Last_Switches.Last loop
2151 if Last_Switches.Table (J).all = "-cargs" then
2157 -- Move the switches that are before -rules when the
2158 -- command is CHECK.
2161 while K <= Last_Switches.Last
2163 (The_Command /= Check
2164 or else Last_Switches.Table (K).all /= "-rules")
2166 Add_To_Carg_Switches (Last_Switches.Table (K));
2170 if K > Last_Switches.Last then
2171 Last_Switches.Set_Last (J - 1);
2175 while K <= Last_Switches.Last loop
2177 Last_Switches.Table (Last) :=
2178 Last_Switches.Table (K);
2182 Last_Switches.Set_Last (Last);
2191 CP_File : constant Path_Name_Type := Configuration_Pragmas_File;
2192 M_File : constant Path_Name_Type := Mapping_File;
2195 if CP_File /= No_Path then
2196 if The_Command = Elim then
2197 First_Switches.Increment_Last;
2198 First_Switches.Table (First_Switches.Last) :=
2199 new String'("-C" & Get_Name_String (CP_File));
2202 Add_To_Carg_Switches
2203 (new String'("-gnatec=" & Get_Name_String (CP_File)));
2207 if M_File /= No_Path then
2208 Add_To_Carg_Switches
2209 (new String'("-gnatem=" & Get_Name_String (M_File)));
2212 -- For gnatcheck, also indicate a global configuration pragmas
2213 -- file and, if -U is not used, a local one.
2215 if The_Command = Check then
2217 Pkg : constant Prj.Package_Id :=
2219 (Name => Name_Builder,
2220 In_Packages => Project.Decl.Packages,
2221 In_Tree => Project_Tree);
2223 Variable : Variable_Value :=
2226 Attribute_Or_Array_Name =>
2227 Name_Global_Configuration_Pragmas,
2229 In_Tree => Project_Tree);
2232 if (Variable = Nil_Variable_Value
2233 or else Length_Of_Name (Variable.Value) = 0)
2234 and then Pkg /= No_Package
2239 Attribute_Or_Array_Name =>
2240 Name_Global_Config_File,
2242 In_Tree => Project_Tree);
2245 if Variable /= Nil_Variable_Value
2246 and then Length_Of_Name (Variable.Value) /= 0
2248 Add_To_Carg_Switches
2250 ("-gnatec=" & Get_Name_String (Variable.Value)));
2254 if not All_Projects then
2256 Pkg : constant Prj.Package_Id :=
2258 (Name => Name_Compiler,
2259 In_Packages => Project.Decl.Packages,
2260 In_Tree => Project_Tree);
2262 Variable : Variable_Value :=
2265 Attribute_Or_Array_Name =>
2266 Name_Local_Configuration_Pragmas,
2268 In_Tree => Project_Tree);
2271 if (Variable = Nil_Variable_Value
2272 or else Length_Of_Name (Variable.Value) = 0)
2273 and then Pkg /= No_Package
2278 Attribute_Or_Array_Name =>
2279 Name_Local_Config_File,
2281 In_Tree => Project_Tree);
2284 if Variable /= Nil_Variable_Value
2285 and then Length_Of_Name (Variable.Value) /= 0
2287 Add_To_Carg_Switches
2290 Get_Name_String (Variable.Value)));
2298 if The_Command = Link then
2302 if The_Command = Link or else The_Command = Bind then
2304 -- For files that are specified as relative paths with directory
2305 -- information, we convert them to absolute paths, with parent
2306 -- being the current working directory if specified on the command
2307 -- line and the project directory if specified in the project
2308 -- file. This is what gnatmake is doing for linker and binder
2311 for J in 1 .. Last_Switches.Last loop
2312 GNATCmd.Test_If_Relative_Path
2313 (Last_Switches.Table (J), Current_Work_Dir);
2316 Get_Name_String (Project.Directory.Name);
2319 Project_Dir : constant String := Name_Buffer (1 .. Name_Len);
2321 for J in 1 .. First_Switches.Last loop
2322 GNATCmd.Test_If_Relative_Path
2323 (First_Switches.Table (J), Project_Dir);
2327 elsif The_Command = Stub then
2329 File_Index : Integer := 0;
2330 Dir_Index : Integer := 0;
2331 Last : constant Integer := Last_Switches.Last;
2332 Lang : constant Language_Ptr :=
2333 Get_Language_From_Name (Project, "ada");
2336 for Index in 1 .. Last loop
2337 if Last_Switches.Table (Index)
2338 (Last_Switches.Table (Index)'First) /= '-'
2340 File_Index := Index;
2345 -- If the project file naming scheme is not standard, and if
2346 -- the file name ends with the spec suffix, then indicate to
2347 -- gnatstub the name of the body file with a -o switch.
2349 if not Is_Standard_GNAT_Naming (Lang.Config.Naming_Data) then
2350 if File_Index /= 0 then
2352 Spec : constant String :=
2354 (Last_Switches.Table (File_Index).all);
2355 Last : Natural := Spec'Last;
2358 Get_Name_String (Lang.Config.Naming_Data.Spec_Suffix);
2360 if Spec'Length > Name_Len
2361 and then Spec (Last - Name_Len + 1 .. Last) =
2362 Name_Buffer (1 .. Name_Len)
2364 Last := Last - Name_Len;
2366 (Lang.Config.Naming_Data.Body_Suffix);
2367 Last_Switches.Increment_Last;
2368 Last_Switches.Table (Last_Switches.Last) :=
2370 Last_Switches.Increment_Last;
2371 Last_Switches.Table (Last_Switches.Last) :=
2372 new String'(Spec (Spec'First .. Last) &
2373 Name_Buffer (1 .. Name_Len));
2379 -- Add the directory of the spec as the destination directory
2380 -- of the body, if there is no destination directory already
2383 if File_Index /= 0 then
2384 for Index in File_Index + 1 .. Last loop
2385 if Last_Switches.Table (Index)
2386 (Last_Switches.Table (Index)'First) /= '-'
2393 if Dir_Index = 0 then
2394 Last_Switches.Increment_Last;
2395 Last_Switches.Table (Last_Switches.Last) :=
2397 (Dir_Name (Last_Switches.Table (File_Index).all));
2403 -- For gnatmetric, the generated files should be put in the object
2404 -- directory. This must be the first switch, because it may be
2405 -- overridden by a switch in package Metrics in the project file or
2406 -- by a command line option. Note that we don't add the -d= switch
2407 -- if there is no object directory available.
2409 if The_Command = Metric
2410 and then Project.Object_Directory /= No_Path_Information
2412 First_Switches.Increment_Last;
2413 First_Switches.Table (2 .. First_Switches.Last) :=
2414 First_Switches.Table (1 .. First_Switches.Last - 1);
2415 First_Switches.Table (1) :=
2417 Get_Name_String (Project.Object_Directory.Name));
2420 -- For gnat check, -rules and the following switches need to be the
2421 -- last options, so move all these switches to table Rules_Switches.
2423 if The_Command = Check then
2426 -- Set to rank of options preceding "-rules"
2428 In_Rules_Switches : Boolean;
2429 -- Set to True when options "-rules" is found
2432 New_Last := First_Switches.Last;
2433 In_Rules_Switches := False;
2435 for J in 1 .. First_Switches.Last loop
2436 if In_Rules_Switches then
2437 Add_To_Rules_Switches (First_Switches.Table (J));
2439 elsif First_Switches.Table (J).all = "-rules" then
2441 In_Rules_Switches := True;
2445 if In_Rules_Switches then
2446 First_Switches.Set_Last (New_Last);
2449 New_Last := Last_Switches.Last;
2450 In_Rules_Switches := False;
2452 for J in 1 .. Last_Switches.Last loop
2453 if In_Rules_Switches then
2454 Add_To_Rules_Switches (Last_Switches.Table (J));
2456 elsif Last_Switches.Table (J).all = "-rules" then
2458 In_Rules_Switches := True;
2462 if In_Rules_Switches then
2463 Last_Switches.Set_Last (New_Last);
2468 -- For gnat check, sync, metric or pretty with -U + a main, get the
2469 -- list of sources from the closure and add them to the arguments.
2471 if ASIS_Main /= null then
2474 -- On VMS, set up the env var again for source dirs file. This is
2475 -- because the call to gnatmake has set this env var to another
2476 -- file that has now been deleted.
2478 if Hostparm.OpenVMS then
2480 -- First make sure that the recorded file names are empty
2482 Prj.Env.Initialize (Project_Tree);
2484 Prj.Env.Set_Ada_Paths
2485 (Project, Project_Tree, Including_Libraries => False);
2488 -- For gnat check, gnat sync, gnat pretty, gnat metric, gnat list,
2489 -- and gnat stack, if no file has been put on the command line, call
2490 -- tool with all the sources of the main project.
2492 elsif The_Command = Check or else
2493 The_Command = Sync or else
2494 The_Command = Pretty or else
2495 The_Command = Metric or else
2496 The_Command = List or else
2503 -- Gather all the arguments and invoke the executable
2506 The_Args : Argument_List
2507 (1 .. First_Switches.Last +
2508 Last_Switches.Last +
2509 Carg_Switches.Last +
2510 Rules_Switches.Last);
2511 Arg_Num : Natural := 0;
2514 for J in 1 .. First_Switches.Last loop
2515 Arg_Num := Arg_Num + 1;
2516 The_Args (Arg_Num) := First_Switches.Table (J);
2519 for J in 1 .. Last_Switches.Last loop
2520 Arg_Num := Arg_Num + 1;
2521 The_Args (Arg_Num) := Last_Switches.Table (J);
2524 for J in 1 .. Carg_Switches.Last loop
2525 Arg_Num := Arg_Num + 1;
2526 The_Args (Arg_Num) := Carg_Switches.Table (J);
2529 for J in 1 .. Rules_Switches.Last loop
2530 Arg_Num := Arg_Num + 1;
2531 The_Args (Arg_Num) := Rules_Switches.Table (J);
2534 -- If Display_Command is on, only display the generated command
2536 if Display_Command then
2537 Put (Standard_Error, "generated command -->");
2538 Put (Standard_Error, Exec_Path.all);
2540 for Arg in The_Args'Range loop
2541 Put (Standard_Error, " ");
2542 Put (Standard_Error, The_Args (Arg).all);
2545 Put (Standard_Error, "<--");
2546 New_Line (Standard_Error);
2550 if Verbose_Mode then
2551 Output.Write_Str (Exec_Path.all);
2553 for Arg in The_Args'Range loop
2554 Output.Write_Char (' ');
2555 Output.Write_Str (The_Args (Arg).all);
2562 Exit_Status (Spawn (Exec_Path.all, The_Args));
2569 if not Keep_Temporary_Files then
2570 Prj.Delete_All_Temp_Files (Project_Tree);
2571 Delete_Temp_Config_Files;
2574 Set_Exit_Status (Failure);
2577 if not Keep_Temporary_Files then
2578 Prj.Delete_All_Temp_Files (Project_Tree);
2579 Delete_Temp_Config_Files;
2582 -- Since GNATCmd is normally called from DCL (the VMS shell), it must
2583 -- return an understandable VMS exit status. However the exit status
2584 -- returned *to* GNATCmd is a Posix style code, so we test it and return
2585 -- just a simple success or failure on VMS.
2587 if Hostparm.OpenVMS and then My_Exit_Status /= Success then
2588 Set_Exit_Status (Failure);
2590 Set_Exit_Status (My_Exit_Status);