OSDN Git Service

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