1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             M L I B . T G T                              --
6--                             (HP-UX 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--  libraries (static only on HP-UX).
30
31--  This is the HP-UX version of the body.
32
33with MLib.Fil;
34with MLib.Utl;
35with Namet;  use Namet;
36with Opt;
37with Output; use Output;
38with Prj.Com;
39with System;
40
41package body MLib.Tgt is
42
43   No_Arguments        : aliased Argument_List         := (1 .. 0 => null);
44   Empty_Argument_List : constant Argument_List_Access := No_Arguments'Access;
45
46   Wl_Init_String : aliased String         := "-Wl,+init";
47   Wl_Init        : constant String_Access := Wl_Init_String'Access;
48   Wl_Fini_String : aliased String         := "-Wl,+fini";
49   Wl_Fini        : constant String_Access := Wl_Fini_String'Access;
50
51   Init_Fini_List :  constant Argument_List_Access :=
52                       new Argument_List'(1 => Wl_Init,
53                                          2 => null,
54                                          3 => Wl_Fini,
55                                          4 => null);
56   --  Used to put switches for automatic elaboration/finalization
57   ---------------------
58   -- Archive_Builder --
59   ---------------------
60
61   function Archive_Builder return String is
62   begin
63      return "ar";
64   end Archive_Builder;
65
66   -----------------------------
67   -- Archive_Builder_Options --
68   -----------------------------
69
70   function Archive_Builder_Options return String_List_Access is
71   begin
72      return new String_List'(1 => new String'("cr"));
73   end Archive_Builder_Options;
74
75   -----------------
76   -- Archive_Ext --
77   -----------------
78
79   function Archive_Ext return String is
80   begin
81      return "a";
82   end Archive_Ext;
83
84   ---------------------
85   -- Archive_Indexer --
86   ---------------------
87
88   function Archive_Indexer return String is
89   begin
90      return "ranlib";
91   end Archive_Indexer;
92
93   ---------------------------
94   -- Build_Dynamic_Library --
95   ---------------------------
96
97   procedure Build_Dynamic_Library
98     (Ofiles       : Argument_List;
99      Foreign      : Argument_List;
100      Afiles       : Argument_List;
101      Options      : Argument_List;
102      Interfaces   : Argument_List;
103      Lib_Filename : String;
104      Lib_Dir      : String;
105      Symbol_Data  : Symbol_Record;
106      Driver_Name  : Name_Id := No_Name;
107      Lib_Address  : String  := "";
108      Lib_Version  : String  := "";
109      Relocatable  : Boolean := False;
110      Auto_Init    : Boolean := False)
111   is
112      pragma Unreferenced (Foreign);
113      pragma Unreferenced (Afiles);
114      pragma Unreferenced (Interfaces);
115      pragma Unreferenced (Symbol_Data);
116      pragma Unreferenced (Lib_Address);
117      pragma Unreferenced (Relocatable);
118
119      Lib_File : constant String :=
120        Lib_Dir & Directory_Separator & "lib" &
121        MLib.Fil.Ext_To (Lib_Filename, DLL_Ext);
122
123      Version_Arg          : String_Access;
124      Symbolic_Link_Needed : Boolean := False;
125
126      Init_Fini : Argument_List_Access := Empty_Argument_List;
127
128      Common_Options : constant Argument_List :=
129                         Options & new String'(PIC_Option);
130      --  Common set of options to the gcc command performing the link.
131      --  On HPUX, this command eventually resorts to collect2, which may
132      --  generate a C file and compile it on the fly. This compilation shall
133      --  also generate position independant code for the final link to
134      --  succeed.
135   begin
136      if Opt.Verbose_Mode then
137         Write_Str ("building relocatable shared library ");
138         Write_Line (Lib_File);
139      end if;
140
141      --  If specified, add automatic elaboration/finalization
142      if Auto_Init then
143         Init_Fini := Init_Fini_List;
144         Init_Fini (2) := new String'("-Wl," & Lib_Filename & "init");
145         Init_Fini (4) := new String'("-Wl," & Lib_Filename & "final");
146      end if;
147
148      if Lib_Version = "" then
149         MLib.Utl.Gcc
150           (Output_File => Lib_File,
151            Objects     => Ofiles,
152            Options     => Common_Options & Init_Fini.all,
153            Driver_Name => Driver_Name);
154
155      else
156         Version_Arg := new String'("-Wl,+h," & Lib_Version);
157
158         if Is_Absolute_Path (Lib_Version) then
159            MLib.Utl.Gcc
160              (Output_File => Lib_Version,
161               Objects     => Ofiles,
162               Options     => Common_Options & Version_Arg & Init_Fini.all,
163               Driver_Name => Driver_Name);
164            Symbolic_Link_Needed := Lib_Version /= Lib_File;
165
166         else
167            MLib.Utl.Gcc
168              (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
169               Objects     => Ofiles,
170               Options     => Common_Options & Version_Arg & Init_Fini.all,
171               Driver_Name => Driver_Name);
172            Symbolic_Link_Needed :=
173              Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
174         end if;
175
176         if Symbolic_Link_Needed then
177            declare
178               Success : Boolean;
179               Oldpath : String (1 .. Lib_Version'Length + 1);
180               Newpath : String (1 .. Lib_File'Length + 1);
181
182               Result : Integer;
183               pragma Unreferenced (Result);
184
185               function Symlink
186                 (Oldpath : System.Address;
187                  Newpath : System.Address) return Integer;
188               pragma Import (C, Symlink, "__gnat_symlink");
189
190            begin
191               Oldpath (1 .. Lib_Version'Length) := Lib_Version;
192               Oldpath (Oldpath'Last)            := ASCII.NUL;
193               Newpath (1 .. Lib_File'Length)    := Lib_File;
194               Newpath (Newpath'Last)            := ASCII.NUL;
195
196               Delete_File (Lib_File, Success);
197
198               Result := Symlink (Oldpath'Address, Newpath'Address);
199            end;
200         end if;
201      end if;
202   end Build_Dynamic_Library;
203
204   -------------------------
205   -- Default_DLL_Address --
206   -------------------------
207
208   function Default_DLL_Address return String is
209   begin
210      return "";
211   end Default_DLL_Address;
212
213   -------------
214   -- DLL_Ext --
215   -------------
216
217   function DLL_Ext return String is
218   begin
219      return "sl";
220   end DLL_Ext;
221
222   --------------------
223   -- Dynamic_Option --
224   --------------------
225
226   function Dynamic_Option return String is
227   begin
228      return "-shared";
229   end Dynamic_Option;
230
231   -------------------
232   -- Is_Object_Ext --
233   -------------------
234
235   function Is_Object_Ext (Ext : String) return Boolean is
236   begin
237      return Ext = ".o";
238   end Is_Object_Ext;
239
240   --------------
241   -- Is_C_Ext --
242   --------------
243
244   function Is_C_Ext (Ext : String) return Boolean is
245   begin
246      return Ext = ".c";
247   end Is_C_Ext;
248
249   --------------------
250   -- Is_Archive_Ext --
251   --------------------
252
253   function Is_Archive_Ext (Ext : String) return Boolean is
254   begin
255      return Ext = ".a" or else Ext = ".so";
256   end Is_Archive_Ext;
257
258   -------------
259   -- Libgnat --
260   -------------
261
262   function Libgnat return String is
263   begin
264      return "libgnat.a";
265   end Libgnat;
266
267   ------------------------
268   -- Library_Exists_For --
269   ------------------------
270
271   function Library_Exists_For (Project : Project_Id) return Boolean is
272   begin
273      if not Projects.Table (Project).Library then
274         Prj.Com.Fail ("INTERNAL ERROR: Library_Exists_For called " &
275                       "for non library project");
276         return False;
277
278      else
279         declare
280            Lib_Dir : constant String :=
281              Get_Name_String (Projects.Table (Project).Library_Dir);
282            Lib_Name : constant String :=
283              Get_Name_String (Projects.Table (Project).Library_Name);
284
285         begin
286            if Projects.Table (Project).Library_Kind = Static then
287               return Is_Regular_File
288                 (Lib_Dir & Directory_Separator & "lib" &
289                  Fil.Ext_To (Lib_Name, Archive_Ext));
290
291            else
292               return Is_Regular_File
293                 (Lib_Dir & Directory_Separator & "lib" &
294                  Fil.Ext_To (Lib_Name, DLL_Ext));
295            end if;
296         end;
297      end if;
298   end Library_Exists_For;
299
300   ---------------------------
301   -- Library_File_Name_For --
302   ---------------------------
303
304   function Library_File_Name_For (Project : Project_Id) return Name_Id is
305   begin
306      if not Projects.Table (Project).Library then
307         Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
308                       "for non library project");
309         return No_Name;
310
311      else
312         declare
313            Lib_Name : constant String :=
314              Get_Name_String (Projects.Table (Project).Library_Name);
315
316         begin
317            Name_Len := 3;
318            Name_Buffer (1 .. Name_Len) := "lib";
319
320            if Projects.Table (Project).Library_Kind = Static then
321               Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
322
323            else
324               Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
325            end if;
326
327            return Name_Find;
328         end;
329      end if;
330   end Library_File_Name_For;
331
332   --------------------------------
333   -- Linker_Library_Path_Option --
334   --------------------------------
335
336   function Linker_Library_Path_Option return String_Access is
337   begin
338      return new String'("-Wl,+b,");
339   end Linker_Library_Path_Option;
340
341   ----------------
342   -- Object_Ext --
343   ----------------
344
345   function Object_Ext return String is
346   begin
347      return "o";
348   end Object_Ext;
349
350   ----------------
351   -- PIC_Option --
352   ----------------
353
354   function PIC_Option return String is
355   begin
356      return "-fPIC";
357   end PIC_Option;
358
359   -----------------------------------------------
360   -- Standalone_Library_Auto_Init_Is_Supported --
361   -----------------------------------------------
362
363   function Standalone_Library_Auto_Init_Is_Supported return Boolean is
364   begin
365      return True;
366   end Standalone_Library_Auto_Init_Is_Supported;
367
368   ---------------------------
369   -- Support_For_Libraries --
370   ---------------------------
371
372   function Support_For_Libraries return Library_Support is
373   begin
374      return Full;
375   end Support_For_Libraries;
376
377end MLib.Tgt;
378