1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This package provides the core high level routines used by GNATDLL
27 -- to build Windows DLL.
31 with GNAT.Directory_Operations;
40 -- Convention used for the library names on Windows:
42 -- Import library: lib<name>.dll
44 function Get_Dll_Name (Lib_Filename : String) return String;
45 -- Returns <Lib_Filename> if it contains a file extension otherwise it
46 -- returns <Lib_Filename>.dll.
48 ---------------------------
49 -- Build_Dynamic_Library --
50 ---------------------------
52 procedure Build_Dynamic_Library
53 (Ofiles : Argument_List;
54 Afiles : Argument_List;
55 Options : Argument_List;
56 Bargs_Options : Argument_List;
57 Largs_Options : Argument_List;
58 Lib_Filename : String;
59 Def_Filename : String;
60 Lib_Address : String := "";
61 Build_Import : Boolean := False;
62 Relocatable : Boolean := False;
63 Map_File : Boolean := False)
66 use type OS_Lib.Argument_List;
68 Base_Filename : constant String := MDLL.Fil.Ext_To (Lib_Filename);
70 Def_File : aliased constant String := Def_Filename;
71 Jnk_File : aliased String := Base_Filename & ".jnk";
72 Bas_File : aliased constant String := Base_Filename & ".base";
73 Dll_File : aliased String := Get_Dll_Name (Lib_Filename);
74 Exp_File : aliased String := Base_Filename & ".exp";
75 Lib_File : aliased constant String := "lib" & Base_Filename & ".dll.a";
77 Bas_Opt : aliased String := "-Wl,--base-file," & Bas_File;
78 Lib_Opt : aliased String := "-mdll";
79 Out_Opt : aliased String := "-o";
80 Adr_Opt : aliased String := "-Wl,--image-base=" & Lib_Address;
81 Map_Opt : aliased String := "-Wl,-Map," & Lib_Filename & ".map";
83 L_Afiles : Argument_List := Afiles;
84 -- Local afiles list. This list can be reordered to ensure that the
85 -- binder ALI file is not the first entry in this list.
87 All_Options : constant Argument_List := Options & Largs_Options;
89 procedure Build_Reloc_DLL;
90 -- Build a relocatable DLL with only objects file specified. This uses
91 -- the well known five step build (see GNAT User's Guide).
93 procedure Ada_Build_Reloc_DLL;
94 -- Build a relocatable DLL with Ada code. This uses the well known five
95 -- step build (see GNAT User's Guide).
97 procedure Build_Non_Reloc_DLL;
98 -- Build a non relocatable DLL containing no Ada code
100 procedure Ada_Build_Non_Reloc_DLL;
101 -- Build a non relocatable DLL with Ada code
103 ---------------------
104 -- Build_Reloc_DLL --
105 ---------------------
107 procedure Build_Reloc_DLL is
109 Objects_Exp_File : constant OS_Lib.Argument_List :=
110 Exp_File'Unchecked_Access & Ofiles;
111 -- Objects plus the export table (.exp) file
114 pragma Warnings (Off, Success);
118 Text_IO.Put_Line ("building relocatable DLL...");
119 Text_IO.Put ("make " & Dll_File);
122 Text_IO.Put_Line (" and " & Lib_File);
128 -- 1) Build base file with objects files
130 Utl.Gcc (Output_File => Jnk_File,
132 Options => All_Options,
133 Base_File => Bas_File,
136 -- 2) Build exp from base file
138 Utl.Dlltool (Def_File, Dll_File, Lib_File,
139 Base_File => Bas_File,
140 Exp_Table => Exp_File,
141 Build_Import => False);
143 -- 3) Build base file with exp file and objects files
145 Utl.Gcc (Output_File => Jnk_File,
146 Files => Objects_Exp_File,
147 Options => All_Options,
148 Base_File => Bas_File,
151 -- 4) Build new exp from base file and the lib file (.a)
153 Utl.Dlltool (Def_File, Dll_File, Lib_File,
154 Base_File => Bas_File,
155 Exp_Table => Exp_File,
156 Build_Import => Build_Import);
158 -- 5) Build the dynamic library
161 Params : constant OS_Lib.Argument_List :=
162 Map_Opt'Unchecked_Access &
163 Adr_Opt'Unchecked_Access & All_Options;
164 First_Param : Positive := Params'First + 1;
168 First_Param := Params'First;
172 (Output_File => Dll_File,
173 Files => Objects_Exp_File,
174 Options => Params (First_Param .. Params'Last),
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 OS_Lib.Delete_File (Exp_File, Success);
185 OS_Lib.Delete_File (Bas_File, Success);
186 OS_Lib.Delete_File (Jnk_File, Success);
190 -------------------------
191 -- Ada_Build_Reloc_DLL --
192 -------------------------
194 procedure Ada_Build_Reloc_DLL is
196 pragma Warnings (Off, Success);
200 Text_IO.Put_Line ("Building relocatable DLL...");
201 Text_IO.Put ("make " & Dll_File);
204 Text_IO.Put_Line (" and " & Lib_File);
210 -- 1) Build base file with objects files
212 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
215 Params : constant OS_Lib.Argument_List :=
216 Out_Opt'Unchecked_Access &
217 Jnk_File'Unchecked_Access &
218 Lib_Opt'Unchecked_Access &
219 Bas_Opt'Unchecked_Access &
223 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
226 -- 2) Build exp from base file
228 Utl.Dlltool (Def_File, Dll_File, Lib_File,
229 Base_File => Bas_File,
230 Exp_Table => Exp_File,
231 Build_Import => False);
233 -- 3) Build base file with exp file and objects files
235 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
238 Params : constant OS_Lib.Argument_List :=
239 Out_Opt'Unchecked_Access &
240 Jnk_File'Unchecked_Access &
241 Lib_Opt'Unchecked_Access &
242 Bas_Opt'Unchecked_Access &
243 Exp_File'Unchecked_Access &
247 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
250 -- 4) Build new exp from base file and the lib file (.a)
252 Utl.Dlltool (Def_File, Dll_File, Lib_File,
253 Base_File => Bas_File,
254 Exp_Table => Exp_File,
255 Build_Import => Build_Import);
257 -- 5) Build the dynamic library
259 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
262 Params : constant OS_Lib.Argument_List :=
263 Map_Opt'Unchecked_Access &
264 Out_Opt'Unchecked_Access &
265 Dll_File'Unchecked_Access &
266 Lib_Opt'Unchecked_Access &
267 Exp_File'Unchecked_Access &
268 Adr_Opt'Unchecked_Access &
271 First_Param : Positive := Params'First + 1;
275 First_Param := Params'First;
279 (L_Afiles (L_Afiles'Last).all,
280 Params (First_Param .. Params'Last));
283 OS_Lib.Delete_File (Exp_File, Success);
284 OS_Lib.Delete_File (Bas_File, Success);
285 OS_Lib.Delete_File (Jnk_File, Success);
289 OS_Lib.Delete_File (Exp_File, Success);
290 OS_Lib.Delete_File (Bas_File, Success);
291 OS_Lib.Delete_File (Jnk_File, Success);
293 end Ada_Build_Reloc_DLL;
295 -------------------------
296 -- Build_Non_Reloc_DLL --
297 -------------------------
299 procedure Build_Non_Reloc_DLL is
301 pragma Warnings (Off, Success);
305 Text_IO.Put_Line ("building non relocatable DLL...");
306 Text_IO.Put ("make " & Dll_File &
307 " using address " & Lib_Address);
310 Text_IO.Put_Line (" and " & Lib_File);
316 -- Build exp table and the lib .a file
318 Utl.Dlltool (Def_File, Dll_File, Lib_File,
319 Exp_Table => Exp_File,
320 Build_Import => Build_Import);
325 Params : OS_Lib.Argument_List :=
326 Adr_Opt'Unchecked_Access & All_Options;
329 Params := Map_Opt'Unchecked_Access & Params;
332 Utl.Gcc (Output_File => Dll_File,
333 Files => Exp_File'Unchecked_Access & Ofiles,
338 OS_Lib.Delete_File (Exp_File, Success);
342 OS_Lib.Delete_File (Exp_File, Success);
344 end Build_Non_Reloc_DLL;
346 -----------------------------
347 -- Ada_Build_Non_Reloc_DLL --
348 -----------------------------
350 -- Build a non relocatable DLL with Ada code
352 procedure Ada_Build_Non_Reloc_DLL is
354 pragma Warnings (Off, Success);
358 Text_IO.Put_Line ("building non relocatable DLL...");
359 Text_IO.Put ("make " & Dll_File &
360 " using address " & Lib_Address);
363 Text_IO.Put_Line (" and " & Lib_File);
369 -- Build exp table and the lib .a file
371 Utl.Dlltool (Def_File, Dll_File, Lib_File,
372 Exp_Table => Exp_File,
373 Build_Import => Build_Import);
377 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
380 Params : OS_Lib.Argument_List :=
381 Out_Opt'Unchecked_Access &
382 Dll_File'Unchecked_Access &
383 Lib_Opt'Unchecked_Access &
384 Exp_File'Unchecked_Access &
385 Adr_Opt'Unchecked_Access &
390 Params := Map_Opt'Unchecked_Access & Params;
393 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
396 OS_Lib.Delete_File (Exp_File, Success);
400 OS_Lib.Delete_File (Exp_File, Success);
402 end Ada_Build_Non_Reloc_DLL;
404 -- Start of processing for Build_Dynamic_Library
407 -- On Windows the binder file must not be in the first position in the
408 -- list. This is due to the way DLL's are built on Windows. We swap the
409 -- first ali with the last one if it is the case.
411 if L_Afiles'Length > 1 then
413 Filename : constant String :=
414 Directory_Operations.Base_Name
415 (L_Afiles (L_Afiles'First).all);
416 First : constant Positive := Filename'First;
419 if Filename (First .. First + 1) = "b~" then
420 L_Afiles (L_Afiles'Last) := Afiles (Afiles'First);
421 L_Afiles (L_Afiles'First) := Afiles (Afiles'Last);
428 if L_Afiles'Length = 0 then
435 if L_Afiles'Length = 0 then
438 Ada_Build_Non_Reloc_DLL;
441 end Build_Dynamic_Library;
443 --------------------------
444 -- Build_Import_Library --
445 --------------------------
447 procedure Build_Import_Library
448 (Lib_Filename : String;
449 Def_Filename : String)
451 procedure Build_Import_Library (Lib_Filename : String);
452 -- Build an import library. This is to build only a .a library to link
455 --------------------------
456 -- Build_Import_Library --
457 --------------------------
459 procedure Build_Import_Library (Lib_Filename : String) is
461 function No_Lib_Prefix (Filename : String) return String;
462 -- Return Filename without the lib prefix if present
468 function No_Lib_Prefix (Filename : String) return String is
470 if Filename (Filename'First .. Filename'First + 2) = "lib" then
471 return Filename (Filename'First + 3 .. Filename'Last);
479 Def_File : String renames Def_Filename;
480 Dll_File : constant String := Get_Dll_Name (Lib_Filename);
481 Base_Filename : constant String :=
482 MDLL.Fil.Ext_To (No_Lib_Prefix (Lib_Filename));
483 Lib_File : constant String := "lib" & Base_Filename & ".dll.a";
485 -- Start of processing for Build_Import_Library
489 Text_IO.Put_Line ("Building import library...");
491 ("make " & Lib_File & " to use dynamic library " & Dll_File);
495 (Def_File, Dll_File, Lib_File, Build_Import => True);
496 end Build_Import_Library;
498 -- Start of processing for Build_Import_Library
501 Build_Import_Library (Lib_Filename);
502 end Build_Import_Library;
508 function Get_Dll_Name (Lib_Filename : String) return String is
510 if MDLL.Fil.Get_Ext (Lib_Filename) = "" then
511 return Lib_Filename & ".dll";