OSDN Git Service

2006-06-07 Paolo Bonzini <bonzini@gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-linux.adb
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-2005, 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 ------------------------------------------------------------------------------
27
28 --  This package provides a set of target dependent routines to build
29 --  static, dynamic and shared libraries.
30
31 --  This is the GNU/Linux version of the body
32
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;
40
41 package body MLib.Tgt is
42
43    use GNAT;
44    use MLib;
45
46    ---------------------
47    -- Archive_Builder --
48    ---------------------
49
50    function Archive_Builder return String is
51    begin
52       return "ar";
53    end Archive_Builder;
54
55    -----------------------------
56    -- Archive_Builder_Options --
57    -----------------------------
58
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;
63
64    -----------------
65    -- Archive_Ext --
66    -----------------
67
68    function Archive_Ext return  String is
69    begin
70       return "a";
71    end Archive_Ext;
72
73    ---------------------
74    -- Archive_Indexer --
75    ---------------------
76
77    function Archive_Indexer return String is
78    begin
79       return "ranlib";
80    end Archive_Indexer;
81
82    -----------------------------
83    -- Archive_Indexer_Options --
84    -----------------------------
85
86    function Archive_Indexer_Options return String_List_Access is
87    begin
88       return new String_List (1 .. 0);
89    end Archive_Indexer_Options;
90
91    ---------------------------
92    -- Build_Dynamic_Library --
93    ---------------------------
94
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)
108    is
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
115
116       Lib_File : constant String :=
117                    Lib_Dir & Directory_Separator & "lib" &
118                    Fil.Ext_To (Lib_Filename, DLL_Ext);
119
120       Version_Arg          : String_Access;
121       Symbolic_Link_Needed : Boolean := False;
122
123    begin
124       if Opt.Verbose_Mode then
125          Write_Str ("building relocatable shared library ");
126          Write_Line (Lib_File);
127       end if;
128
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);
136
137       else
138          Version_Arg := new String'("-Wl,-soname," & Lib_Version);
139
140          if Is_Absolute_Path (Lib_Version) then
141             Utl.Gcc
142               (Output_File => Lib_Version,
143                Objects     => Ofiles,
144                Options     => Options & Version_Arg,
145                Driver_Name => Driver_Name,
146                Options_2   => Options_2);
147             Symbolic_Link_Needed := Lib_Version /= Lib_File;
148
149          else
150             Utl.Gcc
151               (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
152                Objects     => Ofiles,
153                Options     => Options & Version_Arg,
154                Driver_Name => Driver_Name,
155                Options_2   => Options_2);
156             Symbolic_Link_Needed :=
157               Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
158          end if;
159
160          if Symbolic_Link_Needed then
161             declare
162                Success : Boolean;
163                Oldpath : String (1 .. Lib_Version'Length + 1);
164                Newpath : String (1 .. Lib_File'Length + 1);
165
166                Result : Integer;
167                pragma Unreferenced (Result);
168
169                function Symlink
170                  (Oldpath : System.Address;
171                   Newpath : System.Address) return Integer;
172                pragma Import (C, Symlink, "__gnat_symlink");
173
174             begin
175                Oldpath (1 .. Lib_Version'Length) := Lib_Version;
176                Oldpath (Oldpath'Last)            := ASCII.NUL;
177                Newpath (1 .. Lib_File'Length)    := Lib_File;
178                Newpath (Newpath'Last)            := ASCII.NUL;
179
180                Delete_File (Lib_File, Success);
181
182                Result := Symlink (Oldpath'Address, Newpath'Address);
183             end;
184          end if;
185       end if;
186    end Build_Dynamic_Library;
187
188    -------------
189    -- DLL_Ext --
190    -------------
191
192    function DLL_Ext return String is
193    begin
194       return "so";
195    end DLL_Ext;
196
197    ----------------
198    -- DLL_Prefix --
199    ----------------
200
201    function DLL_Prefix return String is
202    begin
203       return "lib";
204    end DLL_Prefix;
205
206    --------------------
207    -- Dynamic_Option --
208    --------------------
209
210    function Dynamic_Option return String is
211    begin
212       return "-shared";
213    end Dynamic_Option;
214
215    -------------------
216    -- Is_Object_Ext --
217    -------------------
218
219    function Is_Object_Ext (Ext : String) return Boolean is
220    begin
221       return Ext = ".o";
222    end Is_Object_Ext;
223
224    --------------
225    -- Is_C_Ext --
226    --------------
227
228    function Is_C_Ext (Ext : String) return Boolean is
229    begin
230       return Ext = ".c";
231    end Is_C_Ext;
232
233    --------------------
234    -- Is_Archive_Ext --
235    --------------------
236
237    function Is_Archive_Ext (Ext : String) return Boolean is
238    begin
239       return Ext = ".a" or else Ext = ".so";
240    end Is_Archive_Ext;
241
242    -------------
243    -- Libgnat --
244    -------------
245
246    function Libgnat return String is
247    begin
248       return "libgnat.a";
249    end Libgnat;
250
251    ------------------------
252    -- Library_Exists_For --
253    ------------------------
254
255    function Library_Exists_For
256      (Project : Project_Id; In_Tree : Project_Tree_Ref) return Boolean
257    is
258    begin
259       if not In_Tree.Projects.Table (Project).Library then
260          Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
261                        "for non library project");
262          return False;
263
264       else
265          declare
266             Lib_Dir : constant String :=
267               Get_Name_String
268                 (In_Tree.Projects.Table (Project).Library_Dir);
269             Lib_Name : constant String :=
270               Get_Name_String
271                 (In_Tree.Projects.Table (Project).Library_Name);
272
273          begin
274             if In_Tree.Projects.Table (Project).Library_Kind =
275                  Static
276             then
277                return Is_Regular_File
278                  (Lib_Dir & Directory_Separator & "lib" &
279                   Fil.Ext_To (Lib_Name, Archive_Ext));
280
281             else
282                return Is_Regular_File
283                  (Lib_Dir & Directory_Separator & "lib" &
284                   Fil.Ext_To (Lib_Name, DLL_Ext));
285             end if;
286          end;
287       end if;
288    end Library_Exists_For;
289
290    ---------------------------
291    -- Library_File_Name_For --
292    ---------------------------
293
294    function Library_File_Name_For
295      (Project : Project_Id;
296       In_Tree : Project_Tree_Ref) return Name_Id
297    is
298    begin
299       if not In_Tree.Projects.Table (Project).Library then
300          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
301                        "for non library project");
302          return No_Name;
303
304       else
305          declare
306             Lib_Name : constant String :=
307               Get_Name_String
308                 (In_Tree.Projects.Table (Project).Library_Name);
309
310          begin
311             Name_Len := 3;
312             Name_Buffer (1 .. Name_Len) := "lib";
313
314             if In_Tree.Projects.Table (Project).Library_Kind =
315                  Static
316             then
317                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
318
319             else
320                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
321             end if;
322
323             return Name_Find;
324          end;
325       end if;
326    end Library_File_Name_For;
327
328    ----------------
329    -- Object_Ext --
330    ----------------
331
332    function Object_Ext return String is
333    begin
334       return "o";
335    end Object_Ext;
336
337    ----------------
338    -- PIC_Option --
339    ----------------
340
341    function PIC_Option return String is
342    begin
343       return "-fPIC";
344    end PIC_Option;
345
346    -----------------------------------------------
347    -- Standalone_Library_Auto_Init_Is_Supported --
348    -----------------------------------------------
349
350    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
351    begin
352       return True;
353    end Standalone_Library_Auto_Init_Is_Supported;
354
355    ---------------------------
356    -- Support_For_Libraries --
357    ---------------------------
358
359    function Support_For_Libraries return Library_Support is
360    begin
361       return Full;
362    end Support_For_Libraries;
363
364 end MLib.Tgt;