1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004-2009, 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 ------------------------------------------------------------------------------
29 with Osint; use Osint;
30 with Output; use Output;
34 with Snames; use Snames;
37 with Ada.Command_Line; use Ada.Command_Line;
39 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
41 with System.Case_Util; use System.Case_Util;
44 package body Makeutl is
46 type Mark_Key is record
47 File : File_Name_Type;
50 -- Identify either a mono-unit source (when Index = 0) or a specific unit
51 -- (index = 1's origin index of unit) in a multi-unit source.
53 -- There follow many global undocumented declarations, comments needed ???
55 Max_Mask_Num : constant := 2048;
57 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
59 function Hash (Key : Mark_Key) return Mark_Num;
61 package Marks is new System.HTable.Simple_HTable
62 (Header_Num => Mark_Num,
68 -- A hash table to keep tracks of the marked units
70 type Linker_Options_Data is record
72 Options : String_List_Id;
75 Linker_Option_Initial_Count : constant := 20;
77 Linker_Options_Buffer : String_List_Access :=
78 new String_List (1 .. Linker_Option_Initial_Count);
80 Last_Linker_Option : Natural := 0;
82 package Linker_Opts is new Table.Table (
83 Table_Component_Type => Linker_Options_Data,
84 Table_Index_Type => Integer,
87 Table_Increment => 100,
88 Table_Name => "Make.Linker_Opts");
90 procedure Add_Linker_Option (Option : String);
97 (Option : String_Access;
98 To : in out String_List_Access;
99 Last : in out Natural)
102 if Last = To'Last then
104 New_Options : constant String_List_Access :=
105 new String_List (1 .. To'Last * 2);
108 New_Options (To'Range) := To.all;
110 -- Set all elements of the original options to null to avoid
111 -- deallocation of copies.
113 To.all := (others => null);
126 To : in out String_List_Access;
127 Last : in out Natural)
130 Add (Option => new String'(Option), To => To, Last => Last);
133 -----------------------
134 -- Add_Linker_Option --
135 -----------------------
137 procedure Add_Linker_Option (Option : String) is
139 if Option'Length > 0 then
140 if Last_Linker_Option = Linker_Options_Buffer'Last then
142 New_Buffer : constant String_List_Access :=
144 (1 .. Linker_Options_Buffer'Last +
145 Linker_Option_Initial_Count);
147 New_Buffer (Linker_Options_Buffer'Range) :=
148 Linker_Options_Buffer.all;
149 Linker_Options_Buffer.all := (others => null);
150 Free (Linker_Options_Buffer);
151 Linker_Options_Buffer := New_Buffer;
155 Last_Linker_Option := Last_Linker_Option + 1;
156 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
158 end Add_Linker_Option;
160 ------------------------------
161 -- Check_Source_Info_In_ALI --
162 ------------------------------
164 function Check_Source_Info_In_ALI (The_ALI : ALI_Id) return Boolean is
168 -- Loop through units
170 for U in ALIs.Table (The_ALI).First_Unit ..
171 ALIs.Table (The_ALI).Last_Unit
173 -- Check if the file name is one of the source of the unit
175 Get_Name_String (Units.Table (U).Uname);
176 Name_Len := Name_Len - 2;
177 Unit_Name := Name_Find;
179 if File_Not_A_Source_Of (Unit_Name, Units.Table (U).Sfile) then
183 -- Loop to do same check for each of the withed units
185 for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
187 WR : ALI.With_Record renames Withs.Table (W);
190 if WR.Sfile /= No_File then
191 Get_Name_String (WR.Uname);
192 Name_Len := Name_Len - 2;
193 Unit_Name := Name_Find;
195 if File_Not_A_Source_Of (Unit_Name, WR.Sfile) then
203 -- Loop to check subunits
205 for D in ALIs.Table (The_ALI).First_Sdep ..
206 ALIs.Table (The_ALI).Last_Sdep
209 SD : Sdep_Record renames Sdep.Table (D);
212 Unit_Name := SD.Subunit_Name;
214 if Unit_Name /= No_Name then
216 -- For separates, the file is no longer associated with the
217 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
218 -- so we need to check whether the source file still exists in
219 -- the source tree: it will if it matches the naming scheme
220 -- (and then will be for the same unit).
223 (In_Tree => Project_Tree,
224 Project => No_Project,
225 Base_Name => SD.Sfile) = No_Source
227 -- If this is not a runtime file or if, when gnatmake switch
228 -- -a is used, we are not able to find this subunit in the
229 -- source directories, then recompilation is needed.
231 if not Fname.Is_Internal_File_Name (SD.Sfile)
233 (Check_Readonly_Files
234 and then Find_File (SD.Sfile, Osint.Source) = No_File)
238 ("While parsing ALI file, file "
239 & Get_Name_String (SD.Sfile)
240 & " is indicated as containing subunit "
241 & Get_Name_String (Unit_Name)
242 & " but this does not match what was found while"
243 & " parsing the project. Will recompile");
254 end Check_Source_Info_In_ALI;
260 function Create_Name (Name : String) return File_Name_Type is
263 Add_Str_To_Name_Buffer (Name);
267 function Create_Name (Name : String) return Name_Id is
270 Add_Str_To_Name_Buffer (Name);
274 function Create_Name (Name : String) return Path_Name_Type is
277 Add_Str_To_Name_Buffer (Name);
281 ----------------------
282 -- Delete_All_Marks --
283 ----------------------
285 procedure Delete_All_Marks is
288 end Delete_All_Marks;
290 ----------------------------
291 -- Executable_Prefix_Path --
292 ----------------------------
294 function Executable_Prefix_Path return String is
295 Exec_Name : constant String := Command_Name;
297 function Get_Install_Dir (S : String) return String;
298 -- S is the executable name preceded by the absolute or relative path,
299 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
300 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
301 -- directory, return "".
303 ---------------------
304 -- Get_Install_Dir --
305 ---------------------
307 function Get_Install_Dir (S : String) return String is
309 Path_Last : Integer := 0;
312 for J in reverse Exec'Range loop
313 if Exec (J) = Directory_Separator then
319 if Path_Last >= Exec'First + 2 then
320 To_Lower (Exec (Path_Last - 2 .. Path_Last));
323 if Path_Last < Exec'First + 2
324 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
325 or else (Path_Last - 3 >= Exec'First
326 and then Exec (Path_Last - 3) /= Directory_Separator)
331 return Normalize_Pathname
332 (Exec (Exec'First .. Path_Last - 4),
333 Resolve_Links => Opt.Follow_Links_For_Dirs)
334 & Directory_Separator;
337 -- Beginning of Executable_Prefix_Path
340 -- First determine if a path prefix was placed in front of the
343 for J in reverse Exec_Name'Range loop
344 if Exec_Name (J) = Directory_Separator then
345 return Get_Install_Dir (Exec_Name);
349 -- If we get here, the user has typed the executable name with no
353 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
359 Dir : constant String := Get_Install_Dir (Path.all);
366 end Executable_Prefix_Path;
368 --------------------------
369 -- File_Not_A_Source_Of --
370 --------------------------
372 function File_Not_A_Source_Of
374 Sfile : File_Name_Type) return Boolean
376 Unit : constant Unit_Index :=
377 Units_Htable.Get (Project_Tree.Units_HT, Uname);
379 At_Least_One_File : Boolean := False;
382 if Unit /= No_Unit_Index then
383 for F in Unit.File_Names'Range loop
384 if Unit.File_Names (F) /= null then
385 At_Least_One_File := True;
386 if Unit.File_Names (F).File = Sfile then
392 if not At_Least_One_File then
394 -- The unit was probably created initially for a separate unit
395 -- (which are initially created as IMPL when both suffixes are the
396 -- same). Later on, Override_Kind changed the type of the file,
397 -- and the unit is no longer valid in fact.
402 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
407 end File_Not_A_Source_Of;
413 function Hash (Key : Mark_Key) return Mark_Num is
415 return Union_Id (Key.File) mod Max_Mask_Num;
422 procedure Inform (N : File_Name_Type; Msg : String) is
424 Inform (Name_Id (N), Msg);
427 procedure Inform (N : Name_Id := No_Name; Msg : String) is
429 Osint.Write_Program_Name;
437 Name : constant String := Get_Name_String (N);
439 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
440 Write_Str (File_Name (Name));
453 ----------------------------
454 -- Is_External_Assignment --
455 ----------------------------
457 function Is_External_Assignment
458 (Tree : Prj.Tree.Project_Node_Tree_Ref;
459 Argv : String) return Boolean
461 Start : Positive := 3;
462 Finish : Natural := Argv'Last;
464 pragma Assert (Argv'First = 1);
465 pragma Assert (Argv (1 .. 2) = "-X");
468 if Argv'Last < 5 then
471 elsif Argv (3) = '"' then
472 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
476 Finish := Argv'Last - 1;
482 Declaration => Argv (Start .. Finish));
483 end Is_External_Assignment;
490 (Source_File : File_Name_Type;
491 Index : Int := 0) return Boolean
494 return Marks.Get (K => (File => Source_File, Index => Index));
497 -----------------------------
498 -- Linker_Options_Switches --
499 -----------------------------
501 function Linker_Options_Switches
502 (Project : Project_Id;
503 In_Tree : Project_Tree_Ref) return String_List
505 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
506 -- The recursive routine used to add linker options
512 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
513 pragma Unreferenced (Dummy);
515 Linker_Package : Package_Id;
516 Options : Variable_Value;
521 (Name => Name_Linker,
522 In_Packages => Proj.Decl.Packages,
529 Attribute_Or_Array_Name => Name_Linker_Options,
530 In_Package => Linker_Package,
533 -- If attribute is present, add the project with
534 -- the attribute to table Linker_Opts.
536 if Options /= Nil_Variable_Value then
537 Linker_Opts.Increment_Last;
538 Linker_Opts.Table (Linker_Opts.Last) :=
539 (Project => Proj, Options => Options.Values);
543 procedure For_All_Projects is
544 new For_Every_Project_Imported (Boolean, Recursive_Add);
546 Dummy : Boolean := False;
548 -- Start of processing for Linker_Options_Switches
553 For_All_Projects (Project, Dummy, Imported_First => True);
555 Last_Linker_Option := 0;
557 for Index in reverse 1 .. Linker_Opts.Last loop
559 Options : String_List_Id;
560 Proj : constant Project_Id :=
561 Linker_Opts.Table (Index).Project;
563 Dir_Path : constant String :=
564 Get_Name_String (Proj.Directory.Name);
567 Options := Linker_Opts.Table (Index).Options;
568 while Options /= Nil_String loop
569 Option := In_Tree.String_Elements.Table (Options).Value;
570 Get_Name_String (Option);
572 -- Do not consider empty linker options
574 if Name_Len /= 0 then
575 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
577 -- Object files and -L switches specified with relative
578 -- paths must be converted to absolute paths.
580 Test_If_Relative_Path
581 (Switch => Linker_Options_Buffer (Last_Linker_Option),
583 Including_L_Switch => True);
586 Options := In_Tree.String_Elements.Table (Options).Next;
591 return Linker_Options_Buffer (1 .. Last_Linker_Option);
592 end Linker_Options_Switches;
598 package body Mains is
600 type File_And_Loc is record
601 File_Name : File_Name_Type;
602 Location : Source_Ptr := No_Location;
605 package Names is new Table.Table
606 (Table_Component_Type => File_And_Loc,
607 Table_Index_Type => Integer,
608 Table_Low_Bound => 1,
610 Table_Increment => 100,
611 Table_Name => "Makeutl.Mains.Names");
612 -- The table that stores the mains
614 Current : Natural := 0;
615 -- The index of the last main retrieved from the table
621 procedure Add_Main (Name : String) is
624 Add_Str_To_Name_Buffer (Name);
625 Names.Increment_Last;
626 Names.Table (Names.Last) := (Name_Find, No_Location);
643 function Get_Location return Source_Ptr is
645 if Current in Names.First .. Names.Last then
646 return Names.Table (Current).Location;
656 function Next_Main return String is
658 if Current >= Names.Last then
661 Current := Current + 1;
662 return Get_Name_String (Names.Table (Current).File_Name);
666 ---------------------
667 -- Number_Of_Mains --
668 ---------------------
670 function Number_Of_Mains return Natural is
688 procedure Set_Location (Location : Source_Ptr) is
690 if Names.Last > 0 then
691 Names.Table (Names.Last).Location := Location;
699 procedure Update_Main (Name : String) is
701 if Current in Names.First .. Names.Last then
703 Add_Str_To_Name_Buffer (Name);
704 Names.Table (Current).File_Name := Name_Find;
713 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
715 Marks.Set (K => (File => Source_File, Index => Index), E => True);
718 -----------------------
719 -- Path_Or_File_Name --
720 -----------------------
722 function Path_Or_File_Name (Path : Path_Name_Type) return String is
723 Path_Name : constant String := Get_Name_String (Path);
725 if Debug.Debug_Flag_F then
726 return File_Name (Path_Name);
730 end Path_Or_File_Name;
732 ---------------------------
733 -- Test_If_Relative_Path --
734 ---------------------------
736 procedure Test_If_Relative_Path
737 (Switch : in out String_Access;
739 Including_L_Switch : Boolean := True;
740 Including_Non_Switch : Boolean := True;
741 Including_RTS : Boolean := False)
744 if Switch /= null then
746 Sw : String (1 .. Switch'Length);
754 and then (Sw (2) = 'A'
756 or else (Including_L_Switch and then Sw (2) = 'L'))
765 and then (Sw (2 .. 3) = "aL"
766 or else Sw (2 .. 3) = "aO"
767 or else Sw (2 .. 3) = "aI")
772 and then Sw'Length >= 7
773 and then Sw (2 .. 6) = "-RTS="
781 -- Because relative path arguments to --RTS= may be relative
782 -- to the search directory prefix, those relative path
783 -- arguments are converted only when they include directory
786 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
787 if Parent'Length = 0 then
789 ("relative search path switches ("""
791 & """) are not allowed");
793 elsif Including_RTS then
794 for J in Start .. Sw'Last loop
795 if Sw (J) = Directory_Separator then
798 (Sw (1 .. Start - 1) &
800 Directory_Separator &
801 Sw (Start .. Sw'Last));
809 (Sw (1 .. Start - 1) &
811 Directory_Separator &
812 Sw (Start .. Sw'Last));
816 elsif Including_Non_Switch then
817 if not Is_Absolute_Path (Sw) then
818 if Parent'Length = 0 then
820 ("relative paths (""" & Sw & """) are not allowed");
822 Switch := new String'(Parent & Directory_Separator & Sw);
828 end Test_If_Relative_Path;
834 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
840 Get_Name_String (ALI_File);
842 -- First, find the last dot
846 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
847 Finish := Finish - 1;
854 -- Now check that the dot is preceded by digits
857 Finish := Finish - 1;
859 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
863 -- If there are no digits, or if the digits are not preceded by the
864 -- character that precedes a unit index, this is not the ALI file of
865 -- a unit in a multi-unit source.
869 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
874 -- Build the index from the digit(s)
876 while Start <= Finish loop
877 Result := Result * 10 +
878 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
889 procedure Verbose_Msg
892 N2 : Name_Id := No_Name;
894 Prefix : String := " -> ";
895 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
898 if not Opt.Verbose_Mode
899 or else Minimum_Verbosity > Opt.Verbosity_Level
910 if N2 /= No_Name then
920 procedure Verbose_Msg
921 (N1 : File_Name_Type;
923 N2 : File_Name_Type := No_File;
925 Prefix : String := " -> ";
926 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
930 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);