1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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 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;
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;
56 Total_Errors : Nat := 0;
57 -- Counts total errors in all files
59 Total_Warnings : Nat := 0;
60 -- Total warnings in all files
62 Main_Lib_File : File_Name_Type;
63 -- Current main library file
65 Std_Lib_File : File_Name_Type;
68 Text : Text_Buffer_Ptr;
71 Output_File_Name_Seen : Boolean := False;
72 Output_File_Name : String_Ptr := new String'("");
74 L_Switch_Seen : Boolean := False;
76 Mapping_File : String_Ptr := null;
78 function Gnatbind_Supports_Auto_Init return Boolean;
79 -- Indicates if automatic initialization of elaboration procedure
80 -- through the constructor mechanism is possible on the platform.
82 procedure List_Applicable_Restrictions;
83 -- List restrictions that apply to this partition if option taken
85 procedure Scan_Bind_Arg (Argv : String);
86 -- Scan and process binder specific arguments. Argv is a single argument.
87 -- All the one character arguments are still handled by Switch. This
88 -- routine handles -aO -aI and -I-.
90 ---------------------------------
91 -- Gnatbind_Supports_Auto_Init --
92 ---------------------------------
94 function Gnatbind_Supports_Auto_Init return Boolean is
95 function gnat_binder_supports_auto_init return Integer;
96 pragma Import (C, gnat_binder_supports_auto_init,
97 "__gnat_binder_supports_auto_init");
99 return gnat_binder_supports_auto_init /= 0;
100 end Gnatbind_Supports_Auto_Init;
102 ----------------------------------
103 -- List_Applicable_Restrictions --
104 ----------------------------------
106 procedure List_Applicable_Restrictions is
108 -- Define those restrictions that should be output if the gnatbind
109 -- -r switch is used. Not all restrictions are output for the reasons
110 -- given above in the list, and this array is used to test whether
111 -- the corresponding pragma should be listed. True means that it
112 -- should not be listed.
114 No_Restriction_List : constant array (All_Restrictions) of Boolean :=
115 (No_Exceptions => True,
116 -- Has unexpected Suppress (All_Checks) effect
118 No_Implicit_Conditionals => True,
119 -- This could modify and pessimize generated code
121 No_Implicit_Dynamic_Code => True,
122 -- This could modify and pessimize generated code
124 No_Implicit_Loops => True,
125 -- This could modify and pessimize generated code
127 No_Recursion => True,
128 -- Not checkable at compile time
130 No_Reentrancy => True,
131 -- Not checkable at compile time
133 Max_Entry_Queue_Length => True,
134 -- Not checkable at compile time
136 Max_Storage_At_Blocking => True,
137 -- Not checkable at compile time
141 Additional_Restrictions_Listed : Boolean := False;
142 -- Set True if we have listed header for restrictions
145 -- Loop through restrictions
147 for R in All_Restrictions loop
148 if not No_Restriction_List (R) then
150 -- We list a restriction if it is not violated, or if
151 -- it is violated but the violation count is exactly known.
153 if Cumulative_Restrictions.Violated (R) = False
154 or else (R in All_Parameter_Restrictions
156 Cumulative_Restrictions.Unknown (R) = False)
158 if not Additional_Restrictions_Listed then
161 ("The following additional restrictions may be" &
162 " applied to this partition:");
163 Additional_Restrictions_Listed := True;
166 Write_Str ("pragma Restrictions (");
169 S : constant String := Restriction_Id'Image (R);
171 Name_Len := S'Length;
172 Name_Buffer (1 .. Name_Len) := S;
175 Set_Casing (Mixed_Case);
176 Write_Str (Name_Buffer (1 .. Name_Len));
178 if R in All_Parameter_Restrictions then
180 Write_Int (Int (Cumulative_Restrictions.Count (R)));
188 end List_Applicable_Restrictions;
194 procedure Scan_Bind_Arg (Argv : String) is
196 -- Now scan arguments that are specific to the binder and are not
197 -- handled by the common circuitry in Switch.
199 if Opt.Output_File_Name_Present
200 and then not Output_File_Name_Seen
202 Output_File_Name_Seen := True;
205 or else (Argv'Length >= 1 and then Argv (1) = '-')
207 Fail ("output File_Name missing after -o");
210 Output_File_Name := new String'(Argv);
213 elsif Argv'Length >= 2 and then Argv (1) = '-' then
217 if Argv (2 .. Argv'Last) = "I-" then
218 Opt.Look_In_Primary_Dir := False;
222 elsif Argv (2) = 'I' then
223 Add_Src_Search_Dir (Argv (3 .. Argv'Last));
224 Add_Lib_Search_Dir (Argv (3 .. Argv'Last));
228 elsif Argv (2) = 'L' then
229 if Argv'Length >= 3 then
231 -- Remember that the -L switch was specified, so that if this
232 -- is on OpenVMS, the export names are put in uppercase.
233 -- This is not known before the target parameters are read.
235 L_Switch_Seen := True;
237 Opt.Bind_For_Library := True;
239 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Init_Suffix);
240 Opt.Ada_Final_Name :=
241 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Final_Suffix);
243 new String'(Argv (3 .. Argv'Last) & Opt.Ada_Main_Name_Suffix);
245 -- This option (-Lxxx) implies -n
247 Opt.Bind_Main_Program := False;
251 ("Prefix of initialization and finalization " &
252 "procedure names missing in -L");
255 -- -Sin -Slo -Shi -Sxx
257 elsif Argv'Length = 4
258 and then Argv (2) = 'S'
261 C1 : Character := Argv (3);
262 C2 : Character := Argv (4);
265 -- Fold to upper case
267 if C1 in 'a' .. 'z' then
268 C1 := Character'Val (Character'Pos (C1) - 32);
271 if C2 in 'a' .. 'z' then
272 C2 := Character'Val (Character'Pos (C2) - 32);
275 -- Test valid option and set mode accordingly
277 if C1 = 'E' and then C2 = 'V' then
280 elsif C1 = 'I' and then C2 = 'N' then
283 elsif C1 = 'L' and then C2 = 'O' then
286 elsif C1 = 'H' and then C2 = 'I' then
289 elsif (C1 in '0' .. '9' or else C1 in 'A' .. 'F')
291 (C2 in '0' .. '9' or else C2 in 'A' .. 'F')
295 -- Invalid -S switch, let Switch give error, set defalut of IN
298 Scan_Binder_Switches (Argv);
303 Initialize_Scalars_Mode1 := C1;
304 Initialize_Scalars_Mode2 := C2;
309 elsif Argv'Length >= 3
310 and then Argv (2 .. 3) = "aI"
312 Add_Src_Search_Dir (Argv (4 .. Argv'Last));
316 elsif Argv'Length >= 3
317 and then Argv (2 .. 3) = "aO"
319 Add_Lib_Search_Dir (Argv (4 .. Argv'Last));
323 elsif Argv (2 .. Argv'Last) = "nostdlib" then
324 Opt.No_Stdlib := True;
328 elsif Argv (2 .. Argv'Last) = "nostdinc" then
329 Opt.No_Stdinc := True;
333 elsif Argv (2 .. Argv'Last) = "static" then
334 Opt.Shared_Libgnat := False;
338 elsif Argv (2 .. Argv'Last) = "shared" then
339 Opt.Shared_Libgnat := True;
343 elsif Argv'Length >= 4 and then Argv (2 .. 3) = "F=" then
344 if Mapping_File /= null then
345 Fail ("cannot specify several mapping files");
348 Mapping_File := new String'(Argv (4 .. Argv'Last));
352 elsif Argv'Length >= 3 and then Argv (2) = 'M' then
353 Opt.Bind_Alternate_Main_Name := True;
354 Opt.Alternate_Main_Name := new String'(Argv (3 .. Argv'Last));
356 -- All other options are single character and are handled by
357 -- Scan_Binder_Switches.
360 Scan_Binder_Switches (Argv);
363 -- Not a switch, so must be a file name (if non-empty)
365 elsif Argv'Length /= 0 then
367 and then Argv (Argv'Last - 3 .. Argv'Last) = ".ali"
371 Add_File (Argv & ".ali");
376 -- Start of processing for Gnatbind
380 -- Set default for Shared_Libgnat option
383 Shared_Libgnat_Default : Character;
385 (C, Shared_Libgnat_Default, "__gnat_shared_libgnat_default");
387 SHARED : constant Character := 'H';
388 STATIC : constant Character := 'T';
392 (Shared_Libgnat_Default = SHARED
394 Shared_Libgnat_Default = STATIC);
395 Shared_Libgnat := (Shared_Libgnat_Default = SHARED);
398 -- Use low level argument routines to avoid dragging in the secondary stack
401 Scan_Args : while Next_Arg < Arg_Count loop
403 Next_Argv : String (1 .. Len_Arg (Next_Arg));
406 Fill_Arg (Next_Argv'Address, Next_Arg);
407 Scan_Bind_Arg (Next_Argv);
409 Next_Arg := Next_Arg + 1;
412 if Use_Pragma_Linker_Constructor then
413 if Bind_Main_Program then
414 Fail ("switch -a must be used in conjunction with -n or -Lxxx");
416 elsif not Gnatbind_Supports_Auto_Init then
417 Fail ("automatic initialisation of elaboration " &
418 "not supported on this platform");
422 -- Test for trailing -o switch
424 if Opt.Output_File_Name_Present
425 and then not Output_File_Name_Seen
427 Fail ("output file name missing after -o");
430 -- Output usage if requested
432 if Usage_Requested then
436 -- Check that the Ada binder file specified has extension .adb and that
437 -- the C binder file has extension .c
439 if Opt.Output_File_Name_Present
440 and then Output_File_Name_Seen
442 Check_Extensions : declare
443 Length : constant Natural := Output_File_Name'Length;
444 Last : constant Natural := Output_File_Name'Last;
447 if Ada_Bind_File then
449 or else Output_File_Name (Last - 3 .. Last) /= ".adb"
451 Fail ("output file name should have .adb extension");
456 or else Output_File_Name (Last - 1 .. Last) /= ".c"
458 Fail ("output file name should have .c extension");
461 end Check_Extensions;
464 Osint.Add_Default_Search_Dirs;
466 -- Carry out package initializations. These are initializations which
467 -- might logically be performed at elaboration time, but Namet at least
468 -- can't be done that way (because it is used in the Compiler), and we
469 -- decide to be consistent. Like elaboration, the order in which these
470 -- calls are made is in some cases important.
476 -- Acquire target parameters
478 Targparm.Get_Target_Parameters;
480 -- Initialize Cumulative_Restrictions with the restrictions on the target
481 -- scanned from the system.ads file. Then as we read ALI files, we will
482 -- accumulate additional restrictions specified in other files.
484 Cumulative_Restrictions := Targparm.Restrictions_On_Target;
486 -- On OpenVMS, when -L is used, all external names used in pragmas Export
487 -- are in upper case. The reason is that on OpenVMS, the macro-assembler
488 -- MACASM-32, used to build Stand-Alone Libraries, only understands
491 if L_Switch_Seen and then OpenVMS_On_Target then
492 To_Upper (Opt.Ada_Init_Name.all);
493 To_Upper (Opt.Ada_Final_Name.all);
494 To_Upper (Opt.Ada_Main_Name.all);
497 -- Acquire configurable run-time mode
499 if Configurable_Run_Time_On_Target then
500 Configurable_Run_Time_Mode := True;
503 -- Output copyright notice if in verbose mode
507 Write_Str ("GNATBIND ");
508 Write_Str (Gnat_Version_String);
510 Write_Str ("Copyright 1995-2005 Free Software Foundation, Inc.");
514 -- Output usage information if no files
516 if not More_Lib_Files then
518 Exit_Program (E_Fatal);
521 -- If a mapping file was specified, initialize the file mapping
523 if Mapping_File /= null then
524 Fmap.Initialize (Mapping_File.all);
527 -- The block here is to catch the Unrecoverable_Error exception in the
528 -- case where we exceed the maximum number of permissible errors or some
529 -- other unrecoverable error occurs.
532 -- Initialize binder packages
536 Initialize_ALI_Source;
544 while More_Lib_Files loop
545 Main_Lib_File := Next_Main_Lib_File;
549 Write_Str ("Checking: ");
551 Write_Str ("Binding: ");
554 Write_Name (Main_Lib_File);
558 Text := Read_Library_Info (Main_Lib_File, True);
562 pragma Warnings (Off, Id);
570 Ignore_Errors => Debug_Flag_I);
578 if No_Run_Time_Mode then
580 -- Set standard configuration parameters
582 Suppress_Standard_Library_On_Target := True;
583 Configurable_Run_Time_Mode := True;
586 -- For main ALI files, even if they are interfaces, we get their
587 -- dependencies. To be sure, we reset the Interface flag for all main
590 for Index in ALIs.First .. ALIs.Last loop
591 ALIs.Table (Index).SAL_Interface := False;
594 -- Add System.Standard_Library to list to ensure that these files are
595 -- included in the bind, even if not directly referenced from Ada code
596 -- This is suppressed if the appropriate targparm switch is set.
598 if not Suppress_Standard_Library_On_Target then
599 Name_Buffer (1 .. 12) := "s-stalib.ali";
601 Std_Lib_File := Name_Find;
602 Text := Read_Library_Info (Std_Lib_File, True);
606 pragma Warnings (Off, Id);
615 Ignore_Errors => Debug_Flag_I);
621 -- Acquire all information in ALI files that have been read in
623 for Index in ALIs.First .. ALIs.Last loop
627 -- Quit if some file needs compiling
629 if No_Object_Specified then
630 raise Unrecoverable_Error;
633 -- Build source file table from the ALI files we have read in
637 -- Check that main library file is a suitable main program
640 and then ALIs.Table (ALIs.First).Main_Program = None
641 and then not No_Main_Subprogram
643 Error_Msg_Name_1 := Main_Lib_File;
644 Error_Msg ("% does not contain a unit that can be a main program");
647 -- Perform consistency and correctness checks
649 Check_Duplicated_Subunits;
652 Check_Configuration_Consistency;
654 -- List restrictions that could be applied to this partition
656 if List_Restrictions then
657 List_Applicable_Restrictions;
660 -- Complete bind if no errors
662 if Errors_Detected = 0 then
665 if Errors_Detected = 0 then
666 if Elab_Order_Output then
668 Write_Str ("ELABORATION ORDER");
671 for J in Elab_Order.First .. Elab_Order.Last loop
672 if not Units.Table (Elab_Order.Table (J)).SAL_Interface then
675 (Units.Table (Elab_Order.Table (J)).Uname);
683 if not Check_Only then
684 Gen_Output_File (Output_File_Name.all);
689 Total_Errors := Total_Errors + Errors_Detected;
690 Total_Warnings := Total_Warnings + Warnings_Detected;
693 when Unrecoverable_Error =>
694 Total_Errors := Total_Errors + Errors_Detected;
695 Total_Warnings := Total_Warnings + Warnings_Detected;
698 -- All done. Set proper exit status
703 if Total_Errors > 0 then
704 Exit_Program (E_Errors);
705 elsif Total_Warnings > 0 then
706 Exit_Program (E_Warnings);
708 Exit_Program (E_Success);