OSDN Git Service

2003-10-22 Arnaud Charlet <charlet@act-europe.fr>
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatsym.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              G N A T S Y M                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2003 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 --  This utility application creates symbol files in a format that is
28 --  platform-dependent.
29
30 --  A symbol file is a text file that lists the symbols to be exported from
31 --  a shared library. The format of a symbol file depends on the platform;
32 --  it may be a simple enumeration of the symbol (one per line) or a more
33 --  elaborate format (on VMS, for example). A symbol file may be used as an
34 --  input to the platform linker when building a shared library.
35
36 --  This utility is not available on all platforms. It is currently supported
37 --  only on OpenVMS.
38
39 --  gnatsym takes as parameters:
40 --    - the name of the symbol file to create or update
41 --    - the names of one or more object files where the symbols are found
42
43 with GNAT.Command_Line; use GNAT.Command_Line;
44 with GNAT.OS_Lib;       use GNAT.OS_Lib;
45
46 with Gnatvsn; use Gnatvsn;
47 with Osint;   use Osint;
48 with Output;  use Output;
49
50 with Symbols; use Symbols;
51 with Table;
52
53 procedure Gnatsym is
54
55    Copyright_Displayed : Boolean := False;
56    --  A flag to prevent multiple display of the Copyright notice
57
58    Success : Boolean := True;
59
60    Force : Boolean := False;
61    --  True when -f switcxh is used
62
63    Verbose : Boolean := False;
64    --  True when -v switch is used
65
66    Quiet : Boolean := False;
67    --  True when -q switch is used
68
69    Symbol_File_Name : String_Access;
70    --  The name of the symbol file
71
72    package Object_Files is new Table.Table
73      (Table_Component_Type => String_Access,
74       Table_Index_Type     => Natural,
75       Table_Low_Bound      => 0,
76       Table_Initial        => 10,
77       Table_Increment      => 10,
78       Table_Name           => "Gnatsymb.Object_Files");
79    --  A table to store the object file names
80
81    Object_File : Natural := 0;
82    --  An index to traverse the Object_Files table
83
84    procedure Display_Copyright;
85    --  Display Copyright notice
86
87    procedure Parse_Cmd_Line;
88    --  Parse the command line switches and file names
89
90    procedure Usage;
91    --  Display the usage
92
93    -----------------------
94    -- Display_Copyright --
95    -----------------------
96
97    procedure Display_Copyright is
98    begin
99       if not Copyright_Displayed then
100          Write_Eol;
101          Write_Str ("GNATSYMB ");
102          Write_Str (Gnat_Version_String);
103          Write_Str (" Copyright 2003 Free Software Foundation, Inc");
104          Write_Eol;
105          Copyright_Displayed := True;
106       end if;
107    end Display_Copyright;
108
109    --------------------
110    -- Parse_Cmd_Line --
111    --------------------
112
113    procedure Parse_Cmd_Line is
114    begin
115       loop
116          case GNAT.Command_Line.Getopt ("f q v") is
117             when ASCII.NUL =>
118                exit;
119
120             when 'f' =>
121                Force := True;
122
123             when 'q' =>
124                Quiet := True;
125
126             when 'v' =>
127                Verbose := True;
128
129             when others =>
130                Fail ("invalid switch: ", Full_Switch);
131          end case;
132       end loop;
133
134       --  Get the file names
135
136       loop
137          declare
138             S : constant String_Access :=
139                            new String'(GNAT.Command_Line.Get_Argument);
140
141          begin
142             exit when S'Length = 0;
143
144             if Symbol_File_Name = null then
145                Symbol_File_Name := S;
146
147             else
148                Object_Files.Increment_Last;
149                Object_Files.Table (Object_Files.Last) := S;
150             end if;
151          end;
152       end loop;
153    exception
154       when Invalid_Switch =>
155          Usage;
156          Fail ("invalid switch : ", Full_Switch);
157    end Parse_Cmd_Line;
158
159    -----------
160    -- Usage --
161    -----------
162
163    procedure Usage is
164    begin
165       Write_Line ("gnatsym [options] sym_file object_file {object_file}");
166       Write_Eol;
167       Write_Line ("   -f  Force generation of symbol file");
168       Write_Line ("   -q  Quiet mode");
169       Write_Line ("   -v  Verbose mode");
170       Write_Eol;
171    end Usage;
172
173 --  Start of processing of Gnatsym
174
175 begin
176    --  Initialize Object_Files table
177
178    Object_Files.Set_Last (0);
179
180    --  Parse the command line
181
182    Parse_Cmd_Line;
183
184    if Verbose then
185       Display_Copyright;
186    end if;
187
188    --  If there is no symbol file or no object files on the command line,
189    --  display the usage and exit with an error status.
190
191    if Object_Files.Last = 0 then
192       Usage;
193       OS_Exit (1);
194
195    else
196       if Verbose then
197          Write_Str ("Initializing symbol file """);
198          Write_Str (Symbol_File_Name.all);
199          Write_Line ("""");
200       end if;
201
202       --  Initialize the symbol file
203
204       Symbols.Initialize (Symbol_File_Name.all, Force, Quiet, Success);
205
206       --  Process the object files in order. Stop as soon as there is
207       --  something wrong.
208
209       Object_File := 0;
210
211       while Success and then Object_File < Object_Files.Last loop
212          Object_File := Object_File + 1;
213
214          if Verbose then
215             Write_Str ("Processing object file """);
216             Write_Str (Object_Files.Table (Object_File).all);
217             Write_Line ("""");
218          end if;
219
220          Process (Object_Files.Table (Object_File).all, Success);
221       end loop;
222
223       --  Finalize the object file
224
225       if Success then
226          if Verbose then
227             Write_Str ("Finalizing """);
228             Write_Str (Symbol_File_Name.all);
229             Write_Line ("""");
230          end if;
231
232          Finalize (Quiet, Success);
233       end if;
234
235       if not Success then
236          Fail ("unable to build symbol file");
237       end if;
238    end if;
239 end Gnatsym;