OSDN Git Service

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