OSDN Git Service

./:
[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 . S P E C I F I C                    --
6 --                             (Darwin Version)                             --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --          Copyright (C) 2001-2007, 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 is the Darwin version of the body
29
30 with MLib;     use MLib;
31 with MLib.Fil;
32 with MLib.Utl;
33 with Opt;      use Opt;
34 with Output;   use Output;
35
36 with System;
37
38 package body MLib.Tgt.Specific is
39
40    --  Non default subprograms
41
42    function Archive_Indexer_Options return String_List_Access;
43
44    procedure Build_Dynamic_Library
45      (Ofiles       : Argument_List;
46       Foreign      : Argument_List;
47       Afiles       : Argument_List;
48       Options      : Argument_List;
49       Options_2    : Argument_List;
50       Interfaces   : Argument_List;
51       Lib_Filename : String;
52       Lib_Dir      : String;
53       Symbol_Data  : Symbol_Record;
54       Driver_Name  : Name_Id := No_Name;
55       Lib_Version  : String  := "";
56       Auto_Init    : Boolean := False);
57
58    function DLL_Ext return String;
59
60    function Dynamic_Option return String;
61
62    function Is_Archive_Ext (Ext : String) return Boolean;
63
64    --  Local objects
65
66    Flat_Namespace : aliased String := "-Wl,-flat_namespace";
67    --  Instruct the linker to build the shared library as a flat
68    --  namespace image. The default is a two-level namespace image.
69
70    Shared_Libgcc  : aliased String := "-shared-libgcc";
71
72    Shared_Options : constant Argument_List :=
73                                (1 => Flat_Namespace'Access,
74                                 2 => Shared_Libgcc'Access);
75
76    -----------------------------
77    -- Archive_Indexer_Options --
78    -----------------------------
79
80    function Archive_Indexer_Options return String_List_Access is
81    begin
82       return new String_List'(1 => new String'("-c"));
83    end Archive_Indexer_Options;
84
85    ---------------------------
86    -- Build_Dynamic_Library --
87    ---------------------------
88
89    procedure Build_Dynamic_Library
90      (Ofiles       : Argument_List;
91       Foreign      : Argument_List;
92       Afiles       : Argument_List;
93       Options      : Argument_List;
94       Options_2    : Argument_List;
95       Interfaces   : Argument_List;
96       Lib_Filename : String;
97       Lib_Dir      : String;
98       Symbol_Data  : Symbol_Record;
99       Driver_Name  : Name_Id := No_Name;
100       Lib_Version  : String  := "";
101       Auto_Init    : Boolean := False)
102    is
103       pragma Unreferenced (Foreign);
104       pragma Unreferenced (Afiles);
105       pragma Unreferenced (Interfaces);
106       pragma Unreferenced (Symbol_Data);
107       pragma Unreferenced (Auto_Init);
108
109       Lib_File : constant String :=
110                    Lib_Dir & Directory_Separator & "lib" &
111                    Fil.Append_To (Lib_Filename, DLL_Ext);
112
113       Symbolic_Link_Needed : Boolean := False;
114
115    begin
116       if Opt.Verbose_Mode then
117          Write_Str ("building relocatable shared library ");
118          Write_Line (Lib_File);
119       end if;
120
121       --  If specified, add automatic elaboration/finalization
122
123       if Lib_Version = "" then
124          Utl.Gcc
125            (Output_File => Lib_File,
126             Objects     => Ofiles,
127             Options     => Options & Shared_Options,
128             Driver_Name => Driver_Name,
129             Options_2   => Options_2);
130
131       else
132
133          if Is_Absolute_Path (Lib_Version) then
134             Utl.Gcc
135               (Output_File => Lib_Version,
136                Objects     => Ofiles,
137                Options     => Options & Shared_Options,
138                Driver_Name => Driver_Name,
139                Options_2   => Options_2);
140             Symbolic_Link_Needed := Lib_Version /= Lib_File;
141
142          else
143             Utl.Gcc
144               (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
145                Objects     => Ofiles,
146                Options     => Options & Shared_Options,
147                Driver_Name => Driver_Name,
148                Options_2   => Options_2);
149             Symbolic_Link_Needed :=
150               Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
151          end if;
152
153          if Symbolic_Link_Needed then
154             declare
155                Success : Boolean;
156                Oldpath : String (1 .. Lib_Version'Length + 1);
157                Newpath : String (1 .. Lib_File'Length + 1);
158
159                Result : Integer;
160                pragma Unreferenced (Result);
161
162                function Symlink
163                  (Oldpath : System.Address;
164                   Newpath : System.Address) return Integer;
165                pragma Import (C, Symlink, "__gnat_symlink");
166
167             begin
168                Oldpath (1 .. Lib_Version'Length) := Lib_Version;
169                Oldpath (Oldpath'Last)            := ASCII.NUL;
170                Newpath (1 .. Lib_File'Length)    := Lib_File;
171                Newpath (Newpath'Last)            := ASCII.NUL;
172
173                Delete_File (Lib_File, Success);
174
175                Result := Symlink (Oldpath'Address, Newpath'Address);
176             end;
177          end if;
178       end if;
179    end Build_Dynamic_Library;
180
181    -------------
182    -- DLL_Ext --
183    -------------
184
185    function DLL_Ext return String is
186    begin
187       return "dylib";
188    end DLL_Ext;
189
190    --------------------
191    -- Dynamic_Option --
192    --------------------
193
194    function Dynamic_Option return String is
195    begin
196       return "-dynamiclib";
197    end Dynamic_Option;
198
199    --------------------
200    -- Is_Archive_Ext --
201    --------------------
202
203    function Is_Archive_Ext (Ext : String) return Boolean is
204    begin
205       return Ext = ".dylib" or else Ext = ".a";
206    end Is_Archive_Ext;
207
208 begin
209    Archive_Indexer_Options_Ptr := Archive_Indexer_Options'Access;
210    Build_Dynamic_Library_Ptr := Build_Dynamic_Library'Access;
211    DLL_Ext_Ptr := DLL_Ext'Access;
212    Dynamic_Option_Ptr := Dynamic_Option'Access;
213    Is_Archive_Ext_Ptr := Is_Archive_Ext'Access;
214 end MLib.Tgt.Specific;