1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
6 -- (Integrity VMS Version) --
10 -- Copyright (C) 2004-2005 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
;
42 with System
; use System
;
43 with System
.Case_Util
; use System
.Case_Util
;
44 with System
.CRTL
; use System
.CRTL
;
46 package body MLib
.Tgt
is
50 Empty_Argument_List
: aliased Argument_List
:= (1 .. 0 => null);
51 Additional_Objects
: Argument_List_Access
:= Empty_Argument_List
'Access;
52 -- Used to add the generated auto-init object files for auto-initializing
53 -- stand-alone libraries.
55 Macro_Name
: constant String := "mcr gnu:[bin]gcc -c -x assembler";
56 -- The name of the command to invoke the macro-assembler
58 VMS_Options
: Argument_List
:= (1 .. 1 => null);
60 Gnatsym_Name
: constant String := "gnatsym";
62 Gnatsym_Path
: String_Access
;
64 Arguments
: Argument_List_Access
:= null;
65 Last_Argument
: Natural := 0;
67 Success
: Boolean := False;
69 Shared_Libgcc
: aliased String := "-shared-libgcc";
71 No_Shared_Libgcc_Switch
: aliased Argument_List
:= (1 .. 0 => null);
72 Shared_Libgcc_Switch
: aliased Argument_List
:=
73 (1 => Shared_Libgcc
'Access);
74 Link_With_Shared_Libgcc
: Argument_List_Access
:=
75 No_Shared_Libgcc_Switch
'Access;
81 function Archive_Builder
return String is
86 -----------------------------
87 -- Archive_Builder_Options --
88 -----------------------------
90 function Archive_Builder_Options
return String_List_Access
is
92 return new String_List
'(1 => new String'("cr"));
93 end Archive_Builder_Options
;
99 function Archive_Ext
return String is
104 ---------------------
105 -- Archive_Indexer --
106 ---------------------
108 function Archive_Indexer
return String is
113 -----------------------------
114 -- Archive_Indexer_Options --
115 -----------------------------
117 function Archive_Indexer_Options
return String_List_Access
is
119 return new String_List
(1 .. 0);
120 end Archive_Indexer_Options
;
122 ---------------------------
123 -- Build_Dynamic_Library --
124 ---------------------------
126 procedure Build_Dynamic_Library
127 (Ofiles
: Argument_List
;
128 Foreign
: Argument_List
;
129 Afiles
: Argument_List
;
130 Options
: Argument_List
;
131 Options_2
: Argument_List
;
132 Interfaces
: Argument_List
;
133 Lib_Filename
: String;
135 Symbol_Data
: Symbol_Record
;
136 Driver_Name
: Name_Id
:= No_Name
;
137 Lib_Version
: String := "";
138 Auto_Init
: Boolean := False)
140 pragma Unreferenced
(Foreign
);
141 pragma Unreferenced
(Afiles
);
143 Lib_File
: constant String :=
144 Lib_Dir
& Directory_Separator
& "lib" &
145 Fil
.Ext_To
(Lib_Filename
, DLL_Ext
);
147 Opts
: Argument_List
:= Options
;
148 Last_Opt
: Natural := Opts
'Last;
149 Opts2
: Argument_List
(Options
'Range);
150 Last_Opt2
: Natural := Opts2
'First - 1;
152 Inter
: constant Argument_List
:= Interfaces
;
154 function Is_Interface
(Obj_File
: String) return Boolean;
155 -- For a Stand-Alone Library, returns True if Obj_File is the object
156 -- file name of an interface of the SAL. For other libraries, always
159 function Option_File_Name
return String;
160 -- Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
162 function Version_String
return String;
163 -- Returns Lib_Version if not empty and if Symbol_Data.Symbol_Policy is
164 -- not Autonomous, otherwise returns "".
165 -- When Symbol_Data.Symbol_Policy is Autonomous, fails gnatmake if
166 -- Lib_Version is not the image of a positive number.
172 function Is_Interface
(Obj_File
: String) return Boolean is
173 ALI
: constant String :=
175 (Filename
=> To_Lower
(Base_Name
(Obj_File
)),
179 if Inter
'Length = 0 then
182 elsif ALI
'Length > 2 and then
183 ALI
(ALI
'First .. ALI
'First + 1) = "b$"
188 for J
in Inter
'Range loop
189 if Inter
(J
).all = ALI
then
198 ----------------------
199 -- Option_File_Name --
200 ----------------------
202 function Option_File_Name
return String is
204 if Symbol_Data
.Symbol_File
= No_Name
then
207 Get_Name_String
(Symbol_Data
.Symbol_File
);
208 To_Lower
(Name_Buffer
(1 .. Name_Len
));
209 return Name_Buffer
(1 .. Name_Len
);
211 end Option_File_Name
;
217 function Version_String
return String is
218 Version
: Integer := 0;
221 or else Symbol_Data
.Symbol_Policy
/= Autonomous
227 Version
:= Integer'Value (Lib_Version
);
230 raise Constraint_Error
;
236 when Constraint_Error
=>
237 Fail
("illegal version """, Lib_Version
,
238 """ (on VMS version must be a positive number)");
244 Opt_File_Name
: constant String := Option_File_Name
;
245 Version
: constant String := Version_String
;
246 For_Linker_Opt
: String_Access
;
248 -- Start of processing for Build_Dynamic_Library
251 -- Invoke gcc with -shared-libgcc, but only for GCC 3 or higher
253 if GCC_Version
>= 3 then
254 Link_With_Shared_Libgcc
:= Shared_Libgcc_Switch
'Access;
256 Link_With_Shared_Libgcc
:= No_Shared_Libgcc_Switch
'Access;
259 -- Option file must end with ".opt"
261 if Opt_File_Name
'Length > 4
263 Opt_File_Name
(Opt_File_Name
'Last - 3 .. Opt_File_Name
'Last) = ".opt"
265 For_Linker_Opt
:= new String'("--for-linker=" & Opt_File_Name);
267 Fail ("Options File """, Opt_File_Name, """ must end with .opt");
270 VMS_Options (VMS_Options'First) := For_Linker_Opt;
272 for J in Inter'Range loop
273 To_Lower (Inter (J).all);
276 -- "gnatsym" is necessary for building the option file
278 if Gnatsym_Path = null then
279 Gnatsym_Path := OS_Lib.Locate_Exec_On_Path (Gnatsym_Name);
281 if Gnatsym_Path = null then
282 Fail (Gnatsym_Name, " not found in path");
286 -- For auto-initialization of a stand-alone library, we create
287 -- a macro-assembly file and we invoke the macro-assembler.
291 Macro_File_Name : constant String := Lib_Filename & "$init.asm";
292 Macro_File : File_Descriptor;
293 Init_Proc : String := Lib_Filename & "INIT";
294 Popen_Result : System.Address;
295 Pclose_Result : Integer;
297 OK : Boolean := True;
299 command : constant String :=
300 Macro_Name & " " & Macro_File_Name & ASCII.NUL;
301 -- The command to invoke the assembler on the generated auto-init
304 mode : constant String := "r" & ASCII.NUL;
305 -- The mode for the invocation of Popen
308 To_Upper (Init_Proc);
311 Write_Str ("Creating auto-init assembly file """);
312 Write_Str (Macro_File_Name);
316 -- Create and write the auto-init assembly file
319 First_Line : constant String :=
321 ".type " & Init_Proc & "#, @function" &
323 Second_Line : constant String :=
325 ".global " & Init_Proc & "#" &
327 Third_Line : constant String :=
329 ".global LIB$INITIALIZE#" &
331 Fourth_Line : constant String :=
333 ".section LIB$INITIALIZE#,""a"",@progbits" &
335 Fifth_Line : constant String :=
337 "data4 @fptr(" & Init_Proc & "#)" &
341 Macro_File := Create_File (Macro_File_Name, Text);
342 OK := Macro_File /= Invalid_FD;
346 (Macro_File, First_Line (First_Line'First)'Address,
348 OK := Len = First_Line'Length;
353 (Macro_File, Second_Line (Second_Line'First)'Address,
355 OK := Len = Second_Line'Length;
360 (Macro_File, Third_Line (Third_Line'First)'Address,
362 OK := Len = Third_Line'Length;
367 (Macro_File, Fourth_Line (Fourth_Line'First)'Address,
369 OK := Len = Fourth_Line'Length;
374 (Macro_File, Fifth_Line (Fifth_Line'First)'Address,
376 OK := Len = Fifth_Line'Length;
380 Close (Macro_File, OK);
384 Fail ("creation of auto-init assembly file """,
385 Macro_File_Name, """ failed");
389 -- Invoke the macro-assembler
392 Write_Str ("Assembling auto-init assembly file """);
393 Write_Str (Macro_File_Name);
397 Popen_Result := popen (command (command'First)'Address,
398 mode (mode'First)'Address);
400 if Popen_Result = Null_Address then
401 Fail ("assembly of auto-init assembly file """,
402 Macro_File_Name, """ failed");
405 -- Wait for the end of execution of the macro-assembler
407 Pclose_Result := pclose (Popen_Result);
409 if Pclose_Result < 0 then
410 Fail ("assembly of auto init assembly file """,
411 Macro_File_Name, """ failed");
414 -- Add the generated object file to the list of objects to be
415 -- included in the library.
417 Additional_Objects :=
419 (1 => new String'(Lib_Filename & "$init.obj"));
423 -- Allocate the argument list and put the symbol file name, the
424 -- reference (if any) and the policy (if not autonomous).
426 Arguments := new Argument_List (1 .. Ofiles'Length + 8);
433 Last_Argument := Last_Argument + 1;
434 Arguments (Last_Argument) := new String'("-v");
437 -- Version number (major ID)
439 if Lib_Version
/= "" then
440 Last_Argument
:= Last_Argument
+ 1;
441 Arguments
(Last_Argument
) := new String'("-V");
442 Last_Argument := Last_Argument + 1;
443 Arguments (Last_Argument) := new String'(Version
);
448 Last_Argument
:= Last_Argument
+ 1;
449 Arguments
(Last_Argument
) := new String'("-s");
450 Last_Argument := Last_Argument + 1;
451 Arguments (Last_Argument) := new String'(Opt_File_Name
);
453 -- Reference Symbol File
455 if Symbol_Data
.Reference
/= No_Name
then
456 Last_Argument
:= Last_Argument
+ 1;
457 Arguments
(Last_Argument
) := new String'("-r");
458 Last_Argument := Last_Argument + 1;
459 Arguments (Last_Argument) :=
460 new String'(Get_Name_String
(Symbol_Data
.Reference
));
465 case Symbol_Data
.Symbol_Policy
is
470 Last_Argument
:= Last_Argument
+ 1;
471 Arguments
(Last_Argument
) := new String'("-c");
474 Last_Argument := Last_Argument + 1;
475 Arguments (Last_Argument) := new String'("-C");
478 Last_Argument
:= Last_Argument
+ 1;
479 Arguments
(Last_Argument
) := new String'("-R");
482 -- Add each relevant object file
484 for Index in Ofiles'Range loop
485 if Is_Interface (Ofiles (Index).all) then
486 Last_Argument := Last_Argument + 1;
487 Arguments (Last_Argument) := new String'(Ofiles
(Index
).all);
493 Spawn
(Program_Name
=> Gnatsym_Path
.all,
494 Args
=> Arguments
(1 .. Last_Argument
),
498 Fail
("unable to create symbol file for library """,
504 -- Move all the -l switches from Opts to Opts2
507 Index
: Natural := Opts
'First;
511 while Index
<= Last_Opt
loop
514 if Opt
'Length > 2 and then
515 Opt
(Opt
'First .. Opt
'First + 1) = "-l"
517 if Index
< Last_Opt
then
518 Opts
(Index
.. Last_Opt
- 1) :=
519 Opts
(Index
+ 1 .. Last_Opt
);
522 Last_Opt
:= Last_Opt
- 1;
524 Last_Opt2
:= Last_Opt2
+ 1;
525 Opts2
(Last_Opt2
) := Opt
;
533 -- Invoke gcc to build the library
536 (Output_File
=> Lib_File
,
537 Objects
=> Ofiles
& Additional_Objects
.all,
538 Options
=> VMS_Options
,
539 Options_2
=> Link_With_Shared_Libgcc
.all &
540 Opts
(Opts
'First .. Last_Opt
) &
541 Opts2
(Opts2
'First .. Last_Opt2
) & Options_2
,
542 Driver_Name
=> Driver_Name
);
544 -- The auto-init object file need to be deleted, so that it will not
545 -- be included in the library as a regular object file, otherwise
546 -- it will be included twice when the library will be built next
547 -- time, which may lead to errors.
551 Auto_Init_Object_File_Name
: constant String :=
552 Lib_Filename
& "$init.obj";
557 Write_Str
("deleting auto-init object file """);
558 Write_Str
(Auto_Init_Object_File_Name
);
562 Delete_File
(Auto_Init_Object_File_Name
, Success
=> Disregard
);
565 end Build_Dynamic_Library
;
571 function DLL_Ext
return String is
580 function Dynamic_Option
return String is
589 function Is_Object_Ext
(Ext
: String) return Boolean is
598 function Is_C_Ext
(Ext
: String) return Boolean is
607 function Is_Archive_Ext
(Ext
: String) return Boolean is
609 return Ext
= ".olb" or else Ext
= ".exe";
616 function Libgnat
return String is
617 Libgnat_A
: constant String := "libgnat.a";
618 Libgnat_Olb
: constant String := "libgnat.olb";
621 Name_Len
:= Libgnat_A
'Length;
622 Name_Buffer
(1 .. Name_Len
) := Libgnat_A
;
624 if Osint
.Find_File
(Name_Enter
, Osint
.Library
) /= No_File
then
632 ------------------------
633 -- Library_Exists_For --
634 ------------------------
636 function Library_Exists_For
637 (Project
: Project_Id
; In_Tree
: Project_Tree_Ref
) return Boolean
640 if not In_Tree
.Projects
.Table
(Project
).Library
then
641 Fail
("INTERNAL ERROR: Library_Exists_For called " &
642 "for non library project");
647 Lib_Dir
: constant String :=
649 (In_Tree
.Projects
.Table
(Project
).Library_Dir
);
650 Lib_Name
: constant String :=
652 (In_Tree
.Projects
.Table
(Project
).Library_Name
);
655 if In_Tree
.Projects
.Table
(Project
).Library_Kind
=
658 return Is_Regular_File
659 (Lib_Dir
& Directory_Separator
& "lib" &
660 Fil
.Ext_To
(Lib_Name
, Archive_Ext
));
663 return Is_Regular_File
664 (Lib_Dir
& Directory_Separator
& "lib" &
665 Fil
.Ext_To
(Lib_Name
, DLL_Ext
));
669 end Library_Exists_For
;
671 ---------------------------
672 -- Library_File_Name_For --
673 ---------------------------
675 function Library_File_Name_For
676 (Project
: Project_Id
;
677 In_Tree
: Project_Tree_Ref
) return Name_Id
680 if not In_Tree
.Projects
.Table
(Project
).Library
then
681 Prj
.Com
.Fail
("INTERNAL ERROR: Library_File_Name_For called " &
682 "for non library project");
687 Lib_Name
: constant String :=
689 (In_Tree
.Projects
.Table
(Project
).Library_Name
);
693 Name_Buffer
(1 .. Name_Len
) := "lib";
695 if In_Tree
.Projects
.Table
(Project
).Library_Kind
=
697 Add_Str_To_Name_Buffer
(Fil
.Ext_To
(Lib_Name
, Archive_Ext
));
700 Add_Str_To_Name_Buffer
(Fil
.Ext_To
(Lib_Name
, DLL_Ext
));
706 end Library_File_Name_For
;
712 function Object_Ext
return String is
721 function PIC_Option
return String is
726 -----------------------------------------------
727 -- Standalone_Library_Auto_Init_Is_Supported --
728 -----------------------------------------------
730 function Standalone_Library_Auto_Init_Is_Supported
return Boolean is
733 end Standalone_Library_Auto_Init_Is_Supported
;
735 ---------------------------
736 -- Support_For_Libraries --
737 ---------------------------
739 function Support_For_Libraries
return Library_Support
is
742 end Support_For_Libraries
;