OSDN Git Service

2007-04-20 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-hpux.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                     M L I B . T G T . S P E C I F I C                    --
6 --                             (HP-UX Version)                              --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --                     Copyright (C) 2003-2007, 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 is the HP-UX version of the body
29
30 with MLib.Fil;
31 with MLib.Utl;
32 with Opt;
33 with Output; use Output;
34 with System;
35
36 package body MLib.Tgt.Specific is
37
38    --  Non default subprograms
39
40    procedure Build_Dynamic_Library
41      (Ofiles       : Argument_List;
42       Foreign      : Argument_List;
43       Afiles       : Argument_List;
44       Options      : Argument_List;
45       Options_2    : Argument_List;
46       Interfaces   : Argument_List;
47       Lib_Filename : String;
48       Lib_Dir      : String;
49       Symbol_Data  : Symbol_Record;
50       Driver_Name  : Name_Id := No_Name;
51       Lib_Version  : String  := "";
52       Auto_Init    : Boolean := False);
53
54    function DLL_Ext return String;
55
56    function Is_Archive_Ext (Ext : String) return Boolean;
57
58    ---------------------------
59    -- Build_Dynamic_Library --
60    ---------------------------
61
62    procedure Build_Dynamic_Library
63      (Ofiles       : Argument_List;
64       Foreign      : Argument_List;
65       Afiles       : Argument_List;
66       Options      : Argument_List;
67       Options_2    : Argument_List;
68       Interfaces   : Argument_List;
69       Lib_Filename : String;
70       Lib_Dir      : String;
71       Symbol_Data  : Symbol_Record;
72       Driver_Name  : Name_Id := No_Name;
73       Lib_Version  : String  := "";
74       Auto_Init    : Boolean := False)
75    is
76       pragma Unreferenced (Foreign);
77       pragma Unreferenced (Afiles);
78       pragma Unreferenced (Interfaces);
79       pragma Unreferenced (Symbol_Data);
80       pragma Unreferenced (Auto_Init);
81
82       Lib_File : constant String :=
83                    Lib_Dir & Directory_Separator & "lib" &
84                    MLib.Fil.Append_To (Lib_Filename, DLL_Ext);
85
86       Version_Arg          : String_Access;
87       Symbolic_Link_Needed : Boolean := False;
88
89       Common_Options : constant Argument_List :=
90                          Options & new String'(PIC_Option);
91       --  Common set of options to the gcc command performing the link.
92       --  On HPUX, this command eventually resorts to collect2, which may
93       --  generate a C file and compile it on the fly. This compilation shall
94       --  also generate position independant code for the final link to
95       --  succeed.
96    begin
97       if Opt.Verbose_Mode then
98          Write_Str ("building relocatable shared library ");
99          Write_Line (Lib_File);
100       end if;
101
102       if Lib_Version = "" then
103          MLib.Utl.Gcc
104            (Output_File => Lib_File,
105             Objects     => Ofiles,
106             Options     => Common_Options,
107             Options_2   => Options_2,
108             Driver_Name => Driver_Name);
109
110       else
111          Version_Arg := new String'("-Wl,+h," & Lib_Version);
112
113          if Is_Absolute_Path (Lib_Version) then
114             MLib.Utl.Gcc
115               (Output_File => Lib_Version,
116                Objects     => Ofiles,
117                Options     => Common_Options & Version_Arg,
118                Options_2   => Options_2,
119                Driver_Name => Driver_Name);
120             Symbolic_Link_Needed := Lib_Version /= Lib_File;
121
122          else
123             MLib.Utl.Gcc
124               (Output_File => Lib_Dir & Directory_Separator & Lib_Version,
125                Objects     => Ofiles,
126                Options     => Common_Options & Version_Arg,
127                Options_2   => Options_2,
128                Driver_Name => Driver_Name);
129             Symbolic_Link_Needed :=
130               Lib_Dir & Directory_Separator & Lib_Version /= Lib_File;
131          end if;
132
133          if Symbolic_Link_Needed then
134             declare
135                Success : Boolean;
136                Oldpath : String (1 .. Lib_Version'Length + 1);
137                Newpath : String (1 .. Lib_File'Length + 1);
138
139                Result : Integer;
140                pragma Unreferenced (Result);
141
142                function Symlink
143                  (Oldpath : System.Address;
144                   Newpath : System.Address) return Integer;
145                pragma Import (C, Symlink, "__gnat_symlink");
146
147             begin
148                Oldpath (1 .. Lib_Version'Length) := Lib_Version;
149                Oldpath (Oldpath'Last)            := ASCII.NUL;
150                Newpath (1 .. Lib_File'Length)    := Lib_File;
151                Newpath (Newpath'Last)            := ASCII.NUL;
152
153                Delete_File (Lib_File, Success);
154
155                Result := Symlink (Oldpath'Address, Newpath'Address);
156             end;
157          end if;
158       end if;
159    end Build_Dynamic_Library;
160
161    -------------
162    -- DLL_Ext --
163    -------------
164
165    function DLL_Ext return String is
166    begin
167       return "sl";
168    end DLL_Ext;
169
170    --------------------
171    -- Is_Archive_Ext --
172    --------------------
173
174    function Is_Archive_Ext (Ext : String) return Boolean is
175    begin
176       return Ext = ".a" or else Ext = ".so";
177    end Is_Archive_Ext;
178
179 begin
180    Build_Dynamic_Library_Ptr := Build_Dynamic_Library'Access;
181    DLL_Ext_Ptr := DLL_Ext'Access;
182    Is_Archive_Ext_Ptr := Is_Archive_Ext'Access;
183 end MLib.Tgt.Specific;