1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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;
78 Output_File_Name_Seen : Boolean := False;
79 Output_File_Name : String_Ptr := new String'("");
81 L_Switch_Seen : Boolean := False;
83 Mapping_File : String_Ptr := null;
85 function Gnatbind_Supports_Auto_Init return Boolean;
86 -- Indicates if automatic initialization of elaboration procedure
87 -- through the constructor mechanism is possible on the platform.
89 procedure List_Applicable_Restrictions;
90 -- List restrictions that apply to this partition if option taken
92 procedure Scan_Bind_Arg (Argv : String);
93 -- Scan and process binder specific arguments. Argv is a single argument.
94 -- All the one character arguments are still handled by Switch. This
95 -- routine handles -aO -aI and -I-. The lower bound of Argv must be 1.
97 function Is_Cross_Compiler return Boolean;
98 -- Returns True iff this is a cross-compiler
100 ---------------------------------
101 -- Gnatbind_Supports_Auto_Init --
102 ---------------------------------
104 function Gnatbind_Supports_Auto_Init return Boolean is
105 function gnat_binder_supports_auto_init return Integer;
106 pragma Import (C, gnat_binder_supports_auto_init,
107 "__gnat_binder_supports_auto_init");
109 return gnat_binder_supports_auto_init /= 0;
110 end Gnatbind_Supports_Auto_Init;
112 -----------------------
113 -- Is_Cross_Compiler --
114 -----------------------
116 function Is_Cross_Compiler return Boolean is
117 Cross_Compiler : Integer;
118 pragma Import (C, Cross_Compiler, "__gnat_is_cross_compiler");
120 return Cross_Compiler = 1;
121 end Is_Cross_Compiler;
123 ----------------------------------
124 -- List_Applicable_Restrictions --
125 ----------------------------------
127 procedure List_Applicable_Restrictions is
129 -- Define those restrictions that should be output if the gnatbind
130 -- -r switch is used. Not all restrictions are output for the reasons
131 -- given below in the list, and this array is used to test whether
132 -- the corresponding pragma should be listed. True means that it
133 -- should not be listed.
135 No_Restriction_List : constant array (All_Restrictions) of Boolean :=
136 (No_Exception_Propagation => True,
137 -- Modifies code resulting in different exception semantics
139 No_Exceptions => True,
140 -- Has unexpected Suppress (All_Checks) effect
142 No_Implicit_Conditionals => True,
143 -- This could modify and pessimize generated code
145 No_Implicit_Dynamic_Code => True,
146 -- This could modify and pessimize generated code
148 No_Implicit_Loops => True,
149 -- This could modify and pessimize generated code
151 No_Recursion => True,
152 -- Not checkable at compile time
154 No_Reentrancy => True,
155 -- Not checkable at compile time
157 Max_Entry_Queue_Length => True,
158 -- Not checkable at compile time
160 Max_Storage_At_Blocking => True,
161 -- Not checkable at compile time
165 Additional_Restrictions_Listed : Boolean := False;
166 -- Set True if we have listed header for restrictions
168 function Restriction_Could_Be_Set (R : Restriction_Id) return Boolean;
169 -- Returns True if the given restriction can be listed as an additional
170 -- restriction that could be set.
172 ------------------------------
173 -- Restriction_Could_Be_Set --
174 ------------------------------
176 function Restriction_Could_Be_Set (R : Restriction_Id) return Boolean is
177 CR : Restrictions_Info renames Cumulative_Restrictions;
182 -- Boolean restriction
184 when All_Boolean_Restrictions =>
186 -- The condition for listing a boolean restriction as an
187 -- additional restriction that could be set is that it is
188 -- not violated by any unit, and not already set.
190 return CR.Violated (R) = False and then CR.Set (R) = False;
192 -- Parameter restriction
194 when All_Parameter_Restrictions =>
196 -- If the restriction is violated and the level of violation is
197 -- unknown, the restriction can definitely not be listed.
199 if CR.Violated (R) and then CR.Unknown (R) then
202 -- We can list the restriction if it is not set
204 elsif not CR.Set (R) then
207 -- We can list the restriction if is set to a greater value
208 -- than the maximum value known for the violation.
211 return CR.Value (R) > CR.Count (R);
214 -- No other values for R possible
220 end Restriction_Could_Be_Set;
222 -- Start of processing for List_Applicable_Restrictions
225 -- Loop through restrictions
227 for R in All_Restrictions loop
228 if not No_Restriction_List (R)
229 and then Restriction_Could_Be_Set (R)
231 if not Additional_Restrictions_Listed then
234 ("The following additional restrictions may be" &
235 " applied to this partition:");
236 Additional_Restrictions_Listed := True;
239 Write_Str ("pragma Restrictions (");
242 S : constant String := Restriction_Id'Image (R);
244 Name_Len := S'Length;
245 Name_Buffer (1 .. Name_Len) := S;
248 Set_Casing (Mixed_Case);
249 Write_Str (Name_Buffer (1 .. Name_Len));
251 if R in All_Parameter_Restrictions then
253 Write_Int (Int (Cumulative_Restrictions.Count (R)));
260 end List_Applicable_Restrictions;
266 procedure Scan_Bind_Arg (Argv : String) is
267 pragma Assert (Argv'First = 1);
270 -- Now scan arguments that are specific to the binder and are not
271 -- handled by the common circuitry in Switch.
273 if Opt.Output_File_Name_Present
274 and then not Output_File_Name_Seen
276 Output_File_Name_Seen := True;
279 or else (Argv'Length >= 1 and then Argv (1) = '-')
281 Fail ("output File_Name missing after -o");
284 Output_File_Name := new String'(Argv);
287 elsif Argv'Length >= 2 and then Argv (1) = '-' then
291 if Argv (2 .. Argv'Last) = "I-" then
292 Opt.Look_In_Primary_Dir := False;
296 elsif Argv (2) = 'I' then
297 Add_Src_Search_Dir (Argv (3 .. Argv'Last));
298 Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
302 elsif Argv (2) = 'L' then
303 if Argv'Length >= 3 then
305 -- Remember that the -L switch was specified, so that if this
306 -- is on OpenVMS, the export names are put in uppercase.
307 -- This is not known before the target parameters are read.
309 L_Switch_Seen := True;
311 Opt.Bind_For_Library := True;
313 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
314 Opt.Ada_Final_Name :=
315 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Final_Suffix);
317 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
319 -- This option (-Lxxx) implies -n
321 Opt.Bind_Main_Program := False;
325 ("Prefix of initialization and finalization " &
326 "procedure names missing in -L");
329 -- -Sin -Slo -Shi -Sxx -Sev
331 elsif Argv'Length = 4
332 and then Argv (2) = 'S'
335 C1 : Character := Argv (3);
336 C2 : Character := Argv (4);
339 -- Fold to upper case
341 if C1 in 'a' .. 'z' then
342 C1 := Character'Val (Character'Pos (C1) - 32);
345 if C2 in 'a' .. 'z' then
346 C2 := Character'Val (Character'Pos (C2) - 32);
349 -- Test valid option and set mode accordingly
351 if C1 = 'E' and then C2 = 'V' then
354 elsif C1 = 'I' and then C2 = 'N' then
357 elsif C1 = 'L' and then C2 = 'O' then
360 elsif C1 = 'H' and then C2 = 'I' then
363 elsif (C1 in '0' .. '9' or else C1 in 'A' .. 'F')
365 (C2 in '0' .. '9' or else C2 in 'A' .. 'F')
369 -- Invalid -S switch, let Switch give error, set default of IN
372 Scan_Binder_Switches (Argv);
377 Initialize_Scalars_Mode1 := C1;
378 Initialize_Scalars_Mode2 := C2;
383 elsif Argv'Length >= 3
384 and then Argv (2 .. 3) = "aI"
386 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
390 elsif Argv'Length >= 3
391 and then Argv (2 .. 3) = "aO"
393 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
397 elsif Argv (2 .. Argv'Last) = "nostdlib" then
398 Opt.No_Stdlib := True;
402 elsif Argv (2 .. Argv'Last) = "nostdinc" then
403 Opt.No_Stdinc := True;
407 elsif Argv (2 .. Argv'Last) = "static" then
408 Opt.Shared_Libgnat := False;
412 elsif Argv (2 .. Argv'Last) = "shared" then
413 Opt.Shared_Libgnat := True;
417 elsif Argv'Length >= 4 and then Argv (2 .. 3) = "F=" then
418 if Mapping_File /= null then
419 Fail ("cannot specify several mapping files");
422 Mapping_File := new String'(Argv (4 .. Argv'Last));
426 elsif Argv'Length >= 3 and then Argv (2) = 'M' then
427 if not Is_Cross_Compiler then
429 ("gnatbind: -M not expected to be used on native platforms");
432 Opt.Bind_Alternate_Main_Name := True;
433 Opt.Alternate_Main_Name := new String'(Argv (3 .. Argv'Last));
435 -- All other options are single character and are handled by
436 -- Scan_Binder_Switches.
439 Scan_Binder_Switches (Argv);
442 -- Not a switch, so must be a file name (if non-empty)
444 elsif Argv'Length /= 0 then
446 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
450 Add_File (Argv & ".ali");
455 procedure Check_Version_And_Help is
456 new Check_Version_And_Help_G (Bindusg.Display);
458 -- Start of processing for Gnatbind
462 -- Set default for Shared_Libgnat option
465 Shared_Libgnat_Default : Character;
467 (C, Shared_Libgnat_Default, "__gnat_shared_libgnat_default");
469 SHARED : constant Character := 'H';
470 STATIC : constant Character := 'T';
474 (Shared_Libgnat_Default = SHARED
476 Shared_Libgnat_Default = STATIC);
477 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
480 -- Scan the switches and arguments
482 -- First, scan to detect --version and/or --help
484 Check_Version_And_Help ("GNATBIND", "1995");
486 -- Use low level argument routines to avoid dragging in the secondary stack
489 Scan_Args : while Next_Arg < Arg_Count loop
491 Next_Argv : String (1 .. Len_Arg (Next_Arg));
493 Fill_Arg (Next_Argv'Address, Next_Arg);
495 if Next_Argv'Length > 0 then
496 if Next_Argv (1) = '@' then
497 if Next_Argv'Length > 1 then
499 Arguments : constant Argument_List :=
500 Response_File.Arguments_From
501 (Response_File_Name =>
502 Next_Argv (2 .. Next_Argv'Last),
504 Ignore_Non_Existing_Files => True);
506 for J in Arguments'Range loop
507 Scan_Bind_Arg (Arguments (J).all);
513 Scan_Bind_Arg (Next_Argv);
518 Next_Arg := Next_Arg + 1;
521 if Use_Pragma_Linker_Constructor then
522 if Bind_Main_Program then
523 Fail ("switch -a must be used in conjunction with -n or -Lxxx");
525 elsif not Gnatbind_Supports_Auto_Init then
526 Fail ("automatic initialisation of elaboration " &
527 "not supported on this platform");
531 -- Test for trailing -o switch
533 if Opt.Output_File_Name_Present
534 and then not Output_File_Name_Seen
536 Fail ("output file name missing after -o");
539 -- Output usage if requested
541 if Usage_Requested then
545 -- Check that the Ada binder file specified has extension .adb and that
546 -- the C binder file has extension .c
548 if Opt.Output_File_Name_Present
549 and then Output_File_Name_Seen
551 Check_Extensions : declare
552 Length : constant Natural := Output_File_Name'Length;
553 Last : constant Natural := Output_File_Name'Last;
556 if Ada_Bind_File then
558 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
560 Fail ("output file name should have .adb extension");
565 or else Output_File_Name (Last - 1 .. Last) /= ".c"
567 Fail ("output file name should have .c extension");
570 end Check_Extensions;
573 Osint.Add_Default_Search_Dirs;
575 -- Carry out package initializations. These are initializations which
576 -- might logically be performed at elaboration time, but Namet at least
577 -- can't be done that way (because it is used in the Compiler), and we
578 -- decide to be consistent. Like elaboration, the order in which these
579 -- calls are made is in some cases important.
585 -- Acquire target parameters
587 Targparm.Get_Target_Parameters;
589 -- Initialize Cumulative_Restrictions with the restrictions on the target
590 -- scanned from the system.ads file. Then as we read ALI files, we will
591 -- accumulate additional restrictions specified in other files.
593 Cumulative_Restrictions := Targparm.Restrictions_On_Target;
595 -- On OpenVMS, when -L is used, all external names used in pragmas Export
596 -- are in upper case. The reason is that on OpenVMS, the macro-assembler
597 -- MACASM-32, used to build Stand-Alone Libraries, only understands
600 if L_Switch_Seen and then OpenVMS_On_Target then
601 To_Upper (Opt.Ada_Init_Name.all);
602 To_Upper (Opt.Ada_Final_Name.all);
603 To_Upper (Opt.Ada_Main_Name.all);
606 -- Acquire configurable run-time mode
608 if Configurable_Run_Time_On_Target then
609 Configurable_Run_Time_Mode := True;
612 -- Output copyright notice if in verbose mode
616 Display_Version ("GNATBIND", "1995");
619 -- Output usage information if no files
621 if not More_Lib_Files then
623 Exit_Program (E_Fatal);
626 -- If a mapping file was specified, initialize the file mapping
628 if Mapping_File /= null then
629 Fmap.Initialize (Mapping_File.all);
632 -- The block here is to catch the Unrecoverable_Error exception in the
633 -- case where we exceed the maximum number of permissible errors or some
634 -- other unrecoverable error occurs.
637 -- Initialize binder packages
641 Initialize_ALI_Source;
649 while More_Lib_Files loop
650 Main_Lib_File := Next_Main_Lib_File;
652 if First_Main_Lib_File = No_File then
653 First_Main_Lib_File := Main_Lib_File;
658 Write_Str ("Checking: ");
660 Write_Str ("Binding: ");
663 Write_Name (Main_Lib_File);
667 Text := Read_Library_Info (Main_Lib_File, True);
671 pragma Warnings (Off, Id);
679 Ignore_Errors => Debug_Flag_I,
680 Directly_Scanned => True);
688 if No_Run_Time_Mode then
690 -- Set standard configuration parameters
692 Suppress_Standard_Library_On_Target := True;
693 Configurable_Run_Time_Mode := True;
696 -- For main ALI files, even if they are interfaces, we get their
697 -- dependencies. To be sure, we reset the Interface flag for all main
700 for Index in ALIs.First .. ALIs.Last loop
701 ALIs.Table (Index).SAL_Interface := False;
704 -- Add System.Standard_Library to list to ensure that these files are
705 -- included in the bind, even if not directly referenced from Ada code
706 -- This is suppressed if the appropriate targparm switch is set.
708 if not Suppress_Standard_Library_On_Target then
709 Name_Buffer (1 .. 12) := "s-stalib.ali";
711 Std_Lib_File := Name_Find;
712 Text := Read_Library_Info (Std_Lib_File, True);
716 pragma Warnings (Off, Id);
725 Ignore_Errors => Debug_Flag_I);
731 -- Acquire all information in ALI files that have been read in
733 for Index in ALIs.First .. ALIs.Last loop
737 -- Quit if some file needs compiling
739 if No_Object_Specified then
740 raise Unrecoverable_Error;
743 -- Build source file table from the ALI files we have read in
747 -- If there is main program to bind, set Main_Lib_File to the first
748 -- library file, and the name from which to derive the binder generate
749 -- file to the first ALI file.
751 if Bind_Main_Program then
752 Main_Lib_File := First_Main_Lib_File;
753 Set_Current_File_Name_Index (To => 1);
756 -- Check that main library file is a suitable main program
759 and then ALIs.Table (ALIs.First).Main_Program = None
760 and then not No_Main_Subprogram
762 Error_Msg_File_1 := Main_Lib_File;
763 Error_Msg ("{ does not contain a unit that can be a main program");
766 -- Perform consistency and correctness checks
768 Check_Duplicated_Subunits;
771 Check_Configuration_Consistency;
773 -- List restrictions that could be applied to this partition
775 if List_Restrictions then
776 List_Applicable_Restrictions;
779 -- Complete bind if no errors
781 if Errors_Detected = 0 then
784 if Errors_Detected = 0 then
785 -- Display elaboration order if -l was specified
787 if Elab_Order_Output then
788 if not Zero_Formatting then
790 Write_Str ("ELABORATION ORDER");
794 for J in Elab_Order.First .. Elab_Order.Last loop
795 if not Units.Table (Elab_Order.Table (J)).SAL_Interface then
796 if not Zero_Formatting then
801 (Units.Table (Elab_Order.Table (J)).Uname);
806 if not Zero_Formatting then
811 if not Check_Only then
812 Gen_Output_File (Output_File_Name.all);
815 -- Display list of sources in the closure (except predefined
816 -- sources) if -R was used.
820 package Sources is new Table.Table
821 (Table_Component_Type => File_Name_Type,
822 Table_Index_Type => Natural,
823 Table_Low_Bound => 1,
825 Table_Increment => 100,
826 Table_Name => "Gnatbind.Sources");
827 -- Table to record the sources in the closure, to avoid
830 Source : File_Name_Type;
832 function Put_In_Sources (S : File_Name_Type) return Boolean;
833 -- Check if S is already in table Sources and put in Sources
834 -- if it is not. Return False if the source is already in
835 -- Sources, and True if it is added.
841 function Put_In_Sources (S : File_Name_Type)
845 for J in 1 .. Sources.Last loop
846 if Sources.Table (J) = S then
856 if not Zero_Formatting then
858 Write_Str ("REFERENCED SOURCES");
862 for J in reverse Elab_Order.First .. Elab_Order.Last loop
864 Source := Units.Table (Elab_Order.Table (J)).Sfile;
866 -- Do not include the sources of the runtime and do not
867 -- include the same source several times.
869 if Put_In_Sources (Source)
870 and then not Is_Internal_File_Name (Source)
872 if not Zero_Formatting then
876 Write_Str (Get_Name_String (Source));
881 -- Subunits do not appear in the elaboration table because
882 -- they are subsumed by their parent units, but we need to
883 -- list them for other tools. For now they are listed after
884 -- other files, rather than right after their parent, since
885 -- there is no easy link between the elaboration table and
886 -- the ALIs table ??? As subunits may appear repeatedly in
887 -- the list, if the parent unit appears in the context of
888 -- several units in the closure, duplicates are suppressed.
890 for J in Sdep.First .. Sdep.Last loop
891 Source := Sdep.Table (J).Sfile;
893 if Sdep.Table (J).Subunit_Name /= No_Name
894 and then Put_In_Sources (Source)
895 and then not Is_Internal_File_Name (Source)
897 if not Zero_Formatting then
901 Write_Str (Get_Name_String (Source));
906 if not Zero_Formatting then
914 Total_Errors := Total_Errors + Errors_Detected;
915 Total_Warnings := Total_Warnings + Warnings_Detected;
918 when Unrecoverable_Error =>
919 Total_Errors := Total_Errors + Errors_Detected;
920 Total_Warnings := Total_Warnings + Warnings_Detected;
923 -- All done. Set proper exit status
928 if Total_Errors > 0 then
929 Exit_Program (E_Errors);
931 elsif Total_Warnings > 0 then
932 Exit_Program (E_Warnings);
935 -- Do not call Exit_Program (E_Success), so that finalization occurs