PR target/16201
[official-gcc.git] / gcc / ada / mlib-tgt-linux.adb
blob71584f26a3a2108f4da064801c5ed96712f4d40d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M L I B . T G T --
6 -- (GNU/Linux Version) --
7 -- --
8 -- B o d y --
9 -- --
10 -- Copyright (C) 2001-2004, 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 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. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 -- --
26 ------------------------------------------------------------------------------
28 -- This package provides a set of target dependent routines to build
29 -- static, dynamic and shared libraries.
31 -- This is the GNU/Linux version of the body.
33 with MLib.Fil;
34 with MLib.Utl;
35 with Namet; use Namet;
36 with Opt;
37 with Output; use Output;
38 with Prj.Com;
39 with System;
41 package body MLib.Tgt is
43 use GNAT;
44 use MLib;
46 No_Arguments : aliased Argument_List := (1 .. 0 => null);
47 Empty_Argument_List : constant Argument_List_Access := No_Arguments'Access;
49 Wl_Init_String : aliased String := "-Wl,-init";
50 Wl_Init : constant String_Access := Wl_Init_String'Access;
51 Wl_Fini_String : aliased String := "-Wl,-fini";
52 Wl_Fini : constant String_Access := Wl_Fini_String'Access;
54 Init_Fini_List : constant Argument_List_Access :=
55 new Argument_List'(1 => Wl_Init,
56 2 => null,
57 3 => Wl_Fini,
58 4 => null);
59 -- Used to put switches for automatic elaboration/finalization
61 ---------------------
62 -- Archive_Builder --
63 ---------------------
65 function Archive_Builder return String is
66 begin
67 return "ar";
68 end Archive_Builder;
70 -----------------------------
71 -- Archive_Builder_Options --
72 -----------------------------
74 function Archive_Builder_Options return String_List_Access is
75 begin
76 return new String_List'(1 => new String'("cr"));
77 end Archive_Builder_Options;
79 -----------------
80 -- Archive_Ext --
81 -----------------
83 function Archive_Ext return String is
84 begin
85 return "a";
86 end Archive_Ext;
88 ---------------------
89 -- Archive_Indexer --
90 ---------------------
92 function Archive_Indexer return String is
93 begin
94 return "ranlib";
95 end Archive_Indexer;
97 -----------------------------
98 -- Archive_Indexer_Options --
99 -----------------------------
101 function Archive_Indexer_Options return String_List_Access is
102 begin
103 return new String_List (1 .. 0);
104 end Archive_Indexer_Options;
106 ---------------------------
107 -- Build_Dynamic_Library --
108 ---------------------------
110 procedure Build_Dynamic_Library
111 (Ofiles : Argument_List;
112 Foreign : Argument_List;
113 Afiles : Argument_List;
114 Options : Argument_List;
115 Options_2 : Argument_List;
116 Interfaces : Argument_List;
117 Lib_Filename : String;
118 Lib_Dir : String;
119 Symbol_Data : Symbol_Record;
120 Driver_Name : Name_Id := No_Name;
121 Lib_Version : String := "";
122 Auto_Init : Boolean := False)
124 pragma Unreferenced (Foreign);
125 pragma Unreferenced (Afiles);
126 pragma Unreferenced (Interfaces);
127 pragma Unreferenced (Symbol_Data);
129 Lib_File : constant String :=
130 Lib_Dir & Directory_Separator & "lib" &
131 Fil.Ext_To (Lib_Filename, DLL_Ext);
133 Version_Arg : String_Access;
134 Symbolic_Link_Needed : Boolean := False;
136 Init_Fini : Argument_List_Access := Empty_Argument_List;
138 begin
139 if Opt.Verbose_Mode then
140 Write_Str ("building relocatable shared library ");
141 Write_Line (Lib_File);
142 end if;
144 -- If specified, add automatic elaboration/finalization
146 if Auto_Init then
147 Init_Fini := Init_Fini_List;
148 Init_Fini (2) := new String'("-Wl," & Lib_Filename & "init");
149 Init_Fini (4) := new String'("-Wl," & Lib_Filename & "final");
150 end if;
152 if Lib_Version = "" then
153 Utl.Gcc
154 (Output_File => Lib_File,
155 Objects => Ofiles,
156 Options => Options & Init_Fini.all,
157 Driver_Name => Driver_Name,
158 Options_2 => Options_2);
160 else
161 Version_Arg := new String'("-Wl,-soname," & Lib_Version);
163 if Is_Absolute_Path (Lib_Version) then
164 Utl.Gcc
165 (Output_File => Lib_Version,
166 Objects => Ofiles,
167 Options => Options & Version_Arg & Init_Fini.all,
168 Driver_Name => Driver_Name,
169 Options_2 => Options_2);
170 Symbolic_Link_Needed := Lib_Version /= Lib_File;
172 else
173 Utl.Gcc
174 (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
175 Objects => Ofiles,
176 Options => Options & Version_Arg & Init_Fini.all,
177 Driver_Name => Driver_Name,
178 Options_2 => Options_2);
179 Symbolic_Link_Needed :=
180 Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
181 end if;
183 if Symbolic_Link_Needed then
184 declare
185 Success : Boolean;
186 Oldpath : String (1 .. Lib_Version'Length + 1);
187 Newpath : String (1 .. Lib_File'Length + 1);
189 Result : Integer;
190 pragma Unreferenced (Result);
192 function Symlink
193 (Oldpath : System.Address;
194 Newpath : System.Address) return Integer;
195 pragma Import (C, Symlink, "__gnat_symlink");
197 begin
198 Oldpath (1 .. Lib_Version'Length) := Lib_Version;
199 Oldpath (Oldpath'Last) := ASCII.NUL;
200 Newpath (1 .. Lib_File'Length) := Lib_File;
201 Newpath (Newpath'Last) := ASCII.NUL;
203 Delete_File (Lib_File, Success);
205 Result := Symlink (Oldpath'Address, Newpath'Address);
206 end;
207 end if;
208 end if;
209 end Build_Dynamic_Library;
211 -------------
212 -- DLL_Ext --
213 -------------
215 function DLL_Ext return String is
216 begin
217 return "so";
218 end DLL_Ext;
220 --------------------
221 -- Dynamic_Option --
222 --------------------
224 function Dynamic_Option return String is
225 begin
226 return "-shared";
227 end Dynamic_Option;
229 -------------------
230 -- Is_Object_Ext --
231 -------------------
233 function Is_Object_Ext (Ext : String) return Boolean is
234 begin
235 return Ext = ".o";
236 end Is_Object_Ext;
238 --------------
239 -- Is_C_Ext --
240 --------------
242 function Is_C_Ext (Ext : String) return Boolean is
243 begin
244 return Ext = ".c";
245 end Is_C_Ext;
247 --------------------
248 -- Is_Archive_Ext --
249 --------------------
251 function Is_Archive_Ext (Ext : String) return Boolean is
252 begin
253 return Ext = ".a" or else Ext = ".so";
254 end Is_Archive_Ext;
256 -------------
257 -- Libgnat --
258 -------------
260 function Libgnat return String is
261 begin
262 return "libgnat.a";
263 end Libgnat;
265 ------------------------
266 -- Library_Exists_For --
267 ------------------------
269 function Library_Exists_For (Project : Project_Id) return Boolean is
270 begin
271 if not Projects.Table (Project).Library then
272 Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
273 "for non library project");
274 return False;
276 else
277 declare
278 Lib_Dir : constant String :=
279 Get_Name_String (Projects.Table (Project).Library_Dir);
280 Lib_Name : constant String :=
281 Get_Name_String (Projects.Table (Project).Library_Name);
283 begin
284 if Projects.Table (Project).Library_Kind = Static then
285 return Is_Regular_File
286 (Lib_Dir & Directory_Separator & "lib" &
287 Fil.Ext_To (Lib_Name, Archive_Ext));
289 else
290 return Is_Regular_File
291 (Lib_Dir & Directory_Separator & "lib" &
292 Fil.Ext_To (Lib_Name, DLL_Ext));
293 end if;
294 end;
295 end if;
296 end Library_Exists_For;
298 ---------------------------
299 -- Library_File_Name_For --
300 ---------------------------
302 function Library_File_Name_For (Project : Project_Id) return Name_Id is
303 begin
304 if not Projects.Table (Project).Library then
305 Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
306 "for non library project");
307 return No_Name;
309 else
310 declare
311 Lib_Name : constant String :=
312 Get_Name_String (Projects.Table (Project).Library_Name);
314 begin
315 Name_Len := 3;
316 Name_Buffer (1 .. Name_Len) := "lib";
318 if Projects.Table (Project).Library_Kind = Static then
319 Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
321 else
322 Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
323 end if;
325 return Name_Find;
326 end;
327 end if;
328 end Library_File_Name_For;
330 ----------------
331 -- Object_Ext --
332 ----------------
334 function Object_Ext return String is
335 begin
336 return "o";
337 end Object_Ext;
339 ----------------
340 -- PIC_Option --
341 ----------------
343 function PIC_Option return String is
344 begin
345 return "-fPIC";
346 end PIC_Option;
348 -----------------------------------------------
349 -- Standalone_Library_Auto_Init_Is_Supported --
350 -----------------------------------------------
352 function Standalone_Library_Auto_Init_Is_Supported return Boolean is
353 begin
354 return True;
355 end Standalone_Library_Auto_Init_Is_Supported;
357 ---------------------------
358 -- Support_For_Libraries --
359 ---------------------------
361 function Support_For_Libraries return Library_Support is
362 begin
363 return Full;
364 end Support_For_Libraries;
366 end MLib.Tgt;