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.Case_Util; use GNAT.Case_Util;
42 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
45 package body Makeutl is
47 type Mark_Key is record
48 File : File_Name_Type;
51 -- Identify either a mono-unit source (when Index = 0) or a specific unit
52 -- (index = 1's origin index of unit) in a multi-unit source.
54 -- There follow many global undocumented declarations, comments needed ???
56 Max_Mask_Num : constant := 2048;
58 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
60 function Hash (Key : Mark_Key) return Mark_Num;
62 package Marks is new GNAT.HTable.Simple_HTable
63 (Header_Num => Mark_Num,
69 -- A hash table to keep tracks of the marked units
71 type Linker_Options_Data is record
73 Options : String_List_Id;
76 Linker_Option_Initial_Count : constant := 20;
78 Linker_Options_Buffer : String_List_Access :=
79 new String_List (1 .. Linker_Option_Initial_Count);
81 Last_Linker_Option : Natural := 0;
83 package Linker_Opts is new Table.Table (
84 Table_Component_Type => Linker_Options_Data,
85 Table_Index_Type => Integer,
88 Table_Increment => 100,
89 Table_Name => "Make.Linker_Opts");
91 procedure Add_Linker_Option (Option : String);
98 (Option : String_Access;
99 To : in out String_List_Access;
100 Last : in out Natural)
103 if Last = To'Last then
105 New_Options : constant String_List_Access :=
106 new String_List (1 .. To'Last * 2);
109 New_Options (To'Range) := To.all;
111 -- Set all elements of the original options to null to avoid
112 -- deallocation of copies.
114 To.all := (others => null);
127 To : in out String_List_Access;
128 Last : in out Natural)
131 Add (Option => new String'(Option), To => To, Last => Last);
134 -----------------------
135 -- Add_Linker_Option --
136 -----------------------
138 procedure Add_Linker_Option (Option : String) is
140 if Option'Length > 0 then
141 if Last_Linker_Option = Linker_Options_Buffer'Last then
143 New_Buffer : constant String_List_Access :=
145 (1 .. Linker_Options_Buffer'Last +
146 Linker_Option_Initial_Count);
148 New_Buffer (Linker_Options_Buffer'Range) :=
149 Linker_Options_Buffer.all;
150 Linker_Options_Buffer.all := (others => null);
151 Free (Linker_Options_Buffer);
152 Linker_Options_Buffer := New_Buffer;
156 Last_Linker_Option := Last_Linker_Option + 1;
157 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
159 end Add_Linker_Option;
161 -------------------------
162 -- Base_Name_Index_For --
163 -------------------------
165 function Base_Name_Index_For
168 Index_Separator : Character) return File_Name_Type
170 Result : File_Name_Type;
174 Add_Str_To_Name_Buffer (Base_Name (Main));
176 -- Remove the extension, if any, that is the last part of the base name
177 -- starting with a dot and following some characters.
179 for J in reverse 2 .. Name_Len loop
180 if Name_Buffer (J) = '.' then
186 -- Add the index info, if index is different from 0
188 if Main_Index > 0 then
189 Add_Char_To_Name_Buffer (Index_Separator);
192 Img : constant String := Main_Index'Img;
194 Add_Str_To_Name_Buffer (Img (2 .. Img'Last));
200 end Base_Name_Index_For;
202 ------------------------------
203 -- Check_Source_Info_In_ALI --
204 ------------------------------
206 function Check_Source_Info_In_ALI (The_ALI : ALI_Id) return Boolean is
210 -- Loop through units
212 for U in ALIs.Table (The_ALI).First_Unit ..
213 ALIs.Table (The_ALI).Last_Unit
215 -- Check if the file name is one of the source of the unit
217 Get_Name_String (Units.Table (U).Uname);
218 Name_Len := Name_Len - 2;
219 Unit_Name := Name_Find;
221 if File_Not_A_Source_Of (Unit_Name, Units.Table (U).Sfile) then
225 -- Loop to do same check for each of the withed units
227 for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
229 WR : ALI.With_Record renames Withs.Table (W);
232 if WR.Sfile /= No_File then
233 Get_Name_String (WR.Uname);
234 Name_Len := Name_Len - 2;
235 Unit_Name := Name_Find;
237 if File_Not_A_Source_Of (Unit_Name, WR.Sfile) then
245 -- Loop to check subunits
247 for D in ALIs.Table (The_ALI).First_Sdep ..
248 ALIs.Table (The_ALI).Last_Sdep
251 SD : Sdep_Record renames Sdep.Table (D);
254 Unit_Name := SD.Subunit_Name;
256 if Unit_Name /= No_Name then
258 -- For separates, the file is no longer associated with the
259 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
260 -- so we need to check whether the source file still exists in
261 -- the source tree: it will if it matches the naming scheme
262 -- (and then will be for the same unit).
265 (In_Tree => Project_Tree,
266 Project => No_Project,
267 Base_Name => SD.Sfile) = No_Source
269 -- If this is not a runtime file or if, when gnatmake switch
270 -- -a is used, we are not able to find this subunit in the
271 -- source directories, then recompilation is needed.
273 if not Fname.Is_Internal_File_Name (SD.Sfile)
275 (Check_Readonly_Files
276 and then Full_Source_Name (SD.Sfile) = No_File)
280 ("While parsing ALI file, file "
281 & Get_Name_String (SD.Sfile)
282 & " is indicated as containing subunit "
283 & Get_Name_String (Unit_Name)
284 & " but this does not match what was found while"
285 & " parsing the project. Will recompile");
296 end Check_Source_Info_In_ALI;
298 --------------------------------
299 -- Create_Binder_Mapping_File --
300 --------------------------------
302 function Create_Binder_Mapping_File return Path_Name_Type is
303 Mapping_Path : Path_Name_Type := No_Path;
305 Mapping_FD : File_Descriptor := Invalid_FD;
306 -- A File Descriptor for an eventual mapping file
308 ALI_Unit : Unit_Name_Type := No_Unit_Name;
309 -- The unit name of an ALI file
311 ALI_Name : File_Name_Type := No_File;
312 -- The file name of the ALI file
314 ALI_Project : Project_Id := No_Project;
315 -- The project of the ALI file
318 OK : Boolean := False;
325 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
326 Record_Temp_File (Project_Tree, Mapping_Path);
328 if Mapping_FD /= Invalid_FD then
331 -- Traverse all units
333 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
334 while Unit /= No_Unit_Index loop
335 if Unit.Name /= No_Name then
337 -- If there is a body, put it in the mapping
339 if Unit.File_Names (Impl) /= No_Source
340 and then Unit.File_Names (Impl).Project /= No_Project
342 Get_Name_String (Unit.Name);
343 Add_Str_To_Name_Buffer ("%b");
344 ALI_Unit := Name_Find;
346 Lib_File_Name (Unit.File_Names (Impl).Display_File);
347 ALI_Project := Unit.File_Names (Impl).Project;
349 -- Otherwise, if there is a spec, put it in the mapping
351 elsif Unit.File_Names (Spec) /= No_Source
352 and then Unit.File_Names (Spec).Project /= No_Project
354 Get_Name_String (Unit.Name);
355 Add_Str_To_Name_Buffer ("%s");
356 ALI_Unit := Name_Find;
358 Lib_File_Name (Unit.File_Names (Spec).Display_File);
359 ALI_Project := Unit.File_Names (Spec).Project;
365 -- If we have something to put in the mapping then do it now.
366 -- However, if the project is extended, we don't put anything
367 -- in the mapping file, since we don't know where the ALI file
368 -- is: it might be in the extended project object directory as
369 -- well as in the extending project object directory.
371 if ALI_Name /= No_File
372 and then ALI_Project.Extended_By = No_Project
373 and then ALI_Project.Extends = No_Project
375 -- First check if the ALI file exists. If it does not, do
376 -- not put the unit in the mapping file.
379 ALI : constant String := Get_Name_String (ALI_Name);
382 -- For library projects, use the library ALI directory,
383 -- for other projects, use the object directory.
385 if ALI_Project.Library then
387 (ALI_Project.Library_ALI_Dir.Display_Name);
390 (ALI_Project.Object_Directory.Display_Name);
394 Is_Directory_Separator (Name_Buffer (Name_Len))
396 Add_Char_To_Name_Buffer (Directory_Separator);
399 Add_Str_To_Name_Buffer (ALI);
400 Add_Char_To_Name_Buffer (ASCII.LF);
403 ALI_Path_Name : constant String :=
404 Name_Buffer (1 .. Name_Len);
408 (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
410 -- First line is the unit name
412 Get_Name_String (ALI_Unit);
413 Add_Char_To_Name_Buffer (ASCII.LF);
417 Name_Buffer (1)'Address,
419 OK := Bytes = Name_Len;
423 -- Second line it the ALI file name
425 Get_Name_String (ALI_Name);
426 Add_Char_To_Name_Buffer (ASCII.LF);
430 Name_Buffer (1)'Address,
432 OK := (Bytes = Name_Len);
436 -- Third line it the ALI path name
441 ALI_Path_Name (1)'Address,
442 ALI_Path_Name'Length);
443 OK := (Bytes = ALI_Path_Name'Length);
445 -- If OK is False, it means we were unable to
446 -- write a line. No point in continuing with the
456 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
459 Close (Mapping_FD, Status);
464 -- If the creation of the mapping file was successful, we add the switch
465 -- to the arguments of gnatbind.
473 end Create_Binder_Mapping_File;
479 function Create_Name (Name : String) return File_Name_Type is
482 Add_Str_To_Name_Buffer (Name);
486 function Create_Name (Name : String) return Name_Id is
489 Add_Str_To_Name_Buffer (Name);
493 function Create_Name (Name : String) return Path_Name_Type is
496 Add_Str_To_Name_Buffer (Name);
500 ----------------------
501 -- Delete_All_Marks --
502 ----------------------
504 procedure Delete_All_Marks is
507 end Delete_All_Marks;
509 ----------------------------
510 -- Executable_Prefix_Path --
511 ----------------------------
513 function Executable_Prefix_Path return String is
514 Exec_Name : constant String := Command_Name;
516 function Get_Install_Dir (S : String) return String;
517 -- S is the executable name preceded by the absolute or relative path,
518 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
519 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
520 -- directory, return "".
522 ---------------------
523 -- Get_Install_Dir --
524 ---------------------
526 function Get_Install_Dir (S : String) return String is
528 Path_Last : Integer := 0;
531 for J in reverse Exec'Range loop
532 if Exec (J) = Directory_Separator then
538 if Path_Last >= Exec'First + 2 then
539 To_Lower (Exec (Path_Last - 2 .. Path_Last));
542 if Path_Last < Exec'First + 2
543 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
544 or else (Path_Last - 3 >= Exec'First
545 and then Exec (Path_Last - 3) /= Directory_Separator)
550 return Normalize_Pathname
551 (Exec (Exec'First .. Path_Last - 4),
552 Resolve_Links => Opt.Follow_Links_For_Dirs)
553 & Directory_Separator;
556 -- Beginning of Executable_Prefix_Path
559 -- For VMS, the path returned is always /gnu/
561 if Hostparm.OpenVMS then
565 -- First determine if a path prefix was placed in front of the
568 for J in reverse Exec_Name'Range loop
569 if Exec_Name (J) = Directory_Separator then
570 return Get_Install_Dir (Exec_Name);
574 -- If we get here, the user has typed the executable name with no
578 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
584 Dir : constant String := Get_Install_Dir (Path.all);
591 end Executable_Prefix_Path;
593 --------------------------
594 -- File_Not_A_Source_Of --
595 --------------------------
597 function File_Not_A_Source_Of
599 Sfile : File_Name_Type) return Boolean
601 Unit : constant Unit_Index :=
602 Units_Htable.Get (Project_Tree.Units_HT, Uname);
604 At_Least_One_File : Boolean := False;
607 if Unit /= No_Unit_Index then
608 for F in Unit.File_Names'Range loop
609 if Unit.File_Names (F) /= null then
610 At_Least_One_File := True;
611 if Unit.File_Names (F).File = Sfile then
617 if not At_Least_One_File then
619 -- The unit was probably created initially for a separate unit
620 -- (which are initially created as IMPL when both suffixes are the
621 -- same). Later on, Override_Kind changed the type of the file,
622 -- and the unit is no longer valid in fact.
627 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
632 end File_Not_A_Source_Of;
638 function Hash (Key : Mark_Key) return Mark_Num is
640 return Union_Id (Key.File) mod Max_Mask_Num;
647 procedure Inform (N : File_Name_Type; Msg : String) is
649 Inform (Name_Id (N), Msg);
652 procedure Inform (N : Name_Id := No_Name; Msg : String) is
654 Osint.Write_Program_Name;
662 Name : constant String := Get_Name_String (N);
664 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
665 Write_Str (File_Name (Name));
678 ----------------------------
679 -- Is_External_Assignment --
680 ----------------------------
682 function Is_External_Assignment
683 (Tree : Prj.Tree.Project_Node_Tree_Ref;
684 Argv : String) return Boolean
686 Start : Positive := 3;
687 Finish : Natural := Argv'Last;
689 pragma Assert (Argv'First = 1);
690 pragma Assert (Argv (1 .. 2) = "-X");
693 if Argv'Last < 5 then
696 elsif Argv (3) = '"' then
697 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
701 Finish := Argv'Last - 1;
707 Declaration => Argv (Start .. Finish));
708 end Is_External_Assignment;
715 (Source_File : File_Name_Type;
716 Index : Int := 0) return Boolean
719 return Marks.Get (K => (File => Source_File, Index => Index));
722 -----------------------------
723 -- Linker_Options_Switches --
724 -----------------------------
726 function Linker_Options_Switches
727 (Project : Project_Id;
728 In_Tree : Project_Tree_Ref) return String_List
730 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
731 -- The recursive routine used to add linker options
737 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
738 pragma Unreferenced (Dummy);
740 Linker_Package : Package_Id;
741 Options : Variable_Value;
746 (Name => Name_Linker,
747 In_Packages => Proj.Decl.Packages,
754 Attribute_Or_Array_Name => Name_Linker_Options,
755 In_Package => Linker_Package,
758 -- If attribute is present, add the project with
759 -- the attribute to table Linker_Opts.
761 if Options /= Nil_Variable_Value then
762 Linker_Opts.Increment_Last;
763 Linker_Opts.Table (Linker_Opts.Last) :=
764 (Project => Proj, Options => Options.Values);
768 procedure For_All_Projects is
769 new For_Every_Project_Imported (Boolean, Recursive_Add);
771 Dummy : Boolean := False;
773 -- Start of processing for Linker_Options_Switches
778 For_All_Projects (Project, Dummy, Imported_First => True);
780 Last_Linker_Option := 0;
782 for Index in reverse 1 .. Linker_Opts.Last loop
784 Options : String_List_Id;
785 Proj : constant Project_Id :=
786 Linker_Opts.Table (Index).Project;
788 Dir_Path : constant String :=
789 Get_Name_String (Proj.Directory.Name);
792 Options := Linker_Opts.Table (Index).Options;
793 while Options /= Nil_String loop
794 Option := In_Tree.String_Elements.Table (Options).Value;
795 Get_Name_String (Option);
797 -- Do not consider empty linker options
799 if Name_Len /= 0 then
800 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
802 -- Object files and -L switches specified with relative
803 -- paths must be converted to absolute paths.
805 Test_If_Relative_Path
806 (Switch => Linker_Options_Buffer (Last_Linker_Option),
808 Including_L_Switch => True);
811 Options := In_Tree.String_Elements.Table (Options).Next;
816 return Linker_Options_Buffer (1 .. Last_Linker_Option);
817 end Linker_Options_Switches;
823 package body Mains is
825 type File_And_Loc is record
826 File_Name : File_Name_Type;
828 Location : Source_Ptr := No_Location;
831 package Names is new Table.Table
832 (Table_Component_Type => File_And_Loc,
833 Table_Index_Type => Integer,
834 Table_Low_Bound => 1,
836 Table_Increment => 100,
837 Table_Name => "Makeutl.Mains.Names");
838 -- The table that stores the mains
840 Current : Natural := 0;
841 -- The index of the last main retrieved from the table
847 procedure Add_Main (Name : String) is
850 Add_Str_To_Name_Buffer (Name);
851 Names.Increment_Last;
852 Names.Table (Names.Last) := (Name_Find, 0, No_Location);
869 function Get_Index return Int is
871 if Current in Names.First .. Names.Last then
872 return Names.Table (Current).Index;
882 function Get_Location return Source_Ptr is
884 if Current in Names.First .. Names.Last then
885 return Names.Table (Current).Location;
895 function Next_Main return String is
897 if Current >= Names.Last then
900 Current := Current + 1;
901 return Get_Name_String (Names.Table (Current).File_Name);
905 ---------------------
906 -- Number_Of_Mains --
907 ---------------------
909 function Number_Of_Mains return Natural is
927 procedure Set_Index (Index : Int) is
929 if Names.Last > 0 then
930 Names.Table (Names.Last).Index := Index;
938 procedure Set_Location (Location : Source_Ptr) is
940 if Names.Last > 0 then
941 Names.Table (Names.Last).Location := Location;
949 procedure Update_Main (Name : String) is
951 if Current in Names.First .. Names.Last then
953 Add_Str_To_Name_Buffer (Name);
954 Names.Table (Current).File_Name := Name_Find;
963 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
965 Marks.Set (K => (File => Source_File, Index => Index), E => True);
968 -----------------------
969 -- Path_Or_File_Name --
970 -----------------------
972 function Path_Or_File_Name (Path : Path_Name_Type) return String is
973 Path_Name : constant String := Get_Name_String (Path);
975 if Debug.Debug_Flag_F then
976 return File_Name (Path_Name);
980 end Path_Or_File_Name;
982 ---------------------------
983 -- Test_If_Relative_Path --
984 ---------------------------
986 procedure Test_If_Relative_Path
987 (Switch : in out String_Access;
989 Including_L_Switch : Boolean := True;
990 Including_Non_Switch : Boolean := True;
991 Including_RTS : Boolean := False)
994 if Switch /= null then
996 Sw : String (1 .. Switch'Length);
1002 if Sw (1) = '-' then
1004 and then (Sw (2) = 'A'
1005 or else Sw (2) = 'I'
1006 or else (Including_L_Switch and then Sw (2) = 'L'))
1014 elsif Sw'Length >= 4
1015 and then (Sw (2 .. 3) = "aL"
1016 or else Sw (2 .. 3) = "aO"
1017 or else Sw (2 .. 3) = "aI")
1022 and then Sw'Length >= 7
1023 and then Sw (2 .. 6) = "-RTS="
1031 -- Because relative path arguments to --RTS= may be relative
1032 -- to the search directory prefix, those relative path
1033 -- arguments are converted only when they include directory
1036 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1037 if Parent'Length = 0 then
1039 ("relative search path switches ("""
1041 & """) are not allowed");
1043 elsif Including_RTS then
1044 for J in Start .. Sw'Last loop
1045 if Sw (J) = Directory_Separator then
1048 (Sw (1 .. Start - 1) &
1050 Directory_Separator &
1051 Sw (Start .. Sw'Last));
1059 (Sw (1 .. Start - 1) &
1061 Directory_Separator &
1062 Sw (Start .. Sw'Last));
1066 elsif Including_Non_Switch then
1067 if not Is_Absolute_Path (Sw) then
1068 if Parent'Length = 0 then
1070 ("relative paths (""" & Sw & """) are not allowed");
1072 Switch := new String'(Parent & Directory_Separator & Sw);
1078 end Test_If_Relative_Path;
1084 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
1090 Get_Name_String (ALI_File);
1092 -- First, find the last dot
1096 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
1097 Finish := Finish - 1;
1104 -- Now check that the dot is preceded by digits
1107 Finish := Finish - 1;
1109 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
1113 -- If there are no digits, or if the digits are not preceded by the
1114 -- character that precedes a unit index, this is not the ALI file of
1115 -- a unit in a multi-unit source.
1119 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
1124 -- Build the index from the digit(s)
1126 while Start <= Finish loop
1127 Result := Result * 10 +
1128 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
1139 procedure Verbose_Msg
1142 N2 : Name_Id := No_Name;
1144 Prefix : String := " -> ";
1145 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1148 if not Opt.Verbose_Mode
1149 or else Minimum_Verbosity > Opt.Verbosity_Level
1160 if N2 /= No_Name then
1170 procedure Verbose_Msg
1171 (N1 : File_Name_Type;
1173 N2 : File_Name_Type := No_File;
1175 Prefix : String := " -> ";
1176 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1180 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);