2005-12-26 Anthony Green <green@redhat.com>
[official-gcc.git] / gcc / ada / makeutl.adb
blobf64aae3d1e98cb18e8465e8f438d351a46c6cf82
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E U T L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2005 Free Software Foundation, Inc. --
10 -- --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Namet; use Namet;
28 with Osint; use Osint;
29 with Prj.Ext;
30 with Prj.Util;
31 with Snames; use Snames;
32 with Table;
34 with System.HTable;
36 package body Makeutl is
38 type Mark_Key is record
39 File : File_Name_Type;
40 Index : Int;
41 end record;
42 -- Identify either a mono-unit source (when Index = 0) or a specific unit
43 -- in a multi-unit source.
45 -- There follow many global undocumented declarations, comments needed ???
47 Max_Mask_Num : constant := 2048;
49 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
51 function Hash (Key : Mark_Key) return Mark_Num;
53 package Marks is new System.HTable.Simple_HTable
54 (Header_Num => Mark_Num,
55 Element => Boolean,
56 No_Element => False,
57 Key => Mark_Key,
58 Hash => Hash,
59 Equal => "=");
60 -- A hash table to keep tracks of the marked units
62 type Linker_Options_Data is record
63 Project : Project_Id;
64 Options : String_List_Id;
65 end record;
67 Linker_Option_Initial_Count : constant := 20;
69 Linker_Options_Buffer : String_List_Access :=
70 new String_List (1 .. Linker_Option_Initial_Count);
72 Last_Linker_Option : Natural := 0;
74 package Linker_Opts is new Table.Table (
75 Table_Component_Type => Linker_Options_Data,
76 Table_Index_Type => Integer,
77 Table_Low_Bound => 1,
78 Table_Initial => 10,
79 Table_Increment => 100,
80 Table_Name => "Make.Linker_Opts");
82 procedure Add_Linker_Option (Option : String);
84 -----------------------
85 -- Add_Linker_Option --
86 -----------------------
88 procedure Add_Linker_Option (Option : String) is
89 begin
90 if Option'Length > 0 then
91 if Last_Linker_Option = Linker_Options_Buffer'Last then
92 declare
93 New_Buffer : constant String_List_Access :=
94 new String_List
95 (1 .. Linker_Options_Buffer'Last +
96 Linker_Option_Initial_Count);
97 begin
98 New_Buffer (Linker_Options_Buffer'Range) :=
99 Linker_Options_Buffer.all;
100 Linker_Options_Buffer.all := (others => null);
101 Free (Linker_Options_Buffer);
102 Linker_Options_Buffer := New_Buffer;
103 end;
104 end if;
106 Last_Linker_Option := Last_Linker_Option + 1;
107 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
108 end if;
109 end Add_Linker_Option;
111 ----------------------
112 -- Delete_All_Marks --
113 ----------------------
115 procedure Delete_All_Marks is
116 begin
117 Marks.Reset;
118 end Delete_All_Marks;
120 ----------
121 -- Hash --
122 ----------
124 function Hash (Key : Mark_Key) return Mark_Num is
125 begin
126 return Union_Id (Key.File) mod Max_Mask_Num;
127 end Hash;
129 ----------------------------
130 -- Is_External_Assignment --
131 ----------------------------
133 function Is_External_Assignment (Argv : String) return Boolean is
134 Start : Positive := 3;
135 Finish : Natural := Argv'Last;
136 Equal_Pos : Natural;
138 begin
139 if Argv'Last < 5 then
140 return False;
142 elsif Argv (3) = '"' then
143 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
144 return False;
145 else
146 Start := 4;
147 Finish := Argv'Last - 1;
148 end if;
149 end if;
151 Equal_Pos := Start;
153 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
154 Equal_Pos := Equal_Pos + 1;
155 end loop;
157 if Equal_Pos = Start
158 or else Equal_Pos >= Finish
159 then
160 return False;
161 else
162 Prj.Ext.Add
163 (External_Name => Argv (Start .. Equal_Pos - 1),
164 Value => Argv (Equal_Pos + 1 .. Finish));
165 return True;
166 end if;
167 end Is_External_Assignment;
169 ---------------
170 -- Is_Marked --
171 ---------------
173 function Is_Marked
174 (Source_File : File_Name_Type;
175 Index : Int := 0) return Boolean
177 begin
178 return Marks.Get (K => (File => Source_File, Index => Index));
179 end Is_Marked;
181 -----------------------------
182 -- Linker_Options_Switches --
183 -----------------------------
185 function Linker_Options_Switches
186 (Project : Project_Id;
187 In_Tree : Project_Tree_Ref) return String_List
189 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
190 -- The recursive routine used to add linker options
192 ----------------------------------
193 -- Recursive_Add_Linker_Options --
194 ----------------------------------
196 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
197 Data : Project_Data;
198 Linker_Package : Package_Id;
199 Options : Variable_Value;
200 Imported : Project_List;
202 begin
203 if Proj /= No_Project then
204 Data := In_Tree.Projects.Table (Proj);
206 if not Data.Seen then
207 In_Tree.Projects.Table (Proj).Seen := True;
208 Imported := Data.Imported_Projects;
210 while Imported /= Empty_Project_List loop
211 Recursive_Add_Linker_Options
212 (In_Tree.Project_Lists.Table
213 (Imported).Project);
214 Imported := In_Tree.Project_Lists.Table
215 (Imported).Next;
216 end loop;
218 if Proj /= Project then
219 Linker_Package :=
220 Prj.Util.Value_Of
221 (Name => Name_Linker,
222 In_Packages => Data.Decl.Packages,
223 In_Tree => In_Tree);
224 Options :=
225 Prj.Util.Value_Of
226 (Name => Name_Ada,
227 Index => 0,
228 Attribute_Or_Array_Name => Name_Linker_Options,
229 In_Package => Linker_Package,
230 In_Tree => In_Tree);
232 -- If attribute is present, add the project with
233 -- the attribute to table Linker_Opts.
235 if Options /= Nil_Variable_Value then
236 Linker_Opts.Increment_Last;
237 Linker_Opts.Table (Linker_Opts.Last) :=
238 (Project => Proj, Options => Options.Values);
239 end if;
240 end if;
241 end if;
242 end if;
243 end Recursive_Add_Linker_Options;
245 -- Start of processing for Linker_Options_Switches
247 begin
248 Linker_Opts.Init;
250 for Index in Project_Table.First ..
251 Project_Table.Last (In_Tree.Projects)
252 loop
253 In_Tree.Projects.Table (Index).Seen := False;
254 end loop;
256 Recursive_Add_Linker_Options (Project);
258 Last_Linker_Option := 0;
260 for Index in reverse 1 .. Linker_Opts.Last loop
261 declare
262 Options : String_List_Id := Linker_Opts.Table (Index).Options;
263 Proj : constant Project_Id :=
264 Linker_Opts.Table (Index).Project;
265 Option : Name_Id;
267 begin
268 -- If Dir_Path has not been computed for this project, do it now
270 if In_Tree.Projects.Table (Proj).Dir_Path = null then
271 In_Tree.Projects.Table (Proj).Dir_Path :=
272 new String'
273 (Get_Name_String
274 (In_Tree.Projects.Table
275 (Proj). Directory));
276 end if;
278 while Options /= Nil_String loop
279 Option :=
280 In_Tree.String_Elements.Table (Options).Value;
281 Options :=
282 In_Tree.String_Elements.Table (Options).Next;
283 Add_Linker_Option (Get_Name_String (Option));
285 -- Object files and -L switches specified with
286 -- relative paths and must be converted to
287 -- absolute paths.
289 Test_If_Relative_Path
290 (Switch =>
291 Linker_Options_Buffer (Last_Linker_Option),
292 Parent =>
293 In_Tree.Projects.Table (Proj).Dir_Path,
294 Including_L_Switch => True);
295 end loop;
296 end;
297 end loop;
299 return Linker_Options_Buffer (1 .. Last_Linker_Option);
300 end Linker_Options_Switches;
302 -----------
303 -- Mains --
304 -----------
306 package body Mains is
308 package Names is new Table.Table
309 (Table_Component_Type => File_Name_Type,
310 Table_Index_Type => Integer,
311 Table_Low_Bound => 1,
312 Table_Initial => 10,
313 Table_Increment => 100,
314 Table_Name => "Makeutl.Mains.Names");
315 -- The table that stores the mains
317 Current : Natural := 0;
318 -- The index of the last main retrieved from the table
320 --------------
321 -- Add_Main --
322 --------------
324 procedure Add_Main (Name : String) is
325 begin
326 Name_Len := 0;
327 Add_Str_To_Name_Buffer (Name);
328 Names.Increment_Last;
329 Names.Table (Names.Last) := Name_Find;
330 end Add_Main;
332 ------------
333 -- Delete --
334 ------------
336 procedure Delete is
337 begin
338 Names.Set_Last (0);
339 Mains.Reset;
340 end Delete;
342 ---------------
343 -- Next_Main --
344 ---------------
346 function Next_Main return String is
347 begin
348 if Current >= Names.Last then
349 return "";
351 else
352 Current := Current + 1;
353 return Get_Name_String (Names.Table (Current));
354 end if;
355 end Next_Main;
357 ---------------------
358 -- Number_Of_Mains --
359 ---------------------
361 function Number_Of_Mains return Natural is
362 begin
363 return Names.Last;
364 end Number_Of_Mains;
366 -----------
367 -- Reset --
368 -----------
370 procedure Reset is
371 begin
372 Current := 0;
373 end Reset;
375 end Mains;
377 ----------
378 -- Mark --
379 ----------
381 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
382 begin
383 Marks.Set (K => (File => Source_File, Index => Index), E => True);
384 end Mark;
386 ---------------------------
387 -- Test_If_Relative_Path --
388 ---------------------------
390 procedure Test_If_Relative_Path
391 (Switch : in out String_Access;
392 Parent : String_Access;
393 Including_L_Switch : Boolean := True)
395 begin
396 if Switch /= null then
397 declare
398 Sw : String (1 .. Switch'Length);
399 Start : Positive;
401 begin
402 Sw := Switch.all;
404 if Sw (1) = '-' then
405 if Sw'Length >= 3
406 and then (Sw (2) = 'A'
407 or else Sw (2) = 'I'
408 or else (Including_L_Switch and then Sw (2) = 'L'))
409 then
410 Start := 3;
412 if Sw = "-I-" then
413 return;
414 end if;
416 elsif Sw'Length >= 4
417 and then (Sw (2 .. 3) = "aL"
418 or else Sw (2 .. 3) = "aO"
419 or else Sw (2 .. 3) = "aI")
420 then
421 Start := 4;
423 else
424 return;
425 end if;
427 -- Because relative path arguments to --RTS= may be relative
428 -- to the search directory prefix, those relative path
429 -- arguments are not converted.
431 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
432 if Parent = null or else Parent'Length = 0 then
433 Do_Fail
434 ("relative search path switches (""",
436 """) are not allowed");
438 else
439 Switch :=
440 new String'
441 (Sw (1 .. Start - 1) &
442 Parent.all &
443 Directory_Separator &
444 Sw (Start .. Sw'Last));
445 end if;
446 end if;
448 else
449 if not Is_Absolute_Path (Sw) then
450 if Parent = null or else Parent'Length = 0 then
451 Do_Fail
452 ("relative paths (""", Sw, """) are not allowed");
454 else
455 Switch :=
456 new String'(Parent.all & Directory_Separator & Sw);
457 end if;
458 end if;
459 end if;
460 end;
461 end if;
462 end Test_If_Relative_Path;
464 -------------------
465 -- Unit_Index_Of --
466 -------------------
468 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
469 Start : Natural;
470 Finish : Natural;
471 Result : Int := 0;
473 begin
474 Get_Name_String (ALI_File);
476 -- First, find the last dot
478 Finish := Name_Len;
480 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
481 Finish := Finish - 1;
482 end loop;
484 if Finish = 1 then
485 return 0;
486 end if;
488 -- Now check that the dot is preceded by digits
490 Start := Finish;
491 Finish := Finish - 1;
493 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
494 Start := Start - 1;
495 end loop;
497 -- If there is no difits, or if the digits are not preceded by
498 -- the character that precedes a unit index, this is not the ALI file
499 -- of a unit in a multi-unit source.
501 if Start > Finish
502 or else Start = 1
503 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
504 then
505 return 0;
506 end if;
508 -- Build the index from the digit(s)
510 while Start <= Finish loop
511 Result := Result * 10 +
512 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
513 Start := Start + 1;
514 end loop;
516 return Result;
517 end Unit_Index_Of;
519 end Makeutl;