OSDN Git Service

2005-03-08 Geert Bosch <bosch@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-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,  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;
189       In_Tree  : Project_Tree_Ref) return String_List
190    is
191       procedure Recursive_Add_Linker_Options (Proj : Project_Id);
192       --  The recursive routine used to add linker options
193
194       ----------------------------------
195       -- Recursive_Add_Linker_Options --
196       ----------------------------------
197
198       procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
199          Data           : Project_Data;
200          Linker_Package : Package_Id;
201          Options        : Variable_Value;
202          Imported       : Project_List;
203
204       begin
205          if Proj /= No_Project then
206             Data := In_Tree.Projects.Table (Proj);
207
208             if not Data.Seen then
209                In_Tree.Projects.Table (Proj).Seen := True;
210                Imported := Data.Imported_Projects;
211
212                while Imported /= Empty_Project_List loop
213                   Recursive_Add_Linker_Options
214                     (In_Tree.Project_Lists.Table
215                        (Imported).Project);
216                   Imported := In_Tree.Project_Lists.Table
217                                 (Imported).Next;
218                end loop;
219
220                if Proj /= Project then
221                   Linker_Package :=
222                     Prj.Util.Value_Of
223                       (Name        => Name_Linker,
224                        In_Packages => Data.Decl.Packages,
225                        In_Tree     => In_Tree);
226                   Options :=
227                     Prj.Util.Value_Of
228                       (Name                    => Name_Ada,
229                        Index                   => 0,
230                        Attribute_Or_Array_Name => Name_Linker_Options,
231                        In_Package              => Linker_Package,
232                        In_Tree                 => In_Tree);
233
234                   --  If attribute is present, add the project with
235                   --  the attribute to table Linker_Opts.
236
237                   if Options /= Nil_Variable_Value then
238                      Linker_Opts.Increment_Last;
239                      Linker_Opts.Table (Linker_Opts.Last) :=
240                        (Project => Proj, Options => Options.Values);
241                   end if;
242                end if;
243             end if;
244          end if;
245       end Recursive_Add_Linker_Options;
246
247    --  Start of processing for Linker_Options_Switches
248
249    begin
250       Linker_Opts.Init;
251
252       for Index in Project_Table.First ..
253                    Project_Table.Last (In_Tree.Projects)
254       loop
255          In_Tree.Projects.Table (Index).Seen := False;
256       end loop;
257
258       Recursive_Add_Linker_Options (Project);
259
260       Last_Linker_Option := 0;
261
262       for Index in reverse 1 .. Linker_Opts.Last loop
263          declare
264             Options : String_List_Id := Linker_Opts.Table (Index).Options;
265             Proj    : constant Project_Id :=
266               Linker_Opts.Table (Index).Project;
267             Option  : Name_Id;
268
269          begin
270             --  If Dir_Path has not been computed for this project, do it now
271
272             if In_Tree.Projects.Table (Proj).Dir_Path = null then
273                In_Tree.Projects.Table (Proj).Dir_Path :=
274                  new String'
275                    (Get_Name_String
276                         (In_Tree.Projects.Table
277                              (Proj). Directory));
278             end if;
279
280             while Options /= Nil_String loop
281                Option :=
282                  In_Tree.String_Elements.Table (Options).Value;
283                Options :=
284                  In_Tree.String_Elements.Table (Options).Next;
285                Add_Linker_Option (Get_Name_String (Option));
286
287                --  Object files and -L switches specified with
288                --  relative paths and must be converted to
289                --  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 loop;
298          end;
299       end loop;
300
301       return Linker_Options_Buffer (1 .. Last_Linker_Option);
302    end Linker_Options_Switches;
303
304    -----------
305    -- Mains --
306    -----------
307
308    package body Mains is
309
310       package Names is new Table.Table
311         (Table_Component_Type => File_Name_Type,
312          Table_Index_Type     => Integer,
313          Table_Low_Bound      => 1,
314          Table_Initial        => 10,
315          Table_Increment      => 100,
316          Table_Name           => "Makeutl.Mains.Names");
317       --  The table that stores the mains
318
319       Current : Natural := 0;
320       --  The index of the last main retrieved from the table
321
322       --------------
323       -- Add_Main --
324       --------------
325
326       procedure Add_Main (Name : String) is
327       begin
328          Name_Len := 0;
329          Add_Str_To_Name_Buffer (Name);
330          Names.Increment_Last;
331          Names.Table (Names.Last) := Name_Find;
332       end Add_Main;
333
334       ------------
335       -- Delete --
336       ------------
337
338       procedure Delete is
339       begin
340          Names.Set_Last (0);
341          Mains.Reset;
342       end Delete;
343
344       ---------------
345       -- Next_Main --
346       ---------------
347
348       function Next_Main return String is
349       begin
350          if Current >= Names.Last then
351             return "";
352
353          else
354             Current := Current + 1;
355             return Get_Name_String (Names.Table (Current));
356          end if;
357       end Next_Main;
358
359       ---------------------
360       -- Number_Of_Mains --
361       ---------------------
362
363       function Number_Of_Mains return Natural is
364       begin
365          return Names.Last;
366       end Number_Of_Mains;
367
368       -----------
369       -- Reset --
370       -----------
371
372       procedure Reset is
373       begin
374          Current := 0;
375       end Reset;
376
377    end Mains;
378
379    ----------
380    -- Mark --
381    ----------
382
383    procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
384    begin
385       Marks.Set (K => (File => Source_File, Index => Index), E => True);
386    end Mark;
387
388    ---------------------------
389    -- Test_If_Relative_Path --
390    ---------------------------
391
392    procedure Test_If_Relative_Path
393      (Switch             : in out String_Access;
394       Parent             : String_Access;
395       Including_L_Switch : Boolean := True)
396    is
397    begin
398       if Switch /= null then
399          declare
400             Sw : String (1 .. Switch'Length);
401             Start : Positive;
402
403          begin
404             Sw := Switch.all;
405
406             if Sw (1) = '-' then
407                if Sw'Length >= 3
408                  and then (Sw (2) = 'A'
409                            or else Sw (2) = 'I'
410                            or else (Including_L_Switch and then Sw (2) = 'L'))
411                then
412                   Start := 3;
413
414                   if Sw = "-I-" then
415                      return;
416                   end if;
417
418                elsif Sw'Length >= 4
419                  and then (Sw (2 .. 3) = "aL"
420                            or else Sw (2 .. 3) = "aO"
421                            or else Sw (2 .. 3) = "aI")
422                then
423                   Start := 4;
424
425                else
426                   return;
427                end if;
428
429                --  Because relative path arguments to --RTS= may be relative
430                --  to the search directory prefix, those relative path
431                --  arguments are not converted.
432
433                if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
434                   if Parent = null or else Parent'Length = 0 then
435                      Do_Fail
436                        ("relative search path switches (""",
437                         Sw,
438                         """) are not allowed");
439
440                   else
441                      Switch :=
442                        new String'
443                          (Sw (1 .. Start - 1) &
444                           Parent.all &
445                           Directory_Separator &
446                           Sw (Start .. Sw'Last));
447                   end if;
448                end if;
449
450             else
451                if not Is_Absolute_Path (Sw) then
452                   if Parent = null or else Parent'Length = 0 then
453                      Do_Fail
454                        ("relative paths (""", Sw, """) are not allowed");
455
456                   else
457                      Switch :=
458                        new String'(Parent.all & Directory_Separator & Sw);
459                   end if;
460                end if;
461             end if;
462          end;
463       end if;
464    end Test_If_Relative_Path;
465
466    -------------------
467    -- Unit_Index_Of --
468    -------------------
469
470    function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
471       Start  : Natural;
472       Finish : Natural;
473       Result : Int := 0;
474
475    begin
476       Get_Name_String (ALI_File);
477
478       --  First, find the last dot
479
480       Finish := Name_Len;
481
482       while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
483          Finish := Finish - 1;
484       end loop;
485
486       if Finish = 1 then
487          return 0;
488       end if;
489
490       --  Now check that the dot is preceded by digits
491
492       Start := Finish;
493       Finish := Finish - 1;
494
495       while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
496          Start := Start - 1;
497       end loop;
498
499       --  If there is no difits, or if the digits are not preceded by
500       --  the character that precedes a unit index, this is not the ALI file
501       --  of a unit in a multi-unit source.
502
503       if Start > Finish
504         or else Start = 1
505         or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
506       then
507          return 0;
508       end if;
509
510       --  Build the index from the digit(s)
511
512       while Start <= Finish loop
513          Result := Result * 10 +
514                      Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
515          Start := Start + 1;
516       end loop;
517
518       return Result;
519    end Unit_Index_Of;
520
521 end Makeutl;