Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / ada / mlib-tgt-specific-vms-alpha.adb
blobc9ffa0d837e14e69fc530fea24f25210e2c090f3
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 ------------------------------------------------------------------------------
27 -- This is the Alpha 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;
181 begin
182 if Lib_Version = ""
183 or else Symbol_Data.Symbol_Policy /= Autonomous
184 then
185 return "";
187 else
188 begin
189 Version := Integer'Value (Lib_Version);
191 if Version <= 0 then
192 raise Constraint_Error;
193 end if;
195 return Lib_Version;
197 exception
198 when Constraint_Error =>
199 Fail ("illegal version """
200 & Lib_Version
201 & """ (on VMS version must be a positive number)");
202 return "";
203 end;
204 end if;
205 end Version_String;
207 ---------------------
208 -- Local Variables --
209 ---------------------
211 Opt_File_Name : constant String := Option_File_Name;
212 Version : constant String := Version_String;
213 For_Linker_Opt : String_Access;
215 -- Start of processing for Build_Dynamic_Library
217 begin
218 -- If option file name does not ends with ".opt", append "/OPTIONS"
219 -- to its specification for the VMS linker.
221 if Opt_File_Name'Length > 4
222 and then
223 Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
224 then
225 For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
226 else
227 For_Linker_Opt :=
228 new String'("--for-linker=" & Opt_File_Name & "/OPTIONS");
229 end if;
231 VMS_Options (VMS_Options'First) := For_Linker_Opt;
233 for J in Inter'Range loop
234 To_Lower (Inter (J).all);
235 end loop;
237 -- "gnatsym" is necessary for building the option file
239 if Gnatsym_Path = null then
240 Gnatsym_Path := Locate_Exec_On_Path (Gnatsym_Name);
242 if Gnatsym_Path = null then
243 Fail (Gnatsym_Name & " not found in path");
244 end if;
245 end if;
247 -- For auto-initialization of a stand-alone library, we create
248 -- a macro-assembly file and we invoke the macro-assembler.
250 if Auto_Init then
251 declare
252 Macro_File_Name : constant String := Lib_Filename & "__init.asm";
253 Macro_File : File_Descriptor;
254 Init_Proc : String := Lib_Filename & "INIT";
255 Popen_Result : System.Address;
256 Pclose_Result : Integer;
257 Len : Natural;
258 OK : Boolean := True;
260 command : constant String :=
261 Macro_Name & " " & Macro_File_Name & ASCII.NUL;
262 -- The command to invoke the assembler on the generated auto-init
263 -- assembly file.
265 mode : constant String := "r" & ASCII.NUL;
266 -- The mode for the invocation of Popen
268 begin
269 To_Upper (Init_Proc);
271 if Verbose_Mode then
272 Write_Str ("Creating auto-init assembly file """);
273 Write_Str (Macro_File_Name);
274 Write_Line ("""");
275 end if;
277 -- Create and write the auto-init assembly file
279 declare
280 use ASCII;
282 -- Output a dummy transfer address for debugging
283 -- followed by the LIB$INITIALIZE section.
285 Lines : constant String :=
286 HT & ".text" & LF &
287 HT & ".align 4" & LF &
288 HT & ".globl __main" & LF &
289 HT & ".ent __main" & LF &
290 "__main..en:" & LF &
291 HT & ".base $27" & LF &
292 HT & ".frame $29,0,$26,8" & LF &
293 HT & "ret $31,($26),1" & LF &
294 HT & ".link" & LF &
295 "__main:" & LF &
296 HT & ".pdesc __main..en,null" & LF &
297 HT & ".end __main" & LF & LF &
298 HT & ".section LIB$INITIALIZE,GBL,NOWRT" & LF &
299 HT & ".long " & Init_Proc & LF;
301 begin
302 Macro_File := Create_File (Macro_File_Name, Text);
303 OK := Macro_File /= Invalid_FD;
305 if OK then
306 Len := Write
307 (Macro_File, Lines (Lines'First)'Address,
308 Lines'Length);
309 OK := Len = Lines'Length;
310 end if;
312 if OK then
313 Close (Macro_File, OK);
314 end if;
316 if not OK then
317 Fail ("creation of auto-init assembly file """
318 & Macro_File_Name
319 & """ failed");
320 end if;
321 end;
323 -- Invoke the macro-assembler
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;
331 Popen_Result := popen (command (command'First)'Address,
332 mode (mode'First)'Address);
334 if Popen_Result = Null_Address then
335 Fail ("assembly of auto-init assembly file """
336 & Macro_File_Name
337 & """ failed");
338 end if;
340 -- Wait for the end of execution of the macro-assembler
342 Pclose_Result := pclose (Popen_Result);
344 if Pclose_Result < 0 then
345 Fail ("assembly of auto init assembly file """
346 & Macro_File_Name
347 & """ failed");
348 end if;
350 -- Add the generated object file to the list of objects to be
351 -- included in the library.
353 Additional_Objects :=
354 new Argument_List'
355 (1 => new String'(Lib_Filename & "__init.obj"));
356 end;
357 end if;
359 -- Allocate the argument list and put the symbol file name, the
360 -- reference (if any) and the policy (if not autonomous).
362 Arguments := new Argument_List (1 .. Ofiles'Length + 8);
364 Last_Argument := 0;
366 -- Verbosity
368 if Verbose_Mode then
369 Last_Argument := Last_Argument + 1;
370 Arguments (Last_Argument) := new String'("-v");
371 end if;
373 -- Version number (major ID)
375 if Lib_Version /= "" then
376 Last_Argument := Last_Argument + 1;
377 Arguments (Last_Argument) := new String'("-V");
378 Last_Argument := Last_Argument + 1;
379 Arguments (Last_Argument) := new String'(Version);
380 end if;
382 -- Symbol file
384 Last_Argument := Last_Argument + 1;
385 Arguments (Last_Argument) := new String'("-s");
386 Last_Argument := Last_Argument + 1;
387 Arguments (Last_Argument) := new String'(Opt_File_Name);
389 -- Reference Symbol File
391 if Symbol_Data.Reference /= No_Path then
392 Last_Argument := Last_Argument + 1;
393 Arguments (Last_Argument) := new String'("-r");
394 Last_Argument := Last_Argument + 1;
395 Arguments (Last_Argument) :=
396 new String'(Get_Name_String (Symbol_Data.Reference));
397 end if;
399 -- Policy
401 case Symbol_Data.Symbol_Policy is
402 when Autonomous =>
403 null;
405 when Compliant =>
406 Last_Argument := Last_Argument + 1;
407 Arguments (Last_Argument) := new String'("-c");
409 when Controlled =>
410 Last_Argument := Last_Argument + 1;
411 Arguments (Last_Argument) := new String'("-C");
413 when Restricted =>
414 Last_Argument := Last_Argument + 1;
415 Arguments (Last_Argument) := new String'("-R");
417 when Direct =>
418 Last_Argument := Last_Argument + 1;
419 Arguments (Last_Argument) := new String'("-D");
421 end case;
423 -- Add each relevant object file
425 for Index in Ofiles'Range loop
426 if Is_Interface (Ofiles (Index).all) then
427 Last_Argument := Last_Argument + 1;
428 Arguments (Last_Argument) := new String'(Ofiles (Index).all);
429 end if;
430 end loop;
432 -- Spawn gnatsym
434 Spawn (Program_Name => Gnatsym_Path.all,
435 Args => Arguments (1 .. Last_Argument),
436 Success => Success);
438 if not Success then
439 Fail ("unable to create symbol file for library """
440 & Lib_Filename
441 & """");
442 end if;
444 Free (Arguments);
446 -- Move all the -l switches from Opts to Opts2
448 declare
449 Index : Natural := Opts'First;
450 Opt : String_Access;
452 begin
453 while Index <= Last_Opt loop
454 Opt := Opts (Index);
456 if Opt'Length > 2 and then
457 Opt (Opt'First .. Opt'First + 1) = "-l"
458 then
459 if Index < Last_Opt then
460 Opts (Index .. Last_Opt - 1) :=
461 Opts (Index + 1 .. Last_Opt);
462 end if;
464 Last_Opt := Last_Opt - 1;
466 Last_Opt2 := Last_Opt2 + 1;
467 Opts2 (Last_Opt2) := Opt;
469 else
470 Index := Index + 1;
471 end if;
472 end loop;
473 end;
475 -- Invoke gcc to build the library
477 Utl.Gcc
478 (Output_File => Lib_File,
479 Objects => Ofiles & Additional_Objects.all,
480 Options => VMS_Options,
481 Options_2 => Shared_Libgcc_Switch &
482 Opts (Opts'First .. Last_Opt) &
483 Opts2 (Opts2'First .. Last_Opt2),
484 Driver_Name => Driver_Name);
486 -- The auto-init object file need to be deleted, so that it will not
487 -- be included in the library as a regular object file, otherwise
488 -- it will be included twice when the library will be built next
489 -- time, which may lead to errors.
491 if Auto_Init then
492 declare
493 Auto_Init_Object_File_Name : constant String :=
494 Lib_Filename & "__init.obj";
495 Disregard : Boolean;
497 begin
498 if Verbose_Mode then
499 Write_Str ("deleting auto-init object file """);
500 Write_Str (Auto_Init_Object_File_Name);
501 Write_Line ("""");
502 end if;
504 Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
505 end;
506 end if;
507 end Build_Dynamic_Library;
509 -- Package initialization
511 begin
512 Build_Dynamic_Library_Ptr := Build_Dynamic_Library'Access;
513 end MLib.Tgt.Specific;