1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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 ------------------------------------------------------------------------------
26 with Unchecked_Conversion;
28 with System.Case_Util; use System.Case_Util;
35 with Gnatvsn; use Gnatvsn;
38 with Output; use Output;
39 with Sdefault; use Sdefault;
41 with Targparm; use Targparm;
45 Running_Program : Program_Type := Unspecified;
46 -- comment required here ???
48 Program_Set : Boolean := False;
49 -- comment required here ???
51 Std_Prefix : String_Ptr;
52 -- Standard prefix, computed dynamically the first time Relocate_Path
53 -- is called, and cached for subsequent calls.
55 Empty : aliased String := "";
56 No_Dir : constant String_Ptr := Empty'Access;
57 -- Used in Locate_File as a fake directory when Name is already an
60 -------------------------------------
61 -- Use of Name_Find and Name_Enter --
62 -------------------------------------
64 -- This package creates a number of source, ALI and object file names
65 -- that are used to locate the actual file and for the purpose of message
66 -- construction. These names need not be accessible by Name_Find, and can
67 -- be therefore created by using routine Name_Enter. The files in question
68 -- are file names with a prefix directory (i.e., the files not in the
69 -- current directory). File names without a prefix directory are entered
70 -- with Name_Find because special values might be attached to the various
71 -- Info fields of the corresponding name table entry.
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 function Append_Suffix_To_File_Name
78 (Name : File_Name_Type;
79 Suffix : String) return File_Name_Type;
80 -- Appends Suffix to Name and returns the new name
82 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type;
83 -- Convert OS format time to GNAT format time stamp.
84 -- Returns Empty_Time_Stamp if T is Invalid_Time
86 function Executable_Prefix return String_Ptr;
87 -- Returns the name of the root directory where the executable is stored.
88 -- The executable must be located in a directory called "bin", or under
89 -- root/lib/gcc-lib/..., or under root/libexec/gcc/... For example, if
90 -- executable is stored in directory "/foo/bar/bin", this routine returns
91 -- "/foo/bar/". Return "" if location is not recognized as described above.
93 function Update_Path (Path : String_Ptr) return String_Ptr;
94 -- Update the specified path to replace the prefix with the location
95 -- where GNAT is installed. See the file prefix.c in GCC for details.
101 Name : String) return File_Name_Type;
102 -- See if the file N whose name is Name exists in directory Dir. Dir is an
103 -- index into the Lib_Search_Directories table if T = Library. Otherwise
104 -- if T = Source, Dir is an index into the Src_Search_Directories table.
105 -- Returns the File_Name_Type of the full file name if file found, or
106 -- No_File if not found.
108 function C_String_Length (S : Address) return Integer;
109 -- Returns length of a C string (zero for a null address)
111 function To_Path_String_Access
112 (Path_Addr : Address;
113 Path_Len : Integer) return String_Access;
114 -- Converts a C String to an Ada String. Are we doing this to avoid withing
115 -- Interfaces.C.Strings ???
117 function Include_Dir_Default_Prefix return String_Access;
118 -- Same as exported version, except returns a String_Access
120 ------------------------------
121 -- Other Local Declarations --
122 ------------------------------
124 EOL : constant Character := ASCII.LF;
125 -- End of line character
127 Number_File_Names : Int := 0;
128 -- Number of file names found on command line and placed in File_Names
130 Look_In_Primary_Directory_For_Current_Main : Boolean := False;
131 -- When this variable is True, Find_File only looks in Primary_Directory
132 -- for the Current_Main file. This variable is always set to True for the
133 -- compiler. It is also True for gnatmake, when the source name given on
134 -- the command line has directory information.
136 Current_Full_Source_Name : File_Name_Type := No_File;
137 Current_Full_Source_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
138 Current_Full_Lib_Name : File_Name_Type := No_File;
139 Current_Full_Lib_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
140 Current_Full_Obj_Name : File_Name_Type := No_File;
141 Current_Full_Obj_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
142 -- Respectively full name (with directory info) and time stamp of the
143 -- latest source, library and object files opened by Read_Source_File and
144 -- Read_Library_Info.
146 package File_Name_Chars is new Table.Table (
147 Table_Component_Type => Character,
148 Table_Index_Type => Int,
149 Table_Low_Bound => 1,
150 Table_Initial => Alloc.File_Name_Chars_Initial,
151 Table_Increment => Alloc.File_Name_Chars_Increment,
152 Table_Name => "File_Name_Chars");
153 -- Table to store text to be printed by Dump_Source_File_Names
155 The_Include_Dir_Default_Prefix : String_Access := null;
156 -- Value returned by Include_Dir_Default_Prefix. We don't initialize it
157 -- here, because that causes an elaboration cycle with Sdefault; we
158 -- initialize it lazily instead.
164 Primary_Directory : constant := 0;
165 -- This is index in the tables created below for the first directory to
166 -- search in for source or library information files. This is the directory
167 -- containing the latest main input file (a source file for the compiler or
168 -- a library file for the binder).
170 package Src_Search_Directories is new Table.Table (
171 Table_Component_Type => String_Ptr,
172 Table_Index_Type => Integer,
173 Table_Low_Bound => Primary_Directory,
175 Table_Increment => 100,
176 Table_Name => "Osint.Src_Search_Directories");
177 -- Table of names of directories in which to search for source (Compiler)
178 -- files. This table is filled in the order in which the directories are
179 -- to be searched, and then used in that order.
181 package Lib_Search_Directories is new Table.Table (
182 Table_Component_Type => String_Ptr,
183 Table_Index_Type => Integer,
184 Table_Low_Bound => Primary_Directory,
186 Table_Increment => 100,
187 Table_Name => "Osint.Lib_Search_Directories");
188 -- Table of names of directories in which to search for library (Binder)
189 -- files. This table is filled in the order in which the directories are
190 -- to be searched and then used in that order. The reason for having two
191 -- distinct tables is that we need them both in gnatmake.
193 ---------------------
194 -- File Hash Table --
195 ---------------------
197 -- The file hash table is provided to free the programmer from any
198 -- efficiency concern when retrieving full file names or time stamps of
199 -- source files. If the programmer calls Source_File_Data (Cache => True)
200 -- he is guaranteed that the price to retrieve the full name (i.e. with
201 -- directory info) or time stamp of the file will be payed only once, the
202 -- first time the full name is actually searched (or the first time the
203 -- time stamp is actually retrieved). This is achieved by employing a hash
204 -- table that stores as a key the File_Name_Type of the file and associates
205 -- to that File_Name_Type the full file name and time stamp of the file.
207 File_Cache_Enabled : Boolean := False;
208 -- Set to true if you want the enable the file data caching mechanism
210 type File_Hash_Num is range 0 .. 1020;
212 function File_Hash (F : File_Name_Type) return File_Hash_Num;
213 -- Compute hash index for use by Simple_HTable
215 package File_Name_Hash_Table is new GNAT.HTable.Simple_HTable (
216 Header_Num => File_Hash_Num,
217 Element => File_Name_Type,
218 No_Element => No_File,
219 Key => File_Name_Type,
223 package File_Stamp_Hash_Table is new GNAT.HTable.Simple_HTable (
224 Header_Num => File_Hash_Num,
225 Element => Time_Stamp_Type,
226 No_Element => Empty_Time_Stamp,
227 Key => File_Name_Type,
231 function Smart_Find_File
233 T : File_Type) return File_Name_Type;
234 -- Exactly like Find_File except that if File_Cache_Enabled is True this
235 -- routine looks first in the hash table to see if the full name of the
236 -- file is already available.
238 function Smart_File_Stamp
240 T : File_Type) return Time_Stamp_Type;
241 -- Takes the same parameter as the routine above (N is a file name without
242 -- any prefix directory information) and behaves like File_Stamp except
243 -- that if File_Cache_Enabled is True this routine looks first in the hash
244 -- table to see if the file stamp of the file is already available.
246 -----------------------------
247 -- Add_Default_Search_Dirs --
248 -----------------------------
250 procedure Add_Default_Search_Dirs is
251 Search_Dir : String_Access;
252 Search_Path : String_Access;
253 Path_File_Name : String_Access;
255 procedure Add_Search_Dir
256 (Search_Dir : String;
257 Additional_Source_Dir : Boolean);
258 procedure Add_Search_Dir
259 (Search_Dir : String_Access;
260 Additional_Source_Dir : Boolean);
261 -- Add a source search dir or a library search dir, depending on the
262 -- value of Additional_Source_Dir.
264 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean);
265 -- Open a path file and read the directory to search, one per line
267 function Get_Libraries_From_Registry return String_Ptr;
268 -- On Windows systems, get the list of installed standard libraries
269 -- from the registry key:
271 -- HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\
272 -- GNAT\Standard Libraries
273 -- Return an empty string on other systems.
275 -- Note that this is an undocumented legacy feature, and that it
276 -- works only when using the default runtime library (i.e. no --RTS=
277 -- command line switch).
283 procedure Add_Search_Dir
284 (Search_Dir : String;
285 Additional_Source_Dir : Boolean)
288 if Additional_Source_Dir then
289 Add_Src_Search_Dir (Search_Dir);
291 Add_Lib_Search_Dir (Search_Dir);
295 procedure Add_Search_Dir
296 (Search_Dir : String_Access;
297 Additional_Source_Dir : Boolean)
300 if Additional_Source_Dir then
301 Add_Src_Search_Dir (Search_Dir.all);
303 Add_Lib_Search_Dir (Search_Dir.all);
307 ------------------------
308 -- Get_Dirs_From_File --
309 ------------------------
311 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean) is
312 File_FD : File_Descriptor;
313 Buffer : constant String := Path_File_Name.all & ASCII.NUL;
315 Actual_Len : Natural;
322 pragma Warnings (Off, Status);
323 -- For the call to Close where status is ignored
326 File_FD := Open_Read (Buffer'Address, Binary);
328 -- If we cannot open the file, we ignore it, we don't fail
330 if File_FD = Invalid_FD then
334 Len := Integer (File_Length (File_FD));
336 S := new String (1 .. Len);
338 -- Read the file. Note that the loop is not necessary since the
339 -- whole file is read at once except on VMS.
343 while Curr <= Len and then Actual_Len /= 0 loop
344 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
345 Curr := Curr + Actual_Len;
348 -- We are done with the file, so we close it (ignore any error on
349 -- the close, since we have successfully read the file).
351 Close (File_FD, Status);
353 -- Now, we read line by line
357 while Curr < Len loop
360 if Ch = ASCII.CR or else Ch = ASCII.LF
361 or else Ch = ASCII.FF or else Ch = ASCII.VT
363 if First <= Curr then
364 Add_Search_Dir (S (First .. Curr), Additional_Source_Dir);
373 -- Last line is a special case, if the file does not end with
374 -- an end of line mark.
376 if First <= S'Last then
377 Add_Search_Dir (S (First .. S'Last), Additional_Source_Dir);
379 end Get_Dirs_From_File;
381 ---------------------------------
382 -- Get_Libraries_From_Registry --
383 ---------------------------------
385 function Get_Libraries_From_Registry return String_Ptr is
386 function C_Get_Libraries_From_Registry return Address;
387 pragma Import (C, C_Get_Libraries_From_Registry,
388 "__gnat_get_libraries_from_registry");
390 function Strlen (Str : Address) return Integer;
391 pragma Import (C, Strlen, "strlen");
393 procedure Strncpy (X : Address; Y : Address; Length : Integer);
394 pragma Import (C, Strncpy, "strncpy");
396 procedure C_Free (Str : Address);
397 pragma Import (C, C_Free, "free");
399 Result_Ptr : Address;
400 Result_Length : Integer;
401 Out_String : String_Ptr;
404 Result_Ptr := C_Get_Libraries_From_Registry;
405 Result_Length := Strlen (Result_Ptr);
407 Out_String := new String (1 .. Result_Length);
408 Strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
413 end Get_Libraries_From_Registry;
415 -- Start of processing for Add_Default_Search_Dirs
418 -- After the locations specified on the command line, the next places
419 -- to look for files are the directories specified by the appropriate
420 -- environment variable. Get this value, extract the directory names
421 -- and store in the tables.
423 -- Check for eventual project path file env vars
425 Path_File_Name := Getenv (Project_Include_Path_File);
427 if Path_File_Name'Length > 0 then
428 Get_Dirs_From_File (Additional_Source_Dir => True);
431 Path_File_Name := Getenv (Project_Objects_Path_File);
433 if Path_File_Name'Length > 0 then
434 Get_Dirs_From_File (Additional_Source_Dir => False);
437 -- On VMS, don't expand the logical name (e.g. environment variable),
438 -- just put it into Unix (e.g. canonical) format. System services
439 -- will handle the expansion as part of the file processing.
441 for Additional_Source_Dir in False .. True loop
442 if Additional_Source_Dir then
443 Search_Path := Getenv (Ada_Include_Path);
445 if Search_Path'Length > 0 then
446 if Hostparm.OpenVMS then
447 Search_Path := To_Canonical_Path_Spec ("ADA_INCLUDE_PATH:");
449 Search_Path := To_Canonical_Path_Spec (Search_Path.all);
454 Search_Path := Getenv (Ada_Objects_Path);
456 if Search_Path'Length > 0 then
457 if Hostparm.OpenVMS then
458 Search_Path := To_Canonical_Path_Spec ("ADA_OBJECTS_PATH:");
460 Search_Path := To_Canonical_Path_Spec (Search_Path.all);
465 Get_Next_Dir_In_Path_Init (Search_Path);
467 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
468 exit when Search_Dir = null;
469 Add_Search_Dir (Search_Dir, Additional_Source_Dir);
473 -- For the compiler, if --RTS= was specified, add the runtime
476 if RTS_Src_Path_Name /= null
477 and then RTS_Lib_Path_Name /= null
479 Add_Search_Dirs (RTS_Src_Path_Name, Include);
480 Add_Search_Dirs (RTS_Lib_Path_Name, Objects);
483 if not Opt.No_Stdinc then
485 -- For WIN32 systems, look for any system libraries defined in
486 -- the registry. These are added to both source and object
489 Search_Path := String_Access (Get_Libraries_From_Registry);
491 Get_Next_Dir_In_Path_Init (Search_Path);
493 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
494 exit when Search_Dir = null;
495 Add_Search_Dir (Search_Dir, False);
496 Add_Search_Dir (Search_Dir, True);
499 -- The last place to look are the defaults
502 Read_Default_Search_Dirs
503 (String_Access (Update_Path (Search_Dir_Prefix)),
505 String_Access (Update_Path (Include_Dir_Default_Name)));
507 Get_Next_Dir_In_Path_Init (Search_Path);
509 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
510 exit when Search_Dir = null;
511 Add_Search_Dir (Search_Dir, True);
515 if not Opt.No_Stdlib and not Opt.RTS_Switch then
517 Read_Default_Search_Dirs
518 (String_Access (Update_Path (Search_Dir_Prefix)),
520 String_Access (Update_Path (Object_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, False);
530 end Add_Default_Search_Dirs;
536 procedure Add_File (File_Name : String; Index : Int := No_Index) is
538 Number_File_Names := Number_File_Names + 1;
540 -- As Add_File may be called for mains specified inside a project file,
541 -- File_Names may be too short and needs to be extended.
543 if Number_File_Names > File_Names'Last then
544 File_Names := new File_Name_Array'(File_Names.all & File_Names.all);
546 new File_Index_Array'(File_Indexes.all & File_Indexes.all);
549 File_Names (Number_File_Names) := new String'(File_Name);
550 File_Indexes (Number_File_Names) := Index;
553 ------------------------
554 -- Add_Lib_Search_Dir --
555 ------------------------
557 procedure Add_Lib_Search_Dir (Dir : String) is
559 if Dir'Length = 0 then
560 Fail ("missing library directory name");
563 Lib_Search_Directories.Increment_Last;
564 Lib_Search_Directories.Table (Lib_Search_Directories.Last) :=
565 Normalize_Directory_Name (Dir);
566 end Add_Lib_Search_Dir;
568 ---------------------
569 -- Add_Search_Dirs --
570 ---------------------
572 procedure Add_Search_Dirs
573 (Search_Path : String_Ptr;
574 Path_Type : Search_File_Type)
576 Current_Search_Path : String_Access;
579 Get_Next_Dir_In_Path_Init (String_Access (Search_Path));
581 Current_Search_Path :=
582 Get_Next_Dir_In_Path (String_Access (Search_Path));
583 exit when Current_Search_Path = null;
585 if Path_Type = Include then
586 Add_Src_Search_Dir (Current_Search_Path.all);
588 Add_Lib_Search_Dir (Current_Search_Path.all);
593 ------------------------
594 -- Add_Src_Search_Dir --
595 ------------------------
597 procedure Add_Src_Search_Dir (Dir : String) is
599 if Dir'Length = 0 then
600 Fail ("missing source directory name");
603 Src_Search_Directories.Increment_Last;
604 Src_Search_Directories.Table (Src_Search_Directories.Last) :=
605 Normalize_Directory_Name (Dir);
606 end Add_Src_Search_Dir;
608 --------------------------------
609 -- Append_Suffix_To_File_Name --
610 --------------------------------
612 function Append_Suffix_To_File_Name
613 (Name : File_Name_Type;
614 Suffix : String) return File_Name_Type
617 Get_Name_String (Name);
618 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
619 Name_Len := Name_Len + Suffix'Length;
621 end Append_Suffix_To_File_Name;
623 ---------------------
624 -- C_String_Length --
625 ---------------------
627 function C_String_Length (S : Address) return Integer is
628 function Strlen (S : Address) return Integer;
629 pragma Import (C, Strlen, "strlen");
631 if S = Null_Address then
638 ------------------------------
639 -- Canonical_Case_File_Name --
640 ------------------------------
642 -- For now, we only deal with the case of a-z. Eventually we should
643 -- worry about other Latin-1 letters on systems that support this ???
645 procedure Canonical_Case_File_Name (S : in out String) is
647 if not File_Names_Case_Sensitive then
648 for J in S'Range loop
649 if S (J) in 'A' .. 'Z' then
650 S (J) := Character'Val (
651 Character'Pos (S (J)) +
652 Character'Pos ('a') -
653 Character'Pos ('A'));
657 end Canonical_Case_File_Name;
659 ---------------------------
660 -- Create_File_And_Check --
661 ---------------------------
663 procedure Create_File_And_Check
664 (Fdesc : out File_Descriptor;
668 Output_File_Name := Name_Enter;
669 Fdesc := Create_File (Name_Buffer'Address, Fmode);
671 if Fdesc = Invalid_FD then
672 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
674 end Create_File_And_Check;
676 ------------------------
677 -- Current_File_Index --
678 ------------------------
680 function Current_File_Index return Int is
682 return File_Indexes (Current_File_Name_Index);
683 end Current_File_Index;
685 --------------------------------
686 -- Current_Library_File_Stamp --
687 --------------------------------
689 function Current_Library_File_Stamp return Time_Stamp_Type is
691 return Current_Full_Lib_Stamp;
692 end Current_Library_File_Stamp;
694 -------------------------------
695 -- Current_Object_File_Stamp --
696 -------------------------------
698 function Current_Object_File_Stamp return Time_Stamp_Type is
700 return Current_Full_Obj_Stamp;
701 end Current_Object_File_Stamp;
703 -------------------------------
704 -- Current_Source_File_Stamp --
705 -------------------------------
707 function Current_Source_File_Stamp return Time_Stamp_Type is
709 return Current_Full_Source_Stamp;
710 end Current_Source_File_Stamp;
712 ----------------------------
713 -- Dir_In_Obj_Search_Path --
714 ----------------------------
716 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr is
718 if Opt.Look_In_Primary_Dir then
720 Lib_Search_Directories.Table (Primary_Directory + Position - 1);
722 return Lib_Search_Directories.Table (Primary_Directory + Position);
724 end Dir_In_Obj_Search_Path;
726 ----------------------------
727 -- Dir_In_Src_Search_Path --
728 ----------------------------
730 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr is
732 if Opt.Look_In_Primary_Dir then
734 Src_Search_Directories.Table (Primary_Directory + Position - 1);
736 return Src_Search_Directories.Table (Primary_Directory + Position);
738 end Dir_In_Src_Search_Path;
740 ----------------------------
741 -- Dump_Source_File_Names --
742 ----------------------------
744 procedure Dump_Source_File_Names is
745 subtype Rng is Int range File_Name_Chars.First .. File_Name_Chars.Last;
747 Write_Str (String (File_Name_Chars.Table (Rng)));
748 end Dump_Source_File_Names;
750 ---------------------
751 -- Executable_Name --
752 ---------------------
754 function Executable_Name (Name : File_Name_Type) return File_Name_Type is
755 Exec_Suffix : String_Access;
758 if Name = No_File then
762 if Executable_Extension_On_Target = No_Name then
763 Exec_Suffix := Get_Target_Executable_Suffix;
765 Get_Name_String (Executable_Extension_On_Target);
766 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
769 Get_Name_String (Name);
771 if Exec_Suffix'Length /= 0 then
773 Buffer : String := Name_Buffer (1 .. Name_Len);
776 -- Get the file name in canonical case to accept as is names
777 -- ending with ".EXE" on VMS and Windows.
779 Canonical_Case_File_Name (Buffer);
781 -- If Executable does not end with the executable suffix, add it
783 if Buffer'Length <= Exec_Suffix'Length
785 Buffer (Buffer'Last - Exec_Suffix'Length + 1 .. Buffer'Last)
788 Name_Buffer (Name_Len + 1 .. Name_Len + Exec_Suffix'Length) :=
790 Name_Len := Name_Len + Exec_Suffix'Length;
801 function Executable_Name (Name : String) return String is
802 Exec_Suffix : String_Access;
803 Canonical_Name : String := Name;
806 if Executable_Extension_On_Target = No_Name then
807 Exec_Suffix := Get_Target_Executable_Suffix;
809 Get_Name_String (Executable_Extension_On_Target);
810 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
814 Suffix : constant String := Exec_Suffix.all;
818 Canonical_Case_File_Name (Canonical_Name);
820 if Suffix'Length /= 0
822 (Canonical_Name'Length <= Suffix'Length
823 or else Canonical_Name (Canonical_Name'Last - Suffix'Length + 1
824 .. Canonical_Name'Last) /= Suffix)
827 Result : String (1 .. Name'Length + Suffix'Length);
829 Result (1 .. Name'Length) := Name;
830 Result (Name'Length + 1 .. Result'Last) := Suffix;
839 -----------------------
840 -- Executable_Prefix --
841 -----------------------
843 function Executable_Prefix return String_Ptr is
845 function Get_Install_Dir (Exec : String) return String_Ptr;
846 -- S is the executable name preceded by the absolute or relative
847 -- path, e.g. "c:\usr\bin\gcc.exe" or "..\bin\gcc".
849 ---------------------
850 -- Get_Install_Dir --
851 ---------------------
853 function Get_Install_Dir (Exec : String) return String_Ptr is
854 Full_Path : constant String := Normalize_Pathname (Exec);
855 -- Use the full path, so that we find "lib" or "bin", even when
856 -- the tool has been invoked with a relative path, as in
857 -- "./gnatls -v" invoked in the GNAT bin directory.
860 for J in reverse Full_Path'Range loop
861 if Is_Directory_Separator (Full_Path (J)) then
862 if J < Full_Path'Last - 5 then
863 if (To_Lower (Full_Path (J + 1)) = 'l'
864 and then To_Lower (Full_Path (J + 2)) = 'i'
865 and then To_Lower (Full_Path (J + 3)) = 'b')
867 (To_Lower (Full_Path (J + 1)) = 'b'
868 and then To_Lower (Full_Path (J + 2)) = 'i'
869 and then To_Lower (Full_Path (J + 3)) = 'n')
871 return new String'(Full_Path (Full_Path'First .. J));
877 return new String'("");
880 -- Start of processing for Executable_Prefix
883 if Exec_Name = null then
884 Exec_Name := new String (1 .. Len_Arg (0));
885 Osint.Fill_Arg (Exec_Name (1)'Address, 0);
888 -- First determine if a path prefix was placed in front of the
891 for J in reverse Exec_Name'Range loop
892 if Is_Directory_Separator (Exec_Name (J)) then
893 return Get_Install_Dir (Exec_Name.all);
897 -- If we come here, the user has typed the executable name with no
900 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name.all).all);
901 end Executable_Prefix;
907 procedure Exit_Program (Exit_Code : Exit_Code_Type) is
909 -- The program will exit with the following status:
911 -- 0 if the object file has been generated (with or without warnings)
912 -- 1 if recompilation was not needed (smart recompilation)
913 -- 2 if gnat1 has been killed by a signal (detected by GCC)
914 -- 4 for a fatal error
915 -- 5 if there were errors
916 -- 6 if no code has been generated (spec)
918 -- Note that exit code 3 is not used and must not be used as this is
919 -- the code returned by a program aborted via C abort() routine on
920 -- Windows. GCC checks for that case and thinks that the child process
921 -- has been aborted. This code (exit code 3) used to be the code used
922 -- for E_No_Code, but E_No_Code was changed to 6 for this reason.
925 when E_Success => OS_Exit (0);
926 when E_Warnings => OS_Exit (0);
927 when E_No_Compile => OS_Exit (1);
928 when E_Fatal => OS_Exit (4);
929 when E_Errors => OS_Exit (5);
930 when E_No_Code => OS_Exit (6);
931 when E_Abort => OS_Abort;
939 procedure Fail (S : String) is
941 -- We use Output in case there is a special output set up.
942 -- In this case Set_Standard_Error will have no immediate effect.
945 Osint.Write_Program_Name;
950 Exit_Program (E_Fatal);
957 function File_Hash (F : File_Name_Type) return File_Hash_Num is
959 return File_Hash_Num (Int (F) rem File_Hash_Num'Range_Length);
966 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type is
968 if Name = No_File then
969 return Empty_Time_Stamp;
972 Get_Name_String (Name);
974 -- File_Time_Stamp will always return Invalid_Time if the file does not
975 -- exist, and that OS_Time_To_GNAT_Time will convert that to
976 -- Empty_Time_Stamp. Therefore we do not need to first test whether the
977 -- file actually exists, which saves a system call
979 return OS_Time_To_GNAT_Time
980 (File_Time_Stamp (Name_Buffer (1 .. Name_Len)));
983 function File_Stamp (Name : Path_Name_Type) return Time_Stamp_Type is
985 return File_Stamp (File_Name_Type (Name));
994 T : File_Type) return File_Name_Type
1000 File_Name : String renames Name_Buffer (1 .. Name_Len);
1001 File : File_Name_Type := No_File;
1005 -- If we are looking for a config file, look only in the current
1006 -- directory, i.e. return input argument unchanged. Also look
1007 -- only in the current directory if we are looking for a .dg
1008 -- file (happens in -gnatD mode).
1011 or else (Debug_Generated_Code
1012 and then Name_Len > 3
1014 (Name_Buffer (Name_Len - 2 .. Name_Len) = ".dg"
1016 (Hostparm.OpenVMS and then
1017 Name_Buffer (Name_Len - 2 .. Name_Len) = "_dg")))
1021 -- If we are trying to find the current main file just look in the
1022 -- directory where the user said it was.
1024 elsif Look_In_Primary_Directory_For_Current_Main
1025 and then Current_Main = N
1027 return Locate_File (N, T, Primary_Directory, File_Name);
1029 -- Otherwise do standard search for source file
1032 -- Check the mapping of this file name
1034 File := Mapped_Path_Name (N);
1036 -- If the file name is mapped to a path name, return the
1037 -- corresponding path name
1039 if File /= No_File then
1041 -- For locally removed file, Error_Name is returned; then
1042 -- return No_File, indicating the file is not a source.
1044 if File = Error_File_Name then
1052 -- First place to look is in the primary directory (i.e. the same
1053 -- directory as the source) unless this has been disabled with -I-
1055 if Opt.Look_In_Primary_Dir then
1056 File := Locate_File (N, T, Primary_Directory, File_Name);
1058 if File /= No_File then
1063 -- Finally look in directories specified with switches -I/-aI/-aO
1066 Last_Dir := Lib_Search_Directories.Last;
1068 Last_Dir := Src_Search_Directories.Last;
1071 for D in Primary_Directory + 1 .. Last_Dir loop
1072 File := Locate_File (N, T, D, File_Name);
1074 if File /= No_File then
1084 -----------------------
1085 -- Find_Program_Name --
1086 -----------------------
1088 procedure Find_Program_Name is
1089 Command_Name : String (1 .. Len_Arg (0));
1090 Cindex1 : Integer := Command_Name'First;
1091 Cindex2 : Integer := Command_Name'Last;
1094 Fill_Arg (Command_Name'Address, 0);
1096 if Command_Name = "" then
1101 -- The program name might be specified by a full path name. However,
1102 -- we don't want to print that all out in an error message, so the
1103 -- path might need to be stripped away.
1105 for J in reverse Cindex1 .. Cindex2 loop
1106 if Is_Directory_Separator (Command_Name (J)) then
1112 -- Command_Name(Cindex1 .. Cindex2) is now the equivalent of the
1113 -- POSIX command "basename argv[0]"
1115 -- Strip off any versioning information such as found on VMS.
1116 -- This would take the form of TOOL.exe followed by a ";" or "."
1117 -- and a sequence of one or more numbers.
1119 if Command_Name (Cindex2) in '0' .. '9' then
1120 for J in reverse Cindex1 .. Cindex2 loop
1121 if Command_Name (J) = '.' or else Command_Name (J) = ';' then
1126 exit when Command_Name (J) not in '0' .. '9';
1130 -- Strip off any executable extension (usually nothing or .exe)
1131 -- but formally reported by autoconf in the variable EXEEXT
1133 if Cindex2 - Cindex1 >= 4 then
1134 if To_Lower (Command_Name (Cindex2 - 3)) = '.'
1135 and then To_Lower (Command_Name (Cindex2 - 2)) = 'e'
1136 and then To_Lower (Command_Name (Cindex2 - 1)) = 'x'
1137 and then To_Lower (Command_Name (Cindex2)) = 'e'
1139 Cindex2 := Cindex2 - 4;
1143 Name_Len := Cindex2 - Cindex1 + 1;
1144 Name_Buffer (1 .. Name_Len) := Command_Name (Cindex1 .. Cindex2);
1145 end Find_Program_Name;
1147 ------------------------
1148 -- Full_Lib_File_Name --
1149 ------------------------
1151 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type is
1153 return Find_File (N, Library);
1154 end Full_Lib_File_Name;
1156 ----------------------------
1157 -- Full_Library_Info_Name --
1158 ----------------------------
1160 function Full_Library_Info_Name return File_Name_Type is
1162 return Current_Full_Lib_Name;
1163 end Full_Library_Info_Name;
1165 ---------------------------
1166 -- Full_Object_File_Name --
1167 ---------------------------
1169 function Full_Object_File_Name return File_Name_Type is
1171 return Current_Full_Obj_Name;
1172 end Full_Object_File_Name;
1174 ----------------------
1175 -- Full_Source_Name --
1176 ----------------------
1178 function Full_Source_Name return File_Name_Type is
1180 return Current_Full_Source_Name;
1181 end Full_Source_Name;
1183 ----------------------
1184 -- Full_Source_Name --
1185 ----------------------
1187 function Full_Source_Name (N : File_Name_Type) return File_Name_Type is
1189 return Smart_Find_File (N, Source);
1190 end Full_Source_Name;
1196 function Get_Directory (Name : File_Name_Type) return File_Name_Type is
1198 Get_Name_String (Name);
1200 for J in reverse 1 .. Name_Len loop
1201 if Is_Directory_Separator (Name_Buffer (J)) then
1207 Name_Len := Hostparm.Normalized_CWD'Length;
1208 Name_Buffer (1 .. Name_Len) := Hostparm.Normalized_CWD;
1212 --------------------------
1213 -- Get_Next_Dir_In_Path --
1214 --------------------------
1216 Search_Path_Pos : Integer;
1217 -- Keeps track of current position in search path. Initialized by the
1218 -- call to Get_Next_Dir_In_Path_Init, updated by Get_Next_Dir_In_Path.
1220 function Get_Next_Dir_In_Path
1221 (Search_Path : String_Access) return String_Access
1223 Lower_Bound : Positive := Search_Path_Pos;
1224 Upper_Bound : Positive;
1228 while Lower_Bound <= Search_Path'Last
1229 and then Search_Path.all (Lower_Bound) = Path_Separator
1231 Lower_Bound := Lower_Bound + 1;
1234 exit when Lower_Bound > Search_Path'Last;
1236 Upper_Bound := Lower_Bound;
1237 while Upper_Bound <= Search_Path'Last
1238 and then Search_Path.all (Upper_Bound) /= Path_Separator
1240 Upper_Bound := Upper_Bound + 1;
1243 Search_Path_Pos := Upper_Bound;
1244 return new String'(Search_Path.all (Lower_Bound .. Upper_Bound - 1));
1248 end Get_Next_Dir_In_Path;
1250 -------------------------------
1251 -- Get_Next_Dir_In_Path_Init --
1252 -------------------------------
1254 procedure Get_Next_Dir_In_Path_Init (Search_Path : String_Access) is
1256 Search_Path_Pos := Search_Path'First;
1257 end Get_Next_Dir_In_Path_Init;
1259 --------------------------------------
1260 -- Get_Primary_Src_Search_Directory --
1261 --------------------------------------
1263 function Get_Primary_Src_Search_Directory return String_Ptr is
1265 return Src_Search_Directories.Table (Primary_Directory);
1266 end Get_Primary_Src_Search_Directory;
1268 ------------------------
1269 -- Get_RTS_Search_Dir --
1270 ------------------------
1272 function Get_RTS_Search_Dir
1273 (Search_Dir : String;
1274 File_Type : Search_File_Type) return String_Ptr
1276 procedure Get_Current_Dir
1277 (Dir : System.Address;
1278 Length : System.Address);
1279 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1282 pragma Import (C, Max_Path, "__gnat_max_path_len");
1283 -- Maximum length of a path name
1285 Current_Dir : String_Ptr;
1286 Default_Search_Dir : String_Access;
1287 Default_Suffix_Dir : String_Access;
1288 Local_Search_Dir : String_Access;
1289 Norm_Search_Dir : String_Access;
1290 Result_Search_Dir : String_Access;
1291 Search_File : String_Access;
1292 Temp_String : String_Ptr;
1295 -- Add a directory separator at the end of the directory if necessary
1296 -- so that we can directly append a file to the directory
1298 if Search_Dir (Search_Dir'Last) /= Directory_Separator then
1300 new String'(Search_Dir & String'(1 => Directory_Separator));
1302 Local_Search_Dir := new String'(Search_Dir);
1305 if File_Type = Include then
1306 Search_File := Include_Search_File;
1307 Default_Suffix_Dir := new String'("adainclude");
1309 Search_File := Objects_Search_File;
1310 Default_Suffix_Dir := new String'("adalib");
1313 Norm_Search_Dir := To_Canonical_Path_Spec (Local_Search_Dir.all);
1315 if Is_Absolute_Path (Norm_Search_Dir.all) then
1317 -- We first verify if there is a directory Include_Search_Dir
1318 -- containing default search directories
1320 Result_Search_Dir :=
1321 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1322 Default_Search_Dir :=
1323 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1324 Free (Norm_Search_Dir);
1326 if Result_Search_Dir /= null then
1327 return String_Ptr (Result_Search_Dir);
1328 elsif Is_Directory (Default_Search_Dir.all) then
1329 return String_Ptr (Default_Search_Dir);
1334 -- Search in the current directory
1337 -- Get the current directory
1340 Buffer : String (1 .. Max_Path + 2);
1341 Path_Len : Natural := Max_Path;
1344 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1346 if Buffer (Path_Len) /= Directory_Separator then
1347 Path_Len := Path_Len + 1;
1348 Buffer (Path_Len) := Directory_Separator;
1351 Current_Dir := new String'(Buffer (1 .. Path_Len));
1355 new String'(Current_Dir.all & Local_Search_Dir.all);
1357 Result_Search_Dir :=
1358 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1360 Default_Search_Dir :=
1361 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1363 Free (Norm_Search_Dir);
1365 if Result_Search_Dir /= null then
1366 return String_Ptr (Result_Search_Dir);
1368 elsif Is_Directory (Default_Search_Dir.all) then
1369 return String_Ptr (Default_Search_Dir);
1372 -- Search in Search_Dir_Prefix/Search_Dir
1376 (Update_Path (Search_Dir_Prefix).all & Local_Search_Dir.all);
1378 Result_Search_Dir :=
1379 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1381 Default_Search_Dir :=
1382 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1384 Free (Norm_Search_Dir);
1386 if Result_Search_Dir /= null then
1387 return String_Ptr (Result_Search_Dir);
1389 elsif Is_Directory (Default_Search_Dir.all) then
1390 return String_Ptr (Default_Search_Dir);
1393 -- We finally search in Search_Dir_Prefix/rts-Search_Dir
1396 new String'(Update_Path (Search_Dir_Prefix).all & "rts-");
1399 new String'(Temp_String.all & Local_Search_Dir.all);
1401 Result_Search_Dir :=
1402 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1404 Default_Search_Dir :=
1405 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1406 Free (Norm_Search_Dir);
1408 if Result_Search_Dir /= null then
1409 return String_Ptr (Result_Search_Dir);
1411 elsif Is_Directory (Default_Search_Dir.all) then
1412 return String_Ptr (Default_Search_Dir);
1420 end Get_RTS_Search_Dir;
1422 --------------------------------
1423 -- Include_Dir_Default_Prefix --
1424 --------------------------------
1426 function Include_Dir_Default_Prefix return String_Access is
1428 if The_Include_Dir_Default_Prefix = null then
1429 The_Include_Dir_Default_Prefix :=
1430 String_Access (Update_Path (Include_Dir_Default_Name));
1433 return The_Include_Dir_Default_Prefix;
1434 end Include_Dir_Default_Prefix;
1436 function Include_Dir_Default_Prefix return String is
1438 return Include_Dir_Default_Prefix.all;
1439 end Include_Dir_Default_Prefix;
1445 procedure Initialize is
1447 Number_File_Names := 0;
1448 Current_File_Name_Index := 0;
1450 Src_Search_Directories.Init;
1451 Lib_Search_Directories.Init;
1453 -- Start off by setting all suppress options to False, these will
1454 -- be reset later (turning some on if -gnato is not specified, and
1455 -- turning all of them on if -gnatp is specified).
1457 Suppress_Options := (others => False);
1459 -- Reserve the first slot in the search paths table. This is the
1460 -- directory of the main source file or main library file and is filled
1461 -- in by each call to Next_Main_Source/Next_Main_Lib_File with the
1462 -- directory specified for this main source or library file. This is the
1463 -- directory which is searched first by default. This default search is
1464 -- inhibited by the option -I- for both source and library files.
1466 Src_Search_Directories.Set_Last (Primary_Directory);
1467 Src_Search_Directories.Table (Primary_Directory) := new String'("");
1469 Lib_Search_Directories.Set_Last (Primary_Directory);
1470 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
1473 ----------------------------
1474 -- Is_Directory_Separator --
1475 ----------------------------
1477 function Is_Directory_Separator (C : Character) return Boolean is
1479 -- In addition to the default directory_separator allow the '/' to
1480 -- act as separator since this is allowed in MS-DOS, Windows 95/NT,
1481 -- and OS2 ports. On VMS, the situation is more complicated because
1482 -- there are two characters to check for.
1485 C = Directory_Separator
1487 or else (Hostparm.OpenVMS
1488 and then (C = ']' or else C = ':'));
1489 end Is_Directory_Separator;
1491 -------------------------
1492 -- Is_Readonly_Library --
1493 -------------------------
1495 function Is_Readonly_Library (File : File_Name_Type) return Boolean is
1497 Get_Name_String (File);
1499 pragma Assert (Name_Buffer (Name_Len - 3 .. Name_Len) = ".ali");
1501 return not Is_Writable_File (Name_Buffer (1 .. Name_Len));
1502 end Is_Readonly_Library;
1508 function Lib_File_Name
1509 (Source_File : File_Name_Type;
1510 Munit_Index : Nat := 0) return File_Name_Type
1513 Get_Name_String (Source_File);
1515 for J in reverse 2 .. Name_Len loop
1516 if Name_Buffer (J) = '.' then
1522 if Munit_Index /= 0 then
1523 Add_Char_To_Name_Buffer (Multi_Unit_Index_Character);
1524 Add_Nat_To_Name_Buffer (Munit_Index);
1527 Add_Char_To_Name_Buffer ('.');
1528 Add_Str_To_Name_Buffer (ALI_Suffix.all);
1536 function Locate_File
1537 (N : File_Name_Type;
1540 Name : String) return File_Name_Type
1542 Dir_Name : String_Ptr;
1545 -- If Name is already an absolute path, do not look for a directory
1547 if Is_Absolute_Path (Name) then
1550 elsif T = Library then
1551 Dir_Name := Lib_Search_Directories.Table (Dir);
1553 else pragma Assert (T /= Config);
1554 Dir_Name := Src_Search_Directories.Table (Dir);
1558 Full_Name : String (1 .. Dir_Name'Length + Name'Length);
1561 Full_Name (1 .. Dir_Name'Length) := Dir_Name.all;
1562 Full_Name (Dir_Name'Length + 1 .. Full_Name'Length) := Name;
1564 if not Is_Regular_File (Full_Name) then
1568 -- If the file is in the current directory then return N itself
1570 if Dir_Name'Length = 0 then
1573 Name_Len := Full_Name'Length;
1574 Name_Buffer (1 .. Name_Len) := Full_Name;
1581 -------------------------------
1582 -- Matching_Full_Source_Name --
1583 -------------------------------
1585 function Matching_Full_Source_Name
1586 (N : File_Name_Type;
1587 T : Time_Stamp_Type) return File_Name_Type
1590 Get_Name_String (N);
1593 File_Name : constant String := Name_Buffer (1 .. Name_Len);
1594 File : File_Name_Type := No_File;
1598 if Opt.Look_In_Primary_Dir then
1599 File := Locate_File (N, Source, Primary_Directory, File_Name);
1601 if File /= No_File and then T = File_Stamp (N) then
1606 Last_Dir := Src_Search_Directories.Last;
1608 for D in Primary_Directory + 1 .. Last_Dir loop
1609 File := Locate_File (N, Source, D, File_Name);
1611 if File /= No_File and then T = File_Stamp (File) then
1618 end Matching_Full_Source_Name;
1624 function More_Files return Boolean is
1626 return (Current_File_Name_Index < Number_File_Names);
1629 -------------------------------
1630 -- Nb_Dir_In_Obj_Search_Path --
1631 -------------------------------
1633 function Nb_Dir_In_Obj_Search_Path return Natural is
1635 if Opt.Look_In_Primary_Dir then
1636 return Lib_Search_Directories.Last - Primary_Directory + 1;
1638 return Lib_Search_Directories.Last - Primary_Directory;
1640 end Nb_Dir_In_Obj_Search_Path;
1642 -------------------------------
1643 -- Nb_Dir_In_Src_Search_Path --
1644 -------------------------------
1646 function Nb_Dir_In_Src_Search_Path return Natural is
1648 if Opt.Look_In_Primary_Dir then
1649 return Src_Search_Directories.Last - Primary_Directory + 1;
1651 return Src_Search_Directories.Last - Primary_Directory;
1653 end Nb_Dir_In_Src_Search_Path;
1655 --------------------
1656 -- Next_Main_File --
1657 --------------------
1659 function Next_Main_File return File_Name_Type is
1660 File_Name : String_Ptr;
1661 Dir_Name : String_Ptr;
1665 pragma Assert (More_Files);
1667 Current_File_Name_Index := Current_File_Name_Index + 1;
1669 -- Get the file and directory name
1671 File_Name := File_Names (Current_File_Name_Index);
1672 Fptr := File_Name'First;
1674 for J in reverse File_Name'Range loop
1675 if File_Name (J) = Directory_Separator
1676 or else File_Name (J) = '/'
1678 if J = File_Name'Last then
1679 Fail ("File name missing");
1687 -- Save name of directory in which main unit resides for use in
1688 -- locating other units
1690 Dir_Name := new String'(File_Name (File_Name'First .. Fptr - 1));
1692 case Running_Program is
1695 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1696 Look_In_Primary_Directory_For_Current_Main := True;
1699 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1701 if Fptr > File_Name'First then
1702 Look_In_Primary_Directory_For_Current_Main := True;
1705 when Binder | Gnatls =>
1706 Dir_Name := Normalize_Directory_Name (Dir_Name.all);
1707 Lib_Search_Directories.Table (Primary_Directory) := Dir_Name;
1713 Name_Len := File_Name'Last - Fptr + 1;
1714 Name_Buffer (1 .. Name_Len) := File_Name (Fptr .. File_Name'Last);
1715 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1716 Current_Main := Name_Find;
1718 -- In the gnatmake case, the main file may have not have the
1719 -- extension. Try ".adb" first then ".ads"
1721 if Running_Program = Make then
1723 Orig_Main : constant File_Name_Type := Current_Main;
1726 if Strip_Suffix (Orig_Main) = Orig_Main then
1728 Append_Suffix_To_File_Name (Orig_Main, ".adb");
1730 if Full_Source_Name (Current_Main) = No_File then
1732 Append_Suffix_To_File_Name (Orig_Main, ".ads");
1734 if Full_Source_Name (Current_Main) = No_File then
1735 Current_Main := Orig_Main;
1742 return Current_Main;
1745 ------------------------------
1746 -- Normalize_Directory_Name --
1747 ------------------------------
1749 function Normalize_Directory_Name (Directory : String) return String_Ptr is
1751 function Is_Quoted (Path : String) return Boolean;
1752 pragma Inline (Is_Quoted);
1753 -- Returns true if Path is quoted (either double or single quotes)
1759 function Is_Quoted (Path : String) return Boolean is
1760 First : constant Character := Path (Path'First);
1761 Last : constant Character := Path (Path'Last);
1764 if (First = ''' and then Last = ''')
1766 (First = '"' and then Last = '"')
1774 Result : String_Ptr;
1776 -- Start of processing for Normalize_Directory_Name
1779 if Directory'Length = 0 then
1780 Result := new String'(Hostparm.Normalized_CWD);
1782 elsif Is_Directory_Separator (Directory (Directory'Last)) then
1783 Result := new String'(Directory);
1785 elsif Is_Quoted (Directory) then
1787 -- This is a quoted string, it certainly means that the directory
1788 -- contains some spaces for example. We can safely remove the quotes
1789 -- here as the OS_Lib.Normalize_Arguments will be called before any
1790 -- spawn routines. This ensure that quotes will be added when needed.
1792 Result := new String (1 .. Directory'Length - 1);
1793 Result (1 .. Directory'Length - 2) :=
1794 Directory (Directory'First + 1 .. Directory'Last - 1);
1795 Result (Result'Last) := Directory_Separator;
1798 Result := new String (1 .. Directory'Length + 1);
1799 Result (1 .. Directory'Length) := Directory;
1800 Result (Directory'Length + 1) := Directory_Separator;
1804 end Normalize_Directory_Name;
1806 ---------------------
1807 -- Number_Of_Files --
1808 ---------------------
1810 function Number_Of_Files return Int is
1812 return Number_File_Names;
1813 end Number_Of_Files;
1815 -------------------------------
1816 -- Object_Dir_Default_Prefix --
1817 -------------------------------
1819 function Object_Dir_Default_Prefix return String is
1820 Object_Dir : String_Access :=
1821 String_Access (Update_Path (Object_Dir_Default_Name));
1824 if Object_Dir = null then
1829 Result : constant String := Object_Dir.all;
1835 end Object_Dir_Default_Prefix;
1837 ----------------------
1838 -- Object_File_Name --
1839 ----------------------
1841 function Object_File_Name (N : File_Name_Type) return File_Name_Type is
1847 Get_Name_String (N);
1848 Name_Len := Name_Len - ALI_Suffix'Length - 1;
1850 for J in Target_Object_Suffix'Range loop
1851 Name_Len := Name_Len + 1;
1852 Name_Buffer (Name_Len) := Target_Object_Suffix (J);
1856 end Object_File_Name;
1858 -------------------------------
1859 -- OS_Exit_Through_Exception --
1860 -------------------------------
1862 procedure OS_Exit_Through_Exception (Status : Integer) is
1864 Current_Exit_Status := Status;
1865 raise Types.Terminate_Program;
1866 end OS_Exit_Through_Exception;
1868 --------------------------
1869 -- OS_Time_To_GNAT_Time --
1870 --------------------------
1872 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type is
1873 GNAT_Time : Time_Stamp_Type;
1883 if T = Invalid_Time then
1884 return Empty_Time_Stamp;
1887 GM_Split (T, Y, Mo, D, H, Mn, S);
1893 Minutes => Nat (Mn),
1898 end OS_Time_To_GNAT_Time;
1904 function Program_Name (Nam : String; Prog : String) return String_Access is
1905 End_Of_Prefix : Natural := 0;
1906 Start_Of_Prefix : Positive := 1;
1907 Start_Of_Suffix : Positive;
1910 -- GNAAMP tool names require special treatment
1912 if AAMP_On_Target then
1914 -- The name "gcc" is mapped to "gnaamp" (the compiler driver)
1917 return new String'("gnaamp");
1919 -- Tool names starting with "gnat" are mapped by substituting the
1920 -- string "gnaamp" for "gnat" (for example, "gnatpp" => "gnaamppp").
1922 elsif Nam'Length >= 4
1923 and then Nam (Nam'First .. Nam'First + 3) = "gnat"
1925 return new String'("gnaamp" & Nam (Nam'First + 4 .. Nam'Last));
1927 -- No other mapping rules, so we continue and handle any other forms
1928 -- of tool names the same as on other targets.
1935 -- Get the name of the current program being executed
1939 Start_Of_Suffix := Name_Len + 1;
1941 -- Find the target prefix if any, for the cross compilation case.
1942 -- For instance in "powerpc-elf-gcc" the target prefix is
1944 -- Ditto for suffix, e.g. in "gcc-4.1", the suffix is "-4.1"
1946 for J in reverse 1 .. Name_Len loop
1947 if Name_Buffer (J) = '/'
1948 or else Name_Buffer (J) = Directory_Separator
1949 or else Name_Buffer (J) = ':'
1951 Start_Of_Prefix := J + 1;
1956 -- Find End_Of_Prefix
1958 for J in Start_Of_Prefix .. Name_Len - Prog'Length + 1 loop
1959 if Name_Buffer (J .. J + Prog'Length - 1) = Prog then
1960 End_Of_Prefix := J - 1;
1965 if End_Of_Prefix > 1 then
1966 Start_Of_Suffix := End_Of_Prefix + Prog'Length + 1;
1969 -- Create the new program name
1972 (Name_Buffer (Start_Of_Prefix .. End_Of_Prefix)
1974 & Name_Buffer (Start_Of_Suffix .. Name_Len));
1977 ------------------------------
1978 -- Read_Default_Search_Dirs --
1979 ------------------------------
1981 function Read_Default_Search_Dirs
1982 (Search_Dir_Prefix : String_Access;
1983 Search_File : String_Access;
1984 Search_Dir_Default_Name : String_Access) return String_Access
1986 Prefix_Len : constant Integer := Search_Dir_Prefix.all'Length;
1987 Buffer : String (1 .. Prefix_Len + Search_File.all'Length + 1);
1988 File_FD : File_Descriptor;
1989 S, S1 : String_Access;
1992 Actual_Len : Integer;
1995 Prev_Was_Separator : Boolean;
1996 Nb_Relative_Dir : Integer;
1998 function Is_Relative (S : String; K : Positive) return Boolean;
1999 pragma Inline (Is_Relative);
2000 -- Returns True if a relative directory specification is found
2001 -- in S at position K, False otherwise.
2007 function Is_Relative (S : String; K : Positive) return Boolean is
2009 return not Is_Absolute_Path (S (K .. S'Last));
2012 -- Start of processing for Read_Default_Search_Dirs
2015 -- Construct a C compatible character string buffer
2017 Buffer (1 .. Search_Dir_Prefix.all'Length)
2018 := Search_Dir_Prefix.all;
2019 Buffer (Search_Dir_Prefix.all'Length + 1 .. Buffer'Last - 1)
2021 Buffer (Buffer'Last) := ASCII.NUL;
2023 File_FD := Open_Read (Buffer'Address, Binary);
2024 if File_FD = Invalid_FD then
2025 return Search_Dir_Default_Name;
2028 Len := Integer (File_Length (File_FD));
2030 -- An extra character for a trailing Path_Separator is allocated
2032 S := new String (1 .. Len + 1);
2033 S (Len + 1) := Path_Separator;
2035 -- Read the file. Note that the loop is not necessary since the
2036 -- whole file is read at once except on VMS.
2040 while Actual_Len /= 0 loop
2041 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
2042 Curr := Curr + Actual_Len;
2045 -- Process the file, dealing with path separators
2047 Prev_Was_Separator := True;
2048 Nb_Relative_Dir := 0;
2049 for J in 1 .. Len loop
2051 -- Treat any control character as a path separator. Note that we do
2052 -- not treat space as a path separator (we used to treat space as a
2053 -- path separator in an earlier version). That way space can appear
2054 -- as a legitimate character in a path name.
2056 -- Why do we treat all control characters as path separators???
2058 if S (J) in ASCII.NUL .. ASCII.US then
2059 S (J) := Path_Separator;
2062 -- Test for explicit path separator (or control char as above)
2064 if S (J) = Path_Separator then
2065 Prev_Was_Separator := True;
2067 -- If not path separator, register use of relative directory
2070 if Prev_Was_Separator and then Is_Relative (S.all, J) then
2071 Nb_Relative_Dir := Nb_Relative_Dir + 1;
2074 Prev_Was_Separator := False;
2078 if Nb_Relative_Dir = 0 then
2082 -- Add the Search_Dir_Prefix to all relative paths
2084 S1 := new String (1 .. S'Length + Nb_Relative_Dir * Prefix_Len);
2086 Prev_Was_Separator := True;
2087 for J in 1 .. Len + 1 loop
2088 if S (J) = Path_Separator then
2089 Prev_Was_Separator := True;
2092 if Prev_Was_Separator and then Is_Relative (S.all, J) then
2093 S1 (J1 .. J1 + Prefix_Len - 1) := Search_Dir_Prefix.all;
2094 J1 := J1 + Prefix_Len;
2097 Prev_Was_Separator := False;
2105 end Read_Default_Search_Dirs;
2107 -----------------------
2108 -- Read_Library_Info --
2109 -----------------------
2111 function Read_Library_Info
2112 (Lib_File : File_Name_Type;
2113 Fatal_Err : Boolean := False) return Text_Buffer_Ptr is
2115 return Read_Library_Info_From_Full
2116 (Full_Lib_File => Find_File (Lib_File, Library),
2117 Fatal_Err => Fatal_Err);
2118 end Read_Library_Info;
2120 ---------------------------------
2121 -- Read_Library_Info_From_Full --
2122 ---------------------------------
2124 function Read_Library_Info_From_Full
2125 (Full_Lib_File : File_Name_Type;
2126 Fatal_Err : Boolean := False) return Text_Buffer_Ptr
2128 Lib_FD : File_Descriptor;
2129 -- The file descriptor for the current library file. A negative value
2130 -- indicates failure to open the specified source file.
2132 Text : Text_Buffer_Ptr;
2133 -- Allocated text buffer
2136 pragma Warnings (Off, Status);
2137 -- For the calls to Close
2140 Current_Full_Lib_Name := Full_Lib_File;
2141 Current_Full_Obj_Name := Object_File_Name (Current_Full_Lib_Name);
2143 if Current_Full_Lib_Name = No_File then
2145 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2147 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2152 Get_Name_String (Current_Full_Lib_Name);
2153 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2155 -- Open the library FD, note that we open in binary mode, because as
2156 -- documented in the spec, the caller is expected to handle either
2157 -- DOS or Unix mode files, and there is no point in wasting time on
2158 -- text translation when it is not required.
2160 Lib_FD := Open_Read (Name_Buffer'Address, Binary);
2162 if Lib_FD = Invalid_FD then
2164 Fail ("Cannot open: " & Name_Buffer (1 .. Name_Len));
2166 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2171 -- Check for object file consistency if requested
2173 if Opt.Check_Object_Consistency then
2174 Current_Full_Lib_Stamp := File_Stamp (Current_Full_Lib_Name);
2175 Current_Full_Obj_Stamp := File_Stamp (Current_Full_Obj_Name);
2177 if Current_Full_Obj_Stamp (1) = ' ' then
2179 -- When the library is readonly always assume object is consistent
2181 if Is_Readonly_Library (Current_Full_Lib_Name) then
2182 Current_Full_Obj_Stamp := Current_Full_Lib_Stamp;
2184 elsif Fatal_Err then
2185 Get_Name_String (Current_Full_Obj_Name);
2186 Close (Lib_FD, Status);
2188 -- No need to check the status, we fail anyway
2190 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2193 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2194 Close (Lib_FD, Status);
2196 -- No need to check the status, we return null anyway
2203 -- Read data from the file
2206 Len : constant Integer := Integer (File_Length (Lib_FD));
2207 -- Length of source file text. If it doesn't fit in an integer
2208 -- we're probably stuck anyway (>2 gigs of source seems a lot!)
2210 Actual_Len : Integer := 0;
2212 Lo : constant Text_Ptr := 0;
2213 -- Low bound for allocated text buffer
2215 Hi : Text_Ptr := Text_Ptr (Len);
2216 -- High bound for allocated text buffer. Note length is Len + 1
2217 -- which allows for extra EOF character at the end of the buffer.
2220 -- Allocate text buffer. Note extra character at end for EOF
2222 Text := new Text_Buffer (Lo .. Hi);
2224 -- Some systems (e.g. VMS) have file types that require one
2225 -- read per line, so read until we get the Len bytes or until
2226 -- there are no more characters.
2230 Actual_Len := Read (Lib_FD, Text (Hi)'Address, Len);
2231 Hi := Hi + Text_Ptr (Actual_Len);
2232 exit when Actual_Len = Len or else Actual_Len <= 0;
2238 -- Read is complete, close file and we are done
2240 Close (Lib_FD, Status);
2241 -- The status should never be False. But, if it is, what can we do?
2242 -- So, we don't test it.
2246 end Read_Library_Info_From_Full;
2248 ----------------------
2249 -- Read_Source_File --
2250 ----------------------
2252 procedure Read_Source_File
2253 (N : File_Name_Type;
2255 Hi : out Source_Ptr;
2256 Src : out Source_Buffer_Ptr;
2257 T : File_Type := Source)
2259 Source_File_FD : File_Descriptor;
2260 -- The file descriptor for the current source file. A negative value
2261 -- indicates failure to open the specified source file.
2264 -- Length of file. Assume no more than 2 gigabytes of source!
2266 Actual_Len : Integer;
2269 pragma Warnings (Off, Status);
2270 -- For the call to Close
2273 Current_Full_Source_Name := Find_File (N, T);
2274 Current_Full_Source_Stamp := File_Stamp (Current_Full_Source_Name);
2276 if Current_Full_Source_Name = No_File then
2278 -- If we were trying to access the main file and we could not find
2279 -- it, we have an error.
2281 if N = Current_Main then
2282 Get_Name_String (N);
2283 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2291 Get_Name_String (Current_Full_Source_Name);
2292 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2294 -- Open the source FD, note that we open in binary mode, because as
2295 -- documented in the spec, the caller is expected to handle either
2296 -- DOS or Unix mode files, and there is no point in wasting time on
2297 -- text translation when it is not required.
2299 Source_File_FD := Open_Read (Name_Buffer'Address, Binary);
2301 if Source_File_FD = Invalid_FD then
2307 -- Print out the file name, if requested, and if it's not part of the
2308 -- runtimes, store it in File_Name_Chars.
2311 Name : String renames Name_Buffer (1 .. Name_Len);
2312 Inc : String renames Include_Dir_Default_Prefix.all;
2315 if Debug.Debug_Flag_Dot_N then
2320 and then Inc'Length < Name_Len
2321 and then Name_Buffer (1 .. Inc'Length) = Inc
2323 -- Part of runtimes, so ignore it
2328 File_Name_Chars.Append_All (File_Name_Chars.Table_Type (Name));
2329 File_Name_Chars.Append (ASCII.LF);
2333 -- Prepare to read data from the file
2335 Len := Integer (File_Length (Source_File_FD));
2337 -- Set Hi so that length is one more than the physical length,
2338 -- allowing for the extra EOF character at the end of the buffer
2340 Hi := Lo + Source_Ptr (Len);
2342 -- Do the actual read operation
2345 subtype Actual_Source_Buffer is Source_Buffer (Lo .. Hi);
2346 -- Physical buffer allocated
2348 type Actual_Source_Ptr is access Actual_Source_Buffer;
2349 -- This is the pointer type for the physical buffer allocated
2351 Actual_Ptr : constant Actual_Source_Ptr := new Actual_Source_Buffer;
2352 -- And this is the actual physical buffer
2355 -- Allocate source buffer, allowing extra character at end for EOF
2357 -- Some systems (e.g. VMS) have file types that require one read per
2358 -- line, so read until we get the Len bytes or until there are no
2363 Actual_Len := Read (Source_File_FD, Actual_Ptr (Hi)'Address, Len);
2364 Hi := Hi + Source_Ptr (Actual_Len);
2365 exit when Actual_Len = Len or else Actual_Len <= 0;
2368 Actual_Ptr (Hi) := EOF;
2370 -- Now we need to work out the proper virtual origin pointer to
2371 -- return. This is exactly Actual_Ptr (0)'Address, but we have to
2372 -- be careful to suppress checks to compute this address.
2375 pragma Suppress (All_Checks);
2377 pragma Warnings (Off);
2378 -- This use of unchecked conversion is aliasing safe
2380 function To_Source_Buffer_Ptr is new
2381 Unchecked_Conversion (Address, Source_Buffer_Ptr);
2383 pragma Warnings (On);
2386 Src := To_Source_Buffer_Ptr (Actual_Ptr (0)'Address);
2390 -- Read is complete, get time stamp and close file and we are done
2392 Close (Source_File_FD, Status);
2394 -- The status should never be False. But, if it is, what can we do?
2395 -- So, we don't test it.
2397 end Read_Source_File;
2403 function Relocate_Path
2405 Path : String) return String_Ptr
2409 procedure set_std_prefix (S : String; Len : Integer);
2410 pragma Import (C, set_std_prefix);
2413 if Std_Prefix = null then
2414 Std_Prefix := Executable_Prefix;
2416 if Std_Prefix.all /= "" then
2418 -- Remove trailing directory separator when calling set_std_prefix
2420 set_std_prefix (Std_Prefix.all, Std_Prefix'Length - 1);
2424 if Path (Prefix'Range) = Prefix then
2425 if Std_Prefix.all /= "" then
2427 (1 .. Std_Prefix'Length + Path'Last - Prefix'Last);
2428 S (1 .. Std_Prefix'Length) := Std_Prefix.all;
2429 S (Std_Prefix'Length + 1 .. S'Last) :=
2430 Path (Prefix'Last + 1 .. Path'Last);
2435 return new String'(Path);
2442 procedure Set_Program (P : Program_Type) is
2445 Fail ("Set_Program called twice");
2448 Program_Set := True;
2449 Running_Program := P;
2456 function Shared_Lib (Name : String) return String is
2457 Library : String (1 .. Name'Length + Library_Version'Length + 3);
2458 -- 3 = 2 for "-l" + 1 for "-" before lib version
2461 Library (1 .. 2) := "-l";
2462 Library (3 .. 2 + Name'Length) := Name;
2463 Library (3 + Name'Length) := '-';
2464 Library (4 + Name'Length .. Library'Last) := Library_Version;
2466 if OpenVMS_On_Target then
2467 for K in Library'First + 2 .. Library'Last loop
2468 if Library (K) = '.' or else Library (K) = '-' then
2477 ----------------------
2478 -- Smart_File_Stamp --
2479 ----------------------
2481 function Smart_File_Stamp
2482 (N : File_Name_Type;
2483 T : File_Type) return Time_Stamp_Type
2485 Time_Stamp : Time_Stamp_Type;
2488 if not File_Cache_Enabled then
2489 return File_Stamp (Find_File (N, T));
2492 Time_Stamp := File_Stamp_Hash_Table.Get (N);
2494 if Time_Stamp (1) = ' ' then
2495 Time_Stamp := File_Stamp (Smart_Find_File (N, T));
2496 File_Stamp_Hash_Table.Set (N, Time_Stamp);
2500 end Smart_File_Stamp;
2502 ---------------------
2503 -- Smart_Find_File --
2504 ---------------------
2506 function Smart_Find_File
2507 (N : File_Name_Type;
2508 T : File_Type) return File_Name_Type
2510 Full_File_Name : File_Name_Type;
2513 if not File_Cache_Enabled then
2514 return Find_File (N, T);
2517 Full_File_Name := File_Name_Hash_Table.Get (N);
2519 if Full_File_Name = No_File then
2520 Full_File_Name := Find_File (N, T);
2521 File_Name_Hash_Table.Set (N, Full_File_Name);
2524 return Full_File_Name;
2525 end Smart_Find_File;
2527 ----------------------
2528 -- Source_File_Data --
2529 ----------------------
2531 procedure Source_File_Data (Cache : Boolean) is
2533 File_Cache_Enabled := Cache;
2534 end Source_File_Data;
2536 -----------------------
2537 -- Source_File_Stamp --
2538 -----------------------
2540 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
2542 return Smart_File_Stamp (N, Source);
2543 end Source_File_Stamp;
2545 ---------------------
2546 -- Strip_Directory --
2547 ---------------------
2549 function Strip_Directory (Name : File_Name_Type) return File_Name_Type is
2551 Get_Name_String (Name);
2553 for J in reverse 1 .. Name_Len - 1 loop
2555 -- If we find the last directory separator
2557 if Is_Directory_Separator (Name_Buffer (J)) then
2559 -- Return the part of Name that follows this last directory
2562 Name_Buffer (1 .. Name_Len - J) := Name_Buffer (J + 1 .. Name_Len);
2563 Name_Len := Name_Len - J;
2568 -- There were no directory separator, just return Name
2571 end Strip_Directory;
2577 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type is
2579 Get_Name_String (Name);
2581 for J in reverse 2 .. Name_Len loop
2583 -- If we found the last '.', return part of Name that precedes it
2585 if Name_Buffer (J) = '.' then
2594 ---------------------------
2595 -- To_Canonical_Dir_Spec --
2596 ---------------------------
2598 function To_Canonical_Dir_Spec
2600 Prefix_Style : Boolean) return String_Access
2602 function To_Canonical_Dir_Spec
2603 (Host_Dir : Address;
2604 Prefix_Flag : Integer) return Address;
2605 pragma Import (C, To_Canonical_Dir_Spec, "__gnat_to_canonical_dir_spec");
2607 C_Host_Dir : String (1 .. Host_Dir'Length + 1);
2608 Canonical_Dir_Addr : Address;
2609 Canonical_Dir_Len : Integer;
2612 C_Host_Dir (1 .. Host_Dir'Length) := Host_Dir;
2613 C_Host_Dir (C_Host_Dir'Last) := ASCII.NUL;
2615 if Prefix_Style then
2616 Canonical_Dir_Addr := To_Canonical_Dir_Spec (C_Host_Dir'Address, 1);
2618 Canonical_Dir_Addr := To_Canonical_Dir_Spec (C_Host_Dir'Address, 0);
2620 Canonical_Dir_Len := C_String_Length (Canonical_Dir_Addr);
2622 if Canonical_Dir_Len = 0 then
2625 return To_Path_String_Access (Canonical_Dir_Addr, Canonical_Dir_Len);
2630 Fail ("erroneous directory spec: " & Host_Dir);
2632 end To_Canonical_Dir_Spec;
2634 ---------------------------
2635 -- To_Canonical_File_List --
2636 ---------------------------
2638 function To_Canonical_File_List
2639 (Wildcard_Host_File : String;
2640 Only_Dirs : Boolean) return String_Access_List_Access
2642 function To_Canonical_File_List_Init
2643 (Host_File : Address;
2644 Only_Dirs : Integer) return Integer;
2645 pragma Import (C, To_Canonical_File_List_Init,
2646 "__gnat_to_canonical_file_list_init");
2648 function To_Canonical_File_List_Next return Address;
2649 pragma Import (C, To_Canonical_File_List_Next,
2650 "__gnat_to_canonical_file_list_next");
2652 procedure To_Canonical_File_List_Free;
2653 pragma Import (C, To_Canonical_File_List_Free,
2654 "__gnat_to_canonical_file_list_free");
2656 Num_Files : Integer;
2657 C_Wildcard_Host_File : String (1 .. Wildcard_Host_File'Length + 1);
2660 C_Wildcard_Host_File (1 .. Wildcard_Host_File'Length) :=
2662 C_Wildcard_Host_File (C_Wildcard_Host_File'Last) := ASCII.NUL;
2664 -- Do the expansion and say how many there are
2666 Num_Files := To_Canonical_File_List_Init
2667 (C_Wildcard_Host_File'Address, Boolean'Pos (Only_Dirs));
2670 Canonical_File_List : String_Access_List (1 .. Num_Files);
2671 Canonical_File_Addr : Address;
2672 Canonical_File_Len : Integer;
2675 -- Retrieve the expanded directory names and build the list
2677 for J in 1 .. Num_Files loop
2678 Canonical_File_Addr := To_Canonical_File_List_Next;
2679 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
2680 Canonical_File_List (J) := To_Path_String_Access
2681 (Canonical_File_Addr, Canonical_File_Len);
2684 -- Free up the storage
2686 To_Canonical_File_List_Free;
2688 return new String_Access_List'(Canonical_File_List);
2690 end To_Canonical_File_List;
2692 ----------------------------
2693 -- To_Canonical_File_Spec --
2694 ----------------------------
2696 function To_Canonical_File_Spec
2697 (Host_File : String) return String_Access
2699 function To_Canonical_File_Spec (Host_File : Address) return Address;
2701 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
2703 C_Host_File : String (1 .. Host_File'Length + 1);
2704 Canonical_File_Addr : Address;
2705 Canonical_File_Len : Integer;
2708 C_Host_File (1 .. Host_File'Length) := Host_File;
2709 C_Host_File (C_Host_File'Last) := ASCII.NUL;
2711 Canonical_File_Addr := To_Canonical_File_Spec (C_Host_File'Address);
2712 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
2714 if Canonical_File_Len = 0 then
2717 return To_Path_String_Access
2718 (Canonical_File_Addr, Canonical_File_Len);
2723 Fail ("erroneous file spec: " & Host_File);
2725 end To_Canonical_File_Spec;
2727 ----------------------------
2728 -- To_Canonical_Path_Spec --
2729 ----------------------------
2731 function To_Canonical_Path_Spec
2732 (Host_Path : String) return String_Access
2734 function To_Canonical_Path_Spec (Host_Path : Address) return Address;
2736 (C, To_Canonical_Path_Spec, "__gnat_to_canonical_path_spec");
2738 C_Host_Path : String (1 .. Host_Path'Length + 1);
2739 Canonical_Path_Addr : Address;
2740 Canonical_Path_Len : Integer;
2743 C_Host_Path (1 .. Host_Path'Length) := Host_Path;
2744 C_Host_Path (C_Host_Path'Last) := ASCII.NUL;
2746 Canonical_Path_Addr := To_Canonical_Path_Spec (C_Host_Path'Address);
2747 Canonical_Path_Len := C_String_Length (Canonical_Path_Addr);
2749 -- Return a null string (vice a null) for zero length paths, for
2750 -- compatibility with getenv().
2752 return To_Path_String_Access (Canonical_Path_Addr, Canonical_Path_Len);
2756 Fail ("erroneous path spec: " & Host_Path);
2758 end To_Canonical_Path_Spec;
2760 ---------------------------
2761 -- To_Host_Dir_Spec --
2762 ---------------------------
2764 function To_Host_Dir_Spec
2765 (Canonical_Dir : String;
2766 Prefix_Style : Boolean) return String_Access
2768 function To_Host_Dir_Spec
2769 (Canonical_Dir : Address;
2770 Prefix_Flag : Integer) return Address;
2771 pragma Import (C, To_Host_Dir_Spec, "__gnat_to_host_dir_spec");
2773 C_Canonical_Dir : String (1 .. Canonical_Dir'Length + 1);
2774 Host_Dir_Addr : Address;
2775 Host_Dir_Len : Integer;
2778 C_Canonical_Dir (1 .. Canonical_Dir'Length) := Canonical_Dir;
2779 C_Canonical_Dir (C_Canonical_Dir'Last) := ASCII.NUL;
2781 if Prefix_Style then
2782 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 1);
2784 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 0);
2786 Host_Dir_Len := C_String_Length (Host_Dir_Addr);
2788 if Host_Dir_Len = 0 then
2791 return To_Path_String_Access (Host_Dir_Addr, Host_Dir_Len);
2793 end To_Host_Dir_Spec;
2795 ----------------------------
2796 -- To_Host_File_Spec --
2797 ----------------------------
2799 function To_Host_File_Spec
2800 (Canonical_File : String) return String_Access
2802 function To_Host_File_Spec (Canonical_File : Address) return Address;
2803 pragma Import (C, To_Host_File_Spec, "__gnat_to_host_file_spec");
2805 C_Canonical_File : String (1 .. Canonical_File'Length + 1);
2806 Host_File_Addr : Address;
2807 Host_File_Len : Integer;
2810 C_Canonical_File (1 .. Canonical_File'Length) := Canonical_File;
2811 C_Canonical_File (C_Canonical_File'Last) := ASCII.NUL;
2813 Host_File_Addr := To_Host_File_Spec (C_Canonical_File'Address);
2814 Host_File_Len := C_String_Length (Host_File_Addr);
2816 if Host_File_Len = 0 then
2819 return To_Path_String_Access
2820 (Host_File_Addr, Host_File_Len);
2822 end To_Host_File_Spec;
2824 ---------------------------
2825 -- To_Path_String_Access --
2826 ---------------------------
2828 function To_Path_String_Access
2829 (Path_Addr : Address;
2830 Path_Len : Integer) return String_Access
2832 subtype Path_String is String (1 .. Path_Len);
2833 type Path_String_Access is access Path_String;
2835 function Address_To_Access is new
2836 Unchecked_Conversion (Source => Address,
2837 Target => Path_String_Access);
2839 Path_Access : constant Path_String_Access :=
2840 Address_To_Access (Path_Addr);
2842 Return_Val : String_Access;
2845 Return_Val := new String (1 .. Path_Len);
2847 for J in 1 .. Path_Len loop
2848 Return_Val (J) := Path_Access (J);
2852 end To_Path_String_Access;
2858 function Update_Path (Path : String_Ptr) return String_Ptr is
2860 function C_Update_Path (Path, Component : Address) return Address;
2861 pragma Import (C, C_Update_Path, "update_path");
2863 function Strlen (Str : Address) return Integer;
2864 pragma Import (C, Strlen, "strlen");
2866 procedure Strncpy (X : Address; Y : Address; Length : Integer);
2867 pragma Import (C, Strncpy, "strncpy");
2869 In_Length : constant Integer := Path'Length;
2870 In_String : String (1 .. In_Length + 1);
2871 Component_Name : aliased String := "GCC" & ASCII.NUL;
2872 Result_Ptr : Address;
2873 Result_Length : Integer;
2874 Out_String : String_Ptr;
2877 In_String (1 .. In_Length) := Path.all;
2878 In_String (In_Length + 1) := ASCII.NUL;
2879 Result_Ptr := C_Update_Path (In_String'Address, Component_Name'Address);
2880 Result_Length := Strlen (Result_Ptr);
2882 Out_String := new String (1 .. Result_Length);
2883 Strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
2891 procedure Write_Info (Info : String) is
2893 Write_With_Check (Info'Address, Info'Length);
2894 Write_With_Check (EOL'Address, 1);
2897 ------------------------
2898 -- Write_Program_Name --
2899 ------------------------
2901 procedure Write_Program_Name is
2902 Save_Buffer : constant String (1 .. Name_Len) :=
2903 Name_Buffer (1 .. Name_Len);
2908 -- Convert the name to lower case so error messages are the same on
2911 for J in 1 .. Name_Len loop
2912 if Name_Buffer (J) in 'A' .. 'Z' then
2914 Character'Val (Character'Pos (Name_Buffer (J)) + 32);
2918 Write_Str (Name_Buffer (1 .. Name_Len));
2920 -- Restore Name_Buffer which was clobbered by the call to
2921 -- Find_Program_Name
2923 Name_Len := Save_Buffer'Last;
2924 Name_Buffer (1 .. Name_Len) := Save_Buffer;
2925 end Write_Program_Name;
2927 ----------------------
2928 -- Write_With_Check --
2929 ----------------------
2931 procedure Write_With_Check (A : Address; N : Integer) is
2933 pragma Warnings (Off, Ignore);
2936 if N = Write (Output_FD, A, N) then
2940 Write_Str ("error: disk full writing ");
2941 Write_Name_Decoded (Output_File_Name);
2943 Name_Len := Name_Len + 1;
2944 Name_Buffer (Name_Len) := ASCII.NUL;
2945 Delete_File (Name_Buffer'Address, Ignore);
2946 Exit_Program (E_Fatal);
2948 end Write_With_Check;
2950 ----------------------------
2951 -- Package Initialization --
2952 ----------------------------
2955 Initialization : declare
2957 function Get_Default_Identifier_Character_Set return Character;
2958 pragma Import (C, Get_Default_Identifier_Character_Set,
2959 "__gnat_get_default_identifier_character_set");
2960 -- Function to determine the default identifier character set,
2961 -- which is system dependent. See Opt package spec for a list of
2962 -- the possible character codes and their interpretations.
2964 function Get_Maximum_File_Name_Length return Int;
2965 pragma Import (C, Get_Maximum_File_Name_Length,
2966 "__gnat_get_maximum_file_name_length");
2967 -- Function to get maximum file name length for system
2970 Identifier_Character_Set := Get_Default_Identifier_Character_Set;
2971 Maximum_File_Name_Length := Get_Maximum_File_Name_Length;
2973 -- Following should be removed by having above function return
2974 -- Integer'Last as indication of no maximum instead of -1 ???
2976 if Maximum_File_Name_Length = -1 then
2977 Maximum_File_Name_Length := Int'Last;
2980 Src_Search_Directories.Set_Last (Primary_Directory);
2981 Src_Search_Directories.Table (Primary_Directory) := new String'("");
2983 Lib_Search_Directories.Set_Last (Primary_Directory);
2984 Lib_Search_Directories.Table (Primary_Directory) := new String'("");