OSDN Git Service

2004-06-14 Pascal Obry <obry@gnat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-mingw.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             M L I B . T G T                              --
6 --                            (Windows Version)                             --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --          Copyright (C) 2002-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 Windows version of the body. Works only with GCC versions
32 --  supporting the "-shared" option.
33
34 with Namet;  use Namet;
35 with Opt;
36 with Output; use Output;
37 with Prj.Com;
38
39 with GNAT.OS_Lib; use GNAT.OS_Lib;
40
41 with MLib.Fil;
42 with MLib.Utl;
43
44 package body MLib.Tgt is
45
46    package Files renames MLib.Fil;
47    package Tools renames MLib.Utl;
48
49    ---------------------
50    -- Archive_Builder --
51    ---------------------
52
53    function Archive_Builder return String is
54    begin
55       return "ar";
56    end Archive_Builder;
57
58    -----------------------------
59    -- Archive_Builder_Options --
60    -----------------------------
61
62    function Archive_Builder_Options return String_List_Access is
63    begin
64       return new String_List'(1 => new String'("cr"));
65    end Archive_Builder_Options;
66
67    -----------------
68    -- Archive_Ext --
69    -----------------
70
71    function Archive_Ext return  String is
72    begin
73       return "a";
74    end Archive_Ext;
75
76    ---------------------
77    -- Archive_Indexer --
78    ---------------------
79
80    function Archive_Indexer return String is
81    begin
82       return "ranlib";
83    end Archive_Indexer;
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       Interfaces   : Argument_List;
95       Lib_Filename : String;
96       Lib_Dir      : String;
97       Symbol_Data  : Symbol_Record;
98       Driver_Name  : Name_Id := No_Name;
99       Lib_Address  : String  := "";
100       Lib_Version  : String  := "";
101       Relocatable  : Boolean := False;
102       Auto_Init    : Boolean := False)
103    is
104       pragma Unreferenced (Foreign);
105       pragma Unreferenced (Afiles);
106       pragma Unreferenced (Auto_Init);
107       pragma Unreferenced (Symbol_Data);
108       pragma Unreferenced (Interfaces);
109       pragma Unreferenced (Lib_Version);
110
111       Strip_Name  : constant String := "strip";
112       Strip_Exec  : String_Access;
113
114       procedure Strip_Reloc (Lib_File : String);
115       --  Strip .reloc section to build a non relocatable DLL
116
117       -----------------
118       -- Strip_Reloc --
119       -----------------
120
121       procedure Strip_Reloc (Lib_File : String) is
122          Arguments   : Argument_List (1 .. 3);
123          Success     : Boolean;
124          Line_Length : Natural;
125
126       begin
127          --  Look for strip executable
128
129          Strip_Exec := Locate_Exec_On_Path (Strip_Name);
130
131          if Strip_Exec = null then
132             Fail (Strip_Name, " not found in path");
133
134          elsif Opt.Verbose_Mode then
135             Write_Str  ("found ");
136             Write_Line (Strip_Exec.all);
137          end if;
138
139          --  Call it: strip -R .reloc <dll>
140
141          Arguments (1) := new String'("-R");
142          Arguments (2) := new String'(".reloc");
143          Arguments (3) := new String'(Lib_File);
144
145          if not Opt.Quiet_Output then
146             Write_Str (Strip_Exec.all);
147             Line_Length := Strip_Exec'Length;
148
149             for K in Arguments'Range loop
150
151                --  Make sure the Output buffer does not overflow
152
153                if Line_Length + 1 + Arguments (K)'Length >
154                  Integer (Opt.Max_Line_Length)
155                then
156                   Write_Eol;
157                   Line_Length := 0;
158                end if;
159
160                Write_Char (' ');
161                Write_Str  (Arguments (K).all);
162                Line_Length := Line_Length + 1 + Arguments (K)'Length;
163             end loop;
164
165             Write_Eol;
166          end if;
167
168          Spawn (Strip_Exec.all, Arguments, Success);
169
170          if not Success then
171             Fail (Strip_Name, " execution error.");
172          end if;
173
174          for K in Arguments'Range loop
175             Free (Arguments (K));
176          end loop;
177       end Strip_Reloc;
178
179       Lib_File : constant String :=
180         Lib_Dir & Directory_Separator & "lib" &
181         Files.Ext_To (Lib_Filename, DLL_Ext);
182
183       I_Base    : aliased String := "-Wl,--image-base," & Lib_Address;
184
185       Options_2 : Argument_List (1 .. 1);
186       O_Index   : Natural := 0;
187
188    --  Start of processing for Build_Dynamic_Library
189
190    begin
191       if Opt.Verbose_Mode then
192          Write_Str ("building ");
193
194          if not Relocatable then
195             Write_Str ("non-");
196          end if;
197
198          Write_Str ("relocatable shared library ");
199          Write_Line (Lib_File);
200       end if;
201
202       if not Relocatable then
203          O_Index := O_Index + 1;
204          Options_2 (O_Index) := I_Base'Unchecked_Access;
205       end if;
206
207       Tools.Gcc
208         (Output_File => Lib_File,
209          Objects     => Ofiles,
210          Options     => Options,
211          Driver_Name => Driver_Name,
212          Options_2   => Options_2 (1 .. O_Index));
213
214       if not Relocatable then
215
216          --  Strip reloc symbols from the DLL
217
218          Strip_Reloc (Lib_File);
219       end if;
220    end Build_Dynamic_Library;
221
222    -------------------------
223    -- Default_DLL_Address --
224    -------------------------
225
226    function Default_DLL_Address return String is
227    begin
228       return "0x11000000";
229    end Default_DLL_Address;
230
231    -------------
232    -- DLL_Ext --
233    -------------
234
235    function DLL_Ext return String is
236    begin
237       return "dll";
238    end DLL_Ext;
239
240    --------------------
241    -- Dynamic_Option --
242    --------------------
243
244    function Dynamic_Option return String is
245    begin
246       return "-shared";
247    end Dynamic_Option;
248
249    -------------------
250    -- Is_Object_Ext --
251    -------------------
252
253    function Is_Object_Ext (Ext : String) return Boolean is
254    begin
255       return Ext = ".o";
256    end Is_Object_Ext;
257
258    --------------
259    -- Is_C_Ext --
260    --------------
261
262    function Is_C_Ext (Ext : String) return Boolean is
263    begin
264       return Ext = ".c";
265    end Is_C_Ext;
266
267    --------------------
268    -- Is_Archive_Ext --
269    --------------------
270
271    function Is_Archive_Ext (Ext : String) return Boolean is
272    begin
273       return Ext = ".a" or else Ext = ".dll";
274    end Is_Archive_Ext;
275
276    -------------
277    -- Libgnat --
278    -------------
279
280    function Libgnat return String is
281    begin
282       return "libgnat.a";
283    end Libgnat;
284
285    ------------------------
286    -- Library_Exists_For --
287    ------------------------
288
289    function Library_Exists_For (Project : Project_Id) return Boolean is
290    begin
291       if not Projects.Table (Project).Library then
292          Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
293                        "for non library project");
294          return False;
295
296       else
297          declare
298             Lib_Dir : constant String :=
299                         Get_Name_String
300                           (Projects.Table (Project).Library_Dir);
301             Lib_Name : constant String :=
302                          Get_Name_String
303                            (Projects.Table (Project).Library_Name);
304
305          begin
306             if Projects.Table (Project).Library_Kind = Static then
307                return Is_Regular_File
308                  (Lib_Dir & Directory_Separator & "lib" &
309                   MLib.Fil.Ext_To (Lib_Name, Archive_Ext));
310
311             else
312                return Is_Regular_File
313                  (Lib_Dir & Directory_Separator & "lib" &
314                   MLib.Fil.Ext_To (Lib_Name, DLL_Ext));
315             end if;
316          end;
317       end if;
318    end Library_Exists_For;
319
320    ---------------------------
321    -- Library_File_Name_For --
322    ---------------------------
323
324    function Library_File_Name_For (Project : Project_Id) return Name_Id is
325    begin
326       if not Projects.Table (Project).Library then
327          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
328                        "for non library project");
329          return No_Name;
330
331       else
332          declare
333             Lib_Name : constant String :=
334               Get_Name_String (Projects.Table (Project).Library_Name);
335
336          begin
337             Name_Len := 3;
338             Name_Buffer (1 .. Name_Len) := "lib";
339
340             if Projects.Table (Project).Library_Kind = Static then
341                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
342
343             else
344                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
345             end if;
346
347             return Name_Find;
348          end;
349       end if;
350    end Library_File_Name_For;
351
352    ----------------
353    -- Object_Ext --
354    ----------------
355
356    function Object_Ext return String is
357    begin
358       return "o";
359    end Object_Ext;
360
361    ----------------
362    -- PIC_Option --
363    ----------------
364
365    function PIC_Option return String is
366    begin
367       return "";
368    end PIC_Option;
369
370    -----------------------------------------------
371    -- Standalone_Library_Auto_Init_Is_Supported --
372    -----------------------------------------------
373
374    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
375    begin
376       return False;
377    end Standalone_Library_Auto_Init_Is_Supported;
378
379    ---------------------------
380    -- Support_For_Libraries --
381    ---------------------------
382
383    function Support_For_Libraries return Library_Support is
384    begin
385       return Full;
386    end Support_For_Libraries;
387
388 end MLib.Tgt;