OSDN Git Service

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