1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 ------------------------------------------------------------------------------
29 with Gnatvsn
; use Gnatvsn
;
32 with Output
; use Output
;
33 with Sdefault
; use Sdefault
;
35 with Targparm
; use Targparm
;
37 with Unchecked_Conversion
;
39 pragma Warnings
(Off
);
40 -- This package is used also by gnatcoll
41 with System
.Case_Util
; use System
.Case_Util
;
51 Running_Program
: Program_Type
:= Unspecified
;
52 -- comment required here ???
54 Program_Set
: Boolean := False;
55 -- comment required here ???
57 Std_Prefix
: String_Ptr
;
58 -- Standard prefix, computed dynamically the first time Relocate_Path
59 -- is called, and cached for subsequent calls.
61 Empty
: aliased String := "";
62 No_Dir
: constant String_Ptr
:= Empty
'Access;
63 -- Used in Locate_File as a fake directory when Name is already an
66 -------------------------------------
67 -- Use of Name_Find and Name_Enter --
68 -------------------------------------
70 -- This package creates a number of source, ALI and object file names
71 -- that are used to locate the actual file and for the purpose of message
72 -- construction. These names need not be accessible by Name_Find, and can
73 -- be therefore created by using routine Name_Enter. The files in question
74 -- are file names with a prefix directory (i.e., the files not in the
75 -- current directory). File names without a prefix directory are entered
76 -- with Name_Find because special values might be attached to the various
77 -- Info fields of the corresponding name table entry.
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 function Append_Suffix_To_File_Name
84 (Name
: File_Name_Type
;
85 Suffix
: String) return File_Name_Type
;
86 -- Appends Suffix to Name and returns the new name
88 function OS_Time_To_GNAT_Time
(T
: OS_Time
) return Time_Stamp_Type
;
89 -- Convert OS format time to GNAT format time stamp. If T is Invalid_Time,
90 -- then returns Empty_Time_Stamp.
92 function Executable_Prefix
return String_Ptr
;
93 -- Returns the name of the root directory where the executable is stored.
94 -- The executable must be located in a directory called "bin", or under
95 -- root/lib/gcc-lib/..., or under root/libexec/gcc/... For example, if
96 -- executable is stored in directory "/foo/bar/bin", this routine returns
97 -- "/foo/bar/". Return "" if location is not recognized as described above.
99 function Update_Path
(Path
: String_Ptr
) return String_Ptr
;
100 -- Update the specified path to replace the prefix with the location where
101 -- GNAT is installed. See the file prefix.c in GCC for details.
103 procedure Locate_File
108 Found
: out File_Name_Type
;
109 Attr
: access File_Attributes
);
110 -- See if the file N whose name is Name exists in directory Dir. Dir is an
111 -- index into the Lib_Search_Directories table if T = Library. Otherwise
112 -- if T = Source, Dir is an index into the Src_Search_Directories table.
113 -- Returns the File_Name_Type of the full file name if file found, or
114 -- No_File if not found.
116 -- On exit, Found is set to the file that was found, and Attr to a cache of
117 -- its attributes (at least those that have been computed so far). Reusing
118 -- the cache will save some system calls.
120 -- Attr is always reset in this call to Unknown_Attributes, even in case of
126 Found
: out File_Name_Type
;
127 Attr
: access File_Attributes
;
128 Full_Name
: Boolean := False);
129 -- A version of Find_File that also returns a cache of the file attributes
132 procedure Smart_Find_File
135 Found
: out File_Name_Type
;
136 Attr
: out File_Attributes
);
137 -- A version of Smart_Find_File that also returns a cache of the file
138 -- attributes for later reuse
140 function C_String_Length
(S
: Address
) return CRTL
.size_t
;
141 -- Returns length of a C string (zero for a null address)
143 function To_Path_String_Access
144 (Path_Addr
: Address
;
145 Path_Len
: CRTL
.size_t
) return String_Access
;
146 -- Converts a C String to an Ada String. Are we doing this to avoid withing
147 -- Interfaces.C.Strings ???
148 -- Caller must free result.
150 function Include_Dir_Default_Prefix
return String_Access
;
151 -- Same as exported version, except returns a String_Access
153 ------------------------------
154 -- Other Local Declarations --
155 ------------------------------
157 EOL
: constant Character := ASCII
.LF
;
158 -- End of line character
160 Number_File_Names
: Nat
:= 0;
161 -- Number of file names found on command line and placed in File_Names
163 Look_In_Primary_Directory_For_Current_Main
: Boolean := False;
164 -- When this variable is True, Find_File only looks in Primary_Directory
165 -- for the Current_Main file. This variable is always set to True for the
166 -- compiler. It is also True for gnatmake, when the source name given on
167 -- the command line has directory information.
169 Current_Full_Source_Name
: File_Name_Type
:= No_File
;
170 Current_Full_Source_Stamp
: Time_Stamp_Type
:= Empty_Time_Stamp
;
171 Current_Full_Lib_Name
: File_Name_Type
:= No_File
;
172 Current_Full_Lib_Stamp
: Time_Stamp_Type
:= Empty_Time_Stamp
;
173 Current_Full_Obj_Name
: File_Name_Type
:= No_File
;
174 Current_Full_Obj_Stamp
: Time_Stamp_Type
:= Empty_Time_Stamp
;
175 -- Respectively full name (with directory info) and time stamp of the
176 -- latest source, library and object files opened by Read_Source_File and
177 -- Read_Library_Info.
179 package File_Name_Chars
is new Table
.Table
(
180 Table_Component_Type
=> Character,
181 Table_Index_Type
=> Int
,
182 Table_Low_Bound
=> 1,
183 Table_Initial
=> Alloc
.File_Name_Chars_Initial
,
184 Table_Increment
=> Alloc
.File_Name_Chars_Increment
,
185 Table_Name
=> "File_Name_Chars");
186 -- Table to store text to be printed by Dump_Source_File_Names
188 The_Include_Dir_Default_Prefix
: String_Access
:= null;
189 -- Value returned by Include_Dir_Default_Prefix. We don't initialize it
190 -- here, because that causes an elaboration cycle with Sdefault; we
191 -- initialize it lazily instead.
197 Primary_Directory
: constant := 0;
198 -- This is index in the tables created below for the first directory to
199 -- search in for source or library information files. This is the directory
200 -- containing the latest main input file (a source file for the compiler or
201 -- a library file for the binder).
203 package Src_Search_Directories
is new Table
.Table
(
204 Table_Component_Type
=> String_Ptr
,
205 Table_Index_Type
=> Integer,
206 Table_Low_Bound
=> Primary_Directory
,
208 Table_Increment
=> 100,
209 Table_Name
=> "Osint.Src_Search_Directories");
210 -- Table of names of directories in which to search for source (Compiler)
211 -- files. This table is filled in the order in which the directories are
212 -- to be searched, and then used in that order.
214 package Lib_Search_Directories
is new Table
.Table
(
215 Table_Component_Type
=> String_Ptr
,
216 Table_Index_Type
=> Integer,
217 Table_Low_Bound
=> Primary_Directory
,
219 Table_Increment
=> 100,
220 Table_Name
=> "Osint.Lib_Search_Directories");
221 -- Table of names of directories in which to search for library (Binder)
222 -- files. This table is filled in the order in which the directories are
223 -- to be searched and then used in that order. The reason for having two
224 -- distinct tables is that we need them both in gnatmake.
226 ---------------------
227 -- File Hash Table --
228 ---------------------
230 -- The file hash table is provided to free the programmer from any
231 -- efficiency concern when retrieving full file names or time stamps of
232 -- source files. If the programmer calls Source_File_Data (Cache => True)
233 -- he is guaranteed that the price to retrieve the full name (i.e. with
234 -- directory info) or time stamp of the file will be payed only once, the
235 -- first time the full name is actually searched (or the first time the
236 -- time stamp is actually retrieved). This is achieved by employing a hash
237 -- table that stores as a key the File_Name_Type of the file and associates
238 -- to that File_Name_Type the full file name and time stamp of the file.
240 File_Cache_Enabled
: Boolean := False;
241 -- Set to true if you want the enable the file data caching mechanism
243 type File_Hash_Num
is range 0 .. 1020;
245 function File_Hash
(F
: File_Name_Type
) return File_Hash_Num
;
246 -- Compute hash index for use by Simple_HTable
248 type File_Info_Cache
is record
249 File
: File_Name_Type
;
250 Attr
: aliased File_Attributes
;
253 No_File_Info_Cache
: constant File_Info_Cache
:=
254 (No_File
, Unknown_Attributes
);
256 package File_Name_Hash_Table
is new GNAT
.HTable
.Simple_HTable
(
257 Header_Num
=> File_Hash_Num
,
258 Element
=> File_Info_Cache
,
259 No_Element
=> No_File_Info_Cache
,
260 Key
=> File_Name_Type
,
264 function Smart_Find_File
266 T
: File_Type
) return File_Name_Type
;
267 -- Exactly like Find_File except that if File_Cache_Enabled is True this
268 -- routine looks first in the hash table to see if the full name of the
269 -- file is already available.
271 function Smart_File_Stamp
273 T
: File_Type
) return Time_Stamp_Type
;
274 -- Takes the same parameter as the routine above (N is a file name without
275 -- any prefix directory information) and behaves like File_Stamp except
276 -- that if File_Cache_Enabled is True this routine looks first in the hash
277 -- table to see if the file stamp of the file is already available.
279 -----------------------------
280 -- Add_Default_Search_Dirs --
281 -----------------------------
283 procedure Add_Default_Search_Dirs
is
284 Search_Dir
: String_Access
;
285 Search_Path
: String_Access
;
286 Path_File_Name
: String_Access
;
288 procedure Add_Search_Dir
289 (Search_Dir
: String;
290 Additional_Source_Dir
: Boolean);
291 procedure Add_Search_Dir
292 (Search_Dir
: String_Access
;
293 Additional_Source_Dir
: Boolean);
294 -- Add a source search dir or a library search dir, depending on the
295 -- value of Additional_Source_Dir.
297 procedure Get_Dirs_From_File
(Additional_Source_Dir
: Boolean);
298 -- Open a path file and read the directory to search, one per line
300 function Get_Libraries_From_Registry
return String_Ptr
;
301 -- On Windows systems, get the list of installed standard libraries
302 -- from the registry key:
304 -- HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\
305 -- GNAT\Standard Libraries
306 -- Return an empty string on other systems.
308 -- Note that this is an undocumented legacy feature, and that it
309 -- works only when using the default runtime library (i.e. no --RTS=
310 -- command line switch).
316 procedure Add_Search_Dir
317 (Search_Dir
: String;
318 Additional_Source_Dir
: Boolean)
321 if Additional_Source_Dir
then
322 Add_Src_Search_Dir
(Search_Dir
);
324 Add_Lib_Search_Dir
(Search_Dir
);
328 procedure Add_Search_Dir
329 (Search_Dir
: String_Access
;
330 Additional_Source_Dir
: Boolean)
333 if Additional_Source_Dir
then
334 Add_Src_Search_Dir
(Search_Dir
.all);
336 Add_Lib_Search_Dir
(Search_Dir
.all);
340 ------------------------
341 -- Get_Dirs_From_File --
342 ------------------------
344 procedure Get_Dirs_From_File
(Additional_Source_Dir
: Boolean) is
345 File_FD
: File_Descriptor
;
346 Buffer
: constant String := Path_File_Name
.all & ASCII
.NUL
;
348 Actual_Len
: Natural;
355 pragma Warnings
(Off
, Status
);
356 -- For the call to Close where status is ignored
359 File_FD
:= Open_Read
(Buffer
'Address, Binary
);
361 -- If we cannot open the file, we ignore it, we don't fail
363 if File_FD
= Invalid_FD
then
367 Len
:= Integer (File_Length
(File_FD
));
369 S
:= new String (1 .. Len
);
371 -- Read the file. Note that the loop is probably not necessary any
372 -- more since the whole file is read in at once on all targets. But
373 -- it is harmless and might be needed in future.
377 while Curr
<= Len
and then Actual_Len
/= 0 loop
378 Actual_Len
:= Read
(File_FD
, S
(Curr
)'Address, Len
);
379 Curr
:= Curr
+ Actual_Len
;
382 -- We are done with the file, so we close it (ignore any error on
383 -- the close, since we have successfully read the file).
385 Close
(File_FD
, Status
);
387 -- Now, we read line by line
391 while Curr
< Len
loop
394 if Ch
= ASCII
.CR
or else Ch
= ASCII
.LF
395 or else Ch
= ASCII
.FF
or else Ch
= ASCII
.VT
397 if First
<= Curr
then
398 Add_Search_Dir
(S
(First
.. Curr
), Additional_Source_Dir
);
407 -- Last line is a special case, if the file does not end with
408 -- an end of line mark.
410 if First
<= S
'Last then
411 Add_Search_Dir
(S
(First
.. S
'Last), Additional_Source_Dir
);
413 end Get_Dirs_From_File
;
415 ---------------------------------
416 -- Get_Libraries_From_Registry --
417 ---------------------------------
419 function Get_Libraries_From_Registry
return String_Ptr
is
420 function C_Get_Libraries_From_Registry
return Address
;
421 pragma Import
(C
, C_Get_Libraries_From_Registry
,
422 "__gnat_get_libraries_from_registry");
424 Result_Ptr
: Address
;
425 Result_Length
: CRTL
.size_t
;
426 Out_String
: String_Ptr
;
429 Result_Ptr
:= C_Get_Libraries_From_Registry
;
430 Result_Length
:= CRTL
.strlen
(Result_Ptr
);
432 Out_String
:= new String (1 .. Integer (Result_Length
));
433 CRTL
.strncpy
(Out_String
.all'Address, Result_Ptr
, Result_Length
);
435 CRTL
.free
(Result_Ptr
);
438 end Get_Libraries_From_Registry
;
440 -- Start of processing for Add_Default_Search_Dirs
443 -- If there was a -gnateO switch, add all object directories from the
444 -- file given in argument to the library search list.
446 if Object_Path_File_Name
/= null then
447 Path_File_Name
:= String_Access
(Object_Path_File_Name
);
448 pragma Assert
(Path_File_Name
'Length > 0);
449 Get_Dirs_From_File
(Additional_Source_Dir
=> False);
452 -- After the locations specified on the command line, the next places
453 -- to look for files are the directories specified by the appropriate
454 -- environment variable. Get this value, extract the directory names
455 -- and store in the tables.
457 -- Check for eventual project path file env vars
459 Path_File_Name
:= Getenv
(Project_Include_Path_File
);
461 if Path_File_Name
'Length > 0 then
462 Get_Dirs_From_File
(Additional_Source_Dir
=> True);
465 Path_File_Name
:= Getenv
(Project_Objects_Path_File
);
467 if Path_File_Name
'Length > 0 then
468 Get_Dirs_From_File
(Additional_Source_Dir
=> False);
471 -- Put path name in canonical form
473 for Additional_Source_Dir
in False .. True loop
474 if Additional_Source_Dir
then
475 Search_Path
:= Getenv
(Ada_Include_Path
);
478 Search_Path
:= Getenv
(Ada_Objects_Path
);
482 Get_Next_Dir_In_Path_Init
(Search_Path
);
484 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
485 exit when Search_Dir
= null;
486 Add_Search_Dir
(Search_Dir
, Additional_Source_Dir
);
490 -- For the compiler, if --RTS= was specified, add the runtime
493 if RTS_Src_Path_Name
/= null and then RTS_Lib_Path_Name
/= null then
494 Add_Search_Dirs
(RTS_Src_Path_Name
, Include
);
495 Add_Search_Dirs
(RTS_Lib_Path_Name
, Objects
);
498 if not Opt
.No_Stdinc
then
500 -- For WIN32 systems, look for any system libraries defined in
501 -- the registry. These are added to both source and object
504 Search_Path
:= String_Access
(Get_Libraries_From_Registry
);
506 Get_Next_Dir_In_Path_Init
(Search_Path
);
508 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
509 exit when Search_Dir
= null;
510 Add_Search_Dir
(Search_Dir
, False);
511 Add_Search_Dir
(Search_Dir
, True);
514 -- The last place to look are the defaults
517 Read_Default_Search_Dirs
518 (String_Access
(Update_Path
(Search_Dir_Prefix
)),
520 String_Access
(Update_Path
(Include_Dir_Default_Name
)));
522 Get_Next_Dir_In_Path_Init
(Search_Path
);
524 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
525 exit when Search_Dir
= null;
526 Add_Search_Dir
(Search_Dir
, True);
530 -- Even when -nostdlib is used, we still want to have visibility on
531 -- the run-time object directory, as it is used by gnatbind to find
532 -- the run-time ALI files in "real" ZFP set up.
534 if not Opt
.RTS_Switch
then
536 Read_Default_Search_Dirs
537 (String_Access
(Update_Path
(Search_Dir_Prefix
)),
539 String_Access
(Update_Path
(Object_Dir_Default_Name
)));
541 Get_Next_Dir_In_Path_Init
(Search_Path
);
543 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
544 exit when Search_Dir
= null;
545 Add_Search_Dir
(Search_Dir
, False);
549 end Add_Default_Search_Dirs
;
555 procedure Add_File
(File_Name
: String; Index
: Int
:= No_Index
) is
557 Number_File_Names
:= Number_File_Names
+ 1;
559 -- As Add_File may be called for mains specified inside a project file,
560 -- File_Names may be too short and needs to be extended.
562 if Number_File_Names
> File_Names
'Last then
563 File_Names
:= new File_Name_Array
'(File_Names.all & File_Names.all);
565 new File_Index_Array'(File_Indexes
.all & File_Indexes
.all);
568 File_Names
(Number_File_Names
) := new String'(File_Name);
569 File_Indexes (Number_File_Names) := Index;
572 ------------------------
573 -- Add_Lib_Search_Dir --
574 ------------------------
576 procedure Add_Lib_Search_Dir (Dir : String) is
578 if Dir'Length = 0 then
579 Fail ("missing library directory name");
583 Norm : String_Ptr := Normalize_Directory_Name (Dir);
586 -- Do nothing if the directory is already in the list. This saves
587 -- system calls and avoid unneeded work
589 for D in Lib_Search_Directories.First ..
590 Lib_Search_Directories.Last
592 if Lib_Search_Directories.Table (D).all = Norm.all then
598 Lib_Search_Directories.Increment_Last;
599 Lib_Search_Directories.Table (Lib_Search_Directories.Last) := Norm;
601 end Add_Lib_Search_Dir;
603 ---------------------
604 -- Add_Search_Dirs --
605 ---------------------
607 procedure Add_Search_Dirs
608 (Search_Path : String_Ptr;
609 Path_Type : Search_File_Type)
611 Current_Search_Path : String_Access;
614 Get_Next_Dir_In_Path_Init (String_Access (Search_Path));
616 Current_Search_Path :=
617 Get_Next_Dir_In_Path (String_Access (Search_Path));
618 exit when Current_Search_Path = null;
620 if Path_Type = Include then
621 Add_Src_Search_Dir (Current_Search_Path.all);
623 Add_Lib_Search_Dir (Current_Search_Path.all);
628 ------------------------
629 -- Add_Src_Search_Dir --
630 ------------------------
632 procedure Add_Src_Search_Dir (Dir : String) is
634 if Dir'Length = 0 then
635 Fail ("missing source directory name");
638 Src_Search_Directories.Increment_Last;
639 Src_Search_Directories.Table (Src_Search_Directories.Last) :=
640 Normalize_Directory_Name (Dir);
641 end Add_Src_Search_Dir;
643 --------------------------------
644 -- Append_Suffix_To_File_Name --
645 --------------------------------
647 function Append_Suffix_To_File_Name
648 (Name : File_Name_Type;
649 Suffix : String) return File_Name_Type
652 Get_Name_String (Name);
653 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
654 Name_Len := Name_Len + Suffix'Length;
656 end Append_Suffix_To_File_Name;
658 ---------------------
659 -- C_String_Length --
660 ---------------------
662 function C_String_Length (S : Address) return CRTL.size_t is
664 if S = Null_Address then
667 return CRTL.strlen (S);
671 ------------------------------
672 -- Canonical_Case_File_Name --
673 ------------------------------
675 procedure Canonical_Case_File_Name (S : in out String) is
677 if not File_Names_Case_Sensitive then
680 end Canonical_Case_File_Name;
682 ---------------------------------
683 -- Canonical_Case_Env_Var_Name --
684 ---------------------------------
686 procedure Canonical_Case_Env_Var_Name (S : in out String) is
688 if not Env_Vars_Case_Sensitive then
691 end Canonical_Case_Env_Var_Name;
693 ---------------------------
694 -- Create_File_And_Check --
695 ---------------------------
697 procedure Create_File_And_Check
698 (Fdesc : out File_Descriptor;
702 Output_File_Name := Name_Enter;
703 Fdesc := Create_File (Name_Buffer'Address, Fmode);
705 if Fdesc = Invalid_FD then
706 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
708 end Create_File_And_Check;
710 -----------------------------------
711 -- Open_File_To_Append_And_Check --
712 -----------------------------------
714 procedure Open_File_To_Append_And_Check
715 (Fdesc : out File_Descriptor;
719 Output_File_Name := Name_Enter;
720 Fdesc := Open_Append (Name_Buffer'Address, Fmode);
722 if Fdesc = Invalid_FD then
723 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
725 end Open_File_To_Append_And_Check;
727 ------------------------
728 -- Current_File_Index --
729 ------------------------
731 function Current_File_Index return Int is
733 return File_Indexes (Current_File_Name_Index);
734 end Current_File_Index;
736 --------------------------------
737 -- Current_Library_File_Stamp --
738 --------------------------------
740 function Current_Library_File_Stamp return Time_Stamp_Type is
742 return Current_Full_Lib_Stamp;
743 end Current_Library_File_Stamp;
745 -------------------------------
746 -- Current_Object_File_Stamp --
747 -------------------------------
749 function Current_Object_File_Stamp return Time_Stamp_Type is
751 return Current_Full_Obj_Stamp;
752 end Current_Object_File_Stamp;
754 -------------------------------
755 -- Current_Source_File_Stamp --
756 -------------------------------
758 function Current_Source_File_Stamp return Time_Stamp_Type is
760 return Current_Full_Source_Stamp;
761 end Current_Source_File_Stamp;
763 ----------------------------
764 -- Dir_In_Obj_Search_Path --
765 ----------------------------
767 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr is
769 if Opt.Look_In_Primary_Dir then
771 Lib_Search_Directories.Table (Primary_Directory + Position - 1);
773 return Lib_Search_Directories.Table (Primary_Directory + Position);
775 end Dir_In_Obj_Search_Path;
777 ----------------------------
778 -- Dir_In_Src_Search_Path --
779 ----------------------------
781 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr is
783 if Opt.Look_In_Primary_Dir then
785 Src_Search_Directories.Table (Primary_Directory + Position - 1);
787 return Src_Search_Directories.Table (Primary_Directory + Position);
789 end Dir_In_Src_Search_Path;
791 ----------------------------
792 -- Dump_Source_File_Names --
793 ----------------------------
795 procedure Dump_Source_File_Names is
796 subtype Rng is Int range File_Name_Chars.First .. File_Name_Chars.Last;
798 Write_Str (String (File_Name_Chars.Table (Rng)));
799 end Dump_Source_File_Names;
801 ---------------------
802 -- Executable_Name --
803 ---------------------
805 function Executable_Name
806 (Name : File_Name_Type;
807 Only_If_No_Suffix : Boolean := False) return File_Name_Type
809 Exec_Suffix : String_Access;
810 Add_Suffix : Boolean;
813 if Name = No_File then
817 if Executable_Extension_On_Target = No_Name then
818 Exec_Suffix := Get_Target_Executable_Suffix;
820 Get_Name_String (Executable_Extension_On_Target);
821 Exec_Suffix := new String'(Name_Buffer
(1 .. Name_Len
));
824 if Exec_Suffix
'Length /= 0 then
825 Get_Name_String
(Name
);
828 if Only_If_No_Suffix
then
829 for J
in reverse 1 .. Name_Len
loop
830 if Name_Buffer
(J
) = '.' then
834 elsif Name_Buffer
(J
) = '/' or else
835 Name_Buffer
(J
) = Directory_Separator
844 Buffer
: String := Name_Buffer
(1 .. Name_Len
);
847 -- Get the file name in canonical case to accept as is. Names
848 -- end with ".EXE" on Windows.
850 Canonical_Case_File_Name
(Buffer
);
852 -- If Executable doesn't end with the executable suffix, add it
854 if Buffer
'Length <= Exec_Suffix
'Length
856 Buffer
(Buffer
'Last - Exec_Suffix
'Length + 1 .. Buffer
'Last)
860 (Name_Len
+ 1 .. Name_Len
+ Exec_Suffix
'Length) :=
862 Name_Len
:= Name_Len
+ Exec_Suffix
'Length;
874 function Executable_Name
876 Only_If_No_Suffix
: Boolean := False) return String
878 Exec_Suffix
: String_Access
;
879 Add_Suffix
: Boolean;
880 Canonical_Name
: String := Name
;
883 if Executable_Extension_On_Target
= No_Name
then
884 Exec_Suffix
:= Get_Target_Executable_Suffix
;
886 Get_Name_String
(Executable_Extension_On_Target
);
887 Exec_Suffix
:= new String'(Name_Buffer (1 .. Name_Len));
890 if Exec_Suffix'Length = 0 then
896 Suffix : constant String := Exec_Suffix.all;
900 Canonical_Case_File_Name (Canonical_Name);
903 if Only_If_No_Suffix then
904 for J in reverse Canonical_Name'Range loop
905 if Canonical_Name (J) = '.' then
909 elsif Canonical_Name (J) = '/' or else
910 Canonical_Name (J) = Directory_Separator
917 if Add_Suffix and then
918 (Canonical_Name'Length <= Suffix'Length
919 or else Canonical_Name (Canonical_Name'Last - Suffix'Length + 1
920 .. Canonical_Name'Last) /= Suffix)
923 Result : String (1 .. Name'Length + Suffix'Length);
925 Result (1 .. Name'Length) := Name;
926 Result (Name'Length + 1 .. Result'Last) := Suffix;
936 -----------------------
937 -- Executable_Prefix --
938 -----------------------
940 function Executable_Prefix return String_Ptr is
942 function Get_Install_Dir (Exec : String) return String_Ptr;
943 -- S is the executable name preceded by the absolute or relative
944 -- path, e.g. "c:\usr\bin\gcc.exe" or "..\bin\gcc".
946 ---------------------
947 -- Get_Install_Dir --
948 ---------------------
950 function Get_Install_Dir (Exec : String) return String_Ptr is
951 Full_Path : constant String := Normalize_Pathname (Exec);
952 -- Use the full path, so that we find "lib" or "bin", even when
953 -- the tool has been invoked with a relative path, as in
954 -- "./gnatls -v" invoked in the GNAT bin directory.
957 for J in reverse Full_Path'Range loop
958 if Is_Directory_Separator (Full_Path (J)) then
959 if J < Full_Path'Last - 5 then
960 if (To_Lower (Full_Path (J + 1)) = 'l
'
961 and then To_Lower (Full_Path (J + 2)) = 'i
'
962 and then To_Lower (Full_Path (J + 3)) = 'b
')
964 (To_Lower (Full_Path (J + 1)) = 'b
'
965 and then To_Lower (Full_Path (J + 2)) = 'i
'
966 and then To_Lower (Full_Path (J + 3)) = 'n
')
968 return new String'(Full_Path
(Full_Path
'First .. J
));
974 return new String'("");
977 -- Start of processing for Executable_Prefix
980 if Exec_Name = null then
981 Exec_Name := new String (1 .. Len_Arg (0));
982 Osint.Fill_Arg (Exec_Name (1)'Address, 0);
985 -- First determine if a path prefix was placed in front of the
988 for J in reverse Exec_Name'Range loop
989 if Is_Directory_Separator (Exec_Name (J)) then
990 return Get_Install_Dir (Exec_Name.all);
994 -- If we come here, the user has typed the executable name with no
997 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name.all).all);
998 end Executable_Prefix;
1004 procedure Exit_Program (Exit_Code : Exit_Code_Type) is
1006 -- The program will exit with the following status:
1008 -- 0 if the object file has been generated (with or without warnings)
1009 -- 1 if recompilation was not needed (smart recompilation)
1010 -- 2 if gnat1 has been killed by a signal (detected by GCC)
1011 -- 4 for a fatal error
1012 -- 5 if there were errors
1013 -- 6 if no code has been generated (spec)
1015 -- Note that exit code 3 is not used and must not be used as this is
1016 -- the code returned by a program aborted via C abort() routine on
1017 -- Windows. GCC checks for that case and thinks that the child process
1018 -- has been aborted. This code (exit code 3) used to be the code used
1019 -- for E_No_Code, but E_No_Code was changed to 6 for this reason.
1022 when E_Success => OS_Exit (0);
1023 when E_Warnings => OS_Exit (0);
1024 when E_No_Compile => OS_Exit (1);
1025 when E_Fatal => OS_Exit (4);
1026 when E_Errors => OS_Exit (5);
1027 when E_No_Code => OS_Exit (6);
1028 when E_Abort => OS_Abort;
1036 procedure Fail (S : String) is
1038 -- We use Output in case there is a special output set up. In this case
1039 -- Set_Standard_Error will have no immediate effect.
1042 Osint.Write_Program_Name;
1047 Exit_Program (E_Fatal);
1054 function File_Hash (F : File_Name_Type) return File_Hash_Num is
1056 return File_Hash_Num (Int (F) rem File_Hash_Num'Range_Length);
1063 function File_Length
1064 (Name : C_File_Name;
1065 Attr : access File_Attributes) return Long_Integer
1070 A : System.Address) return CRTL.int64;
1071 pragma Import (C, Internal, "__gnat_file_length_attr");
1074 -- The conversion from int64 to Long_Integer is ok here as this
1075 -- routine is only to be used by the compiler and we do not expect
1076 -- a unit to be larger than a 32bit integer.
1078 return Long_Integer (Internal (-1, Name, Attr.all'Address));
1081 ---------------------
1082 -- File_Time_Stamp --
1083 ---------------------
1085 function File_Time_Stamp
1086 (Name : C_File_Name;
1087 Attr : access File_Attributes) return OS_Time
1089 function Internal (N : C_File_Name; A : System.Address) return OS_Time;
1090 pragma Import (C, Internal, "__gnat_file_time_name_attr");
1092 return Internal (Name, Attr.all'Address);
1093 end File_Time_Stamp;
1095 function File_Time_Stamp
1096 (Name : Path_Name_Type;
1097 Attr : access File_Attributes) return Time_Stamp_Type
1100 if Name = No_Path then
1101 return Empty_Time_Stamp;
1104 Get_Name_String (Name);
1105 Name_Buffer (Name_Len + 1) := ASCII.NUL;
1106 return OS_Time_To_GNAT_Time
1107 (File_Time_Stamp (Name_Buffer'Address, Attr));
1108 end File_Time_Stamp;
1114 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type is
1116 if Name = No_File then
1117 return Empty_Time_Stamp;
1120 Get_Name_String (Name);
1122 -- File_Time_Stamp will always return Invalid_Time if the file does
1123 -- not exist, and OS_Time_To_GNAT_Time will convert this value to
1124 -- Empty_Time_Stamp. Therefore we do not need to first test whether
1125 -- the file actually exists, which saves a system call.
1127 return OS_Time_To_GNAT_Time
1128 (File_Time_Stamp (Name_Buffer (1 .. Name_Len)));
1131 function File_Stamp (Name : Path_Name_Type) return Time_Stamp_Type is
1133 return File_Stamp (File_Name_Type (Name));
1141 (N : File_Name_Type;
1143 Full_Name : Boolean := False) return File_Name_Type
1145 Attr : aliased File_Attributes;
1146 Found : File_Name_Type;
1148 Find_File (N, T, Found, Attr'Access, Full_Name);
1157 (N : File_Name_Type;
1159 Found : out File_Name_Type;
1160 Attr : access File_Attributes;
1161 Full_Name : Boolean := False)
1164 Get_Name_String (N);
1167 File_Name : String renames Name_Buffer (1 .. Name_Len);
1168 File : File_Name_Type := No_File;
1172 -- If we are looking for a config file, look only in the current
1173 -- directory, i.e. return input argument unchanged. Also look only in
1174 -- the current directory if we are looking for a .dg file (happens in
1178 or else (Debug_Generated_Code
1179 and then Name_Len > 3
1180 and then Name_Buffer (Name_Len - 2 .. Name_Len) = ".dg")
1183 Attr.all := Unknown_Attributes;
1188 Full_Path : constant String :=
1189 Normalize_Pathname (Get_Name_String (N));
1190 Full_Size : constant Natural := Full_Path'Length;
1193 Name_Buffer (1 .. Full_Size) := Full_Path;
1194 Name_Len := Full_Size;
1199 -- Check that it is a file, not a directory
1201 if not Is_Regular_File (Get_Name_String (Found)) then
1208 -- If we are trying to find the current main file just look in the
1209 -- directory where the user said it was.
1211 elsif Look_In_Primary_Directory_For_Current_Main
1212 and then Current_Main = N
1214 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1217 -- Otherwise do standard search for source file
1220 -- Check the mapping of this file name
1222 File := Mapped_Path_Name (N);
1224 -- If the file name is mapped to a path name, return the
1225 -- corresponding path name
1227 if File /= No_File then
1229 -- For locally removed file, Error_Name is returned; then
1230 -- return No_File, indicating the file is not a source.
1232 if File = Error_File_Name then
1238 Attr.all := Unknown_Attributes;
1242 -- First place to look is in the primary directory (i.e. the same
1243 -- directory as the source) unless this has been disabled with -I-
1245 if Opt.Look_In_Primary_Dir then
1246 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1248 if Found /= No_File then
1253 -- Finally look in directories specified with switches -I/-aI/-aO
1256 Last_Dir := Lib_Search_Directories.Last;
1258 Last_Dir := Src_Search_Directories.Last;
1261 for D in Primary_Directory + 1 .. Last_Dir loop
1262 Locate_File (N, T, D, File_Name, Found, Attr);
1264 if Found /= No_File then
1269 Attr.all := Unknown_Attributes;
1275 -----------------------
1276 -- Find_Program_Name --
1277 -----------------------
1279 procedure Find_Program_Name is
1280 Command_Name : String (1 .. Len_Arg (0));
1281 Cindex1 : Integer := Command_Name'First;
1282 Cindex2 : Integer := Command_Name'Last;
1285 Fill_Arg (Command_Name'Address, 0);
1287 if Command_Name = "" then
1292 -- The program name might be specified by a full path name. However,
1293 -- we don't want to print that all out in an error message, so the
1294 -- path might need to be stripped away.
1296 for J in reverse Cindex1 .. Cindex2 loop
1297 if Is_Directory_Separator (Command_Name (J)) then
1303 -- Command_Name(Cindex1 .. Cindex2) is now the equivalent of the
1304 -- POSIX command "basename argv[0]"
1306 -- Strip off any executable extension (usually nothing or .exe)
1307 -- but formally reported by autoconf in the variable EXEEXT
1309 if Cindex2 - Cindex1 >= 4 then
1310 if To_Lower (Command_Name (Cindex2 - 3)) = '.'
1311 and then To_Lower (Command_Name (Cindex2 - 2)) = 'e
'
1312 and then To_Lower (Command_Name (Cindex2 - 1)) = 'x
'
1313 and then To_Lower (Command_Name (Cindex2)) = 'e
'
1315 Cindex2 := Cindex2 - 4;
1319 Name_Len := Cindex2 - Cindex1 + 1;
1320 Name_Buffer (1 .. Name_Len) := Command_Name (Cindex1 .. Cindex2);
1321 end Find_Program_Name;
1323 ------------------------
1324 -- Full_Lib_File_Name --
1325 ------------------------
1327 procedure Full_Lib_File_Name
1328 (N : File_Name_Type;
1329 Lib_File : out File_Name_Type;
1330 Attr : out File_Attributes)
1332 A : aliased File_Attributes;
1334 -- ??? seems we could use Smart_Find_File here
1335 Find_File (N, Library, Lib_File, A'Access);
1337 end Full_Lib_File_Name;
1339 ------------------------
1340 -- Full_Lib_File_Name --
1341 ------------------------
1343 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type is
1344 Attr : File_Attributes;
1345 File : File_Name_Type;
1347 Full_Lib_File_Name (N, File, Attr);
1349 end Full_Lib_File_Name;
1351 ----------------------------
1352 -- Full_Library_Info_Name --
1353 ----------------------------
1355 function Full_Library_Info_Name return File_Name_Type is
1357 return Current_Full_Lib_Name;
1358 end Full_Library_Info_Name;
1360 ---------------------------
1361 -- Full_Object_File_Name --
1362 ---------------------------
1364 function Full_Object_File_Name return File_Name_Type is
1366 return Current_Full_Obj_Name;
1367 end Full_Object_File_Name;
1369 ----------------------
1370 -- Full_Source_Name --
1371 ----------------------
1373 function Full_Source_Name return File_Name_Type is
1375 return Current_Full_Source_Name;
1376 end Full_Source_Name;
1378 ----------------------
1379 -- Full_Source_Name --
1380 ----------------------
1382 function Full_Source_Name (N : File_Name_Type) return File_Name_Type is
1384 return Smart_Find_File (N, Source);
1385 end Full_Source_Name;
1387 ----------------------
1388 -- Full_Source_Name --
1389 ----------------------
1391 procedure Full_Source_Name
1392 (N : File_Name_Type;
1393 Full_File : out File_Name_Type;
1394 Attr : access File_Attributes) is
1396 Smart_Find_File (N, Source, Full_File, Attr.all);
1397 end Full_Source_Name;
1403 function Get_Directory (Name : File_Name_Type) return File_Name_Type is
1405 Get_Name_String (Name);
1407 for J in reverse 1 .. Name_Len loop
1408 if Is_Directory_Separator (Name_Buffer (J)) then
1414 Name_Len := Hostparm.Normalized_CWD'Length;
1415 Name_Buffer (1 .. Name_Len) := Hostparm.Normalized_CWD;
1419 --------------------------
1420 -- Get_Next_Dir_In_Path --
1421 --------------------------
1423 Search_Path_Pos : Integer;
1424 -- Keeps track of current position in search path. Initialized by the
1425 -- call to Get_Next_Dir_In_Path_Init, updated by Get_Next_Dir_In_Path.
1427 function Get_Next_Dir_In_Path
1428 (Search_Path : String_Access) return String_Access
1430 Lower_Bound : Positive := Search_Path_Pos;
1431 Upper_Bound : Positive;
1435 while Lower_Bound <= Search_Path'Last
1436 and then Search_Path.all (Lower_Bound) = Path_Separator
1438 Lower_Bound := Lower_Bound + 1;
1441 exit when Lower_Bound > Search_Path'Last;
1443 Upper_Bound := Lower_Bound;
1444 while Upper_Bound <= Search_Path'Last
1445 and then Search_Path.all (Upper_Bound) /= Path_Separator
1447 Upper_Bound := Upper_Bound + 1;
1450 Search_Path_Pos := Upper_Bound;
1451 return new String'(Search_Path
.all (Lower_Bound
.. Upper_Bound
- 1));
1455 end Get_Next_Dir_In_Path
;
1457 -------------------------------
1458 -- Get_Next_Dir_In_Path_Init --
1459 -------------------------------
1461 procedure Get_Next_Dir_In_Path_Init
(Search_Path
: String_Access
) is
1463 Search_Path_Pos
:= Search_Path
'First;
1464 end Get_Next_Dir_In_Path_Init
;
1466 --------------------------------------
1467 -- Get_Primary_Src_Search_Directory --
1468 --------------------------------------
1470 function Get_Primary_Src_Search_Directory
return String_Ptr
is
1472 return Src_Search_Directories
.Table
(Primary_Directory
);
1473 end Get_Primary_Src_Search_Directory
;
1475 ------------------------
1476 -- Get_RTS_Search_Dir --
1477 ------------------------
1479 function Get_RTS_Search_Dir
1480 (Search_Dir
: String;
1481 File_Type
: Search_File_Type
) return String_Ptr
1483 procedure Get_Current_Dir
1484 (Dir
: System
.Address
;
1485 Length
: System
.Address
);
1486 pragma Import
(C
, Get_Current_Dir
, "__gnat_get_current_dir");
1489 pragma Import
(C
, Max_Path
, "__gnat_max_path_len");
1490 -- Maximum length of a path name
1492 Current_Dir
: String_Ptr
;
1493 Default_Search_Dir
: String_Access
;
1494 Default_Suffix_Dir
: String_Access
;
1495 Local_Search_Dir
: String_Access
;
1496 Norm_Search_Dir
: String_Access
;
1497 Result_Search_Dir
: String_Access
;
1498 Search_File
: String_Access
;
1499 Temp_String
: String_Ptr
;
1502 -- Add a directory separator at the end of the directory if necessary
1503 -- so that we can directly append a file to the directory
1505 if Search_Dir
(Search_Dir
'Last) /= Directory_Separator
then
1507 new String'(Search_Dir & String'(1 => Directory_Separator
));
1509 Local_Search_Dir
:= new String'(Search_Dir);
1512 if File_Type = Include then
1513 Search_File := Include_Search_File;
1514 Default_Suffix_Dir := new String'("adainclude");
1516 Search_File
:= Objects_Search_File
;
1517 Default_Suffix_Dir
:= new String'("adalib");
1520 Norm_Search_Dir := Local_Search_Dir;
1522 if Is_Absolute_Path (Norm_Search_Dir.all) then
1524 -- We first verify if there is a directory Include_Search_Dir
1525 -- containing default search directories
1527 Result_Search_Dir :=
1528 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1529 Default_Search_Dir :=
1530 new String'(Norm_Search_Dir
.all & Default_Suffix_Dir
.all);
1531 Free
(Norm_Search_Dir
);
1533 if Result_Search_Dir
/= null then
1534 return String_Ptr
(Result_Search_Dir
);
1535 elsif Is_Directory
(Default_Search_Dir
.all) then
1536 return String_Ptr
(Default_Search_Dir
);
1541 -- Search in the current directory
1544 -- Get the current directory
1547 Buffer
: String (1 .. Max_Path
+ 2);
1548 Path_Len
: Natural := Max_Path
;
1551 Get_Current_Dir
(Buffer
'Address, Path_Len
'Address);
1553 if Path_Len
= 0 then
1554 raise Program_Error
;
1557 if Buffer
(Path_Len
) /= Directory_Separator
then
1558 Path_Len
:= Path_Len
+ 1;
1559 Buffer
(Path_Len
) := Directory_Separator
;
1562 Current_Dir
:= new String'(Buffer (1 .. Path_Len));
1566 new String'(Current_Dir
.all & Local_Search_Dir
.all);
1568 Result_Search_Dir
:=
1569 Read_Default_Search_Dirs
(Norm_Search_Dir
, Search_File
, null);
1571 Default_Search_Dir
:=
1572 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1574 Free (Norm_Search_Dir);
1576 if Result_Search_Dir /= null then
1577 return String_Ptr (Result_Search_Dir);
1579 elsif Is_Directory (Default_Search_Dir.all) then
1580 return String_Ptr (Default_Search_Dir);
1583 -- Search in Search_Dir_Prefix/Search_Dir
1587 (Update_Path
(Search_Dir_Prefix
).all & Local_Search_Dir
.all);
1589 Result_Search_Dir
:=
1590 Read_Default_Search_Dirs
(Norm_Search_Dir
, Search_File
, null);
1592 Default_Search_Dir
:=
1593 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1595 Free (Norm_Search_Dir);
1597 if Result_Search_Dir /= null then
1598 return String_Ptr (Result_Search_Dir);
1600 elsif Is_Directory (Default_Search_Dir.all) then
1601 return String_Ptr (Default_Search_Dir);
1604 -- We finally search in Search_Dir_Prefix/rts-Search_Dir
1607 new String'(Update_Path
(Search_Dir_Prefix
).all & "rts-");
1610 new String'(Temp_String.all & Local_Search_Dir.all);
1612 Result_Search_Dir :=
1613 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1615 Default_Search_Dir :=
1616 new String'(Norm_Search_Dir
.all & Default_Suffix_Dir
.all);
1617 Free
(Norm_Search_Dir
);
1619 if Result_Search_Dir
/= null then
1620 return String_Ptr
(Result_Search_Dir
);
1622 elsif Is_Directory
(Default_Search_Dir
.all) then
1623 return String_Ptr
(Default_Search_Dir
);
1631 end Get_RTS_Search_Dir
;
1633 --------------------------------
1634 -- Include_Dir_Default_Prefix --
1635 --------------------------------
1637 function Include_Dir_Default_Prefix
return String_Access
is
1639 if The_Include_Dir_Default_Prefix
= null then
1640 The_Include_Dir_Default_Prefix
:=
1641 String_Access
(Update_Path
(Include_Dir_Default_Name
));
1644 return The_Include_Dir_Default_Prefix
;
1645 end Include_Dir_Default_Prefix
;
1647 function Include_Dir_Default_Prefix
return String is
1649 return Include_Dir_Default_Prefix
.all;
1650 end Include_Dir_Default_Prefix
;
1656 procedure Initialize
is
1658 Number_File_Names
:= 0;
1659 Current_File_Name_Index
:= 0;
1661 Src_Search_Directories
.Init
;
1662 Lib_Search_Directories
.Init
;
1664 -- Start off by setting all suppress options, to False. The special
1665 -- overflow fields are set to Not_Set (they will be set by -gnatp, or
1666 -- by -gnato, or, if neither of these appear, in Adjust_Global_Switches
1669 Suppress_Options
:= ((others => False), Not_Set
, Not_Set
);
1671 -- Reserve the first slot in the search paths table. This is the
1672 -- directory of the main source file or main library file and is filled
1673 -- in by each call to Next_Main_Source/Next_Main_Lib_File with the
1674 -- directory specified for this main source or library file. This is the
1675 -- directory which is searched first by default. This default search is
1676 -- inhibited by the option -I- for both source and library files.
1678 Src_Search_Directories
.Set_Last
(Primary_Directory
);
1679 Src_Search_Directories
.Table
(Primary_Directory
) := new String'("");
1681 Lib_Search_Directories.Set_Last (Primary_Directory);
1682 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
1689 function Is_Directory
1690 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1692 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1693 pragma Import
(C
, Internal
, "__gnat_is_directory_attr");
1695 return Internal
(Name
, Attr
.all'Address) /= 0;
1698 ----------------------------
1699 -- Is_Directory_Separator --
1700 ----------------------------
1702 function Is_Directory_Separator
(C
: Character) return Boolean is
1704 -- In addition to the default directory_separator allow the '/' to
1705 -- act as separator since this is allowed in MS-DOS and Windows.
1707 return C
= Directory_Separator
or else C
= '/';
1708 end Is_Directory_Separator
;
1710 -------------------------
1711 -- Is_Readonly_Library --
1712 -------------------------
1714 function Is_Readonly_Library
(File
: File_Name_Type
) return Boolean is
1716 Get_Name_String
(File
);
1718 pragma Assert
(Name_Buffer
(Name_Len
- 3 .. Name_Len
) = ".ali");
1720 return not Is_Writable_File
(Name_Buffer
(1 .. Name_Len
));
1721 end Is_Readonly_Library
;
1723 ------------------------
1724 -- Is_Executable_File --
1725 ------------------------
1727 function Is_Executable_File
1728 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1730 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1731 pragma Import
(C
, Internal
, "__gnat_is_executable_file_attr");
1733 return Internal
(Name
, Attr
.all'Address) /= 0;
1734 end Is_Executable_File
;
1736 ----------------------
1737 -- Is_Readable_File --
1738 ----------------------
1740 function Is_Readable_File
1741 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1743 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1744 pragma Import
(C
, Internal
, "__gnat_is_readable_file_attr");
1746 return Internal
(Name
, Attr
.all'Address) /= 0;
1747 end Is_Readable_File
;
1749 ---------------------
1750 -- Is_Regular_File --
1751 ---------------------
1753 function Is_Regular_File
1754 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1756 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1757 pragma Import
(C
, Internal
, "__gnat_is_regular_file_attr");
1759 return Internal
(Name
, Attr
.all'Address) /= 0;
1760 end Is_Regular_File
;
1762 ----------------------
1763 -- Is_Symbolic_Link --
1764 ----------------------
1766 function Is_Symbolic_Link
1767 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1769 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1770 pragma Import
(C
, Internal
, "__gnat_is_symbolic_link_attr");
1772 return Internal
(Name
, Attr
.all'Address) /= 0;
1773 end Is_Symbolic_Link
;
1775 ----------------------
1776 -- Is_Writable_File --
1777 ----------------------
1779 function Is_Writable_File
1780 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1782 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1783 pragma Import
(C
, Internal
, "__gnat_is_writable_file_attr");
1785 return Internal
(Name
, Attr
.all'Address) /= 0;
1786 end Is_Writable_File
;
1792 function Lib_File_Name
1793 (Source_File
: File_Name_Type
;
1794 Munit_Index
: Nat
:= 0) return File_Name_Type
1797 Get_Name_String
(Source_File
);
1799 for J
in reverse 2 .. Name_Len
loop
1800 if Name_Buffer
(J
) = '.' then
1806 if Munit_Index
/= 0 then
1807 Add_Char_To_Name_Buffer
(Multi_Unit_Index_Character
);
1808 Add_Nat_To_Name_Buffer
(Munit_Index
);
1811 Add_Char_To_Name_Buffer
('.');
1812 Add_Str_To_Name_Buffer
(ALI_Suffix
.all);
1820 procedure Locate_File
1821 (N
: File_Name_Type
;
1825 Found
: out File_Name_Type
;
1826 Attr
: access File_Attributes
)
1828 Dir_Name
: String_Ptr
;
1831 -- If Name is already an absolute path, do not look for a directory
1833 if Is_Absolute_Path
(Name
) then
1836 elsif T
= Library
then
1837 Dir_Name
:= Lib_Search_Directories
.Table
(Dir
);
1840 pragma Assert
(T
/= Config
);
1841 Dir_Name
:= Src_Search_Directories
.Table
(Dir
);
1845 Full_Name
: String (1 .. Dir_Name
'Length + Name
'Length + 1);
1848 Full_Name
(1 .. Dir_Name
'Length) := Dir_Name
.all;
1849 Full_Name
(Dir_Name
'Length + 1 .. Full_Name
'Last - 1) := Name
;
1850 Full_Name
(Full_Name
'Last) := ASCII
.NUL
;
1852 Attr
.all := Unknown_Attributes
;
1854 if not Is_Regular_File
(Full_Name
'Address, Attr
) then
1858 -- If the file is in the current directory then return N itself
1860 if Dir_Name
'Length = 0 then
1863 Name_Len
:= Full_Name
'Length - 1;
1864 Name_Buffer
(1 .. Name_Len
) :=
1865 Full_Name
(1 .. Full_Name
'Last - 1);
1866 Found
:= Name_Find
; -- ??? Was Name_Enter, no obvious reason
1872 -------------------------------
1873 -- Matching_Full_Source_Name --
1874 -------------------------------
1876 function Matching_Full_Source_Name
1877 (N
: File_Name_Type
;
1878 T
: Time_Stamp_Type
) return File_Name_Type
1881 Get_Name_String
(N
);
1884 File_Name
: constant String := Name_Buffer
(1 .. Name_Len
);
1885 File
: File_Name_Type
:= No_File
;
1886 Attr
: aliased File_Attributes
;
1890 if Opt
.Look_In_Primary_Dir
then
1892 (N
, Source
, Primary_Directory
, File_Name
, File
, Attr
'Access);
1894 if File
/= No_File
and then T
= File_Stamp
(N
) then
1899 Last_Dir
:= Src_Search_Directories
.Last
;
1901 for D
in Primary_Directory
+ 1 .. Last_Dir
loop
1902 Locate_File
(N
, Source
, D
, File_Name
, File
, Attr
'Access);
1904 if File
/= No_File
and then T
= File_Stamp
(File
) then
1911 end Matching_Full_Source_Name
;
1917 function More_Files
return Boolean is
1919 return (Current_File_Name_Index
< Number_File_Names
);
1922 -------------------------------
1923 -- Nb_Dir_In_Obj_Search_Path --
1924 -------------------------------
1926 function Nb_Dir_In_Obj_Search_Path
return Natural is
1928 if Opt
.Look_In_Primary_Dir
then
1929 return Lib_Search_Directories
.Last
- Primary_Directory
+ 1;
1931 return Lib_Search_Directories
.Last
- Primary_Directory
;
1933 end Nb_Dir_In_Obj_Search_Path
;
1935 -------------------------------
1936 -- Nb_Dir_In_Src_Search_Path --
1937 -------------------------------
1939 function Nb_Dir_In_Src_Search_Path
return Natural is
1941 if Opt
.Look_In_Primary_Dir
then
1942 return Src_Search_Directories
.Last
- Primary_Directory
+ 1;
1944 return Src_Search_Directories
.Last
- Primary_Directory
;
1946 end Nb_Dir_In_Src_Search_Path
;
1948 --------------------
1949 -- Next_Main_File --
1950 --------------------
1952 function Next_Main_File
return File_Name_Type
is
1953 File_Name
: String_Ptr
;
1954 Dir_Name
: String_Ptr
;
1958 pragma Assert
(More_Files
);
1960 Current_File_Name_Index
:= Current_File_Name_Index
+ 1;
1962 -- Get the file and directory name
1964 File_Name
:= File_Names
(Current_File_Name_Index
);
1965 Fptr
:= File_Name
'First;
1967 for J
in reverse File_Name
'Range loop
1968 if File_Name
(J
) = Directory_Separator
1969 or else File_Name
(J
) = '/'
1971 if J
= File_Name
'Last then
1972 Fail
("File name missing");
1980 -- Save name of directory in which main unit resides for use in
1981 -- locating other units
1983 Dir_Name
:= new String'(File_Name (File_Name'First .. Fptr - 1));
1985 case Running_Program is
1987 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1988 Look_In_Primary_Directory_For_Current_Main := True;
1991 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1993 if Fptr > File_Name'First then
1994 Look_In_Primary_Directory_For_Current_Main := True;
2000 Dir_Name := Normalize_Directory_Name (Dir_Name.all);
2001 Lib_Search_Directories.Table (Primary_Directory) := Dir_Name;
2007 Name_Len := File_Name'Last - Fptr + 1;
2008 Name_Buffer (1 .. Name_Len) := File_Name (Fptr .. File_Name'Last);
2009 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2010 Current_Main := Name_Find;
2012 -- In the gnatmake case, the main file may have not have the
2013 -- extension. Try ".adb" first then ".ads"
2015 if Running_Program = Make then
2017 Orig_Main : constant File_Name_Type := Current_Main;
2020 if Strip_Suffix (Orig_Main) = Orig_Main then
2022 Append_Suffix_To_File_Name (Orig_Main, ".adb");
2024 if Full_Source_Name (Current_Main) = No_File then
2026 Append_Suffix_To_File_Name (Orig_Main, ".ads");
2028 if Full_Source_Name (Current_Main) = No_File then
2029 Current_Main := Orig_Main;
2036 return Current_Main;
2039 ------------------------------
2040 -- Normalize_Directory_Name --
2041 ------------------------------
2043 function Normalize_Directory_Name (Directory : String) return String_Ptr is
2045 function Is_Quoted (Path : String) return Boolean;
2046 pragma Inline (Is_Quoted);
2047 -- Returns true if Path is quoted (either double or single quotes)
2053 function Is_Quoted (Path : String) return Boolean is
2054 First : constant Character := Path (Path'First);
2055 Last : constant Character := Path (Path'Last);
2058 if (First = ''' and then Last = ''')
2060 (First = '"' and then Last = '"')
2068 Result : String_Ptr;
2070 -- Start of processing for Normalize_Directory_Name
2073 if Directory'Length = 0 then
2074 Result := new String'(Hostparm
.Normalized_CWD
);
2076 elsif Is_Directory_Separator
(Directory
(Directory
'Last)) then
2077 Result
:= new String'(Directory);
2079 elsif Is_Quoted (Directory) then
2081 -- This is a quoted string, it certainly means that the directory
2082 -- contains some spaces for example. We can safely remove the quotes
2083 -- here as the OS_Lib.Normalize_Arguments will be called before any
2084 -- spawn routines. This ensure that quotes will be added when needed.
2086 Result := new String (1 .. Directory'Length - 1);
2087 Result (1 .. Directory'Length - 2) :=
2088 Directory (Directory'First + 1 .. Directory'Last - 1);
2089 Result (Result'Last) := Directory_Separator;
2092 Result := new String (1 .. Directory'Length + 1);
2093 Result (1 .. Directory'Length) := Directory;
2094 Result (Directory'Length + 1) := Directory_Separator;
2098 end Normalize_Directory_Name;
2100 ---------------------
2101 -- Number_Of_Files --
2102 ---------------------
2104 function Number_Of_Files return Nat is
2106 return Number_File_Names;
2107 end Number_Of_Files;
2109 -------------------------------
2110 -- Object_Dir_Default_Prefix --
2111 -------------------------------
2113 function Object_Dir_Default_Prefix return String is
2114 Object_Dir : String_Access :=
2115 String_Access (Update_Path (Object_Dir_Default_Name));
2118 if Object_Dir = null then
2123 Result : constant String := Object_Dir.all;
2129 end Object_Dir_Default_Prefix;
2131 ----------------------
2132 -- Object_File_Name --
2133 ----------------------
2135 function Object_File_Name (N : File_Name_Type) return File_Name_Type is
2141 Get_Name_String (N);
2142 Name_Len := Name_Len - ALI_Suffix'Length - 1;
2144 for J in Target_Object_Suffix'Range loop
2145 Name_Len := Name_Len + 1;
2146 Name_Buffer (Name_Len) := Target_Object_Suffix (J);
2150 end Object_File_Name;
2152 -------------------------------
2153 -- OS_Exit_Through_Exception --
2154 -------------------------------
2156 procedure OS_Exit_Through_Exception (Status : Integer) is
2158 Current_Exit_Status := Status;
2159 raise Types.Terminate_Program;
2160 end OS_Exit_Through_Exception;
2162 --------------------------
2163 -- OS_Time_To_GNAT_Time --
2164 --------------------------
2166 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type is
2167 GNAT_Time : Time_Stamp_Type;
2177 if T = Invalid_Time then
2178 return Empty_Time_Stamp;
2181 GM_Split (T, Y, Mo, D, H, Mn, S);
2187 Minutes => Nat (Mn),
2192 end OS_Time_To_GNAT_Time;
2198 function Prep_Suffix return String is
2207 function Program_Name (Nam : String; Prog : String) return String_Access is
2208 End_Of_Prefix : Natural := 0;
2209 Start_Of_Prefix : Positive := 1;
2210 Start_Of_Suffix : Positive;
2213 -- Get the name of the current program being executed
2217 Start_Of_Suffix := Name_Len + 1;
2219 -- Find the target prefix if any, for the cross compilation case.
2220 -- For instance in "powerpc-elf-gcc" the target prefix is
2222 -- Ditto for suffix, e.g. in "gcc-4.1", the suffix is "-4.1"
2224 for J in reverse 1 .. Name_Len loop
2225 if Name_Buffer (J) = '/'
2226 or else Name_Buffer (J) = Directory_Separator
2227 or else Name_Buffer (J) = ':'
2229 Start_Of_Prefix := J + 1;
2234 -- Find End_Of_Prefix
2236 for J in Start_Of_Prefix .. Name_Len - Prog'Length + 1 loop
2237 if Name_Buffer (J .. J + Prog'Length - 1) = Prog then
2238 End_Of_Prefix := J - 1;
2243 if End_Of_Prefix > 1 then
2244 Start_Of_Suffix := End_Of_Prefix + Prog'Length + 1;
2247 -- Create the new program name
2250 (Name_Buffer
(Start_Of_Prefix
.. End_Of_Prefix
)
2252 & Name_Buffer
(Start_Of_Suffix
.. Name_Len
));
2255 ------------------------------
2256 -- Read_Default_Search_Dirs --
2257 ------------------------------
2259 function Read_Default_Search_Dirs
2260 (Search_Dir_Prefix
: String_Access
;
2261 Search_File
: String_Access
;
2262 Search_Dir_Default_Name
: String_Access
) return String_Access
2264 Prefix_Len
: constant Integer := Search_Dir_Prefix
.all'Length;
2265 Buffer
: String (1 .. Prefix_Len
+ Search_File
.all'Length + 1);
2266 File_FD
: File_Descriptor
;
2267 S
, S1
: String_Access
;
2270 Actual_Len
: Integer;
2273 Prev_Was_Separator
: Boolean;
2274 Nb_Relative_Dir
: Integer;
2276 function Is_Relative
(S
: String; K
: Positive) return Boolean;
2277 pragma Inline
(Is_Relative
);
2278 -- Returns True if a relative directory specification is found
2279 -- in S at position K, False otherwise.
2285 function Is_Relative
(S
: String; K
: Positive) return Boolean is
2287 return not Is_Absolute_Path
(S
(K
.. S
'Last));
2290 -- Start of processing for Read_Default_Search_Dirs
2293 -- Construct a C compatible character string buffer
2295 Buffer
(1 .. Search_Dir_Prefix
.all'Length)
2296 := Search_Dir_Prefix
.all;
2297 Buffer
(Search_Dir_Prefix
.all'Length + 1 .. Buffer
'Last - 1)
2299 Buffer
(Buffer
'Last) := ASCII
.NUL
;
2301 File_FD
:= Open_Read
(Buffer
'Address, Binary
);
2302 if File_FD
= Invalid_FD
then
2303 return Search_Dir_Default_Name
;
2306 Len
:= Integer (File_Length
(File_FD
));
2308 -- An extra character for a trailing Path_Separator is allocated
2310 S
:= new String (1 .. Len
+ 1);
2311 S
(Len
+ 1) := Path_Separator
;
2313 -- Read the file. Note that the loop is probably not necessary since the
2314 -- whole file is read at once but the loop is harmless and that way we
2315 -- are sure to accommodate systems where this is not the case.
2319 while Actual_Len
/= 0 loop
2320 Actual_Len
:= Read
(File_FD
, S
(Curr
)'Address, Len
);
2321 Curr
:= Curr
+ Actual_Len
;
2324 -- Process the file, dealing with path separators
2326 Prev_Was_Separator
:= True;
2327 Nb_Relative_Dir
:= 0;
2328 for J
in 1 .. Len
loop
2330 -- Treat any control character as a path separator. Note that we do
2331 -- not treat space as a path separator (we used to treat space as a
2332 -- path separator in an earlier version). That way space can appear
2333 -- as a legitimate character in a path name.
2335 -- Why do we treat all control characters as path separators???
2337 if S
(J
) in ASCII
.NUL
.. ASCII
.US
then
2338 S
(J
) := Path_Separator
;
2341 -- Test for explicit path separator (or control char as above)
2343 if S
(J
) = Path_Separator
then
2344 Prev_Was_Separator
:= True;
2346 -- If not path separator, register use of relative directory
2349 if Prev_Was_Separator
and then Is_Relative
(S
.all, J
) then
2350 Nb_Relative_Dir
:= Nb_Relative_Dir
+ 1;
2353 Prev_Was_Separator
:= False;
2357 if Nb_Relative_Dir
= 0 then
2361 -- Add the Search_Dir_Prefix to all relative paths
2363 S1
:= new String (1 .. S
'Length + Nb_Relative_Dir
* Prefix_Len
);
2365 Prev_Was_Separator
:= True;
2366 for J
in 1 .. Len
+ 1 loop
2367 if S
(J
) = Path_Separator
then
2368 Prev_Was_Separator
:= True;
2371 if Prev_Was_Separator
and then Is_Relative
(S
.all, J
) then
2372 S1
(J1
.. J1
+ Prefix_Len
- 1) := Search_Dir_Prefix
.all;
2373 J1
:= J1
+ Prefix_Len
;
2376 Prev_Was_Separator
:= False;
2384 end Read_Default_Search_Dirs
;
2386 -----------------------
2387 -- Read_Library_Info --
2388 -----------------------
2390 function Read_Library_Info
2391 (Lib_File
: File_Name_Type
;
2392 Fatal_Err
: Boolean := False) return Text_Buffer_Ptr
2394 File
: File_Name_Type
;
2395 Attr
: aliased File_Attributes
;
2397 Find_File
(Lib_File
, Library
, File
, Attr
'Access);
2398 return Read_Library_Info_From_Full
2399 (Full_Lib_File
=> File
,
2400 Lib_File_Attr
=> Attr
'Access,
2401 Fatal_Err
=> Fatal_Err
);
2402 end Read_Library_Info
;
2404 ---------------------------------
2405 -- Read_Library_Info_From_Full --
2406 ---------------------------------
2408 function Read_Library_Info_From_Full
2409 (Full_Lib_File
: File_Name_Type
;
2410 Lib_File_Attr
: access File_Attributes
;
2411 Fatal_Err
: Boolean := False) return Text_Buffer_Ptr
2413 Lib_FD
: File_Descriptor
;
2414 -- The file descriptor for the current library file. A negative value
2415 -- indicates failure to open the specified source file.
2418 -- Length of source file text (ALI). If it doesn't fit in an integer
2419 -- we're probably stuck anyway (>2 gigs of source seems a lot, and
2420 -- there are other places in the compiler that make this assumption).
2422 Text
: Text_Buffer_Ptr
;
2423 -- Allocated text buffer
2426 pragma Warnings
(Off
, Status
);
2427 -- For the calls to Close
2430 Current_Full_Lib_Name
:= Full_Lib_File
;
2431 Current_Full_Obj_Name
:= Object_File_Name
(Current_Full_Lib_Name
);
2433 if Current_Full_Lib_Name
= No_File
then
2435 Fail
("Cannot find: " & Name_Buffer
(1 .. Name_Len
));
2437 Current_Full_Obj_Stamp
:= Empty_Time_Stamp
;
2442 Get_Name_String
(Current_Full_Lib_Name
);
2443 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
2445 -- Open the library FD, note that we open in binary mode, because as
2446 -- documented in the spec, the caller is expected to handle either
2447 -- DOS or Unix mode files, and there is no point in wasting time on
2448 -- text translation when it is not required.
2450 Lib_FD
:= Open_Read
(Name_Buffer
'Address, Binary
);
2452 if Lib_FD
= Invalid_FD
then
2454 Fail
("Cannot open: " & Name_Buffer
(1 .. Name_Len
));
2456 Current_Full_Obj_Stamp
:= Empty_Time_Stamp
;
2461 -- Compute the length of the file (potentially also preparing other data
2462 -- like the timestamp and whether the file is read-only, for future use)
2464 Len
:= Integer (File_Length
(Name_Buffer
'Address, Lib_File_Attr
));
2466 -- Check for object file consistency if requested
2468 if Opt
.Check_Object_Consistency
then
2469 -- On most systems, this does not result in an extra system call
2471 Current_Full_Lib_Stamp
:=
2472 OS_Time_To_GNAT_Time
2473 (File_Time_Stamp
(Name_Buffer
'Address, Lib_File_Attr
));
2475 -- ??? One system call here
2477 Current_Full_Obj_Stamp
:= File_Stamp
(Current_Full_Obj_Name
);
2479 if Current_Full_Obj_Stamp
(1) = ' ' then
2481 -- When the library is readonly always assume object is consistent
2482 -- The call to Is_Writable_File only results in a system call on
2483 -- some systems, but in most cases it has already been computed as
2484 -- part of the call to File_Length above.
2486 Get_Name_String
(Current_Full_Lib_Name
);
2487 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
2489 if not Is_Writable_File
(Name_Buffer
'Address, Lib_File_Attr
) then
2490 Current_Full_Obj_Stamp
:= Current_Full_Lib_Stamp
;
2492 elsif Fatal_Err
then
2493 Get_Name_String
(Current_Full_Obj_Name
);
2494 Close
(Lib_FD
, Status
);
2496 -- No need to check the status, we fail anyway
2498 Fail
("Cannot find: " & Name_Buffer
(1 .. Name_Len
));
2501 Current_Full_Obj_Stamp
:= Empty_Time_Stamp
;
2502 Close
(Lib_FD
, Status
);
2504 -- No need to check the status, we return null anyway
2509 elsif Current_Full_Obj_Stamp
< Current_Full_Lib_Stamp
then
2510 Close
(Lib_FD
, Status
);
2512 -- No need to check the status, we return null anyway
2518 -- Read data from the file
2521 Actual_Len
: Integer := 0;
2523 Lo
: constant Text_Ptr
:= 0;
2524 -- Low bound for allocated text buffer
2526 Hi
: Text_Ptr
:= Text_Ptr
(Len
);
2527 -- High bound for allocated text buffer. Note length is Len + 1
2528 -- which allows for extra EOF character at the end of the buffer.
2531 -- Allocate text buffer. Note extra character at end for EOF
2533 Text
:= new Text_Buffer
(Lo
.. Hi
);
2535 -- Some systems have file types that require one read per line,
2536 -- so read until we get the Len bytes or until there are no more
2541 Actual_Len
:= Read
(Lib_FD
, Text
(Hi
)'Address, Len
);
2542 Hi
:= Hi
+ Text_Ptr
(Actual_Len
);
2543 exit when Actual_Len
= Len
or else Actual_Len
<= 0;
2549 -- Read is complete, close file and we are done
2551 Close
(Lib_FD
, Status
);
2552 -- The status should never be False. But, if it is, what can we do?
2553 -- So, we don't test it.
2557 end Read_Library_Info_From_Full
;
2559 ----------------------
2560 -- Read_Source_File --
2561 ----------------------
2563 procedure Read_Source_File
2564 (N
: File_Name_Type
;
2566 Hi
: out Source_Ptr
;
2567 Src
: out Source_Buffer_Ptr
;
2568 T
: File_Type
:= Source
)
2570 Source_File_FD
: File_Descriptor
;
2571 -- The file descriptor for the current source file. A negative value
2572 -- indicates failure to open the specified source file.
2575 -- Length of file, assume no more than 2 gigabytes of source
2577 Actual_Len
: Integer;
2580 pragma Warnings
(Off
, Status
);
2581 -- For the call to Close
2584 Current_Full_Source_Name
:= Find_File
(N
, T
, Full_Name
=> True);
2585 Current_Full_Source_Stamp
:= File_Stamp
(Current_Full_Source_Name
);
2587 if Current_Full_Source_Name
= No_File
then
2589 -- If we were trying to access the main file and we could not find
2590 -- it, we have an error.
2592 if N
= Current_Main
then
2593 Get_Name_String
(N
);
2594 Fail
("Cannot find: " & Name_Buffer
(1 .. Name_Len
));
2602 Get_Name_String
(Current_Full_Source_Name
);
2603 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
2605 -- Open the source FD, note that we open in binary mode, because as
2606 -- documented in the spec, the caller is expected to handle either
2607 -- DOS or Unix mode files, and there is no point in wasting time on
2608 -- text translation when it is not required.
2610 Source_File_FD
:= Open_Read
(Name_Buffer
'Address, Binary
);
2612 if Source_File_FD
= Invalid_FD
then
2618 -- If it's a Source file, print out the file name, if requested, and if
2619 -- it's not part of the runtimes, store it in File_Name_Chars. We don't
2620 -- want to print non-Source files, like GNAT-TEMP-000001.TMP used to
2621 -- pass information from gprbuild to gcc. We don't want to save runtime
2622 -- file names, because we don't want users to send them in bug reports.
2626 Name
: String renames Name_Buffer
(1 .. Name_Len
);
2627 Inc
: String renames Include_Dir_Default_Prefix
.all;
2629 Part_Of_Runtimes
: constant Boolean :=
2631 and then Inc
'Length < Name_Len
2632 and then Name_Buffer
(1 .. Inc
'Length) = Inc
;
2635 if Debug
.Debug_Flag_Dot_N
then
2639 if not Part_Of_Runtimes
then
2640 File_Name_Chars
.Append_All
(File_Name_Chars
.Table_Type
(Name
));
2641 File_Name_Chars
.Append
(ASCII
.LF
);
2646 -- Prepare to read data from the file
2648 Len
:= Integer (File_Length
(Source_File_FD
));
2650 -- Set Hi so that length is one more than the physical length,
2651 -- allowing for the extra EOF character at the end of the buffer
2653 Hi
:= Lo
+ Source_Ptr
(Len
);
2655 -- Do the actual read operation
2658 Var_Ptr
: constant Source_Buffer_Ptr_Var
:=
2659 new Source_Buffer
(Lo
.. Hi
);
2660 -- Allocate source buffer, allowing extra character at end for EOF
2662 -- Some systems have file types that require one read per line,
2663 -- so read until we get the Len bytes or until there are no more
2668 Actual_Len
:= Read
(Source_File_FD
, Var_Ptr
(Hi
)'Address, Len
);
2669 Hi
:= Hi
+ Source_Ptr
(Actual_Len
);
2670 exit when Actual_Len
= Len
or else Actual_Len
<= 0;
2673 Var_Ptr
(Hi
) := EOF
;
2674 Src
:= Var_Ptr
.all'Access;
2677 -- Read is complete, get time stamp and close file and we are done
2679 Close
(Source_File_FD
, Status
);
2681 -- The status should never be False. But, if it is, what can we do?
2682 -- So, we don't test it.
2684 -- ???We don't really need to return Hi anymore; We could get rid of
2685 -- it. We could also make this into a function.
2687 pragma Assert
(Hi
= Src
'Last);
2688 end Read_Source_File
;
2694 function Relocate_Path
2696 Path
: String) return String_Ptr
2700 procedure set_std_prefix
(S
: String; Len
: Integer);
2701 pragma Import
(C
, set_std_prefix
);
2704 if Std_Prefix
= null then
2705 Std_Prefix
:= Executable_Prefix
;
2707 if Std_Prefix
.all /= "" then
2709 -- Remove trailing directory separator when calling set_std_prefix
2711 set_std_prefix
(Std_Prefix
.all, Std_Prefix
'Length - 1);
2715 if Path
'Last >= Prefix
'Last and then Path
(Prefix
'Range) = Prefix
then
2716 if Std_Prefix
.all /= "" then
2718 (1 .. Std_Prefix
'Length + Path
'Last - Prefix
'Last);
2719 S
(1 .. Std_Prefix
'Length) := Std_Prefix
.all;
2720 S
(Std_Prefix
'Length + 1 .. S
'Last) :=
2721 Path
(Prefix
'Last + 1 .. Path
'Last);
2726 return new String'(Path);
2733 procedure Set_Program (P : Program_Type) is
2736 Fail ("Set_Program called twice");
2739 Program_Set := True;
2740 Running_Program := P;
2747 function Shared_Lib (Name : String) return String is
2748 Library : String (1 .. Name'Length + Library_Version'Length + 3);
2749 -- 3 = 2 for "-l" + 1 for "-" before lib version
2752 Library (1 .. 2) := "-l";
2753 Library (3 .. 2 + Name'Length) := Name;
2754 Library (3 + Name'Length) := '-';
2755 Library (4 + Name'Length .. Library'Last) := Library_Version;
2759 ----------------------
2760 -- Smart_File_Stamp --
2761 ----------------------
2763 function Smart_File_Stamp
2764 (N : File_Name_Type;
2765 T : File_Type) return Time_Stamp_Type
2767 File : File_Name_Type;
2768 Attr : aliased File_Attributes;
2771 if not File_Cache_Enabled then
2772 Find_File (N, T, File, Attr'Access);
2774 Smart_Find_File (N, T, File, Attr);
2777 if File = No_File then
2778 return Empty_Time_Stamp;
2780 Get_Name_String (File);
2781 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2783 OS_Time_To_GNAT_Time
2784 (File_Time_Stamp (Name_Buffer'Address, Attr'Access));
2786 end Smart_File_Stamp;
2788 ---------------------
2789 -- Smart_Find_File --
2790 ---------------------
2792 function Smart_Find_File
2793 (N : File_Name_Type;
2794 T : File_Type) return File_Name_Type
2796 File : File_Name_Type;
2797 Attr : File_Attributes;
2799 Smart_Find_File (N, T, File, Attr);
2801 end Smart_Find_File;
2803 ---------------------
2804 -- Smart_Find_File --
2805 ---------------------
2807 procedure Smart_Find_File
2808 (N : File_Name_Type;
2810 Found : out File_Name_Type;
2811 Attr : out File_Attributes)
2813 Info : File_Info_Cache;
2816 if not File_Cache_Enabled then
2817 Find_File (N, T, Info.File, Info.Attr'Access);
2820 Info := File_Name_Hash_Table.Get (N);
2822 if Info.File = No_File then
2823 Find_File (N, T, Info.File, Info.Attr'Access);
2824 File_Name_Hash_Table.Set (N, Info);
2830 end Smart_Find_File;
2832 ----------------------
2833 -- Source_File_Data --
2834 ----------------------
2836 procedure Source_File_Data (Cache : Boolean) is
2838 File_Cache_Enabled := Cache;
2839 end Source_File_Data;
2841 -----------------------
2842 -- Source_File_Stamp --
2843 -----------------------
2845 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
2847 return Smart_File_Stamp (N, Source);
2848 end Source_File_Stamp;
2850 ---------------------
2851 -- Strip_Directory --
2852 ---------------------
2854 function Strip_Directory (Name : File_Name_Type) return File_Name_Type is
2856 Get_Name_String (Name);
2858 for J in reverse 1 .. Name_Len - 1 loop
2860 -- If we find the last directory separator
2862 if Is_Directory_Separator (Name_Buffer (J)) then
2864 -- Return part of Name that follows this last directory separator
2866 Name_Buffer (1 .. Name_Len - J) := Name_Buffer (J + 1 .. Name_Len);
2867 Name_Len := Name_Len - J;
2872 -- There were no directory separator, just return Name
2875 end Strip_Directory;
2881 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type is
2883 Get_Name_String (Name);
2885 for J in reverse 2 .. Name_Len loop
2887 -- If we found the last '.', return part of Name that precedes it
2889 if Name_Buffer (J) = '.' then
2898 ---------------------------
2899 -- To_Canonical_File_List --
2900 ---------------------------
2902 function To_Canonical_File_List
2903 (Wildcard_Host_File : String;
2904 Only_Dirs : Boolean) return String_Access_List_Access
2906 function To_Canonical_File_List_Init
2907 (Host_File : Address;
2908 Only_Dirs : Integer) return Integer;
2909 pragma Import (C, To_Canonical_File_List_Init,
2910 "__gnat_to_canonical_file_list_init");
2912 function To_Canonical_File_List_Next return Address;
2913 pragma Import (C, To_Canonical_File_List_Next,
2914 "__gnat_to_canonical_file_list_next");
2916 procedure To_Canonical_File_List_Free;
2917 pragma Import (C, To_Canonical_File_List_Free,
2918 "__gnat_to_canonical_file_list_free");
2920 Num_Files : Integer;
2921 C_Wildcard_Host_File : String (1 .. Wildcard_Host_File'Length + 1);
2924 C_Wildcard_Host_File (1 .. Wildcard_Host_File'Length) :=
2926 C_Wildcard_Host_File (C_Wildcard_Host_File'Last) := ASCII.NUL;
2928 -- Do the expansion and say how many there are
2930 Num_Files := To_Canonical_File_List_Init
2931 (C_Wildcard_Host_File'Address, Boolean'Pos (Only_Dirs));
2934 Canonical_File_List : String_Access_List (1 .. Num_Files);
2935 Canonical_File_Addr : Address;
2936 Canonical_File_Len : CRTL.size_t;
2939 -- Retrieve the expanded directory names and build the list
2941 for J in 1 .. Num_Files loop
2942 Canonical_File_Addr := To_Canonical_File_List_Next;
2943 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
2944 Canonical_File_List (J) := To_Path_String_Access
2945 (Canonical_File_Addr, Canonical_File_Len);
2948 -- Free up the storage
2950 To_Canonical_File_List_Free;
2952 return new String_Access_List'(Canonical_File_List
);
2954 end To_Canonical_File_List
;
2956 ----------------------
2957 -- To_Host_Dir_Spec --
2958 ----------------------
2960 function To_Host_Dir_Spec
2961 (Canonical_Dir
: String;
2962 Prefix_Style
: Boolean) return String_Access
2964 function To_Host_Dir_Spec
2965 (Canonical_Dir
: Address
;
2966 Prefix_Flag
: Integer) return Address
;
2967 pragma Import
(C
, To_Host_Dir_Spec
, "__gnat_to_host_dir_spec");
2969 C_Canonical_Dir
: String (1 .. Canonical_Dir
'Length + 1);
2970 Host_Dir_Addr
: Address
;
2971 Host_Dir_Len
: CRTL
.size_t
;
2974 C_Canonical_Dir
(1 .. Canonical_Dir
'Length) := Canonical_Dir
;
2975 C_Canonical_Dir
(C_Canonical_Dir
'Last) := ASCII
.NUL
;
2977 if Prefix_Style
then
2978 Host_Dir_Addr
:= To_Host_Dir_Spec
(C_Canonical_Dir
'Address, 1);
2980 Host_Dir_Addr
:= To_Host_Dir_Spec
(C_Canonical_Dir
'Address, 0);
2982 Host_Dir_Len
:= C_String_Length
(Host_Dir_Addr
);
2984 if Host_Dir_Len
= 0 then
2987 return To_Path_String_Access
(Host_Dir_Addr
, Host_Dir_Len
);
2989 end To_Host_Dir_Spec
;
2991 -----------------------
2992 -- To_Host_File_Spec --
2993 -----------------------
2995 function To_Host_File_Spec
2996 (Canonical_File
: String) return String_Access
2998 function To_Host_File_Spec
(Canonical_File
: Address
) return Address
;
2999 pragma Import
(C
, To_Host_File_Spec
, "__gnat_to_host_file_spec");
3001 C_Canonical_File
: String (1 .. Canonical_File
'Length + 1);
3002 Host_File_Addr
: Address
;
3003 Host_File_Len
: CRTL
.size_t
;
3006 C_Canonical_File
(1 .. Canonical_File
'Length) := Canonical_File
;
3007 C_Canonical_File
(C_Canonical_File
'Last) := ASCII
.NUL
;
3009 Host_File_Addr
:= To_Host_File_Spec
(C_Canonical_File
'Address);
3010 Host_File_Len
:= C_String_Length
(Host_File_Addr
);
3012 if Host_File_Len
= 0 then
3015 return To_Path_String_Access
3016 (Host_File_Addr
, Host_File_Len
);
3018 end To_Host_File_Spec
;
3020 ---------------------------
3021 -- To_Path_String_Access --
3022 ---------------------------
3024 function To_Path_String_Access
3025 (Path_Addr
: Address
;
3026 Path_Len
: CRTL
.size_t
) return String_Access
3028 subtype Path_String
is String (1 .. Integer (Path_Len
));
3029 type Path_String_Access
is access Path_String
;
3031 function Address_To_Access
is new
3032 Unchecked_Conversion
(Source
=> Address
,
3033 Target
=> Path_String_Access
);
3035 Path_Access
: constant Path_String_Access
:=
3036 Address_To_Access
(Path_Addr
);
3038 Return_Val
: String_Access
;
3041 Return_Val
:= new String (1 .. Integer (Path_Len
));
3043 for J
in 1 .. Integer (Path_Len
) loop
3044 Return_Val
(J
) := Path_Access
(J
);
3048 end To_Path_String_Access
;
3054 function Update_Path
(Path
: String_Ptr
) return String_Ptr
is
3056 function C_Update_Path
(Path
, Component
: Address
) return Address
;
3057 pragma Import
(C
, C_Update_Path
, "update_path");
3059 In_Length
: constant Integer := Path
'Length;
3060 In_String
: String (1 .. In_Length
+ 1);
3061 Component_Name
: aliased String := "GCC" & ASCII
.NUL
;
3062 Result_Ptr
: Address
;
3063 Result_Length
: CRTL
.size_t
;
3064 Out_String
: String_Ptr
;
3067 In_String
(1 .. In_Length
) := Path
.all;
3068 In_String
(In_Length
+ 1) := ASCII
.NUL
;
3069 Result_Ptr
:= C_Update_Path
(In_String
'Address, Component_Name
'Address);
3070 Result_Length
:= CRTL
.strlen
(Result_Ptr
);
3072 Out_String
:= new String (1 .. Integer (Result_Length
));
3073 CRTL
.strncpy
(Out_String
.all'Address, Result_Ptr
, Result_Length
);
3081 procedure Write_Info
(Info
: String) is
3083 Write_With_Check
(Info
'Address, Info
'Length);
3084 Write_With_Check
(EOL
'Address, 1);
3087 ------------------------
3088 -- Write_Program_Name --
3089 ------------------------
3091 procedure Write_Program_Name
is
3092 Save_Buffer
: constant String (1 .. Name_Len
) :=
3093 Name_Buffer
(1 .. Name_Len
);
3098 -- Convert the name to lower case so error messages are the same on
3101 for J
in 1 .. Name_Len
loop
3102 if Name_Buffer
(J
) in 'A' .. 'Z' then
3104 Character'Val (Character'Pos (Name_Buffer
(J
)) + 32);
3108 Write_Str
(Name_Buffer
(1 .. Name_Len
));
3110 -- Restore Name_Buffer which was clobbered by the call to
3111 -- Find_Program_Name
3113 Name_Len
:= Save_Buffer
'Last;
3114 Name_Buffer
(1 .. Name_Len
) := Save_Buffer
;
3115 end Write_Program_Name
;
3117 ----------------------
3118 -- Write_With_Check --
3119 ----------------------
3121 procedure Write_With_Check
(A
: Address
; N
: Integer) is
3124 if N
= Write
(Output_FD
, A
, N
) then
3127 Write_Str
("error: disk full writing ");
3128 Write_Name_Decoded
(Output_File_Name
);
3130 Name_Len
:= Name_Len
+ 1;
3131 Name_Buffer
(Name_Len
) := ASCII
.NUL
;
3132 Delete_File
(Name_Buffer
'Address, Ignore
);
3133 Exit_Program
(E_Fatal
);
3135 end Write_With_Check
;
3137 ----------------------------
3138 -- Package Initialization --
3139 ----------------------------
3141 procedure Reset_File_Attributes
(Attr
: System
.Address
);
3142 pragma Import
(C
, Reset_File_Attributes
, "__gnat_reset_attributes");
3145 Initialization
: declare
3147 function Get_Default_Identifier_Character_Set
return Character;
3148 pragma Import
(C
, Get_Default_Identifier_Character_Set
,
3149 "__gnat_get_default_identifier_character_set");
3150 -- Function to determine the default identifier character set,
3151 -- which is system dependent. See Opt package spec for a list of
3152 -- the possible character codes and their interpretations.
3154 function Get_Maximum_File_Name_Length
return Int
;
3155 pragma Import
(C
, Get_Maximum_File_Name_Length
,
3156 "__gnat_get_maximum_file_name_length");
3157 -- Function to get maximum file name length for system
3159 Sizeof_File_Attributes
: Integer;
3160 pragma Import
(C
, Sizeof_File_Attributes
,
3161 "__gnat_size_of_file_attributes");
3164 pragma Assert
(Sizeof_File_Attributes
<= File_Attributes_Size
);
3166 Reset_File_Attributes
(Unknown_Attributes
'Address);
3168 Identifier_Character_Set
:= Get_Default_Identifier_Character_Set
;
3169 Maximum_File_Name_Length
:= Get_Maximum_File_Name_Length
;
3171 -- Following should be removed by having above function return
3172 -- Integer'Last as indication of no maximum instead of -1 ???
3174 if Maximum_File_Name_Length
= -1 then
3175 Maximum_File_Name_Length
:= Int
'Last;
3178 Src_Search_Directories
.Set_Last
(Primary_Directory
);
3179 Src_Search_Directories
.Table
(Primary_Directory
) := new String'("");
3181 Lib_Search_Directories.Set_Last (Primary_Directory);
3182 Lib_Search_Directories.Table (Primary_Directory) := new String'("");