* gcc.c (getenv_spec_function): New function.
[official-gcc.git] / gcc / ada / mlib-tgt-linux.adb
blob737a40a9ee84b13330b128df79e8a67ec7445ba4
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-2006, 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, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, 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 ---------------------
47 -- Archive_Builder --
48 ---------------------
50 function Archive_Builder return String is
51 begin
52 return "ar";
53 end Archive_Builder;
55 -----------------------------
56 -- Archive_Builder_Options --
57 -----------------------------
59 function Archive_Builder_Options return String_List_Access is
60 begin
61 return new String_List'(1 => new String'("cr"));
62 end Archive_Builder_Options;
64 -----------------
65 -- Archive_Ext --
66 -----------------
68 function Archive_Ext return String is
69 begin
70 return "a";
71 end Archive_Ext;
73 ---------------------
74 -- Archive_Indexer --
75 ---------------------
77 function Archive_Indexer return String is
78 begin
79 return "ranlib";
80 end Archive_Indexer;
82 -----------------------------
83 -- Archive_Indexer_Options --
84 -----------------------------
86 function Archive_Indexer_Options return String_List_Access is
87 begin
88 return new String_List (1 .. 0);
89 end Archive_Indexer_Options;
91 ---------------------------
92 -- Build_Dynamic_Library --
93 ---------------------------
95 procedure Build_Dynamic_Library
96 (Ofiles : Argument_List;
97 Foreign : Argument_List;
98 Afiles : Argument_List;
99 Options : Argument_List;
100 Options_2 : Argument_List;
101 Interfaces : Argument_List;
102 Lib_Filename : String;
103 Lib_Dir : String;
104 Symbol_Data : Symbol_Record;
105 Driver_Name : Name_Id := No_Name;
106 Lib_Version : String := "";
107 Auto_Init : Boolean := False)
109 pragma Unreferenced (Foreign);
110 pragma Unreferenced (Afiles);
111 pragma Unreferenced (Interfaces);
112 pragma Unreferenced (Symbol_Data);
113 pragma Unreferenced (Auto_Init);
114 -- Initialization is done through the contructor mechanism
116 Lib_File : constant String :=
117 Lib_Dir & Directory_Separator & "lib" &
118 Fil.Append_To (Lib_Filename, DLL_Ext);
120 Version_Arg : String_Access;
121 Symbolic_Link_Needed : Boolean := False;
123 begin
124 if Opt.Verbose_Mode then
125 Write_Str ("building relocatable shared library ");
126 Write_Line (Lib_File);
127 end if;
129 if Lib_Version = "" then
130 Utl.Gcc
131 (Output_File => Lib_File,
132 Objects => Ofiles,
133 Options => Options,
134 Driver_Name => Driver_Name,
135 Options_2 => Options_2);
137 else
138 declare
139 Maj_Version : constant String := Lib_Version;
140 Last_Maj : Positive := Maj_Version'Last;
141 Last : Positive;
142 Ok_Maj : Boolean := False;
143 begin
144 while Last_Maj > Maj_Version'First loop
145 if Maj_Version (Last_Maj) in '0' .. '9' then
146 Last_Maj := Last_Maj - 1;
148 else
149 Ok_Maj := Last_Maj /= Maj_Version'Last and then
150 Maj_Version (Last_Maj) = '.';
152 if Ok_Maj then
153 Last_Maj := Last_Maj - 1;
154 end if;
156 exit;
157 end if;
158 end loop;
160 if Ok_Maj then
161 Last := Last_Maj;
163 while Last > Maj_Version'First loop
164 if Maj_Version (Last) in '0' .. '9' then
165 Last := Last - 1;
167 else
168 Ok_Maj := Last /= Last_Maj and then
169 Maj_Version (Last) = '.';
171 if Ok_Maj then
172 Last := Last - 1;
174 Ok_Maj := Maj_Version (1 .. Last) = Lib_File;
175 end if;
177 exit;
178 end if;
179 end loop;
180 end if;
182 if Ok_Maj then
183 Version_Arg := new String'("-Wl,-soname," &
184 Maj_Version (1 .. Last_Maj));
186 else
187 Version_Arg := new String'("-Wl,-soname," & Lib_Version);
188 end if;
190 if Is_Absolute_Path (Lib_Version) then
191 Utl.Gcc
192 (Output_File => Lib_Version,
193 Objects => Ofiles,
194 Options => Options & Version_Arg,
195 Driver_Name => Driver_Name,
196 Options_2 => Options_2);
197 Symbolic_Link_Needed := Lib_Version /= Lib_File;
199 else
200 Utl.Gcc
201 (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
202 Objects => Ofiles,
203 Options => Options & Version_Arg,
204 Driver_Name => Driver_Name,
205 Options_2 => Options_2);
206 Symbolic_Link_Needed :=
207 Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
208 end if;
210 if Symbolic_Link_Needed then
211 declare
212 Success : Boolean;
213 Oldpath : String (1 .. Lib_Version'Length + 1);
214 Newpath : String (1 .. Lib_File'Length + 1);
216 Result : Integer;
217 pragma Unreferenced (Result);
219 function Symlink
220 (Oldpath : System.Address;
221 Newpath : System.Address) return Integer;
222 pragma Import (C, Symlink, "__gnat_symlink");
224 begin
225 Oldpath (1 .. Lib_Version'Length) := Lib_Version;
226 Oldpath (Oldpath'Last) := ASCII.NUL;
227 Newpath (1 .. Lib_File'Length) := Lib_File;
228 Newpath (Newpath'Last) := ASCII.NUL;
230 Delete_File (Lib_File, Success);
232 Result := Symlink (Oldpath'Address, Newpath'Address);
233 end;
234 end if;
235 end;
236 end if;
237 end Build_Dynamic_Library;
239 -------------
240 -- DLL_Ext --
241 -------------
243 function DLL_Ext return String is
244 begin
245 return "so";
246 end DLL_Ext;
248 ----------------
249 -- DLL_Prefix --
250 ----------------
252 function DLL_Prefix return String is
253 begin
254 return "lib";
255 end DLL_Prefix;
257 --------------------
258 -- Dynamic_Option --
259 --------------------
261 function Dynamic_Option return String is
262 begin
263 return "-shared";
264 end Dynamic_Option;
266 -------------------
267 -- Is_Object_Ext --
268 -------------------
270 function Is_Object_Ext (Ext : String) return Boolean is
271 begin
272 return Ext = ".o";
273 end Is_Object_Ext;
275 --------------
276 -- Is_C_Ext --
277 --------------
279 function Is_C_Ext (Ext : String) return Boolean is
280 begin
281 return Ext = ".c";
282 end Is_C_Ext;
284 --------------------
285 -- Is_Archive_Ext --
286 --------------------
288 function Is_Archive_Ext (Ext : String) return Boolean is
289 begin
290 return Ext = ".a" or else Ext = ".so";
291 end Is_Archive_Ext;
293 -------------
294 -- Libgnat --
295 -------------
297 function Libgnat return String is
298 begin
299 return "libgnat.a";
300 end Libgnat;
302 ------------------------
303 -- Library_Exists_For --
304 ------------------------
306 function Library_Exists_For
307 (Project : Project_Id;
308 In_Tree : Project_Tree_Ref) return Boolean
310 begin
311 if not In_Tree.Projects.Table (Project).Library then
312 Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
313 "for non library project");
314 return False;
316 else
317 declare
318 Lib_Dir : constant String :=
319 Get_Name_String
320 (In_Tree.Projects.Table (Project).Library_Dir);
321 Lib_Name : constant String :=
322 Get_Name_String
323 (In_Tree.Projects.Table (Project).Library_Name);
325 begin
326 if In_Tree.Projects.Table (Project).Library_Kind = Static then
327 return Is_Regular_File
328 (Lib_Dir & Directory_Separator & "lib" &
329 Fil.Append_To (Lib_Name, Archive_Ext));
331 else
332 return Is_Regular_File
333 (Lib_Dir & Directory_Separator & "lib" &
334 Fil.Append_To (Lib_Name, DLL_Ext));
335 end if;
336 end;
337 end if;
338 end Library_Exists_For;
340 ---------------------------
341 -- Library_File_Name_For --
342 ---------------------------
344 function Library_File_Name_For
345 (Project : Project_Id;
346 In_Tree : Project_Tree_Ref) return Name_Id
348 begin
349 if not In_Tree.Projects.Table (Project).Library then
350 Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
351 "for non library project");
352 return No_Name;
354 else
355 declare
356 Lib_Name : constant String :=
357 Get_Name_String
358 (In_Tree.Projects.Table (Project).Library_Name);
360 begin
361 Name_Len := 3;
362 Name_Buffer (1 .. Name_Len) := "lib";
364 if In_Tree.Projects.Table (Project).Library_Kind =
365 Static
366 then
367 Add_Str_To_Name_Buffer (Fil.Append_To (Lib_Name, Archive_Ext));
368 else
369 Add_Str_To_Name_Buffer (Fil.Append_To (Lib_Name, DLL_Ext));
370 end if;
372 return Name_Find;
373 end;
374 end if;
375 end Library_File_Name_For;
377 ----------------
378 -- Object_Ext --
379 ----------------
381 function Object_Ext return String is
382 begin
383 return "o";
384 end Object_Ext;
386 ----------------
387 -- PIC_Option --
388 ----------------
390 function PIC_Option return String is
391 begin
392 return "-fPIC";
393 end PIC_Option;
395 -----------------------------------------------
396 -- Standalone_Library_Auto_Init_Is_Supported --
397 -----------------------------------------------
399 function Standalone_Library_Auto_Init_Is_Supported return Boolean is
400 begin
401 return True;
402 end Standalone_Library_Auto_Init_Is_Supported;
404 ---------------------------
405 -- Support_For_Libraries --
406 ---------------------------
408 function Support_For_Libraries return Library_Support is
409 begin
410 return Full;
411 end Support_For_Libraries;
413 end MLib.Tgt;