OSDN Git Service

2005-03-08 Geert Bosch <bosch@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-darwin.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             M L I B . T G T                              --
6 --                             (Darwin 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 Darwin 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 => new String'("-c"));
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,-flat_namespace"); -- ???
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 "dylib";
218    end DLL_Ext;
219
220    --------------------
221    -- Dynamic_Option --
222    --------------------
223
224    function Dynamic_Option return String is
225    begin
226       return "-dynamiclib";
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 = ".dyld";
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 (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;
275
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);
282
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));
288
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;
297
298    ---------------------------
299    -- Library_File_Name_For --
300    ---------------------------
301
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;
308
309       else
310          declare
311             Lib_Name : constant String :=
312               Get_Name_String (Projects.Table (Project).Library_Name);
313
314          begin
315             Name_Len := 3;
316             Name_Buffer (1 .. Name_Len) := "lib";
317
318             if Projects.Table (Project).Library_Kind = Static then
319                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
320
321             else
322                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
323             end if;
324
325             return Name_Find;
326          end;
327       end if;
328    end Library_File_Name_For;
329
330    ----------------
331    -- Object_Ext --
332    ----------------
333
334    function Object_Ext return String is
335    begin
336       return "o";
337    end Object_Ext;
338
339    ----------------
340    -- PIC_Option --
341    ----------------
342
343    function PIC_Option return String is
344    begin
345       return "-fPIC";
346    end PIC_Option;
347
348    -----------------------------------------------
349    -- Standalone_Library_Auto_Init_Is_Supported --
350    -----------------------------------------------
351
352    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
353    begin
354       return True;
355    end Standalone_Library_Auto_Init_Is_Supported;
356
357    ---------------------------
358    -- Support_For_Libraries --
359    ---------------------------
360
361    function Support_For_Libraries return Library_Support is
362    begin
363       return Full;
364    end Support_For_Libraries;
365
366 end MLib.Tgt;