1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
30 with ALI.Util; use ALI.Util;
31 with Bcheck; use Bcheck;
32 with Binde; use Binde;
33 with Binderr; use Binderr;
34 with Bindgen; use Bindgen;
36 with Butil; use Butil;
38 with Gnatvsn; use Gnatvsn;
39 with Namet; use Namet;
41 with Osint; use Osint;
42 with Output; use Output;
43 with Switch; use Switch;
44 with Types; use Types;
48 Total_Errors : Nat := 0;
49 -- Counts total errors in all files
51 Total_Warnings : Nat := 0;
52 -- Total warnings in all files
54 Main_Lib_File : File_Name_Type;
55 -- Current main library file
57 Std_Lib_File : File_Name_Type;
60 Text : Text_Buffer_Ptr;
65 Output_File_Name_Seen : Boolean := False;
67 Output_File_Name : String_Ptr := new String'("");
69 procedure Scan_Bind_Arg (Argv : String);
70 -- Scan and process binder specific arguments. Argv is a single argument.
71 -- All the one character arguments are still handled by Switch. This
72 -- routine handles -aO -aI and -I-.
78 procedure Scan_Bind_Arg (Argv : String) is
80 -- Now scan arguments that are specific to the binder and are not
81 -- handled by the common circuitry in Switch.
83 if Opt.Output_File_Name_Present
84 and then not Output_File_Name_Seen
86 Output_File_Name_Seen := True;
89 or else (Argv'Length >= 1
90 and then (Argv (1) = Switch_Character
91 or else Argv (1) = '-'))
93 Fail ("output File_Name missing after -o");
96 Output_File_Name := new String'(Argv);
99 elsif Argv'Length >= 2
100 and then (Argv (1) = Switch_Character
101 or else Argv (1) = '-')
105 if Argv (2 .. Argv'Last) = "I-" then
106 Opt.Look_In_Primary_Dir := False;
110 elsif Argv (2) = 'I' then
111 Add_Src_Search_Dir (Argv (3 .. Argv'Last));
112 Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
116 elsif Argv (2) = 'L' then
117 if Argv'Length >= 3 then
118 Opt.Bind_For_Library := True;
120 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
121 Opt.Ada_Final_Name :=
122 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Final_Suffix);
124 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
126 -- This option (-Lxxx) implies -n
128 Opt.Bind_Main_Program := False;
131 ("Prefix of initialization and finalization " &
132 "procedure names missing in -L");
135 -- -Sin -Slo -Shi -Sxx
137 elsif Argv'Length = 4
138 and then Argv (2) = 'S'
141 C1 : Character := Argv (3);
142 C2 : Character := Argv (4);
145 if C1 in 'a' .. 'z' then
146 C1 := Character'Val (Character'Pos (C1) - 32);
149 if C2 in 'a' .. 'z' then
150 C2 := Character'Val (Character'Pos (C2) - 32);
153 if C1 = 'I' and then C2 = 'N' then
154 Initialize_Scalars_Mode := 'I';
156 elsif C1 = 'L' and then C2 = 'O' then
157 Initialize_Scalars_Mode := 'L';
159 elsif C1 = 'H' and then C2 = 'I' then
160 Initialize_Scalars_Mode := 'H';
162 elsif (C1 in '0' .. '9' or else C1 in 'A' .. 'F')
164 (C2 in '0' .. '9' or else C2 in 'A' .. 'F')
166 Initialize_Scalars_Mode := 'X';
167 Initialize_Scalars_Val (1) := C1;
168 Initialize_Scalars_Val (2) := C2;
170 -- Invalid -S switch, let Switch give error
173 Scan_Binder_Switches (Argv);
179 elsif Argv'Length >= 3
180 and then Argv (2 .. 3) = "aI"
182 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
186 elsif Argv'Length >= 3
187 and then Argv (2 .. 3) = "aO"
189 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
193 elsif Argv (2 .. Argv'Last) = "nostdlib" then
194 Opt.No_Stdlib := True;
198 elsif Argv (2 .. Argv'Last) = "nostdinc" then
199 Opt.No_Stdinc := True;
203 elsif Argv (2 .. Argv'Last) = "static" then
204 Opt.Shared_Libgnat := False;
208 elsif Argv (2 .. Argv'Last) = "shared" then
209 Opt.Shared_Libgnat := True;
213 elsif Argv'Length >= 3 and then Argv (2) = 'M' then
214 Opt.Bind_Alternate_Main_Name := True;
215 Opt.Alternate_Main_Name := new String '(Argv (3 .. Argv'Last));
217 -- All other options are single character and are handled
218 -- by Scan_Binder_Switches.
221 Scan_Binder_Switches (Argv);
224 -- Not a switch, so must be a file name (if non-empty)
226 elsif Argv'Length /= 0 then
228 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
230 Set_Main_File_Name (Argv);
232 Set_Main_File_Name (Argv & ".ali");
237 -- Start of processing for Gnatbind
240 Osint.Initialize (Binder);
242 -- Set default for Shared_Libgnat option
245 Shared_Libgnat_Default : Character;
246 pragma Import (C, Shared_Libgnat_Default, "shared_libgnat_default");
248 SHARED : constant Character := 'H';
249 STATIC : constant Character := 'T';
253 (Shared_Libgnat_Default = SHARED
255 Shared_Libgnat_Default = STATIC);
256 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
259 -- Use low level argument routines to avoid dragging in the secondary stack
262 Scan_Args : while Next_Arg < Arg_Count loop
264 Next_Argv : String (1 .. Len_Arg (Next_Arg));
267 Fill_Arg (Next_Argv'Address, Next_Arg);
268 Scan_Bind_Arg (Next_Argv);
270 Next_Arg := Next_Arg + 1;
273 -- Test for trailing -o switch
275 if Opt.Output_File_Name_Present
276 and then not Output_File_Name_Seen
278 Fail ("output file name missing after -o");
281 -- Output usage if requested
283 if Usage_Requested then
287 -- Check that the Ada binder file specified has extension .adb and that
288 -- the C binder file has extension .c
290 if Opt.Output_File_Name_Present
291 and then Output_File_Name_Seen
293 Check_Extensions : declare
294 Length : constant Natural := Output_File_Name'Length;
295 Last : constant Natural := Output_File_Name'Last;
298 if Ada_Bind_File then
300 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
302 Fail ("output file name should have .adb extension");
307 or else Output_File_Name (Last - 1 .. Last) /= ".c"
309 Fail ("output file name should have .c extension");
312 end Check_Extensions;
315 Osint.Add_Default_Search_Dirs;
319 Write_Str ("GNATBIND ");
320 Write_Str (Gnat_Version_String);
321 Write_Str (" Copyright 1995-2001 Free Software Foundation, Inc.");
325 -- Output usage information if no files
327 if not More_Lib_Files then
329 Exit_Program (E_Fatal);
332 -- The block here is to catch the Unrecoverable_Error exception in the
333 -- case where we exceed the maximum number of permissible errors or some
334 -- other unrecoverable error occurs.
337 -- Carry out package initializations. These are initializations which
338 -- might logically be performed at elaboration time, but Namet at
339 -- least can't be done that way (because it is used in the Compiler),
340 -- and we decide to be consistent. Like elaboration, the order in
341 -- which these calls are made is in some cases important.
347 Initialize_ALI_Source;
355 while More_Lib_Files loop
356 Main_Lib_File := Next_Main_Lib_File;
360 Write_Str ("Checking: ");
362 Write_Str ("Binding: ");
365 Write_Name (Main_Lib_File);
369 Text := Read_Library_Info (Main_Lib_File, True);
373 Ignore_ED => Force_RM_Elaboration_Order,
378 -- Add System.Standard_Library to list to ensure that these files are
379 -- included in the bind, even if not directly referenced from Ada code
380 -- This is of course omitted in No_Run_Time mode
382 if not No_Run_Time_Specified then
383 Name_Buffer (1 .. 12) := "s-stalib.ali";
385 Std_Lib_File := Name_Find;
386 Text := Read_Library_Info (Std_Lib_File, True);
391 Ignore_ED => Force_RM_Elaboration_Order,
396 -- Acquire all information in ALI files that have been read in
398 for Index in ALIs.First .. ALIs.Last loop
402 -- Warn if -f switch used
404 if Force_RM_Elaboration_Order then
406 ("?-f is obsolescent and should not be used");
408 ("?may result in missing run-time elaboration checks");
410 ("?use -gnatE, pragma Suppress (Elaboration_Checks) instead");
413 -- Quit if some file needs compiling
415 if No_Object_Specified then
416 raise Unrecoverable_Error;
419 -- Build source file table from the ALI files we have read in
423 -- Check that main library file is a suitable main program
426 and then ALIs.Table (ALIs.First).Main_Program = None
427 and then not No_Main_Subprogram
429 Error_Msg_Name_1 := Main_Lib_File;
430 Error_Msg ("% does not contain a unit that can be a main program");
433 -- Perform consistency and correctness checks
435 Check_Duplicated_Subunits;
438 Check_Configuration_Consistency;
440 -- Complete bind if no errors
442 if Errors_Detected = 0 then
445 if Errors_Detected = 0 then
446 if Elab_Order_Output then
448 Write_Str ("ELABORATION ORDER");
451 for J in Elab_Order.First .. Elab_Order.Last loop
453 Write_Unit_Name (Units.Table (Elab_Order.Table (J)).Uname);
460 if not Check_Only then
461 Gen_Output_File (Output_File_Name.all);
466 Total_Errors := Total_Errors + Errors_Detected;
467 Total_Warnings := Total_Warnings + Warnings_Detected;
470 when Unrecoverable_Error =>
471 Total_Errors := Total_Errors + Errors_Detected;
472 Total_Warnings := Total_Warnings + Warnings_Detected;
475 -- All done. Set proper exit status.
480 if Total_Errors > 0 then
481 Exit_Program (E_Errors);
482 elsif Total_Warnings > 0 then
483 Exit_Program (E_Warnings);
485 Exit_Program (E_Success);