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
451 (Tree : Prj.Tree.Project_Node_Tree_Ref;
452 Argv : String) return Boolean
454 Start : Positive := 3;
455 Finish : Natural := Argv'Last;
457 pragma Assert (Argv'First = 1);
458 pragma Assert (Argv (1 .. 2) = "-X");
461 if Argv'Last < 5 then
464 elsif Argv (3) = '"' then
465 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
469 Finish := Argv'Last - 1;
475 Declaration => Argv (Start .. Finish));
476 end Is_External_Assignment;
483 (Source_File : File_Name_Type;
484 Index : Int := 0) return Boolean
487 return Marks.Get (K => (File => Source_File, Index => Index));
490 -----------------------------
491 -- Linker_Options_Switches --
492 -----------------------------
494 function Linker_Options_Switches
495 (Project : Project_Id;
496 In_Tree : Project_Tree_Ref) return String_List
498 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
499 -- The recursive routine used to add linker options
505 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
506 pragma Unreferenced (Dummy);
508 Linker_Package : Package_Id;
509 Options : Variable_Value;
514 (Name => Name_Linker,
515 In_Packages => Proj.Decl.Packages,
522 Attribute_Or_Array_Name => Name_Linker_Options,
523 In_Package => Linker_Package,
526 -- If attribute is present, add the project with
527 -- the attribute to table Linker_Opts.
529 if Options /= Nil_Variable_Value then
530 Linker_Opts.Increment_Last;
531 Linker_Opts.Table (Linker_Opts.Last) :=
532 (Project => Proj, Options => Options.Values);
536 procedure For_All_Projects is
537 new For_Every_Project_Imported (Boolean, Recursive_Add);
539 Dummy : Boolean := False;
541 -- Start of processing for Linker_Options_Switches
546 For_All_Projects (Project, Dummy, Imported_First => True);
548 Last_Linker_Option := 0;
550 for Index in reverse 1 .. Linker_Opts.Last loop
552 Options : String_List_Id;
553 Proj : constant Project_Id :=
554 Linker_Opts.Table (Index).Project;
556 Dir_Path : constant String :=
557 Get_Name_String (Proj.Directory.Name);
560 Options := Linker_Opts.Table (Index).Options;
561 while Options /= Nil_String loop
562 Option := In_Tree.String_Elements.Table (Options).Value;
563 Get_Name_String (Option);
565 -- Do not consider empty linker options
567 if Name_Len /= 0 then
568 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
570 -- Object files and -L switches specified with relative
571 -- paths must be converted to absolute paths.
573 Test_If_Relative_Path
574 (Switch => Linker_Options_Buffer (Last_Linker_Option),
576 Including_L_Switch => True);
579 Options := In_Tree.String_Elements.Table (Options).Next;
584 return Linker_Options_Buffer (1 .. Last_Linker_Option);
585 end Linker_Options_Switches;
591 package body Mains is
593 type File_And_Loc is record
594 File_Name : File_Name_Type;
595 Location : Source_Ptr := No_Location;
598 package Names is new Table.Table
599 (Table_Component_Type => File_And_Loc,
600 Table_Index_Type => Integer,
601 Table_Low_Bound => 1,
603 Table_Increment => 100,
604 Table_Name => "Makeutl.Mains.Names");
605 -- The table that stores the mains
607 Current : Natural := 0;
608 -- The index of the last main retrieved from the table
614 procedure Add_Main (Name : String) is
617 Add_Str_To_Name_Buffer (Name);
618 Names.Increment_Last;
619 Names.Table (Names.Last) := (Name_Find, No_Location);
636 function Get_Location return Source_Ptr is
638 if Current in Names.First .. Names.Last then
639 return Names.Table (Current).Location;
649 function Next_Main return String is
651 if Current >= Names.Last then
654 Current := Current + 1;
655 return Get_Name_String (Names.Table (Current).File_Name);
659 ---------------------
660 -- Number_Of_Mains --
661 ---------------------
663 function Number_Of_Mains return Natural is
681 procedure Set_Location (Location : Source_Ptr) is
683 if Names.Last > 0 then
684 Names.Table (Names.Last).Location := Location;
692 procedure Update_Main (Name : String) is
694 if Current in Names.First .. Names.Last then
696 Add_Str_To_Name_Buffer (Name);
697 Names.Table (Current).File_Name := Name_Find;
706 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
708 Marks.Set (K => (File => Source_File, Index => Index), E => True);
711 -----------------------
712 -- Path_Or_File_Name --
713 -----------------------
715 function Path_Or_File_Name (Path : Path_Name_Type) return String is
716 Path_Name : constant String := Get_Name_String (Path);
718 if Debug.Debug_Flag_F then
719 return File_Name (Path_Name);
723 end Path_Or_File_Name;
725 ---------------------------
726 -- Test_If_Relative_Path --
727 ---------------------------
729 procedure Test_If_Relative_Path
730 (Switch : in out String_Access;
732 Including_L_Switch : Boolean := True;
733 Including_Non_Switch : Boolean := True;
734 Including_RTS : Boolean := False)
737 if Switch /= null then
739 Sw : String (1 .. Switch'Length);
747 and then (Sw (2) = 'A'
749 or else (Including_L_Switch and then Sw (2) = 'L'))
758 and then (Sw (2 .. 3) = "aL"
759 or else Sw (2 .. 3) = "aO"
760 or else Sw (2 .. 3) = "aI")
765 and then Sw'Length >= 7
766 and then Sw (2 .. 6) = "-RTS="
774 -- Because relative path arguments to --RTS= may be relative
775 -- to the search directory prefix, those relative path
776 -- arguments are converted only when they include directory
779 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
780 if Parent'Length = 0 then
782 ("relative search path switches ("""
784 & """) are not allowed");
786 elsif Including_RTS then
787 for J in Start .. Sw'Last loop
788 if Sw (J) = Directory_Separator then
791 (Sw (1 .. Start - 1) &
793 Directory_Separator &
794 Sw (Start .. Sw'Last));
802 (Sw (1 .. Start - 1) &
804 Directory_Separator &
805 Sw (Start .. Sw'Last));
809 elsif Including_Non_Switch then
810 if not Is_Absolute_Path (Sw) then
811 if Parent'Length = 0 then
813 ("relative paths (""" & Sw & """) are not allowed");
815 Switch := new String'(Parent & Directory_Separator & Sw);
821 end Test_If_Relative_Path;
827 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
833 Get_Name_String (ALI_File);
835 -- First, find the last dot
839 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
840 Finish := Finish - 1;
847 -- Now check that the dot is preceded by digits
850 Finish := Finish - 1;
852 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
856 -- If there are no digits, or if the digits are not preceded by the
857 -- character that precedes a unit index, this is not the ALI file of
858 -- a unit in a multi-unit source.
862 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
867 -- Build the index from the digit(s)
869 while Start <= Finish loop
870 Result := Result * 10 +
871 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
882 procedure Verbose_Msg
885 N2 : Name_Id := No_Name;
887 Prefix : String := " -> ";
888 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
891 if not Opt.Verbose_Mode
892 or else Minimum_Verbosity > Opt.Verbosity_Level
903 if N2 /= No_Name then
913 procedure Verbose_Msg
914 (N1 : File_Name_Type;
916 N2 : File_Name_Type := No_File;
918 Prefix : String := " -> ";
919 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
923 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);