OSDN Git Service

2008-07-31 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / makeutl.adb
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 ------------------------------------------------------------------------------
25
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;
33
34 with Ada.Command_Line;  use Ada.Command_Line;
35
36 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
37
38 with System.Case_Util; use System.Case_Util;
39 with System.HTable;
40
41 package body Makeutl is
42
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.
49
50    --  There follow many global undocumented declarations, comments needed ???
51
52    Max_Mask_Num : constant := 2048;
53
54    subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
55
56    function Hash (Key : Mark_Key) return Mark_Num;
57
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
66
67    type Linker_Options_Data is record
68       Project : Project_Id;
69       Options : String_List_Id;
70    end record;
71
72    Linker_Option_Initial_Count : constant := 20;
73
74    Linker_Options_Buffer : String_List_Access :=
75      new String_List (1 .. Linker_Option_Initial_Count);
76
77    Last_Linker_Option : Natural := 0;
78
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");
86
87    procedure Add_Linker_Option (Option : String);
88
89    ---------
90    -- Add --
91    ---------
92
93    procedure Add
94      (Option : String_Access;
95       To     : in out String_List_Access;
96       Last   : in out Natural)
97    is
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;
105
106             --  Set all elements of the original options to null to avoid
107             --  deallocation of copies.
108
109             To.all := (others => null);
110
111             Free (To);
112             To := New_Options;
113          end;
114       end if;
115
116       Last := Last + 1;
117       To (Last) := Option;
118    end Add;
119
120    procedure Add
121      (Option : String;
122       To     : in out String_List_Access;
123       Last   : in out Natural)
124    is
125    begin
126       Add (Option => new String'(Option), To => To, Last => Last);
127    end Add;
128
129    -----------------------
130    -- Add_Linker_Option --
131    -----------------------
132
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;
150
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;
155
156    -----------------
157    -- Create_Name --
158    -----------------
159
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;
166
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;
173
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;
180
181    ----------------------
182    -- Delete_All_Marks --
183    ----------------------
184
185    procedure Delete_All_Marks is
186    begin
187       Marks.Reset;
188    end Delete_All_Marks;
189
190    ----------------------------
191    -- Executable_Prefix_Path --
192    ----------------------------
193
194    function Executable_Prefix_Path return String is
195       Exec_Name : constant String := Command_Name;
196
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 "".
202
203       ---------------------
204       -- Get_Install_Dir --
205       ---------------------
206
207       function Get_Install_Dir (S : String) return String is
208          Exec      : String  := S;
209          Path_Last : Integer := 0;
210
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;
218
219          if Path_Last >= Exec'First + 2 then
220             To_Lower (Exec (Path_Last - 2 .. Path_Last));
221          end if;
222
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;
230
231          return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4));
232       end Get_Install_Dir;
233
234    --  Beginning of Executable_Prefix_Path
235
236    begin
237       --  First determine if a path prefix was placed in front of the
238       --  executable name.
239
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;
245
246       --  If we get here, the user has typed the executable name with no
247       --  directory prefix.
248
249       declare
250          Path : constant String_Access := Locate_Exec_On_Path (Exec_Name);
251
252       begin
253          if Path = null then
254             return "";
255
256          else
257             return Get_Install_Dir (Path.all);
258          end if;
259       end;
260    end Executable_Prefix_Path;
261
262    ----------
263    -- Hash --
264    ----------
265
266    function Hash (Key : Mark_Key) return Mark_Num is
267    begin
268       return Union_Id (Key.File) mod Max_Mask_Num;
269    end Hash;
270
271    ------------
272    -- Inform --
273    ------------
274
275    procedure Inform (N : File_Name_Type; Msg : String) is
276    begin
277       Inform (Name_Id (N), Msg);
278    end Inform;
279
280    procedure Inform (N : Name_Id := No_Name; Msg : String) is
281    begin
282       Osint.Write_Program_Name;
283
284       Write_Str (": ");
285
286       if N /= No_Name then
287          Write_Str ("""");
288
289          declare
290             Name : constant String := Get_Name_String (N);
291          begin
292             if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
293                Write_Str (File_Name (Name));
294             else
295                Write_Str (Name);
296             end if;
297          end;
298
299          Write_Str (""" ");
300       end if;
301
302       Write_Str (Msg);
303       Write_Eol;
304    end Inform;
305
306    ----------------------------
307    -- Is_External_Assignment --
308    ----------------------------
309
310    function Is_External_Assignment (Argv : String) return Boolean is
311       Start     : Positive := 3;
312       Finish    : Natural := Argv'Last;
313       Equal_Pos : Natural;
314
315       pragma Assert (Argv'First = 1);
316       pragma Assert (Argv (1 .. 2) = "-X");
317
318    begin
319       if Argv'Last < 5 then
320          return False;
321
322       elsif Argv (3) = '"' then
323          if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
324             return False;
325          else
326             Start := 4;
327             Finish := Argv'Last - 1;
328          end if;
329       end if;
330
331       Equal_Pos := Start;
332
333       while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
334          Equal_Pos := Equal_Pos + 1;
335       end loop;
336
337       if Equal_Pos = Start
338         or else Equal_Pos > Finish
339       then
340          return False;
341       else
342          Prj.Ext.Add
343            (External_Name => Argv (Start .. Equal_Pos - 1),
344             Value         => Argv (Equal_Pos + 1 .. Finish));
345          return True;
346       end if;
347    end Is_External_Assignment;
348
349    ---------------
350    -- Is_Marked --
351    ---------------
352
353    function Is_Marked
354      (Source_File : File_Name_Type;
355       Index       : Int := 0) return Boolean
356    is
357    begin
358       return Marks.Get (K => (File => Source_File, Index => Index));
359    end Is_Marked;
360
361    -----------------------------
362    -- Linker_Options_Switches --
363    -----------------------------
364
365    function Linker_Options_Switches
366      (Project  : Project_Id;
367       In_Tree  : Project_Tree_Ref) return String_List
368    is
369       procedure Recursive_Add_Linker_Options (Proj : Project_Id);
370       --  The recursive routine used to add linker options
371
372       ----------------------------------
373       -- Recursive_Add_Linker_Options --
374       ----------------------------------
375
376       procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
377          Data           : Project_Data;
378          Linker_Package : Package_Id;
379          Options        : Variable_Value;
380          Imported       : Project_List;
381
382       begin
383          if Proj /= No_Project then
384             Data := In_Tree.Projects.Table (Proj);
385
386             if not Data.Seen then
387                In_Tree.Projects.Table (Proj).Seen := True;
388                Imported := Data.Imported_Projects;
389
390                while Imported /= Empty_Project_List loop
391                   Recursive_Add_Linker_Options
392                     (In_Tree.Project_Lists.Table
393                        (Imported).Project);
394                   Imported := In_Tree.Project_Lists.Table
395                                 (Imported).Next;
396                end loop;
397
398                if Proj /= Project then
399                   Linker_Package :=
400                     Prj.Util.Value_Of
401                       (Name        => Name_Linker,
402                        In_Packages => Data.Decl.Packages,
403                        In_Tree     => In_Tree);
404                   Options :=
405                     Prj.Util.Value_Of
406                       (Name                    => Name_Ada,
407                        Index                   => 0,
408                        Attribute_Or_Array_Name => Name_Linker_Options,
409                        In_Package              => Linker_Package,
410                        In_Tree                 => In_Tree);
411
412                   --  If attribute is present, add the project with
413                   --  the attribute to table Linker_Opts.
414
415                   if Options /= Nil_Variable_Value then
416                      Linker_Opts.Increment_Last;
417                      Linker_Opts.Table (Linker_Opts.Last) :=
418                        (Project => Proj, Options => Options.Values);
419                   end if;
420                end if;
421             end if;
422          end if;
423       end Recursive_Add_Linker_Options;
424
425    --  Start of processing for Linker_Options_Switches
426
427    begin
428       Linker_Opts.Init;
429
430       for Index in Project_Table.First ..
431                    Project_Table.Last (In_Tree.Projects)
432       loop
433          In_Tree.Projects.Table (Index).Seen := False;
434       end loop;
435
436       Recursive_Add_Linker_Options (Project);
437
438       Last_Linker_Option := 0;
439
440       for Index in reverse 1 .. Linker_Opts.Last loop
441          declare
442             Options : String_List_Id := Linker_Opts.Table (Index).Options;
443             Proj    : constant Project_Id :=
444               Linker_Opts.Table (Index).Project;
445             Option  : Name_Id;
446
447          begin
448             --  If Dir_Path has not been computed for this project, do it now
449
450             if In_Tree.Projects.Table (Proj).Dir_Path = null then
451                In_Tree.Projects.Table (Proj).Dir_Path :=
452                  new String'
453                    (Get_Name_String
454                         (In_Tree.Projects.Table
455                              (Proj).Directory.Name));
456             end if;
457
458             while Options /= Nil_String loop
459                Option :=
460                  In_Tree.String_Elements.Table (Options).Value;
461                Get_Name_String (Option);
462
463                --  Do not consider empty linker options
464
465                if Name_Len /= 0 then
466                   Add_Linker_Option (Name_Buffer (1 .. Name_Len));
467
468                   --  Object files and -L switches specified with relative
469                   --  paths must be converted to absolute paths.
470
471                   Test_If_Relative_Path
472                     (Switch =>
473                        Linker_Options_Buffer (Last_Linker_Option),
474                      Parent =>
475                        In_Tree.Projects.Table (Proj).Dir_Path,
476                      Including_L_Switch => True);
477                end if;
478
479                Options :=
480                  In_Tree.String_Elements.Table (Options).Next;
481             end loop;
482          end;
483       end loop;
484
485       return Linker_Options_Buffer (1 .. Last_Linker_Option);
486    end Linker_Options_Switches;
487
488    -----------
489    -- Mains --
490    -----------
491
492    package body Mains is
493
494       type File_And_Loc is record
495          File_Name : File_Name_Type;
496          Location  : Source_Ptr := No_Location;
497       end record;
498
499       package Names is new Table.Table
500         (Table_Component_Type => File_And_Loc,
501          Table_Index_Type     => Integer,
502          Table_Low_Bound      => 1,
503          Table_Initial        => 10,
504          Table_Increment      => 100,
505          Table_Name           => "Makeutl.Mains.Names");
506       --  The table that stores the mains
507
508       Current : Natural := 0;
509       --  The index of the last main retrieved from the table
510
511       --------------
512       -- Add_Main --
513       --------------
514
515       procedure Add_Main (Name : String) is
516       begin
517          Name_Len := 0;
518          Add_Str_To_Name_Buffer (Name);
519          Names.Increment_Last;
520          Names.Table (Names.Last) := (Name_Find, No_Location);
521       end Add_Main;
522
523       ------------
524       -- Delete --
525       ------------
526
527       procedure Delete is
528       begin
529          Names.Set_Last (0);
530          Mains.Reset;
531       end Delete;
532
533       ------------------
534       -- Get_Location --
535       ------------------
536
537       function Get_Location return Source_Ptr is
538       begin
539          if Current in Names.First .. Names.Last then
540             return Names.Table (Current).Location;
541          else
542             return No_Location;
543          end if;
544       end Get_Location;
545
546       ---------------
547       -- Next_Main --
548       ---------------
549
550       function Next_Main return String is
551       begin
552          if Current >= Names.Last then
553             return "";
554          else
555             Current := Current + 1;
556             return Get_Name_String (Names.Table (Current).File_Name);
557          end if;
558       end Next_Main;
559
560       ---------------------
561       -- Number_Of_Mains --
562       ---------------------
563
564       function Number_Of_Mains return Natural is
565       begin
566          return Names.Last;
567       end Number_Of_Mains;
568
569       -----------
570       -- Reset --
571       -----------
572
573       procedure Reset is
574       begin
575          Current := 0;
576       end Reset;
577
578       ------------------
579       -- Set_Location --
580       ------------------
581
582       procedure Set_Location (Location : Source_Ptr) is
583       begin
584          if Names.Last > 0 then
585             Names.Table (Names.Last).Location := Location;
586          end if;
587       end Set_Location;
588
589       -----------------
590       -- Update_Main --
591       -----------------
592
593       procedure Update_Main (Name : String) is
594       begin
595          if Current in Names.First .. Names.Last then
596             Name_Len := 0;
597             Add_Str_To_Name_Buffer (Name);
598             Names.Table (Current).File_Name := Name_Find;
599          end if;
600       end Update_Main;
601    end Mains;
602
603    ----------
604    -- Mark --
605    ----------
606
607    procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
608    begin
609       Marks.Set (K => (File => Source_File, Index => Index), E => True);
610    end Mark;
611
612    -----------------------
613    -- Path_Or_File_Name --
614    -----------------------
615
616    function Path_Or_File_Name (Path : Path_Name_Type) return String is
617       Path_Name : constant String := Get_Name_String (Path);
618    begin
619       if Debug.Debug_Flag_F then
620          return File_Name (Path_Name);
621       else
622          return Path_Name;
623       end if;
624    end Path_Or_File_Name;
625
626    ---------------------------
627    -- Test_If_Relative_Path --
628    ---------------------------
629
630    procedure Test_If_Relative_Path
631      (Switch               : in out String_Access;
632       Parent               : String_Access;
633       Including_L_Switch   : Boolean := True;
634       Including_Non_Switch : Boolean := True)
635    is
636    begin
637       if Switch /= null then
638          declare
639             Sw : String (1 .. Switch'Length);
640             Start : Positive;
641
642          begin
643             Sw := Switch.all;
644
645             if Sw (1) = '-' then
646                if Sw'Length >= 3
647                  and then (Sw (2) = 'A'
648                            or else Sw (2) = 'I'
649                            or else (Including_L_Switch and then Sw (2) = 'L'))
650                then
651                   Start := 3;
652
653                   if Sw = "-I-" then
654                      return;
655                   end if;
656
657                elsif Sw'Length >= 4
658                  and then (Sw (2 .. 3) = "aL"
659                            or else Sw (2 .. 3) = "aO"
660                            or else Sw (2 .. 3) = "aI")
661                then
662                   Start := 4;
663
664                else
665                   return;
666                end if;
667
668                --  Because relative path arguments to --RTS= may be relative
669                --  to the search directory prefix, those relative path
670                --  arguments are not converted.
671
672                if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
673                   if Parent = null or else Parent'Length = 0 then
674                      Do_Fail
675                        ("relative search path switches (""",
676                         Sw,
677                         """) are not allowed");
678
679                   else
680                      Switch :=
681                        new String'
682                          (Sw (1 .. Start - 1) &
683                           Parent.all &
684                           Directory_Separator &
685                           Sw (Start .. Sw'Last));
686                   end if;
687                end if;
688
689             elsif Including_Non_Switch then
690                if not Is_Absolute_Path (Sw) then
691                   if Parent = null or else Parent'Length = 0 then
692                      Do_Fail
693                        ("relative paths (""", Sw, """) are not allowed");
694
695                   else
696                      Switch :=
697                        new String'(Parent.all & Directory_Separator & Sw);
698                   end if;
699                end if;
700             end if;
701          end;
702       end if;
703    end Test_If_Relative_Path;
704
705    -------------------
706    -- Unit_Index_Of --
707    -------------------
708
709    function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
710       Start  : Natural;
711       Finish : Natural;
712       Result : Int := 0;
713
714    begin
715       Get_Name_String (ALI_File);
716
717       --  First, find the last dot
718
719       Finish := Name_Len;
720
721       while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
722          Finish := Finish - 1;
723       end loop;
724
725       if Finish = 1 then
726          return 0;
727       end if;
728
729       --  Now check that the dot is preceded by digits
730
731       Start := Finish;
732       Finish := Finish - 1;
733
734       while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
735          Start := Start - 1;
736       end loop;
737
738       --  If there are no digits, or if the digits are not preceded by
739       --  the character that precedes a unit index, this is not the ALI file
740       --  of a unit in a multi-unit source.
741
742       if Start > Finish
743         or else Start = 1
744         or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
745       then
746          return 0;
747       end if;
748
749       --  Build the index from the digit(s)
750
751       while Start <= Finish loop
752          Result := Result * 10 +
753                      Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
754          Start := Start + 1;
755       end loop;
756
757       return Result;
758    end Unit_Index_Of;
759
760 end Makeutl;