1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with ALI.Util; use ALI.Util;
29 with Bcheck; use Bcheck;
30 with Binde; use Binde;
31 with Binderr; use Binderr;
32 with Bindgen; use Bindgen;
34 with Butil; use Butil;
37 with Gnatvsn; use Gnatvsn;
38 with Namet; use Namet;
40 with Osint; use Osint;
41 with Osint.B; use Osint.B;
42 with Output; use Output;
43 with Rident; use Rident;
44 with Switch; use Switch;
45 with Switch.B; use Switch.B;
46 with Targparm; use Targparm;
47 with Types; use Types;
48 with Uintp; use Uintp;
50 with System.Case_Util; use System.Case_Util;
54 Total_Errors : Nat := 0;
55 -- Counts total errors in all files
57 Total_Warnings : Nat := 0;
58 -- Total warnings in all files
60 Main_Lib_File : File_Name_Type;
61 -- Current main library file
63 Std_Lib_File : File_Name_Type;
66 Text : Text_Buffer_Ptr;
69 Output_File_Name_Seen : Boolean := False;
70 Output_File_Name : String_Ptr := new String'("");
72 L_Switch_Seen : Boolean := False;
74 Mapping_File : String_Ptr := null;
76 procedure Scan_Bind_Arg (Argv : String);
77 -- Scan and process binder specific arguments. Argv is a single argument.
78 -- All the one character arguments are still handled by Switch. This
79 -- routine handles -aO -aI and -I-.
85 procedure Scan_Bind_Arg (Argv : String) is
87 -- Now scan arguments that are specific to the binder and are not
88 -- handled by the common circuitry in Switch.
90 if Opt.Output_File_Name_Present
91 and then not Output_File_Name_Seen
93 Output_File_Name_Seen := True;
96 or else (Argv'Length >= 1 and then Argv (1) = '-')
98 Fail ("output File_Name missing after -o");
101 Output_File_Name := new String'(Argv);
104 elsif Argv'Length >= 2 and then Argv (1) = '-' then
108 if Argv (2 .. Argv'Last) = "I-" then
109 Opt.Look_In_Primary_Dir := False;
113 elsif Argv (2) = 'I' then
114 Add_Src_Search_Dir (Argv (3 .. Argv'Last));
115 Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
119 elsif Argv (2) = 'L' then
120 if Argv'Length >= 3 then
122 -- Remember that the -L switch was specified, so that if this
123 -- is on OpenVMS, the export names are put in uppercase.
124 -- This is not known before the target parameters are read.
126 L_Switch_Seen := True;
128 Opt.Bind_For_Library := True;
130 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
131 Opt.Ada_Final_Name :=
132 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Final_Suffix);
134 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
136 -- This option (-Lxxx) implies -n
138 Opt.Bind_Main_Program := False;
142 ("Prefix of initialization and finalization " &
143 "procedure names missing in -L");
146 -- -Sin -Slo -Shi -Sxx
148 elsif Argv'Length = 4
149 and then Argv (2) = 'S'
152 C1 : Character := Argv (3);
153 C2 : Character := Argv (4);
156 -- Fold to upper case
158 if C1 in 'a' .. 'z' then
159 C1 := Character'Val (Character'Pos (C1) - 32);
162 if C2 in 'a' .. 'z' then
163 C2 := Character'Val (Character'Pos (C2) - 32);
166 -- Test valid option and set mode accordingly
168 if C1 = 'E' and then C2 = 'V' then
171 elsif C1 = 'I' and then C2 = 'N' then
174 elsif C1 = 'L' and then C2 = 'O' then
177 elsif C1 = 'H' and then C2 = 'I' then
180 elsif (C1 in '0' .. '9' or else C1 in 'A' .. 'F')
182 (C2 in '0' .. '9' or else C2 in 'A' .. 'F')
186 -- Invalid -S switch, let Switch give error, set defalut of IN
189 Scan_Binder_Switches (Argv);
194 Initialize_Scalars_Mode1 := C1;
195 Initialize_Scalars_Mode2 := C2;
200 elsif Argv'Length >= 3
201 and then Argv (2 .. 3) = "aI"
203 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
207 elsif Argv'Length >= 3
208 and then Argv (2 .. 3) = "aO"
210 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
214 elsif Argv (2 .. Argv'Last) = "nostdlib" then
215 Opt.No_Stdlib := True;
219 elsif Argv (2 .. Argv'Last) = "nostdinc" then
220 Opt.No_Stdinc := True;
224 elsif Argv (2 .. Argv'Last) = "static" then
225 Opt.Shared_Libgnat := False;
229 elsif Argv (2 .. Argv'Last) = "shared" then
230 Opt.Shared_Libgnat := True;
234 elsif Argv'Length >= 4 and then Argv (2 .. 3) = "F=" then
235 if Mapping_File /= null then
236 Fail ("cannot specify several mapping files");
239 Mapping_File := new String'(Argv (4 .. Argv'Last));
243 elsif Argv'Length >= 3 and then Argv (2) = 'M' then
244 Opt.Bind_Alternate_Main_Name := True;
245 Opt.Alternate_Main_Name := new String'(Argv (3 .. Argv'Last));
247 -- All other options are single character and are handled
248 -- by Scan_Binder_Switches.
251 Scan_Binder_Switches (Argv);
254 -- Not a switch, so must be a file name (if non-empty)
256 elsif Argv'Length /= 0 then
258 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
262 Add_File (Argv & ".ali");
267 -- Start of processing for Gnatbind
271 -- Set default for Shared_Libgnat option
274 Shared_Libgnat_Default : Character;
275 pragma Import (C, Shared_Libgnat_Default, "shared_libgnat_default");
277 SHARED : constant Character := 'H';
278 STATIC : constant Character := 'T';
282 (Shared_Libgnat_Default = SHARED
284 Shared_Libgnat_Default = STATIC);
285 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
288 -- Use low level argument routines to avoid dragging in the secondary stack
291 Scan_Args : while Next_Arg < Arg_Count loop
293 Next_Argv : String (1 .. Len_Arg (Next_Arg));
296 Fill_Arg (Next_Argv'Address, Next_Arg);
297 Scan_Bind_Arg (Next_Argv);
299 Next_Arg := Next_Arg + 1;
302 -- Test for trailing -o switch
304 if Opt.Output_File_Name_Present
305 and then not Output_File_Name_Seen
307 Fail ("output file name missing after -o");
310 -- Output usage if requested
312 if Usage_Requested then
316 -- Check that the Ada binder file specified has extension .adb and that
317 -- the C binder file has extension .c
319 if Opt.Output_File_Name_Present
320 and then Output_File_Name_Seen
322 Check_Extensions : declare
323 Length : constant Natural := Output_File_Name'Length;
324 Last : constant Natural := Output_File_Name'Last;
327 if Ada_Bind_File then
329 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
331 Fail ("output file name should have .adb extension");
336 or else Output_File_Name (Last - 1 .. Last) /= ".c"
338 Fail ("output file name should have .c extension");
341 end Check_Extensions;
344 Osint.Add_Default_Search_Dirs;
346 -- Carry out package initializations. These are initializations which
347 -- might logically be performed at elaboration time, but Namet at
348 -- least can't be done that way (because it is used in the Compiler),
349 -- and we decide to be consistent. Like elaboration, the order in
350 -- which these calls are made is in some cases important.
355 -- Acquire target parameters
357 Targparm.Get_Target_Parameters;
359 -- On OpenVMS, when -L is used, all external names used in pragmas Export
360 -- are in upper case. The reason is that on OpenVMS, the macro-assembler
361 -- MACASM-32, used to build Stand-Alone Libraries, only understands
364 if L_Switch_Seen and then OpenVMS_On_Target then
365 To_Upper (Opt.Ada_Init_Name.all);
366 To_Upper (Opt.Ada_Final_Name.all);
367 To_Upper (Opt.Ada_Main_Name.all);
370 -- Acquire configurable run-time mode
372 if Configurable_Run_Time_On_Target then
373 Configurable_Run_Time_Mode := True;
376 -- Output copyright notice if in verbose mode
380 Write_Str ("GNATBIND ");
381 Write_Str (Gnat_Version_String);
382 Write_Str (" Copyright 1995-2003 Free Software Foundation, Inc.");
386 -- Output usage information if no files
388 if not More_Lib_Files then
390 Exit_Program (E_Fatal);
393 -- If a mapping file was specified, initialize the file mapping
395 if Mapping_File /= null then
396 Fmap.Initialize (Mapping_File.all);
399 -- The block here is to catch the Unrecoverable_Error exception in the
400 -- case where we exceed the maximum number of permissible errors or some
401 -- other unrecoverable error occurs.
404 -- Initialize binder packages
408 Initialize_ALI_Source;
416 while More_Lib_Files loop
417 Main_Lib_File := Next_Main_Lib_File;
421 Write_Str ("Checking: ");
423 Write_Str ("Binding: ");
426 Write_Name (Main_Lib_File);
430 Text := Read_Library_Info (Main_Lib_File, True);
434 pragma Warnings (Off, Id);
440 Ignore_ED => Force_RM_Elaboration_Order,
449 if No_Run_Time_Mode then
451 -- Set standard restrictions
453 Restrictions_On_Target (No_Finalization) := True;
454 Restrictions_On_Target (No_Exception_Handlers) := True;
455 Restrictions_On_Target (No_Tasking) := True;
456 Restriction_Parameters_On_Target (Max_Tasks) := Uint_0;
458 -- Set standard configuration parameters
460 Suppress_Standard_Library_On_Target := True;
461 Configurable_Run_Time_Mode := True;
464 -- For main ALI files, even if they are interfaces, we get their
465 -- dependencies. To be sure, we reset the Interface flag for all main
468 for Index in ALIs.First .. ALIs.Last loop
469 ALIs.Table (Index).Interface := False;
472 -- Add System.Standard_Library to list to ensure that these files are
473 -- included in the bind, even if not directly referenced from Ada code
474 -- This is suppressed if the configurable run-time requests it.
476 if not Suppress_Standard_Library_On_Target then
477 Name_Buffer (1 .. 12) := "s-stalib.ali";
479 Std_Lib_File := Name_Find;
480 Text := Read_Library_Info (Std_Lib_File, True);
484 pragma Warnings (Off, Id);
491 Ignore_ED => Force_RM_Elaboration_Order,
498 -- Acquire all information in ALI files that have been read in
500 for Index in ALIs.First .. ALIs.Last loop
504 -- Warn if -f switch used
506 if Force_RM_Elaboration_Order then
508 ("?-f is obsolescent and should not be used");
510 ("?may result in missing run-time elaboration checks");
512 ("?use -gnatE, pragma Suppress (Elaboration_Checks) instead");
515 -- Quit if some file needs compiling
517 if No_Object_Specified then
518 raise Unrecoverable_Error;
521 -- Build source file table from the ALI files we have read in
525 -- Check that main library file is a suitable main program
528 and then ALIs.Table (ALIs.First).Main_Program = None
529 and then not No_Main_Subprogram
531 Error_Msg_Name_1 := Main_Lib_File;
532 Error_Msg ("% does not contain a unit that can be a main program");
535 -- Perform consistency and correctness checks
537 Check_Duplicated_Subunits;
540 Check_Configuration_Consistency;
542 -- Acquire restrictions and add them to target restrictions. After
543 -- this loop, Restrictions_On_Target entries will be set True for
544 -- all partition-wide restrictions specified in the partition.
546 for J in Partition_Restrictions loop
547 if Restrictions (J) = 'r' then
548 Restrictions_On_Target (J) := True;
552 -- Complete bind if no errors
554 if Errors_Detected = 0 then
557 if Errors_Detected = 0 then
558 if Elab_Order_Output then
560 Write_Str ("ELABORATION ORDER");
563 for J in Elab_Order.First .. Elab_Order.Last loop
564 if not Units.Table (Elab_Order.Table (J)).Interface then
567 (Units.Table (Elab_Order.Table (J)).Uname);
575 if not Check_Only then
576 Gen_Output_File (Output_File_Name.all);
581 Total_Errors := Total_Errors + Errors_Detected;
582 Total_Warnings := Total_Warnings + Warnings_Detected;
585 when Unrecoverable_Error =>
586 Total_Errors := Total_Errors + Errors_Detected;
587 Total_Warnings := Total_Warnings + Warnings_Detected;
590 -- All done. Set proper exit status.
595 if Total_Errors > 0 then
596 Exit_Program (E_Errors);
597 elsif Total_Warnings > 0 then
598 Exit_Program (E_Warnings);
600 Exit_Program (E_Success);