1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
6 -- (Alpha VMS Version) --
10 -- Copyright (C) 2003-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 Alpha 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 -- Archive_Indexer_Options --
123 -----------------------------
125 function Archive_Indexer_Options return String_List_Access is
127 return new String_List (1 .. 0);
128 end Archive_Indexer_Options;
130 ---------------------------
131 -- Build_Dynamic_Library --
132 ---------------------------
134 procedure Build_Dynamic_Library
135 (Ofiles : Argument_List;
136 Foreign : Argument_List;
137 Afiles : Argument_List;
138 Options : Argument_List;
139 Options_2 : Argument_List;
140 Interfaces : Argument_List;
141 Lib_Filename : String;
143 Symbol_Data : Symbol_Record;
144 Driver_Name : Name_Id := No_Name;
145 Lib_Version : String := "";
146 Auto_Init : Boolean := False)
148 pragma Unreferenced (Foreign);
149 pragma Unreferenced (Afiles);
151 Lib_File : constant String :=
152 Lib_Dir & Directory_Separator & "lib" &
153 Fil.Ext_To (Lib_Filename, DLL_Ext);
155 Opts : Argument_List := Options;
156 Last_Opt : Natural := Opts'Last;
157 Opts2 : Argument_List (Options'Range);
158 Last_Opt2 : Natural := Opts2'First - 1;
160 Inter : constant Argument_List := Interfaces;
162 function Is_Interface (Obj_File : String) return Boolean;
163 -- For a Stand-Alone Library, returns True if Obj_File is the object
164 -- file name of an interface of the SAL. For other libraries, always
167 function Option_File_Name return String;
168 -- Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
170 function Version_String return String;
171 -- Returns Lib_Version if not empty, otherwise returns "1".
172 -- Fails gnatmake if Lib_Version is not the image of a positive number.
178 function Is_Interface (Obj_File : String) return Boolean is
179 ALI : constant String :=
181 (Filename => To_Lower (Base_Name (Obj_File)),
185 if Inter'Length = 0 then
188 elsif ALI'Length > 2 and then
189 ALI (ALI'First .. ALI'First + 1) = "b$"
194 for J in Inter'Range loop
195 if Inter (J).all = ALI then
204 ----------------------
205 -- Option_File_Name --
206 ----------------------
208 function Option_File_Name return String is
210 if Symbol_Data.Symbol_File = No_Name then
213 Get_Name_String (Symbol_Data.Symbol_File);
214 To_Lower (Name_Buffer (1 .. Name_Len));
215 return Name_Buffer (1 .. Name_Len);
217 end Option_File_Name;
223 function Version_String return String is
224 Version : Integer := 0;
226 if Lib_Version = "" then
231 Version := Integer'Value (Lib_Version);
234 raise Constraint_Error;
240 when Constraint_Error =>
241 Fail ("illegal version """, Lib_Version,
242 """ (on VMS version must be a positive number)");
248 Opt_File_Name : constant String := Option_File_Name;
249 Version : constant String := Version_String;
250 For_Linker_Opt : String_Access;
252 -- Start of processing for Build_Dynamic_Library
255 -- Invoke gcc with -shared-libgcc, but only for GCC 3 or higher
257 if GCC_Version >= 3 then
258 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
260 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
263 -- If option file name does not ends with ".opt", append "/OPTIONS"
264 -- to its specification for the VMS linker.
266 if Opt_File_Name'Length > 4
268 Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
270 For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
273 new String'("--for-linker=" & Opt_File_Name & "/OPTIONS");
276 VMS_Options (VMS_Options'First) := For_Linker_Opt;
278 for J in Inter'Range loop
279 To_Lower (Inter (J).all);
282 -- "gnatsym" is necessary for building the option file
284 if Gnatsym_Path = null then
285 Gnatsym_Path := OS_Lib.Locate_Exec_On_Path (Gnatsym_Name);
287 if Gnatsym_Path = null then
288 Fail (Gnatsym_Name, " not found in path");
292 -- For auto-initialization of a stand-alone library, we create
293 -- a macro-assembly file and we invoke the macro-assembler.
297 Macro_File_Name : constant String := Lib_Filename & "$init.asm";
298 Macro_File : File_Descriptor;
299 Init_Proc : String := Lib_Filename & "INIT";
300 Popen_Result : System.Address;
301 Pclose_Result : Integer;
303 OK : Boolean := True;
305 Command : constant String :=
306 Macro_Name & " " & Macro_File_Name & ASCII.NUL;
307 -- The command to invoke the assembler on the generated auto-init
310 Mode : constant String := "r" & ASCII.NUL;
311 -- The mode for the invocation of Popen
314 To_Upper (Init_Proc);
317 Write_Str ("Creating auto-init assembly file """);
318 Write_Str (Macro_File_Name);
322 -- Create and write the auto-init assembly file
325 First_Line : constant String :=
326 ASCII.HT & ".section LIB$INITIALIZE,GBL,NOWRT" &
328 Second_Line : constant String :=
329 ASCII.HT & ".long " & Init_Proc & ASCII.LF;
330 -- First and second lines of the auto-init assembly file
333 Macro_File := Create_File (Macro_File_Name, Text);
334 OK := Macro_File /= Invalid_FD;
338 (Macro_File, First_Line (First_Line'First)'Address,
340 OK := Len = First_Line'Length;
345 (Macro_File, Second_Line (Second_Line'First)'Address,
347 OK := Len = Second_Line'Length;
351 Close (Macro_File, OK);
355 Fail ("creation of auto-init assembly file """,
356 Macro_File_Name, """ failed");
360 -- Invoke the macro-assembler
363 Write_Str ("Assembling auto-init assembly file """);
364 Write_Str (Macro_File_Name);
368 Popen_Result := Popen (Command (Command'First)'Address,
369 Mode (Mode'First)'Address);
371 if Popen_Result = Null_Address then
372 Fail ("assembly of auto-init assembly file """,
373 Macro_File_Name, """ failed");
376 -- Wait for the end of execution of the macro-assembler
378 Pclose_Result := Pclose (Popen_Result);
380 if Pclose_Result < 0 then
381 Fail ("assembly of auto init assembly file """,
382 Macro_File_Name, """ failed");
385 -- Add the generated object file to the list of objects to be
386 -- included in the library.
388 Additional_Objects :=
390 (1 => new String'(Lib_Filename & "$init.obj"));
394 -- Allocate the argument list and put the symbol file name, the
395 -- reference (if any) and the policy (if not autonomous).
397 Arguments := new Argument_List (1 .. Ofiles'Length + 8);
404 Last_Argument := Last_Argument + 1;
405 Arguments (Last_Argument) := new String'("-v");
408 -- Version number (major ID)
410 if Lib_Version /= "" then
411 Last_Argument := Last_Argument + 1;
412 Arguments (Last_Argument) := new String'("-V");
413 Last_Argument := Last_Argument + 1;
414 Arguments (Last_Argument) := new String'(Version);
419 Last_Argument := Last_Argument + 1;
420 Arguments (Last_Argument) := new String'("-s");
421 Last_Argument := Last_Argument + 1;
422 Arguments (Last_Argument) := new String'(Opt_File_Name);
424 -- Reference Symbol File
426 if Symbol_Data.Reference /= No_Name then
427 Last_Argument := Last_Argument + 1;
428 Arguments (Last_Argument) := new String'("-r");
429 Last_Argument := Last_Argument + 1;
430 Arguments (Last_Argument) :=
431 new String'(Get_Name_String (Symbol_Data.Reference));
436 case Symbol_Data.Symbol_Policy is
441 Last_Argument := Last_Argument + 1;
442 Arguments (Last_Argument) := new String'("-c");
445 Last_Argument := Last_Argument + 1;
446 Arguments (Last_Argument) := new String'("-C");
449 Last_Argument := Last_Argument + 1;
450 Arguments (Last_Argument) := new String'("-R");
453 -- Add each relevant object file
455 for Index in Ofiles'Range loop
456 if Is_Interface (Ofiles (Index).all) then
457 Last_Argument := Last_Argument + 1;
458 Arguments (Last_Argument) := new String'(Ofiles (Index).all);
464 Spawn (Program_Name => Gnatsym_Path.all,
465 Args => Arguments (1 .. Last_Argument),
469 Fail ("unable to create symbol file for library """,
475 -- Move all the -l switches from Opts to Opts2
478 Index : Natural := Opts'First;
482 while Index <= Last_Opt loop
485 if Opt'Length > 2 and then
486 Opt (Opt'First .. Opt'First + 1) = "-l"
488 if Index < Last_Opt then
489 Opts (Index .. Last_Opt - 1) :=
490 Opts (Index + 1 .. Last_Opt);
493 Last_Opt := Last_Opt - 1;
495 Last_Opt2 := Last_Opt2 + 1;
496 Opts2 (Last_Opt2) := Opt;
504 -- Invoke gcc to build the library
507 (Output_File => Lib_File,
508 Objects => Ofiles & Additional_Objects.all,
509 Options => VMS_Options,
510 Options_2 => Link_With_Shared_Libgcc.all &
511 Opts (Opts'First .. Last_Opt) &
512 Opts2 (Opts2'First .. Last_Opt2) & Options_2,
513 Driver_Name => Driver_Name);
515 -- The auto-init object file need to be deleted, so that it will not
516 -- be included in the library as a regular object file, otherwise
517 -- it will be included twice when the library will be built next
518 -- time, which may lead to errors.
522 Auto_Init_Object_File_Name : constant String :=
523 Lib_Filename & "$init.obj";
528 Write_Str ("deleting auto-init object file """);
529 Write_Str (Auto_Init_Object_File_Name);
533 Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
536 end Build_Dynamic_Library;
542 function DLL_Ext return String is
551 function Dynamic_Option return String is
560 function Is_Object_Ext (Ext : String) return Boolean is
569 function Is_C_Ext (Ext : String) return Boolean is
578 function Is_Archive_Ext (Ext : String) return Boolean is
580 return Ext = ".olb" or else Ext = ".exe";
587 function Libgnat return String is
588 Libgnat_A : constant String := "libgnat.a";
589 Libgnat_Olb : constant String := "libgnat.olb";
592 Name_Len := Libgnat_A'Length;
593 Name_Buffer (1 .. Name_Len) := Libgnat_A;
595 if Osint.Find_File (Name_Enter, Osint.Library) /= No_File then
603 ------------------------
604 -- Library_Exists_For --
605 ------------------------
607 function Library_Exists_For (Project : Project_Id) return Boolean is
609 if not Projects.Table (Project).Library then
610 Fail ("INTERNAL ERROR: Library_Exists_For called " &
611 "for non library project");
616 Lib_Dir : constant String :=
617 Get_Name_String (Projects.Table (Project).Library_Dir);
618 Lib_Name : constant String :=
619 Get_Name_String (Projects.Table (Project).Library_Name);
622 if Projects.Table (Project).Library_Kind = Static then
623 return Is_Regular_File
624 (Lib_Dir & Directory_Separator & "lib" &
625 Fil.Ext_To (Lib_Name, Archive_Ext));
628 return Is_Regular_File
629 (Lib_Dir & Directory_Separator & "lib" &
630 Fil.Ext_To (Lib_Name, DLL_Ext));
634 end Library_Exists_For;
636 ---------------------------
637 -- Library_File_Name_For --
638 ---------------------------
640 function Library_File_Name_For (Project : Project_Id) return Name_Id is
642 if not Projects.Table (Project).Library then
643 Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
644 "for non library project");
649 Lib_Name : constant String :=
650 Get_Name_String (Projects.Table (Project).Library_Name);
654 Name_Buffer (1 .. Name_Len) := "lib";
656 if Projects.Table (Project).Library_Kind = Static then
657 Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
660 Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
666 end Library_File_Name_For;
672 function Object_Ext return String is
681 function PIC_Option return String is
686 -----------------------------------------------
687 -- Standalone_Library_Auto_Init_Is_Supported --
688 -----------------------------------------------
690 function Standalone_Library_Auto_Init_Is_Supported return Boolean is
693 end Standalone_Library_Auto_Init_Is_Supported;
695 ---------------------------
696 -- Support_For_Libraries --
697 ---------------------------
699 function Support_For_Libraries return Library_Support is
702 end Support_For_Libraries;