1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
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. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
26 ------------------------------------------------------------------------------
28 -- This package provides the core high level routines used by GNATDLL
29 -- to build Windows DLL
41 ---------------------------
42 -- Build_Dynamic_Library --
43 ---------------------------
45 procedure Build_Dynamic_Library
46 (Ofiles
: Argument_List
;
47 Afiles
: Argument_List
;
48 Options
: Argument_List
;
49 Bargs_Options
: Argument_List
;
50 Largs_Options
: Argument_List
;
51 Lib_Filename
: String;
52 Def_Filename
: String;
53 Lib_Address
: String := "";
54 Build_Import
: Boolean := False;
55 Relocatable
: Boolean := False)
58 use type OS_Lib
.Argument_List
;
60 Base_Filename
: constant String := MDLL
.Fil
.Ext_To
(Lib_Filename
);
62 Def_File
: aliased String := Def_Filename
;
63 Jnk_File
: aliased String := Base_Filename
& ".jnk";
64 Bas_File
: aliased String := Base_Filename
& ".base";
65 Dll_File
: aliased String := Base_Filename
& ".dll";
66 Exp_File
: aliased String := Base_Filename
& ".exp";
67 Lib_File
: aliased String := "lib" & Base_Filename
& ".a";
69 Bas_Opt
: aliased String := "-Wl,--base-file," & Bas_File
;
70 Lib_Opt
: aliased String := "-mdll";
71 Out_Opt
: aliased String := "-o";
72 Adr_Opt
: aliased String := "-Wl,--image-base=" & Lib_Address
;
74 All_Options
: constant Argument_List
:= Options
& Largs_Options
;
76 procedure Build_Reloc_DLL
;
77 -- Build a relocatable DLL with only objects file specified.
78 -- this use the well known 5 steps build. (see GNAT User's Guide).
80 procedure Ada_Build_Reloc_DLL
;
81 -- Build a relocatable DLL with Ada code.
82 -- this use the well known 5 steps build. (see GNAT User's Guide).
84 procedure Build_Non_Reloc_DLL
;
85 -- Build a non relocatable DLL containing no Ada code.
87 procedure Ada_Build_Non_Reloc_DLL
;
88 -- Build a non relocatable DLL with Ada code.
94 procedure Build_Reloc_DLL
is
95 -- Objects plus the export table (.exp) file
97 Objects_Exp_File
: constant OS_Lib
.Argument_List
98 := Exp_File
'Unchecked_Access & Ofiles
;
104 Text_IO
.Put_Line
("building relocatable DLL...");
105 Text_IO
.Put
("make " & Dll_File
);
108 Text_IO
.Put_Line
(" and " & Lib_File
);
114 -- 1) Build base file with objects files.
116 Utl
.Gcc
(Output_File
=> Jnk_File
,
118 Options
=> All_Options
,
119 Base_File
=> Bas_File
,
122 -- 2) Build exp from base file.
124 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
125 Base_File
=> Bas_File
,
126 Exp_Table
=> Exp_File
,
127 Build_Import
=> False);
129 -- 3) Build base file with exp file and objects files.
131 Utl
.Gcc
(Output_File
=> Jnk_File
,
132 Files
=> Objects_Exp_File
,
133 Options
=> All_Options
,
134 Base_File
=> Bas_File
,
137 -- 4) Build new exp from base file and the lib file (.a)
139 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
140 Base_File
=> Bas_File
,
141 Exp_Table
=> Exp_File
,
142 Build_Import
=> Build_Import
);
144 -- 5) Build the dynamic library
146 Utl
.Gcc
(Output_File
=> Dll_File
,
147 Files
=> Objects_Exp_File
,
148 Options
=> Adr_Opt
'Unchecked_Access & All_Options
,
151 OS_Lib
.Delete_File
(Exp_File
, Success
);
152 OS_Lib
.Delete_File
(Bas_File
, Success
);
153 OS_Lib
.Delete_File
(Jnk_File
, Success
);
157 OS_Lib
.Delete_File
(Exp_File
, Success
);
158 OS_Lib
.Delete_File
(Bas_File
, Success
);
159 OS_Lib
.Delete_File
(Jnk_File
, Success
);
163 -------------------------
164 -- Ada_Build_Reloc_DLL --
165 -------------------------
167 procedure Ada_Build_Reloc_DLL
is
171 Text_IO
.Put_Line
("Building relocatable DLL...");
172 Text_IO
.Put
("make " & Dll_File
);
175 Text_IO
.Put_Line
(" and " & Lib_File
);
181 -- 1) Build base file with objects files.
183 Utl
.Gnatbind
(Afiles
, Options
& Bargs_Options
);
186 Params
: OS_Lib
.Argument_List
:=
187 Out_Opt
'Unchecked_Access & Jnk_File
'Unchecked_Access &
188 Lib_Opt
'Unchecked_Access &
189 Bas_Opt
'Unchecked_Access & Ofiles
& All_Options
;
191 Utl
.Gnatlink
(Afiles
(Afiles
'Last).all, Params
);
194 -- 2) Build exp from base file.
196 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
197 Base_File
=> Bas_File
,
198 Exp_Table
=> Exp_File
,
199 Build_Import
=> False);
201 -- 3) Build base file with exp file and objects files.
203 Utl
.Gnatbind
(Afiles
, Options
& Bargs_Options
);
206 Params
: OS_Lib
.Argument_List
:=
207 Out_Opt
'Unchecked_Access & Jnk_File
'Unchecked_Access &
208 Lib_Opt
'Unchecked_Access &
209 Bas_Opt
'Unchecked_Access &
210 Exp_File
'Unchecked_Access &
214 Utl
.Gnatlink
(Afiles
(Afiles
'Last).all, Params
);
217 -- 4) Build new exp from base file and the lib file (.a)
219 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
220 Base_File
=> Bas_File
,
221 Exp_Table
=> Exp_File
,
222 Build_Import
=> Build_Import
);
224 -- 5) Build the dynamic library
226 Utl
.Gnatbind
(Afiles
, Options
& Bargs_Options
);
229 Params
: OS_Lib
.Argument_List
:=
230 Out_Opt
'Unchecked_Access & Dll_File
'Unchecked_Access &
231 Lib_Opt
'Unchecked_Access &
232 Exp_File
'Unchecked_Access &
233 Adr_Opt
'Unchecked_Access &
237 Utl
.Gnatlink
(Afiles
(Afiles
'Last).all, Params
);
240 OS_Lib
.Delete_File
(Exp_File
, Success
);
241 OS_Lib
.Delete_File
(Bas_File
, Success
);
242 OS_Lib
.Delete_File
(Jnk_File
, Success
);
246 OS_Lib
.Delete_File
(Exp_File
, Success
);
247 OS_Lib
.Delete_File
(Bas_File
, Success
);
248 OS_Lib
.Delete_File
(Jnk_File
, Success
);
250 end Ada_Build_Reloc_DLL
;
252 -------------------------
253 -- Build_Non_Reloc_DLL --
254 -------------------------
256 procedure Build_Non_Reloc_DLL
is
260 Text_IO
.Put_Line
("building non relocatable DLL...");
261 Text_IO
.Put
("make " & Dll_File
&
262 " using address " & Lib_Address
);
265 Text_IO
.Put_Line
(" and " & Lib_File
);
271 -- Build exp table and the lib .a file.
273 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
274 Exp_Table
=> Exp_File
,
275 Build_Import
=> Build_Import
);
279 Utl
.Gcc
(Output_File
=> Dll_File
,
280 Files
=> Exp_File
'Unchecked_Access & Ofiles
,
281 Options
=> Adr_Opt
'Unchecked_Access & All_Options
,
284 OS_Lib
.Delete_File
(Exp_File
, Success
);
288 OS_Lib
.Delete_File
(Exp_File
, Success
);
290 end Build_Non_Reloc_DLL
;
292 -----------------------------
293 -- Ada_Build_Non_Reloc_DLL --
294 -----------------------------
296 -- Build a non relocatable DLL with Ada code.
298 procedure Ada_Build_Non_Reloc_DLL
is
302 Text_IO
.Put_Line
("building non relocatable DLL...");
303 Text_IO
.Put
("make " & Dll_File
&
304 " using address " & Lib_Address
);
307 Text_IO
.Put_Line
(" and " & Lib_File
);
313 -- Build exp table and the lib .a file.
315 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
316 Exp_Table
=> Exp_File
,
317 Build_Import
=> Build_Import
);
321 Utl
.Gnatbind
(Afiles
, Options
& Bargs_Options
);
324 Params
: OS_Lib
.Argument_List
:=
325 Out_Opt
'Unchecked_Access & Dll_File
'Unchecked_Access &
326 Lib_Opt
'Unchecked_Access &
327 Exp_File
'Unchecked_Access &
328 Adr_Opt
'Unchecked_Access &
332 Utl
.Gnatlink
(Afiles
(Afiles
'Last).all, Params
);
335 OS_Lib
.Delete_File
(Exp_File
, Success
);
339 OS_Lib
.Delete_File
(Exp_File
, Success
);
341 end Ada_Build_Non_Reloc_DLL
;
347 if Afiles
'Length = 0 then
354 if Afiles
'Length = 0 then
357 Ada_Build_Non_Reloc_DLL
;
361 end Build_Dynamic_Library
;
363 --------------------------
364 -- Build_Import_Library --
365 --------------------------
367 procedure Build_Import_Library
368 (Lib_Filename
: String;
369 Def_Filename
: String)
372 procedure Build_Import_Library
(Def_Base_Filename
: String);
373 -- Build an import library.
374 -- this is to build only a .a library to link against a DLL.
376 Base_Filename
: constant String := MDLL
.Fil
.Ext_To
(Lib_Filename
);
378 --------------------------
379 -- Build_Import_Library --
380 --------------------------
382 procedure Build_Import_Library
(Def_Base_Filename
: String) is
384 Def_File
: String renames Def_Filename
;
385 Dll_File
: constant String := Def_Base_Filename
& ".dll";
386 Lib_File
: constant String := "lib" & Base_Filename
& ".a";
391 Text_IO
.Put_Line
("Building import library...");
392 Text_IO
.Put_Line
("make " & Lib_File
&
393 " to use dynamic library " & Dll_File
);
396 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
397 Build_Import
=> True);
398 end Build_Import_Library
;
401 -- If the library has the form lib<name>.a then the def file should
402 -- be <name>.def and the DLL to link against <name>.dll
403 -- this is a Windows convention and we try as much as possible to
404 -- follow the platform convention.
406 if Lib_Filename
'Length > 3 and then Lib_Filename
(1 .. 3) = "lib" then
407 Build_Import_Library
(Base_Filename
(4 .. Base_Filename
'Last));
409 Build_Import_Library
(Base_Filename
);
411 end Build_Import_Library
;