OSDN Git Service

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