OSDN Git Service

* approved by rth
[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 --                                                                          --
11 --              Copyright (C) 2001, Ada Core Technologies, Inc.             --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 --  This package provides a set of target dependent routines to build
30 --  static, dynamic and shared libraries.
31
32 --  This is the GNU/Linux version of the body.
33
34 with Ada.Characters.Handling;   use Ada.Characters.Handling;
35 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
36 with MLib.Fil;
37 with MLib.Utl;
38 with Namet;       use Namet;
39 with Opt;
40 with Osint;       use Osint;
41 with Output;      use Output;
42 with System;
43
44 package body MLib.Tgt is
45
46    use GNAT;
47    use MLib;
48
49    --  ??? serious lack of comments below, all these declarations need to
50    --  be commented, none are:
51
52    package Files renames MLib.Fil;
53    package Tools renames MLib.Utl;
54
55    Args : Argument_List_Access := new Argument_List (1 .. 20);
56    Last_Arg : Natural := 0;
57
58    Cp      : constant String_Access := Locate_Exec_On_Path ("cp");
59    Force   : constant String_Access := new String'("-f");
60
61    procedure Add_Arg (Arg : String);
62
63    -------------
64    -- Add_Arg --
65    -------------
66
67    procedure Add_Arg (Arg : String) is
68    begin
69       if Last_Arg = Args'Last then
70          declare
71             New_Args : constant Argument_List_Access :=
72                          new Argument_List (1 .. Args'Last * 2);
73
74          begin
75             New_Args (Args'Range) := Args.all;
76             Args := New_Args;
77          end;
78       end if;
79
80       Last_Arg := Last_Arg + 1;
81       Args (Last_Arg) := new String'(Arg);
82    end Add_Arg;
83
84    -----------------
85    -- Archive_Ext --
86    -----------------
87
88    function Archive_Ext return  String is
89    begin
90       return  "a";
91    end Archive_Ext;
92
93    -----------------
94    -- Base_Option --
95    -----------------
96
97    function Base_Option return String is
98    begin
99       return "";
100    end Base_Option;
101
102    ---------------------------
103    -- Build_Dynamic_Library --
104    ---------------------------
105
106    procedure Build_Dynamic_Library
107      (Ofiles       : Argument_List;
108       Foreign      : Argument_List;
109       Afiles       : Argument_List;
110       Options      : Argument_List;
111       Lib_Filename : String;
112       Lib_Dir      : String;
113       Lib_Address  : String  := "";
114       Lib_Version  : String  := "";
115       Relocatable  : Boolean := False)
116    is
117       Lib_File : constant String :=
118         Lib_Dir & Directory_Separator & "lib" &
119         Files.Ext_To (Lib_Filename, DLL_Ext);
120
121       use type Argument_List;
122       use type String_Access;
123
124       Version_Arg  : String_Access;
125
126       Symbolic_Link_Needed : Boolean := False;
127
128    begin
129       if Opt.Verbose_Mode then
130          Write_Str ("building relocatable shared library ");
131          Write_Line (Lib_File);
132       end if;
133
134       if Lib_Version = "" then
135          Tools.Gcc
136            (Output_File => Lib_File,
137             Objects     => Ofiles,
138             Options     => Options);
139
140       else
141          Version_Arg := new String'("-Wl,-soname," & Lib_Version);
142
143          if Is_Absolute_Path (Lib_Version) then
144             Tools.Gcc
145               (Output_File => Lib_Version,
146                Objects     => Ofiles,
147                Options     => Options & Version_Arg);
148             Symbolic_Link_Needed := Lib_Version /= Lib_File;
149
150          else
151             Tools.Gcc
152               (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
153                Objects     => Ofiles,
154                Options     => Options & Version_Arg);
155             Symbolic_Link_Needed :=
156               Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
157          end if;
158
159          if Symbolic_Link_Needed then
160             declare
161                Success : Boolean;
162                Oldpath : String (1 .. Lib_Version'Length + 1);
163                Newpath : String (1 .. Lib_File'Length + 1);
164                Result  : Integer;
165
166                function Symlink
167                  (Oldpath : System.Address;
168                   Newpath : System.Address)
169                   return    Integer;
170                pragma Import (C, Symlink, "__gnat_symlink");
171
172             begin
173                Oldpath (1 .. Lib_Version'Length) := Lib_Version;
174                Oldpath (Oldpath'Last)            := ASCII.NUL;
175                Newpath (1 .. Lib_File'Length)    := Lib_File;
176                Newpath (Newpath'Last)            := ASCII.NUL;
177
178                Delete_File (Lib_File, Success);
179
180                Result := Symlink (Oldpath'Address, Newpath'Address);
181             end;
182          end if;
183       end if;
184    end Build_Dynamic_Library;
185
186    --------------------
187    -- Copy_ALI_Files --
188    --------------------
189
190    procedure Copy_ALI_Files
191      (From : Name_Id;
192       To   : Name_Id)
193    is
194       Dir      : Dir_Type;
195       Name     : String (1 .. 1_000);
196       Last     : Natural;
197       Success  : Boolean;
198       From_Dir : constant String := Get_Name_String (From);
199       To_Dir   : constant String_Access :=
200                    new String'(Get_Name_String (To));
201
202    begin
203       Last_Arg := 0;
204       Open (Dir, From_Dir);
205
206       loop
207          Read (Dir, Name, Last);
208          exit when Last = 0;
209          if Last > 4
210
211            and then
212            To_Lower (Name (Last - 3 .. Last)) = ".ali"
213          then
214             Add_Arg (From_Dir & Directory_Separator & Name (1 .. Last));
215          end if;
216       end loop;
217
218       if Last_Arg /= 0 then
219          if not Opt.Quiet_Output then
220             Write_Str ("cp -f ");
221
222             for J in 1 .. Last_Arg loop
223                Write_Str (Args (J).all);
224                Write_Char (' ');
225             end loop;
226
227             Write_Line (To_Dir.all);
228          end if;
229
230          Spawn (Cp.all,
231                 Force & Args (1 .. Last_Arg) & To_Dir,
232                 Success);
233
234          if not Success then
235             Fail ("could not copy ALI files to library dir");
236          end if;
237       end if;
238    end Copy_ALI_Files;
239
240    -------------------------
241    -- Default_DLL_Address --
242    -------------------------
243
244    function Default_DLL_Address return String is
245    begin
246       return "";
247    end Default_DLL_Address;
248
249    -------------
250    -- DLL_Ext --
251    -------------
252
253    function DLL_Ext return String is
254    begin
255       return "so";
256    end DLL_Ext;
257
258    --------------------
259    -- Dynamic_Option --
260    --------------------
261
262    function Dynamic_Option return String is
263    begin
264       return  "-shared";
265    end Dynamic_Option;
266
267    -------------------
268    -- Is_Object_Ext --
269    -------------------
270
271    function Is_Object_Ext (Ext : String) return Boolean is
272    begin
273       return Ext = ".o";
274    end Is_Object_Ext;
275
276    --------------
277    -- Is_C_Ext --
278    --------------
279
280    function Is_C_Ext (Ext : String) return Boolean is
281    begin
282       return Ext = ".c";
283    end Is_C_Ext;
284
285    --------------------
286    -- Is_Archive_Ext --
287    --------------------
288
289    function Is_Archive_Ext (Ext : String) return Boolean is
290    begin
291       return Ext = ".a" or else Ext = ".so";
292    end Is_Archive_Ext;
293
294    -------------
295    -- Libgnat --
296    -------------
297
298    function Libgnat return String is
299    begin
300       return "libgnat.a";
301    end Libgnat;
302
303    -----------------------------
304    -- Libraries_Are_Supported --
305    -----------------------------
306
307    function Libraries_Are_Supported return Boolean is
308    begin
309       return True;
310    end Libraries_Are_Supported;
311
312    --------------------------------
313    -- Linker_Library_Path_Option --
314    --------------------------------
315
316    function Linker_Library_Path_Option
317      (Directory : String)
318       return      String_Access
319    is
320    begin
321       return new String'("-Wl,-rpath," & Directory);
322    end Linker_Library_Path_Option;
323
324    ----------------
325    -- Object_Ext --
326    ----------------
327
328    function Object_Ext return String is
329    begin
330       return  "o";
331    end Object_Ext;
332
333    ----------------
334    -- PIC_Option --
335    ----------------
336
337    function PIC_Option return String is
338    begin
339       return  "-fPIC";
340    end PIC_Option;
341
342 end MLib.Tgt;