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 ------------------------------------------------------------------------------
28 with Osint; use Osint;
29 with Output; use Output;
33 with Snames; use Snames;
36 with Ada.Command_Line; use Ada.Command_Line;
38 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
40 with System.Case_Util; use System.Case_Util;
43 package body Makeutl is
45 type Mark_Key is record
46 File : File_Name_Type;
49 -- Identify either a mono-unit source (when Index = 0) or a specific unit
50 -- (index = 1's origin index of unit) in a multi-unit source.
52 -- There follow many global undocumented declarations, comments needed ???
54 Max_Mask_Num : constant := 2048;
56 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
58 function Hash (Key : Mark_Key) return Mark_Num;
60 package Marks is new System.HTable.Simple_HTable
61 (Header_Num => Mark_Num,
67 -- A hash table to keep tracks of the marked units
69 type Linker_Options_Data is record
71 Options : String_List_Id;
74 Linker_Option_Initial_Count : constant := 20;
76 Linker_Options_Buffer : String_List_Access :=
77 new String_List (1 .. Linker_Option_Initial_Count);
79 Last_Linker_Option : Natural := 0;
81 package Linker_Opts is new Table.Table (
82 Table_Component_Type => Linker_Options_Data,
83 Table_Index_Type => Integer,
86 Table_Increment => 100,
87 Table_Name => "Make.Linker_Opts");
89 procedure Add_Linker_Option (Option : String);
96 (Option : String_Access;
97 To : in out String_List_Access;
98 Last : in out Natural)
101 if Last = To'Last then
103 New_Options : constant String_List_Access :=
104 new String_List (1 .. To'Last * 2);
107 New_Options (To'Range) := To.all;
109 -- Set all elements of the original options to null to avoid
110 -- deallocation of copies.
112 To.all := (others => null);
125 To : in out String_List_Access;
126 Last : in out Natural)
129 Add (Option => new String'(Option), To => To, Last => Last);
132 -----------------------
133 -- Add_Linker_Option --
134 -----------------------
136 procedure Add_Linker_Option (Option : String) is
138 if Option'Length > 0 then
139 if Last_Linker_Option = Linker_Options_Buffer'Last then
141 New_Buffer : constant String_List_Access :=
143 (1 .. Linker_Options_Buffer'Last +
144 Linker_Option_Initial_Count);
146 New_Buffer (Linker_Options_Buffer'Range) :=
147 Linker_Options_Buffer.all;
148 Linker_Options_Buffer.all := (others => null);
149 Free (Linker_Options_Buffer);
150 Linker_Options_Buffer := New_Buffer;
154 Last_Linker_Option := Last_Linker_Option + 1;
155 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
157 end Add_Linker_Option;
159 ------------------------------
160 -- Check_Source_Info_In_ALI --
161 ------------------------------
163 function Check_Source_Info_In_ALI (The_ALI : ALI_Id) return Boolean is
167 -- Loop through units
169 for U in ALIs.Table (The_ALI).First_Unit ..
170 ALIs.Table (The_ALI).Last_Unit
172 -- Check if the file name is one of the source of the unit
174 Get_Name_String (Units.Table (U).Uname);
175 Name_Len := Name_Len - 2;
176 Unit_Name := Name_Find;
178 if File_Not_A_Source_Of (Unit_Name, Units.Table (U).Sfile) then
182 -- Loop to do same check for each of the withed units
184 for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
186 WR : ALI.With_Record renames Withs.Table (W);
189 if WR.Sfile /= No_File then
190 Get_Name_String (WR.Uname);
191 Name_Len := Name_Len - 2;
192 Unit_Name := Name_Find;
194 if File_Not_A_Source_Of (Unit_Name, WR.Sfile) then
202 -- Loop to check subunits
204 for D in ALIs.Table (The_ALI).First_Sdep ..
205 ALIs.Table (The_ALI).Last_Sdep
208 SD : Sdep_Record renames Sdep.Table (D);
211 Unit_Name := SD.Subunit_Name;
213 if Unit_Name /= No_Name then
215 -- For separates, the file is no longer associated with the
216 -- unit ("proc-sep.adb" is not associated with unit "proc.sep".
217 -- So we need to check whether the source file still exists in
218 -- the source tree: it will if it matches the naming scheme
219 -- (and then will be for the same unit).
222 (In_Tree => Project_Tree,
223 Project => No_Project,
224 Base_Name => SD.Sfile) = No_Source
226 -- If this is not a runtime file (when using -a) ? Otherwise
227 -- we get complaints about a-except.adb, which uses
230 if not Check_Readonly_Files
231 or else Find_File (SD.Sfile, Osint.Source) = No_File
235 ("While parsing ALI file: Sdep associates "
236 & Get_Name_String (SD.Sfile)
237 & " with unit " & Get_Name_String (Unit_Name)
238 & " but this does not match what was found while"
239 & " parsing the project. Will recompile");
249 end Check_Source_Info_In_ALI;
255 function Create_Name (Name : String) return File_Name_Type is
258 Add_Str_To_Name_Buffer (Name);
262 function Create_Name (Name : String) return Name_Id is
265 Add_Str_To_Name_Buffer (Name);
269 function Create_Name (Name : String) return Path_Name_Type is
272 Add_Str_To_Name_Buffer (Name);
276 ----------------------
277 -- Delete_All_Marks --
278 ----------------------
280 procedure Delete_All_Marks is
283 end Delete_All_Marks;
285 ----------------------------
286 -- Executable_Prefix_Path --
287 ----------------------------
289 function Executable_Prefix_Path return String is
290 Exec_Name : constant String := Command_Name;
292 function Get_Install_Dir (S : String) return String;
293 -- S is the executable name preceded by the absolute or relative path,
294 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
295 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
296 -- directory, return "".
298 ---------------------
299 -- Get_Install_Dir --
300 ---------------------
302 function Get_Install_Dir (S : String) return String is
304 Path_Last : Integer := 0;
307 for J in reverse Exec'Range loop
308 if Exec (J) = Directory_Separator then
314 if Path_Last >= Exec'First + 2 then
315 To_Lower (Exec (Path_Last - 2 .. Path_Last));
318 if Path_Last < Exec'First + 2
319 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
320 or else (Path_Last - 3 >= Exec'First
321 and then Exec (Path_Last - 3) /= Directory_Separator)
326 return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4))
327 & Directory_Separator;
330 -- Beginning of Executable_Prefix_Path
333 -- First determine if a path prefix was placed in front of the
336 for J in reverse Exec_Name'Range loop
337 if Exec_Name (J) = Directory_Separator then
338 return Get_Install_Dir (Exec_Name);
342 -- If we get here, the user has typed the executable name with no
346 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
352 Dir : constant String := Get_Install_Dir (Path.all);
359 end Executable_Prefix_Path;
361 --------------------------
362 -- File_Not_A_Source_Of --
363 --------------------------
365 function File_Not_A_Source_Of
367 Sfile : File_Name_Type) return Boolean
369 Unit : constant Unit_Index :=
370 Units_Htable.Get (Project_Tree.Units_HT, Uname);
372 At_Least_One_File : Boolean := False;
375 if Unit /= No_Unit_Index then
376 for F in Unit.File_Names'Range loop
377 if Unit.File_Names (F) /= null then
378 At_Least_One_File := True;
379 if Unit.File_Names (F).File = Sfile then
385 if not At_Least_One_File then
387 -- The unit was probably created initially for a separate unit
388 -- (which are initially created as IMPL when both suffixes are the
389 -- same). Later on, Override_Kind changed the type of the file,
390 -- and the unit is no longer valid in fact.
395 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
400 end File_Not_A_Source_Of;
406 function Hash (Key : Mark_Key) return Mark_Num is
408 return Union_Id (Key.File) mod Max_Mask_Num;
415 procedure Inform (N : File_Name_Type; Msg : String) is
417 Inform (Name_Id (N), Msg);
420 procedure Inform (N : Name_Id := No_Name; Msg : String) is
422 Osint.Write_Program_Name;
430 Name : constant String := Get_Name_String (N);
432 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
433 Write_Str (File_Name (Name));
446 ----------------------------
447 -- Is_External_Assignment --
448 ----------------------------
450 function Is_External_Assignment (Argv : String) return Boolean is
451 Start : Positive := 3;
452 Finish : Natural := Argv'Last;
455 pragma Assert (Argv'First = 1);
456 pragma Assert (Argv (1 .. 2) = "-X");
459 if Argv'Last < 5 then
462 elsif Argv (3) = '"' then
463 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
467 Finish := Argv'Last - 1;
473 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
474 Equal_Pos := Equal_Pos + 1;
477 if Equal_Pos = Start or else Equal_Pos > Finish then
481 (External_Name => Argv (Start .. Equal_Pos - 1),
482 Value => Argv (Equal_Pos + 1 .. Finish));
485 end Is_External_Assignment;
492 (Source_File : File_Name_Type;
493 Index : Int := 0) return Boolean
496 return Marks.Get (K => (File => Source_File, Index => Index));
499 -----------------------------
500 -- Linker_Options_Switches --
501 -----------------------------
503 function Linker_Options_Switches
504 (Project : Project_Id;
505 In_Tree : Project_Tree_Ref) return String_List
507 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
508 -- The recursive routine used to add linker options
514 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
515 pragma Unreferenced (Dummy);
517 Linker_Package : Package_Id;
518 Options : Variable_Value;
523 (Name => Name_Linker,
524 In_Packages => Proj.Decl.Packages,
531 Attribute_Or_Array_Name => Name_Linker_Options,
532 In_Package => Linker_Package,
535 -- If attribute is present, add the project with
536 -- the attribute to table Linker_Opts.
538 if Options /= Nil_Variable_Value then
539 Linker_Opts.Increment_Last;
540 Linker_Opts.Table (Linker_Opts.Last) :=
541 (Project => Proj, Options => Options.Values);
545 procedure For_All_Projects is
546 new For_Every_Project_Imported (Boolean, Recursive_Add);
548 Dummy : Boolean := False;
550 -- Start of processing for Linker_Options_Switches
555 For_All_Projects (Project, Dummy, Imported_First => True);
557 Last_Linker_Option := 0;
559 for Index in reverse 1 .. Linker_Opts.Last loop
561 Options : String_List_Id;
562 Proj : constant Project_Id :=
563 Linker_Opts.Table (Index).Project;
565 Dir_Path : constant String :=
566 Get_Name_String (Proj.Directory.Name);
569 Options := Linker_Opts.Table (Index).Options;
570 while Options /= Nil_String loop
571 Option := In_Tree.String_Elements.Table (Options).Value;
572 Get_Name_String (Option);
574 -- Do not consider empty linker options
576 if Name_Len /= 0 then
577 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
579 -- Object files and -L switches specified with relative
580 -- paths must be converted to absolute paths.
582 Test_If_Relative_Path
583 (Switch => Linker_Options_Buffer (Last_Linker_Option),
585 Including_L_Switch => True);
588 Options := In_Tree.String_Elements.Table (Options).Next;
593 return Linker_Options_Buffer (1 .. Last_Linker_Option);
594 end Linker_Options_Switches;
600 package body Mains is
602 type File_And_Loc is record
603 File_Name : File_Name_Type;
604 Location : Source_Ptr := No_Location;
607 package Names is new Table.Table
608 (Table_Component_Type => File_And_Loc,
609 Table_Index_Type => Integer,
610 Table_Low_Bound => 1,
612 Table_Increment => 100,
613 Table_Name => "Makeutl.Mains.Names");
614 -- The table that stores the mains
616 Current : Natural := 0;
617 -- The index of the last main retrieved from the table
623 procedure Add_Main (Name : String) is
626 Add_Str_To_Name_Buffer (Name);
627 Names.Increment_Last;
628 Names.Table (Names.Last) := (Name_Find, No_Location);
645 function Get_Location return Source_Ptr is
647 if Current in Names.First .. Names.Last then
648 return Names.Table (Current).Location;
658 function Next_Main return String is
660 if Current >= Names.Last then
663 Current := Current + 1;
664 return Get_Name_String (Names.Table (Current).File_Name);
668 ---------------------
669 -- Number_Of_Mains --
670 ---------------------
672 function Number_Of_Mains return Natural is
690 procedure Set_Location (Location : Source_Ptr) is
692 if Names.Last > 0 then
693 Names.Table (Names.Last).Location := Location;
701 procedure Update_Main (Name : String) is
703 if Current in Names.First .. Names.Last then
705 Add_Str_To_Name_Buffer (Name);
706 Names.Table (Current).File_Name := Name_Find;
715 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
717 Marks.Set (K => (File => Source_File, Index => Index), E => True);
720 -----------------------
721 -- Path_Or_File_Name --
722 -----------------------
724 function Path_Or_File_Name (Path : Path_Name_Type) return String is
725 Path_Name : constant String := Get_Name_String (Path);
727 if Debug.Debug_Flag_F then
728 return File_Name (Path_Name);
732 end Path_Or_File_Name;
734 ---------------------------
735 -- Test_If_Relative_Path --
736 ---------------------------
738 procedure Test_If_Relative_Path
739 (Switch : in out String_Access;
741 Including_L_Switch : Boolean := True;
742 Including_Non_Switch : Boolean := True;
743 Including_RTS : Boolean := False)
746 if Switch /= null then
748 Sw : String (1 .. Switch'Length);
756 and then (Sw (2) = 'A'
758 or else (Including_L_Switch and then Sw (2) = 'L'))
767 and then (Sw (2 .. 3) = "aL"
768 or else Sw (2 .. 3) = "aO"
769 or else Sw (2 .. 3) = "aI")
774 and then Sw'Length >= 7
775 and then Sw (2 .. 6) = "-RTS="
783 -- Because relative path arguments to --RTS= may be relative
784 -- to the search directory prefix, those relative path
785 -- arguments are converted only when they include directory
788 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
789 if Parent'Length = 0 then
791 ("relative search path switches ("""
793 & """) are not allowed");
795 elsif Including_RTS then
796 for J in Start .. Sw'Last loop
797 if Sw (J) = Directory_Separator then
800 (Sw (1 .. Start - 1) &
802 Directory_Separator &
803 Sw (Start .. Sw'Last));
811 (Sw (1 .. Start - 1) &
813 Directory_Separator &
814 Sw (Start .. Sw'Last));
818 elsif Including_Non_Switch then
819 if not Is_Absolute_Path (Sw) then
820 if Parent'Length = 0 then
822 ("relative paths (""" & Sw & """) are not allowed");
824 Switch := new String'(Parent & Directory_Separator & Sw);
830 end Test_If_Relative_Path;
836 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
842 Get_Name_String (ALI_File);
844 -- First, find the last dot
848 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
849 Finish := Finish - 1;
856 -- Now check that the dot is preceded by digits
859 Finish := Finish - 1;
861 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
865 -- If there are no digits, or if the digits are not preceded by the
866 -- character that precedes a unit index, this is not the ALI file of
867 -- a unit in a multi-unit source.
871 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
876 -- Build the index from the digit(s)
878 while Start <= Finish loop
879 Result := Result * 10 +
880 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
891 procedure Verbose_Msg
894 N2 : Name_Id := No_Name;
896 Prefix : String := " -> ";
897 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
900 if not Opt.Verbose_Mode
901 or else Minimum_Verbosity > Opt.Verbosity_Level
912 if N2 /= No_Name then
922 procedure Verbose_Msg
923 (N1 : File_Name_Type;
925 N2 : File_Name_Type := No_File;
927 Prefix : String := " -> ";
928 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
932 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);