1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004-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 ------------------------------------------------------------------------------
30 with Osint; use Osint;
31 with Output; use Output;
35 with Snames; use Snames;
39 with Ada.Command_Line; use Ada.Command_Line;
41 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
43 with GNAT.Case_Util; use GNAT.Case_Util;
46 package body Makeutl is
48 type Mark_Key is record
49 File : File_Name_Type;
52 -- Identify either a mono-unit source (when Index = 0) or a specific unit
53 -- (index = 1's origin index of unit) in a multi-unit source.
55 -- There follow many global undocumented declarations, comments needed ???
57 Max_Mask_Num : constant := 2048;
59 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
61 function Hash (Key : Mark_Key) return Mark_Num;
63 package Marks is new GNAT.HTable.Simple_HTable
64 (Header_Num => Mark_Num,
70 -- A hash table to keep tracks of the marked units
72 type Linker_Options_Data is record
74 Options : String_List_Id;
77 Linker_Option_Initial_Count : constant := 20;
79 Linker_Options_Buffer : String_List_Access :=
80 new String_List (1 .. Linker_Option_Initial_Count);
82 Last_Linker_Option : Natural := 0;
84 package Linker_Opts is new Table.Table (
85 Table_Component_Type => Linker_Options_Data,
86 Table_Index_Type => Integer,
89 Table_Increment => 100,
90 Table_Name => "Make.Linker_Opts");
92 procedure Add_Linker_Option (Option : String);
99 (Option : String_Access;
100 To : in out String_List_Access;
101 Last : in out Natural)
104 if Last = To'Last then
106 New_Options : constant String_List_Access :=
107 new String_List (1 .. To'Last * 2);
110 New_Options (To'Range) := To.all;
112 -- Set all elements of the original options to null to avoid
113 -- deallocation of copies.
115 To.all := (others => null);
128 To : in out String_List_Access;
129 Last : in out Natural)
132 Add (Option => new String'(Option), To => To, Last => Last);
135 -----------------------
136 -- Add_Linker_Option --
137 -----------------------
139 procedure Add_Linker_Option (Option : String) is
141 if Option'Length > 0 then
142 if Last_Linker_Option = Linker_Options_Buffer'Last then
144 New_Buffer : constant String_List_Access :=
146 (1 .. Linker_Options_Buffer'Last +
147 Linker_Option_Initial_Count);
149 New_Buffer (Linker_Options_Buffer'Range) :=
150 Linker_Options_Buffer.all;
151 Linker_Options_Buffer.all := (others => null);
152 Free (Linker_Options_Buffer);
153 Linker_Options_Buffer := New_Buffer;
157 Last_Linker_Option := Last_Linker_Option + 1;
158 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
160 end Add_Linker_Option;
162 -------------------------
163 -- Base_Name_Index_For --
164 -------------------------
166 function Base_Name_Index_For
169 Index_Separator : Character) return File_Name_Type
171 Result : File_Name_Type;
175 Add_Str_To_Name_Buffer (Base_Name (Main));
177 -- Remove the extension, if any, that is the last part of the base name
178 -- starting with a dot and following some characters.
180 for J in reverse 2 .. Name_Len loop
181 if Name_Buffer (J) = '.' then
187 -- Add the index info, if index is different from 0
189 if Main_Index > 0 then
190 Add_Char_To_Name_Buffer (Index_Separator);
193 Img : constant String := Main_Index'Img;
195 Add_Str_To_Name_Buffer (Img (2 .. Img'Last));
201 end Base_Name_Index_For;
203 ------------------------------
204 -- Check_Source_Info_In_ALI --
205 ------------------------------
207 function Check_Source_Info_In_ALI (The_ALI : ALI_Id) return Boolean is
211 -- Loop through units
213 for U in ALIs.Table (The_ALI).First_Unit ..
214 ALIs.Table (The_ALI).Last_Unit
216 -- Check if the file name is one of the source of the unit
218 Get_Name_String (Units.Table (U).Uname);
219 Name_Len := Name_Len - 2;
220 Unit_Name := Name_Find;
222 if File_Not_A_Source_Of (Unit_Name, Units.Table (U).Sfile) then
226 -- Loop to do same check for each of the withed units
228 for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
230 WR : ALI.With_Record renames Withs.Table (W);
233 if WR.Sfile /= No_File then
234 Get_Name_String (WR.Uname);
235 Name_Len := Name_Len - 2;
236 Unit_Name := Name_Find;
238 if File_Not_A_Source_Of (Unit_Name, WR.Sfile) then
246 -- Loop to check subunits
248 for D in ALIs.Table (The_ALI).First_Sdep ..
249 ALIs.Table (The_ALI).Last_Sdep
252 SD : Sdep_Record renames Sdep.Table (D);
255 Unit_Name := SD.Subunit_Name;
257 if Unit_Name /= No_Name then
259 -- For separates, the file is no longer associated with the
260 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
261 -- so we need to check whether the source file still exists in
262 -- the source tree: it will if it matches the naming scheme
263 -- (and then will be for the same unit).
266 (In_Tree => Project_Tree,
267 Project => No_Project,
268 Base_Name => SD.Sfile) = No_Source
270 -- If this is not a runtime file or if, when gnatmake switch
271 -- -a is used, we are not able to find this subunit in the
272 -- source directories, then recompilation is needed.
274 if not Fname.Is_Internal_File_Name (SD.Sfile)
276 (Check_Readonly_Files
277 and then Full_Source_Name (SD.Sfile) = No_File)
281 ("While parsing ALI file, file "
282 & Get_Name_String (SD.Sfile)
283 & " is indicated as containing subunit "
284 & Get_Name_String (Unit_Name)
285 & " but this does not match what was found while"
286 & " parsing the project. Will recompile");
297 end Check_Source_Info_In_ALI;
299 --------------------------------
300 -- Create_Binder_Mapping_File --
301 --------------------------------
303 function Create_Binder_Mapping_File return Path_Name_Type is
304 Mapping_Path : Path_Name_Type := No_Path;
306 Mapping_FD : File_Descriptor := Invalid_FD;
307 -- A File Descriptor for an eventual mapping file
309 ALI_Unit : Unit_Name_Type := No_Unit_Name;
310 -- The unit name of an ALI file
312 ALI_Name : File_Name_Type := No_File;
313 -- The file name of the ALI file
315 ALI_Project : Project_Id := No_Project;
316 -- The project of the ALI file
319 OK : Boolean := False;
326 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
327 Record_Temp_File (Project_Tree, Mapping_Path);
329 if Mapping_FD /= Invalid_FD then
332 -- Traverse all units
334 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
335 while Unit /= No_Unit_Index loop
336 if Unit.Name /= No_Name then
338 -- If there is a body, put it in the mapping
340 if Unit.File_Names (Impl) /= No_Source
341 and then Unit.File_Names (Impl).Project /= No_Project
343 Get_Name_String (Unit.Name);
344 Add_Str_To_Name_Buffer ("%b");
345 ALI_Unit := Name_Find;
347 Lib_File_Name (Unit.File_Names (Impl).Display_File);
348 ALI_Project := Unit.File_Names (Impl).Project;
350 -- Otherwise, if there is a spec, put it in the mapping
352 elsif Unit.File_Names (Spec) /= No_Source
353 and then Unit.File_Names (Spec).Project /= No_Project
355 Get_Name_String (Unit.Name);
356 Add_Str_To_Name_Buffer ("%s");
357 ALI_Unit := Name_Find;
359 Lib_File_Name (Unit.File_Names (Spec).Display_File);
360 ALI_Project := Unit.File_Names (Spec).Project;
366 -- If we have something to put in the mapping then do it now.
367 -- However, if the project is extended, we don't put anything
368 -- in the mapping file, since we don't know where the ALI file
369 -- is: it might be in the extended project object directory as
370 -- well as in the extending project object directory.
372 if ALI_Name /= No_File
373 and then ALI_Project.Extended_By = No_Project
374 and then ALI_Project.Extends = No_Project
376 -- First check if the ALI file exists. If it does not, do
377 -- not put the unit in the mapping file.
380 ALI : constant String := Get_Name_String (ALI_Name);
383 -- For library projects, use the library ALI directory,
384 -- for other projects, use the object directory.
386 if ALI_Project.Library then
388 (ALI_Project.Library_ALI_Dir.Display_Name);
391 (ALI_Project.Object_Directory.Display_Name);
395 Is_Directory_Separator (Name_Buffer (Name_Len))
397 Add_Char_To_Name_Buffer (Directory_Separator);
400 Add_Str_To_Name_Buffer (ALI);
401 Add_Char_To_Name_Buffer (ASCII.LF);
404 ALI_Path_Name : constant String :=
405 Name_Buffer (1 .. Name_Len);
409 (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
411 -- First line is the unit name
413 Get_Name_String (ALI_Unit);
414 Add_Char_To_Name_Buffer (ASCII.LF);
418 Name_Buffer (1)'Address,
420 OK := Bytes = Name_Len;
424 -- Second line it the ALI file name
426 Get_Name_String (ALI_Name);
427 Add_Char_To_Name_Buffer (ASCII.LF);
431 Name_Buffer (1)'Address,
433 OK := (Bytes = Name_Len);
437 -- Third line it the ALI path name
442 ALI_Path_Name (1)'Address,
443 ALI_Path_Name'Length);
444 OK := (Bytes = ALI_Path_Name'Length);
446 -- If OK is False, it means we were unable to
447 -- write a line. No point in continuing with the
457 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
460 Close (Mapping_FD, Status);
465 -- If the creation of the mapping file was successful, we add the switch
466 -- to the arguments of gnatbind.
474 end Create_Binder_Mapping_File;
480 function Create_Name (Name : String) return File_Name_Type is
483 Add_Str_To_Name_Buffer (Name);
487 function Create_Name (Name : String) return Name_Id is
490 Add_Str_To_Name_Buffer (Name);
494 function Create_Name (Name : String) return Path_Name_Type is
497 Add_Str_To_Name_Buffer (Name);
501 ----------------------
502 -- Delete_All_Marks --
503 ----------------------
505 procedure Delete_All_Marks is
508 end Delete_All_Marks;
510 ----------------------------
511 -- Executable_Prefix_Path --
512 ----------------------------
514 function Executable_Prefix_Path return String is
515 Exec_Name : constant String := Command_Name;
517 function Get_Install_Dir (S : String) return String;
518 -- S is the executable name preceded by the absolute or relative path,
519 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
520 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
521 -- directory, return "".
523 ---------------------
524 -- Get_Install_Dir --
525 ---------------------
527 function Get_Install_Dir (S : String) return String is
529 Path_Last : Integer := 0;
532 for J in reverse Exec'Range loop
533 if Exec (J) = Directory_Separator then
539 if Path_Last >= Exec'First + 2 then
540 To_Lower (Exec (Path_Last - 2 .. Path_Last));
543 if Path_Last < Exec'First + 2
544 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
545 or else (Path_Last - 3 >= Exec'First
546 and then Exec (Path_Last - 3) /= Directory_Separator)
551 return Normalize_Pathname
552 (Exec (Exec'First .. Path_Last - 4),
553 Resolve_Links => Opt.Follow_Links_For_Dirs)
554 & Directory_Separator;
557 -- Beginning of Executable_Prefix_Path
560 -- For VMS, the path returned is always /gnu/
562 if Hostparm.OpenVMS then
566 -- First determine if a path prefix was placed in front of the
569 for J in reverse Exec_Name'Range loop
570 if Exec_Name (J) = Directory_Separator then
571 return Get_Install_Dir (Exec_Name);
575 -- If we get here, the user has typed the executable name with no
579 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
585 Dir : constant String := Get_Install_Dir (Path.all);
592 end Executable_Prefix_Path;
594 --------------------------
595 -- File_Not_A_Source_Of --
596 --------------------------
598 function File_Not_A_Source_Of
600 Sfile : File_Name_Type) return Boolean
602 Unit : constant Unit_Index :=
603 Units_Htable.Get (Project_Tree.Units_HT, Uname);
605 At_Least_One_File : Boolean := False;
608 if Unit /= No_Unit_Index then
609 for F in Unit.File_Names'Range loop
610 if Unit.File_Names (F) /= null then
611 At_Least_One_File := True;
612 if Unit.File_Names (F).File = Sfile then
618 if not At_Least_One_File then
620 -- The unit was probably created initially for a separate unit
621 -- (which are initially created as IMPL when both suffixes are the
622 -- same). Later on, Override_Kind changed the type of the file,
623 -- and the unit is no longer valid in fact.
628 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
633 end File_Not_A_Source_Of;
639 function Hash (Key : Mark_Key) return Mark_Num is
641 return Union_Id (Key.File) mod Max_Mask_Num;
648 procedure Inform (N : File_Name_Type; Msg : String) is
650 Inform (Name_Id (N), Msg);
653 procedure Inform (N : Name_Id := No_Name; Msg : String) is
655 Osint.Write_Program_Name;
663 Name : constant String := Get_Name_String (N);
665 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
666 Write_Str (File_Name (Name));
679 ----------------------------
680 -- Is_External_Assignment --
681 ----------------------------
683 function Is_External_Assignment
684 (Tree : Prj.Tree.Project_Node_Tree_Ref;
685 Argv : String) return Boolean
687 Start : Positive := 3;
688 Finish : Natural := Argv'Last;
690 pragma Assert (Argv'First = 1);
691 pragma Assert (Argv (1 .. 2) = "-X");
694 if Argv'Last < 5 then
697 elsif Argv (3) = '"' then
698 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
702 Finish := Argv'Last - 1;
708 Declaration => Argv (Start .. Finish));
709 end Is_External_Assignment;
716 (Source_File : File_Name_Type;
717 Index : Int := 0) return Boolean
720 return Marks.Get (K => (File => Source_File, Index => Index));
723 -----------------------------
724 -- Linker_Options_Switches --
725 -----------------------------
727 function Linker_Options_Switches
728 (Project : Project_Id;
729 In_Tree : Project_Tree_Ref) return String_List
731 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
732 -- The recursive routine used to add linker options
738 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
739 pragma Unreferenced (Dummy);
741 Linker_Package : Package_Id;
742 Options : Variable_Value;
747 (Name => Name_Linker,
748 In_Packages => Proj.Decl.Packages,
755 Attribute_Or_Array_Name => Name_Linker_Options,
756 In_Package => Linker_Package,
759 -- If attribute is present, add the project with
760 -- the attribute to table Linker_Opts.
762 if Options /= Nil_Variable_Value then
763 Linker_Opts.Increment_Last;
764 Linker_Opts.Table (Linker_Opts.Last) :=
765 (Project => Proj, Options => Options.Values);
769 procedure For_All_Projects is
770 new For_Every_Project_Imported (Boolean, Recursive_Add);
772 Dummy : Boolean := False;
774 -- Start of processing for Linker_Options_Switches
779 For_All_Projects (Project, Dummy, Imported_First => True);
781 Last_Linker_Option := 0;
783 for Index in reverse 1 .. Linker_Opts.Last loop
785 Options : String_List_Id;
786 Proj : constant Project_Id :=
787 Linker_Opts.Table (Index).Project;
789 Dir_Path : constant String :=
790 Get_Name_String (Proj.Directory.Name);
793 Options := Linker_Opts.Table (Index).Options;
794 while Options /= Nil_String loop
795 Option := In_Tree.String_Elements.Table (Options).Value;
796 Get_Name_String (Option);
798 -- Do not consider empty linker options
800 if Name_Len /= 0 then
801 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
803 -- Object files and -L switches specified with relative
804 -- paths must be converted to absolute paths.
806 Test_If_Relative_Path
807 (Switch => Linker_Options_Buffer (Last_Linker_Option),
809 Including_L_Switch => True);
812 Options := In_Tree.String_Elements.Table (Options).Next;
817 return Linker_Options_Buffer (1 .. Last_Linker_Option);
818 end Linker_Options_Switches;
824 package body Mains is
826 type File_And_Loc is record
827 File_Name : File_Name_Type;
829 Location : Source_Ptr := No_Location;
832 package Names is new Table.Table
833 (Table_Component_Type => File_And_Loc,
834 Table_Index_Type => Integer,
835 Table_Low_Bound => 1,
837 Table_Increment => 100,
838 Table_Name => "Makeutl.Mains.Names");
839 -- The table that stores the mains
841 Current : Natural := 0;
842 -- The index of the last main retrieved from the table
848 procedure Add_Main (Name : String) is
851 Add_Str_To_Name_Buffer (Name);
852 Names.Increment_Last;
853 Names.Table (Names.Last) := (Name_Find, 0, No_Location);
870 function Get_Index return Int is
872 if Current in Names.First .. Names.Last then
873 return Names.Table (Current).Index;
883 function Get_Location return Source_Ptr is
885 if Current in Names.First .. Names.Last then
886 return Names.Table (Current).Location;
896 function Next_Main return String is
898 if Current >= Names.Last then
901 Current := Current + 1;
902 return Get_Name_String (Names.Table (Current).File_Name);
906 ---------------------
907 -- Number_Of_Mains --
908 ---------------------
910 function Number_Of_Mains return Natural is
928 procedure Set_Index (Index : Int) is
930 if Names.Last > 0 then
931 Names.Table (Names.Last).Index := Index;
939 procedure Set_Location (Location : Source_Ptr) is
941 if Names.Last > 0 then
942 Names.Table (Names.Last).Location := Location;
950 procedure Update_Main (Name : String) is
952 if Current in Names.First .. Names.Last then
954 Add_Str_To_Name_Buffer (Name);
955 Names.Table (Current).File_Name := Name_Find;
964 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
966 Marks.Set (K => (File => Source_File, Index => Index), E => True);
969 -----------------------
970 -- Path_Or_File_Name --
971 -----------------------
973 function Path_Or_File_Name (Path : Path_Name_Type) return String is
974 Path_Name : constant String := Get_Name_String (Path);
976 if Debug.Debug_Flag_F then
977 return File_Name (Path_Name);
981 end Path_Or_File_Name;
983 ---------------------------
984 -- Test_If_Relative_Path --
985 ---------------------------
987 procedure Test_If_Relative_Path
988 (Switch : in out String_Access;
990 Including_L_Switch : Boolean := True;
991 Including_Non_Switch : Boolean := True;
992 Including_RTS : Boolean := False)
995 if Switch /= null then
997 Sw : String (1 .. Switch'Length);
1003 if Sw (1) = '-' then
1005 and then (Sw (2) = 'A'
1006 or else Sw (2) = 'I'
1007 or else (Including_L_Switch and then Sw (2) = 'L'))
1015 elsif Sw'Length >= 4
1016 and then (Sw (2 .. 3) = "aL"
1017 or else Sw (2 .. 3) = "aO"
1018 or else Sw (2 .. 3) = "aI")
1023 and then Sw'Length >= 7
1024 and then Sw (2 .. 6) = "-RTS="
1032 -- Because relative path arguments to --RTS= may be relative
1033 -- to the search directory prefix, those relative path
1034 -- arguments are converted only when they include directory
1037 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1038 if Parent'Length = 0 then
1040 ("relative search path switches ("""
1042 & """) are not allowed");
1044 elsif Including_RTS then
1045 for J in Start .. Sw'Last loop
1046 if Sw (J) = Directory_Separator then
1049 (Sw (1 .. Start - 1) &
1051 Directory_Separator &
1052 Sw (Start .. Sw'Last));
1060 (Sw (1 .. Start - 1) &
1062 Directory_Separator &
1063 Sw (Start .. Sw'Last));
1067 elsif Including_Non_Switch then
1068 if not Is_Absolute_Path (Sw) then
1069 if Parent'Length = 0 then
1071 ("relative paths (""" & Sw & """) are not allowed");
1073 Switch := new String'(Parent & Directory_Separator & Sw);
1079 end Test_If_Relative_Path;
1085 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
1091 Get_Name_String (ALI_File);
1093 -- First, find the last dot
1097 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
1098 Finish := Finish - 1;
1105 -- Now check that the dot is preceded by digits
1108 Finish := Finish - 1;
1110 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
1114 -- If there are no digits, or if the digits are not preceded by the
1115 -- character that precedes a unit index, this is not the ALI file of
1116 -- a unit in a multi-unit source.
1120 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
1125 -- Build the index from the digit(s)
1127 while Start <= Finish loop
1128 Result := Result * 10 +
1129 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
1140 procedure Verbose_Msg
1143 N2 : Name_Id := No_Name;
1145 Prefix : String := " -> ";
1146 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1149 if not Opt.Verbose_Mode
1150 or else Minimum_Verbosity > Opt.Verbosity_Level
1161 if N2 /= No_Name then
1171 procedure Verbose_Msg
1172 (N1 : File_Name_Type;
1174 N2 : File_Name_Type := No_File;
1176 Prefix : String := " -> ";
1177 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1181 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);