1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ---------------------------
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;
56 Map_File : Boolean := False)
59 use type OS_Lib.Argument_List;
61 Base_Filename : constant String := MDLL.Fil.Ext_To (Lib_Filename);
63 Def_File : aliased constant String := Def_Filename;
64 Jnk_File : aliased String := Base_Filename & ".jnk";
65 Bas_File : aliased constant String := Base_Filename & ".base";
66 Dll_File : aliased String := Base_Filename & ".dll";
67 Exp_File : aliased String := Base_Filename & ".exp";
68 Lib_File : aliased constant 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";
73 Adr_Opt : aliased String := "-Wl,--image-base=" & Lib_Address;
74 Map_Opt : aliased String := "-Wl,-Map," & Lib_Filename & ".map";
76 L_Afiles : Argument_List := Afiles;
77 -- Local afiles list. This list can be reordered to ensure that the
78 -- binder ali file is not the first entry in this list.
80 All_Options : constant Argument_List := Options & Largs_Options;
82 procedure Build_Reloc_DLL;
83 -- Build a relocatable DLL with only objects file specified.
84 -- this use the well known 5 steps build. (see GNAT User's Guide).
86 procedure Ada_Build_Reloc_DLL;
87 -- Build a relocatable DLL with Ada code.
88 -- this use the well known 5 steps build. (see GNAT User's Guide).
90 procedure Build_Non_Reloc_DLL;
91 -- Build a non relocatable DLL containing no Ada code.
93 procedure Ada_Build_Non_Reloc_DLL;
94 -- Build a non relocatable DLL with Ada code.
100 procedure Build_Reloc_DLL is
101 -- Objects plus the export table (.exp) file
102 Objects_Exp_File : constant OS_Lib.Argument_List
103 := Exp_File'Unchecked_Access & Ofiles;
108 Text_IO.Put_Line ("building relocatable DLL...");
109 Text_IO.Put ("make " & Dll_File);
112 Text_IO.Put_Line (" and " & Lib_File);
118 -- 1) Build base file with objects files.
120 Utl.Gcc (Output_File => Jnk_File,
122 Options => All_Options,
123 Base_File => Bas_File,
126 -- 2) Build exp from base file.
128 Utl.Dlltool (Def_File, Dll_File, Lib_File,
129 Base_File => Bas_File,
130 Exp_Table => Exp_File,
131 Build_Import => False);
133 -- 3) Build base file with exp file and objects files.
135 Utl.Gcc (Output_File => Jnk_File,
136 Files => Objects_Exp_File,
137 Options => All_Options,
138 Base_File => Bas_File,
141 -- 4) Build new exp from base file and the lib file (.a)
143 Utl.Dlltool (Def_File, Dll_File, Lib_File,
144 Base_File => Bas_File,
145 Exp_Table => Exp_File,
146 Build_Import => Build_Import);
148 -- 5) Build the dynamic library
151 Params : OS_Lib.Argument_List :=
152 Adr_Opt'Unchecked_Access & All_Options;
155 Params := Map_Opt'Unchecked_Access & Params;
159 (Output_File => Dll_File,
160 Files => Objects_Exp_File,
165 OS_Lib.Delete_File (Exp_File, Success);
166 OS_Lib.Delete_File (Bas_File, Success);
167 OS_Lib.Delete_File (Jnk_File, Success);
171 OS_Lib.Delete_File (Exp_File, Success);
172 OS_Lib.Delete_File (Bas_File, Success);
173 OS_Lib.Delete_File (Jnk_File, Success);
177 -------------------------
178 -- Ada_Build_Reloc_DLL --
179 -------------------------
181 procedure Ada_Build_Reloc_DLL is
185 Text_IO.Put_Line ("Building relocatable DLL...");
186 Text_IO.Put ("make " & Dll_File);
189 Text_IO.Put_Line (" and " & Lib_File);
195 -- 1) Build base file with objects files.
197 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
200 Params : constant OS_Lib.Argument_List :=
201 Out_Opt'Unchecked_Access &
202 Jnk_File'Unchecked_Access &
203 Lib_Opt'Unchecked_Access &
204 Bas_Opt'Unchecked_Access &
208 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
211 -- 2) Build exp from base file.
213 Utl.Dlltool (Def_File, Dll_File, Lib_File,
214 Base_File => Bas_File,
215 Exp_Table => Exp_File,
216 Build_Import => False);
218 -- 3) Build base file with exp file and objects files.
220 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
223 Params : constant OS_Lib.Argument_List :=
224 Out_Opt'Unchecked_Access &
225 Jnk_File'Unchecked_Access &
226 Lib_Opt'Unchecked_Access &
227 Bas_Opt'Unchecked_Access &
228 Exp_File'Unchecked_Access &
232 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
235 -- 4) Build new exp from base file and the lib file (.a)
237 Utl.Dlltool (Def_File, Dll_File, Lib_File,
238 Base_File => Bas_File,
239 Exp_Table => Exp_File,
240 Build_Import => Build_Import);
242 -- 5) Build the dynamic library
244 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
247 Params : OS_Lib.Argument_List :=
248 Out_Opt'Unchecked_Access &
249 Dll_File'Unchecked_Access &
250 Lib_Opt'Unchecked_Access &
251 Exp_File'Unchecked_Access &
252 Adr_Opt'Unchecked_Access &
257 Params := Map_Opt'Unchecked_Access & Params;
260 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
263 OS_Lib.Delete_File (Exp_File, Success);
264 OS_Lib.Delete_File (Bas_File, Success);
265 OS_Lib.Delete_File (Jnk_File, Success);
269 OS_Lib.Delete_File (Exp_File, Success);
270 OS_Lib.Delete_File (Bas_File, Success);
271 OS_Lib.Delete_File (Jnk_File, Success);
273 end Ada_Build_Reloc_DLL;
275 -------------------------
276 -- Build_Non_Reloc_DLL --
277 -------------------------
279 procedure Build_Non_Reloc_DLL is
283 Text_IO.Put_Line ("building non relocatable DLL...");
284 Text_IO.Put ("make " & Dll_File &
285 " using address " & Lib_Address);
288 Text_IO.Put_Line (" and " & Lib_File);
294 -- Build exp table and the lib .a file.
296 Utl.Dlltool (Def_File, Dll_File, Lib_File,
297 Exp_Table => Exp_File,
298 Build_Import => Build_Import);
303 Params : OS_Lib.Argument_List :=
304 Adr_Opt'Unchecked_Access & All_Options;
307 Params := Map_Opt'Unchecked_Access & Params;
310 Utl.Gcc (Output_File => Dll_File,
311 Files => Exp_File'Unchecked_Access & Ofiles,
316 OS_Lib.Delete_File (Exp_File, Success);
320 OS_Lib.Delete_File (Exp_File, Success);
322 end Build_Non_Reloc_DLL;
324 -----------------------------
325 -- Ada_Build_Non_Reloc_DLL --
326 -----------------------------
328 -- Build a non relocatable DLL with Ada code.
330 procedure Ada_Build_Non_Reloc_DLL is
334 Text_IO.Put_Line ("building non relocatable DLL...");
335 Text_IO.Put ("make " & Dll_File &
336 " using address " & Lib_Address);
339 Text_IO.Put_Line (" and " & Lib_File);
345 -- Build exp table and the lib .a file.
347 Utl.Dlltool (Def_File, Dll_File, Lib_File,
348 Exp_Table => Exp_File,
349 Build_Import => Build_Import);
353 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
356 Params : OS_Lib.Argument_List :=
357 Out_Opt'Unchecked_Access &
358 Dll_File'Unchecked_Access &
359 Lib_Opt'Unchecked_Access &
360 Exp_File'Unchecked_Access &
361 Adr_Opt'Unchecked_Access &
366 Params := Map_Opt'Unchecked_Access & Params;
369 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
372 OS_Lib.Delete_File (Exp_File, Success);
376 OS_Lib.Delete_File (Exp_File, Success);
378 end Ada_Build_Non_Reloc_DLL;
381 -- On Windows the binder file must not be in the first position
382 -- in the list. This is due to the way DLL's are built on Windows.
383 -- We swap the first ali with the last one if it is the case.
385 if L_Afiles'Length > 1 then
387 Filename : constant String :=
388 Directory_Operations.Base_Name (L_Afiles (1).all);
389 First : constant Positive := Filename'First;
392 if Filename (First .. First + 1) = "b~" then
393 L_Afiles (L_Afiles'Last) := Afiles (1);
394 L_Afiles (1) := Afiles (Afiles'Last);
401 if L_Afiles'Length = 0 then
408 if L_Afiles'Length = 0 then
411 Ada_Build_Non_Reloc_DLL;
414 end Build_Dynamic_Library;
416 --------------------------
417 -- Build_Import_Library --
418 --------------------------
420 procedure Build_Import_Library
421 (Lib_Filename : String;
422 Def_Filename : String)
425 procedure Build_Import_Library (Def_Base_Filename : String);
426 -- Build an import library.
427 -- this is to build only a .a library to link against a DLL.
429 Base_Filename : constant String := MDLL.Fil.Ext_To (Lib_Filename);
431 --------------------------
432 -- Build_Import_Library --
433 --------------------------
435 procedure Build_Import_Library (Def_Base_Filename : String) is
436 Def_File : String renames Def_Filename;
437 Dll_File : constant String := Def_Base_Filename & ".dll";
438 Lib_File : constant String := "lib" & Base_Filename & ".a";
442 Text_IO.Put_Line ("Building import library...");
443 Text_IO.Put_Line ("make " & Lib_File &
444 " to use dynamic library " & Dll_File);
447 Utl.Dlltool (Def_File, Dll_File, Lib_File,
448 Build_Import => True);
449 end Build_Import_Library;
452 -- If the library has the form lib<name>.a then the def file should
453 -- be <name>.def and the DLL to link against <name>.dll
454 -- this is a Windows convention and we try as much as possible to
455 -- follow the platform convention.
457 if Lib_Filename'Length > 3 and then Lib_Filename (1 .. 3) = "lib" then
458 Build_Import_Library (Base_Filename (4 .. Base_Filename'Last));
460 Build_Import_Library (Base_Filename);
462 end Build_Import_Library;