PR c++/28018
[official-gcc.git] / gcc / ada / mdll.adb
blob2e7ae46ee9ecac03e0766809f1851cd9456481cf
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M D L L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, 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 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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 -- This package provides the core high level routines used by GNATDLL
28 -- to build Windows DLL
30 with Ada.Text_IO;
32 with GNAT.Directory_Operations;
33 with MDLL.Utl;
34 with MDLL.Fil;
36 package body MDLL is
38 use Ada;
39 use GNAT;
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
110 Success : Boolean;
112 begin
113 if not Quiet then
114 Text_IO.Put_Line ("building relocatable DLL...");
115 Text_IO.Put ("make " & Dll_File);
117 if Build_Import then
118 Text_IO.Put_Line (" and " & Lib_File);
119 else
120 Text_IO.New_Line;
121 end if;
122 end if;
124 -- 1) Build base file with objects files
126 Utl.Gcc (Output_File => Jnk_File,
127 Files => Ofiles,
128 Options => All_Options,
129 Base_File => Bas_File,
130 Build_Lib => True);
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,
145 Build_Lib => True);
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
156 declare
157 Params : constant OS_Lib.Argument_List :=
158 Map_Opt'Unchecked_Access &
159 Adr_Opt'Unchecked_Access & All_Options;
160 First_Param : Positive := Params'First + 1;
162 begin
163 if Map_File then
164 First_Param := Params'First;
165 end if;
167 Utl.Gcc
168 (Output_File => Dll_File,
169 Files => Objects_Exp_File,
170 Options => Params (First_Param .. Params'Last),
171 Build_Lib => True);
172 end;
174 OS_Lib.Delete_File (Exp_File, Success);
175 OS_Lib.Delete_File (Bas_File, Success);
176 OS_Lib.Delete_File (Jnk_File, Success);
178 exception
179 when others =>
180 OS_Lib.Delete_File (Exp_File, Success);
181 OS_Lib.Delete_File (Bas_File, Success);
182 OS_Lib.Delete_File (Jnk_File, Success);
183 raise;
184 end Build_Reloc_DLL;
186 -------------------------
187 -- Ada_Build_Reloc_DLL --
188 -------------------------
190 procedure Ada_Build_Reloc_DLL is
191 Success : Boolean;
193 begin
194 if not Quiet then
195 Text_IO.Put_Line ("Building relocatable DLL...");
196 Text_IO.Put ("make " & Dll_File);
198 if Build_Import then
199 Text_IO.Put_Line (" and " & Lib_File);
200 else
201 Text_IO.New_Line;
202 end if;
203 end if;
205 -- 1) Build base file with objects files
207 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
209 declare
210 Params : constant OS_Lib.Argument_List :=
211 Out_Opt'Unchecked_Access &
212 Jnk_File'Unchecked_Access &
213 Lib_Opt'Unchecked_Access &
214 Bas_Opt'Unchecked_Access &
215 Ofiles &
216 All_Options;
217 begin
218 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
219 end;
221 -- 2) Build exp from base file
223 Utl.Dlltool (Def_File, Dll_File, Lib_File,
224 Base_File => Bas_File,
225 Exp_Table => Exp_File,
226 Build_Import => False);
228 -- 3) Build base file with exp file and objects files
230 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
232 declare
233 Params : constant OS_Lib.Argument_List :=
234 Out_Opt'Unchecked_Access &
235 Jnk_File'Unchecked_Access &
236 Lib_Opt'Unchecked_Access &
237 Bas_Opt'Unchecked_Access &
238 Exp_File'Unchecked_Access &
239 Ofiles &
240 All_Options;
241 begin
242 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
243 end;
245 -- 4) Build new exp from base file and the lib file (.a)
247 Utl.Dlltool (Def_File, Dll_File, Lib_File,
248 Base_File => Bas_File,
249 Exp_Table => Exp_File,
250 Build_Import => Build_Import);
252 -- 5) Build the dynamic library
254 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
256 declare
257 Params : constant OS_Lib.Argument_List :=
258 Map_Opt'Unchecked_Access &
259 Out_Opt'Unchecked_Access &
260 Dll_File'Unchecked_Access &
261 Lib_Opt'Unchecked_Access &
262 Exp_File'Unchecked_Access &
263 Adr_Opt'Unchecked_Access &
264 Ofiles &
265 All_Options;
266 First_Param : Positive := Params'First + 1;
268 begin
269 if Map_File then
270 First_Param := Params'First;
271 end if;
273 Utl.Gnatlink
274 (L_Afiles (L_Afiles'Last).all,
275 Params (First_Param .. Params'Last));
276 end;
278 OS_Lib.Delete_File (Exp_File, Success);
279 OS_Lib.Delete_File (Bas_File, Success);
280 OS_Lib.Delete_File (Jnk_File, Success);
282 exception
283 when others =>
284 OS_Lib.Delete_File (Exp_File, Success);
285 OS_Lib.Delete_File (Bas_File, Success);
286 OS_Lib.Delete_File (Jnk_File, Success);
287 raise;
288 end Ada_Build_Reloc_DLL;
290 -------------------------
291 -- Build_Non_Reloc_DLL --
292 -------------------------
294 procedure Build_Non_Reloc_DLL is
295 Success : Boolean;
297 begin
298 if not Quiet then
299 Text_IO.Put_Line ("building non relocatable DLL...");
300 Text_IO.Put ("make " & Dll_File &
301 " using address " & Lib_Address);
303 if Build_Import then
304 Text_IO.Put_Line (" and " & Lib_File);
305 else
306 Text_IO.New_Line;
307 end if;
308 end if;
310 -- Build exp table and the lib .a file
312 Utl.Dlltool (Def_File, Dll_File, Lib_File,
313 Exp_Table => Exp_File,
314 Build_Import => Build_Import);
316 -- Build the DLL
318 declare
319 Params : OS_Lib.Argument_List :=
320 Adr_Opt'Unchecked_Access & All_Options;
321 begin
322 if Map_File then
323 Params := Map_Opt'Unchecked_Access & Params;
324 end if;
326 Utl.Gcc (Output_File => Dll_File,
327 Files => Exp_File'Unchecked_Access & Ofiles,
328 Options => Params,
329 Build_Lib => True);
330 end;
332 OS_Lib.Delete_File (Exp_File, Success);
334 exception
335 when others =>
336 OS_Lib.Delete_File (Exp_File, Success);
337 raise;
338 end Build_Non_Reloc_DLL;
340 -----------------------------
341 -- Ada_Build_Non_Reloc_DLL --
342 -----------------------------
344 -- Build a non relocatable DLL with Ada code
346 procedure Ada_Build_Non_Reloc_DLL is
347 Success : Boolean;
349 begin
350 if not Quiet then
351 Text_IO.Put_Line ("building non relocatable DLL...");
352 Text_IO.Put ("make " & Dll_File &
353 " using address " & Lib_Address);
355 if Build_Import then
356 Text_IO.Put_Line (" and " & Lib_File);
357 else
358 Text_IO.New_Line;
359 end if;
360 end if;
362 -- Build exp table and the lib .a file
364 Utl.Dlltool (Def_File, Dll_File, Lib_File,
365 Exp_Table => Exp_File,
366 Build_Import => Build_Import);
368 -- Build the DLL
370 Utl.Gnatbind (L_Afiles, Options & Bargs_Options);
372 declare
373 Params : OS_Lib.Argument_List :=
374 Out_Opt'Unchecked_Access &
375 Dll_File'Unchecked_Access &
376 Lib_Opt'Unchecked_Access &
377 Exp_File'Unchecked_Access &
378 Adr_Opt'Unchecked_Access &
379 Ofiles &
380 All_Options;
381 begin
382 if Map_File then
383 Params := Map_Opt'Unchecked_Access & Params;
384 end if;
386 Utl.Gnatlink (L_Afiles (L_Afiles'Last).all, Params);
387 end;
389 OS_Lib.Delete_File (Exp_File, Success);
391 exception
392 when others =>
393 OS_Lib.Delete_File (Exp_File, Success);
394 raise;
395 end Ada_Build_Non_Reloc_DLL;
397 begin
398 -- On Windows the binder file must not be in the first position in the
399 -- list. This is due to the way DLL's are built on Windows. We swap the
400 -- first ali with the last one if it is the case.
402 if L_Afiles'Length > 1 then
403 declare
404 Filename : constant String :=
405 Directory_Operations.Base_Name (L_Afiles (1).all);
406 First : constant Positive := Filename'First;
408 begin
409 if Filename (First .. First + 1) = "b~" then
410 L_Afiles (L_Afiles'Last) := Afiles (1);
411 L_Afiles (1) := Afiles (Afiles'Last);
412 end if;
413 end;
414 end if;
416 case Relocatable is
417 when True =>
418 if L_Afiles'Length = 0 then
419 Build_Reloc_DLL;
420 else
421 Ada_Build_Reloc_DLL;
422 end if;
424 when False =>
425 if L_Afiles'Length = 0 then
426 Build_Non_Reloc_DLL;
427 else
428 Ada_Build_Non_Reloc_DLL;
429 end if;
430 end case;
431 end Build_Dynamic_Library;
433 --------------------------
434 -- Build_Import_Library --
435 --------------------------
437 procedure Build_Import_Library
438 (Lib_Filename : String;
439 Def_Filename : String)
442 procedure Build_Import_Library (Lib_Filename : String);
443 -- Build an import library. This is to build only a .a library to link
444 -- against a DLL.
446 --------------------------
447 -- Build_Import_Library --
448 --------------------------
450 procedure Build_Import_Library (Lib_Filename : String) is
451 Def_File : String renames Def_Filename;
452 Dll_File : constant String := Get_Dll_Name (Lib_Filename);
453 Base_Filename : constant String := MDLL.Fil.Ext_To (Lib_Filename);
454 Lib_File : constant String := "lib" & Base_Filename & ".a";
456 begin
457 if not Quiet then
458 Text_IO.Put_Line ("Building import library...");
459 Text_IO.Put_Line
460 ("make " & Lib_File & " to use dynamic library " & Dll_File);
461 end if;
463 Utl.Dlltool
464 (Def_File, Dll_File, Lib_File, Build_Import => True);
465 end Build_Import_Library;
467 -- Start of processing for Build_Import_Library
469 begin
470 -- If the library has the form lib<name>.a then the def file should be
471 -- <name>.def and the DLL to link against <name>.dll. This is a Windows
472 -- convention and we try as much as possible to follow the platform
473 -- convention.
475 if Lib_Filename'Length > 3 and then Lib_Filename (1 .. 3) = "lib" then
476 Build_Import_Library (Lib_Filename (4 .. Lib_Filename'Last));
477 else
478 Build_Import_Library (Lib_Filename);
479 end if;
480 end Build_Import_Library;
482 ------------------
483 -- Get_Dll_Name --
484 ------------------
486 function Get_Dll_Name (Lib_Filename : String) return String is
487 begin
488 if MDLL.Fil.Get_Ext (Lib_Filename) = "" then
489 return Lib_Filename & ".dll";
490 else
491 return Lib_Filename;
492 end if;
493 end Get_Dll_Name;
495 end MDLL;