1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
6 -- (Integrity VMS Version) --
10 -- Copyright (C) 2004, 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, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, 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 Integrity VMS version of the body
30 with Ada.Characters.Handling; use Ada.Characters.Handling;
32 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
33 with GNAT.OS_Lib; use GNAT.OS_Lib;
37 with Namet; use Namet;
39 with Output; use Output;
41 with System; use System;
42 with System.Case_Util; use System.Case_Util;
44 package body MLib.Tgt is
48 Empty_Argument_List : aliased Argument_List := (1 .. 0 => null);
49 Additional_Objects : Argument_List_Access := Empty_Argument_List'Access;
50 -- Used to add the generated auto-init object files for auto-initializing
51 -- stand-alone libraries.
53 Macro_Name : constant String := "mcr gnu:[bin]gcc -c -x assembler";
54 -- The name of the command to invoke the macro-assembler
56 VMS_Options : Argument_List := (1 .. 1 => null);
58 Gnatsym_Name : constant String := "gnatsym";
60 Gnatsym_Path : String_Access;
62 Arguments : Argument_List_Access := null;
63 Last_Argument : Natural := 0;
65 Success : Boolean := False;
67 Shared_Libgcc : aliased String := "-shared-libgcc";
69 No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
70 Shared_Libgcc_Switch : aliased Argument_List :=
71 (1 => Shared_Libgcc'Access);
72 Link_With_Shared_Libgcc : Argument_List_Access :=
73 No_Shared_Libgcc_Switch'Access;
75 ------------------------------
76 -- Target dependent section --
77 ------------------------------
79 function Popen (Command, Mode : System.Address) return System.Address;
80 pragma Import (C, Popen);
82 function Pclose (File : System.Address) return Integer;
83 pragma Import (C, Pclose);
89 function Archive_Builder return String is
94 -----------------------------
95 -- Archive_Builder_Options --
96 -----------------------------
98 function Archive_Builder_Options return String_List_Access is
100 return new String_List'(1 => new String'("cr"));
101 end Archive_Builder_Options;
107 function Archive_Ext return String is
112 ---------------------
113 -- Archive_Indexer --
114 ---------------------
116 function Archive_Indexer return String is
121 ---------------------------
122 -- Build_Dynamic_Library --
123 ---------------------------
125 procedure Build_Dynamic_Library
126 (Ofiles : Argument_List;
127 Foreign : Argument_List;
128 Afiles : Argument_List;
129 Options : Argument_List;
130 Options_2 : Argument_List;
131 Interfaces : Argument_List;
132 Lib_Filename : String;
134 Symbol_Data : Symbol_Record;
135 Driver_Name : Name_Id := No_Name;
136 Lib_Version : String := "";
137 Auto_Init : Boolean := False)
139 pragma Unreferenced (Foreign);
140 pragma Unreferenced (Afiles);
142 Lib_File : constant String :=
143 Lib_Dir & Directory_Separator & "lib" &
144 Fil.Ext_To (Lib_Filename, DLL_Ext);
146 Opts : Argument_List := Options;
147 Last_Opt : Natural := Opts'Last;
148 Opts2 : Argument_List (Options'Range);
149 Last_Opt2 : Natural := Opts2'First - 1;
151 Inter : constant Argument_List := Interfaces;
153 function Is_Interface (Obj_File : String) return Boolean;
154 -- For a Stand-Alone Library, returns True if Obj_File is the object
155 -- file name of an interface of the SAL. For other libraries, always
158 function Option_File_Name return String;
159 -- Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
161 function Version_String return String;
162 -- Returns Lib_Version if not empty, otherwise returns "1".
163 -- Fails gnatmake if Lib_Version is not the image of a positive number.
169 function Is_Interface (Obj_File : String) return Boolean is
170 ALI : constant String :=
172 (Filename => To_Lower (Base_Name (Obj_File)),
176 if Inter'Length = 0 then
179 elsif ALI'Length > 2 and then
180 ALI (ALI'First .. ALI'First + 1) = "b$"
185 for J in Inter'Range loop
186 if Inter (J).all = ALI then
195 ----------------------
196 -- Option_File_Name --
197 ----------------------
199 function Option_File_Name return String is
201 if Symbol_Data.Symbol_File = No_Name then
204 Get_Name_String (Symbol_Data.Symbol_File);
205 To_Lower (Name_Buffer (1 .. Name_Len));
206 return Name_Buffer (1 .. Name_Len);
208 end Option_File_Name;
214 function Version_String return String is
215 Version : Integer := 0;
217 if Lib_Version = "" then
222 Version := Integer'Value (Lib_Version);
225 raise Constraint_Error;
231 when Constraint_Error =>
232 Fail ("illegal version """, Lib_Version,
233 """ (on VMS version must be a positive number)");
239 Opt_File_Name : constant String := Option_File_Name;
240 Version : constant String := Version_String;
241 For_Linker_Opt : String_Access;
243 -- Start of processing for Build_Dynamic_Library
246 -- Invoke gcc with -shared-libgcc, but only for GCC 3 or higher
248 if GCC_Version >= 3 then
249 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
251 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
254 -- Option file must end with ".opt"
256 if Opt_File_Name'Length > 4
258 Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
260 For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
262 Fail ("Options File """, Opt_File_Name, """ must end with .opt");
265 VMS_Options (VMS_Options'First) := For_Linker_Opt;
267 for J in Inter'Range loop
268 To_Lower (Inter (J).all);
271 -- "gnatsym" is necessary for building the option file
273 if Gnatsym_Path = null then
274 Gnatsym_Path := OS_Lib.Locate_Exec_On_Path (Gnatsym_Name);
276 if Gnatsym_Path = null then
277 Fail (Gnatsym_Name, " not found in path");
281 -- For auto-initialization of a stand-alone library, we create
282 -- a macro-assembly file and we invoke the macro-assembler.
286 Macro_File_Name : constant String := Lib_Filename & "$init.asm";
287 Macro_File : File_Descriptor;
288 Init_Proc : String := Lib_Filename & "INIT";
289 Popen_Result : System.Address;
290 Pclose_Result : Integer;
292 OK : Boolean := True;
294 Command : constant String :=
295 Macro_Name & " " & Macro_File_Name & ASCII.NUL;
296 -- The command to invoke the assembler on the generated auto-init
299 Mode : constant String := "r" & ASCII.NUL;
300 -- The mode for the invocation of Popen
303 To_Upper (Init_Proc);
306 Write_Str ("Creating auto-init assembly file """);
307 Write_Str (Macro_File_Name);
311 -- Create and write the auto-init assembly file
314 First_Line : constant String :=
316 ".type " & Init_Proc & "#, @function" &
318 Second_Line : constant String :=
320 ".global " & Init_Proc & "#" &
322 Third_Line : constant String :=
324 ".global LIB$INITIALIZE#" &
326 Fourth_Line : constant String :=
328 ".section LIB$INITIALIZE#,""a"",@progbits" &
330 Fifth_Line : constant String :=
332 "data4 @fptr(" & Init_Proc & "#)" &
336 Macro_File := Create_File (Macro_File_Name, Text);
337 OK := Macro_File /= Invalid_FD;
341 (Macro_File, First_Line (First_Line'First)'Address,
343 OK := Len = First_Line'Length;
348 (Macro_File, Second_Line (Second_Line'First)'Address,
350 OK := Len = Second_Line'Length;
355 (Macro_File, Third_Line (Third_Line'First)'Address,
357 OK := Len = Third_Line'Length;
362 (Macro_File, Fourth_Line (Fourth_Line'First)'Address,
364 OK := Len = Fourth_Line'Length;
369 (Macro_File, Fifth_Line (Fifth_Line'First)'Address,
371 OK := Len = Fifth_Line'Length;
375 Close (Macro_File, OK);
379 Fail ("creation of auto-init assembly file """,
380 Macro_File_Name, """ failed");
384 -- Invoke the macro-assembler
387 Write_Str ("Assembling auto-init assembly file """);
388 Write_Str (Macro_File_Name);
392 Popen_Result := Popen (Command (Command'First)'Address,
393 Mode (Mode'First)'Address);
395 if Popen_Result = Null_Address then
396 Fail ("assembly of auto-init assembly file """,
397 Macro_File_Name, """ failed");
400 -- Wait for the end of execution of the macro-assembler
402 Pclose_Result := Pclose (Popen_Result);
404 if Pclose_Result < 0 then
405 Fail ("assembly of auto init assembly file """,
406 Macro_File_Name, """ failed");
409 -- Add the generated object file to the list of objects to be
410 -- included in the library.
412 Additional_Objects :=
414 (1 => new String'(Lib_Filename & "$init.obj"));
418 -- Allocate the argument list and put the symbol file name, the
419 -- reference (if any) and the policy (if not autonomous).
421 Arguments := new Argument_List (1 .. Ofiles'Length + 8);
428 Last_Argument := Last_Argument + 1;
429 Arguments (Last_Argument) := new String'("-v");
432 -- Version number (major ID)
434 if Lib_Version /= "" then
435 Last_Argument := Last_Argument + 1;
436 Arguments (Last_Argument) := new String'("-V");
437 Last_Argument := Last_Argument + 1;
438 Arguments (Last_Argument) := new String'(Version);
443 Last_Argument := Last_Argument + 1;
444 Arguments (Last_Argument) := new String'("-s");
445 Last_Argument := Last_Argument + 1;
446 Arguments (Last_Argument) := new String'(Opt_File_Name);
448 -- Reference Symbol File
450 if Symbol_Data.Reference /= No_Name then
451 Last_Argument := Last_Argument + 1;
452 Arguments (Last_Argument) := new String'("-r");
453 Last_Argument := Last_Argument + 1;
454 Arguments (Last_Argument) :=
455 new String'(Get_Name_String (Symbol_Data.Reference));
460 case Symbol_Data.Symbol_Policy is
465 Last_Argument := Last_Argument + 1;
466 Arguments (Last_Argument) := new String'("-c");
469 Last_Argument := Last_Argument + 1;
470 Arguments (Last_Argument) := new String'("-C");
473 Last_Argument := Last_Argument + 1;
474 Arguments (Last_Argument) := new String'("-R");
477 -- Add each relevant object file
479 for Index in Ofiles'Range loop
480 if Is_Interface (Ofiles (Index).all) then
481 Last_Argument := Last_Argument + 1;
482 Arguments (Last_Argument) := new String'(Ofiles (Index).all);
488 Spawn (Program_Name => Gnatsym_Path.all,
489 Args => Arguments (1 .. Last_Argument),
493 Fail ("unable to create symbol file for library """,
499 -- Move all the -l switches from Opts to Opts2
502 Index : Natural := Opts'First;
506 while Index <= Last_Opt loop
509 if Opt'Length > 2 and then
510 Opt (Opt'First .. Opt'First + 1) = "-l"
512 if Index < Last_Opt then
513 Opts (Index .. Last_Opt - 1) :=
514 Opts (Index + 1 .. Last_Opt);
517 Last_Opt := Last_Opt - 1;
519 Last_Opt2 := Last_Opt2 + 1;
520 Opts2 (Last_Opt2) := Opt;
528 -- Invoke gcc to build the library
531 (Output_File => Lib_File,
532 Objects => Ofiles & Additional_Objects.all,
533 Options => VMS_Options,
534 Options_2 => Link_With_Shared_Libgcc.all &
535 Opts (Opts'First .. Last_Opt) &
536 Opts2 (Opts2'First .. Last_Opt2) & Options_2,
537 Driver_Name => Driver_Name);
539 -- The auto-init object file need to be deleted, so that it will not
540 -- be included in the library as a regular object file, otherwise
541 -- it will be included twice when the library will be built next
542 -- time, which may lead to errors.
546 Auto_Init_Object_File_Name : constant String :=
547 Lib_Filename & "$init.obj";
552 Write_Str ("deleting auto-init object file """);
553 Write_Str (Auto_Init_Object_File_Name);
557 Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
560 end Build_Dynamic_Library;
566 function DLL_Ext return String is
575 function Dynamic_Option return String is
584 function Is_Object_Ext (Ext : String) return Boolean is
593 function Is_C_Ext (Ext : String) return Boolean is
602 function Is_Archive_Ext (Ext : String) return Boolean is
604 return Ext = ".olb" or else Ext = ".exe";
611 function Libgnat return String is
612 Libgnat_A : constant String := "libgnat.a";
613 Libgnat_Olb : constant String := "libgnat.olb";
616 Name_Len := Libgnat_A'Length;
617 Name_Buffer (1 .. Name_Len) := Libgnat_A;
619 if Osint.Find_File (Name_Enter, Osint.Library) /= No_File then
627 ------------------------
628 -- Library_Exists_For --
629 ------------------------
631 function Library_Exists_For (Project : Project_Id) return Boolean is
633 if not Projects.Table (Project).Library then
634 Fail ("INTERNAL ERROR: Library_Exists_For called " &
635 "for non library project");
640 Lib_Dir : constant String :=
641 Get_Name_String (Projects.Table (Project).Library_Dir);
642 Lib_Name : constant String :=
643 Get_Name_String (Projects.Table (Project).Library_Name);
646 if Projects.Table (Project).Library_Kind = Static then
647 return Is_Regular_File
648 (Lib_Dir & Directory_Separator & "lib" &
649 Fil.Ext_To (Lib_Name, Archive_Ext));
652 return Is_Regular_File
653 (Lib_Dir & Directory_Separator & "lib" &
654 Fil.Ext_To (Lib_Name, DLL_Ext));
658 end Library_Exists_For;
660 ---------------------------
661 -- Library_File_Name_For --
662 ---------------------------
664 function Library_File_Name_For (Project : Project_Id) return Name_Id is
666 if not Projects.Table (Project).Library then
667 Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
668 "for non library project");
673 Lib_Name : constant String :=
674 Get_Name_String (Projects.Table (Project).Library_Name);
678 Name_Buffer (1 .. Name_Len) := "lib";
680 if Projects.Table (Project).Library_Kind = Static then
681 Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
684 Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
690 end Library_File_Name_For;
696 function Object_Ext return String is
705 function PIC_Option return String is
710 -----------------------------------------------
711 -- Standalone_Library_Auto_Init_Is_Supported --
712 -----------------------------------------------
714 function Standalone_Library_Auto_Init_Is_Supported return Boolean is
717 end Standalone_Library_Auto_Init_Is_Supported;
719 ---------------------------
720 -- Support_For_Libraries --
721 ---------------------------
723 function Support_For_Libraries return Library_Support is
726 end Support_For_Libraries;