OSDN Git Service

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