OSDN Git Service

* config/arm/crti.asm: Give _init and _fini function type.
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatdll.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              G N A T D L L                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1997-2004, 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
26
27 --  GNATDLL is a Windows specific tool for building a DLL.
28 --  Both relocatable and non-relocatable DLL's are supported
29
30 with Ada.Text_IO;           use Ada.Text_IO;
31 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
32 with Ada.Exceptions;        use Ada.Exceptions;
33 with Ada.Command_Line;      use Ada.Command_Line;
34 with GNAT.OS_Lib;           use GNAT.OS_Lib;
35 with GNAT.Command_Line;     use GNAT.Command_Line;
36 with Gnatvsn;
37
38 with MDLL.Fil;              use MDLL.Fil;
39 with MDLL.Utl;              use MDLL.Utl;
40
41 procedure Gnatdll is
42
43    use type GNAT.OS_Lib.Argument_List;
44
45    procedure Syntax;
46    --  Print out usage
47
48    procedure Check (Filename : String);
49    --  Check that the file whose name is Filename exists
50
51    procedure Parse_Command_Line;
52    --  Parse the command line arguments passed to gnatdll
53
54    procedure Check_Context;
55    --  Check the context before runing any commands to build the library
56
57    Syntax_Error : exception;
58    --  Raised when a syntax error is detected, in this case a usage info will
59    --  be displayed.
60
61    Context_Error : exception;
62    --  Raised when some files (specifed on the command line) are missing to
63    --  build the DLL.
64
65    Help : Boolean := False;
66    --  Help will be set to True the usage information is to be displayed.
67
68    Version : constant String := Gnatvsn.Gnat_Version_String;
69    --  Why should it be necessary to make a copy of this
70
71    Default_DLL_Address : constant String := "0x11000000";
72    --  Default address for non relocatable DLL (Win32)
73
74    Lib_Filename : Unbounded_String := Null_Unbounded_String;
75    --  The DLL filename that will be created (.dll)
76
77    Def_Filename : Unbounded_String := Null_Unbounded_String;
78    --  The definition filename (.def)
79
80    List_Filename : Unbounded_String := Null_Unbounded_String;
81    --  The name of the file containing the objects file to put into the DLL
82
83    DLL_Address : Unbounded_String := To_Unbounded_String (Default_DLL_Address);
84    --  The DLL's base address
85
86    Gen_Map_File : Boolean := False;
87    --  Set to True if a map file is to be generated
88
89    Objects_Files : Argument_List_Access := MDLL.Null_Argument_List_Access;
90    --  List of objects to put inside the library
91
92    Ali_Files : Argument_List_Access := MDLL.Null_Argument_List_Access;
93    --  For each Ada file specified, we keep arecord of the corresponding
94    --  ALI file. This list of SLI files is used to build the binder program.
95
96    Options : Argument_List_Access := MDLL.Null_Argument_List_Access;
97    --  A list of options set in the command line
98
99    Largs_Options : Argument_List_Access := MDLL.Null_Argument_List_Access;
100    Bargs_Options : Argument_List_Access := MDLL.Null_Argument_List_Access;
101    --  GNAT linker and binder args options
102
103    type Build_Mode_State is (Import_Lib, Dynamic_Lib, Dynamic_Lib_Only, Nil);
104    --  Import_Lib means only the .a file will be created, Dynamic_Lib means
105    --  that both the DLL and the import library will be created.
106    --  Dynamic_Lib_Only means that only the DLL will be created (no import
107    --  library).
108
109    Build_Mode : Build_Mode_State := Nil;
110    --  Will be set when parsing the command line
111
112    Must_Build_Relocatable : Boolean := True;
113    --  True means build a relocatable DLL, will be set to False if a
114    --  non-relocatable DLL must be built.
115
116    ------------
117    -- Syntax --
118    ------------
119
120    procedure Syntax is
121       procedure P (Str : String) renames Put_Line;
122    begin
123       P ("Usage : gnatdll [options] [list-of-files]");
124       New_Line;
125       P ("[list-of-files] a list of Ada libraries (.ali) and/or " &
126          "foreign object files");
127       New_Line;
128       P ("[options] can be");
129       P ("   -h            Help - display this message");
130       P ("   -v            Verbose");
131       P ("   -q            Quiet");
132       P ("   -k            Remove @nn suffix from exported names");
133       P ("   -g            Generate debugging information");
134       P ("   -Idir         Specify source and object files search path");
135       P ("   -l file       File contains a list-of-files to be added to "
136          & "the library");
137       P ("   -e file       Definition file containing exports");
138       P ("   -d file       Put objects in the relocatable dynamic "
139          & "library <file>");
140       P ("   -b addr       Set base address for the relocatable DLL");
141       P ("                 default address is " & Default_DLL_Address);
142       P ("   -a[addr]      Build non-relocatable DLL at address <addr>");
143       P ("                 if <addr> is not specified use "
144          & Default_DLL_Address);
145       P ("   -m            Generate map file");
146       P ("   -n            No-import - do not create the import library");
147       P ("   -bargs opts   opts are passed to the binder");
148       P ("   -largs opts   opts are passed to the linker");
149    end Syntax;
150
151    -----------
152    -- Check --
153    -----------
154
155    procedure Check (Filename : in String) is
156    begin
157       if not Is_Regular_File (Filename) then
158          Raise_Exception
159            (Context_Error'Identity, "Error: " & Filename & " not found.");
160       end if;
161    end Check;
162
163    ------------------------
164    -- Parse_Command_Line --
165    ------------------------
166
167    procedure Parse_Command_Line is
168
169       use GNAT.Command_Line;
170
171       procedure Add_File (Filename : in String);
172       --  Add one file to the list of file to handle
173
174       procedure Add_Files_From_List (List_Filename : in String);
175       --  Add the files listed in List_Filename (one by line) to the list
176       --  of file to handle
177
178       Max_Files   : constant := 5_000;
179       Max_Options : constant :=   100;
180       --  These are arbitrary limits, a better way will be to use linked list.
181       --  No, a better choice would be to use tables ???
182       --  Limits on what???
183
184       Ofiles : Argument_List (1 .. Max_Files);
185       O      : Positive := Ofiles'First;
186       --  List of object files to put in the library. O is the next entry
187       --  to be used.
188
189       Afiles : Argument_List (1 .. Max_Files);
190       A      : Positive := Afiles'First;
191       --  List of ALI files. A is the next entry to be used
192
193       Gopts  : Argument_List (1 .. Max_Options);
194       G      : Positive := Gopts'First;
195       --  List of gcc options. G is the next entry to be used
196
197       Lopts  : Argument_List (1 .. Max_Options);
198       L      : Positive := Lopts'First;
199       --  A list of -largs options (L is next entry to be used)
200
201       Bopts  : Argument_List (1 .. Max_Options);
202       B      : Positive := Bopts'First;
203       --  A list of -bargs options (B is next entry to be used)
204
205       Build_Import : Boolean := True;
206       --  Set to Fals if option -n if specified (no-import)
207
208       --------------
209       -- Add_File --
210       --------------
211
212       procedure Add_File (Filename : in String) is
213       begin
214          if Is_Ali (Filename) then
215
216             Check (Filename);
217
218             --  Record it to generate the binder program when
219             --  building dynamic library
220
221             Afiles (A) := new String'(Filename);
222             A := A + 1;
223
224          elsif Is_Obj (Filename) then
225
226             Check (Filename);
227
228             --  Just record this object file
229
230             Ofiles (O) := new String'(Filename);
231             O := O + 1;
232
233          else
234             --  Unknown file type
235
236             Raise_Exception
237               (Syntax_Error'Identity,
238                "don't know what to do with " & Filename & " !");
239          end if;
240       end Add_File;
241
242       -------------------------
243       -- Add_Files_From_List --
244       -------------------------
245
246       procedure Add_Files_From_List (List_Filename : in String) is
247          File   : File_Type;
248          Buffer : String (1 .. 500);
249          Last   : Natural;
250
251       begin
252          Open (File, In_File, List_Filename);
253
254          while not End_Of_File (File) loop
255             Get_Line (File, Buffer, Last);
256             Add_File (Buffer (1 .. Last));
257          end loop;
258
259          Close (File);
260       end Add_Files_From_List;
261
262    --  Start of processing for Parse_Command_Line
263
264    begin
265       Initialize_Option_Scan ('-', False, "bargs largs");
266
267       --  scan gnatdll switches
268
269       loop
270          case Getopt ("g h v q k a? b: d: e: l: n m I:") is
271
272             when ASCII.Nul =>
273                exit;
274
275             when 'h' =>
276                Help := True;
277
278             when 'g' =>
279                Gopts (G) := new String'("-g");
280                G := G + 1;
281
282             when 'v' =>
283
284                --  Turn verbose mode on
285
286                MDLL.Verbose := True;
287                if MDLL.Quiet then
288                   Raise_Exception
289                     (Syntax_Error'Identity,
290                      "impossible to use -q and -v together.");
291                end if;
292
293             when 'q' =>
294
295                --  Turn quiet mode on
296
297                MDLL.Quiet := True;
298                if MDLL.Verbose then
299                   Raise_Exception
300                     (Syntax_Error'Identity,
301                      "impossible to use -v and -q together.");
302                end if;
303
304             when 'k' =>
305
306                MDLL.Kill_Suffix := True;
307
308             when 'a' =>
309
310                if Parameter = "" then
311
312                   --  Default address for a relocatable dynamic library.
313                   --  address for a non relocatable dynamic library.
314
315                   DLL_Address := To_Unbounded_String (Default_DLL_Address);
316
317                else
318                   DLL_Address := To_Unbounded_String (Parameter);
319                end if;
320
321                Must_Build_Relocatable := False;
322
323             when 'b' =>
324
325                DLL_Address := To_Unbounded_String (Parameter);
326
327                Must_Build_Relocatable := True;
328
329             when 'e' =>
330
331                Def_Filename := To_Unbounded_String (Parameter);
332
333             when 'd' =>
334
335                --  Build a non relocatable DLL
336
337                Lib_Filename := To_Unbounded_String (Parameter);
338
339                if Def_Filename = Null_Unbounded_String then
340                   Def_Filename := To_Unbounded_String
341                     (Ext_To (Parameter, "def"));
342                end if;
343
344                Build_Mode := Dynamic_Lib;
345
346             when 'm' =>
347
348                Gen_Map_File := True;
349
350             when 'n' =>
351
352                Build_Import := False;
353
354             when 'l' =>
355                List_Filename := To_Unbounded_String (Parameter);
356
357             when 'I' =>
358                Gopts (G) := new String'("-I" & Parameter);
359                G := G + 1;
360
361             when others =>
362                raise Invalid_Switch;
363          end case;
364       end loop;
365
366       --  Get parameters
367
368       loop
369          declare
370             File : constant String := Get_Argument (Do_Expansion => True);
371          begin
372             exit when File'Length = 0;
373             Add_File (File);
374          end;
375       end loop;
376
377       --  Get largs parameters
378
379       Goto_Section ("largs");
380
381       loop
382          case Getopt ("*") is
383             when ASCII.Nul =>
384                exit;
385
386             when others =>
387                Lopts (L) := new String'(Full_Switch);
388                L := L + 1;
389          end case;
390       end loop;
391
392       --  Get bargs parameters
393
394       Goto_Section ("bargs");
395
396       loop
397          case Getopt ("*") is
398
399             when ASCII.Nul =>
400                exit;
401
402             when others =>
403                Bopts (B) := new String'(Full_Switch);
404                B := B + 1;
405
406          end case;
407       end loop;
408
409       --  if list filename has been specified, parse it
410
411       if List_Filename /= Null_Unbounded_String then
412          Add_Files_From_List (To_String (List_Filename));
413       end if;
414
415       --  Check if the set of parameters are compatible
416
417       if Build_Mode = Nil and then not Help and then not MDLL.Verbose then
418          Raise_Exception (Syntax_Error'Identity, "nothing to do.");
419       end if;
420
421       --  -n option but no file specified
422
423       if not Build_Import
424         and then A = Afiles'First
425         and then O = Ofiles'First
426       then
427          Raise_Exception
428            (Syntax_Error'Identity,
429             "-n specified but there are no objects to build the library.");
430       end if;
431
432       --  Check if we want to build an import library (option -e and
433       --  no file specified)
434
435       if Build_Mode = Dynamic_Lib
436         and then A = Afiles'First
437         and then O = Ofiles'First
438       then
439          Build_Mode := Import_Lib;
440       end if;
441
442       --  If map file is to be generated, add linker option here
443
444       if Gen_Map_File and then Build_Mode = Import_Lib then
445          Raise_Exception
446            (Syntax_Error'Identity,
447             "Can't generate a map file for an import library.");
448       end if;
449
450       --  Check if only a dynamic library must be built
451
452       if Build_Mode = Dynamic_Lib and then not Build_Import then
453          Build_Mode := Dynamic_Lib_Only;
454       end if;
455
456       if O /= Ofiles'First then
457          Objects_Files := new Argument_List'(Ofiles (1 .. O - 1));
458       end if;
459
460       if A /= Afiles'First then
461          Ali_Files     := new Argument_List'(Afiles (1 .. A - 1));
462       end if;
463
464       if G /= Gopts'First then
465          Options       := new Argument_List'(Gopts (1 .. G - 1));
466       end if;
467
468       if L /= Lopts'First then
469          Largs_Options := new Argument_List'(Lopts (1 .. L - 1));
470       end if;
471
472       if B /= Bopts'First then
473          Bargs_Options := new Argument_List'(Bopts (1 .. B - 1));
474       end if;
475
476    exception
477
478       when Invalid_Switch    =>
479          Raise_Exception
480            (Syntax_Error'Identity,
481             Message => "Invalid Switch " & Full_Switch);
482
483       when Invalid_Parameter =>
484          Raise_Exception
485            (Syntax_Error'Identity,
486             Message => "No parameter for " & Full_Switch);
487
488    end Parse_Command_Line;
489
490    -------------------
491    -- Check_Context --
492    -------------------
493
494    procedure Check_Context is
495    begin
496
497       Check (To_String (Def_Filename));
498
499       --  Check that each object file specified exists and raise exception
500       --  Context_Error if it does not.
501
502       for F in Objects_Files'Range loop
503          Check (Objects_Files (F).all);
504       end loop;
505    end Check_Context;
506
507 --  Start of processing for Gnatdll
508
509 begin
510    if Ada.Command_Line.Argument_Count = 0 then
511       Help := True;
512    else
513       Parse_Command_Line;
514    end if;
515
516    if MDLL.Verbose or else Help then
517       New_Line;
518       Put_Line ("GNATDLL " & Version & " - Dynamic Libraries Builder");
519       New_Line;
520    end if;
521
522    MDLL.Utl.Locate;
523
524    if Help
525      or else (MDLL.Verbose and then Ada.Command_Line.Argument_Count = 1)
526    then
527       Syntax;
528    else
529       Check_Context;
530
531       case Build_Mode is
532
533          when Import_Lib =>
534             MDLL.Build_Import_Library
535               (To_String (Lib_Filename),
536                To_String (Def_Filename));
537
538          when Dynamic_Lib =>
539             MDLL.Build_Dynamic_Library
540               (Objects_Files.all,
541                Ali_Files.all,
542                Options.all,
543                Bargs_Options.all,
544                Largs_Options.all,
545                To_String (Lib_Filename),
546                To_String (Def_Filename),
547                To_String (DLL_Address),
548                Build_Import => True,
549                Relocatable  => Must_Build_Relocatable,
550                Map_File     => Gen_Map_File);
551
552          when Dynamic_Lib_Only =>
553             MDLL.Build_Dynamic_Library
554               (Objects_Files.all,
555                Ali_Files.all,
556                Options.all,
557                Bargs_Options.all,
558                Largs_Options.all,
559                To_String (Lib_Filename),
560                To_String (Def_Filename),
561                To_String (DLL_Address),
562                Build_Import => False,
563                Relocatable  => Must_Build_Relocatable,
564                Map_File     => Gen_Map_File);
565
566          when Nil =>
567             null;
568       end case;
569    end if;
570
571    Set_Exit_Status (Success);
572
573 exception
574
575    when SE : Syntax_Error =>
576       Put_Line ("Syntax error : " & Exception_Message (SE));
577       New_Line;
578       Syntax;
579       Set_Exit_Status (Failure);
580
581    when E : MDLL.Tools_Error | Context_Error =>
582       Put_Line (Exception_Message (E));
583       Set_Exit_Status (Failure);
584
585    when others =>
586       Put_Line ("gnatdll: INTERNAL ERROR. Please report");
587       Set_Exit_Status (Failure);
588
589 end Gnatdll;