1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- M L I B . T G T . S P E C I F I C --
6 -- (Alpha VMS Version) --
10 -- Copyright (C) 2003-2007, Free Software Foundation, Inc. --
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. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
26 ------------------------------------------------------------------------------
28 -- This is the Alpha VMS version of the body
30 with Ada.Characters.Handling; use Ada.Characters.Handling;
36 pragma Warnings (Off, MLib.Tgt.VMS);
37 -- MLib.Tgt.VMS is with'ed only for elaboration purposes
40 with Output; use Output;
42 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
44 with System; use System;
45 with System.Case_Util; use System.Case_Util;
46 with System.CRTL; use System.CRTL;
48 package body MLib.Tgt.Specific is
50 -- Non default subprogram. See comment in mlib-tgt.ads.
52 procedure Build_Dynamic_Library
53 (Ofiles : Argument_List;
54 Options : Argument_List;
55 Interfaces : Argument_List;
56 Lib_Filename : String;
58 Symbol_Data : Symbol_Record;
59 Driver_Name : Name_Id := No_Name;
60 Lib_Version : String := "";
61 Auto_Init : Boolean := False);
65 Empty_Argument_List : aliased Argument_List := (1 .. 0 => null);
66 Additional_Objects : Argument_List_Access := Empty_Argument_List'Access;
67 -- Used to add the generated auto-init object files for auto-initializing
68 -- stand-alone libraries.
70 Macro_Name : constant String := "mcr gnu:[bin]gcc -c -x assembler";
71 -- The name of the command to invoke the macro-assembler
73 VMS_Options : Argument_List := (1 .. 1 => null);
75 Gnatsym_Name : constant String := "gnatsym";
77 Gnatsym_Path : String_Access;
79 Arguments : Argument_List_Access := null;
80 Last_Argument : Natural := 0;
82 Success : Boolean := False;
84 Shared_Libgcc : aliased String := "-shared-libgcc";
86 Shared_Libgcc_Switch : constant Argument_List :=
87 (1 => Shared_Libgcc'Access);
89 ---------------------------
90 -- Build_Dynamic_Library --
91 ---------------------------
93 procedure Build_Dynamic_Library
94 (Ofiles : Argument_List;
95 Options : Argument_List;
96 Interfaces : Argument_List;
97 Lib_Filename : String;
99 Symbol_Data : Symbol_Record;
100 Driver_Name : Name_Id := No_Name;
101 Lib_Version : String := "";
102 Auto_Init : Boolean := False)
105 Lib_File : constant String :=
106 Lib_Dir & Directory_Separator & "lib" &
107 Fil.Ext_To (Lib_Filename, DLL_Ext);
109 Opts : Argument_List := Options;
110 Last_Opt : Natural := Opts'Last;
111 Opts2 : Argument_List (Options'Range);
112 Last_Opt2 : Natural := Opts2'First - 1;
114 Inter : constant Argument_List := Interfaces;
116 function Is_Interface (Obj_File : String) return Boolean;
117 -- For a Stand-Alone Library, returns True if Obj_File is the object
118 -- file name of an interface of the SAL. For other libraries, always
121 function Option_File_Name return String;
122 -- Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
124 function Version_String return String;
125 -- Returns Lib_Version if not empty and if Symbol_Data.Symbol_Policy is
126 -- not Autonomous, otherwise returns "". When Symbol_Data.Symbol_Policy
127 -- is Autonomous, fails gnatmake if Lib_Version is not the image of a
134 function Is_Interface (Obj_File : String) return Boolean is
135 ALI : constant String :=
137 (Filename => To_Lower (Base_Name (Obj_File)),
141 if Inter'Length = 0 then
144 elsif ALI'Length > 2 and then
145 ALI (ALI'First .. ALI'First + 2) = "b__"
150 for J in Inter'Range loop
151 if Inter (J).all = ALI then
160 ----------------------
161 -- Option_File_Name --
162 ----------------------
164 function Option_File_Name return String is
166 if Symbol_Data.Symbol_File = No_Path then
169 Get_Name_String (Symbol_Data.Symbol_File);
170 To_Lower (Name_Buffer (1 .. Name_Len));
171 return Name_Buffer (1 .. Name_Len);
173 end Option_File_Name;
179 function Version_String return String is
180 Version : Integer := 0;
184 or else Symbol_Data.Symbol_Policy /= Autonomous
190 Version := Integer'Value (Lib_Version);
193 raise Constraint_Error;
199 when Constraint_Error =>
200 Fail ("illegal version """, Lib_Version,
201 """ (on VMS version must be a positive number)");
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
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
223 Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
225 For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
228 new String'("--for-linker=" & Opt_File_Name & "/OPTIONS");
231 VMS_Options (VMS_Options'First) := For_Linker_Opt;
233 for J in Inter'Range loop
234 To_Lower (Inter (J).all);
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");
247 -- For auto-initialization of a stand-alone library, we create
248 -- a macro-assembly file and we invoke the macro-assembler.
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;
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
265 mode : constant String := "r" & ASCII.NUL;
266 -- The mode for the invocation of Popen
269 To_Upper (Init_Proc);
272 Write_Str ("Creating auto-init assembly file """);
273 Write_Str (Macro_File_Name);
277 -- Create and write the auto-init assembly file
280 First_Line : constant String :=
281 ASCII.HT & ".section LIB$INITIALIZE,GBL,NOWRT" &
283 Second_Line : constant String :=
284 ASCII.HT & ".long " & Init_Proc & ASCII.LF;
285 -- First and second lines of the auto-init assembly file
288 Macro_File := Create_File (Macro_File_Name, Text);
289 OK := Macro_File /= Invalid_FD;
293 (Macro_File, First_Line (First_Line'First)'Address,
295 OK := Len = First_Line'Length;
300 (Macro_File, Second_Line (Second_Line'First)'Address,
302 OK := Len = Second_Line'Length;
306 Close (Macro_File, OK);
310 Fail ("creation of auto-init assembly file """,
311 Macro_File_Name, """ failed");
315 -- Invoke the macro-assembler
318 Write_Str ("Assembling auto-init assembly file """);
319 Write_Str (Macro_File_Name);
323 Popen_Result := popen (command (command'First)'Address,
324 mode (mode'First)'Address);
326 if Popen_Result = Null_Address then
327 Fail ("assembly of auto-init assembly file """,
328 Macro_File_Name, """ failed");
331 -- Wait for the end of execution of the macro-assembler
333 Pclose_Result := pclose (Popen_Result);
335 if Pclose_Result < 0 then
336 Fail ("assembly of auto init assembly file """,
337 Macro_File_Name, """ failed");
340 -- Add the generated object file to the list of objects to be
341 -- included in the library.
343 Additional_Objects :=
345 (1 => new String'(Lib_Filename & "__init.obj"));
349 -- Allocate the argument list and put the symbol file name, the
350 -- reference (if any) and the policy (if not autonomous).
352 Arguments := new Argument_List (1 .. Ofiles'Length + 8);
359 Last_Argument := Last_Argument + 1;
360 Arguments (Last_Argument) := new String'("-v");
363 -- Version number (major ID)
365 if Lib_Version /= "" then
366 Last_Argument := Last_Argument + 1;
367 Arguments (Last_Argument) := new String'("-V");
368 Last_Argument := Last_Argument + 1;
369 Arguments (Last_Argument) := new String'(Version);
374 Last_Argument := Last_Argument + 1;
375 Arguments (Last_Argument) := new String'("-s");
376 Last_Argument := Last_Argument + 1;
377 Arguments (Last_Argument) := new String'(Opt_File_Name);
379 -- Reference Symbol File
381 if Symbol_Data.Reference /= No_Path then
382 Last_Argument := Last_Argument + 1;
383 Arguments (Last_Argument) := new String'("-r");
384 Last_Argument := Last_Argument + 1;
385 Arguments (Last_Argument) :=
386 new String'(Get_Name_String (Symbol_Data.Reference));
391 case Symbol_Data.Symbol_Policy is
396 Last_Argument := Last_Argument + 1;
397 Arguments (Last_Argument) := new String'("-c");
400 Last_Argument := Last_Argument + 1;
401 Arguments (Last_Argument) := new String'("-C");
404 Last_Argument := Last_Argument + 1;
405 Arguments (Last_Argument) := new String'("-R");
408 Last_Argument := Last_Argument + 1;
409 Arguments (Last_Argument) := new String'("-D");
413 -- Add each relevant object file
415 for Index in Ofiles'Range loop
416 if Is_Interface (Ofiles (Index).all) then
417 Last_Argument := Last_Argument + 1;
418 Arguments (Last_Argument) := new String'(Ofiles (Index).all);
424 Spawn (Program_Name => Gnatsym_Path.all,
425 Args => Arguments (1 .. Last_Argument),
429 Fail ("unable to create symbol file for library """,
435 -- Move all the -l switches from Opts to Opts2
438 Index : Natural := Opts'First;
442 while Index <= Last_Opt loop
445 if Opt'Length > 2 and then
446 Opt (Opt'First .. Opt'First + 1) = "-l"
448 if Index < Last_Opt then
449 Opts (Index .. Last_Opt - 1) :=
450 Opts (Index + 1 .. Last_Opt);
453 Last_Opt := Last_Opt - 1;
455 Last_Opt2 := Last_Opt2 + 1;
456 Opts2 (Last_Opt2) := Opt;
464 -- Invoke gcc to build the library
467 (Output_File => Lib_File,
468 Objects => Ofiles & Additional_Objects.all,
469 Options => VMS_Options,
470 Options_2 => Shared_Libgcc_Switch &
471 Opts (Opts'First .. Last_Opt) &
472 Opts2 (Opts2'First .. Last_Opt2),
473 Driver_Name => Driver_Name);
475 -- The auto-init object file need to be deleted, so that it will not
476 -- be included in the library as a regular object file, otherwise
477 -- it will be included twice when the library will be built next
478 -- time, which may lead to errors.
482 Auto_Init_Object_File_Name : constant String :=
483 Lib_Filename & "__init.obj";
488 Write_Str ("deleting auto-init object file """);
489 Write_Str (Auto_Init_Object_File_Name);
493 Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
496 end Build_Dynamic_Library;
498 -- Package initialization
501 Build_Dynamic_Library_Ptr := Build_Dynamic_Library'Access;
502 end MLib.Tgt.Specific;