OSDN Git Service

2008-08-22 Geert Bosch <bosch@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-specific-vms-ia64.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 --                         (Integrity VMS Version)                          --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --          Copyright (C) 2004-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 Integrity 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       begin
181          if Lib_Version = ""
182            or else Symbol_Data.Symbol_Policy /= Autonomous
183          then
184             return "";
185
186          else
187             begin
188                Version := Integer'Value (Lib_Version);
189
190                if Version <= 0 then
191                   raise Constraint_Error;
192                end if;
193
194                return Lib_Version;
195
196             exception
197                when Constraint_Error =>
198                   Fail ("illegal version """, Lib_Version,
199                         """ (on VMS version must be a positive number)");
200                   return "";
201             end;
202          end if;
203       end Version_String;
204
205       ---------------------
206       -- Local Variables --
207       ---------------------
208
209       Opt_File_Name  : constant String := Option_File_Name;
210       Version        : constant String := Version_String;
211       For_Linker_Opt : String_Access;
212
213    --  Start of processing for Build_Dynamic_Library
214
215    begin
216       --  Option file must end with ".opt"
217
218       if Opt_File_Name'Length > 4
219         and then
220           Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
221       then
222          For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
223       else
224          Fail ("Options File """, Opt_File_Name, """ must end with .opt");
225       end if;
226
227       VMS_Options (VMS_Options'First) := For_Linker_Opt;
228
229       for J in Inter'Range loop
230          To_Lower (Inter (J).all);
231       end loop;
232
233       --  "gnatsym" is necessary for building the option file
234
235       if Gnatsym_Path = null then
236          Gnatsym_Path := Locate_Exec_On_Path (Gnatsym_Name);
237
238          if Gnatsym_Path = null then
239             Fail (Gnatsym_Name, " not found in path");
240          end if;
241       end if;
242
243       --  For auto-initialization of a stand-alone library, we create
244       --  a macro-assembly file and we invoke the macro-assembler.
245
246       if Auto_Init then
247          declare
248             Macro_File_Name : constant String := Lib_Filename & "__init.asm";
249             Macro_File      : File_Descriptor;
250             Init_Proc       : String := Lib_Filename & "INIT";
251             Popen_Result    : System.Address;
252             Pclose_Result   : Integer;
253             Len             : Natural;
254             OK              : Boolean := True;
255
256             command : constant String :=
257                         Macro_Name & " " & Macro_File_Name & ASCII.NUL;
258             --  The command to invoke the assembler on the generated auto-init
259             --  assembly file.
260             --  Why odd lower case name ???
261
262             mode : constant String := "r" & ASCII.NUL;
263             --  The mode for the invocation of Popen
264             --  Why odd lower case name ???
265
266          begin
267             To_Upper (Init_Proc);
268
269             if Verbose_Mode then
270                Write_Str ("Creating auto-init assembly file """);
271                Write_Str (Macro_File_Name);
272                Write_Line ("""");
273             end if;
274
275             --  Create and write the auto-init assembly file
276
277             declare
278                use ASCII;
279
280                --  Output a dummy transfer address for debugging
281                --  followed by the LIB$INITIALIZE section.
282
283                Lines : constant String :=
284                  HT & ".pred.safe_across_calls p1-p5,p16-p63" & LF &
285                  HT & ".text" & LF &
286                  HT & ".align 16" & LF &
287                  HT & ".global __main#" & LF &
288                  HT & ".proc __main#" & LF &
289                       "__main:" & LF &
290                  HT & ".prologue" & LF &
291                  HT & ".body" & LF &
292                  HT & ".mib" & LF &
293                  HT & "nop 0" & LF &
294                  HT & "nop 0" & LF &
295                  HT & "br.ret.sptk.many b0" & LF &
296                  HT & ".endp __main#" & LF & LF &
297                  HT & ".type " & Init_Proc & "#, @function" & LF &
298                  HT & ".global " & Init_Proc & "#" & LF &
299                  HT & ".global LIB$INITIALIZE#" & LF &
300                  HT & ".section LIB$INITIALIZE#,""a"",@progbits" & LF &
301                  HT & "data4 @fptr(" & Init_Proc & "#)" & LF;
302
303             begin
304                Macro_File := Create_File (Macro_File_Name, Text);
305                OK := Macro_File /= Invalid_FD;
306
307                if OK then
308                   Len := Write
309                     (Macro_File, Lines (Lines'First)'Address,
310                      Lines'Length);
311                   OK := Len = Lines'Length;
312                end if;
313
314                if OK then
315                   Close (Macro_File, OK);
316                end if;
317
318                if not OK then
319                   Fail ("creation of auto-init assembly file """,
320                         Macro_File_Name, """ failed");
321                end if;
322             end;
323
324             --  Invoke the macro-assembler
325
326             if Verbose_Mode then
327                Write_Str ("Assembling auto-init assembly file """);
328                Write_Str (Macro_File_Name);
329                Write_Line ("""");
330             end if;
331
332             Popen_Result := popen (command (command'First)'Address,
333                                    mode (mode'First)'Address);
334
335             if Popen_Result = Null_Address then
336                Fail ("assembly of auto-init assembly file """,
337                      Macro_File_Name, """ failed");
338             end if;
339
340             --  Wait for the end of execution of the macro-assembler
341
342             Pclose_Result := pclose (Popen_Result);
343
344             if Pclose_Result < 0 then
345                Fail ("assembly of auto init assembly file """,
346                      Macro_File_Name, """ failed");
347             end if;
348
349             --  Add the generated object file to the list of objects to be
350             --  included in the library.
351
352             Additional_Objects :=
353               new Argument_List'
354                 (1 => new String'(Lib_Filename & "__init.obj"));
355          end;
356       end if;
357
358       --  Allocate the argument list and put the symbol file name, the
359       --  reference (if any) and the policy (if not autonomous).
360
361       Arguments := new Argument_List (1 .. Ofiles'Length + 8);
362
363       Last_Argument := 0;
364
365       --  Verbosity
366
367       if Verbose_Mode then
368          Last_Argument := Last_Argument + 1;
369          Arguments (Last_Argument) := new String'("-v");
370       end if;
371
372       --  Version number (major ID)
373
374       if Lib_Version /= "" then
375          Last_Argument := Last_Argument + 1;
376          Arguments (Last_Argument) := new String'("-V");
377          Last_Argument := Last_Argument + 1;
378          Arguments (Last_Argument) := new String'(Version);
379       end if;
380
381       --  Symbol file
382
383       Last_Argument := Last_Argument + 1;
384       Arguments (Last_Argument) := new String'("-s");
385       Last_Argument := Last_Argument + 1;
386       Arguments (Last_Argument) := new String'(Opt_File_Name);
387
388       --  Reference Symbol File
389
390       if Symbol_Data.Reference /= No_Path then
391          Last_Argument := Last_Argument + 1;
392          Arguments (Last_Argument) := new String'("-r");
393          Last_Argument := Last_Argument + 1;
394          Arguments (Last_Argument) :=
395            new String'(Get_Name_String (Symbol_Data.Reference));
396       end if;
397
398       --  Policy
399
400       case Symbol_Data.Symbol_Policy is
401          when Autonomous =>
402             null;
403
404          when Compliant =>
405             Last_Argument := Last_Argument + 1;
406             Arguments (Last_Argument) := new String'("-c");
407
408          when Controlled =>
409             Last_Argument := Last_Argument + 1;
410             Arguments (Last_Argument) := new String'("-C");
411
412          when Restricted =>
413             Last_Argument := Last_Argument + 1;
414             Arguments (Last_Argument) := new String'("-R");
415
416          when Direct =>
417             Last_Argument := Last_Argument + 1;
418             Arguments (Last_Argument) := new String'("-D");
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),
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
493             Disregard : Boolean;
494             pragma Warnings (Off, Disregard);
495
496          begin
497             if Verbose_Mode then
498                Write_Str ("deleting auto-init object file """);
499                Write_Str (Auto_Init_Object_File_Name);
500                Write_Line ("""");
501             end if;
502
503             Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
504          end;
505       end if;
506    end Build_Dynamic_Library;
507
508 --  Package initialization
509
510 begin
511    Build_Dynamic_Library_Ptr    := Build_Dynamic_Library'Access;
512 end MLib.Tgt.Specific;