OSDN Git Service

2005-06-14 Jose Ruiz <ruiz@adacore.com>
[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,  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 ------------------------------------------------------------------------------
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    No_Arguments        : aliased Argument_List         := (1 .. 0 => null);
47    Empty_Argument_List : constant Argument_List_Access := No_Arguments'Access;
48
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;
53
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
60
61    ---------------------
62    -- Archive_Builder --
63    ---------------------
64
65    function Archive_Builder return String is
66    begin
67       return "ar";
68    end Archive_Builder;
69
70    -----------------------------
71    -- Archive_Builder_Options --
72    -----------------------------
73
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;
78
79    -----------------
80    -- Archive_Ext --
81    -----------------
82
83    function Archive_Ext return  String is
84    begin
85       return "a";
86    end Archive_Ext;
87
88    ---------------------
89    -- Archive_Indexer --
90    ---------------------
91
92    function Archive_Indexer return String is
93    begin
94       return "ranlib";
95    end Archive_Indexer;
96
97    -----------------------------
98    -- Archive_Indexer_Options --
99    -----------------------------
100
101    function Archive_Indexer_Options return String_List_Access is
102    begin
103       return new String_List (1 .. 0);
104    end Archive_Indexer_Options;
105
106    ---------------------------
107    -- Build_Dynamic_Library --
108    ---------------------------
109
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)
123    is
124       pragma Unreferenced (Foreign);
125       pragma Unreferenced (Afiles);
126       pragma Unreferenced (Interfaces);
127       pragma Unreferenced (Symbol_Data);
128
129       Lib_File : constant String :=
130                    Lib_Dir & Directory_Separator & "lib" &
131                    Fil.Ext_To (Lib_Filename, DLL_Ext);
132
133       Version_Arg          : String_Access;
134       Symbolic_Link_Needed : Boolean := False;
135
136       Init_Fini : Argument_List_Access := Empty_Argument_List;
137
138    begin
139       if Opt.Verbose_Mode then
140          Write_Str ("building relocatable shared library ");
141          Write_Line (Lib_File);
142       end if;
143
144       --  If specified, add automatic elaboration/finalization
145
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;
151
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);
159
160       else
161          Version_Arg := new String'("-Wl,-soname," & Lib_Version);
162
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;
171
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;
182
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);
188
189                Result : Integer;
190                pragma Unreferenced (Result);
191
192                function Symlink
193                  (Oldpath : System.Address;
194                   Newpath : System.Address) return Integer;
195                pragma Import (C, Symlink, "__gnat_symlink");
196
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;
202
203                Delete_File (Lib_File, Success);
204
205                Result := Symlink (Oldpath'Address, Newpath'Address);
206             end;
207          end if;
208       end if;
209    end Build_Dynamic_Library;
210
211    -------------
212    -- DLL_Ext --
213    -------------
214
215    function DLL_Ext return String is
216    begin
217       return "so";
218    end DLL_Ext;
219
220    --------------------
221    -- Dynamic_Option --
222    --------------------
223
224    function Dynamic_Option return String is
225    begin
226       return "-shared";
227    end Dynamic_Option;
228
229    -------------------
230    -- Is_Object_Ext --
231    -------------------
232
233    function Is_Object_Ext (Ext : String) return Boolean is
234    begin
235       return Ext = ".o";
236    end Is_Object_Ext;
237
238    --------------
239    -- Is_C_Ext --
240    --------------
241
242    function Is_C_Ext (Ext : String) return Boolean is
243    begin
244       return Ext = ".c";
245    end Is_C_Ext;
246
247    --------------------
248    -- Is_Archive_Ext --
249    --------------------
250
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;
255
256    -------------
257    -- Libgnat --
258    -------------
259
260    function Libgnat return String is
261    begin
262       return "libgnat.a";
263    end Libgnat;
264
265    ------------------------
266    -- Library_Exists_For --
267    ------------------------
268
269    function Library_Exists_For
270      (Project : Project_Id; In_Tree : Project_Tree_Ref) return Boolean
271    is
272    begin
273       if not In_Tree.Projects.Table (Project).Library then
274          Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
275                        "for non library project");
276          return False;
277
278       else
279          declare
280             Lib_Dir : constant String :=
281               Get_Name_String
282                 (In_Tree.Projects.Table (Project).Library_Dir);
283             Lib_Name : constant String :=
284               Get_Name_String
285                 (In_Tree.Projects.Table (Project).Library_Name);
286
287          begin
288             if In_Tree.Projects.Table (Project).Library_Kind =
289                  Static
290             then
291                return Is_Regular_File
292                  (Lib_Dir & Directory_Separator & "lib" &
293                   Fil.Ext_To (Lib_Name, Archive_Ext));
294
295             else
296                return Is_Regular_File
297                  (Lib_Dir & Directory_Separator & "lib" &
298                   Fil.Ext_To (Lib_Name, DLL_Ext));
299             end if;
300          end;
301       end if;
302    end Library_Exists_For;
303
304    ---------------------------
305    -- Library_File_Name_For --
306    ---------------------------
307
308    function Library_File_Name_For
309      (Project : Project_Id;
310       In_Tree : Project_Tree_Ref) return Name_Id
311    is
312    begin
313       if not In_Tree.Projects.Table (Project).Library then
314          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
315                        "for non library project");
316          return No_Name;
317
318       else
319          declare
320             Lib_Name : constant String :=
321               Get_Name_String
322                 (In_Tree.Projects.Table (Project).Library_Name);
323
324          begin
325             Name_Len := 3;
326             Name_Buffer (1 .. Name_Len) := "lib";
327
328             if In_Tree.Projects.Table (Project).Library_Kind =
329                  Static
330             then
331                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
332
333             else
334                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
335             end if;
336
337             return Name_Find;
338          end;
339       end if;
340    end Library_File_Name_For;
341
342    ----------------
343    -- Object_Ext --
344    ----------------
345
346    function Object_Ext return String is
347    begin
348       return "o";
349    end Object_Ext;
350
351    ----------------
352    -- PIC_Option --
353    ----------------
354
355    function PIC_Option return String is
356    begin
357       return "-fPIC";
358    end PIC_Option;
359
360    -----------------------------------------------
361    -- Standalone_Library_Auto_Init_Is_Supported --
362    -----------------------------------------------
363
364    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
365    begin
366       return True;
367    end Standalone_Library_Auto_Init_Is_Supported;
368
369    ---------------------------
370    -- Support_For_Libraries --
371    ---------------------------
372
373    function Support_For_Libraries return Library_Support is
374    begin
375       return Full;
376    end Support_For_Libraries;
377
378 end MLib.Tgt;