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
;
38 with Ada
.Command_Line
; use Ada
.Command_Line
;
40 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
42 with GNAT
.Case_Util
; use GNAT
.Case_Util
;
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
(The_ALI
: ALI_Id
) return Boolean is
210 -- Loop through units
212 for U
in ALIs
.Table
(The_ALI
).First_Unit
..
213 ALIs
.Table
(The_ALI
).Last_Unit
215 -- Check if the file name is one of the source of the unit
217 Get_Name_String
(Units
.Table
(U
).Uname
);
218 Name_Len
:= Name_Len
- 2;
219 Unit_Name
:= Name_Find
;
221 if File_Not_A_Source_Of
(Unit_Name
, Units
.Table
(U
).Sfile
) then
225 -- Loop to do same check for each of the withed units
227 for W
in Units
.Table
(U
).First_With
.. Units
.Table
(U
).Last_With
loop
229 WR
: ALI
.With_Record
renames Withs
.Table
(W
);
232 if WR
.Sfile
/= No_File
then
233 Get_Name_String
(WR
.Uname
);
234 Name_Len
:= Name_Len
- 2;
235 Unit_Name
:= Name_Find
;
237 if File_Not_A_Source_Of
(Unit_Name
, WR
.Sfile
) then
245 -- Loop to check subunits
247 for D
in ALIs
.Table
(The_ALI
).First_Sdep
..
248 ALIs
.Table
(The_ALI
).Last_Sdep
251 SD
: Sdep_Record
renames Sdep
.Table
(D
);
254 Unit_Name
:= SD
.Subunit_Name
;
256 if Unit_Name
/= No_Name
then
258 -- For separates, the file is no longer associated with the
259 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
260 -- so we need to check whether the source file still exists in
261 -- the source tree: it will if it matches the naming scheme
262 -- (and then will be for the same unit).
265 (In_Tree
=> Project_Tree
,
266 Project
=> No_Project
,
267 Base_Name
=> SD
.Sfile
) = No_Source
269 -- If this is not a runtime file or if, when gnatmake switch
270 -- -a is used, we are not able to find this subunit in the
271 -- source directories, then recompilation is needed.
273 if not Fname
.Is_Internal_File_Name
(SD
.Sfile
)
275 (Check_Readonly_Files
276 and then Full_Source_Name
(SD
.Sfile
) = No_File
)
280 ("While parsing ALI file, file "
281 & Get_Name_String
(SD
.Sfile
)
282 & " is indicated as containing subunit "
283 & Get_Name_String
(Unit_Name
)
284 & " but this does not match what was found while"
285 & " parsing the project. Will recompile");
296 end Check_Source_Info_In_ALI
;
302 function Create_Name
(Name
: String) return File_Name_Type
is
305 Add_Str_To_Name_Buffer
(Name
);
309 function Create_Name
(Name
: String) return Name_Id
is
312 Add_Str_To_Name_Buffer
(Name
);
316 function Create_Name
(Name
: String) return Path_Name_Type
is
319 Add_Str_To_Name_Buffer
(Name
);
323 ----------------------
324 -- Delete_All_Marks --
325 ----------------------
327 procedure Delete_All_Marks
is
330 end Delete_All_Marks
;
332 ----------------------------
333 -- Executable_Prefix_Path --
334 ----------------------------
336 function Executable_Prefix_Path
return String is
337 Exec_Name
: constant String := Command_Name
;
339 function Get_Install_Dir
(S
: String) return String;
340 -- S is the executable name preceded by the absolute or relative path,
341 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
342 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
343 -- directory, return "".
345 ---------------------
346 -- Get_Install_Dir --
347 ---------------------
349 function Get_Install_Dir
(S
: String) return String is
351 Path_Last
: Integer := 0;
354 for J
in reverse Exec
'Range loop
355 if Exec
(J
) = Directory_Separator
then
361 if Path_Last
>= Exec
'First + 2 then
362 To_Lower
(Exec
(Path_Last
- 2 .. Path_Last
));
365 if Path_Last
< Exec
'First + 2
366 or else Exec
(Path_Last
- 2 .. Path_Last
) /= "bin"
367 or else (Path_Last
- 3 >= Exec
'First
368 and then Exec
(Path_Last
- 3) /= Directory_Separator
)
373 return Normalize_Pathname
374 (Exec
(Exec
'First .. Path_Last
- 4),
375 Resolve_Links
=> Opt
.Follow_Links_For_Dirs
)
376 & Directory_Separator
;
379 -- Beginning of Executable_Prefix_Path
382 -- For VMS, the path returned is always /gnu/
384 if Hostparm
.OpenVMS
then
388 -- First determine if a path prefix was placed in front of the
391 for J
in reverse Exec_Name
'Range loop
392 if Exec_Name
(J
) = Directory_Separator
then
393 return Get_Install_Dir
(Exec_Name
);
397 -- If we get here, the user has typed the executable name with no
401 Path
: String_Access
:= Locate_Exec_On_Path
(Exec_Name
);
407 Dir
: constant String := Get_Install_Dir
(Path
.all);
414 end Executable_Prefix_Path
;
416 --------------------------
417 -- File_Not_A_Source_Of --
418 --------------------------
420 function File_Not_A_Source_Of
422 Sfile
: File_Name_Type
) return Boolean
424 Unit
: constant Unit_Index
:=
425 Units_Htable
.Get
(Project_Tree
.Units_HT
, Uname
);
427 At_Least_One_File
: Boolean := False;
430 if Unit
/= No_Unit_Index
then
431 for F
in Unit
.File_Names
'Range loop
432 if Unit
.File_Names
(F
) /= null then
433 At_Least_One_File
:= True;
434 if Unit
.File_Names
(F
).File
= Sfile
then
440 if not At_Least_One_File
then
442 -- The unit was probably created initially for a separate unit
443 -- (which are initially created as IMPL when both suffixes are the
444 -- same). Later on, Override_Kind changed the type of the file,
445 -- and the unit is no longer valid in fact.
450 Verbose_Msg
(Uname
, "sources do not include ", Name_Id
(Sfile
));
455 end File_Not_A_Source_Of
;
461 function Hash
(Key
: Mark_Key
) return Mark_Num
is
463 return Union_Id
(Key
.File
) mod Max_Mask_Num
;
470 procedure Inform
(N
: File_Name_Type
; Msg
: String) is
472 Inform
(Name_Id
(N
), Msg
);
475 procedure Inform
(N
: Name_Id
:= No_Name
; Msg
: String) is
477 Osint
.Write_Program_Name
;
485 Name
: constant String := Get_Name_String
(N
);
487 if Debug
.Debug_Flag_F
and then Is_Absolute_Path
(Name
) then
488 Write_Str
(File_Name
(Name
));
501 ----------------------------
502 -- Is_External_Assignment --
503 ----------------------------
505 function Is_External_Assignment
506 (Tree
: Prj
.Tree
.Project_Node_Tree_Ref
;
507 Argv
: String) return Boolean
509 Start
: Positive := 3;
510 Finish
: Natural := Argv
'Last;
512 pragma Assert
(Argv
'First = 1);
513 pragma Assert
(Argv
(1 .. 2) = "-X");
516 if Argv
'Last < 5 then
519 elsif Argv
(3) = '"' then
520 if Argv
(Argv
'Last) /= '"' or else Argv
'Last < 7 then
524 Finish
:= Argv
'Last - 1;
530 Declaration
=> Argv
(Start
.. Finish
));
531 end Is_External_Assignment
;
538 (Source_File
: File_Name_Type
;
539 Index
: Int
:= 0) return Boolean
542 return Marks
.Get
(K
=> (File
=> Source_File
, Index
=> Index
));
545 -----------------------------
546 -- Linker_Options_Switches --
547 -----------------------------
549 function Linker_Options_Switches
550 (Project
: Project_Id
;
551 In_Tree
: Project_Tree_Ref
) return String_List
553 procedure Recursive_Add
(Proj
: Project_Id
; Dummy
: in out Boolean);
554 -- The recursive routine used to add linker options
560 procedure Recursive_Add
(Proj
: Project_Id
; Dummy
: in out Boolean) is
561 pragma Unreferenced
(Dummy
);
563 Linker_Package
: Package_Id
;
564 Options
: Variable_Value
;
569 (Name
=> Name_Linker
,
570 In_Packages
=> Proj
.Decl
.Packages
,
577 Attribute_Or_Array_Name
=> Name_Linker_Options
,
578 In_Package
=> Linker_Package
,
581 -- If attribute is present, add the project with
582 -- the attribute to table Linker_Opts.
584 if Options
/= Nil_Variable_Value
then
585 Linker_Opts
.Increment_Last
;
586 Linker_Opts
.Table
(Linker_Opts
.Last
) :=
587 (Project
=> Proj
, Options
=> Options
.Values
);
591 procedure For_All_Projects
is
592 new For_Every_Project_Imported
(Boolean, Recursive_Add
);
594 Dummy
: Boolean := False;
596 -- Start of processing for Linker_Options_Switches
601 For_All_Projects
(Project
, Dummy
, Imported_First
=> True);
603 Last_Linker_Option
:= 0;
605 for Index
in reverse 1 .. Linker_Opts
.Last
loop
607 Options
: String_List_Id
;
608 Proj
: constant Project_Id
:=
609 Linker_Opts
.Table
(Index
).Project
;
611 Dir_Path
: constant String :=
612 Get_Name_String
(Proj
.Directory
.Name
);
615 Options
:= Linker_Opts
.Table
(Index
).Options
;
616 while Options
/= Nil_String
loop
617 Option
:= In_Tree
.String_Elements
.Table
(Options
).Value
;
618 Get_Name_String
(Option
);
620 -- Do not consider empty linker options
622 if Name_Len
/= 0 then
623 Add_Linker_Option
(Name_Buffer
(1 .. Name_Len
));
625 -- Object files and -L switches specified with relative
626 -- paths must be converted to absolute paths.
628 Test_If_Relative_Path
629 (Switch
=> Linker_Options_Buffer
(Last_Linker_Option
),
631 Including_L_Switch
=> True);
634 Options
:= In_Tree
.String_Elements
.Table
(Options
).Next
;
639 return Linker_Options_Buffer
(1 .. Last_Linker_Option
);
640 end Linker_Options_Switches
;
646 package body Mains
is
648 type File_And_Loc
is record
649 File_Name
: File_Name_Type
;
651 Location
: Source_Ptr
:= No_Location
;
654 package Names
is new Table
.Table
655 (Table_Component_Type
=> File_And_Loc
,
656 Table_Index_Type
=> Integer,
657 Table_Low_Bound
=> 1,
659 Table_Increment
=> 100,
660 Table_Name
=> "Makeutl.Mains.Names");
661 -- The table that stores the mains
663 Current
: Natural := 0;
664 -- The index of the last main retrieved from the table
670 procedure Add_Main
(Name
: String) is
673 Add_Str_To_Name_Buffer
(Name
);
674 Names
.Increment_Last
;
675 Names
.Table
(Names
.Last
) := (Name_Find
, 0, No_Location
);
692 function Get_Index
return Int
is
694 if Current
in Names
.First
.. Names
.Last
then
695 return Names
.Table
(Current
).Index
;
705 function Get_Location
return Source_Ptr
is
707 if Current
in Names
.First
.. Names
.Last
then
708 return Names
.Table
(Current
).Location
;
718 function Next_Main
return String is
720 if Current
>= Names
.Last
then
723 Current
:= Current
+ 1;
724 return Get_Name_String
(Names
.Table
(Current
).File_Name
);
728 ---------------------
729 -- Number_Of_Mains --
730 ---------------------
732 function Number_Of_Mains
return Natural is
750 procedure Set_Index
(Index
: Int
) is
752 if Names
.Last
> 0 then
753 Names
.Table
(Names
.Last
).Index
:= Index
;
761 procedure Set_Location
(Location
: Source_Ptr
) is
763 if Names
.Last
> 0 then
764 Names
.Table
(Names
.Last
).Location
:= Location
;
772 procedure Update_Main
(Name
: String) is
774 if Current
in Names
.First
.. Names
.Last
then
776 Add_Str_To_Name_Buffer
(Name
);
777 Names
.Table
(Current
).File_Name
:= Name_Find
;
786 procedure Mark
(Source_File
: File_Name_Type
; Index
: Int
:= 0) is
788 Marks
.Set
(K
=> (File
=> Source_File
, Index
=> Index
), E
=> True);
791 -----------------------
792 -- Path_Or_File_Name --
793 -----------------------
795 function Path_Or_File_Name
(Path
: Path_Name_Type
) return String is
796 Path_Name
: constant String := Get_Name_String
(Path
);
798 if Debug
.Debug_Flag_F
then
799 return File_Name
(Path_Name
);
803 end Path_Or_File_Name
;
805 ---------------------------
806 -- Test_If_Relative_Path --
807 ---------------------------
809 procedure Test_If_Relative_Path
810 (Switch
: in out String_Access
;
812 Including_L_Switch
: Boolean := True;
813 Including_Non_Switch
: Boolean := True;
814 Including_RTS
: Boolean := False)
817 if Switch
/= null then
819 Sw
: String (1 .. Switch
'Length);
827 and then (Sw
(2) = 'A'
829 or else (Including_L_Switch
and then Sw
(2) = 'L'))
838 and then (Sw
(2 .. 3) = "aL"
839 or else Sw
(2 .. 3) = "aO"
840 or else Sw
(2 .. 3) = "aI")
845 and then Sw
'Length >= 7
846 and then Sw
(2 .. 6) = "-RTS="
854 -- Because relative path arguments to --RTS= may be relative
855 -- to the search directory prefix, those relative path
856 -- arguments are converted only when they include directory
859 if not Is_Absolute_Path
(Sw
(Start
.. Sw
'Last)) then
860 if Parent
'Length = 0 then
862 ("relative search path switches ("""
864 & """) are not allowed");
866 elsif Including_RTS
then
867 for J
in Start
.. Sw
'Last loop
868 if Sw
(J
) = Directory_Separator
then
871 (Sw (1 .. Start - 1) &
873 Directory_Separator &
874 Sw (Start .. Sw'Last));
882 (Sw
(1 .. Start
- 1) &
884 Directory_Separator
&
885 Sw
(Start
.. Sw
'Last));
889 elsif Including_Non_Switch
then
890 if not Is_Absolute_Path
(Sw
) then
891 if Parent
'Length = 0 then
893 ("relative paths (""" & Sw
& """) are not allowed");
895 Switch
:= new String'(Parent & Directory_Separator & Sw);
901 end Test_If_Relative_Path;
907 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
913 Get_Name_String (ALI_File);
915 -- First, find the last dot
919 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
920 Finish := Finish - 1;
927 -- Now check that the dot is preceded by digits
930 Finish := Finish - 1;
932 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
936 -- If there are no digits, or if the digits are not preceded by the
937 -- character that precedes a unit index, this is not the ALI file of
938 -- a unit in a multi-unit source.
942 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
947 -- Build the index from the digit(s)
949 while Start <= Finish loop
950 Result := Result * 10 +
951 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
962 procedure Verbose_Msg
965 N2 : Name_Id := No_Name;
967 Prefix : String := " -> ";
968 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
971 if not Opt.Verbose_Mode
972 or else Minimum_Verbosity > Opt.Verbosity_Level
983 if N2 /= No_Name then
993 procedure Verbose_Msg
994 (N1 : File_Name_Type;
996 N2 : File_Name_Type := No_File;
998 Prefix : String := " -> ";
999 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1003 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);