1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1997-2002, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
26 ------------------------------------------------------------------------------
28 -- GNATDLL is a Windows specific tool for building a DLL.
29 -- Both relocatable and non-relocatable DLL's are supported
32 with Ada
.Strings
.Unbounded
;
34 with Ada
.Command_Line
;
36 with GNAT
.Command_Line
;
47 use Ada
.Strings
.Unbounded
;
49 use type OS_Lib
.Argument_List
;
54 procedure Check
(Filename
: String);
55 -- Check that the file whose name is Filename exists
57 procedure Parse_Command_Line
;
58 -- Parse the command line arguments passed to gnatdll
60 procedure Check_Context
;
61 -- Check the context before runing any commands to build the library
63 Syntax_Error
: exception;
64 -- Raised when a syntax error is detected, in this case a usage info will
67 Context_Error
: exception;
68 -- Raised when some files (specifed on the command line) are missing to
71 Help
: Boolean := False;
72 -- Help will be set to True the usage information is to be displayed.
74 Version
: constant String := Gnatvsn
.Gnat_Version_String
;
75 -- Why should it be necessary to make a copy of this
77 Default_DLL_Address
: constant String := "0x11000000";
78 -- Default address for non relocatable DLL (Win32)
80 Lib_Filename
: Unbounded_String
:= Null_Unbounded_String
;
81 -- The DLL filename that will be created (.dll)
83 Def_Filename
: Unbounded_String
:= Null_Unbounded_String
;
84 -- The definition filename (.def)
86 List_Filename
: Unbounded_String
:= Null_Unbounded_String
;
87 -- The name of the file containing the objects file to put into the DLL
89 DLL_Address
: Unbounded_String
:=
90 To_Unbounded_String
(Default_DLL_Address
);
91 -- The DLL's base address
93 Objects_Files
: Argument_List_Access
:= Null_Argument_List_Access
;
94 -- List of objects to put inside the library
96 Ali_Files
: Argument_List_Access
:= Null_Argument_List_Access
;
97 -- For each Ada file specified, we keep arecord of the corresponding
98 -- ALI file. This list of SLI files is used to build the binder program.
100 Options
: Argument_List_Access
:= Null_Argument_List_Access
;
101 -- A list of options set in the command line.
103 Largs_Options
: Argument_List_Access
:= Null_Argument_List_Access
;
104 Bargs_Options
: Argument_List_Access
:= Null_Argument_List_Access
;
105 -- GNAT linker and binder args options
107 type Build_Mode_State
is (Import_Lib
, Dynamic_Lib
, Dynamic_Lib_Only
, Nil
);
108 -- Import_Lib means only the .a file will be created, Dynamic_Lib means
109 -- that both the DLL and the import library will be created.
110 -- Dynamic_Lib_Only means that only the DLL will be created (no import
113 Build_Mode
: Build_Mode_State
:= Nil
;
114 -- Will be set when parsing the command line.
116 Must_Build_Relocatable
: Boolean := True;
117 -- True means build a relocatable DLL, will be set to False if a
118 -- non-relocatable DLL must be built.
127 procedure P
(Str
: in String) renames Text_IO
.Put_Line
;
130 P
("Usage : gnatdll [options] [list-of-files]");
132 P
("[list-of-files] a list of Ada libraries (.ali) and/or " &
133 "foreign object files");
135 P
("[options] can be");
136 P
(" -h Help - display this message");
139 P
(" -k Remove @nn suffix from exported names");
140 P
(" -g Generate debugging information");
141 P
(" -Idir Specify source and object files search path");
142 P
(" -l file File contains a list-of-files to be added to "
144 P
(" -e file Definition file containing exports");
145 P
(" -d file Put objects in the relocatable dynamic "
147 P
(" -b addr Set base address for the relocatable DLL");
148 P
(" default address is " & Default_DLL_Address
);
149 P
(" -a[addr] Build non-relocatable DLL at address <addr>");
150 P
(" if <addr> is not specified use "
151 & Default_DLL_Address
);
152 P
(" -n No-import - do not create the import library");
153 P
(" -bargs opts opts are passed to the binder");
154 P
(" -largs opts opts are passed to the linker");
161 procedure Check
(Filename
: in String) is
163 if not OS_Lib
.Is_Regular_File
(Filename
) then
164 Exceptions
.Raise_Exception
(Context_Error
'Identity,
165 "Error: " & Filename
& " not found.");
169 ------------------------
170 -- Parse_Command_Line --
171 ------------------------
173 procedure Parse_Command_Line
is
175 use GNAT
.Command_Line
;
177 procedure Add_File
(Filename
: in String);
178 -- Add one file to the list of file to handle
180 procedure Add_Files_From_List
(List_Filename
: in String);
181 -- Add the files listed in List_Filename (one by line) to the list
184 Max_Files
: constant := 5_000
;
185 Max_Options
: constant := 100;
186 -- These are arbitrary limits, a better way will be to use linked list.
187 -- No, a better choice would be to use tables ???
190 Ofiles
: OS_Lib
.Argument_List
(1 .. Max_Files
);
191 O
: Positive := Ofiles
'First;
192 -- List of object files to put in the library. O is the next entry
195 Afiles
: OS_Lib
.Argument_List
(1 .. Max_Files
);
196 A
: Positive := Afiles
'First;
197 -- List of ALI files. A is the next entry to be used.
199 Gopts
: OS_Lib
.Argument_List
(1 .. Max_Options
);
200 G
: Positive := Gopts
'First;
201 -- List of gcc options. G is the next entry to be used.
203 Lopts
: OS_Lib
.Argument_List
(1 .. Max_Options
);
204 L
: Positive := Lopts
'First;
205 -- A list of -largs options (L is next entry to be used)
207 Bopts
: OS_Lib
.Argument_List
(1 .. Max_Options
);
208 B
: Positive := Bopts
'First;
209 -- A list of -bargs options (B is next entry to be used)
211 Build_Import
: Boolean := True;
212 -- Set to Fals if option -n if specified (no-import).
218 procedure Add_File
(Filename
: in String) is
220 if Fil
.Is_Ali
(Filename
) then
224 -- Record it to generate the binder program when
225 -- building dynamic library
227 Afiles
(A
) := new String'(Filename);
230 elsif Fil.Is_Obj (Filename) then
234 -- Just record this object file
236 Ofiles (O) := new String'(Filename
);
242 Exceptions
.Raise_Exception
243 (Syntax_Error
'Identity,
244 "don't know what to do with " & Filename
& " !");
248 -------------------------
249 -- Add_Files_From_List --
250 -------------------------
252 procedure Add_Files_From_List
(List_Filename
: in String) is
253 File
: Text_IO
.File_Type
;
254 Buffer
: String (1 .. 500);
258 Text_IO
.Open
(File
, Text_IO
.In_File
, List_Filename
);
260 while not Text_IO
.End_Of_File
(File
) loop
261 Text_IO
.Get_Line
(File
, Buffer
, Last
);
262 Add_File
(Buffer
(1 .. Last
));
265 Text_IO
.Close
(File
);
266 end Add_Files_From_List
;
268 -- Start of processing for Parse_Command_Line
271 Initialize_Option_Scan
('-', False, "bargs largs");
273 -- scan gnatdll switches
276 case Getopt
("g h v q k a? b: d: e: l: n I:") is
285 Gopts
(G
) := new String'("-g");
290 -- Turn verbose mode on
292 MDLL.Verbose := True;
294 Exceptions.Raise_Exception
295 (Syntax_Error'Identity,
296 "impossible to use -q and -v together.");
301 -- Turn quiet mode on
305 Exceptions.Raise_Exception
306 (Syntax_Error'Identity,
307 "impossible to use -v and -q together.");
312 MDLL.Kill_Suffix := True;
316 if Parameter = "" then
318 -- Default address for a relocatable dynamic library.
319 -- address for a non relocatable dynamic library.
321 DLL_Address := To_Unbounded_String (Default_DLL_Address);
324 DLL_Address := To_Unbounded_String (Parameter);
327 Must_Build_Relocatable := False;
331 DLL_Address := To_Unbounded_String (Parameter);
333 Must_Build_Relocatable := True;
337 Def_Filename := To_Unbounded_String (Parameter);
341 -- Build a non relocatable DLL
343 Lib_Filename := To_Unbounded_String (Parameter);
345 if Def_Filename = Null_Unbounded_String then
346 Def_Filename := To_Unbounded_String
347 (Fil.Ext_To (Parameter, "def"));
350 Build_Mode := Dynamic_Lib;
354 Build_Import := False;
357 List_Filename := To_Unbounded_String (Parameter);
360 Gopts (G) := new String'("-I" & Parameter
);
364 raise Invalid_Switch
;
373 File
: constant String := Get_Argument
(Do_Expansion
=> True);
375 exit when File
'Length = 0;
380 -- Get largs parameters
382 Goto_Section
("largs");
391 Lopts
(L
) := new String'(Full_Switch);
397 -- Get bargs parameters
399 Goto_Section ("bargs");
408 Bopts (B) := new String'(Full_Switch
);
414 -- if list filename has been specified, parse it
416 if List_Filename
/= Null_Unbounded_String
then
417 Add_Files_From_List
(To_String
(List_Filename
));
420 -- Check if the set of parameters are compatible.
422 if Build_Mode
= Nil
and then not Help
and then not Verbose
then
423 Exceptions
.Raise_Exception
424 (Syntax_Error
'Identity,
428 -- -n option but no file specified
431 and then A
= Afiles
'First
432 and then O
= Ofiles
'First
434 Exceptions
.Raise_Exception
435 (Syntax_Error
'Identity,
436 "-n specified but there are no objects to build the library.");
439 -- Check if we want to build an import library (option -e and
440 -- no file specified)
442 if Build_Mode
= Dynamic_Lib
443 and then A
= Afiles
'First
444 and then O
= Ofiles
'First
446 Build_Mode
:= Import_Lib
;
449 -- Check if only a dynamic library must be built.
451 if Build_Mode
= Dynamic_Lib
and then not Build_Import
then
452 Build_Mode
:= Dynamic_Lib_Only
;
455 if O
/= Ofiles
'First then
456 Objects_Files
:= new OS_Lib
.Argument_List
'(Ofiles (1 .. O - 1));
459 if A /= Afiles'First then
460 Ali_Files := new OS_Lib.Argument_List'(Afiles
(1 .. A
- 1));
463 if G
/= Gopts
'First then
464 Options
:= new OS_Lib
.Argument_List
'(Gopts (1 .. G - 1));
467 if L /= Lopts'First then
468 Largs_Options := new OS_Lib.Argument_List'(Lopts
(1 .. L
- 1));
471 if B
/= Bopts
'First then
472 Bargs_Options
:= new OS_Lib
.Argument_List
'(Bopts (1 .. B - 1));
477 when Invalid_Switch =>
478 Exceptions.Raise_Exception
479 (Syntax_Error'Identity,
480 Message => "Invalid Switch " & Full_Switch);
482 when Invalid_Parameter =>
483 Exceptions.Raise_Exception
484 (Syntax_Error'Identity,
485 Message => "No parameter for " & Full_Switch);
487 end Parse_Command_Line;
493 procedure Check_Context is
496 Check (To_String (Def_Filename));
498 -- Check that each object file specified exists and raise exception
499 -- Context_Error if it does not.
501 for F in Objects_Files'Range loop
502 Check (Objects_Files (F).all);
506 -- Start of processing for Gnatdll
509 if Ada.Command_Line.Argument_Count = 0 then
515 if MDLL.Verbose or else Help then
517 Text_IO.Put_Line ("GNATDLL " & Version & " - Dynamic Libraries Builder");
524 or else (MDLL.Verbose and then Ada.Command_Line.Argument_Count = 1)
533 MDLL.Build_Import_Library
534 (To_String (Lib_Filename),
535 To_String (Def_Filename));
538 MDLL.Build_Dynamic_Library
544 To_String (Lib_Filename),
545 To_String (Def_Filename),
546 To_String (DLL_Address),
547 Build_Import => True,
548 Relocatable => Must_Build_Relocatable);
550 when Dynamic_Lib_Only =>
551 MDLL.Build_Dynamic_Library
557 To_String (Lib_Filename),
558 To_String (Def_Filename),
559 To_String (DLL_Address),
560 Build_Import => False,
561 Relocatable => Must_Build_Relocatable);
570 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
574 when SE : Syntax_Error =>
575 Text_IO.Put_Line ("Syntax error : " & Exceptions.Exception_Message (SE));
578 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
580 when E : Tools_Error | Context_Error =>
581 Text_IO.Put_Line (Exceptions.Exception_Message (E));
582 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
585 Text_IO.Put_Line ("gnatdll: INTERNAL ERROR. Please report");
586 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);