2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / makeutl.adb
blob1755ade229cfd926d3c2cc7134c498881d282188
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-2008, 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 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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Debug;
27 with Osint; use Osint;
28 with Output; use Output;
29 with Prj.Ext;
30 with Prj.Util;
31 with Snames; use Snames;
32 with Table;
34 with Ada.Command_Line; use Ada.Command_Line;
36 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
38 with System.Case_Util; use System.Case_Util;
39 with System.HTable;
41 package body Makeutl is
43 type Mark_Key is record
44 File : File_Name_Type;
45 Index : Int;
46 end record;
47 -- Identify either a mono-unit source (when Index = 0) or a specific unit
48 -- (index = 1's origin index of unit) in a multi-unit source.
50 -- There follow many global undocumented declarations, comments needed ???
52 Max_Mask_Num : constant := 2048;
54 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
56 function Hash (Key : Mark_Key) return Mark_Num;
58 package Marks is new System.HTable.Simple_HTable
59 (Header_Num => Mark_Num,
60 Element => Boolean,
61 No_Element => False,
62 Key => Mark_Key,
63 Hash => Hash,
64 Equal => "=");
65 -- A hash table to keep tracks of the marked units
67 type Linker_Options_Data is record
68 Project : Project_Id;
69 Options : String_List_Id;
70 end record;
72 Linker_Option_Initial_Count : constant := 20;
74 Linker_Options_Buffer : String_List_Access :=
75 new String_List (1 .. Linker_Option_Initial_Count);
77 Last_Linker_Option : Natural := 0;
79 package Linker_Opts is new Table.Table (
80 Table_Component_Type => Linker_Options_Data,
81 Table_Index_Type => Integer,
82 Table_Low_Bound => 1,
83 Table_Initial => 10,
84 Table_Increment => 100,
85 Table_Name => "Make.Linker_Opts");
87 procedure Add_Linker_Option (Option : String);
89 ---------
90 -- Add --
91 ---------
93 procedure Add
94 (Option : String_Access;
95 To : in out String_List_Access;
96 Last : in out Natural)
98 begin
99 if Last = To'Last then
100 declare
101 New_Options : constant String_List_Access :=
102 new String_List (1 .. To'Last * 2);
103 begin
104 New_Options (To'Range) := To.all;
106 -- Set all elements of the original options to null to avoid
107 -- deallocation of copies.
109 To.all := (others => null);
111 Free (To);
112 To := New_Options;
113 end;
114 end if;
116 Last := Last + 1;
117 To (Last) := Option;
118 end Add;
120 procedure Add
121 (Option : String;
122 To : in out String_List_Access;
123 Last : in out Natural)
125 begin
126 Add (Option => new String'(Option), To => To, Last => Last);
127 end Add;
129 -----------------------
130 -- Add_Linker_Option --
131 -----------------------
133 procedure Add_Linker_Option (Option : String) is
134 begin
135 if Option'Length > 0 then
136 if Last_Linker_Option = Linker_Options_Buffer'Last then
137 declare
138 New_Buffer : constant String_List_Access :=
139 new String_List
140 (1 .. Linker_Options_Buffer'Last +
141 Linker_Option_Initial_Count);
142 begin
143 New_Buffer (Linker_Options_Buffer'Range) :=
144 Linker_Options_Buffer.all;
145 Linker_Options_Buffer.all := (others => null);
146 Free (Linker_Options_Buffer);
147 Linker_Options_Buffer := New_Buffer;
148 end;
149 end if;
151 Last_Linker_Option := Last_Linker_Option + 1;
152 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
153 end if;
154 end Add_Linker_Option;
156 -----------------
157 -- Create_Name --
158 -----------------
160 function Create_Name (Name : String) return File_Name_Type is
161 begin
162 Name_Len := 0;
163 Add_Str_To_Name_Buffer (Name);
164 return Name_Find;
165 end Create_Name;
167 function Create_Name (Name : String) return Name_Id is
168 begin
169 Name_Len := 0;
170 Add_Str_To_Name_Buffer (Name);
171 return Name_Find;
172 end Create_Name;
174 function Create_Name (Name : String) return Path_Name_Type is
175 begin
176 Name_Len := 0;
177 Add_Str_To_Name_Buffer (Name);
178 return Name_Find;
179 end Create_Name;
181 ----------------------
182 -- Delete_All_Marks --
183 ----------------------
185 procedure Delete_All_Marks is
186 begin
187 Marks.Reset;
188 end Delete_All_Marks;
190 ----------------------------
191 -- Executable_Prefix_Path --
192 ----------------------------
194 function Executable_Prefix_Path return String is
195 Exec_Name : constant String := Command_Name;
197 function Get_Install_Dir (S : String) return String;
198 -- S is the executable name preceded by the absolute or relative
199 -- path, e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory
200 -- where "bin" lies (in the example "C:\usr").
201 -- If the executable is not in a "bin" directory, return "".
203 ---------------------
204 -- Get_Install_Dir --
205 ---------------------
207 function Get_Install_Dir (S : String) return String is
208 Exec : String := S;
209 Path_Last : Integer := 0;
211 begin
212 for J in reverse Exec'Range loop
213 if Exec (J) = Directory_Separator then
214 Path_Last := J - 1;
215 exit;
216 end if;
217 end loop;
219 if Path_Last >= Exec'First + 2 then
220 To_Lower (Exec (Path_Last - 2 .. Path_Last));
221 end if;
223 if Path_Last < Exec'First + 2
224 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
225 or else (Path_Last - 3 >= Exec'First
226 and then Exec (Path_Last - 3) /= Directory_Separator)
227 then
228 return "";
229 end if;
231 return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4));
232 end Get_Install_Dir;
234 -- Beginning of Executable_Prefix_Path
236 begin
237 -- First determine if a path prefix was placed in front of the
238 -- executable name.
240 for J in reverse Exec_Name'Range loop
241 if Exec_Name (J) = Directory_Separator then
242 return Get_Install_Dir (Exec_Name);
243 end if;
244 end loop;
246 -- If we get here, the user has typed the executable name with no
247 -- directory prefix.
249 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name).all);
250 end Executable_Prefix_Path;
252 ----------
253 -- Hash --
254 ----------
256 function Hash (Key : Mark_Key) return Mark_Num is
257 begin
258 return Union_Id (Key.File) mod Max_Mask_Num;
259 end Hash;
261 ------------
262 -- Inform --
263 ------------
265 procedure Inform (N : File_Name_Type; Msg : String) is
266 begin
267 Inform (Name_Id (N), Msg);
268 end Inform;
270 procedure Inform (N : Name_Id := No_Name; Msg : String) is
271 begin
272 Osint.Write_Program_Name;
274 Write_Str (": ");
276 if N /= No_Name then
277 Write_Str ("""");
279 declare
280 Name : constant String := Get_Name_String (N);
281 begin
282 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
283 Write_Str (File_Name (Name));
284 else
285 Write_Str (Name);
286 end if;
287 end;
289 Write_Str (""" ");
290 end if;
292 Write_Str (Msg);
293 Write_Eol;
294 end Inform;
296 ----------------------------
297 -- Is_External_Assignment --
298 ----------------------------
300 function Is_External_Assignment (Argv : String) return Boolean is
301 Start : Positive := 3;
302 Finish : Natural := Argv'Last;
303 Equal_Pos : Natural;
305 pragma Assert (Argv'First = 1);
306 pragma Assert (Argv (1 .. 2) = "-X");
308 begin
309 if Argv'Last < 5 then
310 return False;
312 elsif Argv (3) = '"' then
313 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
314 return False;
315 else
316 Start := 4;
317 Finish := Argv'Last - 1;
318 end if;
319 end if;
321 Equal_Pos := Start;
323 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
324 Equal_Pos := Equal_Pos + 1;
325 end loop;
327 if Equal_Pos = Start
328 or else Equal_Pos > Finish
329 then
330 return False;
331 else
332 Prj.Ext.Add
333 (External_Name => Argv (Start .. Equal_Pos - 1),
334 Value => Argv (Equal_Pos + 1 .. Finish));
335 return True;
336 end if;
337 end Is_External_Assignment;
339 ---------------
340 -- Is_Marked --
341 ---------------
343 function Is_Marked
344 (Source_File : File_Name_Type;
345 Index : Int := 0) return Boolean
347 begin
348 return Marks.Get (K => (File => Source_File, Index => Index));
349 end Is_Marked;
351 -----------------------------
352 -- Linker_Options_Switches --
353 -----------------------------
355 function Linker_Options_Switches
356 (Project : Project_Id;
357 In_Tree : Project_Tree_Ref) return String_List
359 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
360 -- The recursive routine used to add linker options
362 ----------------------------------
363 -- Recursive_Add_Linker_Options --
364 ----------------------------------
366 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
367 Data : Project_Data;
368 Linker_Package : Package_Id;
369 Options : Variable_Value;
370 Imported : Project_List;
372 begin
373 if Proj /= No_Project then
374 Data := In_Tree.Projects.Table (Proj);
376 if not Data.Seen then
377 In_Tree.Projects.Table (Proj).Seen := True;
378 Imported := Data.Imported_Projects;
380 while Imported /= Empty_Project_List loop
381 Recursive_Add_Linker_Options
382 (In_Tree.Project_Lists.Table
383 (Imported).Project);
384 Imported := In_Tree.Project_Lists.Table
385 (Imported).Next;
386 end loop;
388 if Proj /= Project then
389 Linker_Package :=
390 Prj.Util.Value_Of
391 (Name => Name_Linker,
392 In_Packages => Data.Decl.Packages,
393 In_Tree => In_Tree);
394 Options :=
395 Prj.Util.Value_Of
396 (Name => Name_Ada,
397 Index => 0,
398 Attribute_Or_Array_Name => Name_Linker_Options,
399 In_Package => Linker_Package,
400 In_Tree => In_Tree);
402 -- If attribute is present, add the project with
403 -- the attribute to table Linker_Opts.
405 if Options /= Nil_Variable_Value then
406 Linker_Opts.Increment_Last;
407 Linker_Opts.Table (Linker_Opts.Last) :=
408 (Project => Proj, Options => Options.Values);
409 end if;
410 end if;
411 end if;
412 end if;
413 end Recursive_Add_Linker_Options;
415 -- Start of processing for Linker_Options_Switches
417 begin
418 Linker_Opts.Init;
420 for Index in Project_Table.First ..
421 Project_Table.Last (In_Tree.Projects)
422 loop
423 In_Tree.Projects.Table (Index).Seen := False;
424 end loop;
426 Recursive_Add_Linker_Options (Project);
428 Last_Linker_Option := 0;
430 for Index in reverse 1 .. Linker_Opts.Last loop
431 declare
432 Options : String_List_Id := Linker_Opts.Table (Index).Options;
433 Proj : constant Project_Id :=
434 Linker_Opts.Table (Index).Project;
435 Option : Name_Id;
437 begin
438 -- If Dir_Path has not been computed for this project, do it now
440 if In_Tree.Projects.Table (Proj).Dir_Path = null then
441 In_Tree.Projects.Table (Proj).Dir_Path :=
442 new String'
443 (Get_Name_String
444 (In_Tree.Projects.Table
445 (Proj).Directory.Name));
446 end if;
448 while Options /= Nil_String loop
449 Option :=
450 In_Tree.String_Elements.Table (Options).Value;
451 Get_Name_String (Option);
453 -- Do not consider empty linker options
455 if Name_Len /= 0 then
456 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
458 -- Object files and -L switches specified with relative
459 -- paths must be converted to absolute paths.
461 Test_If_Relative_Path
462 (Switch =>
463 Linker_Options_Buffer (Last_Linker_Option),
464 Parent =>
465 In_Tree.Projects.Table (Proj).Dir_Path,
466 Including_L_Switch => True);
467 end if;
469 Options :=
470 In_Tree.String_Elements.Table (Options).Next;
471 end loop;
472 end;
473 end loop;
475 return Linker_Options_Buffer (1 .. Last_Linker_Option);
476 end Linker_Options_Switches;
478 -----------
479 -- Mains --
480 -----------
482 package body Mains is
484 type File_And_Loc is record
485 File_Name : File_Name_Type;
486 Location : Source_Ptr := No_Location;
487 end record;
489 package Names is new Table.Table
490 (Table_Component_Type => File_And_Loc,
491 Table_Index_Type => Integer,
492 Table_Low_Bound => 1,
493 Table_Initial => 10,
494 Table_Increment => 100,
495 Table_Name => "Makeutl.Mains.Names");
496 -- The table that stores the mains
498 Current : Natural := 0;
499 -- The index of the last main retrieved from the table
501 --------------
502 -- Add_Main --
503 --------------
505 procedure Add_Main (Name : String) is
506 begin
507 Name_Len := 0;
508 Add_Str_To_Name_Buffer (Name);
509 Names.Increment_Last;
510 Names.Table (Names.Last) := (Name_Find, No_Location);
511 end Add_Main;
513 ------------
514 -- Delete --
515 ------------
517 procedure Delete is
518 begin
519 Names.Set_Last (0);
520 Mains.Reset;
521 end Delete;
523 ------------------
524 -- Get_Location --
525 ------------------
527 function Get_Location return Source_Ptr is
528 begin
529 if Current in Names.First .. Names.Last then
530 return Names.Table (Current).Location;
531 else
532 return No_Location;
533 end if;
534 end Get_Location;
536 ---------------
537 -- Next_Main --
538 ---------------
540 function Next_Main return String is
541 begin
542 if Current >= Names.Last then
543 return "";
544 else
545 Current := Current + 1;
546 return Get_Name_String (Names.Table (Current).File_Name);
547 end if;
548 end Next_Main;
550 ---------------------
551 -- Number_Of_Mains --
552 ---------------------
554 function Number_Of_Mains return Natural is
555 begin
556 return Names.Last;
557 end Number_Of_Mains;
559 -----------
560 -- Reset --
561 -----------
563 procedure Reset is
564 begin
565 Current := 0;
566 end Reset;
568 ------------------
569 -- Set_Location --
570 ------------------
572 procedure Set_Location (Location : Source_Ptr) is
573 begin
574 if Names.Last > 0 then
575 Names.Table (Names.Last).Location := Location;
576 end if;
577 end Set_Location;
579 -----------------
580 -- Update_Main --
581 -----------------
583 procedure Update_Main (Name : String) is
584 begin
585 if Current in Names.First .. Names.Last then
586 Name_Len := 0;
587 Add_Str_To_Name_Buffer (Name);
588 Names.Table (Current).File_Name := Name_Find;
589 end if;
590 end Update_Main;
591 end Mains;
593 ----------
594 -- Mark --
595 ----------
597 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
598 begin
599 Marks.Set (K => (File => Source_File, Index => Index), E => True);
600 end Mark;
602 -----------------------
603 -- Path_Or_File_Name --
604 -----------------------
606 function Path_Or_File_Name (Path : Path_Name_Type) return String is
607 Path_Name : constant String := Get_Name_String (Path);
608 begin
609 if Debug.Debug_Flag_F then
610 return File_Name (Path_Name);
611 else
612 return Path_Name;
613 end if;
614 end Path_Or_File_Name;
616 ---------------------------
617 -- Test_If_Relative_Path --
618 ---------------------------
620 procedure Test_If_Relative_Path
621 (Switch : in out String_Access;
622 Parent : String_Access;
623 Including_L_Switch : Boolean := True;
624 Including_Non_Switch : Boolean := True)
626 begin
627 if Switch /= null then
628 declare
629 Sw : String (1 .. Switch'Length);
630 Start : Positive;
632 begin
633 Sw := Switch.all;
635 if Sw (1) = '-' then
636 if Sw'Length >= 3
637 and then (Sw (2) = 'A'
638 or else Sw (2) = 'I'
639 or else (Including_L_Switch and then Sw (2) = 'L'))
640 then
641 Start := 3;
643 if Sw = "-I-" then
644 return;
645 end if;
647 elsif Sw'Length >= 4
648 and then (Sw (2 .. 3) = "aL"
649 or else Sw (2 .. 3) = "aO"
650 or else Sw (2 .. 3) = "aI")
651 then
652 Start := 4;
654 else
655 return;
656 end if;
658 -- Because relative path arguments to --RTS= may be relative
659 -- to the search directory prefix, those relative path
660 -- arguments are not converted.
662 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
663 if Parent = null or else Parent'Length = 0 then
664 Do_Fail
665 ("relative search path switches (""",
667 """) are not allowed");
669 else
670 Switch :=
671 new String'
672 (Sw (1 .. Start - 1) &
673 Parent.all &
674 Directory_Separator &
675 Sw (Start .. Sw'Last));
676 end if;
677 end if;
679 elsif Including_Non_Switch then
680 if not Is_Absolute_Path (Sw) then
681 if Parent = null or else Parent'Length = 0 then
682 Do_Fail
683 ("relative paths (""", Sw, """) are not allowed");
685 else
686 Switch :=
687 new String'(Parent.all & Directory_Separator & Sw);
688 end if;
689 end if;
690 end if;
691 end;
692 end if;
693 end Test_If_Relative_Path;
695 -------------------
696 -- Unit_Index_Of --
697 -------------------
699 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
700 Start : Natural;
701 Finish : Natural;
702 Result : Int := 0;
704 begin
705 Get_Name_String (ALI_File);
707 -- First, find the last dot
709 Finish := Name_Len;
711 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
712 Finish := Finish - 1;
713 end loop;
715 if Finish = 1 then
716 return 0;
717 end if;
719 -- Now check that the dot is preceded by digits
721 Start := Finish;
722 Finish := Finish - 1;
724 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
725 Start := Start - 1;
726 end loop;
728 -- If there are no digits, or if the digits are not preceded by
729 -- the character that precedes a unit index, this is not the ALI file
730 -- of a unit in a multi-unit source.
732 if Start > Finish
733 or else Start = 1
734 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
735 then
736 return 0;
737 end if;
739 -- Build the index from the digit(s)
741 while Start <= Finish loop
742 Result := Result * 10 +
743 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
744 Start := Start + 1;
745 end loop;
747 return Result;
748 end Unit_Index_Of;
750 end Makeutl;