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 -- Base_Name_Index_For --
162 -------------------------
164 function Base_Name_Index_For
167 Index_Separator
: Character) return File_Name_Type
169 Result
: File_Name_Type
;
173 Add_Str_To_Name_Buffer
(Base_Name
(Main
));
175 -- Remove the extension, if any, that is the last part of the base name
176 -- starting with a dot and following some characters.
178 for J
in reverse 2 .. Name_Len
loop
179 if Name_Buffer
(J
) = '.' then
185 -- Add the index info, if index is different from 0
187 if Main_Index
> 0 then
188 Add_Char_To_Name_Buffer
(Index_Separator
);
191 Img
: constant String := Main_Index
'Img;
193 Add_Str_To_Name_Buffer
(Img
(2 .. Img
'Last));
199 end Base_Name_Index_For
;
201 ------------------------------
202 -- Check_Source_Info_In_ALI --
203 ------------------------------
205 function Check_Source_Info_In_ALI
(The_ALI
: ALI_Id
) return Boolean is
209 -- Loop through units
211 for U
in ALIs
.Table
(The_ALI
).First_Unit
..
212 ALIs
.Table
(The_ALI
).Last_Unit
214 -- Check if the file name is one of the source of the unit
216 Get_Name_String
(Units
.Table
(U
).Uname
);
217 Name_Len
:= Name_Len
- 2;
218 Unit_Name
:= Name_Find
;
220 if File_Not_A_Source_Of
(Unit_Name
, Units
.Table
(U
).Sfile
) then
224 -- Loop to do same check for each of the withed units
226 for W
in Units
.Table
(U
).First_With
.. Units
.Table
(U
).Last_With
loop
228 WR
: ALI
.With_Record
renames Withs
.Table
(W
);
231 if WR
.Sfile
/= No_File
then
232 Get_Name_String
(WR
.Uname
);
233 Name_Len
:= Name_Len
- 2;
234 Unit_Name
:= Name_Find
;
236 if File_Not_A_Source_Of
(Unit_Name
, WR
.Sfile
) then
244 -- Loop to check subunits
246 for D
in ALIs
.Table
(The_ALI
).First_Sdep
..
247 ALIs
.Table
(The_ALI
).Last_Sdep
250 SD
: Sdep_Record
renames Sdep
.Table
(D
);
253 Unit_Name
:= SD
.Subunit_Name
;
255 if Unit_Name
/= No_Name
then
257 -- For separates, the file is no longer associated with the
258 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
259 -- so we need to check whether the source file still exists in
260 -- the source tree: it will if it matches the naming scheme
261 -- (and then will be for the same unit).
264 (In_Tree
=> Project_Tree
,
265 Project
=> No_Project
,
266 Base_Name
=> SD
.Sfile
) = No_Source
268 -- If this is not a runtime file or if, when gnatmake switch
269 -- -a is used, we are not able to find this subunit in the
270 -- source directories, then recompilation is needed.
272 if not Fname
.Is_Internal_File_Name
(SD
.Sfile
)
274 (Check_Readonly_Files
275 and then Full_Source_Name
(SD
.Sfile
) = No_File
)
279 ("While parsing ALI file, file "
280 & Get_Name_String
(SD
.Sfile
)
281 & " is indicated as containing subunit "
282 & Get_Name_String
(Unit_Name
)
283 & " but this does not match what was found while"
284 & " parsing the project. Will recompile");
295 end Check_Source_Info_In_ALI
;
301 function Create_Name
(Name
: String) return File_Name_Type
is
304 Add_Str_To_Name_Buffer
(Name
);
308 function Create_Name
(Name
: String) return Name_Id
is
311 Add_Str_To_Name_Buffer
(Name
);
315 function Create_Name
(Name
: String) return Path_Name_Type
is
318 Add_Str_To_Name_Buffer
(Name
);
322 ----------------------
323 -- Delete_All_Marks --
324 ----------------------
326 procedure Delete_All_Marks
is
329 end Delete_All_Marks
;
331 ----------------------------
332 -- Executable_Prefix_Path --
333 ----------------------------
335 function Executable_Prefix_Path
return String is
336 Exec_Name
: constant String := Command_Name
;
338 function Get_Install_Dir
(S
: String) return String;
339 -- S is the executable name preceded by the absolute or relative path,
340 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
341 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
342 -- directory, return "".
344 ---------------------
345 -- Get_Install_Dir --
346 ---------------------
348 function Get_Install_Dir
(S
: String) return String is
350 Path_Last
: Integer := 0;
353 for J
in reverse Exec
'Range loop
354 if Exec
(J
) = Directory_Separator
then
360 if Path_Last
>= Exec
'First + 2 then
361 To_Lower
(Exec
(Path_Last
- 2 .. Path_Last
));
364 if Path_Last
< Exec
'First + 2
365 or else Exec
(Path_Last
- 2 .. Path_Last
) /= "bin"
366 or else (Path_Last
- 3 >= Exec
'First
367 and then Exec
(Path_Last
- 3) /= Directory_Separator
)
372 return Normalize_Pathname
373 (Exec
(Exec
'First .. Path_Last
- 4),
374 Resolve_Links
=> Opt
.Follow_Links_For_Dirs
)
375 & Directory_Separator
;
378 -- Beginning of Executable_Prefix_Path
381 -- First determine if a path prefix was placed in front of the
384 for J
in reverse Exec_Name
'Range loop
385 if Exec_Name
(J
) = Directory_Separator
then
386 return Get_Install_Dir
(Exec_Name
);
390 -- If we get here, the user has typed the executable name with no
394 Path
: String_Access
:= Locate_Exec_On_Path
(Exec_Name
);
400 Dir
: constant String := Get_Install_Dir
(Path
.all);
407 end Executable_Prefix_Path
;
409 --------------------------
410 -- File_Not_A_Source_Of --
411 --------------------------
413 function File_Not_A_Source_Of
415 Sfile
: File_Name_Type
) return Boolean
417 Unit
: constant Unit_Index
:=
418 Units_Htable
.Get
(Project_Tree
.Units_HT
, Uname
);
420 At_Least_One_File
: Boolean := False;
423 if Unit
/= No_Unit_Index
then
424 for F
in Unit
.File_Names
'Range loop
425 if Unit
.File_Names
(F
) /= null then
426 At_Least_One_File
:= True;
427 if Unit
.File_Names
(F
).File
= Sfile
then
433 if not At_Least_One_File
then
435 -- The unit was probably created initially for a separate unit
436 -- (which are initially created as IMPL when both suffixes are the
437 -- same). Later on, Override_Kind changed the type of the file,
438 -- and the unit is no longer valid in fact.
443 Verbose_Msg
(Uname
, "sources do not include ", Name_Id
(Sfile
));
448 end File_Not_A_Source_Of
;
454 function Hash
(Key
: Mark_Key
) return Mark_Num
is
456 return Union_Id
(Key
.File
) mod Max_Mask_Num
;
463 procedure Inform
(N
: File_Name_Type
; Msg
: String) is
465 Inform
(Name_Id
(N
), Msg
);
468 procedure Inform
(N
: Name_Id
:= No_Name
; Msg
: String) is
470 Osint
.Write_Program_Name
;
478 Name
: constant String := Get_Name_String
(N
);
480 if Debug
.Debug_Flag_F
and then Is_Absolute_Path
(Name
) then
481 Write_Str
(File_Name
(Name
));
494 ----------------------------
495 -- Is_External_Assignment --
496 ----------------------------
498 function Is_External_Assignment
499 (Tree
: Prj
.Tree
.Project_Node_Tree_Ref
;
500 Argv
: String) return Boolean
502 Start
: Positive := 3;
503 Finish
: Natural := Argv
'Last;
505 pragma Assert
(Argv
'First = 1);
506 pragma Assert
(Argv
(1 .. 2) = "-X");
509 if Argv
'Last < 5 then
512 elsif Argv
(3) = '"' then
513 if Argv
(Argv
'Last) /= '"' or else Argv
'Last < 7 then
517 Finish
:= Argv
'Last - 1;
523 Declaration
=> Argv
(Start
.. Finish
));
524 end Is_External_Assignment
;
531 (Source_File
: File_Name_Type
;
532 Index
: Int
:= 0) return Boolean
535 return Marks
.Get
(K
=> (File
=> Source_File
, Index
=> Index
));
538 -----------------------------
539 -- Linker_Options_Switches --
540 -----------------------------
542 function Linker_Options_Switches
543 (Project
: Project_Id
;
544 In_Tree
: Project_Tree_Ref
) return String_List
546 procedure Recursive_Add
(Proj
: Project_Id
; Dummy
: in out Boolean);
547 -- The recursive routine used to add linker options
553 procedure Recursive_Add
(Proj
: Project_Id
; Dummy
: in out Boolean) is
554 pragma Unreferenced
(Dummy
);
556 Linker_Package
: Package_Id
;
557 Options
: Variable_Value
;
562 (Name
=> Name_Linker
,
563 In_Packages
=> Proj
.Decl
.Packages
,
570 Attribute_Or_Array_Name
=> Name_Linker_Options
,
571 In_Package
=> Linker_Package
,
574 -- If attribute is present, add the project with
575 -- the attribute to table Linker_Opts.
577 if Options
/= Nil_Variable_Value
then
578 Linker_Opts
.Increment_Last
;
579 Linker_Opts
.Table
(Linker_Opts
.Last
) :=
580 (Project
=> Proj
, Options
=> Options
.Values
);
584 procedure For_All_Projects
is
585 new For_Every_Project_Imported
(Boolean, Recursive_Add
);
587 Dummy
: Boolean := False;
589 -- Start of processing for Linker_Options_Switches
594 For_All_Projects
(Project
, Dummy
, Imported_First
=> True);
596 Last_Linker_Option
:= 0;
598 for Index
in reverse 1 .. Linker_Opts
.Last
loop
600 Options
: String_List_Id
;
601 Proj
: constant Project_Id
:=
602 Linker_Opts
.Table
(Index
).Project
;
604 Dir_Path
: constant String :=
605 Get_Name_String
(Proj
.Directory
.Name
);
608 Options
:= Linker_Opts
.Table
(Index
).Options
;
609 while Options
/= Nil_String
loop
610 Option
:= In_Tree
.String_Elements
.Table
(Options
).Value
;
611 Get_Name_String
(Option
);
613 -- Do not consider empty linker options
615 if Name_Len
/= 0 then
616 Add_Linker_Option
(Name_Buffer
(1 .. Name_Len
));
618 -- Object files and -L switches specified with relative
619 -- paths must be converted to absolute paths.
621 Test_If_Relative_Path
622 (Switch
=> Linker_Options_Buffer
(Last_Linker_Option
),
624 Including_L_Switch
=> True);
627 Options
:= In_Tree
.String_Elements
.Table
(Options
).Next
;
632 return Linker_Options_Buffer
(1 .. Last_Linker_Option
);
633 end Linker_Options_Switches
;
639 package body Mains
is
641 type File_And_Loc
is record
642 File_Name
: File_Name_Type
;
644 Location
: Source_Ptr
:= No_Location
;
647 package Names
is new Table
.Table
648 (Table_Component_Type
=> File_And_Loc
,
649 Table_Index_Type
=> Integer,
650 Table_Low_Bound
=> 1,
652 Table_Increment
=> 100,
653 Table_Name
=> "Makeutl.Mains.Names");
654 -- The table that stores the mains
656 Current
: Natural := 0;
657 -- The index of the last main retrieved from the table
663 procedure Add_Main
(Name
: String) is
666 Add_Str_To_Name_Buffer
(Name
);
667 Names
.Increment_Last
;
668 Names
.Table
(Names
.Last
) := (Name_Find
, 0, No_Location
);
685 function Get_Index
return Int
is
687 if Current
in Names
.First
.. Names
.Last
then
688 return Names
.Table
(Current
).Index
;
698 function Get_Location
return Source_Ptr
is
700 if Current
in Names
.First
.. Names
.Last
then
701 return Names
.Table
(Current
).Location
;
711 function Next_Main
return String is
713 if Current
>= Names
.Last
then
716 Current
:= Current
+ 1;
717 return Get_Name_String
(Names
.Table
(Current
).File_Name
);
721 ---------------------
722 -- Number_Of_Mains --
723 ---------------------
725 function Number_Of_Mains
return Natural is
743 procedure Set_Index
(Index
: Int
) is
745 if Names
.Last
> 0 then
746 Names
.Table
(Names
.Last
).Index
:= Index
;
754 procedure Set_Location
(Location
: Source_Ptr
) is
756 if Names
.Last
> 0 then
757 Names
.Table
(Names
.Last
).Location
:= Location
;
765 procedure Update_Main
(Name
: String) is
767 if Current
in Names
.First
.. Names
.Last
then
769 Add_Str_To_Name_Buffer
(Name
);
770 Names
.Table
(Current
).File_Name
:= Name_Find
;
779 procedure Mark
(Source_File
: File_Name_Type
; Index
: Int
:= 0) is
781 Marks
.Set
(K
=> (File
=> Source_File
, Index
=> Index
), E
=> True);
784 -----------------------
785 -- Path_Or_File_Name --
786 -----------------------
788 function Path_Or_File_Name
(Path
: Path_Name_Type
) return String is
789 Path_Name
: constant String := Get_Name_String
(Path
);
791 if Debug
.Debug_Flag_F
then
792 return File_Name
(Path_Name
);
796 end Path_Or_File_Name
;
798 ---------------------------
799 -- Test_If_Relative_Path --
800 ---------------------------
802 procedure Test_If_Relative_Path
803 (Switch
: in out String_Access
;
805 Including_L_Switch
: Boolean := True;
806 Including_Non_Switch
: Boolean := True;
807 Including_RTS
: Boolean := False)
810 if Switch
/= null then
812 Sw
: String (1 .. Switch
'Length);
820 and then (Sw
(2) = 'A'
822 or else (Including_L_Switch
and then Sw
(2) = 'L'))
831 and then (Sw
(2 .. 3) = "aL"
832 or else Sw
(2 .. 3) = "aO"
833 or else Sw
(2 .. 3) = "aI")
838 and then Sw
'Length >= 7
839 and then Sw
(2 .. 6) = "-RTS="
847 -- Because relative path arguments to --RTS= may be relative
848 -- to the search directory prefix, those relative path
849 -- arguments are converted only when they include directory
852 if not Is_Absolute_Path
(Sw
(Start
.. Sw
'Last)) then
853 if Parent
'Length = 0 then
855 ("relative search path switches ("""
857 & """) are not allowed");
859 elsif Including_RTS
then
860 for J
in Start
.. Sw
'Last loop
861 if Sw
(J
) = Directory_Separator
then
864 (Sw (1 .. Start - 1) &
866 Directory_Separator &
867 Sw (Start .. Sw'Last));
875 (Sw
(1 .. Start
- 1) &
877 Directory_Separator
&
878 Sw
(Start
.. Sw
'Last));
882 elsif Including_Non_Switch
then
883 if not Is_Absolute_Path
(Sw
) then
884 if Parent
'Length = 0 then
886 ("relative paths (""" & Sw
& """) are not allowed");
888 Switch
:= new String'(Parent & Directory_Separator & Sw);
894 end Test_If_Relative_Path;
900 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
906 Get_Name_String (ALI_File);
908 -- First, find the last dot
912 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
913 Finish := Finish - 1;
920 -- Now check that the dot is preceded by digits
923 Finish := Finish - 1;
925 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
929 -- If there are no digits, or if the digits are not preceded by the
930 -- character that precedes a unit index, this is not the ALI file of
931 -- a unit in a multi-unit source.
935 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
940 -- Build the index from the digit(s)
942 while Start <= Finish loop
943 Result := Result * 10 +
944 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
955 procedure Verbose_Msg
958 N2 : Name_Id := No_Name;
960 Prefix : String := " -> ";
961 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
964 if not Opt.Verbose_Mode
965 or else Minimum_Verbosity > Opt.Verbosity_Level
976 if N2 /= No_Name then
986 procedure Verbose_Msg
987 (N1 : File_Name_Type;
989 N2 : File_Name_Type := No_File;
991 Prefix : String := " -> ";
992 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
996 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);