1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with ALI
.Util
; use ALI
.Util
;
28 with Bcheck
; use Bcheck
;
29 with Binde
; use Binde
;
30 with Binderr
; use Binderr
;
31 with Bindgen
; use Bindgen
;
33 with Butil
; use Butil
;
34 with Casing
; use Casing
;
36 with Debug
; use Debug
;
38 with Fname
; use Fname
;
39 with Namet
; use Namet
;
41 with Osint
; use Osint
;
42 with Osint
.B
; use Osint
.B
;
43 with Output
; use Output
;
44 with Rident
; use Rident
;
46 with Switch
; use Switch
;
47 with Switch
.B
; use Switch
.B
;
49 with Targparm
; use Targparm
;
50 with Types
; use Types
;
52 with System
.Case_Util
; use System
.Case_Util
;
53 with System
.OS_Lib
; use System
.OS_Lib
;
55 with Ada
.Command_Line
.Response_File
; use Ada
.Command_Line
;
59 Total_Errors
: Nat
:= 0;
60 -- Counts total errors in all files
62 Total_Warnings
: Nat
:= 0;
63 -- Total warnings in all files
65 Main_Lib_File
: File_Name_Type
;
66 -- Current main library file
68 First_Main_Lib_File
: File_Name_Type
:= No_File
;
69 -- The first library file, that should be a main subprogram if neither -n
72 Std_Lib_File
: File_Name_Type
;
75 Text
: Text_Buffer_Ptr
;
77 Output_File_Name_Seen
: Boolean := False;
78 Output_File_Name
: String_Ptr
:= new String'("");
80 Mapping_File : String_Ptr := null;
82 package Closure_Sources is new Table.Table
83 (Table_Component_Type => File_Name_Type,
84 Table_Index_Type => Natural,
87 Table_Increment => 100,
88 Table_Name => "Gnatbind.Closure_Sources");
89 -- Table to record the sources in the closure, to avoid duplications. Used
90 -- only with switch -R.
92 function Gnatbind_Supports_Auto_Init return Boolean;
93 -- Indicates if automatic initialization of elaboration procedure
94 -- through the constructor mechanism is possible on the platform.
96 procedure List_Applicable_Restrictions;
97 -- List restrictions that apply to this partition if option taken
99 procedure Scan_Bind_Arg (Argv : String);
100 -- Scan and process binder specific arguments. Argv is a single argument.
101 -- All the one character arguments are still handled by Switch. This
102 -- routine handles -aO -aI and -I-. The lower bound of Argv must be 1.
105 with procedure Action (Argv : String);
106 procedure Generic_Scan_Bind_Args;
107 -- Iterate through the args calling Action on each one, taking care of
110 procedure Write_Arg (S : String);
111 -- Passed to Generic_Scan_Bind_Args to print args
113 function Is_Cross_Compiler return Boolean;
114 -- Returns True iff this is a cross-compiler
116 ---------------------------------
117 -- Gnatbind_Supports_Auto_Init --
118 ---------------------------------
120 function Gnatbind_Supports_Auto_Init return Boolean is
121 function gnat_binder_supports_auto_init return Integer;
122 pragma Import (C, gnat_binder_supports_auto_init,
123 "__gnat_binder_supports_auto_init");
125 return gnat_binder_supports_auto_init /= 0;
126 end Gnatbind_Supports_Auto_Init;
128 -----------------------
129 -- Is_Cross_Compiler --
130 -----------------------
132 function Is_Cross_Compiler return Boolean is
133 Cross_Compiler : Integer;
134 pragma Import (C, Cross_Compiler, "__gnat_is_cross_compiler");
136 return Cross_Compiler = 1;
137 end Is_Cross_Compiler;
139 ----------------------------------
140 -- List_Applicable_Restrictions --
141 ----------------------------------
143 procedure List_Applicable_Restrictions is
145 -- Define those restrictions that should be output if the gnatbind
146 -- -r switch is used. Not all restrictions are output for the reasons
147 -- given below in the list, and this array is used to test whether
148 -- the corresponding pragma should be listed. True means that it
149 -- should not be listed.
151 No_Restriction_List : constant array (All_Restrictions) of Boolean :=
152 (No_Standard_Allocators_After_Elaboration => True,
153 -- This involves run-time conditions not checkable at compile time
155 No_Anonymous_Allocators => True,
156 -- Premature, since we have not implemented this yet
158 No_Exception_Propagation => True,
159 -- Modifies code resulting in different exception semantics
161 No_Exceptions => True,
162 -- Has unexpected Suppress (All_Checks) effect
164 No_Implicit_Conditionals => True,
165 -- This could modify and pessimize generated code
167 No_Implicit_Dynamic_Code => True,
168 -- This could modify and pessimize generated code
170 No_Implicit_Loops => True,
171 -- This could modify and pessimize generated code
173 No_Recursion => True,
174 -- Not checkable at compile time
176 No_Reentrancy => True,
177 -- Not checkable at compile time
179 Max_Entry_Queue_Length => True,
180 -- Not checkable at compile time
182 Max_Storage_At_Blocking => True,
183 -- Not checkable at compile time
185 -- The following three should not be partition-wide, so the
186 -- following tests are junk to be removed eventually ???
188 No_Specification_Of_Aspect => True,
189 -- Requires a parameter value, not a count
191 No_Use_Of_Attribute => True,
192 -- Requires a parameter value, not a count
194 No_Use_Of_Pragma => True,
195 -- Requires a parameter value, not a count
199 Additional_Restrictions_Listed : Boolean := False;
200 -- Set True if we have listed header for restrictions
202 function Restriction_Could_Be_Set (R : Restriction_Id) return Boolean;
203 -- Returns True if the given restriction can be listed as an additional
204 -- restriction that could be set.
206 ------------------------------
207 -- Restriction_Could_Be_Set --
208 ------------------------------
210 function Restriction_Could_Be_Set (R : Restriction_Id) return Boolean is
211 CR : Restrictions_Info renames Cumulative_Restrictions;
216 -- Boolean restriction
218 when All_Boolean_Restrictions =>
220 -- The condition for listing a boolean restriction as an
221 -- additional restriction that could be set is that it is
222 -- not violated by any unit, and not already set.
224 return CR.Violated (R) = False and then CR.Set (R) = False;
226 -- Parameter restriction
228 when All_Parameter_Restrictions =>
230 -- If the restriction is violated and the level of violation is
231 -- unknown, the restriction can definitely not be listed.
233 if CR.Violated (R) and then CR.Unknown (R) then
236 -- We can list the restriction if it is not set
238 elsif not CR.Set (R) then
241 -- We can list the restriction if is set to a greater value
242 -- than the maximum value known for the violation.
245 return CR.Value (R) > CR.Count (R);
248 -- No other values for R possible
254 end Restriction_Could_Be_Set;
256 -- Start of processing for List_Applicable_Restrictions
259 -- Loop through restrictions
261 for R in All_Restrictions loop
262 if not No_Restriction_List (R)
263 and then Restriction_Could_Be_Set (R)
265 if not Additional_Restrictions_Listed then
268 ("The following additional restrictions may be" &
269 " applied to this partition:");
270 Additional_Restrictions_Listed := True;
273 Write_Str ("pragma Restrictions (");
276 S : constant String := Restriction_Id'Image (R);
278 Name_Len := S'Length;
279 Name_Buffer (1 .. Name_Len) := S;
282 Set_Casing (Mixed_Case);
283 Write_Str (Name_Buffer (1 .. Name_Len));
285 if R in All_Parameter_Restrictions then
287 Write_Int (Int (Cumulative_Restrictions.Count (R)));
294 end List_Applicable_Restrictions;
300 procedure Scan_Bind_Arg (Argv : String) is
301 pragma Assert (Argv'First = 1);
304 -- Now scan arguments that are specific to the binder and are not
305 -- handled by the common circuitry in Switch.
307 if Opt.Output_File_Name_Present
308 and then not Output_File_Name_Seen
310 Output_File_Name_Seen := True;
313 or else (Argv'Length >= 1 and then Argv (1) = '-')
315 Fail ("output File_Name missing after -o");
318 Output_File_Name := new String'(Argv
);
321 elsif Argv
'Length >= 2 and then Argv
(1) = '-' then
325 if Argv
(2 .. Argv
'Last) = "I-" then
326 Opt
.Look_In_Primary_Dir
:= False;
330 elsif Argv
(2) = 'I' then
331 Add_Src_Search_Dir
(Argv
(3 .. Argv
'Last));
332 Add_Lib_Search_Dir
(Argv
(3 .. Argv
'Last));
336 elsif Argv
(2) = 'L' then
337 if Argv
'Length >= 3 then
339 Opt
.Bind_For_Library
:= True;
341 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
342 Opt.Ada_Final_Name :=
343 new String'(Argv
(3 .. Argv
'Last) & Opt
.Ada_Final_Suffix
);
345 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
347 -- This option (-Lxxx) implies -n
349 Opt.Bind_Main_Program := False;
353 ("Prefix of initialization and finalization " &
354 "procedure names missing in -L");
357 -- -Sin -Slo -Shi -Sxx -Sev
359 elsif Argv'Length = 4
360 and then Argv (2) = 'S
'
363 C1 : Character := Argv (3);
364 C2 : Character := Argv (4);
367 -- Fold to upper case
369 if C1 in 'a
' .. 'z
' then
370 C1 := Character'Val (Character'Pos (C1) - 32);
373 if C2 in 'a
' .. 'z
' then
374 C2 := Character'Val (Character'Pos (C2) - 32);
377 -- Test valid option and set mode accordingly
379 if C1 = 'E
' and then C2 = 'V
' then
382 elsif C1 = 'I
' and then C2 = 'N
' then
385 elsif C1 = 'L
' and then C2 = 'O
' then
388 elsif C1 = 'H
' and then C2 = 'I
' then
391 elsif (C1 in '0' .. '9' or else C1 in 'A
' .. 'F
')
393 (C2 in '0' .. '9' or else C2 in 'A
' .. 'F
')
397 -- Invalid -S switch, let Switch give error, set default of IN
400 Scan_Binder_Switches (Argv);
405 Initialize_Scalars_Mode1 := C1;
406 Initialize_Scalars_Mode2 := C2;
411 elsif Argv'Length >= 3
412 and then Argv (2 .. 3) = "aI"
414 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
418 elsif Argv'Length >= 3
419 and then Argv (2 .. 3) = "aO"
421 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
425 elsif Argv (2 .. Argv'Last) = "nostdlib" then
426 Opt.No_Stdlib := True;
430 elsif Argv (2 .. Argv'Last) = "nostdinc" then
431 Opt.No_Stdinc := True;
435 elsif Argv (2 .. Argv'Last) = "static" then
436 Opt.Shared_Libgnat := False;
440 elsif Argv (2 .. Argv'Last) = "shared" then
441 Opt.Shared_Libgnat := True;
445 elsif Argv'Length >= 4 and then Argv (2 .. 3) = "F=" then
446 if Mapping_File /= null then
447 Fail ("cannot specify several mapping files");
450 Mapping_File := new String'(Argv
(4 .. Argv
'Last));
454 elsif Argv
'Length >= 3 and then Argv
(2) = 'M' then
455 if not Is_Cross_Compiler
then
457 ("gnatbind: -M not expected to be used on native platforms");
460 Opt
.Bind_Alternate_Main_Name
:= True;
461 Opt
.Alternate_Main_Name
:= new String'(Argv (3 .. Argv'Last));
463 -- All other options are single character and are handled by
464 -- Scan_Binder_Switches.
467 Scan_Binder_Switches (Argv);
470 -- Not a switch, so must be a file name (if non-empty)
472 elsif Argv'Length /= 0 then
474 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
478 Add_File (Argv & ".ali");
483 ----------------------------
484 -- Generic_Scan_Bind_Args --
485 ----------------------------
487 procedure Generic_Scan_Bind_Args is
488 Next_Arg : Positive := 1;
491 -- Use low level argument routines to avoid dragging in secondary stack
493 while Next_Arg < Arg_Count loop
495 Next_Argv : String (1 .. Len_Arg (Next_Arg));
498 Fill_Arg (Next_Argv'Address, Next_Arg);
500 if Next_Argv'Length > 0 then
501 if Next_Argv (1) = '@
' then
502 if Next_Argv'Length > 1 then
504 Arguments : constant Argument_List :=
505 Response_File.Arguments_From
506 (Response_File_Name =>
507 Next_Argv (2 .. Next_Argv'Last),
509 Ignore_Non_Existing_Files => True);
511 for J in Arguments'Range loop
512 Action (Arguments (J).all);
523 Next_Arg := Next_Arg + 1;
525 end Generic_Scan_Bind_Args;
531 procedure Write_Arg (S : String) is
536 procedure Scan_Bind_Args is new Generic_Scan_Bind_Args (Scan_Bind_Arg);
537 procedure Put_Bind_Args is new Generic_Scan_Bind_Args (Write_Arg);
539 procedure Check_Version_And_Help is
540 new Check_Version_And_Help_G (Bindusg.Display);
542 -- Start of processing for Gnatbind
545 -- Set default for Shared_Libgnat option
548 Shared_Libgnat_Default : Character;
550 (C, Shared_Libgnat_Default, "__gnat_shared_libgnat_default");
552 SHARED : constant Character := 'H
';
553 STATIC : constant Character := 'T
';
557 (Shared_Libgnat_Default = SHARED
559 Shared_Libgnat_Default = STATIC);
560 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
563 -- Carry out package initializations. These are initializations which
564 -- might logically be performed at elaboration time, and we decide to be
565 -- consistent. Like elaboration, the order in which these calls are made
566 -- is in some cases important.
571 -- Scan the switches and arguments. Note that Snames must already be
572 -- initialized (for processing of the -V switch).
574 -- First, scan to detect --version and/or --help
576 Check_Version_And_Help ("GNATBIND", "1992");
578 -- We need to Scan_Bind_Args first, to set Verbose_Mode, so we know whether
584 Write_Str (Command_Name);
589 if Use_Pragma_Linker_Constructor then
590 if Bind_Main_Program then
591 Fail ("switch -a must be used in conjunction with -n or -Lxxx");
593 elsif not Gnatbind_Supports_Auto_Init then
594 Fail ("automatic initialisation of elaboration " &
595 "not supported on this platform");
599 -- Test for trailing -o switch
601 if Opt.Output_File_Name_Present and then not Output_File_Name_Seen then
602 Fail ("output file name missing after -o");
605 -- Output usage if requested
607 if Usage_Requested then
611 -- Check that the binder file specified has extension .adb
613 if Opt.Output_File_Name_Present and then Output_File_Name_Seen then
614 Check_Extensions : declare
615 Length : constant Natural := Output_File_Name'Length;
616 Last : constant Natural := Output_File_Name'Last;
619 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
621 Fail ("output file name should have .adb extension");
623 end Check_Extensions;
626 Osint.Add_Default_Search_Dirs;
628 -- Acquire target parameters
630 Targparm.Get_Target_Parameters;
632 -- Initialize Cumulative_Restrictions with the restrictions on the target
633 -- scanned from the system.ads file. Then as we read ALI files, we will
634 -- accumulate additional restrictions specified in other files.
636 Cumulative_Restrictions := Targparm.Restrictions_On_Target;
638 -- Acquire configurable run-time mode
640 if Configurable_Run_Time_On_Target then
641 Configurable_Run_Time_Mode := True;
644 -- Output copyright notice if in verbose mode
648 Display_Version ("GNATBIND", "1995");
651 -- Output usage information if no arguments
653 if not More_Lib_Files then
654 if Argument_Count = 0 then
657 Write_Line ("try ""gnatbind --help"" for more information.");
660 Exit_Program (E_Fatal);
663 -- If a mapping file was specified, initialize the file mapping
665 if Mapping_File /= null then
666 Fmap.Initialize (Mapping_File.all);
669 -- The block here is to catch the Unrecoverable_Error exception in the
670 -- case where we exceed the maximum number of permissible errors or some
671 -- other unrecoverable error occurs.
674 -- Initialize binder packages
678 Initialize_ALI_Source;
686 while More_Lib_Files loop
687 Main_Lib_File := Next_Main_Lib_File;
689 if First_Main_Lib_File = No_File then
690 First_Main_Lib_File := Main_Lib_File;
695 Write_Str ("Checking: ");
697 Write_Str ("Binding: ");
700 Write_Name (Main_Lib_File);
704 Text := Read_Library_Info (Main_Lib_File, True);
708 pragma Warnings (Off, Id);
716 Ignore_Errors => Debug_Flag_I,
717 Directly_Scanned => True);
725 if No_Run_Time_Mode then
727 -- Set standard configuration parameters
729 Suppress_Standard_Library_On_Target := True;
730 Configurable_Run_Time_Mode := True;
733 -- For main ALI files, even if they are interfaces, we get their
734 -- dependencies. To be sure, we reset the Interface flag for all main
737 for Index in ALIs.First .. ALIs.Last loop
738 ALIs.Table (Index).SAL_Interface := False;
741 -- Add System.Standard_Library to list to ensure that these files are
742 -- included in the bind, even if not directly referenced from Ada code
743 -- This is suppressed if the appropriate targparm switch is set.
745 if not Suppress_Standard_Library_On_Target then
746 Name_Buffer (1 .. 12) := "s-stalib.ali";
748 Std_Lib_File := Name_Find;
749 Text := Read_Library_Info (Std_Lib_File, True);
753 pragma Warnings (Off, Id);
762 Ignore_Errors => Debug_Flag_I);
768 -- Load ALIs for all dependent units
770 for Index in ALIs.First .. ALIs.Last loop
771 Read_Withed_ALIs (Index);
774 -- Quit if some file needs compiling
776 if No_Object_Specified then
777 raise Unrecoverable_Error;
780 -- Quit with message if we had a GNATprove file
782 if GNATprove_Mode_Specified then
783 Error_Msg ("one or more files compiled in GNATprove mode");
784 raise Unrecoverable_Error;
787 -- Output list of ALI files in closure
789 if Output_ALI_List then
790 if ALI_List_Filename /= null then
791 Set_List_File (ALI_List_Filename.all);
794 for Index in ALIs.First .. ALIs.Last loop
796 Full_Afile : constant File_Name_Type :=
797 Find_File (ALIs.Table (Index).Afile, Library);
799 Write_Name (Full_Afile);
804 if ALI_List_Filename /= null then
809 -- Build source file table from the ALI files we have read in
813 -- If there is main program to bind, set Main_Lib_File to the first
814 -- library file, and the name from which to derive the binder generate
815 -- file to the first ALI file.
817 if Bind_Main_Program then
818 Main_Lib_File := First_Main_Lib_File;
819 Set_Current_File_Name_Index (To => 1);
822 -- Check that main library file is a suitable main program
825 and then ALIs.Table (ALIs.First).Main_Program = None
826 and then not No_Main_Subprogram
829 (Units.Table (ALIs.Table (ALIs.First).First_Unit).Uname);
832 Unit_Name : String := Name_Buffer (1 .. Name_Len - 2);
834 To_Mixed (Unit_Name);
835 Get_Name_String (ALIs.Table (ALIs.First).Sfile);
836 Add_Str_To_Name_Buffer (":1: ");
837 Add_Str_To_Name_Buffer (Unit_Name);
838 Add_Str_To_Name_Buffer (" cannot be used as a main program");
839 Write_Line (Name_Buffer (1 .. Name_Len));
840 Errors_Detected := Errors_Detected + 1;
844 -- Perform consistency and correctness checks
846 Check_Duplicated_Subunits;
849 Check_Configuration_Consistency;
851 -- List restrictions that could be applied to this partition
853 if List_Restrictions then
854 List_Applicable_Restrictions;
857 -- Complete bind if no errors
859 if Errors_Detected = 0 then
862 if Errors_Detected = 0 then
863 -- Display elaboration order if -l was specified
865 if Elab_Order_Output then
866 if not Zero_Formatting then
868 Write_Str ("ELABORATION ORDER");
872 for J in Elab_Order.First .. Elab_Order.Last loop
873 if not Units.Table (Elab_Order.Table (J)).SAL_Interface then
874 if not Zero_Formatting then
879 (Units.Table (Elab_Order.Table (J)).Uname);
884 if not Zero_Formatting then
889 if not Check_Only then
890 Gen_Output_File (Output_File_Name.all);
893 -- Display list of sources in the closure (except predefined
894 -- sources) if -R was used.
897 List_Closure_Display : declare
898 Source : File_Name_Type;
900 function Put_In_Sources (S : File_Name_Type) return Boolean;
901 -- Check if S is already in table Sources and put in Sources
902 -- if it is not. Return False if the source is already in
903 -- Sources, and True if it is added.
909 function Put_In_Sources
910 (S : File_Name_Type) return Boolean
913 for J in 1 .. Closure_Sources.Last loop
914 if Closure_Sources.Table (J) = S then
919 Closure_Sources.Append (S);
923 -- Start of processing for List_Closure_Display
926 Closure_Sources.Init;
928 if not Zero_Formatting then
930 Write_Str ("REFERENCED SOURCES");
934 for J in reverse Elab_Order.First .. Elab_Order.Last loop
935 Source := Units.Table (Elab_Order.Table (J)).Sfile;
937 -- Do not include same source more than once
939 if Put_In_Sources (Source)
941 -- Do not include run-time units unless -Ra switch set
943 and then (List_Closure_All
944 or else not Is_Internal_File_Name (Source))
946 if not Zero_Formatting then
950 Write_Str (Get_Name_String (Source));
955 -- Subunits do not appear in the elaboration table because
956 -- they are subsumed by their parent units, but we need to
957 -- list them for other tools. For now they are listed after
958 -- other files, rather than right after their parent, since
959 -- there is no easy link between the elaboration table and
960 -- the ALIs table ??? As subunits may appear repeatedly in
961 -- the list, if the parent unit appears in the context of
962 -- several units in the closure, duplicates are suppressed.
964 for J in Sdep.First .. Sdep.Last loop
965 Source := Sdep.Table (J).Sfile;
967 if Sdep.Table (J).Subunit_Name /= No_Name
968 and then Put_In_Sources (Source)
969 and then not Is_Internal_File_Name (Source)
971 if not Zero_Formatting then
975 Write_Str (Get_Name_String (Source));
980 if not Zero_Formatting then
983 end List_Closure_Display;
988 Total_Errors := Total_Errors + Errors_Detected;
989 Total_Warnings := Total_Warnings + Warnings_Detected;
992 when Unrecoverable_Error =>
993 Total_Errors := Total_Errors + Errors_Detected;
994 Total_Warnings := Total_Warnings + Warnings_Detected;
997 -- All done. Set proper exit status
1002 if Total_Errors > 0 then
1003 Exit_Program (E_Errors);
1005 elsif Total_Warnings > 0 then
1006 Exit_Program (E_Warnings);
1009 -- Do not call Exit_Program (E_Success), so that finalization occurs