OSDN Git Service

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