1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Fname; use Fname;
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;
47 with Switch; use Switch;
48 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 Std_Lib_File : File_Name_Type;
71 Text : Text_Buffer_Ptr;
74 Output_File_Name_Seen : Boolean := False;
75 Output_File_Name : String_Ptr := new String'("");
77 L_Switch_Seen : Boolean := False;
79 Mapping_File : String_Ptr := null;
81 function Gnatbind_Supports_Auto_Init return Boolean;
82 -- Indicates if automatic initialization of elaboration procedure
83 -- through the constructor mechanism is possible on the platform.
85 procedure List_Applicable_Restrictions;
86 -- List restrictions that apply to this partition if option taken
88 procedure Scan_Bind_Arg (Argv : String);
89 -- Scan and process binder specific arguments. Argv is a single argument.
90 -- All the one character arguments are still handled by Switch. This
91 -- routine handles -aO -aI and -I-. The lower bound of Argv must be 1.
93 function Is_Cross_Compiler return Boolean;
94 -- Returns True iff this is a cross-compiler
96 ---------------------------------
97 -- Gnatbind_Supports_Auto_Init --
98 ---------------------------------
100 function Gnatbind_Supports_Auto_Init return Boolean is
101 function gnat_binder_supports_auto_init return Integer;
102 pragma Import (C, gnat_binder_supports_auto_init,
103 "__gnat_binder_supports_auto_init");
105 return gnat_binder_supports_auto_init /= 0;
106 end Gnatbind_Supports_Auto_Init;
108 -----------------------
109 -- Is_Cross_Compiler --
110 -----------------------
112 function Is_Cross_Compiler return Boolean is
113 Cross_Compiler : Integer;
114 pragma Import (C, Cross_Compiler, "__gnat_is_cross_compiler");
116 return Cross_Compiler = 1;
117 end Is_Cross_Compiler;
119 ----------------------------------
120 -- List_Applicable_Restrictions --
121 ----------------------------------
123 procedure List_Applicable_Restrictions is
125 -- Define those restrictions that should be output if the gnatbind
126 -- -r switch is used. Not all restrictions are output for the reasons
127 -- given below in the list, and this array is used to test whether
128 -- the corresponding pragma should be listed. True means that it
129 -- should not be listed.
131 No_Restriction_List : constant array (All_Restrictions) of Boolean :=
132 (No_Exception_Propagation => True,
133 -- Modifies code resulting in different exception semantics
135 No_Exceptions => True,
136 -- Has unexpected Suppress (All_Checks) effect
138 No_Implicit_Conditionals => True,
139 -- This could modify and pessimize generated code
141 No_Implicit_Dynamic_Code => True,
142 -- This could modify and pessimize generated code
144 No_Implicit_Loops => True,
145 -- This could modify and pessimize generated code
147 No_Recursion => True,
148 -- Not checkable at compile time
150 No_Reentrancy => True,
151 -- Not checkable at compile time
153 Max_Entry_Queue_Length => True,
154 -- Not checkable at compile time
156 Max_Storage_At_Blocking => True,
157 -- Not checkable at compile time
161 Additional_Restrictions_Listed : Boolean := False;
162 -- Set True if we have listed header for restrictions
165 -- Loop through restrictions
167 for R in All_Restrictions loop
168 if not No_Restriction_List (R) then
170 -- We list a restriction if it is not violated, or if
171 -- it is violated but the violation count is exactly known.
173 if Cumulative_Restrictions.Violated (R) = False
174 or else (R in All_Parameter_Restrictions
176 Cumulative_Restrictions.Unknown (R) = False)
178 if not Additional_Restrictions_Listed then
181 ("The following additional restrictions may be" &
182 " applied to this partition:");
183 Additional_Restrictions_Listed := True;
186 Write_Str ("pragma Restrictions (");
189 S : constant String := Restriction_Id'Image (R);
191 Name_Len := S'Length;
192 Name_Buffer (1 .. Name_Len) := S;
195 Set_Casing (Mixed_Case);
196 Write_Str (Name_Buffer (1 .. Name_Len));
198 if R in All_Parameter_Restrictions then
200 Write_Int (Int (Cumulative_Restrictions.Count (R)));
208 end List_Applicable_Restrictions;
214 procedure Scan_Bind_Arg (Argv : String) is
215 pragma Assert (Argv'First = 1);
218 -- Now scan arguments that are specific to the binder and are not
219 -- handled by the common circuitry in Switch.
221 if Opt.Output_File_Name_Present
222 and then not Output_File_Name_Seen
224 Output_File_Name_Seen := True;
227 or else (Argv'Length >= 1 and then Argv (1) = '-')
229 Fail ("output File_Name missing after -o");
232 Output_File_Name := new String'(Argv);
235 elsif Argv'Length >= 2 and then Argv (1) = '-' then
239 if Argv (2 .. Argv'Last) = "I-" then
240 Opt.Look_In_Primary_Dir := False;
244 elsif Argv (2) = 'I' then
245 Add_Src_Search_Dir (Argv (3 .. Argv'Last));
246 Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
250 elsif Argv (2) = 'L' then
251 if Argv'Length >= 3 then
253 -- Remember that the -L switch was specified, so that if this
254 -- is on OpenVMS, the export names are put in uppercase.
255 -- This is not known before the target parameters are read.
257 L_Switch_Seen := True;
259 Opt.Bind_For_Library := True;
261 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
262 Opt.Ada_Final_Name :=
263 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Final_Suffix);
265 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
267 -- This option (-Lxxx) implies -n
269 Opt.Bind_Main_Program := False;
273 ("Prefix of initialization and finalization " &
274 "procedure names missing in -L");
277 -- -Sin -Slo -Shi -Sxx -Sev
279 elsif Argv'Length = 4
280 and then Argv (2) = 'S'
283 C1 : Character := Argv (3);
284 C2 : Character := Argv (4);
287 -- Fold to upper case
289 if C1 in 'a' .. 'z' then
290 C1 := Character'Val (Character'Pos (C1) - 32);
293 if C2 in 'a' .. 'z' then
294 C2 := Character'Val (Character'Pos (C2) - 32);
297 -- Test valid option and set mode accordingly
299 if C1 = 'E' and then C2 = 'V' then
302 elsif C1 = 'I' and then C2 = 'N' then
305 elsif C1 = 'L' and then C2 = 'O' then
308 elsif C1 = 'H' and then C2 = 'I' then
311 elsif (C1 in '0' .. '9' or else C1 in 'A' .. 'F')
313 (C2 in '0' .. '9' or else C2 in 'A' .. 'F')
317 -- Invalid -S switch, let Switch give error, set defalut of IN
320 Scan_Binder_Switches (Argv);
325 Initialize_Scalars_Mode1 := C1;
326 Initialize_Scalars_Mode2 := C2;
331 elsif Argv'Length >= 3
332 and then Argv (2 .. 3) = "aI"
334 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
338 elsif Argv'Length >= 3
339 and then Argv (2 .. 3) = "aO"
341 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
345 elsif Argv (2 .. Argv'Last) = "nostdlib" then
346 Opt.No_Stdlib := True;
350 elsif Argv (2 .. Argv'Last) = "nostdinc" then
351 Opt.No_Stdinc := True;
355 elsif Argv (2 .. Argv'Last) = "static" then
356 Opt.Shared_Libgnat := False;
360 elsif Argv (2 .. Argv'Last) = "shared" then
361 Opt.Shared_Libgnat := True;
365 elsif Argv'Length >= 4 and then Argv (2 .. 3) = "F=" then
366 if Mapping_File /= null then
367 Fail ("cannot specify several mapping files");
370 Mapping_File := new String'(Argv (4 .. Argv'Last));
374 elsif Argv'Length >= 3 and then Argv (2) = 'M' then
375 if not Is_Cross_Compiler then
377 ("gnatbind: -M not expected to be used on native platforms");
380 Opt.Bind_Alternate_Main_Name := True;
381 Opt.Alternate_Main_Name := new String'(Argv (3 .. Argv'Last));
383 -- All other options are single character and are handled by
384 -- Scan_Binder_Switches.
387 Scan_Binder_Switches (Argv);
390 -- Not a switch, so must be a file name (if non-empty)
392 elsif Argv'Length /= 0 then
394 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
398 Add_File (Argv & ".ali");
403 -- Start of processing for Gnatbind
407 -- Set default for Shared_Libgnat option
410 Shared_Libgnat_Default : Character;
412 (C, Shared_Libgnat_Default, "__gnat_shared_libgnat_default");
414 SHARED : constant Character := 'H';
415 STATIC : constant Character := 'T';
419 (Shared_Libgnat_Default = SHARED
421 Shared_Libgnat_Default = STATIC);
422 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
425 -- Scan the switches and arguments
427 -- First, scan to detect --version and/or --help
429 Check_Version_And_Help ("GNATBIND", "1995", Bindusg.Display'Access);
431 -- Use low level argument routines to avoid dragging in the secondary stack
434 Scan_Args : while Next_Arg < Arg_Count loop
436 Next_Argv : String (1 .. Len_Arg (Next_Arg));
438 Fill_Arg (Next_Argv'Address, Next_Arg);
440 if Next_Argv'Length > 0 then
441 if Next_Argv (1) = '@' then
442 if Next_Argv'Length > 1 then
444 Arguments : constant Argument_List :=
445 Response_File.Arguments_From
446 (Response_File_Name =>
447 Next_Argv (2 .. Next_Argv'Last),
449 Ignore_Non_Existing_Files => True);
451 for J in Arguments'Range loop
452 Scan_Bind_Arg (Arguments (J).all);
458 Scan_Bind_Arg (Next_Argv);
463 Next_Arg := Next_Arg + 1;
466 if Use_Pragma_Linker_Constructor then
467 if Bind_Main_Program then
468 Fail ("switch -a must be used in conjunction with -n or -Lxxx");
470 elsif not Gnatbind_Supports_Auto_Init then
471 Fail ("automatic initialisation of elaboration " &
472 "not supported on this platform");
476 -- Test for trailing -o switch
478 if Opt.Output_File_Name_Present
479 and then not Output_File_Name_Seen
481 Fail ("output file name missing after -o");
484 -- Output usage if requested
486 if Usage_Requested then
490 -- Check that the Ada binder file specified has extension .adb and that
491 -- the C binder file has extension .c
493 if Opt.Output_File_Name_Present
494 and then Output_File_Name_Seen
496 Check_Extensions : declare
497 Length : constant Natural := Output_File_Name'Length;
498 Last : constant Natural := Output_File_Name'Last;
501 if Ada_Bind_File then
503 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
505 Fail ("output file name should have .adb extension");
510 or else Output_File_Name (Last - 1 .. Last) /= ".c"
512 Fail ("output file name should have .c extension");
515 end Check_Extensions;
518 Osint.Add_Default_Search_Dirs;
520 -- Carry out package initializations. These are initializations which
521 -- might logically be performed at elaboration time, but Namet at least
522 -- can't be done that way (because it is used in the Compiler), and we
523 -- decide to be consistent. Like elaboration, the order in which these
524 -- calls are made is in some cases important.
530 -- Acquire target parameters
532 Targparm.Get_Target_Parameters;
534 -- Initialize Cumulative_Restrictions with the restrictions on the target
535 -- scanned from the system.ads file. Then as we read ALI files, we will
536 -- accumulate additional restrictions specified in other files.
538 Cumulative_Restrictions := Targparm.Restrictions_On_Target;
540 -- On OpenVMS, when -L is used, all external names used in pragmas Export
541 -- are in upper case. The reason is that on OpenVMS, the macro-assembler
542 -- MACASM-32, used to build Stand-Alone Libraries, only understands
545 if L_Switch_Seen and then OpenVMS_On_Target then
546 To_Upper (Opt.Ada_Init_Name.all);
547 To_Upper (Opt.Ada_Final_Name.all);
548 To_Upper (Opt.Ada_Main_Name.all);
551 -- Acquire configurable run-time mode
553 if Configurable_Run_Time_On_Target then
554 Configurable_Run_Time_Mode := True;
557 -- Output copyright notice if in verbose mode
561 Display_Version ("GNATBIND", "1995");
564 -- Output usage information if no files
566 if not More_Lib_Files then
568 Exit_Program (E_Fatal);
571 -- If a mapping file was specified, initialize the file mapping
573 if Mapping_File /= null then
574 Fmap.Initialize (Mapping_File.all);
577 -- The block here is to catch the Unrecoverable_Error exception in the
578 -- case where we exceed the maximum number of permissible errors or some
579 -- other unrecoverable error occurs.
582 -- Initialize binder packages
586 Initialize_ALI_Source;
594 while More_Lib_Files loop
595 Main_Lib_File := Next_Main_Lib_File;
599 Write_Str ("Checking: ");
601 Write_Str ("Binding: ");
604 Write_Name (Main_Lib_File);
608 Text := Read_Library_Info (Main_Lib_File, True);
612 pragma Warnings (Off, Id);
620 Ignore_Errors => Debug_Flag_I);
628 if No_Run_Time_Mode then
630 -- Set standard configuration parameters
632 Suppress_Standard_Library_On_Target := True;
633 Configurable_Run_Time_Mode := True;
636 -- For main ALI files, even if they are interfaces, we get their
637 -- dependencies. To be sure, we reset the Interface flag for all main
640 for Index in ALIs.First .. ALIs.Last loop
641 ALIs.Table (Index).SAL_Interface := False;
644 -- Add System.Standard_Library to list to ensure that these files are
645 -- included in the bind, even if not directly referenced from Ada code
646 -- This is suppressed if the appropriate targparm switch is set.
648 if not Suppress_Standard_Library_On_Target then
649 Name_Buffer (1 .. 12) := "s-stalib.ali";
651 Std_Lib_File := Name_Find;
652 Text := Read_Library_Info (Std_Lib_File, True);
656 pragma Warnings (Off, Id);
665 Ignore_Errors => Debug_Flag_I);
671 -- Acquire all information in ALI files that have been read in
673 for Index in ALIs.First .. ALIs.Last loop
677 -- Quit if some file needs compiling
679 if No_Object_Specified then
680 raise Unrecoverable_Error;
683 -- Build source file table from the ALI files we have read in
687 -- Check that main library file is a suitable main program
690 and then ALIs.Table (ALIs.First).Main_Program = None
691 and then not No_Main_Subprogram
693 Error_Msg_File_1 := Main_Lib_File;
694 Error_Msg ("% does not contain a unit that can be a main program");
697 -- Perform consistency and correctness checks
699 Check_Duplicated_Subunits;
702 Check_Configuration_Consistency;
704 -- List restrictions that could be applied to this partition
706 if List_Restrictions then
707 List_Applicable_Restrictions;
710 -- Complete bind if no errors
712 if Errors_Detected = 0 then
715 if Errors_Detected = 0 then
716 -- Display elaboration order if -l was specified
718 if Elab_Order_Output then
719 if not Zero_Formatting then
721 Write_Str ("ELABORATION ORDER");
725 for J in Elab_Order.First .. Elab_Order.Last loop
726 if not Units.Table (Elab_Order.Table (J)).SAL_Interface then
727 if not Zero_Formatting then
732 (Units.Table (Elab_Order.Table (J)).Uname);
737 if not Zero_Formatting then
742 if not Check_Only then
743 Gen_Output_File (Output_File_Name.all);
746 -- Display list of sources in the closure (except predefined
747 -- sources) if -R was used.
750 if not Zero_Formatting then
752 Write_Str ("REFERENCED SOURCES");
756 for J in reverse Elab_Order.First .. Elab_Order.Last loop
758 -- Do not include the sources of the runtime
760 if not Is_Internal_File_Name
761 (Units.Table (Elab_Order.Table (J)).Sfile)
763 if not Zero_Formatting then
769 (Units.Table (Elab_Order.Table (J)).Sfile));
774 if not Zero_Formatting then
781 Total_Errors := Total_Errors + Errors_Detected;
782 Total_Warnings := Total_Warnings + Warnings_Detected;
785 when Unrecoverable_Error =>
786 Total_Errors := Total_Errors + Errors_Detected;
787 Total_Warnings := Total_Warnings + Warnings_Detected;
790 -- All done. Set proper exit status
795 if Total_Errors > 0 then
796 Exit_Program (E_Errors);
798 elsif Total_Warnings > 0 then
799 Exit_Program (E_Warnings);
802 -- Do not call Exit_Program (E_Success), so that finalization occurs