2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / mlib-tgt-specific-vms-ia64.adb
blobbaa8ce213f1d5c1982c77707db71d52752c2b73c
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 ------------------------------------------------------------------------------
27 -- This is the Integrity VMS version of the body
29 with Ada.Characters.Handling; use Ada.Characters.Handling;
31 with MLib.Fil;
32 with MLib.Utl;
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
38 with Opt; use Opt;
39 with Output; use Output;
41 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
43 with System; use System;
44 with System.Case_Util; use System.Case_Util;
45 with System.CRTL; use System.CRTL;
47 package body MLib.Tgt.Specific is
49 -- Non default subprogram. See comment in mlib-tgt.ads.
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);
62 -- Local variables
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.
69 Macro_Name : constant String := "mcr gnu:[bin]gcc -c -x assembler";
70 -- The name of the command to invoke the macro-assembler
72 VMS_Options : Argument_List := (1 .. 1 => null);
74 Gnatsym_Name : constant String := "gnatsym";
76 Gnatsym_Path : String_Access;
78 Arguments : Argument_List_Access := null;
79 Last_Argument : Natural := 0;
81 Success : Boolean := False;
83 Shared_Libgcc : aliased String := "-shared-libgcc";
85 Shared_Libgcc_Switch : constant Argument_List :=
86 (1 => Shared_Libgcc'Access);
88 ---------------------------
89 -- Build_Dynamic_Library --
90 ---------------------------
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)
104 Lib_File : constant String :=
105 Lib_Dir & Directory_Separator & "lib" &
106 Fil.Ext_To (Lib_Filename, DLL_Ext);
108 Opts : Argument_List := Options;
109 Last_Opt : Natural := Opts'Last;
110 Opts2 : Argument_List (Options'Range);
111 Last_Opt2 : Natural := Opts2'First - 1;
113 Inter : constant Argument_List := Interfaces;
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.
120 function Option_File_Name return String;
121 -- Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
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.
129 ------------------
130 -- Is_Interface --
131 ------------------
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");
139 begin
140 if Inter'Length = 0 then
141 return True;
143 elsif ALI'Length > 2 and then
144 ALI (ALI'First .. ALI'First + 2) = "b__"
145 then
146 return True;
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;
155 return False;
156 end if;
157 end Is_Interface;
159 ----------------------
160 -- Option_File_Name --
161 ----------------------
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;
174 --------------------
175 -- Version_String --
176 --------------------
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 "";
186 else
187 begin
188 Version := Integer'Value (Lib_Version);
190 if Version <= 0 then
191 raise Constraint_Error;
192 end if;
194 return Lib_Version;
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;
205 ---------------------
206 -- Local Variables --
207 ---------------------
209 Opt_File_Name : constant String := Option_File_Name;
210 Version : constant String := Version_String;
211 For_Linker_Opt : String_Access;
213 -- Start of processing for Build_Dynamic_Library
215 begin
216 -- Option file must end with ".opt"
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;
227 VMS_Options (VMS_Options'First) := For_Linker_Opt;
229 for J in Inter'Range loop
230 To_Lower (Inter (J).all);
231 end loop;
233 -- "gnatsym" is necessary for building the option file
235 if Gnatsym_Path = null then
236 Gnatsym_Path := Locate_Exec_On_Path (Gnatsym_Name);
238 if Gnatsym_Path = null then
239 Fail (Gnatsym_Name, " not found in path");
240 end if;
241 end if;
243 -- For auto-initialization of a stand-alone library, we create
244 -- a macro-assembly file and we invoke the macro-assembler.
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;
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 ???
262 mode : constant String := "r" & ASCII.NUL;
263 -- The mode for the invocation of Popen
264 -- Why odd lower case name ???
266 begin
267 To_Upper (Init_Proc);
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;
275 -- Create and write the auto-init assembly file
277 declare
278 First_Line : constant String :=
279 ASCII.HT
280 & ".type " & Init_Proc & "#, @function"
281 & ASCII.LF;
282 Second_Line : constant String :=
283 ASCII.HT
284 & ".global " & Init_Proc & "#"
285 & ASCII.LF;
286 Third_Line : constant String :=
287 ASCII.HT
288 & ".global LIB$INITIALIZE#"
289 & ASCII.LF;
290 Fourth_Line : constant String :=
291 ASCII.HT
292 & ".section LIB$INITIALIZE#,""a"",@progbits"
293 & ASCII.LF;
294 Fifth_Line : constant String :=
295 ASCII.HT
296 & "data4 @fptr(" & Init_Proc & "#)"
297 & ASCII.LF;
299 begin
300 Macro_File := Create_File (Macro_File_Name, Text);
301 OK := Macro_File /= Invalid_FD;
303 if OK then
304 Len := Write
305 (Macro_File, First_Line (First_Line'First)'Address,
306 First_Line'Length);
307 OK := Len = First_Line'Length;
308 end if;
310 if OK then
311 Len := Write
312 (Macro_File, Second_Line (Second_Line'First)'Address,
313 Second_Line'Length);
314 OK := Len = Second_Line'Length;
315 end if;
317 if OK then
318 Len := Write
319 (Macro_File, Third_Line (Third_Line'First)'Address,
320 Third_Line'Length);
321 OK := Len = Third_Line'Length;
322 end if;
324 if OK then
325 Len := Write
326 (Macro_File, Fourth_Line (Fourth_Line'First)'Address,
327 Fourth_Line'Length);
328 OK := Len = Fourth_Line'Length;
329 end if;
331 if OK then
332 Len := Write
333 (Macro_File, Fifth_Line (Fifth_Line'First)'Address,
334 Fifth_Line'Length);
335 OK := Len = Fifth_Line'Length;
336 end if;
338 if OK then
339 Close (Macro_File, OK);
340 end if;
342 if not OK then
343 Fail ("creation of auto-init assembly file """,
344 Macro_File_Name, """ failed");
345 end if;
346 end;
348 -- Invoke the macro-assembler
350 if Verbose_Mode then
351 Write_Str ("Assembling auto-init assembly file """);
352 Write_Str (Macro_File_Name);
353 Write_Line ("""");
354 end if;
356 Popen_Result := popen (command (command'First)'Address,
357 mode (mode'First)'Address);
359 if Popen_Result = Null_Address then
360 Fail ("assembly of auto-init assembly file """,
361 Macro_File_Name, """ failed");
362 end if;
364 -- Wait for the end of execution of the macro-assembler
366 Pclose_Result := pclose (Popen_Result);
368 if Pclose_Result < 0 then
369 Fail ("assembly of auto init assembly file """,
370 Macro_File_Name, """ failed");
371 end if;
373 -- Add the generated object file to the list of objects to be
374 -- included in the library.
376 Additional_Objects :=
377 new Argument_List'
378 (1 => new String'(Lib_Filename & "__init.obj"));
379 end;
380 end if;
382 -- Allocate the argument list and put the symbol file name, the
383 -- reference (if any) and the policy (if not autonomous).
385 Arguments := new Argument_List (1 .. Ofiles'Length + 8);
387 Last_Argument := 0;
389 -- Verbosity
391 if Verbose_Mode then
392 Last_Argument := Last_Argument + 1;
393 Arguments (Last_Argument) := new String'("-v");
394 end if;
396 -- Version number (major ID)
398 if Lib_Version /= "" then
399 Last_Argument := Last_Argument + 1;
400 Arguments (Last_Argument) := new String'("-V");
401 Last_Argument := Last_Argument + 1;
402 Arguments (Last_Argument) := new String'(Version);
403 end if;
405 -- Symbol file
407 Last_Argument := Last_Argument + 1;
408 Arguments (Last_Argument) := new String'("-s");
409 Last_Argument := Last_Argument + 1;
410 Arguments (Last_Argument) := new String'(Opt_File_Name);
412 -- Reference Symbol File
414 if Symbol_Data.Reference /= No_Path then
415 Last_Argument := Last_Argument + 1;
416 Arguments (Last_Argument) := new String'("-r");
417 Last_Argument := Last_Argument + 1;
418 Arguments (Last_Argument) :=
419 new String'(Get_Name_String (Symbol_Data.Reference));
420 end if;
422 -- Policy
424 case Symbol_Data.Symbol_Policy is
425 when Autonomous =>
426 null;
428 when Compliant =>
429 Last_Argument := Last_Argument + 1;
430 Arguments (Last_Argument) := new String'("-c");
432 when Controlled =>
433 Last_Argument := Last_Argument + 1;
434 Arguments (Last_Argument) := new String'("-C");
436 when Restricted =>
437 Last_Argument := Last_Argument + 1;
438 Arguments (Last_Argument) := new String'("-R");
440 when Direct =>
441 Last_Argument := Last_Argument + 1;
442 Arguments (Last_Argument) := new String'("-D");
443 end case;
445 -- Add each relevant object file
447 for Index in Ofiles'Range loop
448 if Is_Interface (Ofiles (Index).all) then
449 Last_Argument := Last_Argument + 1;
450 Arguments (Last_Argument) := new String'(Ofiles (Index).all);
451 end if;
452 end loop;
454 -- Spawn gnatsym
456 Spawn (Program_Name => Gnatsym_Path.all,
457 Args => Arguments (1 .. Last_Argument),
458 Success => Success);
460 if not Success then
461 Fail ("unable to create symbol file for library """,
462 Lib_Filename, """");
463 end if;
465 Free (Arguments);
467 -- Move all the -l switches from Opts to Opts2
469 declare
470 Index : Natural := Opts'First;
471 Opt : String_Access;
473 begin
474 while Index <= Last_Opt loop
475 Opt := Opts (Index);
477 if Opt'Length > 2 and then
478 Opt (Opt'First .. Opt'First + 1) = "-l"
479 then
480 if Index < Last_Opt then
481 Opts (Index .. Last_Opt - 1) :=
482 Opts (Index + 1 .. Last_Opt);
483 end if;
485 Last_Opt := Last_Opt - 1;
487 Last_Opt2 := Last_Opt2 + 1;
488 Opts2 (Last_Opt2) := Opt;
490 else
491 Index := Index + 1;
492 end if;
493 end loop;
494 end;
496 -- Invoke gcc to build the library
498 Utl.Gcc
499 (Output_File => Lib_File,
500 Objects => Ofiles & Additional_Objects.all,
501 Options => VMS_Options,
502 Options_2 => Shared_Libgcc_Switch &
503 Opts (Opts'First .. Last_Opt) &
504 Opts2 (Opts2'First .. Last_Opt2),
505 Driver_Name => Driver_Name);
507 -- The auto-init object file need to be deleted, so that it will not
508 -- be included in the library as a regular object file, otherwise
509 -- it will be included twice when the library will be built next
510 -- time, which may lead to errors.
512 if Auto_Init then
513 declare
514 Auto_Init_Object_File_Name : constant String :=
515 Lib_Filename & "__init.obj";
517 Disregard : Boolean;
518 pragma Warnings (Off, Disregard);
520 begin
521 if Verbose_Mode then
522 Write_Str ("deleting auto-init object file """);
523 Write_Str (Auto_Init_Object_File_Name);
524 Write_Line ("""");
525 end if;
527 Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
528 end;
529 end if;
530 end Build_Dynamic_Library;
532 -- Package initialization
534 begin
535 Build_Dynamic_Library_Ptr := Build_Dynamic_Library'Access;
536 end MLib.Tgt.Specific;