1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Ada
.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 -- Set by Set_Program to indicate which of Compiler, Binder, etc is
55 Program_Set
: Boolean := False;
56 -- True if Set_Program has been called; used to detect duplicate calls.
58 Std_Prefix
: String_Ptr
;
59 -- Standard prefix, computed dynamically the first time Relocate_Path
60 -- is called, and cached for subsequent calls.
62 Empty
: aliased String := "";
63 No_Dir
: constant String_Ptr
:= Empty
'Access;
64 -- Used in Locate_File as a fake directory when Name is already an
67 -------------------------------------
68 -- Use of Name_Find and Name_Enter --
69 -------------------------------------
71 -- This package creates a number of source, ALI and object file names
72 -- that are used to locate the actual file and for the purpose of message
73 -- construction. These names need not be accessible by Name_Find, and can
74 -- be therefore created by using routine Name_Enter. The files in question
75 -- are file names with a prefix directory (i.e., the files not in the
76 -- current directory). File names without a prefix directory are entered
77 -- with Name_Find because special values might be attached to the various
78 -- Info fields of the corresponding name table entry.
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Append_Suffix_To_File_Name
85 (Name
: File_Name_Type
;
86 Suffix
: String) return File_Name_Type
;
87 -- Appends Suffix to Name and returns the new name
89 function OS_Time_To_GNAT_Time
(T
: OS_Time
) return Time_Stamp_Type
;
90 -- Convert OS format time to GNAT format time stamp. If T is Invalid_Time,
91 -- then returns Empty_Time_Stamp.
92 -- Round to even seconds on Windows before conversion.
93 -- Windows ALI files had timestamps rounded to even seconds historically.
94 -- The rounding was originally done in GM_Split. Now that GM_Split no
95 -- longer does it, we are rounding it here only for ALI files.
97 function Executable_Prefix
return String_Ptr
;
98 -- Returns the name of the root directory where the executable is stored.
99 -- The executable must be located in a directory called "bin", or under
100 -- root/lib/gcc-lib/..., or under root/libexec/gcc/... For example, if
101 -- executable is stored in directory "/foo/bar/bin", this routine returns
102 -- "/foo/bar/". Return "" if location is not recognized as described above.
104 function File_Names_Equal
(File1
, File2
: String) return Boolean;
105 -- Compare File1 and File2 taking into account the case insensitivity
108 function Update_Path
(Path
: String_Ptr
) return String_Ptr
;
109 -- Update the specified path to replace the prefix with the location where
110 -- GNAT is installed. See the file prefix.c in GCC for details.
112 procedure Locate_File
117 Found
: out File_Name_Type
;
118 Attr
: access File_Attributes
);
119 -- See if the file N whose name is Name exists in directory Dir. Dir is an
120 -- index into the Lib_Search_Directories table if T = Library. Otherwise
121 -- if T = Source, Dir is an index into the Src_Search_Directories table.
122 -- Returns the File_Name_Type of the full file name if file found, or
123 -- No_File if not found.
125 -- On exit, Found is set to the file that was found, and Attr to a cache of
126 -- its attributes (at least those that have been computed so far). Reusing
127 -- the cache will save some system calls.
129 -- Attr is always reset in this call to Unknown_Attributes, even in case of
135 Found
: out File_Name_Type
;
136 Attr
: access File_Attributes
;
137 Full_Name
: Boolean := False);
138 -- A version of Find_File that also returns a cache of the file attributes
141 procedure Smart_Find_File
144 Found
: out File_Name_Type
;
145 Attr
: out File_Attributes
);
146 -- A version of Smart_Find_File that also returns a cache of the file
147 -- attributes for later reuse
149 function C_String_Length
(S
: Address
) return CRTL
.size_t
;
150 -- Returns length of a C string (zero for a null address)
152 function To_Path_String_Access
153 (Path_Addr
: Address
;
154 Path_Len
: CRTL
.size_t
) return String_Access
;
155 -- Converts a C String to an Ada String. We don't use a more general
156 -- purpose facility, because we are dealing with low-level types like
157 -- Address. Caller must free result.
159 function Include_Dir_Default_Prefix
return String_Access
;
160 -- Same as exported version, except returns a String_Access
162 ------------------------------
163 -- Other Local Declarations --
164 ------------------------------
166 EOL
: constant Character := ASCII
.LF
;
167 -- End of line character
169 Number_File_Names
: Nat
:= 0;
170 -- Number of file names found on command line and placed in File_Names
172 Look_In_Primary_Directory_For_Current_Main
: Boolean := False;
173 -- When this variable is True, Find_File only looks in Primary_Directory
174 -- for the Current_Main file. This variable is always set to True for the
175 -- compiler. It is also True for gnatmake, when the source name given on
176 -- the command line has directory information.
178 Current_Full_Source_Name
: File_Name_Type
:= No_File
;
179 Current_Full_Source_Stamp
: Time_Stamp_Type
:= Empty_Time_Stamp
;
180 Current_Full_Lib_Name
: File_Name_Type
:= No_File
;
181 Current_Full_Lib_Stamp
: Time_Stamp_Type
:= Empty_Time_Stamp
;
182 Current_Full_Obj_Name
: File_Name_Type
:= No_File
;
183 Current_Full_Obj_Stamp
: Time_Stamp_Type
:= Empty_Time_Stamp
;
184 -- Respectively full name (with directory info) and time stamp of the
185 -- latest source, library and object files opened by Read_Source_File and
186 -- Read_Library_Info.
188 package File_Name_Chars
is new Table
.Table
(
189 Table_Component_Type
=> Character,
190 Table_Index_Type
=> Int
,
191 Table_Low_Bound
=> 1,
192 Table_Initial
=> Alloc
.File_Name_Chars_Initial
,
193 Table_Increment
=> Alloc
.File_Name_Chars_Increment
,
194 Table_Name
=> "File_Name_Chars");
195 -- Table to store text to be printed by Dump_Source_File_Names
197 The_Include_Dir_Default_Prefix
: String_Access
:= null;
198 -- Value returned by Include_Dir_Default_Prefix. We don't initialize it
199 -- here, because that causes an elaboration cycle with Sdefault; we
200 -- initialize it lazily instead.
206 Primary_Directory
: constant := 0;
207 -- This is index in the tables created below for the first directory to
208 -- search in for source or library information files. This is the directory
209 -- containing the latest main input file (a source file for the compiler or
210 -- a library file for the binder).
212 package Src_Search_Directories
is new Table
.Table
(
213 Table_Component_Type
=> String_Ptr
,
214 Table_Index_Type
=> Integer,
215 Table_Low_Bound
=> Primary_Directory
,
217 Table_Increment
=> 100,
218 Table_Name
=> "Osint.Src_Search_Directories");
219 -- Table of names of directories in which to search for source (Compiler)
220 -- files. This table is filled in the order in which the directories are
221 -- to be searched, and then used in that order.
223 package Lib_Search_Directories
is new Table
.Table
(
224 Table_Component_Type
=> String_Ptr
,
225 Table_Index_Type
=> Integer,
226 Table_Low_Bound
=> Primary_Directory
,
228 Table_Increment
=> 100,
229 Table_Name
=> "Osint.Lib_Search_Directories");
230 -- Table of names of directories in which to search for library (Binder)
231 -- files. This table is filled in the order in which the directories are
232 -- to be searched and then used in that order. The reason for having two
233 -- distinct tables is that we need them both in gnatmake.
235 ---------------------
236 -- File Hash Table --
237 ---------------------
239 -- The file hash table is provided to free the programmer from any
240 -- efficiency concern when retrieving full file names or time stamps of
241 -- source files. If the programmer calls Source_File_Data (Cache => True)
242 -- he is guaranteed that the price to retrieve the full name (i.e. with
243 -- directory info) or time stamp of the file will be payed only once, the
244 -- first time the full name is actually searched (or the first time the
245 -- time stamp is actually retrieved). This is achieved by employing a hash
246 -- table that stores as a key the File_Name_Type of the file and associates
247 -- to that File_Name_Type the full file name and time stamp of the file.
249 File_Cache_Enabled
: Boolean := False;
250 -- Set to true if you want the enable the file data caching mechanism
252 type File_Hash_Num
is range 0 .. 1020;
254 function File_Hash
(F
: File_Name_Type
) return File_Hash_Num
;
255 -- Compute hash index for use by Simple_HTable
257 type File_Info_Cache
is record
258 File
: File_Name_Type
;
259 Attr
: aliased File_Attributes
;
262 No_File_Info_Cache
: constant File_Info_Cache
:= (No_File
, (others => 0));
264 package File_Name_Hash_Table
is new GNAT
.HTable
.Simple_HTable
(
265 Header_Num
=> File_Hash_Num
,
266 Element
=> File_Info_Cache
,
267 No_Element
=> No_File_Info_Cache
,
268 Key
=> File_Name_Type
,
272 function Smart_Find_File
274 T
: File_Type
) return File_Name_Type
;
275 -- Exactly like Find_File except that if File_Cache_Enabled is True this
276 -- routine looks first in the hash table to see if the full name of the
277 -- file is already available.
279 function Smart_File_Stamp
281 T
: File_Type
) return Time_Stamp_Type
;
282 -- Takes the same parameter as the routine above (N is a file name without
283 -- any prefix directory information) and behaves like File_Stamp except
284 -- that if File_Cache_Enabled is True this routine looks first in the hash
285 -- table to see if the file stamp of the file is already available.
287 -----------------------------
288 -- Add_Default_Search_Dirs --
289 -----------------------------
291 procedure Add_Default_Search_Dirs
is
292 Search_Dir
: String_Access
;
293 Search_Path
: String_Access
;
294 Path_File_Name
: String_Access
;
296 procedure Add_Search_Dir
297 (Search_Dir
: String;
298 Additional_Source_Dir
: Boolean);
299 procedure Add_Search_Dir
300 (Search_Dir
: String_Access
;
301 Additional_Source_Dir
: Boolean);
302 -- Add a source search dir or a library search dir, depending on the
303 -- value of Additional_Source_Dir.
305 procedure Get_Dirs_From_File
(Additional_Source_Dir
: Boolean);
306 -- Open a path file and read the directory to search, one per line
308 function Get_Libraries_From_Registry
return String_Ptr
;
309 -- On Windows systems, get the list of installed standard libraries
310 -- from the registry key:
312 -- HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\
313 -- GNAT\Standard Libraries
314 -- Return an empty string on other systems.
316 -- Note that this is an undocumented legacy feature, and that it
317 -- works only when using the default runtime library (i.e. no --RTS=
318 -- command line switch).
324 procedure Add_Search_Dir
325 (Search_Dir
: String;
326 Additional_Source_Dir
: Boolean)
329 if Additional_Source_Dir
then
330 Add_Src_Search_Dir
(Search_Dir
);
332 Add_Lib_Search_Dir
(Search_Dir
);
336 procedure Add_Search_Dir
337 (Search_Dir
: String_Access
;
338 Additional_Source_Dir
: Boolean)
341 if Additional_Source_Dir
then
342 Add_Src_Search_Dir
(Search_Dir
.all);
344 Add_Lib_Search_Dir
(Search_Dir
.all);
348 ------------------------
349 -- Get_Dirs_From_File --
350 ------------------------
352 procedure Get_Dirs_From_File
(Additional_Source_Dir
: Boolean) is
353 File_FD
: File_Descriptor
;
354 Buffer
: constant String := Path_File_Name
.all & ASCII
.NUL
;
356 Actual_Len
: Natural;
363 pragma Warnings
(Off
, Status
);
364 -- For the call to Close where status is ignored
367 File_FD
:= Open_Read
(Buffer
'Address, Binary
);
369 -- If we cannot open the file, we ignore it, we don't fail
371 if File_FD
= Invalid_FD
then
375 Len
:= Integer (File_Length
(File_FD
));
377 S
:= new String (1 .. Len
);
379 -- Read the file. Note that the loop is probably not necessary any
380 -- more since the whole file is read in at once on all targets. But
381 -- it is harmless and might be needed in future.
385 while Curr
<= Len
and then Actual_Len
/= 0 loop
386 Actual_Len
:= Read
(File_FD
, S
(Curr
)'Address, Len
);
387 Curr
:= Curr
+ Actual_Len
;
390 -- We are done with the file, so we close it (ignore any error on
391 -- the close, since we have successfully read the file).
393 Close
(File_FD
, Status
);
395 -- Now, we read line by line
399 while Curr
< Len
loop
402 if Ch
= ASCII
.CR
or else Ch
= ASCII
.LF
403 or else Ch
= ASCII
.FF
or else Ch
= ASCII
.VT
405 if First
<= Curr
then
406 Add_Search_Dir
(S
(First
.. Curr
), Additional_Source_Dir
);
415 -- Last line is a special case, if the file does not end with
416 -- an end of line mark.
418 if First
<= S
'Last then
419 Add_Search_Dir
(S
(First
.. S
'Last), Additional_Source_Dir
);
421 end Get_Dirs_From_File
;
423 ---------------------------------
424 -- Get_Libraries_From_Registry --
425 ---------------------------------
427 function Get_Libraries_From_Registry
return String_Ptr
is
428 function C_Get_Libraries_From_Registry
return Address
;
429 pragma Import
(C
, C_Get_Libraries_From_Registry
,
430 "__gnat_get_libraries_from_registry");
432 Result_Ptr
: Address
;
433 Result_Length
: CRTL
.size_t
;
434 Out_String
: String_Ptr
;
437 Result_Ptr
:= C_Get_Libraries_From_Registry
;
438 Result_Length
:= CRTL
.strlen
(Result_Ptr
);
440 Out_String
:= new String (1 .. Integer (Result_Length
));
441 CRTL
.strncpy
(Out_String
.all'Address, Result_Ptr
, Result_Length
);
443 CRTL
.free
(Result_Ptr
);
446 end Get_Libraries_From_Registry
;
448 -- Start of processing for Add_Default_Search_Dirs
451 -- If there was a -gnateO switch, add all object directories from the
452 -- file given in argument to the library search list.
454 if Object_Path_File_Name
/= null then
455 Path_File_Name
:= String_Access
(Object_Path_File_Name
);
456 pragma Assert
(Path_File_Name
'Length > 0);
457 Get_Dirs_From_File
(Additional_Source_Dir
=> False);
460 -- After the locations specified on the command line, the next places
461 -- to look for files are the directories specified by the appropriate
462 -- environment variable. Get this value, extract the directory names
463 -- and store in the tables.
465 -- Check for eventual project path file env vars
467 Path_File_Name
:= Getenv
(Project_Include_Path_File
);
469 if Path_File_Name
'Length > 0 then
470 Get_Dirs_From_File
(Additional_Source_Dir
=> True);
473 Path_File_Name
:= Getenv
(Project_Objects_Path_File
);
475 if Path_File_Name
'Length > 0 then
476 Get_Dirs_From_File
(Additional_Source_Dir
=> False);
479 -- Put path name in canonical form
481 for Additional_Source_Dir
in False .. True loop
482 if Additional_Source_Dir
then
483 Search_Path
:= Getenv
(Ada_Include_Path
);
486 Search_Path
:= Getenv
(Ada_Objects_Path
);
490 Get_Next_Dir_In_Path_Init
(Search_Path
);
492 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
493 exit when Search_Dir
= null;
494 Add_Search_Dir
(Search_Dir
, Additional_Source_Dir
);
498 -- For the compiler, if --RTS= was specified, add the runtime
501 if RTS_Src_Path_Name
/= null and then RTS_Lib_Path_Name
/= null then
502 Add_Search_Dirs
(RTS_Src_Path_Name
, Include
);
503 Add_Search_Dirs
(RTS_Lib_Path_Name
, Objects
);
506 if not Opt
.No_Stdinc
then
508 -- For WIN32 systems, look for any system libraries defined in
509 -- the registry. These are added to both source and object
512 Search_Path
:= String_Access
(Get_Libraries_From_Registry
);
514 Get_Next_Dir_In_Path_Init
(Search_Path
);
516 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
517 exit when Search_Dir
= null;
518 Add_Search_Dir
(Search_Dir
, False);
519 Add_Search_Dir
(Search_Dir
, True);
522 -- The last place to look are the defaults
525 Read_Default_Search_Dirs
526 (String_Access
(Update_Path
(Search_Dir_Prefix
)),
528 String_Access
(Update_Path
(Include_Dir_Default_Name
)));
530 Get_Next_Dir_In_Path_Init
(Search_Path
);
532 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
533 exit when Search_Dir
= null;
534 Add_Search_Dir
(Search_Dir
, True);
538 -- Even when -nostdlib is used, we still want to have visibility on
539 -- the run-time object directory, as it is used by gnatbind to find
540 -- the run-time ALI files in "real" ZFP set up.
542 if not Opt
.RTS_Switch
then
544 Read_Default_Search_Dirs
545 (String_Access
(Update_Path
(Search_Dir_Prefix
)),
547 String_Access
(Update_Path
(Object_Dir_Default_Name
)));
549 Get_Next_Dir_In_Path_Init
(Search_Path
);
551 Search_Dir
:= Get_Next_Dir_In_Path
(Search_Path
);
552 exit when Search_Dir
= null;
553 Add_Search_Dir
(Search_Dir
, False);
557 end Add_Default_Search_Dirs
;
563 procedure Add_File
(File_Name
: String; Index
: Int
:= No_Index
) is
565 Number_File_Names
:= Number_File_Names
+ 1;
567 -- As Add_File may be called for mains specified inside a project file,
568 -- File_Names may be too short and needs to be extended.
570 if Number_File_Names
> File_Names
'Last then
571 File_Names
:= new File_Name_Array
'(File_Names.all & File_Names.all);
573 new File_Index_Array'(File_Indexes
.all & File_Indexes
.all);
576 File_Names
(Number_File_Names
) := new String'(File_Name);
577 File_Indexes (Number_File_Names) := Index;
580 ------------------------
581 -- Add_Lib_Search_Dir --
582 ------------------------
584 procedure Add_Lib_Search_Dir (Dir : String) is
586 if Dir'Length = 0 then
587 Fail ("missing library directory name");
591 Norm : String_Ptr := Normalize_Directory_Name (Dir);
594 -- Do nothing if the directory is already in the list. This saves
595 -- system calls and avoid unneeded work
597 for D in Lib_Search_Directories.First ..
598 Lib_Search_Directories.Last
600 if Lib_Search_Directories.Table (D).all = Norm.all then
606 Lib_Search_Directories.Increment_Last;
607 Lib_Search_Directories.Table (Lib_Search_Directories.Last) := Norm;
609 end Add_Lib_Search_Dir;
611 ---------------------
612 -- Add_Search_Dirs --
613 ---------------------
615 procedure Add_Search_Dirs
616 (Search_Path : String_Ptr;
617 Path_Type : Search_File_Type)
619 Current_Search_Path : String_Access;
622 Get_Next_Dir_In_Path_Init (String_Access (Search_Path));
624 Current_Search_Path :=
625 Get_Next_Dir_In_Path (String_Access (Search_Path));
626 exit when Current_Search_Path = null;
628 if Path_Type = Include then
629 Add_Src_Search_Dir (Current_Search_Path.all);
631 Add_Lib_Search_Dir (Current_Search_Path.all);
636 ------------------------
637 -- Add_Src_Search_Dir --
638 ------------------------
640 procedure Add_Src_Search_Dir (Dir : String) is
642 if Dir'Length = 0 then
643 Fail ("missing source directory name");
646 Src_Search_Directories.Increment_Last;
647 Src_Search_Directories.Table (Src_Search_Directories.Last) :=
648 Normalize_Directory_Name (Dir);
649 end Add_Src_Search_Dir;
651 --------------------------------
652 -- Append_Suffix_To_File_Name --
653 --------------------------------
655 function Append_Suffix_To_File_Name
656 (Name : File_Name_Type;
657 Suffix : String) return File_Name_Type
660 Get_Name_String (Name);
661 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
662 Name_Len := Name_Len + Suffix'Length;
664 end Append_Suffix_To_File_Name;
666 ---------------------
667 -- C_String_Length --
668 ---------------------
670 function C_String_Length (S : Address) return CRTL.size_t is
672 if S = Null_Address then
675 return CRTL.strlen (S);
679 ------------------------------
680 -- Canonical_Case_File_Name --
681 ------------------------------
683 procedure Canonical_Case_File_Name (S : in out String) is
685 if not File_Names_Case_Sensitive then
688 end Canonical_Case_File_Name;
690 ---------------------------------
691 -- Canonical_Case_Env_Var_Name --
692 ---------------------------------
694 procedure Canonical_Case_Env_Var_Name (S : in out String) is
696 if not Env_Vars_Case_Sensitive then
699 end Canonical_Case_Env_Var_Name;
701 ---------------------------
702 -- Create_File_And_Check --
703 ---------------------------
705 procedure Create_File_And_Check
706 (Fdesc : out File_Descriptor;
710 Output_File_Name := Name_Enter;
711 Fdesc := Create_File (Name_Buffer'Address, Fmode);
713 if Fdesc = Invalid_FD then
714 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
716 end Create_File_And_Check;
718 -----------------------------------
719 -- Open_File_To_Append_And_Check --
720 -----------------------------------
722 procedure Open_File_To_Append_And_Check
723 (Fdesc : out File_Descriptor;
727 Output_File_Name := Name_Enter;
728 Fdesc := Open_Append (Name_Buffer'Address, Fmode);
730 if Fdesc = Invalid_FD then
731 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
733 end Open_File_To_Append_And_Check;
735 ------------------------
736 -- Current_File_Index --
737 ------------------------
739 function Current_File_Index return Int is
741 return File_Indexes (Current_File_Name_Index);
742 end Current_File_Index;
744 --------------------------------
745 -- Current_Library_File_Stamp --
746 --------------------------------
748 function Current_Library_File_Stamp return Time_Stamp_Type is
750 return Current_Full_Lib_Stamp;
751 end Current_Library_File_Stamp;
753 -------------------------------
754 -- Current_Object_File_Stamp --
755 -------------------------------
757 function Current_Object_File_Stamp return Time_Stamp_Type is
759 return Current_Full_Obj_Stamp;
760 end Current_Object_File_Stamp;
762 -------------------------------
763 -- Current_Source_File_Stamp --
764 -------------------------------
766 function Current_Source_File_Stamp return Time_Stamp_Type is
768 return Current_Full_Source_Stamp;
769 end Current_Source_File_Stamp;
771 ----------------------------
772 -- Dir_In_Obj_Search_Path --
773 ----------------------------
775 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr is
777 if Opt.Look_In_Primary_Dir then
779 Lib_Search_Directories.Table (Primary_Directory + Position - 1);
781 return Lib_Search_Directories.Table (Primary_Directory + Position);
783 end Dir_In_Obj_Search_Path;
785 ----------------------------
786 -- Dir_In_Src_Search_Path --
787 ----------------------------
789 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr is
791 if Opt.Look_In_Primary_Dir then
793 Src_Search_Directories.Table (Primary_Directory + Position - 1);
795 return Src_Search_Directories.Table (Primary_Directory + Position);
797 end Dir_In_Src_Search_Path;
799 -----------------------------------------
800 -- Dump_Command_Line_Source_File_Names --
801 -----------------------------------------
803 procedure Dump_Command_Line_Source_File_Names is
805 for J in 1 .. Number_Of_Files loop
806 Write_Str (File_Names (J).all & " ");
808 end Dump_Command_Line_Source_File_Names;
810 ----------------------------
811 -- Dump_Source_File_Names --
812 ----------------------------
814 procedure Dump_Source_File_Names is
815 subtype Rng is Int range File_Name_Chars.First .. File_Name_Chars.Last;
817 Write_Str (String (File_Name_Chars.Table (Rng)));
818 end Dump_Source_File_Names;
820 ---------------------
821 -- Executable_Name --
822 ---------------------
824 function Executable_Name
825 (Name : File_Name_Type;
826 Only_If_No_Suffix : Boolean := False) return File_Name_Type
828 Exec_Suffix : String_Access;
829 Add_Suffix : Boolean;
832 if Name = No_File then
836 if Executable_Extension_On_Target = No_Name then
837 Exec_Suffix := Get_Target_Executable_Suffix;
839 Get_Name_String (Executable_Extension_On_Target);
840 Exec_Suffix := new String'(Name_Buffer
(1 .. Name_Len
));
843 if Exec_Suffix
'Length /= 0 then
844 Get_Name_String
(Name
);
847 if Only_If_No_Suffix
then
848 for J
in reverse 1 .. Name_Len
loop
849 if Name_Buffer
(J
) = '.' then
853 elsif Is_Directory_Separator
(Name_Buffer
(J
)) then
860 -- If Executable doesn't end with the executable suffix, add it
862 if Name_Len
<= Exec_Suffix
'Length
866 (Name_Len
- Exec_Suffix
'Length + 1 .. Name_Len
),
870 (Name_Len
+ 1 .. Name_Len
+ Exec_Suffix
'Length) :=
872 Name_Len
:= Name_Len
+ Exec_Suffix
'Length;
883 function Executable_Name
885 Only_If_No_Suffix
: Boolean := False) return String
887 Exec_Suffix
: String_Access
;
888 Add_Suffix
: Boolean;
891 if Executable_Extension_On_Target
= No_Name
then
892 Exec_Suffix
:= Get_Target_Executable_Suffix
;
894 Get_Name_String
(Executable_Extension_On_Target
);
895 Exec_Suffix
:= new String'(Name_Buffer (1 .. Name_Len));
898 if Exec_Suffix'Length = 0 then
904 Suffix : constant String := Exec_Suffix.all;
910 if Only_If_No_Suffix then
911 for J in reverse Name'Range loop
912 if Name (J) = '.' then
916 elsif Is_Directory_Separator (Name (J)) then
922 if Add_Suffix and then
923 (Name'Length <= Suffix'Length
926 (Name (Name'Last - Suffix'Length + 1 .. Name'Last),
930 Result : String (1 .. Name'Length + Suffix'Length);
932 Result (1 .. Name'Length) := Name;
933 Result (Name'Length + 1 .. Result'Last) := Suffix;
943 -----------------------
944 -- Executable_Prefix --
945 -----------------------
947 function Executable_Prefix return String_Ptr is
949 function Get_Install_Dir (Exec : String) return String_Ptr;
950 -- S is the executable name preceded by the absolute or relative
951 -- path, e.g. "c:\usr\bin\gcc.exe" or "..\bin\gcc".
953 ---------------------
954 -- Get_Install_Dir --
955 ---------------------
957 function Get_Install_Dir (Exec : String) return String_Ptr is
958 Full_Path : constant String := Normalize_Pathname (Exec);
959 -- Use the full path, so that we find "lib" or "bin", even when
960 -- the tool has been invoked with a relative path, as in
961 -- "./gnatls -v" invoked in the GNAT bin directory.
964 for J in reverse Full_Path'Range loop
965 if Is_Directory_Separator (Full_Path (J)) then
966 if J < Full_Path'Last - 5 then
967 if (To_Lower (Full_Path (J + 1)) = 'l
'
968 and then To_Lower (Full_Path (J + 2)) = 'i
'
969 and then To_Lower (Full_Path (J + 3)) = 'b
')
971 (To_Lower (Full_Path (J + 1)) = 'b
'
972 and then To_Lower (Full_Path (J + 2)) = 'i
'
973 and then To_Lower (Full_Path (J + 3)) = 'n
')
975 return new String'(Full_Path
(Full_Path
'First .. J
));
981 return new String'("");
984 -- Start of processing for Executable_Prefix
987 if Exec_Name = null then
988 Exec_Name := new String (1 .. Len_Arg (0));
989 Osint.Fill_Arg (Exec_Name (1)'Address, 0);
992 -- First determine if a path prefix was placed in front of the
995 for J in reverse Exec_Name'Range loop
996 if Is_Directory_Separator (Exec_Name (J)) then
997 return Get_Install_Dir (Exec_Name.all);
1001 -- If we come here, the user has typed the executable name with no
1002 -- directory prefix.
1004 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name.all).all);
1005 end Executable_Prefix;
1011 procedure Exit_Program (Exit_Code : Exit_Code_Type) is
1013 -- The program will exit with the following status:
1015 -- 0 if the object file has been generated (with or without warnings)
1016 -- 1 if recompilation was not needed (smart recompilation)
1017 -- 2 if gnat1 has been killed by a signal (detected by GCC)
1018 -- 4 for a fatal error
1019 -- 5 if there were errors
1020 -- 6 if no code has been generated (spec)
1022 -- Note that exit code 3 is not used and must not be used as this is
1023 -- the code returned by a program aborted via C abort() routine on
1024 -- Windows. GCC checks for that case and thinks that the child process
1025 -- has been aborted. This code (exit code 3) used to be the code used
1026 -- for E_No_Code, but E_No_Code was changed to 6 for this reason.
1029 when E_Success => OS_Exit (0);
1030 when E_Warnings => OS_Exit (0);
1031 when E_No_Compile => OS_Exit (1);
1032 when E_Fatal => OS_Exit (4);
1033 when E_Errors => OS_Exit (5);
1034 when E_No_Code => OS_Exit (6);
1035 when E_Abort => OS_Abort;
1043 procedure Fail (S : String) is
1045 -- We use Output in case there is a special output set up. In this case
1046 -- Set_Standard_Error will have no immediate effect.
1049 Osint.Write_Program_Name;
1054 Exit_Program (E_Fatal);
1057 ----------------------
1058 -- File_Names_Equal --
1059 ----------------------
1061 function File_Names_Equal (File1, File2 : String) return Boolean is
1063 function To_Lower (A : String) return String;
1064 -- For bootstrap reasons, we cannot use To_Lower function from
1065 -- System.Case_Util.
1071 function To_Lower (A : String) return String is
1072 Result : String := A;
1078 -- Start of processing for File_Names_Equal
1081 if File_Names_Case_Sensitive then
1082 return File1 = File2;
1084 return To_Lower (File1) = To_Lower (File2);
1086 end File_Names_Equal;
1092 function File_Hash (F : File_Name_Type) return File_Hash_Num is
1094 return File_Hash_Num (Int (F) mod File_Hash_Num'Range_Length);
1101 function File_Length
1102 (Name : C_File_Name;
1103 Attr : access File_Attributes) return Long_Integer
1108 A : System.Address) return CRTL.int64;
1109 pragma Import (C, Internal, "__gnat_file_length_attr");
1112 -- The conversion from int64 to Long_Integer is ok here as this
1113 -- routine is only to be used by the compiler and we do not expect
1114 -- a unit to be larger than a 32bit integer.
1116 return Long_Integer (Internal (-1, Name, Attr.all'Address));
1119 ---------------------
1120 -- File_Time_Stamp --
1121 ---------------------
1123 function File_Time_Stamp
1124 (Name : C_File_Name;
1125 Attr : access File_Attributes) return OS_Time
1127 function Internal (N : C_File_Name; A : System.Address) return OS_Time;
1128 pragma Import (C, Internal, "__gnat_file_time_name_attr");
1130 return Internal (Name, Attr.all'Address);
1131 end File_Time_Stamp;
1133 function File_Time_Stamp
1134 (Name : Path_Name_Type;
1135 Attr : access File_Attributes) return Time_Stamp_Type
1138 if Name = No_Path then
1139 return Empty_Time_Stamp;
1142 Get_Name_String (Name);
1143 Name_Buffer (Name_Len + 1) := ASCII.NUL;
1144 return OS_Time_To_GNAT_Time
1145 (File_Time_Stamp (Name_Buffer'Address, Attr));
1146 end File_Time_Stamp;
1152 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type is
1154 if Name = No_File then
1155 return Empty_Time_Stamp;
1158 Get_Name_String (Name);
1160 -- File_Time_Stamp will always return Invalid_Time if the file does
1161 -- not exist, and OS_Time_To_GNAT_Time will convert this value to
1162 -- Empty_Time_Stamp. Therefore we do not need to first test whether
1163 -- the file actually exists, which saves a system call.
1165 return OS_Time_To_GNAT_Time
1166 (File_Time_Stamp (Name_Buffer (1 .. Name_Len)));
1169 function File_Stamp (Name : Path_Name_Type) return Time_Stamp_Type is
1171 return File_Stamp (File_Name_Type (Name));
1179 (N : File_Name_Type;
1181 Full_Name : Boolean := False) return File_Name_Type
1183 Attr : aliased File_Attributes;
1184 Found : File_Name_Type;
1186 Find_File (N, T, Found, Attr'Access, Full_Name);
1195 (N : File_Name_Type;
1197 Found : out File_Name_Type;
1198 Attr : access File_Attributes;
1199 Full_Name : Boolean := False)
1202 Get_Name_String (N);
1205 File_Name : String renames Name_Buffer (1 .. Name_Len);
1206 File : File_Name_Type := No_File;
1210 -- If we are looking for a config file, look only in the current
1211 -- directory, i.e. return input argument unchanged. Also look only in
1212 -- the current directory if we are looking for a .dg file (happens in
1216 or else (Debug_Generated_Code
1217 and then Name_Len > 3
1218 and then Name_Buffer (Name_Len - 2 .. Name_Len) = ".dg")
1221 Attr.all := Unknown_Attributes;
1226 Full_Path : constant String :=
1227 Normalize_Pathname (Get_Name_String (N));
1228 Full_Size : constant Natural := Full_Path'Length;
1231 Name_Buffer (1 .. Full_Size) := Full_Path;
1232 Name_Len := Full_Size;
1237 -- Check that it is a file, not a directory
1239 if not Is_Regular_File (Get_Name_String (Found)) then
1246 -- If we are trying to find the current main file just look in the
1247 -- directory where the user said it was.
1249 elsif Look_In_Primary_Directory_For_Current_Main
1250 and then Current_Main = N
1252 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1255 -- Otherwise do standard search for source file
1258 -- Check the mapping of this file name
1260 File := Mapped_Path_Name (N);
1262 -- If the file name is mapped to a path name, return the
1263 -- corresponding path name
1265 if File /= No_File then
1267 -- For locally removed file, Error_Name is returned; then
1268 -- return No_File, indicating the file is not a source.
1270 if File = Error_File_Name then
1276 Attr.all := Unknown_Attributes;
1280 -- First place to look is in the primary directory (i.e. the same
1281 -- directory as the source) unless this has been disabled with -I-
1283 if Opt.Look_In_Primary_Dir then
1284 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1286 if Found /= No_File then
1291 -- Finally look in directories specified with switches -I/-aI/-aO
1294 Last_Dir := Lib_Search_Directories.Last;
1296 Last_Dir := Src_Search_Directories.Last;
1299 for D in Primary_Directory + 1 .. Last_Dir loop
1300 Locate_File (N, T, D, File_Name, Found, Attr);
1302 if Found /= No_File then
1307 Attr.all := Unknown_Attributes;
1313 -----------------------
1314 -- Find_Program_Name --
1315 -----------------------
1317 procedure Find_Program_Name is
1318 Command_Name : String (1 .. Len_Arg (0));
1319 Cindex1 : Integer := Command_Name'First;
1320 Cindex2 : Integer := Command_Name'Last;
1323 Fill_Arg (Command_Name'Address, 0);
1325 if Command_Name = "" then
1330 -- The program name might be specified by a full path name. However,
1331 -- we don't want to print that all out in an error message, so the
1332 -- path might need to be stripped away.
1334 for J in reverse Cindex1 .. Cindex2 loop
1335 if Is_Directory_Separator (Command_Name (J)) then
1341 -- Command_Name(Cindex1 .. Cindex2) is now the equivalent of the
1342 -- POSIX command "basename argv[0]"
1344 -- Strip off any executable extension (usually nothing or .exe)
1345 -- but formally reported by autoconf in the variable EXEEXT
1347 if Cindex2 - Cindex1 >= 4 then
1348 if To_Lower (Command_Name (Cindex2 - 3)) = '.'
1349 and then To_Lower (Command_Name (Cindex2 - 2)) = 'e
'
1350 and then To_Lower (Command_Name (Cindex2 - 1)) = 'x
'
1351 and then To_Lower (Command_Name (Cindex2)) = 'e
'
1353 Cindex2 := Cindex2 - 4;
1357 Name_Len := Cindex2 - Cindex1 + 1;
1358 Name_Buffer (1 .. Name_Len) := Command_Name (Cindex1 .. Cindex2);
1359 end Find_Program_Name;
1361 ------------------------
1362 -- Full_Lib_File_Name --
1363 ------------------------
1365 procedure Full_Lib_File_Name
1366 (N : File_Name_Type;
1367 Lib_File : out File_Name_Type;
1368 Attr : out File_Attributes)
1371 Smart_Find_File (N, Library, Lib_File, Attr);
1372 end Full_Lib_File_Name;
1374 ------------------------
1375 -- Full_Lib_File_Name --
1376 ------------------------
1378 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type is
1379 Attr : File_Attributes;
1380 File : File_Name_Type;
1382 Full_Lib_File_Name (N, File, Attr);
1384 end Full_Lib_File_Name;
1386 ----------------------------
1387 -- Full_Library_Info_Name --
1388 ----------------------------
1390 function Full_Library_Info_Name return File_Name_Type is
1392 return Current_Full_Lib_Name;
1393 end Full_Library_Info_Name;
1395 ---------------------------
1396 -- Full_Object_File_Name --
1397 ---------------------------
1399 function Full_Object_File_Name return File_Name_Type is
1401 return Current_Full_Obj_Name;
1402 end Full_Object_File_Name;
1404 ----------------------
1405 -- Full_Source_Name --
1406 ----------------------
1408 function Full_Source_Name return File_Name_Type is
1410 return Current_Full_Source_Name;
1411 end Full_Source_Name;
1413 ----------------------
1414 -- Full_Source_Name --
1415 ----------------------
1417 function Full_Source_Name (N : File_Name_Type) return File_Name_Type is
1419 return Smart_Find_File (N, Source);
1420 end Full_Source_Name;
1422 ----------------------
1423 -- Full_Source_Name --
1424 ----------------------
1426 procedure Full_Source_Name
1427 (N : File_Name_Type;
1428 Full_File : out File_Name_Type;
1429 Attr : access File_Attributes) is
1431 Smart_Find_File (N, Source, Full_File, Attr.all);
1432 end Full_Source_Name;
1438 function Get_Directory (Name : File_Name_Type) return File_Name_Type is
1440 Get_Name_String (Name);
1442 for J in reverse 1 .. Name_Len loop
1443 if Is_Directory_Separator (Name_Buffer (J)) then
1449 Name_Len := Hostparm.Normalized_CWD'Length;
1450 Name_Buffer (1 .. Name_Len) := Hostparm.Normalized_CWD;
1454 ------------------------------
1455 -- Get_First_Main_File_Name --
1456 ------------------------------
1458 function Get_First_Main_File_Name return String is
1460 return File_Names (1).all;
1461 end Get_First_Main_File_Name;
1463 --------------------------
1464 -- Get_Next_Dir_In_Path --
1465 --------------------------
1467 Search_Path_Pos : Integer;
1468 -- Keeps track of current position in search path. Initialized by the
1469 -- call to Get_Next_Dir_In_Path_Init, updated by Get_Next_Dir_In_Path.
1471 function Get_Next_Dir_In_Path
1472 (Search_Path : String_Access) return String_Access
1474 Lower_Bound : Positive := Search_Path_Pos;
1475 Upper_Bound : Positive;
1479 while Lower_Bound <= Search_Path'Last
1480 and then Search_Path.all (Lower_Bound) = Path_Separator
1482 Lower_Bound := Lower_Bound + 1;
1485 exit when Lower_Bound > Search_Path'Last;
1487 Upper_Bound := Lower_Bound;
1488 while Upper_Bound <= Search_Path'Last
1489 and then Search_Path.all (Upper_Bound) /= Path_Separator
1491 Upper_Bound := Upper_Bound + 1;
1494 Search_Path_Pos := Upper_Bound;
1495 return new String'(Search_Path
.all (Lower_Bound
.. Upper_Bound
- 1));
1499 end Get_Next_Dir_In_Path
;
1501 -------------------------------
1502 -- Get_Next_Dir_In_Path_Init --
1503 -------------------------------
1505 procedure Get_Next_Dir_In_Path_Init
(Search_Path
: String_Access
) is
1507 Search_Path_Pos
:= Search_Path
'First;
1508 end Get_Next_Dir_In_Path_Init
;
1510 --------------------------------------
1511 -- Get_Primary_Src_Search_Directory --
1512 --------------------------------------
1514 function Get_Primary_Src_Search_Directory
return String_Ptr
is
1516 return Src_Search_Directories
.Table
(Primary_Directory
);
1517 end Get_Primary_Src_Search_Directory
;
1519 ------------------------
1520 -- Get_RTS_Search_Dir --
1521 ------------------------
1523 function Get_RTS_Search_Dir
1524 (Search_Dir
: String;
1525 File_Type
: Search_File_Type
) return String_Ptr
1527 procedure Get_Current_Dir
1528 (Dir
: System
.Address
;
1529 Length
: System
.Address
);
1530 pragma Import
(C
, Get_Current_Dir
, "__gnat_get_current_dir");
1533 pragma Import
(C
, Max_Path
, "__gnat_max_path_len");
1534 -- Maximum length of a path name
1536 Current_Dir
: String_Ptr
;
1537 Default_Search_Dir
: String_Access
;
1538 Default_Suffix_Dir
: String_Access
;
1539 Local_Search_Dir
: String_Access
;
1540 Norm_Search_Dir
: String_Access
;
1541 Result_Search_Dir
: String_Access
;
1542 Search_File
: String_Access
;
1543 Temp_String
: String_Ptr
;
1546 -- Add a directory separator at the end of the directory if necessary
1547 -- so that we can directly append a file to the directory
1549 if not Is_Directory_Separator
(Search_Dir
(Search_Dir
'Last)) then
1551 new String'(Search_Dir & String'(1 => Directory_Separator
));
1553 Local_Search_Dir
:= new String'(Search_Dir);
1556 if File_Type = Include then
1557 Search_File := Include_Search_File;
1558 Default_Suffix_Dir := new String'("adainclude");
1560 Search_File
:= Objects_Search_File
;
1561 Default_Suffix_Dir
:= new String'("adalib");
1564 Norm_Search_Dir := Local_Search_Dir;
1566 if Is_Absolute_Path (Norm_Search_Dir.all) then
1568 -- We first verify if there is a directory Include_Search_Dir
1569 -- containing default search directories
1571 Result_Search_Dir :=
1572 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1573 Default_Search_Dir :=
1574 new String'(Norm_Search_Dir
.all & Default_Suffix_Dir
.all);
1575 Free
(Norm_Search_Dir
);
1577 if Result_Search_Dir
/= null then
1578 return String_Ptr
(Result_Search_Dir
);
1579 elsif Is_Directory
(Default_Search_Dir
.all) then
1580 return String_Ptr
(Default_Search_Dir
);
1585 -- Search in the current directory
1588 -- Get the current directory
1591 Buffer
: String (1 .. Max_Path
+ 2);
1592 Path_Len
: Natural := Max_Path
;
1595 Get_Current_Dir
(Buffer
'Address, Path_Len
'Address);
1597 if Path_Len
= 0 then
1598 raise Program_Error
;
1601 if not Is_Directory_Separator
(Buffer
(Path_Len
)) then
1602 Path_Len
:= Path_Len
+ 1;
1603 Buffer
(Path_Len
) := Directory_Separator
;
1606 Current_Dir
:= new String'(Buffer (1 .. Path_Len));
1610 new String'(Current_Dir
.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);
1618 Free (Norm_Search_Dir);
1620 if Result_Search_Dir /= null then
1621 return String_Ptr (Result_Search_Dir);
1623 elsif Is_Directory (Default_Search_Dir.all) then
1624 return String_Ptr (Default_Search_Dir);
1627 -- Search in Search_Dir_Prefix/Search_Dir
1631 (Update_Path
(Search_Dir_Prefix
).all & Local_Search_Dir
.all);
1633 Result_Search_Dir
:=
1634 Read_Default_Search_Dirs
(Norm_Search_Dir
, Search_File
, null);
1636 Default_Search_Dir
:=
1637 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1639 Free (Norm_Search_Dir);
1641 if Result_Search_Dir /= null then
1642 return String_Ptr (Result_Search_Dir);
1644 elsif Is_Directory (Default_Search_Dir.all) then
1645 return String_Ptr (Default_Search_Dir);
1648 -- We finally search in Search_Dir_Prefix/rts-Search_Dir
1651 new String'(Update_Path
(Search_Dir_Prefix
).all & "rts-");
1654 new String'(Temp_String.all & Local_Search_Dir.all);
1656 Result_Search_Dir :=
1657 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1659 Default_Search_Dir :=
1660 new String'(Norm_Search_Dir
.all & Default_Suffix_Dir
.all);
1661 Free
(Norm_Search_Dir
);
1663 if Result_Search_Dir
/= null then
1664 return String_Ptr
(Result_Search_Dir
);
1666 elsif Is_Directory
(Default_Search_Dir
.all) then
1667 return String_Ptr
(Default_Search_Dir
);
1675 end Get_RTS_Search_Dir
;
1677 --------------------------------
1678 -- Include_Dir_Default_Prefix --
1679 --------------------------------
1681 function Include_Dir_Default_Prefix
return String_Access
is
1683 if The_Include_Dir_Default_Prefix
= null then
1684 The_Include_Dir_Default_Prefix
:=
1685 String_Access
(Update_Path
(Include_Dir_Default_Name
));
1688 return The_Include_Dir_Default_Prefix
;
1689 end Include_Dir_Default_Prefix
;
1691 function Include_Dir_Default_Prefix
return String is
1693 return Include_Dir_Default_Prefix
.all;
1694 end Include_Dir_Default_Prefix
;
1700 procedure Initialize
is
1702 Number_File_Names
:= 0;
1703 Current_File_Name_Index
:= 0;
1705 Src_Search_Directories
.Init
;
1706 Lib_Search_Directories
.Init
;
1708 -- Start off by setting all suppress options, to False. The special
1709 -- overflow fields are set to Not_Set (they will be set by -gnatp, or
1710 -- by -gnato, or, if neither of these appear, in Adjust_Global_Switches
1713 Suppress_Options
:= ((others => False), Not_Set
, Not_Set
);
1715 -- Reserve the first slot in the search paths table. This is the
1716 -- directory of the main source file or main library file and is filled
1717 -- in by each call to Next_Main_Source/Next_Main_Lib_File with the
1718 -- directory specified for this main source or library file. This is the
1719 -- directory which is searched first by default. This default search is
1720 -- inhibited by the option -I- for both source and library files.
1722 Src_Search_Directories
.Set_Last
(Primary_Directory
);
1723 Src_Search_Directories
.Table
(Primary_Directory
) := new String'("");
1725 Lib_Search_Directories.Set_Last (Primary_Directory);
1726 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
1733 function Is_Directory
1734 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1736 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1737 pragma Import
(C
, Internal
, "__gnat_is_directory_attr");
1739 return Internal
(Name
, Attr
.all'Address) /= 0;
1742 ----------------------------
1743 -- Is_Directory_Separator --
1744 ----------------------------
1746 function Is_Directory_Separator
(C
: Character) return Boolean is
1748 -- In addition to the default directory_separator allow the '/' to
1749 -- act as separator since this is allowed in MS-DOS and Windows.
1751 return C
= Directory_Separator
or else C
= '/';
1752 end Is_Directory_Separator
;
1754 -------------------------
1755 -- Is_Readonly_Library --
1756 -------------------------
1758 function Is_Readonly_Library
(File
: File_Name_Type
) return Boolean is
1760 Get_Name_String
(File
);
1762 pragma Assert
(Name_Buffer
(Name_Len
- 3 .. Name_Len
) = ".ali");
1764 return not Is_Writable_File
(Name_Buffer
(1 .. Name_Len
));
1765 end Is_Readonly_Library
;
1767 ------------------------
1768 -- Is_Executable_File --
1769 ------------------------
1771 function Is_Executable_File
1772 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1774 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1775 pragma Import
(C
, Internal
, "__gnat_is_executable_file_attr");
1777 return Internal
(Name
, Attr
.all'Address) /= 0;
1778 end Is_Executable_File
;
1780 ----------------------
1781 -- Is_Readable_File --
1782 ----------------------
1784 function Is_Readable_File
1785 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1787 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1788 pragma Import
(C
, Internal
, "__gnat_is_readable_file_attr");
1790 return Internal
(Name
, Attr
.all'Address) /= 0;
1791 end Is_Readable_File
;
1793 ---------------------
1794 -- Is_Regular_File --
1795 ---------------------
1797 function Is_Regular_File
1798 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1800 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1801 pragma Import
(C
, Internal
, "__gnat_is_regular_file_attr");
1803 return Internal
(Name
, Attr
.all'Address) /= 0;
1804 end Is_Regular_File
;
1806 ----------------------
1807 -- Is_Symbolic_Link --
1808 ----------------------
1810 function Is_Symbolic_Link
1811 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1813 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1814 pragma Import
(C
, Internal
, "__gnat_is_symbolic_link_attr");
1816 return Internal
(Name
, Attr
.all'Address) /= 0;
1817 end Is_Symbolic_Link
;
1819 ----------------------
1820 -- Is_Writable_File --
1821 ----------------------
1823 function Is_Writable_File
1824 (Name
: C_File_Name
; Attr
: access File_Attributes
) return Boolean
1826 function Internal
(N
: C_File_Name
; A
: System
.Address
) return Integer;
1827 pragma Import
(C
, Internal
, "__gnat_is_writable_file_attr");
1829 return Internal
(Name
, Attr
.all'Address) /= 0;
1830 end Is_Writable_File
;
1836 function Lib_File_Name
1837 (Source_File
: File_Name_Type
;
1838 Munit_Index
: Nat
:= 0) return File_Name_Type
1841 Get_Name_String
(Source_File
);
1843 for J
in reverse 2 .. Name_Len
loop
1844 if Name_Buffer
(J
) = '.' then
1850 if Munit_Index
/= 0 then
1851 Add_Char_To_Name_Buffer
(Multi_Unit_Index_Character
);
1852 Add_Nat_To_Name_Buffer
(Munit_Index
);
1855 Add_Char_To_Name_Buffer
('.');
1856 Add_Str_To_Name_Buffer
(ALI_Suffix
.all);
1864 procedure Locate_File
1865 (N
: File_Name_Type
;
1869 Found
: out File_Name_Type
;
1870 Attr
: access File_Attributes
)
1872 Dir_Name
: String_Ptr
;
1875 -- If Name is already an absolute path, do not look for a directory
1877 if Is_Absolute_Path
(Name
) then
1880 elsif T
= Library
then
1881 Dir_Name
:= Lib_Search_Directories
.Table
(Dir
);
1884 pragma Assert
(T
/= Config
);
1885 Dir_Name
:= Src_Search_Directories
.Table
(Dir
);
1890 constant String (1 .. Dir_Name
'Length + Name
'Length + 1) :=
1891 Dir_Name
.all & Name
& ASCII
.NUL
;
1892 -- Use explicit bounds, because Dir_Name might be a substring whose
1896 Attr
.all := Unknown_Attributes
;
1898 if not Is_Regular_File
(Full_Name
'Address, Attr
) then
1902 -- If the file is in the current directory then return N itself
1904 if Dir_Name
'Length = 0 then
1908 Name_Find
(Full_Name
(Full_Name
'First .. Full_Name
'Last - 1));
1914 -------------------------------
1915 -- Matching_Full_Source_Name --
1916 -------------------------------
1918 function Matching_Full_Source_Name
1919 (N
: File_Name_Type
;
1920 T
: Time_Stamp_Type
) return File_Name_Type
1923 Get_Name_String
(N
);
1926 File_Name
: constant String := Name_Buffer
(1 .. Name_Len
);
1927 File
: File_Name_Type
:= No_File
;
1928 Attr
: aliased File_Attributes
;
1932 if Opt
.Look_In_Primary_Dir
then
1934 (N
, Source
, Primary_Directory
, File_Name
, File
,
1935 Attr
'Unchecked_Access);
1937 if File
/= No_File
and then T
= File_Stamp
(N
) then
1942 Last_Dir
:= Src_Search_Directories
.Last
;
1944 for D
in Primary_Directory
+ 1 .. Last_Dir
loop
1945 Locate_File
(N
, Source
, D
, File_Name
, File
, Attr
'Unchecked_Access);
1947 if File
/= No_File
and then T
= File_Stamp
(File
) then
1954 end Matching_Full_Source_Name
;
1960 function More_Files
return Boolean is
1962 return (Current_File_Name_Index
< Number_File_Names
);
1965 -------------------------------
1966 -- Nb_Dir_In_Obj_Search_Path --
1967 -------------------------------
1969 function Nb_Dir_In_Obj_Search_Path
return Natural is
1971 if Opt
.Look_In_Primary_Dir
then
1972 return Lib_Search_Directories
.Last
- Primary_Directory
+ 1;
1974 return Lib_Search_Directories
.Last
- Primary_Directory
;
1976 end Nb_Dir_In_Obj_Search_Path
;
1978 -------------------------------
1979 -- Nb_Dir_In_Src_Search_Path --
1980 -------------------------------
1982 function Nb_Dir_In_Src_Search_Path
return Natural is
1984 if Opt
.Look_In_Primary_Dir
then
1985 return Src_Search_Directories
.Last
- Primary_Directory
+ 1;
1987 return Src_Search_Directories
.Last
- Primary_Directory
;
1989 end Nb_Dir_In_Src_Search_Path
;
1991 --------------------
1992 -- Next_Main_File --
1993 --------------------
1995 function Next_Main_File
return File_Name_Type
is
1996 File_Name
: String_Ptr
;
1997 Dir_Name
: String_Ptr
;
2001 pragma Assert
(More_Files
);
2003 Current_File_Name_Index
:= Current_File_Name_Index
+ 1;
2005 -- Get the file and directory name
2007 File_Name
:= File_Names
(Current_File_Name_Index
);
2008 Fptr
:= File_Name
'First;
2010 for J
in reverse File_Name
'Range loop
2011 if Is_Directory_Separator
(File_Name
(J
)) then
2012 if J
= File_Name
'Last then
2013 Fail
("File name missing");
2021 -- Save name of directory in which main unit resides for use in
2022 -- locating other units
2024 Dir_Name
:= new String'(File_Name (File_Name'First .. Fptr - 1));
2026 case Running_Program is
2028 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
2029 Look_In_Primary_Directory_For_Current_Main := True;
2032 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
2034 if Fptr > File_Name'First then
2035 Look_In_Primary_Directory_For_Current_Main := True;
2041 Dir_Name := Normalize_Directory_Name (Dir_Name.all);
2042 Lib_Search_Directories.Table (Primary_Directory) := Dir_Name;
2048 Name_Len := File_Name'Last - Fptr + 1;
2049 Name_Buffer (1 .. Name_Len) := File_Name (Fptr .. File_Name'Last);
2050 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2051 Current_Main := Name_Find;
2053 -- In the gnatmake case, the main file may have not have the
2054 -- extension. Try ".adb" first then ".ads"
2056 if Running_Program = Make then
2058 Orig_Main : constant File_Name_Type := Current_Main;
2061 if Strip_Suffix (Orig_Main) = Orig_Main then
2063 Append_Suffix_To_File_Name (Orig_Main, ".adb");
2065 if Full_Source_Name (Current_Main) = No_File then
2067 Append_Suffix_To_File_Name (Orig_Main, ".ads");
2069 if Full_Source_Name (Current_Main) = No_File then
2070 Current_Main := Orig_Main;
2077 return Current_Main;
2080 ------------------------------
2081 -- Normalize_Directory_Name --
2082 ------------------------------
2084 function Normalize_Directory_Name (Directory : String) return String_Ptr is
2086 function Is_Quoted (Path : String) return Boolean;
2087 pragma Inline (Is_Quoted);
2088 -- Returns true if Path is quoted (either double or single quotes)
2094 function Is_Quoted (Path : String) return Boolean is
2095 First : constant Character := Path (Path'First);
2096 Last : constant Character := Path (Path'Last);
2099 if (First = ''' and then Last = ''')
2101 (First = '"' and then Last = '"')
2109 Result : String_Ptr;
2111 -- Start of processing for Normalize_Directory_Name
2114 if Directory'Length = 0 then
2115 Result := new String'(Hostparm
.Normalized_CWD
);
2117 elsif Is_Directory_Separator
(Directory
(Directory
'Last)) then
2118 Result
:= new String'(Directory);
2120 elsif Is_Quoted (Directory) then
2122 -- This is a quoted string, it certainly means that the directory
2123 -- contains some spaces for example. We can safely remove the quotes
2124 -- here as the OS_Lib.Normalize_Arguments will be called before any
2125 -- spawn routines. This ensure that quotes will be added when needed.
2127 Result := new String (1 .. Directory'Length - 1);
2128 Result (1 .. Directory'Length - 2) :=
2129 Directory (Directory'First + 1 .. Directory'Last - 1);
2130 Result (Result'Last) := Directory_Separator;
2133 Result := new String (1 .. Directory'Length + 1);
2134 Result (1 .. Directory'Length) := Directory;
2135 Result (Directory'Length + 1) := Directory_Separator;
2139 end Normalize_Directory_Name;
2141 ---------------------
2142 -- Number_Of_Files --
2143 ---------------------
2145 function Number_Of_Files return Nat is
2147 return Number_File_Names;
2148 end Number_Of_Files;
2150 -------------------------------
2151 -- Object_Dir_Default_Prefix --
2152 -------------------------------
2154 function Object_Dir_Default_Prefix return String is
2155 Object_Dir : String_Access :=
2156 String_Access (Update_Path (Object_Dir_Default_Name));
2159 if Object_Dir = null then
2164 Result : constant String := Object_Dir.all;
2170 end Object_Dir_Default_Prefix;
2172 ----------------------
2173 -- Object_File_Name --
2174 ----------------------
2176 function Object_File_Name (N : File_Name_Type) return File_Name_Type is
2182 Get_Name_String (N);
2183 Name_Len := Name_Len - ALI_Suffix'Length - 1;
2185 for J in Target_Object_Suffix'Range loop
2186 Name_Len := Name_Len + 1;
2187 Name_Buffer (Name_Len) := Target_Object_Suffix (J);
2191 end Object_File_Name;
2193 -------------------------------
2194 -- OS_Exit_Through_Exception --
2195 -------------------------------
2197 procedure OS_Exit_Through_Exception (Status : Integer) is
2199 Current_Exit_Status := Status;
2200 raise Types.Terminate_Program;
2201 end OS_Exit_Through_Exception;
2203 --------------------------
2204 -- OS_Time_To_GNAT_Time --
2205 --------------------------
2207 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type is
2208 GNAT_Time : Time_Stamp_Type;
2210 type Underlying_OS_Time is
2211 range -(2 ** 63) .. +(2 ** 63 - 1);
2212 -- Underlying_OS_Time is a redeclaration of OS_Time to allow integer
2213 -- manipulation. Remove this in favor of To_Ada/To_C once newer
2214 -- GNAT releases are available with these functions.
2217 new Ada.Unchecked_Conversion (OS_Time, Underlying_OS_Time);
2218 function From_Int is
2219 new Ada.Unchecked_Conversion (Underlying_OS_Time, OS_Time);
2221 TI : Underlying_OS_Time := To_Int (T);
2230 if T = Invalid_Time then
2231 return Empty_Time_Stamp;
2234 if On_Windows and then TI mod 2 > 0 then
2235 -- Windows ALI files had timestamps rounded to even seconds
2236 -- historically. The rounding was originally done in GM_Split.
2237 -- Now that GM_Split no longer does it, we are rounding it here
2238 -- only for ALI files.
2243 GM_Split (From_Int (TI), Y, Mo, D, H, Mn, S);
2250 Minutes => Nat (Mn),
2255 end OS_Time_To_GNAT_Time;
2261 function Prep_Suffix return String is
2270 function Program_Name (Nam : String; Prog : String) return String_Access is
2271 End_Of_Prefix : Natural := 0;
2272 Start_Of_Prefix : Positive := 1;
2273 Start_Of_Suffix : Positive;
2276 -- Get the name of the current program being executed
2280 -- Find the target prefix if any, for the cross compilation case.
2281 -- For instance in "powerpc-elf-gcc" the target prefix is
2283 -- Ditto for suffix, e.g. in "gcc-4.1", the suffix is "-4.1"
2285 for J in reverse 1 .. Name_Len loop
2286 if Is_Directory_Separator (Name_Buffer (J))
2287 or else Name_Buffer (J) = ':'
2289 Start_Of_Prefix := J + 1;
2294 -- Find End_Of_Prefix
2296 for J in Start_Of_Prefix .. Name_Len - Prog'Length + 1 loop
2297 if Name_Buffer (J .. J + Prog'Length - 1) = Prog then
2298 End_Of_Prefix := J - 1;
2303 Start_Of_Suffix := End_Of_Prefix + Prog'Length + 1;
2305 -- Create the new program name
2308 (Name_Buffer
(Start_Of_Prefix
.. End_Of_Prefix
)
2310 & Name_Buffer
(Start_Of_Suffix
.. Name_Len
));
2313 ------------------------------
2314 -- Read_Default_Search_Dirs --
2315 ------------------------------
2317 function Read_Default_Search_Dirs
2318 (Search_Dir_Prefix
: String_Access
;
2319 Search_File
: String_Access
;
2320 Search_Dir_Default_Name
: String_Access
) return String_Access
2322 Prefix_Len
: constant Integer := Search_Dir_Prefix
.all'Length;
2323 Buffer
: String (1 .. Prefix_Len
+ Search_File
.all'Length + 1);
2324 File_FD
: File_Descriptor
;
2325 S
, S1
: String_Access
;
2328 Actual_Len
: Integer;
2331 Prev_Was_Separator
: Boolean;
2332 Nb_Relative_Dir
: Integer;
2334 function Is_Relative
(S
: String; K
: Positive) return Boolean;
2335 pragma Inline
(Is_Relative
);
2336 -- Returns True if a relative directory specification is found
2337 -- in S at position K, False otherwise.
2343 function Is_Relative
(S
: String; K
: Positive) return Boolean is
2345 return not Is_Absolute_Path
(S
(K
.. S
'Last));
2348 -- Start of processing for Read_Default_Search_Dirs
2351 -- Construct a C compatible character string buffer
2353 Buffer
(1 .. Search_Dir_Prefix
.all'Length)
2354 := Search_Dir_Prefix
.all;
2355 Buffer
(Search_Dir_Prefix
.all'Length + 1 .. Buffer
'Last - 1)
2357 Buffer
(Buffer
'Last) := ASCII
.NUL
;
2359 File_FD
:= Open_Read
(Buffer
'Address, Binary
);
2360 if File_FD
= Invalid_FD
then
2361 return Search_Dir_Default_Name
;
2364 Len
:= Integer (File_Length
(File_FD
));
2366 -- An extra character for a trailing Path_Separator is allocated
2368 S
:= new String (1 .. Len
+ 1);
2369 S
(Len
+ 1) := Path_Separator
;
2371 -- Read the file. Note that the loop is probably not necessary since the
2372 -- whole file is read at once but the loop is harmless and that way we
2373 -- are sure to accommodate systems where this is not the case.
2377 while Actual_Len
/= 0 loop
2378 Actual_Len
:= Read
(File_FD
, S
(Curr
)'Address, Len
);
2379 Curr
:= Curr
+ Actual_Len
;
2382 -- Process the file, dealing with path separators
2384 Prev_Was_Separator
:= True;
2385 Nb_Relative_Dir
:= 0;
2386 for J
in 1 .. Len
loop
2388 -- Treat any EOL character as a path separator. Note that we do
2389 -- not treat space as a path separator (we used to treat space as a
2390 -- path separator in an earlier version). That way space can appear
2391 -- as a legitimate character in a path name.
2393 if S
(J
) = ASCII
.LF
or else S
(J
) = ASCII
.CR
then
2394 S
(J
) := Path_Separator
;
2397 -- Test for explicit path separator (or control char as above)
2399 if S
(J
) = Path_Separator
then
2400 Prev_Was_Separator
:= True;
2402 -- If not path separator, register use of relative directory
2405 if Prev_Was_Separator
and then Is_Relative
(S
.all, J
) then
2406 Nb_Relative_Dir
:= Nb_Relative_Dir
+ 1;
2409 Prev_Was_Separator
:= False;
2413 if Nb_Relative_Dir
= 0 then
2417 -- Add the Search_Dir_Prefix to all relative paths
2419 S1
:= new String (1 .. S
'Length + Nb_Relative_Dir
* Prefix_Len
);
2421 Prev_Was_Separator
:= True;
2422 for J
in 1 .. Len
+ 1 loop
2423 if S
(J
) = Path_Separator
then
2424 Prev_Was_Separator
:= True;
2427 if Prev_Was_Separator
and then Is_Relative
(S
.all, J
) then
2428 S1
(J1
.. J1
+ Prefix_Len
- 1) := Search_Dir_Prefix
.all;
2429 J1
:= J1
+ Prefix_Len
;
2432 Prev_Was_Separator
:= False;
2440 end Read_Default_Search_Dirs
;
2442 -----------------------
2443 -- Read_Library_Info --
2444 -----------------------
2446 function Read_Library_Info
2447 (Lib_File
: File_Name_Type
;
2448 Fatal_Err
: Boolean := False) return Text_Buffer_Ptr
2450 File
: File_Name_Type
;
2451 Attr
: aliased File_Attributes
;
2453 Find_File
(Lib_File
, Library
, File
, Attr
'Access);
2454 return Read_Library_Info_From_Full
2455 (Full_Lib_File
=> File
,
2456 Lib_File_Attr
=> Attr
'Access,
2457 Fatal_Err
=> Fatal_Err
);
2458 end Read_Library_Info
;
2460 ---------------------------------
2461 -- Read_Library_Info_From_Full --
2462 ---------------------------------
2464 function Read_Library_Info_From_Full
2465 (Full_Lib_File
: File_Name_Type
;
2466 Lib_File_Attr
: access File_Attributes
;
2467 Fatal_Err
: Boolean := False) return Text_Buffer_Ptr
2469 Lib_FD
: File_Descriptor
;
2470 -- The file descriptor for the current library file. A negative value
2471 -- indicates failure to open the specified source file.
2474 -- Length of source file text (ALI). If it doesn't fit in an integer
2475 -- we're probably stuck anyway (>2 gigs of source seems a lot, and
2476 -- there are other places in the compiler that make this assumption).
2478 Text
: Text_Buffer_Ptr
;
2479 -- Allocated text buffer
2482 pragma Warnings
(Off
, Status
);
2483 -- For the calls to Close
2486 Current_Full_Lib_Name
:= Full_Lib_File
;
2487 Current_Full_Obj_Name
:= Object_File_Name
(Current_Full_Lib_Name
);
2489 if Current_Full_Lib_Name
= No_File
then
2491 Fail
("Cannot find: " & Name_Buffer
(1 .. Name_Len
));
2493 Current_Full_Obj_Stamp
:= Empty_Time_Stamp
;
2498 Get_Name_String
(Current_Full_Lib_Name
);
2499 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
2501 -- Open the library FD, note that we open in binary mode, because as
2502 -- documented in the spec, the caller is expected to handle either
2503 -- DOS or Unix mode files, and there is no point in wasting time on
2504 -- text translation when it is not required.
2506 Lib_FD
:= Open_Read
(Name_Buffer
'Address, Binary
);
2508 if Lib_FD
= Invalid_FD
then
2510 Fail
("Cannot open: " & Name_Buffer
(1 .. Name_Len
));
2512 Current_Full_Obj_Stamp
:= Empty_Time_Stamp
;
2517 -- Compute the length of the file (potentially also preparing other data
2518 -- like the timestamp and whether the file is read-only, for future use)
2520 Len
:= Integer (File_Length
(Name_Buffer
'Address, Lib_File_Attr
));
2522 -- Check for object file consistency if requested
2524 if Opt
.Check_Object_Consistency
then
2525 -- On most systems, this does not result in an extra system call
2527 Current_Full_Lib_Stamp
:=
2528 OS_Time_To_GNAT_Time
2529 (File_Time_Stamp
(Name_Buffer
'Address, Lib_File_Attr
));
2531 -- ??? One system call here
2533 Current_Full_Obj_Stamp
:= File_Stamp
(Current_Full_Obj_Name
);
2535 if Current_Full_Obj_Stamp
(1) = ' ' then
2537 -- When the library is readonly always assume object is consistent
2538 -- The call to Is_Writable_File only results in a system call on
2539 -- some systems, but in most cases it has already been computed as
2540 -- part of the call to File_Length above.
2542 Get_Name_String
(Current_Full_Lib_Name
);
2543 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
2545 if not Is_Writable_File
(Name_Buffer
'Address, Lib_File_Attr
) then
2546 Current_Full_Obj_Stamp
:= Current_Full_Lib_Stamp
;
2548 elsif Fatal_Err
then
2549 Get_Name_String
(Current_Full_Obj_Name
);
2550 Close
(Lib_FD
, Status
);
2552 -- No need to check the status, we fail anyway
2554 Fail
("Cannot find: " & Name_Buffer
(1 .. Name_Len
));
2557 Current_Full_Obj_Stamp
:= Empty_Time_Stamp
;
2558 Close
(Lib_FD
, Status
);
2560 -- No need to check the status, we return null anyway
2565 elsif Current_Full_Obj_Stamp
< Current_Full_Lib_Stamp
then
2566 Close
(Lib_FD
, Status
);
2568 -- No need to check the status, we return null anyway
2574 -- Read data from the file
2577 Actual_Len
: Integer;
2579 Lo
: constant Text_Ptr
:= 0;
2580 -- Low bound for allocated text buffer
2582 Hi
: Text_Ptr
:= Text_Ptr
(Len
);
2583 -- High bound for allocated text buffer. Note length is Len + 1
2584 -- which allows for extra EOF character at the end of the buffer.
2587 -- Allocate text buffer. Note extra character at end for EOF
2589 Text
:= new Text_Buffer
(Lo
.. Hi
);
2591 -- Some systems have file types that require one read per line,
2592 -- so read until we get the Len bytes or until there are no more
2597 Actual_Len
:= Read
(Lib_FD
, Text
(Hi
)'Address, Len
);
2598 Hi
:= Hi
+ Text_Ptr
(Actual_Len
);
2599 exit when Actual_Len
= Len
or else Actual_Len
<= 0;
2605 -- Read is complete, close file and we are done
2607 Close
(Lib_FD
, Status
);
2608 -- The status should never be False. But, if it is, what can we do?
2609 -- So, we don't test it.
2613 end Read_Library_Info_From_Full
;
2615 ----------------------
2616 -- Read_Source_File --
2617 ----------------------
2619 procedure Read_Source_File
2620 (N
: File_Name_Type
;
2622 Hi
: out Source_Ptr
;
2623 Src
: out Source_Buffer_Ptr
;
2624 FD
: out File_Descriptor
;
2625 T
: File_Type
:= Source
)
2628 -- Length of file, assume no more than 2 gigabytes of source
2630 Actual_Len
: Integer;
2633 pragma Warnings
(Off
, Status
);
2634 -- For the call to Close
2637 Current_Full_Source_Name
:= Find_File
(N
, T
, Full_Name
=> True);
2638 Current_Full_Source_Stamp
:= File_Stamp
(Current_Full_Source_Name
);
2640 if Current_Full_Source_Name
= No_File
then
2642 -- If we were trying to access the main file and we could not find
2643 -- it, we have an error.
2645 if N
= Current_Main
then
2646 Get_Name_String
(N
);
2647 Fail
("Cannot find: " & Name_Buffer
(1 .. Name_Len
));
2656 Get_Name_String
(Current_Full_Source_Name
);
2657 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
2659 -- Open the source FD, note that we open in binary mode, because as
2660 -- documented in the spec, the caller is expected to handle either
2661 -- DOS or Unix mode files, and there is no point in wasting time on
2662 -- text translation when it is not required.
2664 FD
:= Open_Read
(Name_Buffer
'Address, Binary
);
2666 if FD
= Invalid_FD
then
2672 -- If it's a Source file, print out the file name, if requested, and if
2673 -- it's not part of the runtimes, store it in File_Name_Chars. We don't
2674 -- want to print non-Source files, like GNAT-TEMP-000001.TMP used to
2675 -- pass information from gprbuild to gcc. We don't want to save runtime
2676 -- file names, because we don't want users to send them in bug reports.
2680 Name
: String renames Name_Buffer
(1 .. Name_Len
);
2681 Inc
: String renames Include_Dir_Default_Prefix
.all;
2683 Part_Of_Runtimes
: constant Boolean :=
2685 and then Inc
'Length < Name_Len
2686 and then Name_Buffer
(1 .. Inc
'Length) = Inc
;
2689 if Debug
.Debug_Flag_Dot_N
then
2693 if not Part_Of_Runtimes
then
2694 File_Name_Chars
.Append_All
(File_Name_Chars
.Table_Type
(Name
));
2695 File_Name_Chars
.Append
(ASCII
.LF
);
2700 -- Prepare to read data from the file
2702 Len
:= Integer (File_Length
(FD
));
2704 -- Set Hi so that length is one more than the physical length,
2705 -- allowing for the extra EOF character at the end of the buffer
2707 Hi
:= Lo
+ Source_Ptr
(Len
);
2709 -- Do the actual read operation
2712 Var_Ptr
: constant Source_Buffer_Ptr_Var
:=
2713 new Source_Buffer
(Lo
.. Hi
);
2714 -- Allocate source buffer, allowing extra character at end for EOF
2716 -- Some systems have file types that require one read per line,
2717 -- so read until we get the Len bytes or until there are no more
2722 Actual_Len
:= Read
(FD
, Var_Ptr
(Hi
)'Address, Len
);
2723 Hi
:= Hi
+ Source_Ptr
(Actual_Len
);
2724 exit when Actual_Len
= Len
or else Actual_Len
<= 0;
2727 Var_Ptr
(Hi
) := EOF
;
2728 Src
:= Var_Ptr
.all'Access;
2731 -- Read is complete, get time stamp and close file and we are done
2735 -- The status should never be False. But, if it is, what can we do?
2736 -- So, we don't test it.
2738 -- ???We don't really need to return Hi anymore; We could get rid of
2739 -- it. We could also make this into a function.
2741 pragma Assert
(Hi
= Src
'Last);
2742 end Read_Source_File
;
2748 function Relocate_Path
2750 Path
: String) return String_Ptr
2754 procedure set_std_prefix
(S
: String; Len
: Integer);
2755 pragma Import
(C
, set_std_prefix
);
2758 if Std_Prefix
= null then
2759 Std_Prefix
:= String_Ptr
(Getenv
("GNSA_ROOT"));
2761 if Std_Prefix
.all = "" then
2762 Std_Prefix
:= Executable_Prefix
;
2764 elsif not Is_Directory_Separator
(Std_Prefix
(Std_Prefix
'Last)) then
2766 -- The remainder of this function assumes that Std_Prefix
2767 -- terminates with a dir separator, so we force this here.
2770 Old_Prefix
: String_Ptr
:= Std_Prefix
;
2772 Std_Prefix
:= new String (1 .. Old_Prefix
'Length + 1);
2773 Std_Prefix
(1 .. Old_Prefix
'Length) := Old_Prefix
.all;
2774 Std_Prefix
(Old_Prefix
'Length + 1) := Directory_Separator
;
2779 if Std_Prefix
.all /= "" then
2781 -- Remove trailing directory separator when calling set_std_prefix
2783 set_std_prefix
(Std_Prefix
.all, Std_Prefix
'Length - 1);
2787 if Path
'Last >= Prefix
'Last and then Path
(Prefix
'Range) = Prefix
then
2788 if Std_Prefix
.all /= "" then
2790 (1 .. Std_Prefix
'Length + Path
'Last - Prefix
'Last);
2791 S
(1 .. Std_Prefix
'Length) := Std_Prefix
.all;
2792 S
(Std_Prefix
'Length + 1 .. S
'Last) :=
2793 Path
(Prefix
'Last + 1 .. Path
'Last);
2798 return new String'(Path);
2805 procedure Set_Program (P : Program_Type) is
2808 Fail ("Set_Program called twice");
2811 Program_Set := True;
2812 Running_Program := P;
2819 function Shared_Lib (Name : String) return String is
2820 Library : String (1 .. Name'Length + Library_Version'Length + 3);
2821 -- 3 = 2 for "-l" + 1 for "-" before lib version
2824 Library (1 .. 2) := "-l";
2825 Library (3 .. 2 + Name'Length) := Name;
2826 Library (3 + Name'Length) := '-';
2827 Library (4 + Name'Length .. Library'Last) := Library_Version;
2831 ----------------------
2832 -- Smart_File_Stamp --
2833 ----------------------
2835 function Smart_File_Stamp
2836 (N : File_Name_Type;
2837 T : File_Type) return Time_Stamp_Type
2839 File : File_Name_Type;
2840 Attr : aliased File_Attributes;
2843 if not File_Cache_Enabled then
2844 Find_File (N, T, File, Attr'Access);
2846 Smart_Find_File (N, T, File, Attr);
2849 if File = No_File then
2850 return Empty_Time_Stamp;
2852 Get_Name_String (File);
2853 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2855 OS_Time_To_GNAT_Time
2856 (File_Time_Stamp (Name_Buffer'Address, Attr'Access));
2858 end Smart_File_Stamp;
2860 ---------------------
2861 -- Smart_Find_File --
2862 ---------------------
2864 function Smart_Find_File
2865 (N : File_Name_Type;
2866 T : File_Type) return File_Name_Type
2868 File : File_Name_Type;
2869 Attr : File_Attributes;
2871 Smart_Find_File (N, T, File, Attr);
2873 end Smart_Find_File;
2875 ---------------------
2876 -- Smart_Find_File --
2877 ---------------------
2879 procedure Smart_Find_File
2880 (N : File_Name_Type;
2882 Found : out File_Name_Type;
2883 Attr : out File_Attributes)
2885 Info : File_Info_Cache;
2888 if not File_Cache_Enabled then
2889 Find_File (N, T, Info.File, Info.Attr'Access);
2892 Info := File_Name_Hash_Table.Get (N);
2894 if Info.File = No_File then
2895 Find_File (N, T, Info.File, Info.Attr'Access);
2896 File_Name_Hash_Table.Set (N, Info);
2902 end Smart_Find_File;
2904 ----------------------
2905 -- Source_File_Data --
2906 ----------------------
2908 procedure Source_File_Data (Cache : Boolean) is
2910 File_Cache_Enabled := Cache;
2911 end Source_File_Data;
2913 -----------------------
2914 -- Source_File_Stamp --
2915 -----------------------
2917 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
2919 return Smart_File_Stamp (N, Source);
2920 end Source_File_Stamp;
2922 ---------------------
2923 -- Strip_Directory --
2924 ---------------------
2926 function Strip_Directory (Name : File_Name_Type) return File_Name_Type is
2928 Get_Name_String (Name);
2930 for J in reverse 1 .. Name_Len - 1 loop
2932 -- If we find the last directory separator
2934 if Is_Directory_Separator (Name_Buffer (J)) then
2936 -- Return part of Name that follows this last directory separator
2938 Name_Buffer (1 .. Name_Len - J) := Name_Buffer (J + 1 .. Name_Len);
2939 Name_Len := Name_Len - J;
2944 -- There were no directory separator, just return Name
2947 end Strip_Directory;
2953 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type is
2955 Get_Name_String (Name);
2957 for J in reverse 2 .. Name_Len loop
2959 -- If we found the last '.', return part of Name that precedes it
2961 if Name_Buffer (J) = '.' then
2970 ---------------------------
2971 -- To_Canonical_File_List --
2972 ---------------------------
2974 function To_Canonical_File_List
2975 (Wildcard_Host_File : String;
2976 Only_Dirs : Boolean) return String_Access_List_Access
2978 function To_Canonical_File_List_Init
2979 (Host_File : Address;
2980 Only_Dirs : Integer) return Integer;
2981 pragma Import (C, To_Canonical_File_List_Init,
2982 "__gnat_to_canonical_file_list_init");
2984 function To_Canonical_File_List_Next return Address;
2985 pragma Import (C, To_Canonical_File_List_Next,
2986 "__gnat_to_canonical_file_list_next");
2988 procedure To_Canonical_File_List_Free;
2989 pragma Import (C, To_Canonical_File_List_Free,
2990 "__gnat_to_canonical_file_list_free");
2992 Num_Files : Integer;
2993 C_Wildcard_Host_File : String (1 .. Wildcard_Host_File'Length + 1);
2996 C_Wildcard_Host_File (1 .. Wildcard_Host_File'Length) :=
2998 C_Wildcard_Host_File (C_Wildcard_Host_File'Last) := ASCII.NUL;
3000 -- Do the expansion and say how many there are
3002 Num_Files := To_Canonical_File_List_Init
3003 (C_Wildcard_Host_File'Address, Boolean'Pos (Only_Dirs));
3006 Canonical_File_List : String_Access_List (1 .. Num_Files);
3007 Canonical_File_Addr : Address;
3008 Canonical_File_Len : CRTL.size_t;
3011 -- Retrieve the expanded directory names and build the list
3013 for J in 1 .. Num_Files loop
3014 Canonical_File_Addr := To_Canonical_File_List_Next;
3015 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
3016 Canonical_File_List (J) := To_Path_String_Access
3017 (Canonical_File_Addr, Canonical_File_Len);
3020 -- Free up the storage
3022 To_Canonical_File_List_Free;
3024 return new String_Access_List'(Canonical_File_List
);
3026 end To_Canonical_File_List
;
3028 ----------------------
3029 -- To_Host_Dir_Spec --
3030 ----------------------
3032 function To_Host_Dir_Spec
3033 (Canonical_Dir
: String;
3034 Prefix_Style
: Boolean) return String_Access
3036 function To_Host_Dir_Spec
3037 (Canonical_Dir
: Address
;
3038 Prefix_Flag
: Integer) return Address
;
3039 pragma Import
(C
, To_Host_Dir_Spec
, "__gnat_to_host_dir_spec");
3041 C_Canonical_Dir
: String (1 .. Canonical_Dir
'Length + 1);
3042 Host_Dir_Addr
: Address
;
3043 Host_Dir_Len
: CRTL
.size_t
;
3046 C_Canonical_Dir
(1 .. Canonical_Dir
'Length) := Canonical_Dir
;
3047 C_Canonical_Dir
(C_Canonical_Dir
'Last) := ASCII
.NUL
;
3049 if Prefix_Style
then
3050 Host_Dir_Addr
:= To_Host_Dir_Spec
(C_Canonical_Dir
'Address, 1);
3052 Host_Dir_Addr
:= To_Host_Dir_Spec
(C_Canonical_Dir
'Address, 0);
3054 Host_Dir_Len
:= C_String_Length
(Host_Dir_Addr
);
3056 if Host_Dir_Len
= 0 then
3059 return To_Path_String_Access
(Host_Dir_Addr
, Host_Dir_Len
);
3061 end To_Host_Dir_Spec
;
3063 -----------------------
3064 -- To_Host_File_Spec --
3065 -----------------------
3067 function To_Host_File_Spec
3068 (Canonical_File
: String) return String_Access
3070 function To_Host_File_Spec
(Canonical_File
: Address
) return Address
;
3071 pragma Import
(C
, To_Host_File_Spec
, "__gnat_to_host_file_spec");
3073 C_Canonical_File
: String (1 .. Canonical_File
'Length + 1);
3074 Host_File_Addr
: Address
;
3075 Host_File_Len
: CRTL
.size_t
;
3078 C_Canonical_File
(1 .. Canonical_File
'Length) := Canonical_File
;
3079 C_Canonical_File
(C_Canonical_File
'Last) := ASCII
.NUL
;
3081 Host_File_Addr
:= To_Host_File_Spec
(C_Canonical_File
'Address);
3082 Host_File_Len
:= C_String_Length
(Host_File_Addr
);
3084 if Host_File_Len
= 0 then
3087 return To_Path_String_Access
3088 (Host_File_Addr
, Host_File_Len
);
3090 end To_Host_File_Spec
;
3092 ---------------------------
3093 -- To_Path_String_Access --
3094 ---------------------------
3096 function To_Path_String_Access
3097 (Path_Addr
: Address
;
3098 Path_Len
: CRTL
.size_t
) return String_Access
3100 subtype Path_String
is String (1 .. Integer (Path_Len
));
3101 type Path_String_Access
is access Path_String
;
3103 function Address_To_Access
is new
3104 Ada
.Unchecked_Conversion
(Source
=> Address
,
3105 Target
=> Path_String_Access
);
3107 Path_Access
: constant Path_String_Access
:=
3108 Address_To_Access
(Path_Addr
);
3110 Return_Val
: String_Access
;
3113 Return_Val
:= new String (1 .. Integer (Path_Len
));
3115 for J
in 1 .. Integer (Path_Len
) loop
3116 Return_Val
(J
) := Path_Access
(J
);
3120 end To_Path_String_Access
;
3126 function Update_Path
(Path
: String_Ptr
) return String_Ptr
is
3128 function C_Update_Path
(Path
, Component
: Address
) return Address
;
3129 pragma Import
(C
, C_Update_Path
, "update_path");
3131 In_Length
: constant Integer := Path
'Length;
3132 In_String
: String (1 .. In_Length
+ 1);
3133 Component_Name
: aliased String := "GCC" & ASCII
.NUL
;
3134 Result_Ptr
: Address
;
3135 Result_Length
: CRTL
.size_t
;
3136 Out_String
: String_Ptr
;
3139 In_String
(1 .. In_Length
) := Path
.all;
3140 In_String
(In_Length
+ 1) := ASCII
.NUL
;
3141 Result_Ptr
:= C_Update_Path
(In_String
'Address, Component_Name
'Address);
3142 Result_Length
:= CRTL
.strlen
(Result_Ptr
);
3144 Out_String
:= new String (1 .. Integer (Result_Length
));
3145 CRTL
.strncpy
(Out_String
.all'Address, Result_Ptr
, Result_Length
);
3153 procedure Write_Info
(Info
: String) is
3155 Write_With_Check
(Info
'Address, Info
'Length);
3156 Write_With_Check
(EOL
'Address, 1);
3159 ------------------------
3160 -- Write_Program_Name --
3161 ------------------------
3163 procedure Write_Program_Name
is
3164 Save_Buffer
: constant String (1 .. Name_Len
) :=
3165 Name_Buffer
(1 .. Name_Len
);
3170 -- Convert the name to lower case so error messages are the same on
3173 for J
in 1 .. Name_Len
loop
3174 if Name_Buffer
(J
) in 'A' .. 'Z' then
3176 Character'Val (Character'Pos (Name_Buffer
(J
)) + 32);
3180 Write_Str
(Name_Buffer
(1 .. Name_Len
));
3182 -- Restore Name_Buffer which was clobbered by the call to
3183 -- Find_Program_Name
3185 Name_Len
:= Save_Buffer
'Last;
3186 Name_Buffer
(1 .. Name_Len
) := Save_Buffer
;
3187 end Write_Program_Name
;
3189 ----------------------
3190 -- Write_With_Check --
3191 ----------------------
3193 procedure Write_With_Check
(A
: Address
; N
: Integer) is
3196 if N
= Write
(Output_FD
, A
, N
) then
3199 Write_Str
("error: disk full writing ");
3200 Write_Name_Decoded
(Output_File_Name
);
3202 Name_Len
:= Name_Len
+ 1;
3203 Name_Buffer
(Name_Len
) := ASCII
.NUL
;
3204 Delete_File
(Name_Buffer
'Address, Ignore
);
3205 Exit_Program
(E_Fatal
);
3207 end Write_With_Check
;
3209 ----------------------------
3210 -- Package Initialization --
3211 ----------------------------
3213 procedure Reset_File_Attributes
(Attr
: System
.Address
);
3214 pragma Import
(C
, Reset_File_Attributes
, "__gnat_reset_attributes");
3217 Initialization
: declare
3219 function Get_Default_Identifier_Character_Set
return Character;
3220 pragma Import
(C
, Get_Default_Identifier_Character_Set
,
3221 "__gnat_get_default_identifier_character_set");
3222 -- Function to determine the default identifier character set,
3223 -- which is system dependent. See Opt package spec for a list of
3224 -- the possible character codes and their interpretations.
3226 function Get_Maximum_File_Name_Length
return Int
;
3227 pragma Import
(C
, Get_Maximum_File_Name_Length
,
3228 "__gnat_get_maximum_file_name_length");
3229 -- Function to get maximum file name length for system
3231 Sizeof_File_Attributes
: Integer;
3232 pragma Import
(C
, Sizeof_File_Attributes
,
3233 "__gnat_size_of_file_attributes");
3236 pragma Assert
(Sizeof_File_Attributes
<= File_Attributes_Size
);
3238 Reset_File_Attributes
(Unknown_Attributes
'Address);
3240 Identifier_Character_Set
:= Get_Default_Identifier_Character_Set
;
3241 Maximum_File_Name_Length
:= Get_Maximum_File_Name_Length
;
3243 -- Following should be removed by having above function return
3244 -- Integer'Last as indication of no maximum instead of -1 ???
3246 if Maximum_File_Name_Length
= -1 then
3247 Maximum_File_Name_Length
:= Int
'Last;
3250 Src_Search_Directories
.Set_Last
(Primary_Directory
);
3251 Src_Search_Directories
.Table
(Primary_Directory
) := new String'("");
3253 Lib_Search_Directories.Set_Last (Primary_Directory);
3254 Lib_Search_Directories.Table (Primary_Directory) := new String'("");