1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Namet; use Namet;
28 with Osint; use Osint;
32 with Snames; use Snames;
34 with Types; use Types;
38 package body Makeutl is
40 type Mark_Key is record
41 File : File_Name_Type;
44 -- Identify either a mono-unit source (when Index = 0) or a specific unit
45 -- in a multi-unit source.
47 -- There follow many global undocumented declarations, comments needed ???
49 Max_Mask_Num : constant := 2048;
51 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
53 function Hash (Key : Mark_Key) return Mark_Num;
55 package Marks is new System.HTable.Simple_HTable
56 (Header_Num => Mark_Num,
62 -- A hash table to keep tracks of the marked units.
64 type Linker_Options_Data is record
66 Options : String_List_Id;
69 Linker_Option_Initial_Count : constant := 20;
71 Linker_Options_Buffer : String_List_Access :=
72 new String_List (1 .. Linker_Option_Initial_Count);
74 Last_Linker_Option : Natural := 0;
76 package Linker_Opts is new Table.Table (
77 Table_Component_Type => Linker_Options_Data,
78 Table_Index_Type => Integer,
81 Table_Increment => 100,
82 Table_Name => "Make.Linker_Opts");
84 procedure Add_Linker_Option (Option : String);
86 -----------------------
87 -- Add_Linker_Option --
88 -----------------------
90 procedure Add_Linker_Option (Option : String) is
92 if Option'Length > 0 then
93 if Last_Linker_Option = Linker_Options_Buffer'Last then
95 New_Buffer : constant String_List_Access :=
97 (1 .. Linker_Options_Buffer'Last +
98 Linker_Option_Initial_Count);
100 New_Buffer (Linker_Options_Buffer'Range) :=
101 Linker_Options_Buffer.all;
102 Linker_Options_Buffer.all := (others => null);
103 Free (Linker_Options_Buffer);
104 Linker_Options_Buffer := New_Buffer;
108 Last_Linker_Option := Last_Linker_Option + 1;
109 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
111 end Add_Linker_Option;
113 ----------------------
114 -- Delete_All_Marks --
115 ----------------------
117 procedure Delete_All_Marks is
120 end Delete_All_Marks;
126 function Hash (Key : Mark_Key) return Mark_Num is
128 return Union_Id (Key.File) mod Max_Mask_Num;
131 ----------------------------
132 -- Is_External_Assignment --
133 ----------------------------
135 function Is_External_Assignment (Argv : String) return Boolean is
136 Start : Positive := 3;
137 Finish : Natural := Argv'Last;
141 if Argv'Last < 5 then
144 elsif Argv (3) = '"' then
145 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
149 Finish := Argv'Last - 1;
155 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
156 Equal_Pos := Equal_Pos + 1;
160 or else Equal_Pos >= Finish
165 (External_Name => Argv (Start .. Equal_Pos - 1),
166 Value => Argv (Equal_Pos + 1 .. Finish));
169 end Is_External_Assignment;
176 (Source_File : File_Name_Type;
177 Index : Int := 0) return Boolean
180 return Marks.Get (K => (File => Source_File, Index => Index));
183 -----------------------------
184 -- Linker_Options_Switches --
185 -----------------------------
187 function Linker_Options_Switches
188 (Project : Project_Id) return String_List
190 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
191 -- The recursive routine used to add linker options
193 ----------------------------------
194 -- Recursive_Add_Linker_Options --
195 ----------------------------------
197 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
199 Linker_Package : Package_Id;
200 Options : Variable_Value;
201 Imported : Project_List;
204 if Proj /= No_Project then
205 Data := Projects.Table (Proj);
207 if not Data.Seen then
208 Projects.Table (Proj).Seen := True;
209 Imported := Data.Imported_Projects;
211 while Imported /= Empty_Project_List loop
212 Recursive_Add_Linker_Options
213 (Project_Lists.Table (Imported).Project);
214 Imported := Project_Lists.Table (Imported).Next;
217 if Proj /= Project then
220 (Name => Name_Linker,
221 In_Packages => Data.Decl.Packages);
226 Attribute_Or_Array_Name => Name_Linker_Options,
227 In_Package => Linker_Package);
229 -- If attribute is present, add the project with
230 -- the attribute to table Linker_Opts.
232 if Options /= Nil_Variable_Value then
233 Linker_Opts.Increment_Last;
234 Linker_Opts.Table (Linker_Opts.Last) :=
235 (Project => Proj, Options => Options.Values);
240 end Recursive_Add_Linker_Options;
242 -- Start of processing for Linker_Options_Switches
247 for Index in 1 .. Projects.Last loop
248 Projects.Table (Index).Seen := False;
251 Recursive_Add_Linker_Options (Project);
253 Last_Linker_Option := 0;
255 for Index in reverse 1 .. Linker_Opts.Last loop
257 Options : String_List_Id := Linker_Opts.Table (Index).Options;
258 Proj : constant Project_Id :=
259 Linker_Opts.Table (Index).Project;
263 -- If Dir_Path has not been computed for this project, do it now
265 if Projects.Table (Proj).Dir_Path = null then
266 Projects.Table (Proj).Dir_Path :=
268 (Get_Name_String (Projects.Table (Proj). Directory));
271 while Options /= Nil_String loop
272 Option := String_Elements.Table (Options).Value;
273 Options := String_Elements.Table (Options).Next;
274 Add_Linker_Option (Get_Name_String (Option));
276 -- Object files and -L switches specified with
277 -- relative paths and must be converted to
280 Test_If_Relative_Path
282 Linker_Options_Buffer (Last_Linker_Option),
283 Parent => Projects.Table (Proj).Dir_Path,
284 Including_L_Switch => True);
289 return Linker_Options_Buffer (1 .. Last_Linker_Option);
290 end Linker_Options_Switches;
296 package body Mains is
298 package Names is new Table.Table
299 (Table_Component_Type => File_Name_Type,
300 Table_Index_Type => Integer,
301 Table_Low_Bound => 1,
303 Table_Increment => 100,
304 Table_Name => "Makeutl.Mains.Names");
305 -- The table that stores the mains
307 Current : Natural := 0;
308 -- The index of the last main retrieved from the table
314 procedure Add_Main (Name : String) is
317 Add_Str_To_Name_Buffer (Name);
318 Names.Increment_Last;
319 Names.Table (Names.Last) := Name_Find;
336 function Next_Main return String is
338 if Current >= Names.Last then
342 Current := Current + 1;
343 return Get_Name_String (Names.Table (Current));
347 ---------------------
348 -- Number_Of_Mains --
349 ---------------------
351 function Number_Of_Mains return Natural is
371 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
373 Marks.Set (K => (File => Source_File, Index => Index), E => True);
376 ---------------------------
377 -- Test_If_Relative_Path --
378 ---------------------------
380 procedure Test_If_Relative_Path
381 (Switch : in out String_Access;
382 Parent : String_Access;
383 Including_L_Switch : Boolean := True)
386 if Switch /= null then
388 Sw : String (1 .. Switch'Length);
396 and then (Sw (2) = 'A'
398 or else (Including_L_Switch and then Sw (2) = 'L'))
407 and then (Sw (2 .. 3) = "aL"
408 or else Sw (2 .. 3) = "aO"
409 or else Sw (2 .. 3) = "aI")
417 -- Because relative path arguments to --RTS= may be relative
418 -- to the search directory prefix, those relative path
419 -- arguments are not converted.
421 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
422 if Parent = null or else Parent'Length = 0 then
424 ("relative search path switches (""",
426 """) are not allowed");
431 (Sw (1 .. Start - 1) &
433 Directory_Separator &
434 Sw (Start .. Sw'Last));
439 if not Is_Absolute_Path (Sw) then
440 if Parent = null or else Parent'Length = 0 then
442 ("relative paths (""", Sw, """) are not allowed");
446 new String'(Parent.all & Directory_Separator & Sw);
452 end Test_If_Relative_Path;
458 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
464 Get_Name_String (ALI_File);
466 -- First, find the last dot
470 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
471 Finish := Finish - 1;
478 -- Now check that the dot is preceded by digits
481 Finish := Finish - 1;
483 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
487 -- If there is no difits, or if the digits are not preceded by
488 -- the character that precedes a unit index, this is not the ALI file
489 -- of a unit in a multi-unit source.
493 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
498 -- Build the index from the digit(s)
500 while Start <= Finish loop
501 Result := Result * 10 +
502 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');