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);
420 Add_Str_To_Name_Buffer (ALI);
421 Add_Char_To_Name_Buffer (ASCII.LF);
424 ALI_Path_Name : constant String :=
425 Name_Buffer (1 .. Name_Len);
429 (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
431 -- First line is the unit name
433 Get_Name_String (ALI_Unit);
434 Add_Char_To_Name_Buffer (ASCII.LF);
438 Name_Buffer (1)'Address,
440 OK := Bytes = Name_Len;
444 -- Second line it the ALI file name
446 Get_Name_String (ALI_Name);
447 Add_Char_To_Name_Buffer (ASCII.LF);
451 Name_Buffer (1)'Address,
453 OK := (Bytes = Name_Len);
457 -- Third line it the ALI path name
462 ALI_Path_Name (1)'Address,
463 ALI_Path_Name'Length);
464 OK := (Bytes = ALI_Path_Name'Length);
466 -- If OK is False, it means we were unable to
467 -- write a line. No point in continuing with the
477 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
480 Close (Mapping_FD, Status);
485 -- If the creation of the mapping file was successful, we add the switch
486 -- to the arguments of gnatbind.
494 end Create_Binder_Mapping_File;
500 function Create_Name (Name : String) return File_Name_Type is
503 Add_Str_To_Name_Buffer (Name);
507 function Create_Name (Name : String) return Name_Id is
510 Add_Str_To_Name_Buffer (Name);
514 function Create_Name (Name : String) return Path_Name_Type is
517 Add_Str_To_Name_Buffer (Name);
521 ----------------------
522 -- Delete_All_Marks --
523 ----------------------
525 procedure Delete_All_Marks is
528 end Delete_All_Marks;
530 ----------------------------
531 -- Executable_Prefix_Path --
532 ----------------------------
534 function Executable_Prefix_Path return String is
535 Exec_Name : constant String := Command_Name;
537 function Get_Install_Dir (S : String) return String;
538 -- S is the executable name preceded by the absolute or relative path,
539 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
540 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
541 -- directory, return "".
543 ---------------------
544 -- Get_Install_Dir --
545 ---------------------
547 function Get_Install_Dir (S : String) return String is
549 Path_Last : Integer := 0;
552 for J in reverse Exec'Range loop
553 if Exec (J) = Directory_Separator then
559 if Path_Last >= Exec'First + 2 then
560 To_Lower (Exec (Path_Last - 2 .. Path_Last));
563 if Path_Last < Exec'First + 2
564 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
565 or else (Path_Last - 3 >= Exec'First
566 and then Exec (Path_Last - 3) /= Directory_Separator)
571 return Normalize_Pathname
572 (Exec (Exec'First .. Path_Last - 4),
573 Resolve_Links => Opt.Follow_Links_For_Dirs)
574 & Directory_Separator;
577 -- Beginning of Executable_Prefix_Path
580 -- For VMS, the path returned is always /gnu/
582 if Hostparm.OpenVMS then
586 -- First determine if a path prefix was placed in front of the
589 for J in reverse Exec_Name'Range loop
590 if Exec_Name (J) = Directory_Separator then
591 return Get_Install_Dir (Exec_Name);
595 -- If we get here, the user has typed the executable name with no
599 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
605 Dir : constant String := Get_Install_Dir (Path.all);
612 end Executable_Prefix_Path;
614 --------------------------
615 -- File_Not_A_Source_Of --
616 --------------------------
618 function File_Not_A_Source_Of
620 Sfile : File_Name_Type) return Boolean
622 Unit : constant Unit_Index :=
623 Units_Htable.Get (Project_Tree.Units_HT, Uname);
625 At_Least_One_File : Boolean := False;
628 if Unit /= No_Unit_Index then
629 for F in Unit.File_Names'Range loop
630 if Unit.File_Names (F) /= null then
631 At_Least_One_File := True;
632 if Unit.File_Names (F).File = Sfile then
638 if not At_Least_One_File then
640 -- The unit was probably created initially for a separate unit
641 -- (which are initially created as IMPL when both suffixes are the
642 -- same). Later on, Override_Kind changed the type of the file,
643 -- and the unit is no longer valid in fact.
648 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
653 end File_Not_A_Source_Of;
659 function Hash (Key : Mark_Key) return Mark_Num is
661 return Union_Id (Key.File) mod Max_Mask_Num;
668 procedure Inform (N : File_Name_Type; Msg : String) is
670 Inform (Name_Id (N), Msg);
673 procedure Inform (N : Name_Id := No_Name; Msg : String) is
675 Osint.Write_Program_Name;
683 Name : constant String := Get_Name_String (N);
685 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
686 Write_Str (File_Name (Name));
699 ----------------------------
700 -- Is_External_Assignment --
701 ----------------------------
703 function Is_External_Assignment
704 (Tree : Prj.Tree.Project_Node_Tree_Ref;
705 Argv : String) return Boolean
707 Start : Positive := 3;
708 Finish : Natural := Argv'Last;
710 pragma Assert (Argv'First = 1);
711 pragma Assert (Argv (1 .. 2) = "-X");
714 if Argv'Last < 5 then
717 elsif Argv (3) = '"' then
718 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
722 Finish := Argv'Last - 1;
728 Declaration => Argv (Start .. Finish));
729 end Is_External_Assignment;
736 (Source_File : File_Name_Type;
737 Index : Int := 0) return Boolean
740 return Marks.Get (K => (File => Source_File, Index => Index));
743 -----------------------------
744 -- Linker_Options_Switches --
745 -----------------------------
747 function Linker_Options_Switches
748 (Project : Project_Id;
749 In_Tree : Project_Tree_Ref) return String_List
751 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
752 -- The recursive routine used to add linker options
758 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
759 pragma Unreferenced (Dummy);
761 Linker_Package : Package_Id;
762 Options : Variable_Value;
767 (Name => Name_Linker,
768 In_Packages => Proj.Decl.Packages,
775 Attribute_Or_Array_Name => Name_Linker_Options,
776 In_Package => Linker_Package,
779 -- If attribute is present, add the project with
780 -- the attribute to table Linker_Opts.
782 if Options /= Nil_Variable_Value then
783 Linker_Opts.Increment_Last;
784 Linker_Opts.Table (Linker_Opts.Last) :=
785 (Project => Proj, Options => Options.Values);
789 procedure For_All_Projects is
790 new For_Every_Project_Imported (Boolean, Recursive_Add);
792 Dummy : Boolean := False;
794 -- Start of processing for Linker_Options_Switches
799 For_All_Projects (Project, Dummy, Imported_First => True);
801 Last_Linker_Option := 0;
803 for Index in reverse 1 .. Linker_Opts.Last loop
805 Options : String_List_Id;
806 Proj : constant Project_Id :=
807 Linker_Opts.Table (Index).Project;
809 Dir_Path : constant String :=
810 Get_Name_String (Proj.Directory.Name);
813 Options := Linker_Opts.Table (Index).Options;
814 while Options /= Nil_String loop
815 Option := In_Tree.String_Elements.Table (Options).Value;
816 Get_Name_String (Option);
818 -- Do not consider empty linker options
820 if Name_Len /= 0 then
821 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
823 -- Object files and -L switches specified with relative
824 -- paths must be converted to absolute paths.
826 Test_If_Relative_Path
827 (Switch => Linker_Options_Buffer (Last_Linker_Option),
829 Including_L_Switch => True);
832 Options := In_Tree.String_Elements.Table (Options).Next;
837 return Linker_Options_Buffer (1 .. Last_Linker_Option);
838 end Linker_Options_Switches;
844 package body Mains is
846 type File_And_Loc is record
847 File_Name : File_Name_Type;
849 Location : Source_Ptr := No_Location;
852 package Names is new Table.Table
853 (Table_Component_Type => File_And_Loc,
854 Table_Index_Type => Integer,
855 Table_Low_Bound => 1,
857 Table_Increment => 100,
858 Table_Name => "Makeutl.Mains.Names");
859 -- The table that stores the mains
861 Current : Natural := 0;
862 -- The index of the last main retrieved from the table
868 procedure Add_Main (Name : String) is
871 Add_Str_To_Name_Buffer (Name);
872 Names.Increment_Last;
873 Names.Table (Names.Last) := (Name_Find, 0, No_Location);
890 function Get_Index return Int is
892 if Current in Names.First .. Names.Last then
893 return Names.Table (Current).Index;
903 function Get_Location return Source_Ptr is
905 if Current in Names.First .. Names.Last then
906 return Names.Table (Current).Location;
916 function Next_Main return String is
918 if Current >= Names.Last then
921 Current := Current + 1;
922 return Get_Name_String (Names.Table (Current).File_Name);
926 ---------------------
927 -- Number_Of_Mains --
928 ---------------------
930 function Number_Of_Mains return Natural is
948 procedure Set_Index (Index : Int) is
950 if Names.Last > 0 then
951 Names.Table (Names.Last).Index := Index;
959 procedure Set_Location (Location : Source_Ptr) is
961 if Names.Last > 0 then
962 Names.Table (Names.Last).Location := Location;
970 procedure Update_Main (Name : String) is
972 if Current in Names.First .. Names.Last then
974 Add_Str_To_Name_Buffer (Name);
975 Names.Table (Current).File_Name := Name_Find;
984 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
986 Marks.Set (K => (File => Source_File, Index => Index), E => True);
989 -----------------------
990 -- Path_Or_File_Name --
991 -----------------------
993 function Path_Or_File_Name (Path : Path_Name_Type) return String is
994 Path_Name : constant String := Get_Name_String (Path);
996 if Debug.Debug_Flag_F then
997 return File_Name (Path_Name);
1001 end Path_Or_File_Name;
1003 ---------------------------
1004 -- Test_If_Relative_Path --
1005 ---------------------------
1007 procedure Test_If_Relative_Path
1008 (Switch : in out String_Access;
1010 Including_L_Switch : Boolean := True;
1011 Including_Non_Switch : Boolean := True;
1012 Including_RTS : Boolean := False)
1015 if Switch /= null then
1017 Sw : String (1 .. Switch'Length);
1023 if Sw (1) = '-' then
1025 and then (Sw (2) = 'A'
1026 or else Sw (2) = 'I'
1027 or else (Including_L_Switch and then Sw (2) = 'L'))
1035 elsif Sw'Length >= 4
1036 and then (Sw (2 .. 3) = "aL"
1037 or else Sw (2 .. 3) = "aO"
1038 or else Sw (2 .. 3) = "aI")
1043 and then Sw'Length >= 7
1044 and then Sw (2 .. 6) = "-RTS="
1052 -- Because relative path arguments to --RTS= may be relative
1053 -- to the search directory prefix, those relative path
1054 -- arguments are converted only when they include directory
1057 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1058 if Parent'Length = 0 then
1060 ("relative search path switches ("""
1062 & """) are not allowed");
1064 elsif Including_RTS then
1065 for J in Start .. Sw'Last loop
1066 if Sw (J) = Directory_Separator then
1069 (Sw (1 .. Start - 1) &
1071 Directory_Separator &
1072 Sw (Start .. Sw'Last));
1080 (Sw (1 .. Start - 1) &
1082 Directory_Separator &
1083 Sw (Start .. Sw'Last));
1087 elsif Including_Non_Switch then
1088 if not Is_Absolute_Path (Sw) then
1089 if Parent'Length = 0 then
1091 ("relative paths (""" & Sw & """) are not allowed");
1093 Switch := new String'(Parent & Directory_Separator & Sw);
1099 end Test_If_Relative_Path;
1105 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
1111 Get_Name_String (ALI_File);
1113 -- First, find the last dot
1117 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
1118 Finish := Finish - 1;
1125 -- Now check that the dot is preceded by digits
1128 Finish := Finish - 1;
1130 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
1134 -- If there are no digits, or if the digits are not preceded by the
1135 -- character that precedes a unit index, this is not the ALI file of
1136 -- a unit in a multi-unit source.
1140 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
1145 -- Build the index from the digit(s)
1147 while Start <= Finish loop
1148 Result := Result * 10 +
1149 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
1160 procedure Verbose_Msg
1163 N2 : Name_Id := No_Name;
1165 Prefix : String := " -> ";
1166 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1169 if not Opt.Verbose_Mode
1170 or else Minimum_Verbosity > Opt.Verbosity_Level
1181 if N2 /= No_Name then
1191 procedure Verbose_Msg
1192 (N1 : File_Name_Type;
1194 N2 : File_Name_Type := No_File;
1196 Prefix : String := " -> ";
1197 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1201 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);