Debian Codegraph @

gcc/ada/mdll.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M D L L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 -- This package provides the core high level routines used by GNATDLL
27 -- to build Windows DLL.
28
29 with Ada.Text_IO;
30
31 with GNAT.Directory_Operations;
32 with MDLL.Utl;
33 with MDLL.Fil;
34
35 package body MDLL is
36
37 use Ada;
38 use GNAT;
39
40 -- Convention used for the library names on Windows:
41 -- DLL: <name>.dll
42 -- Import library: lib<name>.dll
43
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.
47
48 ---------------------------
49 -- Build_Dynamic_Library --
50 ---------------------------
51
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)
64 is
65
66 use type OS_Lib.Argument_List;
67
68 Base_Filename : constant String := MDLL.Fil.Ext_To (Lib_Filename);
69
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";
76
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";
82
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.
86
87 All_Options : constant Argument_List := Options & Largs_Options;
88
89 procedure Build_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).
92
93 procedure Ada_Build_DLL;
94 -- Build a relocatable DLL with Ada code. This uses the well known five
95 -- step build (see GNAT User's Guide).
96
97 ---------------
98 -- Build_DLL --
99 ---------------
100
101 procedure Build_DLL is
102
103 Objects_Exp_File : constant OS_Lib.Argument_List :=
104 Exp_File'Unchecked_Access & Ofiles;
105 -- Objects plus the export table (.exp) file
106
107 Success : Boolean;
108 pragma Warnings (Off, Success);
109
110 begin
111 if not Quiet then
112 Text_IO.Put_Line ("building relocatable DLL...");
113 Text_IO.Put ("make " & Dll_File);
114
115 if Build_Import then
116 Text_IO.Put_Line (" and " & Lib_File);
117 else
118 Text_IO.New_Line;
119 end if;
120 end if;
121
122 -- 1) Build base file with objects files
123
124 Utl.Gcc (Output_File => Jnk_File,
125 Files => Ofiles,
126 Options => All_Options,
127 Base_File => Bas_File,
128 Build_Lib => True);
129
130 -- 2) Build exp from base file
131
132 Utl.Dlltool (Def_File, Dll_File, Lib_File,
133 Base_File => (if Relocatable then "" else Bas_File),
134 Exp_Table => Exp_File,
135 Build_Import => False);
136
137 -- 3) Build base file with exp file and objects files
138
139 Utl.Gcc (Output_File => Jnk_File,
140 Files => Objects_Exp_File,
141 Options => All_Options,
142 Base_File => Bas_File,
143 Build_Lib => True);
144
145 -- 4) Build new exp from base file and the lib file (.a)
146
147 Utl.Dlltool (Def_File, Dll_File, Lib_File,
148 Base_File => (if Relocatable then "" else Bas_File),
149 Exp_Table => Exp_File,
150 Build_Import => Build_Import);
151
152 -- 5) Build the dynamic library
153
154 declare
155 Params : constant OS_Lib.Argument_List :=
156 Map_Opt'Unchecked_Access &
157 Adr_Opt'Unchecked_Access & All_Options;
158 First_Param : Positive := Params'First + 1;
159
160 begin
161 if Map_File then
162 First_Param := Params'First;
163 end if;
164
165 Utl.Gcc
166 (Output_File => Dll_File,
167 Files => Objects_Exp_File,
168 Options => Params (First_Param .. Params'Last),
169 Build_Lib => True);
170 end;
171
172 OS_Lib.Delete_File (Exp_File, Success);
173 OS_Lib.Delete_File (Bas_File, Success);
174 OS_Lib.Delete_File (Jnk_File, Success);
175
176 exception
177 when others =>
178 OS_Lib.Delete_File (Exp_File, Success);
179 OS_Lib.Delete_File (Bas_File, Success);
180 OS_Lib.Delete_File (Jnk_File, Success);
181 raise;
182 end Build_DLL;
183
184 -------------------
185 -- Ada_Build_DLL --
186 -------------------
187
188 procedure Ada_Build_DLL is
189 Success : Boolean;
190 pragma Warnings (Off, Success);
191
192 begin
193 if not Quiet then
194 Text_IO.Put_Line ("Building relocatable DLL...");
195 Text_IO.Put ("make " & Dll_File);
196
197 if Build_Import then
198 Text_IO.Put_Line (" and " & Lib_File);
199 else
200 Text_IO.New_Line;
201 end if;
202 end if;
203
204 -- 1) Build base file with objects files
205
206 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
207
208 declare
209 Params : constant OS_Lib.Argument_List :=
210 Out_Opt'Unchecked_Access &
211 Jnk_File'Unchecked_Access &
212 Lib_Opt'Unchecked_Access &
213 Bas_Opt'Unchecked_Access &
214 Ofiles &
215 All_Options;
216 begin
217 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
218 end;
219
220 -- 2) Build exp from base file
221
222 Utl.Dlltool (Def_File, Dll_File, Lib_File,
223 Base_File => (if Relocatable then "" else Bas_File),
224 Exp_Table => Exp_File,
225 Build_Import => False);
226
227 -- 3) Build base file with exp file and objects files
228
229 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
230
231 declare
232 Params : constant OS_Lib.Argument_List :=
233 Out_Opt'Unchecked_Access &
234 Jnk_File'Unchecked_Access &
235 Lib_Opt'Unchecked_Access &
236 Bas_Opt'Unchecked_Access &
237 Exp_File'Unchecked_Access &
238 Ofiles &
239 All_Options;
240 begin
241 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
242 end;
243
244 -- 4) Build new exp from base file and the lib file (.a)
245
246 Utl.Dlltool (Def_File, Dll_File, Lib_File,
247 Base_File => (if Relocatable then "" else Bas_File),
248 Exp_Table => Exp_File,
249 Build_Import => Build_Import);
250
251 -- 5) Build the dynamic library
252
253 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
254
255 declare
256 Params : constant OS_Lib.Argument_List :=
257 Map_Opt'Unchecked_Access &
258 Out_Opt'Unchecked_Access &
259 Dll_File'Unchecked_Access &
260 Lib_Opt'Unchecked_Access &
261 Exp_File'Unchecked_Access &
262 Adr_Opt'Unchecked_Access &
263 Ofiles &
264 All_Options;
265 First_Param : Positive := Params'First + 1;
266
267 begin
268 if Map_File then
269 First_Param := Params'First;
270 end if;
271
272 Utl.Gnatlink
273 (L_Afiles (L_Afiles'Last).all,
274 Params (First_Param .. Params'Last));
275 end;
276
277 OS_Lib.Delete_File (Exp_File, Success);
278 OS_Lib.Delete_File (Bas_File, Success);
279 OS_Lib.Delete_File (Jnk_File, Success);
280
281 exception
282 when others =>
283 OS_Lib.Delete_File (Exp_File, Success);
284 OS_Lib.Delete_File (Bas_File, Success);
285 OS_Lib.Delete_File (Jnk_File, Success);
286 raise;
287 end Ada_Build_DLL;
288
289 -- Start of processing for Build_Dynamic_Library
290
291 begin
292 -- On Windows the binder file must not be in the first position in the
293 -- list. This is due to the way DLL's are built on Windows. We swap the
294 -- first ali with the last one if it is the case.
295
296 if L_Afiles'Length > 1 then
297 declare
298 Filename : constant String :=
299 Directory_Operations.Base_Name
300 (L_Afiles (L_Afiles'First).all);
301 First : constant Positive := Filename'First;
302
303 begin
304 if Filename (First .. First + 1) = "b~" then
305 L_Afiles (L_Afiles'Last) := Afiles (Afiles'First);
306 L_Afiles (L_Afiles'First) := Afiles (Afiles'Last);
307 end if;
308 end;
309 end if;
310
311 if L_Afiles'Length = 0 then
312 Build_DLL;
313 else
314 Ada_Build_DLL;
315 end if;
316 end Build_Dynamic_Library;
317
318 --------------------------
319 -- Build_Import_Library --
320 --------------------------
321
322 procedure Build_Import_Library
323 (Lib_Filename : String;
324 Def_Filename : String)
325 is
326 function Strip_Lib_Prefix (Filename : String) return String;
327 -- Return Filename without the lib prefix if present
328
329 ----------------------
330 -- Strip_Lib_Prefix --
331 ----------------------
332
333 function Strip_Lib_Prefix (Filename : String) return String is
334 begin
335 if Filename (Filename'First .. Filename'First + 2) = "lib" then
336 return Filename (Filename'First + 3 .. Filename'Last);
337 else
338 return Filename;
339 end if;
340 end Strip_Lib_Prefix;
341
342 -- Local variables
343
344 Def_File : String renames Def_Filename;
345 Dll_File : constant String := Get_Dll_Name (Lib_Filename);
346 Base_Filename : constant String :=
347 MDLL.Fil.Ext_To (Strip_Lib_Prefix (Lib_Filename));
348 Lib_File : constant String := "lib" & Base_Filename & ".dll.a";
349
350 -- Start of processing for Build_Import_Library
351
352 begin
353 if not Quiet then
354 Text_IO.Put_Line ("Building import library...");
355 Text_IO.Put_Line
356 ("make " & Lib_File & " to use dynamic library " & Dll_File);
357 end if;
358
359 Utl.Dlltool
360 (Def_File, Dll_File, Lib_File, Build_Import => True);
361 end Build_Import_Library;
362
363 ------------------
364 -- Get_Dll_Name --
365 ------------------
366
367 function Get_Dll_Name (Lib_Filename : String) return String is
368 begin
369 if MDLL.Fil.Get_Ext (Lib_Filename) = "" then
370 return Lib_Filename & ".dll";
371 else
372 return Lib_Filename;
373 end if;
374 end Get_Dll_Name;
375
376 end MDLL;