OSDN Git Service

2008-08-01 Doug Rupp <rupp@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-specific-vms-alpha.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                    M L I B . T G T . S P E C I F I C                     --
6 --                           (Alpha VMS Version)                            --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --          Copyright (C) 2003-2008, Free Software Foundation, Inc.         --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
20 -- http://www.gnu.org/licenses for a complete copy of the license.          --
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 --  This is the Alpha VMS version of the body
28
29 with Ada.Characters.Handling; use Ada.Characters.Handling;
30
31 with MLib.Fil;
32 with MLib.Utl;
33
34 with MLib.Tgt.VMS_Common;
35 pragma Warnings (Off, MLib.Tgt.VMS_Common);
36 --  MLib.Tgt.VMS_Common is with'ed only for elaboration purposes
37
38 with Opt;      use Opt;
39 with Output;   use Output;
40
41 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
42
43 with System;           use System;
44 with System.Case_Util; use System.Case_Util;
45 with System.CRTL;      use System.CRTL;
46
47 package body MLib.Tgt.Specific is
48
49    --  Non default subprogram. See comment in mlib-tgt.ads.
50
51    procedure Build_Dynamic_Library
52      (Ofiles       : Argument_List;
53       Options      : Argument_List;
54       Interfaces   : Argument_List;
55       Lib_Filename : String;
56       Lib_Dir      : String;
57       Symbol_Data  : Symbol_Record;
58       Driver_Name  : Name_Id := No_Name;
59       Lib_Version  : String  := "";
60       Auto_Init    : Boolean := False);
61
62    --  Local variables
63
64    Empty_Argument_List : aliased Argument_List := (1 .. 0 => null);
65    Additional_Objects  : Argument_List_Access := Empty_Argument_List'Access;
66    --  Used to add the generated auto-init object files for auto-initializing
67    --  stand-alone libraries.
68
69    Macro_Name : constant String := "mcr gnu:[bin]gcc -c -x assembler";
70    --  The name of the command to invoke the macro-assembler
71
72    VMS_Options : Argument_List := (1 .. 1 => null);
73
74    Gnatsym_Name : constant String := "gnatsym";
75
76    Gnatsym_Path : String_Access;
77
78    Arguments : Argument_List_Access := null;
79    Last_Argument : Natural := 0;
80
81    Success : Boolean := False;
82
83    Shared_Libgcc : aliased String := "-shared-libgcc";
84
85    Shared_Libgcc_Switch : constant Argument_List :=
86                             (1 => Shared_Libgcc'Access);
87
88    ---------------------------
89    -- Build_Dynamic_Library --
90    ---------------------------
91
92    procedure Build_Dynamic_Library
93      (Ofiles       : Argument_List;
94       Options      : Argument_List;
95       Interfaces   : Argument_List;
96       Lib_Filename : String;
97       Lib_Dir      : String;
98       Symbol_Data  : Symbol_Record;
99       Driver_Name  : Name_Id := No_Name;
100       Lib_Version  : String  := "";
101       Auto_Init    : Boolean := False)
102    is
103
104       Lib_File : constant String :=
105                    Lib_Dir & Directory_Separator & "lib" &
106                    Fil.Ext_To (Lib_Filename, DLL_Ext);
107
108       Opts      : Argument_List := Options;
109       Last_Opt  : Natural       := Opts'Last;
110       Opts2     : Argument_List (Options'Range);
111       Last_Opt2 : Natural       := Opts2'First - 1;
112
113       Inter : constant Argument_List := Interfaces;
114
115       function Is_Interface (Obj_File : String) return Boolean;
116       --  For a Stand-Alone Library, returns True if Obj_File is the object
117       --  file name of an interface of the SAL. For other libraries, always
118       --  return True.
119
120       function Option_File_Name return String;
121       --  Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
122
123       function Version_String return String;
124       --  Returns Lib_Version if not empty and if Symbol_Data.Symbol_Policy is
125       --  not Autonomous, otherwise returns "". When Symbol_Data.Symbol_Policy
126       --  is Autonomous, fails gnatmake if Lib_Version is not the image of a
127       --  positive number.
128
129       ------------------
130       -- Is_Interface --
131       ------------------
132
133       function Is_Interface (Obj_File : String) return Boolean is
134          ALI : constant String :=
135                  Fil.Ext_To
136                   (Filename => To_Lower (Base_Name (Obj_File)),
137                    New_Ext  => "ali");
138
139       begin
140          if Inter'Length = 0 then
141             return True;
142
143          elsif ALI'Length > 2 and then
144                ALI (ALI'First .. ALI'First + 2) = "b__"
145          then
146             return True;
147
148          else
149             for J in Inter'Range loop
150                if Inter (J).all = ALI then
151                   return True;
152                end if;
153             end loop;
154
155             return False;
156          end if;
157       end Is_Interface;
158
159       ----------------------
160       -- Option_File_Name --
161       ----------------------
162
163       function Option_File_Name return String is
164       begin
165          if Symbol_Data.Symbol_File = No_Path then
166             return "symvec.opt";
167          else
168             Get_Name_String (Symbol_Data.Symbol_File);
169             To_Lower (Name_Buffer (1 .. Name_Len));
170             return Name_Buffer (1 .. Name_Len);
171          end if;
172       end Option_File_Name;
173
174       --------------------
175       -- Version_String --
176       --------------------
177
178       function Version_String return String is
179          Version : Integer := 0;
180
181       begin
182          if Lib_Version = ""
183            or else Symbol_Data.Symbol_Policy /= Autonomous
184          then
185             return "";
186
187          else
188             begin
189                Version := Integer'Value (Lib_Version);
190
191                if Version <= 0 then
192                   raise Constraint_Error;
193                end if;
194
195                return Lib_Version;
196
197             exception
198                when Constraint_Error =>
199                   Fail ("illegal version """, Lib_Version,
200                         """ (on VMS version must be a positive number)");
201                   return "";
202             end;
203          end if;
204       end Version_String;
205
206       ---------------------
207       -- Local Variables --
208       ---------------------
209
210       Opt_File_Name  : constant String := Option_File_Name;
211       Version        : constant String := Version_String;
212       For_Linker_Opt : String_Access;
213
214    --  Start of processing for Build_Dynamic_Library
215
216    begin
217       --  If option file name does not ends with ".opt", append "/OPTIONS"
218       --  to its specification for the VMS linker.
219
220       if Opt_File_Name'Length > 4
221         and then
222           Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
223       then
224          For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
225       else
226          For_Linker_Opt :=
227            new String'("--for-linker=" & Opt_File_Name & "/OPTIONS");
228       end if;
229
230       VMS_Options (VMS_Options'First) := For_Linker_Opt;
231
232       for J in Inter'Range loop
233          To_Lower (Inter (J).all);
234       end loop;
235
236       --  "gnatsym" is necessary for building the option file
237
238       if Gnatsym_Path = null then
239          Gnatsym_Path := Locate_Exec_On_Path (Gnatsym_Name);
240
241          if Gnatsym_Path = null then
242             Fail (Gnatsym_Name, " not found in path");
243          end if;
244       end if;
245
246       --  For auto-initialization of a stand-alone library, we create
247       --  a macro-assembly file and we invoke the macro-assembler.
248
249       if Auto_Init then
250          declare
251             Macro_File_Name : constant String := Lib_Filename & "__init.asm";
252             Macro_File      : File_Descriptor;
253             Init_Proc       : String := Lib_Filename & "INIT";
254             Popen_Result    : System.Address;
255             Pclose_Result   : Integer;
256             Len             : Natural;
257             OK              : Boolean := True;
258
259             command  : constant String :=
260                          Macro_Name & " " & Macro_File_Name & ASCII.NUL;
261             --  The command to invoke the assembler on the generated auto-init
262             --  assembly file.
263
264             mode : constant String := "r" & ASCII.NUL;
265             --  The mode for the invocation of Popen
266
267          begin
268             To_Upper (Init_Proc);
269
270             if Verbose_Mode then
271                Write_Str ("Creating auto-init assembly file """);
272                Write_Str (Macro_File_Name);
273                Write_Line ("""");
274             end if;
275
276             --  Create and write the auto-init assembly file
277
278             declare
279                use ASCII;
280
281                --  Output a dummy transfer address for debugging
282                --  followed by the LIB$INITIALIZE section.
283
284                Lines : constant String :=
285                  HT & ".text" & LF &
286                  HT & ".align 4" & LF &
287                  HT & ".globl __main" & LF &
288                  HT & ".ent __main" & LF &
289                  "__main..en:" & LF &
290                  HT & ".base $27" & LF &
291                  HT & ".frame $29,0,$26,8" & LF &
292                  HT & "ret $31,($26),1" & LF &
293                  HT & ".link" & LF &
294                  "__main:" & LF &
295                  HT & ".pdesc __main..en,null" & LF &
296                  HT & ".end __main" & LF & LF &
297                  HT & ".section LIB$INITIALIZE,GBL,NOWRT" & LF &
298                  HT & ".long " & Init_Proc & LF;
299
300             begin
301                Macro_File := Create_File (Macro_File_Name, Text);
302                OK := Macro_File /= Invalid_FD;
303
304                if OK then
305                   Len := Write
306                     (Macro_File, Lines (Lines'First)'Address,
307                      Lines'Length);
308                   OK := Len = Lines'Length;
309                end if;
310
311                if OK then
312                   Close (Macro_File, OK);
313                end if;
314
315                if not OK then
316                   Fail ("creation of auto-init assembly file """,
317                         Macro_File_Name, """ failed");
318                end if;
319             end;
320
321             --  Invoke the macro-assembler
322
323             if Verbose_Mode then
324                Write_Str ("Assembling auto-init assembly file """);
325                Write_Str (Macro_File_Name);
326                Write_Line ("""");
327             end if;
328
329             Popen_Result := popen (command (command'First)'Address,
330                                    mode (mode'First)'Address);
331
332             if Popen_Result = Null_Address then
333                Fail ("assembly of auto-init assembly file """,
334                      Macro_File_Name, """ failed");
335             end if;
336
337             --  Wait for the end of execution of the macro-assembler
338
339             Pclose_Result := pclose (Popen_Result);
340
341             if Pclose_Result < 0 then
342                Fail ("assembly of auto init assembly file """,
343                      Macro_File_Name, """ failed");
344             end if;
345
346             --  Add the generated object file to the list of objects to be
347             --  included in the library.
348
349             Additional_Objects :=
350               new Argument_List'
351                 (1 => new String'(Lib_Filename & "__init.obj"));
352          end;
353       end if;
354
355       --  Allocate the argument list and put the symbol file name, the
356       --  reference (if any) and the policy (if not autonomous).
357
358       Arguments := new Argument_List (1 .. Ofiles'Length + 8);
359
360       Last_Argument := 0;
361
362       --  Verbosity
363
364       if Verbose_Mode then
365          Last_Argument := Last_Argument + 1;
366          Arguments (Last_Argument) := new String'("-v");
367       end if;
368
369       --  Version number (major ID)
370
371       if Lib_Version /= "" then
372          Last_Argument := Last_Argument + 1;
373          Arguments (Last_Argument) := new String'("-V");
374          Last_Argument := Last_Argument + 1;
375          Arguments (Last_Argument) := new String'(Version);
376       end if;
377
378       --  Symbol file
379
380       Last_Argument := Last_Argument + 1;
381       Arguments (Last_Argument) := new String'("-s");
382       Last_Argument := Last_Argument + 1;
383       Arguments (Last_Argument) := new String'(Opt_File_Name);
384
385       --  Reference Symbol File
386
387       if Symbol_Data.Reference /= No_Path then
388          Last_Argument := Last_Argument + 1;
389          Arguments (Last_Argument) := new String'("-r");
390          Last_Argument := Last_Argument + 1;
391          Arguments (Last_Argument) :=
392            new String'(Get_Name_String (Symbol_Data.Reference));
393       end if;
394
395       --  Policy
396
397       case Symbol_Data.Symbol_Policy is
398          when Autonomous =>
399             null;
400
401          when Compliant =>
402             Last_Argument := Last_Argument + 1;
403             Arguments (Last_Argument) := new String'("-c");
404
405          when Controlled =>
406             Last_Argument := Last_Argument + 1;
407             Arguments (Last_Argument) := new String'("-C");
408
409          when Restricted =>
410             Last_Argument := Last_Argument + 1;
411             Arguments (Last_Argument) := new String'("-R");
412
413          when Direct =>
414             Last_Argument := Last_Argument + 1;
415             Arguments (Last_Argument) := new String'("-D");
416
417       end case;
418
419       --  Add each relevant object file
420
421       for Index in Ofiles'Range loop
422          if Is_Interface (Ofiles (Index).all) then
423             Last_Argument := Last_Argument + 1;
424             Arguments (Last_Argument) := new String'(Ofiles (Index).all);
425          end if;
426       end loop;
427
428       --  Spawn gnatsym
429
430       Spawn (Program_Name => Gnatsym_Path.all,
431              Args         => Arguments (1 .. Last_Argument),
432              Success      => Success);
433
434       if not Success then
435          Fail ("unable to create symbol file for library """,
436                Lib_Filename, """");
437       end if;
438
439       Free (Arguments);
440
441       --  Move all the -l switches from Opts to Opts2
442
443       declare
444          Index : Natural := Opts'First;
445          Opt   : String_Access;
446
447       begin
448          while Index <= Last_Opt loop
449             Opt := Opts (Index);
450
451             if Opt'Length > 2 and then
452               Opt (Opt'First .. Opt'First + 1) = "-l"
453             then
454                if Index < Last_Opt then
455                   Opts (Index .. Last_Opt - 1) :=
456                     Opts (Index + 1 .. Last_Opt);
457                end if;
458
459                Last_Opt := Last_Opt - 1;
460
461                Last_Opt2 := Last_Opt2 + 1;
462                Opts2 (Last_Opt2) := Opt;
463
464             else
465                Index := Index + 1;
466             end if;
467          end loop;
468       end;
469
470       --  Invoke gcc to build the library
471
472       Utl.Gcc
473         (Output_File => Lib_File,
474          Objects     => Ofiles & Additional_Objects.all,
475          Options     => VMS_Options,
476          Options_2   => Shared_Libgcc_Switch &
477                         Opts (Opts'First .. Last_Opt) &
478                         Opts2 (Opts2'First .. Last_Opt2),
479          Driver_Name => Driver_Name);
480
481       --  The auto-init object file need to be deleted, so that it will not
482       --  be included in the library as a regular object file, otherwise
483       --  it will be included twice when the library will be built next
484       --  time, which may lead to errors.
485
486       if Auto_Init then
487          declare
488             Auto_Init_Object_File_Name : constant String :=
489                                            Lib_Filename & "__init.obj";
490             Disregard : Boolean;
491
492          begin
493             if Verbose_Mode then
494                Write_Str ("deleting auto-init object file """);
495                Write_Str (Auto_Init_Object_File_Name);
496                Write_Line ("""");
497             end if;
498
499             Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
500          end;
501       end if;
502    end Build_Dynamic_Library;
503
504 --  Package initialization
505
506 begin
507    Build_Dynamic_Library_Ptr    := Build_Dynamic_Library'Access;
508 end MLib.Tgt.Specific;