OSDN Git Service

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