OSDN Git Service

2004-04-19 Arnaud Charlet <charlet@act-europe.fr>
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5lml-tgt.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-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 ------------------------------------------------------------------------------
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    -- Build_Dynamic_Library --
99    ---------------------------
100
101    procedure Build_Dynamic_Library
102      (Ofiles       : Argument_List;
103       Foreign      : Argument_List;
104       Afiles       : Argument_List;
105       Options      : Argument_List;
106       Interfaces   : Argument_List;
107       Lib_Filename : String;
108       Lib_Dir      : String;
109       Symbol_Data  : Symbol_Record;
110       Driver_Name  : Name_Id := No_Name;
111       Lib_Address  : String  := "";
112       Lib_Version  : String  := "";
113       Relocatable  : Boolean := False;
114       Auto_Init    : Boolean := False)
115    is
116       pragma Unreferenced (Foreign);
117       pragma Unreferenced (Afiles);
118       pragma Unreferenced (Interfaces);
119       pragma Unreferenced (Symbol_Data);
120       pragma Unreferenced (Lib_Address);
121       pragma Unreferenced (Relocatable);
122
123       Lib_File : constant String :=
124         Lib_Dir & Directory_Separator & "lib" &
125         Fil.Ext_To (Lib_Filename, DLL_Ext);
126
127       Version_Arg          : String_Access;
128       Symbolic_Link_Needed : Boolean := False;
129
130       Init_Fini : Argument_List_Access := Empty_Argument_List;
131
132    begin
133       if Opt.Verbose_Mode then
134          Write_Str ("building relocatable shared library ");
135          Write_Line (Lib_File);
136       end if;
137
138       --  If specified, add automatic elaboration/finalization
139       if Auto_Init then
140          Init_Fini := Init_Fini_List;
141          Init_Fini (2) := new String'("-Wl," & Lib_Filename & "init");
142          Init_Fini (4) := new String'("-Wl," & Lib_Filename & "final");
143       end if;
144
145       if Lib_Version = "" then
146          Utl.Gcc
147            (Output_File => Lib_File,
148             Objects     => Ofiles,
149             Options     => Options & Init_Fini.all,
150             Driver_Name => Driver_Name);
151
152       else
153          Version_Arg := new String'("-Wl,-soname," & Lib_Version);
154
155          if Is_Absolute_Path (Lib_Version) then
156             Utl.Gcc
157               (Output_File => Lib_Version,
158                Objects     => Ofiles,
159                Options     => Options & Version_Arg & Init_Fini.all,
160                Driver_Name => Driver_Name);
161             Symbolic_Link_Needed := Lib_Version /= Lib_File;
162
163          else
164             Utl.Gcc
165               (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
166                Objects     => Ofiles,
167                Options     => Options & Version_Arg & Init_Fini.all,
168                Driver_Name => Driver_Name);
169             Symbolic_Link_Needed :=
170               Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
171          end if;
172
173          if Symbolic_Link_Needed then
174             declare
175                Success : Boolean;
176                Oldpath : String (1 .. Lib_Version'Length + 1);
177                Newpath : String (1 .. Lib_File'Length + 1);
178
179                Result : Integer;
180                pragma Unreferenced (Result);
181
182                function Symlink
183                  (Oldpath : System.Address;
184                   Newpath : System.Address) return Integer;
185                pragma Import (C, Symlink, "__gnat_symlink");
186
187             begin
188                Oldpath (1 .. Lib_Version'Length) := Lib_Version;
189                Oldpath (Oldpath'Last)            := ASCII.NUL;
190                Newpath (1 .. Lib_File'Length)    := Lib_File;
191                Newpath (Newpath'Last)            := ASCII.NUL;
192
193                Delete_File (Lib_File, Success);
194
195                Result := Symlink (Oldpath'Address, Newpath'Address);
196             end;
197          end if;
198       end if;
199    end Build_Dynamic_Library;
200
201    -------------------------
202    -- Default_DLL_Address --
203    -------------------------
204
205    function Default_DLL_Address return String is
206    begin
207       return "";
208    end Default_DLL_Address;
209
210    -------------
211    -- DLL_Ext --
212    -------------
213
214    function DLL_Ext return String is
215    begin
216       return "so";
217    end DLL_Ext;
218
219    --------------------
220    -- Dynamic_Option --
221    --------------------
222
223    function Dynamic_Option return String is
224    begin
225       return "-shared";
226    end Dynamic_Option;
227
228    -------------------
229    -- Is_Object_Ext --
230    -------------------
231
232    function Is_Object_Ext (Ext : String) return Boolean is
233    begin
234       return Ext = ".o";
235    end Is_Object_Ext;
236
237    --------------
238    -- Is_C_Ext --
239    --------------
240
241    function Is_C_Ext (Ext : String) return Boolean is
242    begin
243       return Ext = ".c";
244    end Is_C_Ext;
245
246    --------------------
247    -- Is_Archive_Ext --
248    --------------------
249
250    function Is_Archive_Ext (Ext : String) return Boolean is
251    begin
252       return Ext = ".a" or else Ext = ".so";
253    end Is_Archive_Ext;
254
255    -------------
256    -- Libgnat --
257    -------------
258
259    function Libgnat return String is
260    begin
261       return "libgnat.a";
262    end Libgnat;
263
264    ------------------------
265    -- Library_Exists_For --
266    ------------------------
267
268    function Library_Exists_For (Project : Project_Id) return Boolean is
269    begin
270       if not Projects.Table (Project).Library then
271          Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
272                        "for non library project");
273          return False;
274
275       else
276          declare
277             Lib_Dir : constant String :=
278               Get_Name_String (Projects.Table (Project).Library_Dir);
279             Lib_Name : constant String :=
280               Get_Name_String (Projects.Table (Project).Library_Name);
281
282          begin
283             if Projects.Table (Project).Library_Kind = Static then
284                return Is_Regular_File
285                  (Lib_Dir & Directory_Separator & "lib" &
286                   Fil.Ext_To (Lib_Name, Archive_Ext));
287
288             else
289                return Is_Regular_File
290                  (Lib_Dir & Directory_Separator & "lib" &
291                   Fil.Ext_To (Lib_Name, DLL_Ext));
292             end if;
293          end;
294       end if;
295    end Library_Exists_For;
296
297    ---------------------------
298    -- Library_File_Name_For --
299    ---------------------------
300
301    function Library_File_Name_For (Project : Project_Id) return Name_Id is
302    begin
303       if not Projects.Table (Project).Library then
304          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
305                        "for non library project");
306          return No_Name;
307
308       else
309          declare
310             Lib_Name : constant String :=
311               Get_Name_String (Projects.Table (Project).Library_Name);
312
313          begin
314             Name_Len := 3;
315             Name_Buffer (1 .. Name_Len) := "lib";
316
317             if Projects.Table (Project).Library_Kind = Static then
318                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
319
320             else
321                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
322             end if;
323
324             return Name_Find;
325          end;
326       end if;
327    end Library_File_Name_For;
328
329    ----------------
330    -- Object_Ext --
331    ----------------
332
333    function Object_Ext return String is
334    begin
335       return "o";
336    end Object_Ext;
337
338    ----------------
339    -- PIC_Option --
340    ----------------
341
342    function PIC_Option return String is
343    begin
344       return "-fPIC";
345    end PIC_Option;
346
347    -----------------------------------------------
348    -- Standalone_Library_Auto_Init_Is_Supported --
349    -----------------------------------------------
350
351    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
352    begin
353       return True;
354    end Standalone_Library_Auto_Init_Is_Supported;
355
356    ---------------------------
357    -- Support_For_Libraries --
358    ---------------------------
359
360    function Support_For_Libraries return Library_Support is
361    begin
362       return Full;
363    end Support_For_Libraries;
364
365 end MLib.Tgt;