1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This package provides the core high level routines used by GNATDLL
28 -- to build Windows DLL
32 with GNAT
.Directory_Operations
;
41 function Get_Dll_Name
(Lib_Filename
: String) return String;
42 -- Returns <Lib_Filename> if it contains a file extension otherwise it
43 -- returns <Lib_Filename>.dll.
45 ---------------------------
46 -- Build_Dynamic_Library --
47 ---------------------------
49 procedure Build_Dynamic_Library
50 (Ofiles
: Argument_List
;
51 Afiles
: Argument_List
;
52 Options
: Argument_List
;
53 Bargs_Options
: Argument_List
;
54 Largs_Options
: Argument_List
;
55 Lib_Filename
: String;
56 Def_Filename
: String;
57 Lib_Address
: String := "";
58 Build_Import
: Boolean := False;
59 Relocatable
: Boolean := False;
60 Map_File
: Boolean := False)
63 use type OS_Lib
.Argument_List
;
65 Base_Filename
: constant String := MDLL
.Fil
.Ext_To
(Lib_Filename
);
67 Def_File
: aliased constant String := Def_Filename
;
68 Jnk_File
: aliased String := Base_Filename
& ".jnk";
69 Bas_File
: aliased constant String := Base_Filename
& ".base";
70 Dll_File
: aliased String := Get_Dll_Name
(Lib_Filename
);
71 Exp_File
: aliased String := Base_Filename
& ".exp";
72 Lib_File
: aliased constant String := "lib" & Base_Filename
& ".a";
74 Bas_Opt
: aliased String := "-Wl,--base-file," & Bas_File
;
75 Lib_Opt
: aliased String := "-mdll";
76 Out_Opt
: aliased String := "-o";
77 Adr_Opt
: aliased String := "-Wl,--image-base=" & Lib_Address
;
78 Map_Opt
: aliased String := "-Wl,-Map," & Lib_Filename
& ".map";
80 L_Afiles
: Argument_List
:= Afiles
;
81 -- Local afiles list. This list can be reordered to ensure that the
82 -- binder ALI file is not the first entry in this list.
84 All_Options
: constant Argument_List
:= Options
& Largs_Options
;
86 procedure Build_Reloc_DLL
;
87 -- Build a relocatable DLL with only objects file specified. This uses
88 -- the well known five step build (see GNAT User's Guide).
90 procedure Ada_Build_Reloc_DLL
;
91 -- Build a relocatable DLL with Ada code. This uses the well known five
92 -- step build (see GNAT User's Guide).
94 procedure Build_Non_Reloc_DLL
;
95 -- Build a non relocatable DLL containing no Ada code
97 procedure Ada_Build_Non_Reloc_DLL
;
98 -- Build a non relocatable DLL with Ada code
100 ---------------------
101 -- Build_Reloc_DLL --
102 ---------------------
104 procedure Build_Reloc_DLL
is
106 Objects_Exp_File
: constant OS_Lib
.Argument_List
:=
107 Exp_File
'Unchecked_Access & Ofiles
;
108 -- Objects plus the export table (.exp) file
114 Text_IO
.Put_Line
("building relocatable DLL...");
115 Text_IO
.Put
("make " & Dll_File
);
118 Text_IO
.Put_Line
(" and " & Lib_File
);
124 -- 1) Build base file with objects files
126 Utl
.Gcc
(Output_File
=> Jnk_File
,
128 Options
=> All_Options
,
129 Base_File
=> Bas_File
,
132 -- 2) Build exp from base file
134 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
135 Base_File
=> Bas_File
,
136 Exp_Table
=> Exp_File
,
137 Build_Import
=> False);
139 -- 3) Build base file with exp file and objects files
141 Utl
.Gcc
(Output_File
=> Jnk_File
,
142 Files
=> Objects_Exp_File
,
143 Options
=> All_Options
,
144 Base_File
=> Bas_File
,
147 -- 4) Build new exp from base file and the lib file (.a)
149 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
150 Base_File
=> Bas_File
,
151 Exp_Table
=> Exp_File
,
152 Build_Import
=> Build_Import
);
154 -- 5) Build the dynamic library
157 Params
: OS_Lib
.Argument_List
:=
158 Adr_Opt
'Unchecked_Access & All_Options
;
162 Params
:= Map_Opt
'Unchecked_Access & Params
;
166 (Output_File
=> Dll_File
,
167 Files
=> Objects_Exp_File
,
172 OS_Lib
.Delete_File
(Exp_File
, Success
);
173 OS_Lib
.Delete_File
(Bas_File
, Success
);
174 OS_Lib
.Delete_File
(Jnk_File
, Success
);
178 OS_Lib
.Delete_File
(Exp_File
, Success
);
179 OS_Lib
.Delete_File
(Bas_File
, Success
);
180 OS_Lib
.Delete_File
(Jnk_File
, Success
);
184 -------------------------
185 -- Ada_Build_Reloc_DLL --
186 -------------------------
188 procedure Ada_Build_Reloc_DLL
is
193 Text_IO
.Put_Line
("Building relocatable DLL...");
194 Text_IO
.Put
("make " & Dll_File
);
197 Text_IO
.Put_Line
(" and " & Lib_File
);
203 -- 1) Build base file with objects files
205 Utl
.Gnatbind
(L_Afiles
, Options
& Bargs_Options
);
208 Params
: constant OS_Lib
.Argument_List
:=
209 Out_Opt
'Unchecked_Access &
210 Jnk_File
'Unchecked_Access &
211 Lib_Opt
'Unchecked_Access &
212 Bas_Opt
'Unchecked_Access &
216 Utl
.Gnatlink
(L_Afiles
(L_Afiles
'Last).all, Params
);
219 -- 2) Build exp from base file
221 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
222 Base_File
=> Bas_File
,
223 Exp_Table
=> Exp_File
,
224 Build_Import
=> False);
226 -- 3) Build base file with exp file and objects files
228 Utl
.Gnatbind
(L_Afiles
, Options
& Bargs_Options
);
231 Params
: constant OS_Lib
.Argument_List
:=
232 Out_Opt
'Unchecked_Access &
233 Jnk_File
'Unchecked_Access &
234 Lib_Opt
'Unchecked_Access &
235 Bas_Opt
'Unchecked_Access &
236 Exp_File
'Unchecked_Access &
240 Utl
.Gnatlink
(L_Afiles
(L_Afiles
'Last).all, Params
);
243 -- 4) Build new exp from base file and the lib file (.a)
245 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
246 Base_File
=> Bas_File
,
247 Exp_Table
=> Exp_File
,
248 Build_Import
=> Build_Import
);
250 -- 5) Build the dynamic library
252 Utl
.Gnatbind
(L_Afiles
, Options
& Bargs_Options
);
255 Params
: OS_Lib
.Argument_List
:=
256 Out_Opt
'Unchecked_Access &
257 Dll_File
'Unchecked_Access &
258 Lib_Opt
'Unchecked_Access &
259 Exp_File
'Unchecked_Access &
260 Adr_Opt
'Unchecked_Access &
265 Params
:= Map_Opt
'Unchecked_Access & Params
;
268 Utl
.Gnatlink
(L_Afiles
(L_Afiles
'Last).all, Params
);
271 OS_Lib
.Delete_File
(Exp_File
, Success
);
272 OS_Lib
.Delete_File
(Bas_File
, Success
);
273 OS_Lib
.Delete_File
(Jnk_File
, Success
);
277 OS_Lib
.Delete_File
(Exp_File
, Success
);
278 OS_Lib
.Delete_File
(Bas_File
, Success
);
279 OS_Lib
.Delete_File
(Jnk_File
, Success
);
281 end Ada_Build_Reloc_DLL
;
283 -------------------------
284 -- Build_Non_Reloc_DLL --
285 -------------------------
287 procedure Build_Non_Reloc_DLL
is
292 Text_IO
.Put_Line
("building non relocatable DLL...");
293 Text_IO
.Put
("make " & Dll_File
&
294 " using address " & Lib_Address
);
297 Text_IO
.Put_Line
(" and " & Lib_File
);
303 -- Build exp table and the lib .a file
305 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
306 Exp_Table
=> Exp_File
,
307 Build_Import
=> Build_Import
);
312 Params
: OS_Lib
.Argument_List
:=
313 Adr_Opt
'Unchecked_Access & All_Options
;
316 Params
:= Map_Opt
'Unchecked_Access & Params
;
319 Utl
.Gcc
(Output_File
=> Dll_File
,
320 Files
=> Exp_File
'Unchecked_Access & Ofiles
,
325 OS_Lib
.Delete_File
(Exp_File
, Success
);
329 OS_Lib
.Delete_File
(Exp_File
, Success
);
331 end Build_Non_Reloc_DLL
;
333 -----------------------------
334 -- Ada_Build_Non_Reloc_DLL --
335 -----------------------------
337 -- Build a non relocatable DLL with Ada code
339 procedure Ada_Build_Non_Reloc_DLL
is
344 Text_IO
.Put_Line
("building non relocatable DLL...");
345 Text_IO
.Put
("make " & Dll_File
&
346 " using address " & Lib_Address
);
349 Text_IO
.Put_Line
(" and " & Lib_File
);
355 -- Build exp table and the lib .a file
357 Utl
.Dlltool
(Def_File
, Dll_File
, Lib_File
,
358 Exp_Table
=> Exp_File
,
359 Build_Import
=> Build_Import
);
363 Utl
.Gnatbind
(L_Afiles
, Options
& Bargs_Options
);
366 Params
: OS_Lib
.Argument_List
:=
367 Out_Opt
'Unchecked_Access &
368 Dll_File
'Unchecked_Access &
369 Lib_Opt
'Unchecked_Access &
370 Exp_File
'Unchecked_Access &
371 Adr_Opt
'Unchecked_Access &
376 Params
:= Map_Opt
'Unchecked_Access & Params
;
379 Utl
.Gnatlink
(L_Afiles
(L_Afiles
'Last).all, Params
);
382 OS_Lib
.Delete_File
(Exp_File
, Success
);
386 OS_Lib
.Delete_File
(Exp_File
, Success
);
388 end Ada_Build_Non_Reloc_DLL
;
391 -- On Windows the binder file must not be in the first position in the
392 -- list. This is due to the way DLL's are built on Windows. We swap the
393 -- first ali with the last one if it is the case.
395 if L_Afiles
'Length > 1 then
397 Filename
: constant String :=
398 Directory_Operations
.Base_Name
(L_Afiles
(1).all);
399 First
: constant Positive := Filename
'First;
402 if Filename
(First
.. First
+ 1) = "b~" then
403 L_Afiles
(L_Afiles
'Last) := Afiles
(1);
404 L_Afiles
(1) := Afiles
(Afiles
'Last);
411 if L_Afiles
'Length = 0 then
418 if L_Afiles
'Length = 0 then
421 Ada_Build_Non_Reloc_DLL
;
424 end Build_Dynamic_Library
;
426 --------------------------
427 -- Build_Import_Library --
428 --------------------------
430 procedure Build_Import_Library
431 (Lib_Filename
: String;
432 Def_Filename
: String)
435 procedure Build_Import_Library
(Lib_Filename
: String);
436 -- Build an import library. This is to build only a .a library to link
439 --------------------------
440 -- Build_Import_Library --
441 --------------------------
443 procedure Build_Import_Library
(Lib_Filename
: String) is
444 Def_File
: String renames Def_Filename
;
445 Dll_File
: constant String := Get_Dll_Name
(Lib_Filename
);
446 Base_Filename
: constant String := MDLL
.Fil
.Ext_To
(Lib_Filename
);
447 Lib_File
: constant String := "lib" & Base_Filename
& ".a";
451 Text_IO
.Put_Line
("Building import library...");
453 ("make " & Lib_File
& " to use dynamic library " & Dll_File
);
457 (Def_File
, Dll_File
, Lib_File
, Build_Import
=> True);
458 end Build_Import_Library
;
460 -- Start of processing for Build_Import_Library
463 -- If the library has the form lib<name>.a then the def file should be
464 -- <name>.def and the DLL to link against <name>.dll. This is a Windows
465 -- convention and we try as much as possible to follow the platform
468 if Lib_Filename
'Length > 3 and then Lib_Filename
(1 .. 3) = "lib" then
469 Build_Import_Library
(Lib_Filename
(4 .. Lib_Filename
'Last));
471 Build_Import_Library
(Lib_Filename
);
473 end Build_Import_Library
;
479 function Get_Dll_Name
(Lib_Filename
: in String) return String is
481 if MDLL
.Fil
.Get_Ext
(Lib_Filename
) = "" then
482 return Lib_Filename
& ".dll";