1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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
;
35 with Casing
; use Casing
;
37 with Debug
; use Debug
;
39 with Gnatvsn
; use Gnatvsn
;
40 with Namet
; use Namet
;
42 with Osint
; use Osint
;
43 with Osint
.B
; use Osint
.B
;
44 with Output
; use Output
;
45 with Rident
; use Rident
;
46 with Switch
; use Switch
;
47 with Switch
.B
; use Switch
.B
;
48 with Targparm
; use Targparm
;
49 with Types
; use Types
;
51 with System
.Case_Util
; use System
.Case_Util
;
55 Total_Errors
: Nat
:= 0;
56 -- Counts total errors in all files
58 Total_Warnings
: Nat
:= 0;
59 -- Total warnings in all files
61 Main_Lib_File
: File_Name_Type
;
62 -- Current main library file
64 Std_Lib_File
: File_Name_Type
;
67 Text
: Text_Buffer_Ptr
;
70 Output_File_Name_Seen
: Boolean := False;
71 Output_File_Name
: String_Ptr
:= new String'("");
73 L_Switch_Seen : Boolean := False;
75 Mapping_File : String_Ptr := null;
77 procedure List_Applicable_Restrictions;
78 -- List restrictions that apply to this partition if option taken
80 procedure Scan_Bind_Arg (Argv : String);
81 -- Scan and process binder specific arguments. Argv is a single argument.
82 -- All the one character arguments are still handled by Switch. This
83 -- routine handles -aO -aI and -I-.
85 ----------------------------------
86 -- List_Applicable_Restrictions --
87 ----------------------------------
89 procedure List_Applicable_Restrictions is
91 -- Define those restrictions that should be output if the gnatbind
92 -- -r switch is used. Not all restrictions are output for the reasons
93 -- given above in the list, and this array is used to test whether
94 -- the corresponding pragma should be listed. True means that it
95 -- should not be listed.
97 No_Restriction_List : constant array (All_Restrictions) of Boolean :=
98 (No_Exceptions => True,
99 -- Has unexpected Suppress (All_Checks) effect
101 No_Implicit_Conditionals => True,
102 -- This could modify and pessimize generated code
104 No_Implicit_Dynamic_Code => True,
105 -- This could modify and pessimize generated code
107 No_Implicit_Loops => True,
108 -- This could modify and pessimize generated code
110 No_Recursion => True,
111 -- Not checkable at compile time
113 No_Reentrancy => True,
114 -- Not checkable at compile time
116 Max_Entry_Queue_Length => True,
117 -- Not checkable at compile time
119 Max_Storage_At_Blocking => True,
120 -- Not checkable at compile time
124 Additional_Restrictions_Listed : Boolean := False;
125 -- Set True if we have listed header for restrictions
128 -- Loop through restrictions
130 for R in All_Restrictions loop
131 if not No_Restriction_List (R) then
133 -- We list a restriction if it is not violated, or if
134 -- it is violated but the violation count is exactly known.
136 if Cumulative_Restrictions.Violated (R) = False
137 or else (R in All_Parameter_Restrictions
139 Cumulative_Restrictions.Unknown (R) = False)
141 if not Additional_Restrictions_Listed then
144 ("The following additional restrictions may be" &
145 " applied to this partition:");
146 Additional_Restrictions_Listed := True;
149 Write_Str ("pragma Restrictions (");
152 S : constant String := Restriction_Id'Image (R);
154 Name_Len := S'Length;
155 Name_Buffer (1 .. Name_Len) := S;
158 Set_Casing (Mixed_Case);
159 Write_Str (Name_Buffer (1 .. Name_Len));
161 if R in All_Parameter_Restrictions then
163 Write_Int (Int (Cumulative_Restrictions.Count (R)));
171 end List_Applicable_Restrictions;
177 procedure Scan_Bind_Arg (Argv : String) is
179 -- Now scan arguments that are specific to the binder and are not
180 -- handled by the common circuitry in Switch.
182 if Opt.Output_File_Name_Present
183 and then not Output_File_Name_Seen
185 Output_File_Name_Seen := True;
188 or else (Argv'Length >= 1 and then Argv (1) = '-')
190 Fail ("output File_Name missing after -o");
193 Output_File_Name := new String'(Argv
);
196 elsif Argv
'Length >= 2 and then Argv
(1) = '-' then
200 if Argv
(2 .. Argv
'Last) = "I-" then
201 Opt
.Look_In_Primary_Dir
:= False;
205 elsif Argv
(2) = 'I' then
206 Add_Src_Search_Dir
(Argv
(3 .. Argv
'Last));
207 Add_Lib_Search_Dir
(Argv
(3 .. Argv
'Last));
211 elsif Argv
(2) = 'L' then
212 if Argv
'Length >= 3 then
214 -- Remember that the -L switch was specified, so that if this
215 -- is on OpenVMS, the export names are put in uppercase.
216 -- This is not known before the target parameters are read.
218 L_Switch_Seen
:= True;
220 Opt
.Bind_For_Library
:= True;
222 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
223 Opt.Ada_Final_Name :=
224 new String'(Argv
(3 .. Argv
'Last) & Opt
.Ada_Final_Suffix
);
226 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
228 -- This option (-Lxxx) implies -n
230 Opt.Bind_Main_Program := False;
234 ("Prefix of initialization and finalization " &
235 "procedure names missing in -L");
238 -- -Sin -Slo -Shi -Sxx
240 elsif Argv'Length = 4
241 and then Argv (2) = 'S
'
244 C1 : Character := Argv (3);
245 C2 : Character := Argv (4);
248 -- Fold to upper case
250 if C1 in 'a
' .. 'z
' then
251 C1 := Character'Val (Character'Pos (C1) - 32);
254 if C2 in 'a
' .. 'z
' then
255 C2 := Character'Val (Character'Pos (C2) - 32);
258 -- Test valid option and set mode accordingly
260 if C1 = 'E
' and then C2 = 'V
' then
263 elsif C1 = 'I
' and then C2 = 'N
' then
266 elsif C1 = 'L
' and then C2 = 'O
' then
269 elsif C1 = 'H
' and then C2 = 'I
' then
272 elsif (C1 in '0' .. '9' or else C1 in 'A
' .. 'F
')
274 (C2 in '0' .. '9' or else C2 in 'A
' .. 'F
')
278 -- Invalid -S switch, let Switch give error, set defalut of IN
281 Scan_Binder_Switches (Argv);
286 Initialize_Scalars_Mode1 := C1;
287 Initialize_Scalars_Mode2 := C2;
292 elsif Argv'Length >= 3
293 and then Argv (2 .. 3) = "aI"
295 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
299 elsif Argv'Length >= 3
300 and then Argv (2 .. 3) = "aO"
302 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
306 elsif Argv (2 .. Argv'Last) = "nostdlib" then
307 Opt.No_Stdlib := True;
311 elsif Argv (2 .. Argv'Last) = "nostdinc" then
312 Opt.No_Stdinc := True;
316 elsif Argv (2 .. Argv'Last) = "static" then
317 Opt.Shared_Libgnat := False;
321 elsif Argv (2 .. Argv'Last) = "shared" then
322 Opt.Shared_Libgnat := True;
326 elsif Argv'Length >= 4 and then Argv (2 .. 3) = "F=" then
327 if Mapping_File /= null then
328 Fail ("cannot specify several mapping files");
331 Mapping_File := new String'(Argv
(4 .. Argv
'Last));
335 elsif Argv
'Length >= 3 and then Argv
(2) = 'M' then
336 Opt
.Bind_Alternate_Main_Name
:= True;
337 Opt
.Alternate_Main_Name
:= new String'(Argv (3 .. Argv'Last));
339 -- All other options are single character and are handled
340 -- by Scan_Binder_Switches.
343 Scan_Binder_Switches (Argv);
346 -- Not a switch, so must be a file name (if non-empty)
348 elsif Argv'Length /= 0 then
350 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
354 Add_File (Argv & ".ali");
359 -- Start of processing for Gnatbind
363 -- Set default for Shared_Libgnat option
366 Shared_Libgnat_Default : Character;
368 (C, Shared_Libgnat_Default, "__gnat_shared_libgnat_default");
370 SHARED : constant Character := 'H
';
371 STATIC : constant Character := 'T
';
375 (Shared_Libgnat_Default = SHARED
377 Shared_Libgnat_Default = STATIC);
378 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
381 -- Use low level argument routines to avoid dragging in the secondary stack
384 Scan_Args : while Next_Arg < Arg_Count loop
386 Next_Argv : String (1 .. Len_Arg (Next_Arg));
389 Fill_Arg (Next_Argv'Address, Next_Arg);
390 Scan_Bind_Arg (Next_Argv);
392 Next_Arg := Next_Arg + 1;
395 -- Test for trailing -o switch
397 if Opt.Output_File_Name_Present
398 and then not Output_File_Name_Seen
400 Fail ("output file name missing after -o");
403 -- Output usage if requested
405 if Usage_Requested then
409 -- Check that the Ada binder file specified has extension .adb and that
410 -- the C binder file has extension .c
412 if Opt.Output_File_Name_Present
413 and then Output_File_Name_Seen
415 Check_Extensions : declare
416 Length : constant Natural := Output_File_Name'Length;
417 Last : constant Natural := Output_File_Name'Last;
420 if Ada_Bind_File then
422 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
424 Fail ("output file name should have .adb extension");
429 or else Output_File_Name (Last - 1 .. Last) /= ".c"
431 Fail ("output file name should have .c extension");
434 end Check_Extensions;
437 Osint.Add_Default_Search_Dirs;
439 -- Carry out package initializations. These are initializations which
440 -- might logically be performed at elaboration time, but Namet at
441 -- least can't be done that way (because it is used in the Compiler),
442 -- and we decide to be consistent. Like elaboration, the order in
443 -- which these calls are made is in some cases important.
448 -- Acquire target parameters
450 Targparm.Get_Target_Parameters;
452 -- Initialize Cumulative_Restrictions with the restrictions on the target
453 -- scanned from the system.ads file. Then as we read ALI files, we will
454 -- accumulate additional restrictions specified in other files.
456 Cumulative_Restrictions := Targparm.Restrictions_On_Target;
458 -- On OpenVMS, when -L is used, all external names used in pragmas Export
459 -- are in upper case. The reason is that on OpenVMS, the macro-assembler
460 -- MACASM-32, used to build Stand-Alone Libraries, only understands
463 if L_Switch_Seen and then OpenVMS_On_Target then
464 To_Upper (Opt.Ada_Init_Name.all);
465 To_Upper (Opt.Ada_Final_Name.all);
466 To_Upper (Opt.Ada_Main_Name.all);
469 -- Acquire configurable run-time mode
471 if Configurable_Run_Time_On_Target then
472 Configurable_Run_Time_Mode := True;
475 -- Output copyright notice if in verbose mode
479 Write_Str ("GNATBIND ");
480 Write_Str (Gnat_Version_String);
481 Write_Str (" Copyright 1995-2004 Free Software Foundation, Inc.");
485 -- Output usage information if no files
487 if not More_Lib_Files then
489 Exit_Program (E_Fatal);
492 -- If a mapping file was specified, initialize the file mapping
494 if Mapping_File /= null then
495 Fmap.Initialize (Mapping_File.all);
498 -- The block here is to catch the Unrecoverable_Error exception in the
499 -- case where we exceed the maximum number of permissible errors or some
500 -- other unrecoverable error occurs.
503 -- Initialize binder packages
507 Initialize_ALI_Source;
515 while More_Lib_Files loop
516 Main_Lib_File := Next_Main_Lib_File;
520 Write_Str ("Checking: ");
522 Write_Str ("Binding: ");
525 Write_Name (Main_Lib_File);
529 Text := Read_Library_Info (Main_Lib_File, True);
533 pragma Warnings (Off, Id);
539 Ignore_ED => Force_RM_Elaboration_Order,
541 Ignore_Errors => Debug_Flag_I);
549 if No_Run_Time_Mode then
551 -- Set standard configuration parameters
553 Suppress_Standard_Library_On_Target := True;
554 Configurable_Run_Time_Mode := True;
557 -- For main ALI files, even if they are interfaces, we get their
558 -- dependencies. To be sure, we reset the Interface flag for all main
561 for Index in ALIs.First .. ALIs.Last loop
562 ALIs.Table (Index).Interface := False;
565 -- Add System.Standard_Library to list to ensure that these files are
566 -- included in the bind, even if not directly referenced from Ada code
567 -- This is suppressed if the appropriate targparm switch is set.
569 if not Suppress_Standard_Library_On_Target then
570 Name_Buffer (1 .. 12) := "s-stalib.ali";
572 Std_Lib_File := Name_Find;
573 Text := Read_Library_Info (Std_Lib_File, True);
577 pragma Warnings (Off, Id);
584 Ignore_ED => Force_RM_Elaboration_Order,
586 Ignore_Errors => Debug_Flag_I);
592 -- Acquire all information in ALI files that have been read in
594 for Index in ALIs.First .. ALIs.Last loop
598 -- Warn if -f switch used
600 if Force_RM_Elaboration_Order then
602 ("?-f is obsolescent and should not be used");
604 ("?may result in missing run-time elaboration checks");
606 ("?use -gnatE, pragma Suppress (Elaboration_Checks) instead");
609 -- Quit if some file needs compiling
611 if No_Object_Specified then
612 raise Unrecoverable_Error;
615 -- Build source file table from the ALI files we have read in
619 -- Check that main library file is a suitable main program
622 and then ALIs.Table (ALIs.First).Main_Program = None
623 and then not No_Main_Subprogram
625 Error_Msg_Name_1 := Main_Lib_File;
626 Error_Msg ("% does not contain a unit that can be a main program");
629 -- Perform consistency and correctness checks
631 Check_Duplicated_Subunits;
634 Check_Configuration_Consistency;
636 -- List restrictions that could be applied to this partition
638 if List_Restrictions then
639 List_Applicable_Restrictions;
642 -- Complete bind if no errors
644 if Errors_Detected = 0 then
647 if Errors_Detected = 0 then
648 if Elab_Order_Output then
650 Write_Str ("ELABORATION ORDER");
653 for J in Elab_Order.First .. Elab_Order.Last loop
654 if not Units.Table (Elab_Order.Table (J)).Interface then
657 (Units.Table (Elab_Order.Table (J)).Uname);
665 if not Check_Only then
666 Gen_Output_File (Output_File_Name.all);
671 Total_Errors := Total_Errors + Errors_Detected;
672 Total_Warnings := Total_Warnings + Warnings_Detected;
675 when Unrecoverable_Error =>
676 Total_Errors := Total_Errors + Errors_Detected;
677 Total_Warnings := Total_Warnings + Warnings_Detected;
680 -- All done. Set proper exit status.
685 if Total_Errors > 0 then
686 Exit_Program (E_Errors);
687 elsif Total_Warnings > 0 then
688 Exit_Program (E_Warnings);
690 Exit_Program (E_Success);