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
208 Tree : Project_Tree_Ref) return Boolean
213 -- Loop through units
215 for U in ALIs.Table (The_ALI).First_Unit ..
216 ALIs.Table (The_ALI).Last_Unit
218 -- Check if the file name is one of the source of the unit
220 Get_Name_String (Units.Table (U).Uname);
221 Name_Len := Name_Len - 2;
222 Unit_Name := Name_Find;
224 if File_Not_A_Source_Of (Unit_Name, Units.Table (U).Sfile) then
228 -- Loop to do same check for each of the withed units
230 for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
232 WR : ALI.With_Record renames Withs.Table (W);
235 if WR.Sfile /= No_File then
236 Get_Name_String (WR.Uname);
237 Name_Len := Name_Len - 2;
238 Unit_Name := Name_Find;
240 if File_Not_A_Source_Of (Unit_Name, WR.Sfile) then
248 -- Loop to check subunits and replaced sources
250 for D in ALIs.Table (The_ALI).First_Sdep ..
251 ALIs.Table (The_ALI).Last_Sdep
254 SD : Sdep_Record renames Sdep.Table (D);
257 Unit_Name := SD.Subunit_Name;
259 if Unit_Name = No_Name then
260 -- Check if this source file has been replaced by a source with
261 -- a different file name.
263 if Tree /= null and then Tree.Replaced_Source_Number > 0 then
265 Replacement : constant File_Name_Type :=
266 Replaced_Source_HTable.Get
267 (Tree.Replaced_Sources, SD.Sfile);
270 if Replacement /= No_File then
274 Get_Name_String (SD.Sfile) &
275 " has been replaced by " &
276 Get_Name_String (Replacement));
285 -- For separates, the file is no longer associated with the
286 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
287 -- so we need to check whether the source file still exists in
288 -- the source tree: it will if it matches the naming scheme
289 -- (and then will be for the same unit).
292 (In_Tree => Project_Tree,
293 Project => No_Project,
294 Base_Name => SD.Sfile) = No_Source
296 -- If this is not a runtime file or if, when gnatmake switch
297 -- -a is used, we are not able to find this subunit in the
298 -- source directories, then recompilation is needed.
300 if not Fname.Is_Internal_File_Name (SD.Sfile)
302 (Check_Readonly_Files
303 and then Full_Source_Name (SD.Sfile) = No_File)
307 ("While parsing ALI file, file "
308 & Get_Name_String (SD.Sfile)
309 & " is indicated as containing subunit "
310 & Get_Name_String (Unit_Name)
311 & " but this does not match what was found while"
312 & " parsing the project. Will recompile");
323 end Check_Source_Info_In_ALI;
325 --------------------------------
326 -- Create_Binder_Mapping_File --
327 --------------------------------
329 function Create_Binder_Mapping_File return Path_Name_Type is
330 Mapping_Path : Path_Name_Type := No_Path;
332 Mapping_FD : File_Descriptor := Invalid_FD;
333 -- A File Descriptor for an eventual mapping file
335 ALI_Unit : Unit_Name_Type := No_Unit_Name;
336 -- The unit name of an ALI file
338 ALI_Name : File_Name_Type := No_File;
339 -- The file name of the ALI file
341 ALI_Project : Project_Id := No_Project;
342 -- The project of the ALI file
345 OK : Boolean := False;
352 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
353 Record_Temp_File (Project_Tree, Mapping_Path);
355 if Mapping_FD /= Invalid_FD then
358 -- Traverse all units
360 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
361 while Unit /= No_Unit_Index loop
362 if Unit.Name /= No_Name then
364 -- If there is a body, put it in the mapping
366 if Unit.File_Names (Impl) /= No_Source
367 and then Unit.File_Names (Impl).Project /= No_Project
369 Get_Name_String (Unit.Name);
370 Add_Str_To_Name_Buffer ("%b");
371 ALI_Unit := Name_Find;
373 Lib_File_Name (Unit.File_Names (Impl).Display_File);
374 ALI_Project := Unit.File_Names (Impl).Project;
376 -- Otherwise, if there is a spec, put it in the mapping
378 elsif Unit.File_Names (Spec) /= No_Source
379 and then Unit.File_Names (Spec).Project /= No_Project
381 Get_Name_String (Unit.Name);
382 Add_Str_To_Name_Buffer ("%s");
383 ALI_Unit := Name_Find;
385 Lib_File_Name (Unit.File_Names (Spec).Display_File);
386 ALI_Project := Unit.File_Names (Spec).Project;
392 -- If we have something to put in the mapping then do it now.
393 -- However, if the project is extended, we don't put anything
394 -- in the mapping file, since we don't know where the ALI file
395 -- is: it might be in the extended project object directory as
396 -- well as in the extending project object directory.
398 if ALI_Name /= No_File
399 and then ALI_Project.Extended_By = No_Project
400 and then ALI_Project.Extends = No_Project
402 -- First check if the ALI file exists. If it does not, do
403 -- not put the unit in the mapping file.
406 ALI : constant String := Get_Name_String (ALI_Name);
409 -- For library projects, use the library ALI directory,
410 -- for other projects, use the object directory.
412 if ALI_Project.Library then
414 (ALI_Project.Library_ALI_Dir.Display_Name);
417 (ALI_Project.Object_Directory.Display_Name);
421 Is_Directory_Separator (Name_Buffer (Name_Len))
423 Add_Char_To_Name_Buffer (Directory_Separator);
426 Add_Str_To_Name_Buffer (ALI);
427 Add_Char_To_Name_Buffer (ASCII.LF);
430 ALI_Path_Name : constant String :=
431 Name_Buffer (1 .. Name_Len);
435 (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
437 -- First line is the unit name
439 Get_Name_String (ALI_Unit);
440 Add_Char_To_Name_Buffer (ASCII.LF);
444 Name_Buffer (1)'Address,
446 OK := Bytes = Name_Len;
450 -- Second line it the ALI file name
452 Get_Name_String (ALI_Name);
453 Add_Char_To_Name_Buffer (ASCII.LF);
457 Name_Buffer (1)'Address,
459 OK := (Bytes = Name_Len);
463 -- Third line it the ALI path name
468 ALI_Path_Name (1)'Address,
469 ALI_Path_Name'Length);
470 OK := (Bytes = ALI_Path_Name'Length);
472 -- If OK is False, it means we were unable to
473 -- write a line. No point in continuing with the
483 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
486 Close (Mapping_FD, Status);
491 -- If the creation of the mapping file was successful, we add the switch
492 -- to the arguments of gnatbind.
500 end Create_Binder_Mapping_File;
506 function Create_Name (Name : String) return File_Name_Type is
509 Add_Str_To_Name_Buffer (Name);
513 function Create_Name (Name : String) return Name_Id is
516 Add_Str_To_Name_Buffer (Name);
520 function Create_Name (Name : String) return Path_Name_Type is
523 Add_Str_To_Name_Buffer (Name);
527 ----------------------
528 -- Delete_All_Marks --
529 ----------------------
531 procedure Delete_All_Marks is
534 end Delete_All_Marks;
536 ----------------------------
537 -- Executable_Prefix_Path --
538 ----------------------------
540 function Executable_Prefix_Path return String is
541 Exec_Name : constant String := Command_Name;
543 function Get_Install_Dir (S : String) return String;
544 -- S is the executable name preceded by the absolute or relative path,
545 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
546 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
547 -- directory, return "".
549 ---------------------
550 -- Get_Install_Dir --
551 ---------------------
553 function Get_Install_Dir (S : String) return String is
555 Path_Last : Integer := 0;
558 for J in reverse Exec'Range loop
559 if Exec (J) = Directory_Separator then
565 if Path_Last >= Exec'First + 2 then
566 To_Lower (Exec (Path_Last - 2 .. Path_Last));
569 if Path_Last < Exec'First + 2
570 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
571 or else (Path_Last - 3 >= Exec'First
572 and then Exec (Path_Last - 3) /= Directory_Separator)
577 return Normalize_Pathname
578 (Exec (Exec'First .. Path_Last - 4),
579 Resolve_Links => Opt.Follow_Links_For_Dirs)
580 & Directory_Separator;
583 -- Beginning of Executable_Prefix_Path
586 -- For VMS, the path returned is always /gnu/
588 if Hostparm.OpenVMS then
592 -- First determine if a path prefix was placed in front of the
595 for J in reverse Exec_Name'Range loop
596 if Exec_Name (J) = Directory_Separator then
597 return Get_Install_Dir (Exec_Name);
601 -- If we get here, the user has typed the executable name with no
605 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
611 Dir : constant String := Get_Install_Dir (Path.all);
618 end Executable_Prefix_Path;
620 --------------------------
621 -- File_Not_A_Source_Of --
622 --------------------------
624 function File_Not_A_Source_Of
626 Sfile : File_Name_Type) return Boolean
628 Unit : constant Unit_Index :=
629 Units_Htable.Get (Project_Tree.Units_HT, Uname);
631 At_Least_One_File : Boolean := False;
634 if Unit /= No_Unit_Index then
635 for F in Unit.File_Names'Range loop
636 if Unit.File_Names (F) /= null then
637 At_Least_One_File := True;
638 if Unit.File_Names (F).File = Sfile then
644 if not At_Least_One_File then
646 -- The unit was probably created initially for a separate unit
647 -- (which are initially created as IMPL when both suffixes are the
648 -- same). Later on, Override_Kind changed the type of the file,
649 -- and the unit is no longer valid in fact.
654 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
659 end File_Not_A_Source_Of;
665 function Hash (Key : Mark_Key) return Mark_Num is
667 return Union_Id (Key.File) mod Max_Mask_Num;
674 procedure Inform (N : File_Name_Type; Msg : String) is
676 Inform (Name_Id (N), Msg);
679 procedure Inform (N : Name_Id := No_Name; Msg : String) is
681 Osint.Write_Program_Name;
689 Name : constant String := Get_Name_String (N);
691 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
692 Write_Str (File_Name (Name));
705 ----------------------------
706 -- Is_External_Assignment --
707 ----------------------------
709 function Is_External_Assignment
710 (Tree : Prj.Tree.Project_Node_Tree_Ref;
711 Argv : String) return Boolean
713 Start : Positive := 3;
714 Finish : Natural := Argv'Last;
716 pragma Assert (Argv'First = 1);
717 pragma Assert (Argv (1 .. 2) = "-X");
720 if Argv'Last < 5 then
723 elsif Argv (3) = '"' then
724 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
728 Finish := Argv'Last - 1;
734 Declaration => Argv (Start .. Finish));
735 end Is_External_Assignment;
742 (Source_File : File_Name_Type;
743 Index : Int := 0) return Boolean
746 return Marks.Get (K => (File => Source_File, Index => Index));
749 -----------------------------
750 -- Linker_Options_Switches --
751 -----------------------------
753 function Linker_Options_Switches
754 (Project : Project_Id;
755 In_Tree : Project_Tree_Ref) return String_List
757 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
758 -- The recursive routine used to add linker options
764 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
765 pragma Unreferenced (Dummy);
767 Linker_Package : Package_Id;
768 Options : Variable_Value;
773 (Name => Name_Linker,
774 In_Packages => Proj.Decl.Packages,
781 Attribute_Or_Array_Name => Name_Linker_Options,
782 In_Package => Linker_Package,
785 -- If attribute is present, add the project with
786 -- the attribute to table Linker_Opts.
788 if Options /= Nil_Variable_Value then
789 Linker_Opts.Increment_Last;
790 Linker_Opts.Table (Linker_Opts.Last) :=
791 (Project => Proj, Options => Options.Values);
795 procedure For_All_Projects is
796 new For_Every_Project_Imported (Boolean, Recursive_Add);
798 Dummy : Boolean := False;
800 -- Start of processing for Linker_Options_Switches
805 For_All_Projects (Project, Dummy, Imported_First => True);
807 Last_Linker_Option := 0;
809 for Index in reverse 1 .. Linker_Opts.Last loop
811 Options : String_List_Id;
812 Proj : constant Project_Id :=
813 Linker_Opts.Table (Index).Project;
815 Dir_Path : constant String :=
816 Get_Name_String (Proj.Directory.Name);
819 Options := Linker_Opts.Table (Index).Options;
820 while Options /= Nil_String loop
821 Option := In_Tree.String_Elements.Table (Options).Value;
822 Get_Name_String (Option);
824 -- Do not consider empty linker options
826 if Name_Len /= 0 then
827 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
829 -- Object files and -L switches specified with relative
830 -- paths must be converted to absolute paths.
832 Test_If_Relative_Path
833 (Switch => Linker_Options_Buffer (Last_Linker_Option),
835 Including_L_Switch => True);
838 Options := In_Tree.String_Elements.Table (Options).Next;
843 return Linker_Options_Buffer (1 .. Last_Linker_Option);
844 end Linker_Options_Switches;
850 package body Mains is
852 type File_And_Loc is record
853 File_Name : File_Name_Type;
855 Location : Source_Ptr := No_Location;
858 package Names is new Table.Table
859 (Table_Component_Type => File_And_Loc,
860 Table_Index_Type => Integer,
861 Table_Low_Bound => 1,
863 Table_Increment => 100,
864 Table_Name => "Makeutl.Mains.Names");
865 -- The table that stores the mains
867 Current : Natural := 0;
868 -- The index of the last main retrieved from the table
874 procedure Add_Main (Name : String) is
877 Add_Str_To_Name_Buffer (Name);
878 Names.Increment_Last;
879 Names.Table (Names.Last) := (Name_Find, 0, No_Location);
896 function Get_Index return Int is
898 if Current in Names.First .. Names.Last then
899 return Names.Table (Current).Index;
909 function Get_Location return Source_Ptr is
911 if Current in Names.First .. Names.Last then
912 return Names.Table (Current).Location;
922 function Next_Main return String is
924 if Current >= Names.Last then
927 Current := Current + 1;
928 return Get_Name_String (Names.Table (Current).File_Name);
932 ---------------------
933 -- Number_Of_Mains --
934 ---------------------
936 function Number_Of_Mains return Natural is
954 procedure Set_Index (Index : Int) is
956 if Names.Last > 0 then
957 Names.Table (Names.Last).Index := Index;
965 procedure Set_Location (Location : Source_Ptr) is
967 if Names.Last > 0 then
968 Names.Table (Names.Last).Location := Location;
976 procedure Update_Main (Name : String) is
978 if Current in Names.First .. Names.Last then
980 Add_Str_To_Name_Buffer (Name);
981 Names.Table (Current).File_Name := Name_Find;
990 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
992 Marks.Set (K => (File => Source_File, Index => Index), E => True);
995 -----------------------
996 -- Path_Or_File_Name --
997 -----------------------
999 function Path_Or_File_Name (Path : Path_Name_Type) return String is
1000 Path_Name : constant String := Get_Name_String (Path);
1002 if Debug.Debug_Flag_F then
1003 return File_Name (Path_Name);
1007 end Path_Or_File_Name;
1009 ---------------------------
1010 -- Test_If_Relative_Path --
1011 ---------------------------
1013 procedure Test_If_Relative_Path
1014 (Switch : in out String_Access;
1016 Including_L_Switch : Boolean := True;
1017 Including_Non_Switch : Boolean := True;
1018 Including_RTS : Boolean := False)
1021 if Switch /= null then
1023 Sw : String (1 .. Switch'Length);
1029 if Sw (1) = '-' then
1031 and then (Sw (2) = 'A'
1032 or else Sw (2) = 'I'
1033 or else (Including_L_Switch and then Sw (2) = 'L'))
1041 elsif Sw'Length >= 4
1042 and then (Sw (2 .. 3) = "aL"
1043 or else Sw (2 .. 3) = "aO"
1044 or else Sw (2 .. 3) = "aI")
1049 and then Sw'Length >= 7
1050 and then Sw (2 .. 6) = "-RTS="
1058 -- Because relative path arguments to --RTS= may be relative
1059 -- to the search directory prefix, those relative path
1060 -- arguments are converted only when they include directory
1063 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1064 if Parent'Length = 0 then
1066 ("relative search path switches ("""
1068 & """) are not allowed");
1070 elsif Including_RTS then
1071 for J in Start .. Sw'Last loop
1072 if Sw (J) = Directory_Separator then
1075 (Sw (1 .. Start - 1) &
1077 Directory_Separator &
1078 Sw (Start .. Sw'Last));
1086 (Sw (1 .. Start - 1) &
1088 Directory_Separator &
1089 Sw (Start .. Sw'Last));
1093 elsif Including_Non_Switch then
1094 if not Is_Absolute_Path (Sw) then
1095 if Parent'Length = 0 then
1097 ("relative paths (""" & Sw & """) are not allowed");
1099 Switch := new String'(Parent & Directory_Separator & Sw);
1105 end Test_If_Relative_Path;
1111 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
1117 Get_Name_String (ALI_File);
1119 -- First, find the last dot
1123 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
1124 Finish := Finish - 1;
1131 -- Now check that the dot is preceded by digits
1134 Finish := Finish - 1;
1136 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
1140 -- If there are no digits, or if the digits are not preceded by the
1141 -- character that precedes a unit index, this is not the ALI file of
1142 -- a unit in a multi-unit source.
1146 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
1151 -- Build the index from the digit(s)
1153 while Start <= Finish loop
1154 Result := Result * 10 +
1155 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
1166 procedure Verbose_Msg
1169 N2 : Name_Id := No_Name;
1171 Prefix : String := " -> ";
1172 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1175 if not Opt.Verbose_Mode
1176 or else Minimum_Verbosity > Opt.Verbosity_Level
1187 if N2 /= No_Name then
1197 procedure Verbose_Msg
1198 (N1 : File_Name_Type;
1200 N2 : File_Name_Type := No_File;
1202 Prefix : String := " -> ";
1203 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1207 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);