OSDN Git Service

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