1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- This package provides the core high level routines used by GNATDLL
30 -- to build Windows DLL
42 ---------------------------
43 -- Build_Dynamic_Library --
44 ---------------------------
46 procedure Build_Dynamic_Library
47 (Ofiles : Argument_List;
48 Afiles : Argument_List;
49 Options : Argument_List;
50 Bargs_Options : Argument_List;
51 Largs_Options : Argument_List;
52 Lib_Filename : String;
53 Def_Filename : String;
54 Lib_Address : String := "";
55 Build_Import : Boolean := False;
56 Relocatable : Boolean := False)
59 use type OS_Lib.Argument_List;
61 Base_Filename : constant String := MDLL.Files.Ext_To (Lib_Filename);
63 Def_File : aliased String := Def_Filename;
64 Jnk_File : aliased String := Base_Filename & ".jnk";
65 Bas_File : aliased String := Base_Filename & ".base";
66 Dll_File : aliased String := Base_Filename & ".dll";
67 Exp_File : aliased String := Base_Filename & ".exp";
68 Lib_File : aliased String := "lib" & Base_Filename & ".a";
70 Bas_Opt : aliased String := "-Wl,--base-file," & Bas_File;
71 Lib_Opt : aliased String := "-mdll";
72 Out_Opt : aliased String := "-o";
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
96 -- Objects plus the export table (.exp) file
98 Objects_Exp_File : OS_Lib.Argument_List
99 := Exp_File'Unchecked_Access & Ofiles;
103 Text_IO.Put_Line ("building relocatable DLL...");
104 Text_IO.Put ("make " & Dll_File);
107 Text_IO.Put_Line (" and " & Lib_File);
113 -- 1) Build base file with objects files.
115 Tools.Gcc (Output_File => Jnk_File,
117 Options => All_Options,
118 Base_File => Bas_File,
121 -- 2) Build exp from base file.
123 Tools.Dlltool (Def_File, Dll_File, Lib_File,
124 Base_File => Bas_File,
125 Exp_Table => Exp_File,
126 Build_Import => False);
128 -- 3) Build base file with exp file and objects files.
130 Tools.Gcc (Output_File => Jnk_File,
131 Files => Objects_Exp_File,
132 Options => All_Options,
133 Base_File => Bas_File,
136 -- 4) Build new exp from base file and the lib file (.a)
138 Tools.Dlltool (Def_File, Dll_File, Lib_File,
139 Base_File => Bas_File,
140 Exp_Table => Exp_File,
141 Build_Import => Build_Import);
143 -- 5) Build the dynamic library
145 Tools.Gcc (Output_File => Dll_File,
146 Files => Objects_Exp_File,
147 Options => All_Options,
150 Tools.Delete_File (Exp_File);
151 Tools.Delete_File (Bas_File);
152 Tools.Delete_File (Jnk_File);
156 Tools.Delete_File (Exp_File);
157 Tools.Delete_File (Bas_File);
158 Tools.Delete_File (Jnk_File);
162 -------------------------
163 -- Ada_Build_Reloc_DLL --
164 -------------------------
166 procedure Ada_Build_Reloc_DLL is
169 Text_IO.Put_Line ("Building relocatable DLL...");
170 Text_IO.Put ("make " & Dll_File);
173 Text_IO.Put_Line (" and " & Lib_File);
179 -- 1) Build base file with objects files.
181 Tools.Gnatbind (Afiles, Options & Bargs_Options);
184 Params : OS_Lib.Argument_List :=
185 Out_Opt'Unchecked_Access & Jnk_File'Unchecked_Access &
186 Lib_Opt'Unchecked_Access &
187 Bas_Opt'Unchecked_Access & Ofiles & All_Options;
189 Tools.Gnatlink (Afiles (Afiles'Last).all,
193 -- 2) Build exp from base file.
195 Tools.Dlltool (Def_File, Dll_File, Lib_File,
196 Base_File => Bas_File,
197 Exp_Table => Exp_File,
198 Build_Import => False);
200 -- 3) Build base file with exp file and objects files.
202 Tools.Gnatbind (Afiles, Options & Bargs_Options);
205 Params : OS_Lib.Argument_List :=
206 Out_Opt'Unchecked_Access & Jnk_File'Unchecked_Access &
207 Lib_Opt'Unchecked_Access &
208 Bas_Opt'Unchecked_Access &
209 Exp_File'Unchecked_Access &
213 Tools.Gnatlink (Afiles (Afiles'Last).all,
217 -- 4) Build new exp from base file and the lib file (.a)
219 Tools.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 Tools.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 &
236 Tools.Gnatlink (Afiles (Afiles'Last).all,
240 Tools.Delete_File (Exp_File);
241 Tools.Delete_File (Bas_File);
242 Tools.Delete_File (Jnk_File);
246 Tools.Delete_File (Exp_File);
247 Tools.Delete_File (Bas_File);
248 Tools.Delete_File (Jnk_File);
250 end Ada_Build_Reloc_DLL;
252 -------------------------
253 -- Build_Non_Reloc_DLL --
254 -------------------------
256 procedure Build_Non_Reloc_DLL is
259 Text_IO.Put_Line ("building non relocatable DLL...");
260 Text_IO.Put ("make " & Dll_File &
261 " using address " & Lib_Address);
264 Text_IO.Put_Line (" and " & Lib_File);
270 -- Build exp table and the lib .a file.
272 Tools.Dlltool (Def_File, Dll_File, Lib_File,
273 Exp_Table => Exp_File,
274 Build_Import => Build_Import);
278 Tools.Gcc (Output_File => Dll_File,
279 Files => Exp_File'Unchecked_Access & Ofiles,
280 Options => All_Options,
283 Tools.Delete_File (Exp_File);
287 Tools.Delete_File (Exp_File);
289 end Build_Non_Reloc_DLL;
291 -----------------------------
292 -- Ada_Build_Non_Reloc_DLL --
293 -----------------------------
295 -- Build a non relocatable DLL with Ada code.
297 procedure Ada_Build_Non_Reloc_DLL is
300 Text_IO.Put_Line ("building non relocatable DLL...");
301 Text_IO.Put ("make " & Dll_File &
302 " using address " & Lib_Address);
305 Text_IO.Put_Line (" and " & Lib_File);
311 -- Build exp table and the lib .a file.
313 Tools.Dlltool (Def_File, Dll_File, Lib_File,
314 Exp_Table => Exp_File,
315 Build_Import => Build_Import);
319 Tools.Gnatbind (Afiles, Options & Bargs_Options);
322 Params : OS_Lib.Argument_List :=
323 Out_Opt'Unchecked_Access & Dll_File'Unchecked_Access &
324 Lib_Opt'Unchecked_Access &
325 Exp_File'Unchecked_Access &
329 Tools.Gnatlink (Afiles (Afiles'Last).all,
333 Tools.Delete_File (Exp_File);
337 Tools.Delete_File (Exp_File);
339 end Ada_Build_Non_Reloc_DLL;
345 if Afiles'Length = 0 then
352 if Afiles'Length = 0 then
355 Ada_Build_Non_Reloc_DLL;
359 end Build_Dynamic_Library;
361 --------------------------
362 -- Build_Import_Library --
363 --------------------------
365 procedure Build_Import_Library
366 (Lib_Filename : String;
367 Def_Filename : String)
370 procedure Build_Import_Library (Def_Base_Filename : String);
371 -- Build an import library.
372 -- this is to build only a .a library to link against a DLL.
374 Base_Filename : constant String := MDLL.Files.Ext_To (Lib_Filename);
376 --------------------------
377 -- Build_Import_Library --
378 --------------------------
380 procedure Build_Import_Library (Def_Base_Filename : String) is
382 Def_File : String renames Def_Filename;
383 Dll_File : constant String := Def_Base_Filename & ".dll";
384 Lib_File : constant String := "lib" & Base_Filename & ".a";
389 Text_IO.Put_Line ("Building import library...");
390 Text_IO.Put_Line ("make " & Lib_File &
391 " to use dynamic library " & Dll_File);
394 Tools.Dlltool (Def_File, Dll_File, Lib_File,
395 Build_Import => True);
396 end Build_Import_Library;
399 -- If the library has the form lib<name>.a then the def file should
400 -- be <name>.def and the DLL to link against <name>.dll
401 -- this is a Windows convention and we try as much as possible to
402 -- follow the platform convention.
404 if Lib_Filename'Length > 3 and then Lib_Filename (1 .. 3) = "lib" then
405 Build_Import_Library (Base_Filename (4 .. Base_Filename'Last));
407 Build_Import_Library (Base_Filename);
409 end Build_Import_Library;