OSDN Git Service

* sysdep.c: Problem discovered during IA64 VMS port.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5bml-tgt.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, 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 Ada.Text_IO; use Ada.Text_IO;
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 Sdefault;
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    Wl_Initfini_String  : constant String := "-Wl,-binitfini:";
49
50    Init_Fini_List      :  constant Argument_List_Access :=
51                             new Argument_List'(1 => null);
52    --  Used to put switch for automatic elaboration/finalization
53
54    Bexpall : aliased String := "-Wl,-bexpall";
55    Bexpall_Option : constant String_Access := Bexpall'Access;
56    --  The switch to export all symbols
57
58    Lpthreads : aliased String := "-lpthreads";
59    Native_Thread_Options : aliased Argument_List := (1 => Lpthreads'Access);
60    --  The switch to use when linking a library against libgnarl when using
61    --  Native threads.
62
63    Lgthreads : aliased String := "-lgthreads";
64    Lmalloc   : aliased String := "-lmalloc";
65    FSU_Thread_Options : aliased Argument_List :=
66                           (1 => Lgthreads'Access, 2 => Lmalloc'Access);
67    --  The switches to use when linking a library against libgnarl when using
68    --  FSU threads.
69
70    Thread_Options : Argument_List_Access := null;
71    --  Designate the thread switches to used when linking a library against
72    --  libgnarl. Depends on the thread library (Native or FSU). Resolved for
73    --  the first library linked against libgnarl.
74
75    ---------------------
76    -- Archive_Builder --
77    ---------------------
78
79    function Archive_Builder return String is
80    begin
81       return "ar";
82    end Archive_Builder;
83
84    -----------------------------
85    -- Archive_Builder_Options --
86    -----------------------------
87
88    function Archive_Builder_Options return String_List_Access is
89    begin
90       return new String_List'(1 => new String'("cr"));
91    end Archive_Builder_Options;
92
93    -----------------
94    -- Archive_Ext --
95    -----------------
96
97    function Archive_Ext return String is
98    begin
99       return "a";
100    end Archive_Ext;
101
102    ---------------------
103    -- Archive_Indexer --
104    ---------------------
105
106    function Archive_Indexer return String is
107    begin
108       return "ranlib";
109    end Archive_Indexer;
110
111    ---------------------------
112    -- Build_Dynamic_Library --
113    ---------------------------
114
115    procedure Build_Dynamic_Library
116      (Ofiles       : Argument_List;
117       Foreign      : Argument_List;
118       Afiles       : Argument_List;
119       Options      : Argument_List;
120       Interfaces   : Argument_List;
121       Lib_Filename : String;
122       Lib_Dir      : String;
123       Driver_Name  : Name_Id := No_Name;
124       Lib_Address  : String  := "";
125       Lib_Version  : String  := "";
126       Relocatable  : Boolean := False;
127       Auto_Init    : Boolean := False)
128    is
129       pragma Unreferenced (Foreign);
130       pragma Unreferenced (Afiles);
131       pragma Unreferenced (Interfaces);
132       pragma Unreferenced (Lib_Address);
133       pragma Unreferenced (Lib_Version);
134       pragma Unreferenced (Relocatable);
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 : Ada.Text_IO.File_Type;
174                   Line : String (1 .. 100);
175                   Last : Natural;
176
177                begin
178                   Open (File, In_File,
179                         Sdefault.Include_Dir_Default_Name.all &
180                         "/s-osinte.ads");
181
182                   while not End_Of_File (File) loop
183                      Get_Line (File, Line, Last);
184
185                      if Index (Line (1 .. Last), "-lpthreads") /= 0 then
186                         Thread_Options := Native_Thread_Options'Access;
187                         exit;
188
189                      elsif Index (Line (1 .. Last), "-lgthreads") /= 0 then
190                         Thread_Options := FSU_Thread_Options'Access;
191                         exit;
192                      end if;
193                   end loop;
194
195                   Close (File);
196
197                   if Thread_Options = null then
198                      Prj.Com.Fail ("cannot find the thread library in use");
199                   end if;
200
201                exception
202                   when others =>
203                      Prj.Com.Fail ("cannot open s-osinte.ads");
204                end;
205             end if;
206
207             Options_2 := Thread_Options;
208             exit;
209          end if;
210       end loop;
211
212       --  Finally, call GCC (or the driver specified) to build the library
213
214       MLib.Utl.Gcc
215            (Output_File => Lib_File,
216             Objects     => Ofiles,
217             Options     => Options & Bexpall_Option & Init_Fini.all,
218             Driver_Name => Driver_Name,
219             Options_2   => Options_2.all);
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 "";
229    end Default_DLL_Address;
230
231    -------------
232    -- DLL_Ext --
233    -------------
234
235    function DLL_Ext return String is
236    begin
237       return "a";
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";
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 (Projects.Table (Project).Library_Dir);
300             Lib_Name : constant String :=
301               Get_Name_String (Projects.Table (Project).Library_Name);
302
303          begin
304             if Projects.Table (Project).Library_Kind = Static then
305                return Is_Regular_File
306                  (Lib_Dir & Directory_Separator & "lib" &
307                   Fil.Ext_To (Lib_Name, Archive_Ext));
308
309             else
310                return Is_Regular_File
311                  (Lib_Dir & Directory_Separator & "lib" &
312                   Fil.Ext_To (Lib_Name, DLL_Ext));
313             end if;
314          end;
315       end if;
316    end Library_Exists_For;
317
318    ---------------------------
319    -- Library_File_Name_For --
320    ---------------------------
321
322    function Library_File_Name_For (Project : Project_Id) return Name_Id is
323    begin
324       if not Projects.Table (Project).Library then
325          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
326                        "for non library project");
327          return No_Name;
328
329       else
330          declare
331             Lib_Name : constant String :=
332               Get_Name_String (Projects.Table (Project).Library_Name);
333
334          begin
335             Name_Len := 3;
336             Name_Buffer (1 .. Name_Len) := "lib";
337
338             if Projects.Table (Project).Library_Kind = Static then
339                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
340
341             else
342                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
343             end if;
344
345             return Name_Find;
346          end;
347       end if;
348    end Library_File_Name_For;
349
350    --------------------------------
351    -- Linker_Library_Path_Option --
352    --------------------------------
353
354    function Linker_Library_Path_Option return String_Access is
355    begin
356       --  On AIX, any path specify with an -L switch is automatically added
357       --  to the library path. So, nothing is needed here.
358
359       return null;
360    end Linker_Library_Path_Option;
361
362    ----------------
363    -- Object_Ext --
364    ----------------
365
366    function Object_Ext return String is
367    begin
368       return "o";
369    end Object_Ext;
370
371    ----------------
372    -- PIC_Option --
373    ----------------
374
375    function PIC_Option return String is
376    begin
377       return "-fPIC";
378    end PIC_Option;
379
380    -----------------------------------------------
381    -- Standalone_Library_Auto_Init_Is_Supported --
382    -----------------------------------------------
383
384    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
385    begin
386       return True;
387    end Standalone_Library_Auto_Init_Is_Supported;
388
389    ---------------------------
390    -- Support_For_Libraries --
391    ---------------------------
392
393    function Support_For_Libraries return Library_Support is
394    begin
395       return Full;
396    end Support_For_Libraries;
397
398 end MLib.Tgt;