OSDN Git Service

* config/arm/crti.asm: Give _init and _fini function type.
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-aix.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             M L I B . T G T                              --
6 --                              (AIX Version)                               --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --           Copyright (C) 2003-2004, 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 -- 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 or relocatable libraries.
30
31 --  This is the AIX version of the body.
32
33 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
34 with GNAT.OS_Lib;       use GNAT.OS_Lib;
35
36 with MLib.Fil;
37 with MLib.Utl;
38 with Namet;    use Namet;
39 with Osint;    use Osint;
40 with Opt;
41 with Output;   use Output;
42 with Prj.Com;
43 with Prj.Util; use Prj.Util;
44
45 package body MLib.Tgt is
46
47    No_Arguments        : aliased Argument_List         := (1 .. 0 => null);
48    Empty_Argument_List : constant Argument_List_Access := No_Arguments'Access;
49
50    Wl_Initfini_String  : constant String := "-Wl,-binitfini:";
51
52    Init_Fini_List      :  constant Argument_List_Access :=
53                             new Argument_List'(1 => null);
54    --  Used to put switch for automatic elaboration/finalization
55
56    Bexpall : aliased String := "-Wl,-bexpall";
57    Bexpall_Option : constant String_Access := Bexpall'Access;
58    --  The switch to export all symbols
59
60    Lpthreads : aliased String := "-lpthreads";
61    Native_Thread_Options : aliased Argument_List := (1 => Lpthreads'Access);
62    --  The switch to use when linking a library against libgnarl when using
63    --  Native threads.
64
65    Lgthreads : aliased String := "-lgthreads";
66    Lmalloc   : aliased String := "-lmalloc";
67    FSU_Thread_Options : aliased Argument_List :=
68                           (1 => Lgthreads'Access, 2 => Lmalloc'Access);
69    --  The switches to use when linking a library against libgnarl when using
70    --  FSU threads.
71
72    Thread_Options : Argument_List_Access := null;
73    --  Designate the thread switches to used when linking a library against
74    --  libgnarl. Depends on the thread library (Native or FSU). Resolved for
75    --  the first library linked against libgnarl.
76
77    ---------------------
78    -- Archive_Builder --
79    ---------------------
80
81    function Archive_Builder return String is
82    begin
83       return "ar";
84    end Archive_Builder;
85
86    -----------------------------
87    -- Archive_Builder_Options --
88    -----------------------------
89
90    function Archive_Builder_Options return String_List_Access is
91    begin
92       return new String_List'(1 => new String'("cr"));
93    end Archive_Builder_Options;
94
95    -----------------
96    -- Archive_Ext --
97    -----------------
98
99    function Archive_Ext return String is
100    begin
101       return "a";
102    end Archive_Ext;
103
104    ---------------------
105    -- Archive_Indexer --
106    ---------------------
107
108    function Archive_Indexer return String is
109    begin
110       return "ranlib";
111    end Archive_Indexer;
112
113    ---------------------------
114    -- Build_Dynamic_Library --
115    ---------------------------
116
117    procedure Build_Dynamic_Library
118      (Ofiles       : Argument_List;
119       Foreign      : Argument_List;
120       Afiles       : Argument_List;
121       Options      : Argument_List;
122       Interfaces   : Argument_List;
123       Lib_Filename : String;
124       Lib_Dir      : String;
125       Symbol_Data  : Symbol_Record;
126       Driver_Name  : Name_Id := No_Name;
127       Lib_Version  : String  := "";
128       Auto_Init    : Boolean := False)
129    is
130       pragma Unreferenced (Foreign);
131       pragma Unreferenced (Afiles);
132       pragma Unreferenced (Interfaces);
133       pragma Unreferenced (Symbol_Data);
134       pragma Unreferenced (Lib_Version);
135
136       Lib_File : constant String :=
137                    Lib_Dir & Directory_Separator & "lib" &
138                    MLib.Fil.Ext_To (Lib_Filename, DLL_Ext);
139       --  The file name of the library
140
141       Init_Fini : Argument_List_Access := Empty_Argument_List;
142       --  The switch for automatic initialization of Stand-Alone Libraries.
143       --  Changed to a real switch when Auto_Init is True.
144
145       Options_2 : Argument_List_Access := Empty_Argument_List;
146       --  Changed to the thread options, if -lgnarl is specified
147
148    begin
149       if Opt.Verbose_Mode then
150          Write_Str ("building relocatable shared library ");
151          Write_Line (Lib_File);
152       end if;
153
154       --  If specified, add automatic elaboration/finalization
155
156       if Auto_Init then
157          Init_Fini := Init_Fini_List;
158          Init_Fini (1) :=
159            new String'(Wl_Initfini_String & Lib_Filename & "init:" &
160                        Lib_Filename & "final");
161       end if;
162
163       --  Look for -lgnarl in Options. If found, set the thread options.
164
165       for J in Options'Range loop
166          if Options (J).all = "-lgnarl" then
167
168             --  If Thread_Options is null, read s-osinte.ads to discover the
169             --  thread library and set Thread_Options accordingly.
170
171             if Thread_Options = null then
172                declare
173                   File : Text_File;
174                   Line : String (1 .. 100);
175                   Last : Natural;
176
177                begin
178                   Open
179                     (File, Include_Dir_Default_Prefix & "/s-osinte.ads");
180
181                   while not End_Of_File (File) loop
182                      Get_Line (File, Line, Last);
183
184                      if Index (Line (1 .. Last), "-lpthreads") /= 0 then
185                         Thread_Options := Native_Thread_Options'Access;
186                         exit;
187
188                      elsif Index (Line (1 .. Last), "-lgthreads") /= 0 then
189                         Thread_Options := FSU_Thread_Options'Access;
190                         exit;
191                      end if;
192                   end loop;
193
194                   Close (File);
195
196                   if Thread_Options = null then
197                      Prj.Com.Fail ("cannot find the thread library in use");
198                   end if;
199
200                exception
201                   when others =>
202                      Prj.Com.Fail ("cannot open s-osinte.ads");
203                end;
204             end if;
205
206             Options_2 := Thread_Options;
207             exit;
208          end if;
209       end loop;
210
211       --  Finally, call GCC (or the driver specified) to build the library
212
213       MLib.Utl.Gcc
214            (Output_File => Lib_File,
215             Objects     => Ofiles,
216             Options     => Options & Bexpall_Option & Init_Fini.all,
217             Driver_Name => Driver_Name,
218             Options_2   => Options_2.all);
219    end Build_Dynamic_Library;
220
221    -------------
222    -- DLL_Ext --
223    -------------
224
225    function DLL_Ext return String is
226    begin
227       return "a";
228    end DLL_Ext;
229
230    --------------------
231    -- Dynamic_Option --
232    --------------------
233
234    function Dynamic_Option return String is
235    begin
236       return "-shared";
237    end Dynamic_Option;
238
239    -------------------
240    -- Is_Object_Ext --
241    -------------------
242
243    function Is_Object_Ext (Ext : String) return Boolean is
244    begin
245       return Ext = ".o";
246    end Is_Object_Ext;
247
248    --------------
249    -- Is_C_Ext --
250    --------------
251
252    function Is_C_Ext (Ext : String) return Boolean is
253    begin
254       return Ext = ".c";
255    end Is_C_Ext;
256
257    --------------------
258    -- Is_Archive_Ext --
259    --------------------
260
261    function Is_Archive_Ext (Ext : String) return Boolean is
262    begin
263       return Ext = ".a";
264    end Is_Archive_Ext;
265
266    -------------
267    -- Libgnat --
268    -------------
269
270    function Libgnat return String is
271    begin
272       return "libgnat.a";
273    end Libgnat;
274
275    ------------------------
276    -- Library_Exists_For --
277    ------------------------
278
279    function Library_Exists_For (Project : Project_Id) return Boolean is
280    begin
281       if not Projects.Table (Project).Library then
282          Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
283                        "for non library project");
284          return False;
285
286       else
287          declare
288             Lib_Dir  : constant String :=
289                          Get_Name_String
290                            (Projects.Table (Project).Library_Dir);
291             Lib_Name : constant String :=
292                          Get_Name_String
293                            (Projects.Table (Project).Library_Name);
294
295          begin
296             if Projects.Table (Project).Library_Kind = Static then
297                return Is_Regular_File
298                  (Lib_Dir & Directory_Separator & "lib" &
299                   Fil.Ext_To (Lib_Name, Archive_Ext));
300
301             else
302                return Is_Regular_File
303                  (Lib_Dir & Directory_Separator & "lib" &
304                   Fil.Ext_To (Lib_Name, DLL_Ext));
305             end if;
306          end;
307       end if;
308    end Library_Exists_For;
309
310    ---------------------------
311    -- Library_File_Name_For --
312    ---------------------------
313
314    function Library_File_Name_For (Project : Project_Id) return Name_Id is
315    begin
316       if not Projects.Table (Project).Library then
317          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
318                        "for non library project");
319          return No_Name;
320
321       else
322          declare
323             Lib_Name : constant String :=
324               Get_Name_String (Projects.Table (Project).Library_Name);
325
326          begin
327             Name_Len := 3;
328             Name_Buffer (1 .. Name_Len) := "lib";
329
330             if Projects.Table (Project).Library_Kind = Static then
331                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
332
333             else
334                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
335             end if;
336
337             return Name_Find;
338          end;
339       end if;
340    end Library_File_Name_For;
341
342    ----------------
343    -- Object_Ext --
344    ----------------
345
346    function Object_Ext return String is
347    begin
348       return "o";
349    end Object_Ext;
350
351    ----------------
352    -- PIC_Option --
353    ----------------
354
355    function PIC_Option return String is
356    begin
357       return "-fPIC";
358    end PIC_Option;
359
360    -----------------------------------------------
361    -- Standalone_Library_Auto_Init_Is_Supported --
362    -----------------------------------------------
363
364    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
365    begin
366       return True;
367    end Standalone_Library_Auto_Init_Is_Supported;
368
369    ---------------------------
370    -- Support_For_Libraries --
371    ---------------------------
372
373    function Support_For_Libraries return Library_Support is
374    begin
375       return Full;
376    end Support_For_Libraries;
377
378 end MLib.Tgt;