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 Gnatvsn; use Gnatvsn;
41 with Namet; use Namet;
43 with Osint; use Osint;
44 with Osint.B; use Osint.B;
45 with Output; use Output;
46 with Rident; use Rident;
48 with Switch; use Switch;
49 with Switch.B; use Switch.B;
50 with Targparm; use Targparm;
51 with Types; use Types;
53 with System.Case_Util; use System.Case_Util;
54 with System.OS_Lib; use System.OS_Lib;
56 with Ada.Command_Line.Response_File; use Ada.Command_Line;
60 Total_Errors : Nat := 0;
61 -- Counts total errors in all files
63 Total_Warnings : Nat := 0;
64 -- Total warnings in all files
66 Main_Lib_File : File_Name_Type;
67 -- Current main library file
69 Std_Lib_File : File_Name_Type;
72 Text : Text_Buffer_Ptr;
75 Output_File_Name_Seen : Boolean := False;
76 Output_File_Name : String_Ptr := new String'("");
78 L_Switch_Seen : Boolean := False;
80 Mapping_File : String_Ptr := null;
82 function Gnatbind_Supports_Auto_Init return Boolean;
83 -- Indicates if automatic initialization of elaboration procedure
84 -- through the constructor mechanism is possible on the platform.
86 procedure List_Applicable_Restrictions;
87 -- List restrictions that apply to this partition if option taken
89 procedure Scan_Bind_Arg (Argv : String);
90 -- Scan and process binder specific arguments. Argv is a single argument.
91 -- All the one character arguments are still handled by Switch. This
92 -- routine handles -aO -aI and -I-. The lower bound of Argv must be 1.
94 function Is_Cross_Compiler return Boolean;
95 -- Returns True iff this is a cross-compiler
97 ---------------------------------
98 -- Gnatbind_Supports_Auto_Init --
99 ---------------------------------
101 function Gnatbind_Supports_Auto_Init return Boolean is
102 function gnat_binder_supports_auto_init return Integer;
103 pragma Import (C, gnat_binder_supports_auto_init,
104 "__gnat_binder_supports_auto_init");
106 return gnat_binder_supports_auto_init /= 0;
107 end Gnatbind_Supports_Auto_Init;
109 -----------------------
110 -- Is_Cross_Compiler --
111 -----------------------
113 function Is_Cross_Compiler return Boolean is
114 Cross_Compiler : Integer;
115 pragma Import (C, Cross_Compiler, "__gnat_is_cross_compiler");
117 return Cross_Compiler = 1;
118 end Is_Cross_Compiler;
120 ----------------------------------
121 -- List_Applicable_Restrictions --
122 ----------------------------------
124 procedure List_Applicable_Restrictions is
126 -- Define those restrictions that should be output if the gnatbind
127 -- -r switch is used. Not all restrictions are output for the reasons
128 -- given below in the list, and this array is used to test whether
129 -- the corresponding pragma should be listed. True means that it
130 -- should not be listed.
132 No_Restriction_List : constant array (All_Restrictions) of Boolean :=
133 (No_Exception_Propagation => True,
134 -- Modifies code resulting in different exception semantics
136 No_Exceptions => True,
137 -- Has unexpected Suppress (All_Checks) effect
139 No_Implicit_Conditionals => True,
140 -- This could modify and pessimize generated code
142 No_Implicit_Dynamic_Code => True,
143 -- This could modify and pessimize generated code
145 No_Implicit_Loops => True,
146 -- This could modify and pessimize generated code
148 No_Recursion => True,
149 -- Not checkable at compile time
151 No_Reentrancy => True,
152 -- Not checkable at compile time
154 Max_Entry_Queue_Length => True,
155 -- Not checkable at compile time
157 Max_Storage_At_Blocking => True,
158 -- Not checkable at compile time
162 Additional_Restrictions_Listed : Boolean := False;
163 -- Set True if we have listed header for restrictions
166 -- Loop through restrictions
168 for R in All_Restrictions loop
169 if not No_Restriction_List (R) then
171 -- We list a restriction if it is not violated, or if
172 -- it is violated but the violation count is exactly known.
174 if Cumulative_Restrictions.Violated (R) = False
175 or else (R in All_Parameter_Restrictions
177 Cumulative_Restrictions.Unknown (R) = False)
179 if not Additional_Restrictions_Listed then
182 ("The following additional restrictions may be" &
183 " applied to this partition:");
184 Additional_Restrictions_Listed := True;
187 Write_Str ("pragma Restrictions (");
190 S : constant String := Restriction_Id'Image (R);
192 Name_Len := S'Length;
193 Name_Buffer (1 .. Name_Len) := S;
196 Set_Casing (Mixed_Case);
197 Write_Str (Name_Buffer (1 .. Name_Len));
199 if R in All_Parameter_Restrictions then
201 Write_Int (Int (Cumulative_Restrictions.Count (R)));
209 end List_Applicable_Restrictions;
215 procedure Scan_Bind_Arg (Argv : String) is
216 pragma Assert (Argv'First = 1);
219 -- Now scan arguments that are specific to the binder and are not
220 -- handled by the common circuitry in Switch.
222 if Opt.Output_File_Name_Present
223 and then not Output_File_Name_Seen
225 Output_File_Name_Seen := True;
228 or else (Argv'Length >= 1 and then Argv (1) = '-')
230 Fail ("output File_Name missing after -o");
233 Output_File_Name := new String'(Argv);
236 elsif Argv'Length >= 2 and then Argv (1) = '-' then
240 if Argv (2 .. Argv'Last) = "I-" then
241 Opt.Look_In_Primary_Dir := False;
245 elsif Argv (2) = 'I' then
246 Add_Src_Search_Dir (Argv (3 .. Argv'Last));
247 Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
251 elsif Argv (2) = 'L' then
252 if Argv'Length >= 3 then
254 -- Remember that the -L switch was specified, so that if this
255 -- is on OpenVMS, the export names are put in uppercase.
256 -- This is not known before the target parameters are read.
258 L_Switch_Seen := True;
260 Opt.Bind_For_Library := True;
262 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
263 Opt.Ada_Final_Name :=
264 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Final_Suffix);
266 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
268 -- This option (-Lxxx) implies -n
270 Opt.Bind_Main_Program := False;
274 ("Prefix of initialization and finalization " &
275 "procedure names missing in -L");
278 -- -Sin -Slo -Shi -Sxx -Sev
280 elsif Argv'Length = 4
281 and then Argv (2) = 'S'
284 C1 : Character := Argv (3);
285 C2 : Character := Argv (4);
288 -- Fold to upper case
290 if C1 in 'a' .. 'z' then
291 C1 := Character'Val (Character'Pos (C1) - 32);
294 if C2 in 'a' .. 'z' then
295 C2 := Character'Val (Character'Pos (C2) - 32);
298 -- Test valid option and set mode accordingly
300 if C1 = 'E' and then C2 = 'V' then
303 elsif C1 = 'I' and then C2 = 'N' then
306 elsif C1 = 'L' and then C2 = 'O' then
309 elsif C1 = 'H' and then C2 = 'I' then
312 elsif (C1 in '0' .. '9' or else C1 in 'A' .. 'F')
314 (C2 in '0' .. '9' or else C2 in 'A' .. 'F')
318 -- Invalid -S switch, let Switch give error, set defalut of IN
321 Scan_Binder_Switches (Argv);
326 Initialize_Scalars_Mode1 := C1;
327 Initialize_Scalars_Mode2 := C2;
332 elsif Argv'Length >= 3
333 and then Argv (2 .. 3) = "aI"
335 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
339 elsif Argv'Length >= 3
340 and then Argv (2 .. 3) = "aO"
342 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
346 elsif Argv (2 .. Argv'Last) = "nostdlib" then
347 Opt.No_Stdlib := True;
351 elsif Argv (2 .. Argv'Last) = "nostdinc" then
352 Opt.No_Stdinc := True;
356 elsif Argv (2 .. Argv'Last) = "static" then
357 Opt.Shared_Libgnat := False;
361 elsif Argv (2 .. Argv'Last) = "shared" then
362 Opt.Shared_Libgnat := True;
366 elsif Argv'Length >= 4 and then Argv (2 .. 3) = "F=" then
367 if Mapping_File /= null then
368 Fail ("cannot specify several mapping files");
371 Mapping_File := new String'(Argv (4 .. Argv'Last));
375 elsif Argv'Length >= 3 and then Argv (2) = 'M' then
376 if not Is_Cross_Compiler then
378 ("gnatbind: -M not expected to be used on native platforms");
381 Opt.Bind_Alternate_Main_Name := True;
382 Opt.Alternate_Main_Name := new String'(Argv (3 .. Argv'Last));
384 -- All other options are single character and are handled by
385 -- Scan_Binder_Switches.
388 Scan_Binder_Switches (Argv);
391 -- Not a switch, so must be a file name (if non-empty)
393 elsif Argv'Length /= 0 then
395 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
399 Add_File (Argv & ".ali");
404 -- Start of processing for Gnatbind
408 -- Set default for Shared_Libgnat option
411 Shared_Libgnat_Default : Character;
413 (C, Shared_Libgnat_Default, "__gnat_shared_libgnat_default");
415 SHARED : constant Character := 'H';
416 STATIC : constant Character := 'T';
420 (Shared_Libgnat_Default = SHARED
422 Shared_Libgnat_Default = STATIC);
423 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
426 -- Use low level argument routines to avoid dragging in the secondary stack
429 Scan_Args : while Next_Arg < Arg_Count loop
431 Next_Argv : String (1 .. Len_Arg (Next_Arg));
433 Fill_Arg (Next_Argv'Address, Next_Arg);
435 if Next_Argv'Length > 0 then
436 if Next_Argv (1) = '@' then
437 if Next_Argv'Length > 1 then
439 Arguments : constant Argument_List :=
440 Response_File.Arguments_From
441 (Response_File_Name =>
442 Next_Argv (2 .. Next_Argv'Last),
444 Ignore_Non_Existing_Files => True);
446 for J in Arguments'Range loop
447 Scan_Bind_Arg (Arguments (J).all);
453 Scan_Bind_Arg (Next_Argv);
458 Next_Arg := Next_Arg + 1;
461 if Use_Pragma_Linker_Constructor then
462 if Bind_Main_Program then
463 Fail ("switch -a must be used in conjunction with -n or -Lxxx");
465 elsif not Gnatbind_Supports_Auto_Init then
466 Fail ("automatic initialisation of elaboration " &
467 "not supported on this platform");
471 -- Test for trailing -o switch
473 if Opt.Output_File_Name_Present
474 and then not Output_File_Name_Seen
476 Fail ("output file name missing after -o");
479 -- Output usage if requested
481 if Usage_Requested then
485 -- Check that the Ada binder file specified has extension .adb and that
486 -- the C binder file has extension .c
488 if Opt.Output_File_Name_Present
489 and then Output_File_Name_Seen
491 Check_Extensions : declare
492 Length : constant Natural := Output_File_Name'Length;
493 Last : constant Natural := Output_File_Name'Last;
496 if Ada_Bind_File then
498 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
500 Fail ("output file name should have .adb extension");
505 or else Output_File_Name (Last - 1 .. Last) /= ".c"
507 Fail ("output file name should have .c extension");
510 end Check_Extensions;
513 Osint.Add_Default_Search_Dirs;
515 -- Carry out package initializations. These are initializations which
516 -- might logically be performed at elaboration time, but Namet at least
517 -- can't be done that way (because it is used in the Compiler), and we
518 -- decide to be consistent. Like elaboration, the order in which these
519 -- calls are made is in some cases important.
525 -- Acquire target parameters
527 Targparm.Get_Target_Parameters;
529 -- Initialize Cumulative_Restrictions with the restrictions on the target
530 -- scanned from the system.ads file. Then as we read ALI files, we will
531 -- accumulate additional restrictions specified in other files.
533 Cumulative_Restrictions := Targparm.Restrictions_On_Target;
535 -- On OpenVMS, when -L is used, all external names used in pragmas Export
536 -- are in upper case. The reason is that on OpenVMS, the macro-assembler
537 -- MACASM-32, used to build Stand-Alone Libraries, only understands
540 if L_Switch_Seen and then OpenVMS_On_Target then
541 To_Upper (Opt.Ada_Init_Name.all);
542 To_Upper (Opt.Ada_Final_Name.all);
543 To_Upper (Opt.Ada_Main_Name.all);
546 -- Acquire configurable run-time mode
548 if Configurable_Run_Time_On_Target then
549 Configurable_Run_Time_Mode := True;
552 -- Output copyright notice if in verbose mode
556 Write_Str ("GNATBIND ");
557 Write_Str (Gnat_Version_String);
559 Write_Str ("Copyright 1995-" &
561 ", Free Software Foundation, Inc.");
565 -- Output usage information if no files
567 if not More_Lib_Files then
569 Exit_Program (E_Fatal);
572 -- If a mapping file was specified, initialize the file mapping
574 if Mapping_File /= null then
575 Fmap.Initialize (Mapping_File.all);
578 -- The block here is to catch the Unrecoverable_Error exception in the
579 -- case where we exceed the maximum number of permissible errors or some
580 -- other unrecoverable error occurs.
583 -- Initialize binder packages
587 Initialize_ALI_Source;
595 while More_Lib_Files loop
596 Main_Lib_File := Next_Main_Lib_File;
600 Write_Str ("Checking: ");
602 Write_Str ("Binding: ");
605 Write_Name (Main_Lib_File);
609 Text := Read_Library_Info (Main_Lib_File, True);
613 pragma Warnings (Off, Id);
621 Ignore_Errors => Debug_Flag_I);
629 if No_Run_Time_Mode then
631 -- Set standard configuration parameters
633 Suppress_Standard_Library_On_Target := True;
634 Configurable_Run_Time_Mode := True;
637 -- For main ALI files, even if they are interfaces, we get their
638 -- dependencies. To be sure, we reset the Interface flag for all main
641 for Index in ALIs.First .. ALIs.Last loop
642 ALIs.Table (Index).SAL_Interface := False;
645 -- Add System.Standard_Library to list to ensure that these files are
646 -- included in the bind, even if not directly referenced from Ada code
647 -- This is suppressed if the appropriate targparm switch is set.
649 if not Suppress_Standard_Library_On_Target then
650 Name_Buffer (1 .. 12) := "s-stalib.ali";
652 Std_Lib_File := Name_Find;
653 Text := Read_Library_Info (Std_Lib_File, True);
657 pragma Warnings (Off, Id);
666 Ignore_Errors => Debug_Flag_I);
672 -- Acquire all information in ALI files that have been read in
674 for Index in ALIs.First .. ALIs.Last loop
678 -- Quit if some file needs compiling
680 if No_Object_Specified then
681 raise Unrecoverable_Error;
684 -- Build source file table from the ALI files we have read in
688 -- Check that main library file is a suitable main program
691 and then ALIs.Table (ALIs.First).Main_Program = None
692 and then not No_Main_Subprogram
694 Error_Msg_File_1 := Main_Lib_File;
695 Error_Msg ("% does not contain a unit that can be a main program");
698 -- Perform consistency and correctness checks
700 Check_Duplicated_Subunits;
703 Check_Configuration_Consistency;
705 -- List restrictions that could be applied to this partition
707 if List_Restrictions then
708 List_Applicable_Restrictions;
711 -- Complete bind if no errors
713 if Errors_Detected = 0 then
716 if Errors_Detected = 0 then
717 -- Display elaboration order if -l was specified
719 if Elab_Order_Output then
720 if not Zero_Formatting then
722 Write_Str ("ELABORATION ORDER");
726 for J in Elab_Order.First .. Elab_Order.Last loop
727 if not Units.Table (Elab_Order.Table (J)).SAL_Interface then
728 if not Zero_Formatting then
733 (Units.Table (Elab_Order.Table (J)).Uname);
738 if not Zero_Formatting then
743 if not Check_Only then
744 Gen_Output_File (Output_File_Name.all);
747 -- Display list of sources in the closure (except predefined
748 -- sources) if -R was used.
751 if not Zero_Formatting then
753 Write_Str ("REFERENCED SOURCES");
757 for J in reverse Elab_Order.First .. Elab_Order.Last loop
759 -- Do not include the sources of the runtime
761 if not Is_Internal_File_Name
762 (Units.Table (Elab_Order.Table (J)).Sfile)
764 if not Zero_Formatting then
770 (Units.Table (Elab_Order.Table (J)).Sfile));
775 if not Zero_Formatting then
782 Total_Errors := Total_Errors + Errors_Detected;
783 Total_Warnings := Total_Warnings + Warnings_Detected;
786 when Unrecoverable_Error =>
787 Total_Errors := Total_Errors + Errors_Detected;
788 Total_Warnings := Total_Warnings + Warnings_Detected;
791 -- All done. Set proper exit status
796 if Total_Errors > 0 then
797 Exit_Program (E_Errors);
799 elsif Total_Warnings > 0 then
800 Exit_Program (E_Warnings);
803 -- Do not call Exit_Program (E_Success), so that finalization occurs