1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2005, AdaCore --
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 ------------------------------------------------------------------------------
27 with Ada
.Command_Line
; use Ada
.Command_Line
;
28 with Ada
.Text_IO
; use Ada
.Text_IO
;
30 with GNAT
.Command_Line
; use GNAT
.Command_Line
;
31 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
32 with GNAT
.Heap_Sort_G
;
38 with System
.CRTL
; use System
.CRTL
;
42 Terminate_Program
: exception;
43 -- Used to terminate execution immediately
45 Config_File_Name
: constant String_Access
:= new String'("gnat.adc");
46 -- The name of the file holding the GNAT configuration pragmas
48 Gcc : String_Access := new String'("gcc");
49 -- May be modified by switch --GCC=
51 Gcc_Set
: Boolean := False;
52 -- True if a switch --GCC= is used
54 Gnat_Cmd
: String_Access
;
55 -- Command to execute the GNAT compiler
57 Gnat_Args
: Argument_List_Access
:=
62 new String'("-gnats"),
63 new String'("-gnatu"));
64 -- Arguments used in Gnat_Cmd call
66 EOF
: constant Character := Character'Val (26);
67 -- Special character to signal end of file. Not required in input
68 -- files, but properly treated if present. Not generated in output
69 -- files except as a result of copying input file.
75 subtype File_Num
is Natural;
76 subtype File_Offset
is Natural;
78 type File_Entry
is record
80 -- Name of chop file or directory
82 SR_Name
: String_Access
;
83 -- Null unless the chop file starts with a source reference pragma
84 -- in which case this field points to the file name from this pragma.
87 package File
is new GNAT
.Table
88 (Table_Component_Type
=> File_Entry
,
89 Table_Index_Type
=> File_Num
,
92 Table_Increment
=> 100);
94 Directory
: String_Access
;
95 -- Record name of directory, or a null string if no directory given
97 Compilation_Mode
: Boolean := False;
98 Overwrite_Files
: Boolean := False;
99 Preserve_Mode
: Boolean := False;
100 Quiet_Mode
: Boolean := False;
101 Source_References
: Boolean := False;
102 Verbose_Mode
: Boolean := False;
103 Exit_On_Error
: Boolean := False;
106 Write_gnat_adc
: Boolean := False;
107 -- Gets set true if we append to gnat.adc or create a new gnat.adc.
108 -- Used to inhibit complaint about no units generated.
114 type Line_Num
is new Natural;
115 -- Line number (for source reference pragmas)
117 type Unit_Count_Type
is new Integer;
118 subtype Unit_Num
is Unit_Count_Type
range 1 .. Unit_Count_Type
'Last;
119 -- Used to refer to unit number in unit table
121 type SUnit_Num
is new Integer;
122 -- Used to refer to entry in sorted units table. Note that entry
123 -- zero is only for use by Heapsort, and is not otherwise referenced.
125 type Unit_Kind
is (Unit_Spec
, Unit_Body
, Config_Pragmas
);
127 -- Structure to contain all necessary information for one unit.
128 -- Entries are also temporarily used to record config pragma sequences.
130 type Unit_Info
is record
131 File_Name
: String_Access
;
132 -- File name from GNAT output line
134 Chop_File
: File_Num
;
135 -- File number in chop file sequence
137 Start_Line
: Line_Num
;
138 -- Line number from GNAT output line
140 Offset
: File_Offset
;
141 -- Offset name from GNAT output line
143 SR_Present
: Boolean;
144 -- Set True if SR parameter present
146 Length
: File_Offset
;
147 -- A length of 0 means that the Unit is the last one in the file
150 -- Indicates kind of unit
152 Sorted_Index
: SUnit_Num
;
153 -- Index of unit in sorted unit list
155 Bufferg
: String_Access
;
156 -- Pointer to buffer containing configuration pragmas to be
157 -- prepended. Null if no pragmas to be prepended.
161 -- The following table stores the unit offset information
163 package Unit
is new GNAT
.Table
164 (Table_Component_Type
=> Unit_Info
,
165 Table_Index_Type
=> Unit_Count_Type
,
166 Table_Low_Bound
=> 1,
167 Table_Initial
=> 500,
168 Table_Increment
=> 100);
170 -- The following table is used as a sorted index to the Unit.Table.
171 -- The entries in Unit.Table are not moved, instead we just shuffle
172 -- the entries in Sorted_Units. Note that the zeroeth entry in this
173 -- table is used by GNAT.Heap_Sort_G.
175 package Sorted_Units
is new GNAT
.Table
176 (Table_Component_Type
=> Unit_Num
,
177 Table_Index_Type
=> SUnit_Num
,
178 Table_Low_Bound
=> 0,
179 Table_Initial
=> 500,
180 Table_Increment
=> 100);
182 function Is_Duplicated
(U
: SUnit_Num
) return Boolean;
183 -- Returns true if U is duplicated by a later unit.
184 -- Note that this function returns false for the last entry.
186 procedure Sort_Units
;
187 -- Sort units and set up sorted unit table
189 ----------------------
190 -- File_Descriptors --
191 ----------------------
193 function dup
(handle
: File_Descriptor
) return File_Descriptor
;
194 function dup2
(from
, to
: File_Descriptor
) return File_Descriptor
;
196 ---------------------
197 -- Local variables --
198 ---------------------
200 Warning_Count
: Natural := 0;
201 -- Count of warnings issued so far
203 -----------------------
204 -- Local subprograms --
205 -----------------------
207 procedure Error_Msg
(Message
: String; Warning
: Boolean := False);
208 -- Produce an error message on standard error output
210 procedure File_Time_Stamp
(Name
: C_File_Name
; Time
: OS_Time
);
211 -- Given the name of a file or directory, Name, set the
212 -- time stamp. This function must be used for an unopened file.
214 function Files_Exist
return Boolean;
215 -- Check Unit.Table for possible file names that already exist
216 -- in the file system. Returns true if files exist, False otherwise
218 function Get_Maximum_File_Name_Length
return Integer;
219 pragma Import
(C
, Get_Maximum_File_Name_Length
,
220 "__gnat_get_maximum_file_name_length");
221 -- Function to get maximum file name length for system
223 Maximum_File_Name_Length
: constant Integer := Get_Maximum_File_Name_Length
;
224 Maximum_File_Name_Length_String
: constant String :=
226 (Maximum_File_Name_Length
);
228 function Locate_Executable
229 (Program_Name
: String;
230 Look_For_Prefix
: Boolean := True)
231 return String_Access
;
232 -- Locate executable for given program name. This takes into account
233 -- the target-prefix of the current command, if Look_For_Prefix is True.
235 subtype EOL_Length
is Natural range 0 .. 2;
236 -- Possible lengths of end of line sequence
238 type EOL_String
(Len
: EOL_Length
:= 0) is record
239 Str
: String (1 .. Len
);
243 (Source
: access String;
246 -- Return the line terminator used in the passed string
248 procedure Parse_EOL
(Source
: access String; Ptr
: in out Positive);
249 -- On return Source (Ptr) is the first character of the next line
250 -- or EOF. Source.all must be terminated by EOF.
252 function Parse_File
(Num
: File_Num
) return Boolean;
253 -- Calls the GNAT compiler to parse the given source file and parses the
254 -- output using Parse_Offset_Info. Returns True if parse operation
255 -- completes, False if some system error (e.g. failure to read the
256 -- offset information) occurs.
258 procedure Parse_Offset_Info
(Chop_File
: File_Num
; Source
: access String);
259 -- Parses the output of the compiler indicating the offsets
260 -- and names of the compilation units in Chop_File.
262 procedure Parse_Token
263 (Source
: access String;
264 Ptr
: in out Positive;
265 Token_Ptr
: out Positive);
266 -- Skips any separators and stores the start of the token in Token_Ptr.
267 -- Then stores the position of the next separator in Ptr.
268 -- On return Source (Token_Ptr .. Ptr - 1) is the token.
271 (FD
: File_Descriptor
;
272 Contents
: out String_Access
;
273 Success
: out Boolean);
274 -- Reads file associated with FS into the newly allocated
276 -- [VMS] Success is true iff the number of bytes read is less than or
277 -- equal to the file size.
278 -- [Other] Success is true iff the number of bytes read is equal to
281 function Report_Duplicate_Units
return Boolean;
282 -- Output messages about duplicate units in the input files in Unit.Table
283 -- Returns True if any duplicates found, Fals if no duplicates found.
285 function Scan_Arguments
return Boolean;
286 -- Scan command line options and set global variables accordingly.
287 -- Also scan out file and directory arguments. Returns True if scan
288 -- was successful, and False if the scan fails for any reason.
291 -- Output message on standard output describing syntax of gnatchop command
293 procedure Warning_Msg
(Message
: String);
294 -- Output a warning message on standard error and update warning count
296 function Write_Chopped_Files
(Input
: File_Num
) return Boolean;
297 -- Write all units that result from chopping the Input file
299 procedure Write_Config_File
(Input
: File_Num
; U
: Unit_Num
);
300 -- Call to write configuration pragmas (append them to gnat.adc)
301 -- Input is the file number for the chop file and U identifies the
302 -- unit entry for the configuration pragmas.
304 function Get_Config_Pragmas
307 return String_Access
;
308 -- Call to read configuration pragmas from given unit entry, and
309 -- return a buffer containing the pragmas to be appended to
310 -- following units. Input is the file number for the chop file and
311 -- U identifies the unit entry for the configuration pragmas.
313 procedure Write_Source_Reference_Pragma
316 FD
: File_Descriptor
;
318 Success
: in out Boolean);
319 -- If Success is True on entry, writes a source reference pragma using
320 -- the chop file from Info, and the given line number. On return Success
321 -- indicates whether the write succeeded. If Success is False on entry,
322 -- or if the global flag Source_References is False, then the call to
323 -- Write_Source_Reference_Pragma has no effect. EOL indicates the end
324 -- of line sequence to be written at the end of the pragma.
327 (Source
: access String;
330 Success
: out Boolean);
331 -- Write one compilation unit of the source to file
337 function dup
(handle
: File_Descriptor
) return File_Descriptor
is
339 return File_Descriptor
(System
.CRTL
.dup
(int
(handle
)));
346 function dup2
(from
, to
: File_Descriptor
) return File_Descriptor
is
348 return File_Descriptor
(System
.CRTL
.dup2
(int
(from
), int
(to
)));
355 procedure Error_Msg
(Message
: String; Warning
: Boolean := False) is
357 Put_Line
(Standard_Error
, Message
);
360 Set_Exit_Status
(Failure
);
362 if Exit_On_Error
then
363 raise Terminate_Program
;
368 ---------------------
369 -- File_Time_Stamp --
370 ---------------------
372 procedure File_Time_Stamp
(Name
: C_File_Name
; Time
: OS_Time
) is
373 procedure Set_File_Time
(Name
: C_File_Name
; Time
: OS_Time
);
374 pragma Import
(C
, Set_File_Time
, "__gnat_set_file_time_name");
377 Set_File_Time
(Name
, Time
);
384 function Files_Exist
return Boolean is
385 Exists
: Boolean := False;
388 for SNum
in 1 .. SUnit_Num
(Unit
.Last
) loop
390 -- Only check and report for the last instance of duplicated files
392 if not Is_Duplicated
(SNum
) then
394 Info
: constant Unit_Info
:=
395 Unit
.Table
(Sorted_Units
.Table
(SNum
));
398 if Is_Writable_File
(Info
.File_Name
.all) then
399 if Hostparm
.OpenVMS
then
402 & " already exists, use /OVERWRITE to overwrite");
404 Error_Msg
(Info
.File_Name
.all
405 & " already exists, use -w to overwrite");
417 ------------------------
418 -- Get_Config_Pragmas --
419 ------------------------
421 function Get_Config_Pragmas
426 Info
: Unit_Info
renames Unit
.Table
(U
);
427 FD
: File_Descriptor
;
428 Name
: aliased constant String :=
429 File
.Table
(Input
).Name
.all & ASCII
.Nul
;
430 Length
: File_Offset
;
431 Buffer
: String_Access
;
433 Result
: String_Access
;
436 FD
:= Open_Read
(Name
'Address, Binary
);
438 if FD
= Invalid_FD
then
439 Error_Msg
("cannot open " & File
.Table
(Input
).Name
.all);
443 Read_File
(FD
, Buffer
, Success
);
445 -- A length of 0 indicates that the rest of the file belongs to
446 -- this unit. The actual length must be calculated now. Take into
447 -- account that the last character (EOF) must not be written.
449 if Info
.Length
= 0 then
450 Length
:= Buffer
'Last - (Buffer
'First + Info
.Offset
);
452 Length
:= Info
.Length
;
455 Result
:= new String'(Buffer (1 .. Length));
458 end Get_Config_Pragmas;
465 (Source : access String;
469 Ptr : Positive := Start;
474 -- Skip to end of line
476 while Source (Ptr) /= ASCII.CR and then
477 Source (Ptr) /= ASCII.LF and then
485 if Source (Ptr) /= EOF then
495 -- Recognize CR/LF or LF/CR combination
497 if (Source (Ptr + 1) = ASCII.CR or Source (Ptr + 1) = ASCII.LF)
498 and then Source (Ptr) /= Source (Ptr + 1)
503 return (Len => Last + 1 - First, Str => Source (First .. Last));
510 function Is_Duplicated (U : SUnit_Num) return Boolean is
512 return U < SUnit_Num (Unit.Last)
514 Unit.Table (Sorted_Units.Table (U)).File_Name.all =
515 Unit.Table (Sorted_Units.Table (U + 1)).File_Name.all;
518 -----------------------
519 -- Locate_Executable --
520 -----------------------
522 function Locate_Executable
523 (Program_Name : String;
524 Look_For_Prefix : Boolean := True) return String_Access
526 Current_Command : constant String := Normalize_Pathname (Command_Name);
527 End_Of_Prefix : Natural;
528 Start_Of_Prefix : Positive;
529 Result : String_Access;
532 Start_Of_Prefix := Current_Command'First;
533 End_Of_Prefix := Start_Of_Prefix - 1;
535 if Look_For_Prefix then
537 -- Find Start_Of_Prefix
539 for J in reverse Current_Command'Range loop
540 if Current_Command (J) = '/' or
541 Current_Command (J) = Directory_Separator or
542 Current_Command (J) = ':'
544 Start_Of_Prefix := J + 1;
549 -- Find End_Of_Prefix
551 for J in reverse Start_Of_Prefix .. Current_Command'Last loop
552 if Current_Command (J) = '-' then
560 Command : constant String :=
561 Current_Command (Start_Of_Prefix .. End_Of_Prefix) &
564 Result := Locate_Exec_On_Path (Command);
566 if Result = null then
568 (Command & ": installation problem, executable not found");
573 end Locate_Executable;
579 procedure Parse_EOL (Source : access String; Ptr : in out Positive) is
581 -- Skip to end of line
583 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
584 and then Source (Ptr) /= EOF
589 if Source (Ptr) /= EOF then
590 Ptr := Ptr + 1; -- skip CR or LF
593 -- Skip past CR/LF or LF/CR combination
595 if (Source (Ptr) = ASCII.CR or Source (Ptr) = ASCII.LF)
596 and then Source (Ptr) /= Source (Ptr - 1)
606 function Parse_File (Num : File_Num) return Boolean is
607 Chop_Name : constant String_Access := File.Table (Num).Name;
608 Save_Stdout : constant File_Descriptor := dup (Standout);
609 Offset_Name : Temp_File_Name;
610 Offset_FD : File_Descriptor;
611 Buffer : String_Access;
616 -- Display copy of GNAT command if verbose mode
621 for J in 1 .. Gnat_Args'Length loop
623 Put (Gnat_Args (J).all);
627 Put_Line (Chop_Name.all);
630 -- Create temporary file
632 Create_Temp_File (Offset_FD, Offset_Name);
634 if Offset_FD = Invalid_FD then
635 Error_Msg ("gnatchop: cannot create temporary file");
640 -- Redirect Stdout to this temporary file in the Unix way
642 if dup2 (Offset_FD, Standout) = Invalid_FD then
643 Error_Msg ("gnatchop: cannot redirect stdout to temporary file");
649 -- Call Gnat on the source filename argument with special options
650 -- to generate offset information. If this special compilation completes
651 -- successfully then we can do the actual gnatchop operation.
653 Spawn (Gnat_Cmd.all, Gnat_Args.all & Chop_Name, Success);
656 Error_Msg (Chop_Name.all & ": parse errors detected");
657 Error_Msg (Chop_Name.all & ": chop may not be successful");
662 if dup2 (Save_Stdout, Standout) = Invalid_FD then
663 Error_Msg ("gnatchop: cannot restore stdout");
666 -- Reopen the file to start reading from the beginning
670 Offset_FD := Open_Read (Offset_Name'Address, Binary);
672 if Offset_FD = Invalid_FD then
673 Error_Msg ("gnatchop: cannot access offset info");
677 Read_File (Offset_FD, Buffer, Success);
680 Error_Msg ("gnatchop: error reading offset info");
684 Parse_Offset_Info (Num, Buffer);
687 -- Close and delete temporary file
690 Delete_File (Offset_Name'Address, Success);
695 when Failure | Terminate_Program =>
697 Delete_File (Offset_Name'Address, Success);
702 -----------------------
703 -- Parse_Offset_Info --
704 -----------------------
706 procedure Parse_Offset_Info
707 (Chop_File : File_Num;
708 Source : access String)
710 First_Unit : constant Unit_Num := Unit.Last + 1;
711 Bufferg : String_Access := null;
712 Parse_Ptr : File_Offset := Source'First;
713 Token_Ptr : File_Offset;
716 function Match (Literal : String) return Boolean;
717 -- Checks if given string appears at the current Token_Ptr location
718 -- and if so, bumps Parse_Ptr past the token and returns True. If
719 -- the string is not present, sets Parse_Ptr to Token_Ptr and
726 function Match (Literal : String) return Boolean is
728 Parse_Token (Source, Parse_Ptr, Token_Ptr);
730 if Source'Last + 1 - Token_Ptr < Literal'Length
732 Source (Token_Ptr .. Token_Ptr + Literal'Length - 1) /= Literal
734 Parse_Ptr := Token_Ptr;
738 Parse_Ptr := Token_Ptr + Literal'Length;
742 -- Start of processing for Parse_Offset_Info
746 -- Set default values, should get changed for all
747 -- units/pragmas except for the last
749 Info.Chop_File := Chop_File;
752 -- Parse the current line of offset information into Info
753 -- and exit the loop if there are any errors or on EOF.
755 -- First case, parse a line in the following format:
757 -- Unit x (spec) line 7, file offset 142, [SR, ]file name x.ads
759 -- Note that the unit name can be an operator name in quotes.
760 -- This is of course illegal, but both GNAT and gnatchop handle
761 -- the case so that this error does not intefere with chopping.
763 -- The SR ir present indicates that a source reference pragma
764 -- was processed as part of this unit (and that therefore no
765 -- Source_Reference pragma should be generated.
767 if Match ("Unit") then
768 Parse_Token (Source, Parse_Ptr, Token_Ptr);
770 if Match ("(body)") then
771 Info.Kind := Unit_Body;
772 elsif Match ("(spec)") then
773 Info.Kind := Unit_Spec;
778 exit when not Match ("line");
779 Parse_Token (Source, Parse_Ptr, Token_Ptr);
780 Info.Start_Line := Line_Num'Value
781 (Source (Token_Ptr .. Parse_Ptr - 1));
783 exit when not Match ("file offset");
784 Parse_Token (Source, Parse_Ptr, Token_Ptr);
785 Info.Offset := File_Offset'Value
786 (Source (Token_Ptr .. Parse_Ptr - 1));
788 Info.SR_Present := Match ("SR, ");
790 exit when not Match ("file name");
791 Parse_Token (Source, Parse_Ptr, Token_Ptr);
792 Info.File_Name := new String'
793 (Directory
.all & Source
(Token_Ptr
.. Parse_Ptr
- 1));
794 Parse_EOL
(Source
, Parse_Ptr
);
796 -- Second case, parse a line of the following form
798 -- Configuration pragmas at line 10, file offset 223
800 elsif Match
("Configuration pragmas at") then
801 Info
.Kind
:= Config_Pragmas
;
802 Info
.File_Name
:= Config_File_Name
;
804 exit when not Match
("line");
805 Parse_Token
(Source
, Parse_Ptr
, Token_Ptr
);
806 Info
.Start_Line
:= Line_Num
'Value
807 (Source
(Token_Ptr
.. Parse_Ptr
- 1));
809 exit when not Match
("file offset");
810 Parse_Token
(Source
, Parse_Ptr
, Token_Ptr
);
811 Info
.Offset
:= File_Offset
'Value
812 (Source
(Token_Ptr
.. Parse_Ptr
- 1));
814 Parse_EOL
(Source
, Parse_Ptr
);
816 -- Third case, parse a line of the following form
818 -- Source_Reference pragma for file "filename"
820 -- This appears at the start of the file only, and indicates
821 -- the name to be used on any generated Source_Reference pragmas.
823 elsif Match
("Source_Reference pragma for file ") then
824 Parse_Token
(Source
, Parse_Ptr
, Token_Ptr
);
825 File
.Table
(Chop_File
).SR_Name
:=
826 new String'(Source (Token_Ptr + 1 .. Parse_Ptr - 2));
827 Parse_EOL (Source, Parse_Ptr);
830 -- Unrecognized keyword or end of file
836 -- Store the data in the Info record in the Unit.Table
839 Unit.Table (Unit.Last) := Info;
841 -- If this is not the first unit from the file, calculate
842 -- the length of the previous unit as difference of the offsets
844 if Unit.Last > First_Unit then
845 Unit.Table (Unit.Last - 1).Length :=
846 Info.Offset - Unit.Table (Unit.Last - 1).Offset;
849 -- If not in compilation mode combine current unit with any
850 -- preceding configuration pragmas.
852 if not Compilation_Mode
853 and then Unit.Last > First_Unit
854 and then Unit.Table (Unit.Last - 1).Kind = Config_Pragmas
856 Info.Start_Line := Unit.Table (Unit.Last - 1).Start_Line;
857 Info.Offset := Unit.Table (Unit.Last - 1).Offset;
859 -- Delete the configuration pragma entry
861 Unit.Table (Unit.Last - 1) := Info;
865 -- If in compilation mode, and previous entry is the initial
866 -- entry for the file and is for configuration pragmas, then
867 -- they are to be appended to every unit in the file.
870 and then Unit.Last = First_Unit + 1
871 and then Unit.Table (First_Unit).Kind = Config_Pragmas
875 (Unit.Table (Unit.Last - 1).Chop_File, First_Unit);
876 Unit.Table (Unit.Last - 1) := Info;
880 Unit.Table (Unit.Last).Bufferg := Bufferg;
882 -- If in compilation mode, and this is not the first item,
883 -- combine configuration pragmas with previous unit, which
884 -- will cause an error message to be generated when the unit
888 and then Unit.Last > First_Unit
889 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
899 -- Find out if the loop was exited prematurely because of
900 -- an error or if the EOF marker was found.
902 if Source (Parse_Ptr) /= EOF then
904 (File.Table (Chop_File).Name.all & ": error parsing offset info");
908 -- Handle case of a chop file consisting only of config pragmas
910 if Unit.Last = First_Unit
911 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
913 -- In compilation mode, we append such a file to gnat.adc
915 if Compilation_Mode then
916 Write_Config_File (Unit.Table (Unit.Last).Chop_File, First_Unit);
919 -- In default (non-compilation) mode, this is invalid
923 (File.Table (Chop_File).Name.all &
924 ": no units found (only pragmas)");
929 -- Handle case of a chop file ending with config pragmas. This can
930 -- happen only in default non-compilation mode, since in compilation
931 -- mode such configuration pragmas are part of the preceding unit.
932 -- We simply concatenate such pragmas to the previous file which
933 -- will cause a compilation error, which is appropriate.
935 if Unit.Last > First_Unit
936 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
940 end Parse_Offset_Info;
946 procedure Parse_Token
947 (Source : access String;
948 Ptr : in out Positive;
949 Token_Ptr : out Positive)
951 In_Quotes : Boolean := False;
956 while Source (Ptr) = ' ' or Source (Ptr) = ',' loop
964 while (In_Quotes or else not (Source (Ptr) = ' ' or Source (Ptr) = ','))
965 and then Source (Ptr) >= ' '
967 if Source (Ptr) = '"' then
968 In_Quotes := not In_Quotes;
980 (FD : File_Descriptor;
981 Contents : out String_Access;
982 Success : out Boolean)
984 Length : constant File_Offset := File_Offset (File_Length (FD));
985 -- Include room for EOF char
986 Buffer : constant String_Access := new String (1 .. Length + 1);
989 Read_Ptr : File_Offset := 1;
994 This_Read := Read (FD,
995 A => Buffer (Read_Ptr)'Address,
996 N => Length + 1 - Read_Ptr);
997 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
998 exit when This_Read <= 0;
1001 Buffer (Read_Ptr) := EOF;
1002 Contents := new String (1 .. Read_Ptr);
1003 Contents.all := Buffer (1 .. Read_Ptr);
1005 -- Things aren't simple on VMS due to the plethora of file types
1006 -- and organizations. It seems clear that there shouldn't be more
1007 -- bytes read than are contained in the file though.
1009 if Hostparm.OpenVMS then
1010 Success := Read_Ptr <= Length + 1;
1012 Success := Read_Ptr = Length + 1;
1016 ----------------------------
1017 -- Report_Duplicate_Units --
1018 ----------------------------
1020 function Report_Duplicate_Units return Boolean is
1024 Duplicates : Boolean := False;
1028 while US < SUnit_Num (Unit.Last) loop
1029 U := Sorted_Units.Table (US);
1031 if Is_Duplicated (US) then
1034 -- Move to last two versions of duplicated file to make it clearer
1035 -- to understand which file is retained in case of overwriting.
1037 while US + 1 < SUnit_Num (Unit.Last) loop
1038 exit when not Is_Duplicated (US + 1);
1042 U := Sorted_Units.Table (US);
1044 if Overwrite_Files then
1045 Warning_Msg (Unit.Table (U).File_Name.all
1046 & " is duplicated
(all but last will be skipped
)");
1048 elsif Unit.Table (U).Chop_File =
1049 Unit.Table (Sorted_Units.Table (US + 1)).Chop_File
1051 Error_Msg (Unit.Table (U).File_Name.all
1052 & " is duplicated
in "
1053 & File.Table (Unit.Table (U).Chop_File).Name.all);
1056 Error_Msg (Unit.Table (U).File_Name.all
1058 & File.Table (Unit.Table (U).Chop_File).Name.all
1059 & " is duplicated
in "
1062 (Sorted_Units.Table (US + 1)).Chop_File).Name.all);
1069 if Duplicates and not Overwrite_Files then
1070 if Hostparm.OpenVMS then
1072 ("use /OVERWRITE to overwrite files
and keep last version
");
1074 Put_Line ("use -w to overwrite files
and keep last version
");
1079 end Report_Duplicate_Units;
1081 --------------------
1082 -- Scan_Arguments --
1083 --------------------
1085 function Scan_Arguments return Boolean is
1086 Kset : Boolean := False;
1087 -- Set true if -k switch found
1090 Initialize_Option_Scan;
1092 -- Scan options first
1095 case Getopt ("c gnat? h k? p q r v w x
-GCC
=!") is
1100 Gcc := new String'(Parameter);
1104 Compilation_Mode := True;
1108 new Argument_List'(Gnat_Args.all &
1109 new String'("-gnat
" & Parameter));
1113 raise Terminate_Program;
1117 Param : String_Access := new String'(Parameter);
1120 if Param.all /= "" then
1121 for J in Param'Range loop
1122 if Param (J) not in '0' .. '9' then
1123 if Hostparm.OpenVMS then
1124 Error_Msg ("/FILE_NAME_MAX_LENGTH
=nnn
" &
1125 " requires numeric parameter
");
1127 Error_Msg ("-k# requires numeric parameter
");
1135 if Hostparm.OpenVMS then
1136 Param := new String'("39");
1138 Param := new String'("8");
1143 new Argument_List'(Gnat_Args.all &
1144 new String'("-gnatk
" & Param.all));
1149 Preserve_Mode := True;
1155 Source_References := True;
1158 Verbose_Mode := True;
1160 -- Why is following written to standard error. Most other
1161 -- tools write to standard output ???
1163 Put (Standard_Error, "GNATCHOP
");
1164 Put_Line (Standard_Error, Gnatvsn.Gnat_Version_String);
1166 (Standard_Error, "Copyright
1998-2005, AdaCore
");
1169 Overwrite_Files := True;
1172 Exit_On_Error := True;
1179 if not Kset and then Maximum_File_Name_Length > 0 then
1181 -- If this system has restricted filename lengths, tell gnat1
1182 -- about them, removing the leading blank from the image string.
1185 new Argument_List'(Gnat_Args.all
1186 & new String'("-gnatk
"
1187 & Maximum_File_Name_Length_String
1188 (Maximum_File_Name_Length_String'First + 1
1189 .. Maximum_File_Name_Length_String'Last)));
1196 S : constant String := Get_Argument (Do_Expansion => True);
1200 File.Increment_Last;
1201 File.Table (File.Last).Name := new String'(S);
1202 File.Table (File.Last).SR_Name := null;
1206 -- Case of more than one file where last file is a directory
1209 and then Is_Directory (File.Table (File.Last).Name.all)
1211 Directory := File.Table (File.Last).Name;
1212 File.Decrement_Last;
1214 -- Make sure Directory is terminated with a directory separator,
1215 -- so we can generate the output by just appending a filename.
1217 if Directory (Directory'Last) /= Directory_Separator
1218 and then Directory (Directory'Last) /= '/'
1220 Directory := new String'(Directory.all & Directory_Separator);
1223 -- At least one filename must be given
1225 elsif File.Last = 0 then
1229 -- No directory given, set directory to null, so that we can just
1230 -- concatenate the directory name to the file name unconditionally.
1233 Directory := new String'("");
1236 -- Finally check all filename arguments
1238 for File_Num in 1 .. File.Last loop
1240 F : constant String := File.Table (File_Num).Name.all;
1244 if Is_Directory (F) then
1245 Error_Msg (F & " is a directory
, cannot be chopped
");
1248 elsif not Is_Regular_File (F) then
1249 Error_Msg (F & " not found
");
1258 when Invalid_Switch =>
1259 Error_Msg ("invalid switch
" & Full_Switch);
1262 when Invalid_Parameter =>
1263 if Hostparm.OpenVMS then
1264 Error_Msg ("/FILE_NAME_MAX_LENGTH
=nnn qualifier
" &
1265 " requires numeric parameter
");
1267 Error_Msg ("-k switch requires numeric parameter
");
1278 procedure Sort_Units is
1280 procedure Move (From : Natural; To : Natural);
1281 -- Procedure used to sort the unit list
1282 -- Unit.Table (To) := Unit_List (From); used by sort
1284 function Lt (Left, Right : Natural) return Boolean;
1285 -- Compares Left and Right units based on file name (first),
1286 -- Chop_File (second) and Offset (third). This ordering is
1287 -- important to keep the last version in case of duplicate files.
1289 package Unit_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1290 -- Used for sorting on filename to detect duplicates
1296 function Lt (Left, Right : Natural) return Boolean is
1297 L : Unit_Info renames
1298 Unit.Table (Sorted_Units.Table (SUnit_Num (Left)));
1300 R : Unit_Info renames
1301 Unit.Table (Sorted_Units.Table (SUnit_Num (Right)));
1304 return L.File_Name.all < R.File_Name.all
1305 or else (L.File_Name.all = R.File_Name.all
1306 and then (L.Chop_File < R.Chop_File
1307 or else (L.Chop_File = R.Chop_File
1308 and then L.Offset < R.Offset)));
1315 procedure Move (From : Natural; To : Natural) is
1317 Sorted_Units.Table (SUnit_Num (To)) :=
1318 Sorted_Units.Table (SUnit_Num (From));
1321 -- Start of processing for Sort_Units
1324 Sorted_Units.Set_Last (SUnit_Num (Unit.Last));
1326 for J in 1 .. Unit.Last loop
1327 Sorted_Units.Table (SUnit_Num (J)) := J;
1330 -- Sort Unit.Table, using Sorted_Units.Table (0) as scratch
1332 Unit_Sort.Sort (Natural (Unit.Last));
1334 -- Set the Sorted_Index fields in the unit tables
1336 for J in 1 .. SUnit_Num (Unit.Last) loop
1337 Unit.Table (Sorted_Units.Table (J)).Sorted_Index := J;
1348 ("Usage
: gnatchop
[-c
] [-h
] [-k#
] " &
1349 "[-r
] [-p
] [-q
] [-v
] [-w
] [-x
] [--GCC=xx] file [file ...] [dir]");
1353 (" -c compilation mode, configuration pragmas " &
1357 (" -gnatxxx passes the -gnatxxx switch to gnat parser");
1360 (" -h help: output this usage information");
1363 (" -k# krunch file names of generated files to " &
1364 "no more than # characters");
1367 (" -k krunch file names of generated files to " &
1368 "no more than 8 characters");
1371 (" -p preserve time stamp, output files will " &
1372 "have same stamp as input");
1375 (" -q quiet mode, no output of generated file " &
1379 (" -r generate Source_Reference pragmas refer" &
1380 "encing original source file");
1383 (" -v verbose mode, output version and generat" &
1387 (" -w overwrite existing filenames");
1390 (" -x exit on error");
1393 (" --GCC=xx specify the path of the gnat parser to be used");
1397 (" file... list of source files to be chopped");
1400 (" dir directory location for split files (defa" &
1401 "ult = current directory)");
1408 procedure Warning_Msg
(Message
: String) is
1410 Warning_Count
:= Warning_Count
+ 1;
1411 Put_Line
(Standard_Error
, "warning: " & Message
);
1414 -------------------------
1415 -- Write_Chopped_Files --
1416 -------------------------
1418 function Write_Chopped_Files
(Input
: File_Num
) return Boolean is
1419 Name
: aliased constant String :=
1420 File
.Table
(Input
).Name
.all & ASCII
.Nul
;
1421 FD
: File_Descriptor
;
1422 Buffer
: String_Access
;
1427 FD
:= Open_Read
(Name
'Address, Binary
);
1428 TS_Time
:= File_Time_Stamp
(FD
);
1430 if FD
= Invalid_FD
then
1431 Error_Msg
("cannot open " & File
.Table
(Input
).Name
.all);
1435 Read_File
(FD
, Buffer
, Success
);
1438 Error_Msg
("cannot read " & File
.Table
(Input
).Name
.all);
1443 if not Quiet_Mode
then
1444 Put_Line
("splitting " & File
.Table
(Input
).Name
.all & " into:");
1447 -- Only chop those units that come from this file
1449 for Num
in 1 .. Unit
.Last
loop
1450 if Unit
.Table
(Num
).Chop_File
= Input
then
1451 Write_Unit
(Buffer
, Num
, TS_Time
, Success
);
1452 exit when not Success
;
1459 end Write_Chopped_Files
;
1461 -----------------------
1462 -- Write_Config_File --
1463 -----------------------
1465 procedure Write_Config_File
(Input
: File_Num
; U
: Unit_Num
) is
1466 FD
: File_Descriptor
;
1467 Name
: aliased constant String := "gnat.adc" & ASCII
.NUL
;
1468 Buffer
: String_Access
;
1471 Buffera
: String_Access
;
1475 Write_gnat_adc
:= True;
1476 FD
:= Open_Read_Write
(Name
'Address, Binary
);
1478 if FD
= Invalid_FD
then
1479 FD
:= Create_File
(Name
'Address, Binary
);
1482 if not Quiet_Mode
then
1483 Put_Line
("writing configuration pragmas from " &
1484 File
.Table
(Input
).Name
.all & " to gnat.adc");
1490 if not Quiet_Mode
then
1492 ("appending configuration pragmas from " &
1493 File
.Table
(Input
).Name
.all & " to gnat.adc");
1497 Success
:= FD
/= Invalid_FD
;
1500 Error_Msg
("cannot create gnat.adc");
1504 -- In append mode, acquire existing gnat.adc file
1507 Read_File
(FD
, Buffera
, Success
);
1510 Error_Msg
("cannot read gnat.adc");
1514 -- Find location of EOF byte if any to exclude from append
1517 while Bufferl
<= Buffera
'Last
1518 and then Buffera
(Bufferl
) /= EOF
1520 Bufferl
:= Bufferl
+ 1;
1523 Bufferl
:= Bufferl
- 1;
1526 -- Write existing gnat.adc to new gnat.adc file
1528 FD
:= Create_File
(Name
'Address, Binary
);
1529 Success
:= Write
(FD
, Buffera
(1)'Address, Bufferl
) = Bufferl
;
1532 Error_Msg
("error writing gnat.adc");
1537 Buffer
:= Get_Config_Pragmas
(Input
, U
);
1539 if Buffer
/= null then
1540 Success
:= Write
(FD
, Buffer
.all'Address, Buffer
'Length) =
1544 Error_Msg
("disk full writing gnat.adc");
1550 end Write_Config_File
;
1552 -----------------------------------
1553 -- Write_Source_Reference_Pragma --
1554 -----------------------------------
1556 procedure Write_Source_Reference_Pragma
1559 FD
: File_Descriptor
;
1561 Success
: in out Boolean)
1563 FTE
: File_Entry
renames File
.Table
(Info
.Chop_File
);
1564 Nam
: String_Access
;
1567 if Success
and Source_References
and not Info
.SR_Present
then
1568 if FTE
.SR_Name
/= null then
1575 Reference
: aliased String :=
1576 "pragma Source_Reference (000000, """
1577 & Nam
.all & """);" & EOL
.Str
;
1579 Pos
: Positive := Reference
'First;
1580 Lin
: Line_Num
:= Line
;
1583 while Reference
(Pos
+ 1) /= ',' loop
1587 while Reference
(Pos
) = '0' loop
1588 Reference
(Pos
) := Character'Val
1589 (Character'Pos ('0') + Lin
mod 10);
1594 -- Assume there are enough zeroes for any program length
1596 pragma Assert
(Lin
= 0);
1599 Write
(FD
, Reference
'Address, Reference
'Length)
1603 end Write_Source_Reference_Pragma
;
1609 procedure Write_Unit
1610 (Source
: access String;
1613 Success
: out Boolean)
1615 Info
: Unit_Info
renames Unit
.Table
(Num
);
1616 FD
: File_Descriptor
;
1617 Name
: aliased constant String := Info
.File_Name
.all & ASCII
.NUL
;
1618 Length
: File_Offset
;
1619 EOL
: constant EOL_String
:=
1620 Get_EOL
(Source
, Source
'First + Info
.Offset
);
1623 -- Skip duplicated files
1625 if Is_Duplicated
(Info
.Sorted_Index
) then
1626 Put_Line
(" " & Info
.File_Name
.all & " skipped");
1627 Success
:= Overwrite_Files
;
1631 if Overwrite_Files
then
1632 FD
:= Create_File
(Name
'Address, Binary
);
1634 FD
:= Create_New_File
(Name
'Address, Binary
);
1637 Success
:= FD
/= Invalid_FD
;
1640 Error_Msg
("cannot create " & Info
.File_Name
.all);
1644 -- A length of 0 indicates that the rest of the file belongs to
1645 -- this unit. The actual length must be calculated now. Take into
1646 -- account that the last character (EOF) must not be written.
1648 if Info
.Length
= 0 then
1649 Length
:= Source
'Last - (Source
'First + Info
.Offset
);
1651 Length
:= Info
.Length
;
1654 -- Prepend configuration pragmas if necessary
1656 if Success
and then Info
.Bufferg
/= null then
1657 Write_Source_Reference_Pragma
(Info
, 1, FD
, EOL
, Success
);
1659 Write
(FD
, Info
.Bufferg
.all'Address, Info
.Bufferg
'Length) =
1660 Info
.Bufferg
'Length;
1663 Write_Source_Reference_Pragma
(Info
, Info
.Start_Line
, FD
, EOL
, Success
);
1666 Success
:= Write
(FD
, Source
(Source
'First + Info
.Offset
)'Address,
1671 Error_Msg
("disk full writing " & Info
.File_Name
.all);
1675 if not Quiet_Mode
then
1676 Put_Line
(" " & Info
.File_Name
.all);
1681 if Preserve_Mode
then
1682 File_Time_Stamp
(Name
'Address, TS_Time
);
1687 -- Start of processing for gnatchop
1690 -- Add the directory where gnatchop is invoked in front of the
1691 -- path, if gnatchop is invoked with directory information.
1692 -- Only do this if the platform is not VMS, where the notion of path
1693 -- does not really exist.
1695 if not Hostparm
.OpenVMS
then
1697 Command
: constant String := Command_Name
;
1700 for Index
in reverse Command
'Range loop
1701 if Command
(Index
) = Directory_Separator
then
1703 Absolute_Dir
: constant String :=
1705 (Command
(Command
'First .. Index
));
1707 PATH
: constant String :=
1710 Getenv
("PATH").all;
1713 Setenv
("PATH", PATH
);
1722 -- Process command line options and initialize global variables
1724 if not Scan_Arguments
then
1725 Set_Exit_Status
(Failure
);
1729 -- Check presence of required executables
1731 Gnat_Cmd
:= Locate_Executable
(Gcc
.all, not Gcc_Set
);
1733 if Gnat_Cmd
= null then
1734 goto No_Files_Written
;
1737 -- First parse all files and read offset information
1739 for Num
in 1 .. File
.Last
loop
1740 if not Parse_File
(Num
) then
1741 goto No_Files_Written
;
1745 -- Check if any units have been found (assumes non-empty Unit.Table)
1747 if Unit
.Last
= 0 then
1748 if not Write_gnat_adc
then
1749 Error_Msg
("no compilation units found", Warning
=> True);
1752 goto No_Files_Written
;
1757 -- Check if any duplicate files would be created. If so, emit
1758 -- a warning if Overwrite_Files is true, otherwise generate an error.
1760 if Report_Duplicate_Units
and then not Overwrite_Files
then
1761 goto No_Files_Written
;
1764 -- Check if any files exist, if so do not write anything
1765 -- Because all files have been parsed and checked already,
1766 -- there won't be any duplicates
1768 if not Overwrite_Files
and then Files_Exist
then
1769 goto No_Files_Written
;
1772 -- After this point, all source files are read in succession
1773 -- and chopped into their destination files.
1775 -- As the Source_File_Name pragmas are handled as logical file 0,
1778 for F
in 1 .. File
.Last
loop
1779 if not Write_Chopped_Files
(F
) then
1780 Set_Exit_Status
(Failure
);
1785 if Warning_Count
> 0 then
1787 Warnings_Msg
: constant String := Warning_Count
'Img & " warning(s)";
1789 Error_Msg
(Warnings_Msg
(2 .. Warnings_Msg
'Last), Warning
=> True);
1795 <<No_Files_Written
>>
1797 -- Special error exit for all situations where no files have
1800 if not Write_gnat_adc
then
1801 Error_Msg
("no source files written", Warning
=> True);
1807 when Terminate_Program
=>