Fix thinko
[official-gcc.git] / gcc / ada / 5lml-tgt.adb
blobf884381d5ef9bef5283dec69fc070fce82154c76
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, Ada Core Technologies, 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 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 -- This package provides a set of target dependent routines to build
29 -- static, dynamic and shared libraries.
31 -- This is the GNU/Linux version of the body.
33 with Ada.Characters.Handling; use Ada.Characters.Handling;
34 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
35 with MLib.Fil;
36 with MLib.Utl;
37 with Namet; use Namet;
38 with Opt;
39 with Osint; use Osint;
40 with Output; use Output;
41 with System;
43 package body MLib.Tgt is
45 use GNAT;
46 use MLib;
48 -- ??? serious lack of comments below, all these declarations need to
49 -- be commented, none are:
51 package Files renames MLib.Fil;
52 package Tools renames MLib.Utl;
54 Args : Argument_List_Access := new Argument_List (1 .. 20);
55 Last_Arg : Natural := 0;
57 Cp : constant String_Access := Locate_Exec_On_Path ("cp");
58 Force : constant String_Access := new String'("-f");
60 procedure Add_Arg (Arg : String);
62 -------------
63 -- Add_Arg --
64 -------------
66 procedure Add_Arg (Arg : String) is
67 begin
68 if Last_Arg = Args'Last then
69 declare
70 New_Args : constant Argument_List_Access :=
71 new Argument_List (1 .. Args'Last * 2);
73 begin
74 New_Args (Args'Range) := Args.all;
75 Args := New_Args;
76 end;
77 end if;
79 Last_Arg := Last_Arg + 1;
80 Args (Last_Arg) := new String'(Arg);
81 end Add_Arg;
83 -----------------
84 -- Archive_Ext --
85 -----------------
87 function Archive_Ext return String is
88 begin
89 return "a";
90 end Archive_Ext;
92 -----------------
93 -- Base_Option --
94 -----------------
96 function Base_Option return String is
97 begin
98 return "";
99 end Base_Option;
101 ---------------------------
102 -- Build_Dynamic_Library --
103 ---------------------------
105 procedure Build_Dynamic_Library
106 (Ofiles : Argument_List;
107 Foreign : Argument_List;
108 Afiles : Argument_List;
109 Options : Argument_List;
110 Lib_Filename : String;
111 Lib_Dir : String;
112 Lib_Address : String := "";
113 Lib_Version : String := "";
114 Relocatable : Boolean := False)
116 Lib_File : constant String :=
117 Lib_Dir & Directory_Separator & "lib" &
118 Files.Ext_To (Lib_Filename, DLL_Ext);
120 use type Argument_List;
121 use type String_Access;
123 Version_Arg : String_Access;
125 Symbolic_Link_Needed : Boolean := False;
127 begin
128 if Opt.Verbose_Mode then
129 Write_Str ("building relocatable shared library ");
130 Write_Line (Lib_File);
131 end if;
133 if Lib_Version = "" then
134 Tools.Gcc
135 (Output_File => Lib_File,
136 Objects => Ofiles,
137 Options => Options);
139 else
140 Version_Arg := new String'("-Wl,-soname," & Lib_Version);
142 if Is_Absolute_Path (Lib_Version) then
143 Tools.Gcc
144 (Output_File => Lib_Version,
145 Objects => Ofiles,
146 Options => Options & Version_Arg);
147 Symbolic_Link_Needed := Lib_Version /= Lib_File;
149 else
150 Tools.Gcc
151 (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
152 Objects => Ofiles,
153 Options => Options & Version_Arg);
154 Symbolic_Link_Needed :=
155 Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
156 end if;
158 if Symbolic_Link_Needed then
159 declare
160 Success : Boolean;
161 Oldpath : String (1 .. Lib_Version'Length + 1);
162 Newpath : String (1 .. Lib_File'Length + 1);
163 Result : Integer;
165 function Symlink
166 (Oldpath : System.Address;
167 Newpath : System.Address)
168 return Integer;
169 pragma Import (C, Symlink, "__gnat_symlink");
171 begin
172 Oldpath (1 .. Lib_Version'Length) := Lib_Version;
173 Oldpath (Oldpath'Last) := ASCII.NUL;
174 Newpath (1 .. Lib_File'Length) := Lib_File;
175 Newpath (Newpath'Last) := ASCII.NUL;
177 Delete_File (Lib_File, Success);
179 Result := Symlink (Oldpath'Address, Newpath'Address);
180 end;
181 end if;
182 end if;
183 end Build_Dynamic_Library;
185 --------------------
186 -- Copy_ALI_Files --
187 --------------------
189 procedure Copy_ALI_Files
190 (From : Name_Id;
191 To : Name_Id)
193 Dir : Dir_Type;
194 Name : String (1 .. 1_000);
195 Last : Natural;
196 Success : Boolean;
197 From_Dir : constant String := Get_Name_String (From);
198 To_Dir : constant String_Access :=
199 new String'(Get_Name_String (To));
201 begin
202 Last_Arg := 0;
203 Open (Dir, From_Dir);
205 loop
206 Read (Dir, Name, Last);
207 exit when Last = 0;
208 if Last > 4
210 and then
211 To_Lower (Name (Last - 3 .. Last)) = ".ali"
212 then
213 Add_Arg (From_Dir & Directory_Separator & Name (1 .. Last));
214 end if;
215 end loop;
217 if Last_Arg /= 0 then
218 if not Opt.Quiet_Output then
219 Write_Str ("cp -f ");
221 for J in 1 .. Last_Arg loop
222 Write_Str (Args (J).all);
223 Write_Char (' ');
224 end loop;
226 Write_Line (To_Dir.all);
227 end if;
229 Spawn (Cp.all,
230 Force & Args (1 .. Last_Arg) & To_Dir,
231 Success);
233 if not Success then
234 Fail ("could not copy ALI files to library dir");
235 end if;
236 end if;
237 end Copy_ALI_Files;
239 -------------------------
240 -- Default_DLL_Address --
241 -------------------------
243 function Default_DLL_Address return String is
244 begin
245 return "";
246 end Default_DLL_Address;
248 -------------
249 -- DLL_Ext --
250 -------------
252 function DLL_Ext return String is
253 begin
254 return "so";
255 end DLL_Ext;
257 --------------------
258 -- Dynamic_Option --
259 --------------------
261 function Dynamic_Option return String is
262 begin
263 return "-shared";
264 end Dynamic_Option;
266 -------------------
267 -- Is_Object_Ext --
268 -------------------
270 function Is_Object_Ext (Ext : String) return Boolean is
271 begin
272 return Ext = ".o";
273 end Is_Object_Ext;
275 --------------
276 -- Is_C_Ext --
277 --------------
279 function Is_C_Ext (Ext : String) return Boolean is
280 begin
281 return Ext = ".c";
282 end Is_C_Ext;
284 --------------------
285 -- Is_Archive_Ext --
286 --------------------
288 function Is_Archive_Ext (Ext : String) return Boolean is
289 begin
290 return Ext = ".a" or else Ext = ".so";
291 end Is_Archive_Ext;
293 -------------
294 -- Libgnat --
295 -------------
297 function Libgnat return String is
298 begin
299 return "libgnat.a";
300 end Libgnat;
302 -----------------------------
303 -- Libraries_Are_Supported --
304 -----------------------------
306 function Libraries_Are_Supported return Boolean is
307 begin
308 return True;
309 end Libraries_Are_Supported;
311 --------------------------------
312 -- Linker_Library_Path_Option --
313 --------------------------------
315 function Linker_Library_Path_Option
316 (Directory : String)
317 return String_Access
319 begin
320 return new String'("-Wl,-rpath," & Directory);
321 end Linker_Library_Path_Option;
323 ----------------
324 -- Object_Ext --
325 ----------------
327 function Object_Ext return String is
328 begin
329 return "o";
330 end Object_Ext;
332 ----------------
333 -- PIC_Option --
334 ----------------
336 function PIC_Option return String is
337 begin
338 return "-fPIC";
339 end PIC_Option;
341 end MLib.Tgt;