1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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;
33 with Gnatvsn; use Gnatvsn;
36 with Output; use Output;
37 with Sdefault; use Sdefault;
39 with Targparm; use Targparm;
43 Running_Program : Program_Type := Unspecified;
44 -- comment required here ???
46 Program_Set : Boolean := False;
47 -- comment required here ???
49 Std_Prefix : String_Ptr;
50 -- Standard prefix, computed dynamically the first time Relocate_Path
51 -- is called, and cached for subsequent calls.
53 Empty : aliased String := "";
54 No_Dir : constant String_Ptr := Empty'Access;
55 -- Used in Locate_File as a fake directory when Name is already an
58 -------------------------------------
59 -- Use of Name_Find and Name_Enter --
60 -------------------------------------
62 -- This package creates a number of source, ALI and object file names
63 -- that are used to locate the actual file and for the purpose of message
64 -- construction. These names need not be accessible by Name_Find, and can
65 -- be therefore created by using routine Name_Enter. The files in question
66 -- are file names with a prefix directory (ie the files not in the current
67 -- directory). File names without a prefix directory are entered with
68 -- Name_Find because special values might be attached to the various Info
69 -- fields of the corresponding name table entry.
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 function Append_Suffix_To_File_Name
76 (Name : File_Name_Type;
77 Suffix : String) return File_Name_Type;
78 -- Appends Suffix to Name and returns the new name
80 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type;
81 -- Convert OS format time to GNAT format time stamp
83 function Executable_Prefix return String_Ptr;
84 -- Returns the name of the root directory where the executable is stored.
85 -- The executable must be located in a directory called "bin", or under
86 -- root/lib/gcc-lib/..., or under root/libexec/gcc/... For example, if
87 -- executable is stored in directory "/foo/bar/bin", this routine returns
88 -- "/foo/bar/". Return "" if location is not recognized as described above.
90 function Update_Path (Path : String_Ptr) return String_Ptr;
91 -- Update the specified path to replace the prefix with the location
92 -- where GNAT is installed. See the file prefix.c in GCC for details.
98 Name : String) return File_Name_Type;
99 -- See if the file N whose name is Name exists in directory Dir. Dir is an
100 -- index into the Lib_Search_Directories table if T = Library. Otherwise
101 -- if T = Source, Dir is an index into the Src_Search_Directories table.
102 -- Returns the File_Name_Type of the full file name if file found, or
103 -- No_File if not found.
105 function C_String_Length (S : Address) return Integer;
106 -- Returns length of a C string (zero for a null address)
108 function To_Path_String_Access
109 (Path_Addr : Address;
110 Path_Len : Integer) return String_Access;
111 -- Converts a C String to an Ada String. Are we doing this to avoid withing
112 -- Interfaces.C.Strings ???
114 ------------------------------
115 -- Other Local Declarations --
116 ------------------------------
118 EOL : constant Character := ASCII.LF;
119 -- End of line character
121 Number_File_Names : Int := 0;
122 -- Number of file names founde on command line and placed in File_Names
124 Look_In_Primary_Directory_For_Current_Main : Boolean := False;
125 -- When this variable is True, Find_File only looks in Primary_Directory
126 -- for the Current_Main file. This variable is always set to True for the
127 -- compiler. It is also True for gnatmake, when the soucr name given on
128 -- the command line has directory information.
130 Current_Full_Source_Name : File_Name_Type := No_File;
131 Current_Full_Source_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
132 Current_Full_Lib_Name : File_Name_Type := No_File;
133 Current_Full_Lib_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
134 Current_Full_Obj_Name : File_Name_Type := No_File;
135 Current_Full_Obj_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
136 -- Respectively full name (with directory info) and time stamp of the
137 -- latest source, library and object files opened by Read_Source_File and
138 -- Read_Library_Info.
144 Primary_Directory : constant := 0;
145 -- This is index in the tables created below for the first directory to
146 -- search in for source or library information files. This is the directory
147 -- containing the latest main input file (a source file for the compiler or
148 -- a library file for the binder).
150 package Src_Search_Directories is new Table.Table (
151 Table_Component_Type => String_Ptr,
152 Table_Index_Type => Integer,
153 Table_Low_Bound => Primary_Directory,
155 Table_Increment => 100,
156 Table_Name => "Osint.Src_Search_Directories");
157 -- Table of names of directories in which to search for source (Compiler)
158 -- files. This table is filled in the order in which the directories are
159 -- to be searched, and then used in that order.
161 package Lib_Search_Directories is new Table.Table (
162 Table_Component_Type => String_Ptr,
163 Table_Index_Type => Integer,
164 Table_Low_Bound => Primary_Directory,
166 Table_Increment => 100,
167 Table_Name => "Osint.Lib_Search_Directories");
168 -- Table of names of directories in which to search for library (Binder)
169 -- files. This table is filled in the order in which the directories are
170 -- to be searched and then used in that order. The reason for having two
171 -- distinct tables is that we need them both in gnatmake.
173 ---------------------
174 -- File Hash Table --
175 ---------------------
177 -- The file hash table is provided to free the programmer from any
178 -- efficiency concern when retrieving full file names or time stamps of
179 -- source files. If the programmer calls Source_File_Data (Cache => True)
180 -- he is guaranteed that the price to retrieve the full name (ie with
181 -- directory info) or time stamp of the file will be payed only once, the
182 -- first time the full name is actually searched (or the first time the
183 -- time stamp is actually retrieved). This is achieved by employing a hash
184 -- table that stores as a key the File_Name_Type of the file and associates
185 -- to that File_Name_Type the full file name and time stamp of the file.
187 File_Cache_Enabled : Boolean := False;
188 -- Set to true if you want the enable the file data caching mechanism
190 type File_Hash_Num is range 0 .. 1020;
192 function File_Hash (F : File_Name_Type) return File_Hash_Num;
193 -- Compute hash index for use by Simple_HTable
195 package File_Name_Hash_Table is new GNAT.HTable.Simple_HTable (
196 Header_Num => File_Hash_Num,
197 Element => File_Name_Type,
198 No_Element => No_File,
199 Key => File_Name_Type,
203 package File_Stamp_Hash_Table is new GNAT.HTable.Simple_HTable (
204 Header_Num => File_Hash_Num,
205 Element => Time_Stamp_Type,
206 No_Element => Empty_Time_Stamp,
207 Key => File_Name_Type,
211 function Smart_Find_File
213 T : File_Type) return File_Name_Type;
214 -- Exactly like Find_File except that if File_Cache_Enabled is True this
215 -- routine looks first in the hash table to see if the full name of the
216 -- file is already available.
218 function Smart_File_Stamp
220 T : File_Type) return Time_Stamp_Type;
221 -- Takes the same parameter as the routine above (N is a file name without
222 -- any prefix directory information) and behaves like File_Stamp except
223 -- that if File_Cache_Enabled is True this routine looks first in the hash
224 -- table to see if the file stamp of the file is already available.
226 -----------------------------
227 -- Add_Default_Search_Dirs --
228 -----------------------------
230 procedure Add_Default_Search_Dirs is
231 Search_Dir : String_Access;
232 Search_Path : String_Access;
233 Path_File_Name : String_Access;
235 procedure Add_Search_Dir
236 (Search_Dir : String;
237 Additional_Source_Dir : Boolean);
238 procedure Add_Search_Dir
239 (Search_Dir : String_Access;
240 Additional_Source_Dir : Boolean);
241 -- Add a source search dir or a library search dir, depending on the
242 -- value of Additional_Source_Dir.
244 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean);
245 -- Open a path file and read the directory to search, one per line
247 function Get_Libraries_From_Registry return String_Ptr;
248 -- On Windows systems, get the list of installed standard libraries
249 -- from the registry key:
251 -- HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\
252 -- GNAT\Standard Libraries
253 -- Return an empty string on other systems
259 procedure Add_Search_Dir
260 (Search_Dir : String;
261 Additional_Source_Dir : Boolean)
264 if Additional_Source_Dir then
265 Add_Src_Search_Dir (Search_Dir);
267 Add_Lib_Search_Dir (Search_Dir);
271 procedure Add_Search_Dir
272 (Search_Dir : String_Access;
273 Additional_Source_Dir : Boolean)
276 if Additional_Source_Dir then
277 Add_Src_Search_Dir (Search_Dir.all);
279 Add_Lib_Search_Dir (Search_Dir.all);
283 ------------------------
284 -- Get_Dirs_From_File --
285 ------------------------
287 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean) is
288 File_FD : File_Descriptor;
289 Buffer : constant String := Path_File_Name.all & ASCII.NUL;
291 Actual_Len : Natural;
298 -- For the call to Close
301 File_FD := Open_Read (Buffer'Address, Binary);
303 -- If we cannot open the file, we ignore it, we don't fail
305 if File_FD = Invalid_FD then
309 Len := Integer (File_Length (File_FD));
311 S := new String (1 .. Len);
313 -- Read the file. Note that the loop is not necessary since the
314 -- whole file is read at once except on VMS.
318 while Curr <= Len and then Actual_Len /= 0 loop
319 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
320 Curr := Curr + Actual_Len;
323 -- We are done with the file, so we close it (ignore any error on
324 -- the close, since we have successfully read the file).
326 Close (File_FD, Status);
328 -- Now, we read line by line
332 while Curr < Len loop
335 if Ch = ASCII.CR or else Ch = ASCII.LF
336 or else Ch = ASCII.FF or else Ch = ASCII.VT
338 if First <= Curr then
339 Add_Search_Dir (S (First .. Curr), Additional_Source_Dir);
348 -- Last line is a special case, if the file does not end with
349 -- an end of line mark.
351 if First <= S'Last then
352 Add_Search_Dir (S (First .. S'Last), Additional_Source_Dir);
354 end Get_Dirs_From_File;
356 ---------------------------------
357 -- Get_Libraries_From_Registry --
358 ---------------------------------
360 function Get_Libraries_From_Registry return String_Ptr is
361 function C_Get_Libraries_From_Registry return Address;
362 pragma Import (C, C_Get_Libraries_From_Registry,
363 "__gnat_get_libraries_from_registry");
365 function Strlen (Str : Address) return Integer;
366 pragma Import (C, Strlen, "strlen");
368 procedure Strncpy (X : Address; Y : Address; Length : Integer);
369 pragma Import (C, Strncpy, "strncpy");
371 Result_Ptr : Address;
372 Result_Length : Integer;
373 Out_String : String_Ptr;
376 Result_Ptr := C_Get_Libraries_From_Registry;
377 Result_Length := Strlen (Result_Ptr);
379 Out_String := new String (1 .. Result_Length);
380 Strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
382 end Get_Libraries_From_Registry;
384 -- Start of processing for Add_Default_Search_Dirs
387 -- After the locations specified on the command line, the next places
388 -- to look for files are the directories specified by the appropriate
389 -- environment variable. Get this value, extract the directory names
390 -- and store in the tables.
392 -- Check for eventual project path file env vars
394 Path_File_Name := Getenv (Project_Include_Path_File);
396 if Path_File_Name'Length > 0 then
397 Get_Dirs_From_File (Additional_Source_Dir => True);
400 Path_File_Name := Getenv (Project_Objects_Path_File);
402 if Path_File_Name'Length > 0 then
403 Get_Dirs_From_File (Additional_Source_Dir => False);
406 -- On VMS, don't expand the logical name (e.g. environment variable),
407 -- just put it into Unix (e.g. canonical) format. System services
408 -- will handle the expansion as part of the file processing.
410 for Additional_Source_Dir in False .. True loop
411 if Additional_Source_Dir then
412 Search_Path := Getenv (Ada_Include_Path);
414 if Search_Path'Length > 0 then
415 if Hostparm.OpenVMS then
416 Search_Path := To_Canonical_Path_Spec ("ADA_INCLUDE_PATH:");
418 Search_Path := To_Canonical_Path_Spec (Search_Path.all);
423 Search_Path := Getenv (Ada_Objects_Path);
425 if Search_Path'Length > 0 then
426 if Hostparm.OpenVMS then
427 Search_Path := To_Canonical_Path_Spec ("ADA_OBJECTS_PATH:");
429 Search_Path := To_Canonical_Path_Spec (Search_Path.all);
434 Get_Next_Dir_In_Path_Init (Search_Path);
436 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
437 exit when Search_Dir = null;
438 Add_Search_Dir (Search_Dir, Additional_Source_Dir);
442 -- For the compiler, if --RTS= was specified, add the runtime
445 if RTS_Src_Path_Name /= null
446 and then RTS_Lib_Path_Name /= null
448 Add_Search_Dirs (RTS_Src_Path_Name, Include);
449 Add_Search_Dirs (RTS_Lib_Path_Name, Objects);
452 if not Opt.No_Stdinc then
454 -- For WIN32 systems, look for any system libraries defined in
455 -- the registry. These are added to both source and object
458 Search_Path := String_Access (Get_Libraries_From_Registry);
460 Get_Next_Dir_In_Path_Init (Search_Path);
462 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
463 exit when Search_Dir = null;
464 Add_Search_Dir (Search_Dir, False);
465 Add_Search_Dir (Search_Dir, True);
468 -- The last place to look are the defaults
471 Read_Default_Search_Dirs
472 (String_Access (Update_Path (Search_Dir_Prefix)),
474 String_Access (Update_Path (Include_Dir_Default_Name)));
476 Get_Next_Dir_In_Path_Init (Search_Path);
478 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
479 exit when Search_Dir = null;
480 Add_Search_Dir (Search_Dir, True);
484 if not Opt.No_Stdlib and not Opt.RTS_Switch then
486 Read_Default_Search_Dirs
487 (String_Access (Update_Path (Search_Dir_Prefix)),
489 String_Access (Update_Path (Object_Dir_Default_Name)));
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);
499 end Add_Default_Search_Dirs;
505 procedure Add_File (File_Name : String; Index : Int := No_Index) is
507 Number_File_Names := Number_File_Names + 1;
509 -- As Add_File may be called for mains specified inside a project file,
510 -- File_Names may be too short and needs to be extended.
512 if Number_File_Names > File_Names'Last then
513 File_Names := new File_Name_Array'(File_Names.all & File_Names.all);
515 new File_Index_Array'(File_Indexes.all & File_Indexes.all);
518 File_Names (Number_File_Names) := new String'(File_Name);
519 File_Indexes (Number_File_Names) := Index;
522 ------------------------
523 -- Add_Lib_Search_Dir --
524 ------------------------
526 procedure Add_Lib_Search_Dir (Dir : String) is
528 if Dir'Length = 0 then
529 Fail ("missing library directory name");
532 Lib_Search_Directories.Increment_Last;
533 Lib_Search_Directories.Table (Lib_Search_Directories.Last) :=
534 Normalize_Directory_Name (Dir);
535 end Add_Lib_Search_Dir;
537 ---------------------
538 -- Add_Search_Dirs --
539 ---------------------
541 procedure Add_Search_Dirs
542 (Search_Path : String_Ptr;
543 Path_Type : Search_File_Type)
545 Current_Search_Path : String_Access;
548 Get_Next_Dir_In_Path_Init (String_Access (Search_Path));
550 Current_Search_Path :=
551 Get_Next_Dir_In_Path (String_Access (Search_Path));
552 exit when Current_Search_Path = null;
554 if Path_Type = Include then
555 Add_Src_Search_Dir (Current_Search_Path.all);
557 Add_Lib_Search_Dir (Current_Search_Path.all);
562 ------------------------
563 -- Add_Src_Search_Dir --
564 ------------------------
566 procedure Add_Src_Search_Dir (Dir : String) is
568 if Dir'Length = 0 then
569 Fail ("missing source directory name");
572 Src_Search_Directories.Increment_Last;
573 Src_Search_Directories.Table (Src_Search_Directories.Last) :=
574 Normalize_Directory_Name (Dir);
575 end Add_Src_Search_Dir;
577 --------------------------------
578 -- Append_Suffix_To_File_Name --
579 --------------------------------
581 function Append_Suffix_To_File_Name
582 (Name : File_Name_Type;
583 Suffix : String) return File_Name_Type
586 Get_Name_String (Name);
587 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
588 Name_Len := Name_Len + Suffix'Length;
590 end Append_Suffix_To_File_Name;
592 ---------------------
593 -- C_String_Length --
594 ---------------------
596 function C_String_Length (S : Address) return Integer is
597 function Strlen (S : Address) return Integer;
598 pragma Import (C, Strlen, "strlen");
600 if S = Null_Address then
607 ------------------------------
608 -- Canonical_Case_File_Name --
609 ------------------------------
611 -- For now, we only deal with the case of a-z. Eventually we should
612 -- worry about other Latin-1 letters on systems that support this ???
614 procedure Canonical_Case_File_Name (S : in out String) is
616 if not File_Names_Case_Sensitive then
617 for J in S'Range loop
618 if S (J) in 'A' .. 'Z' then
619 S (J) := Character'Val (
620 Character'Pos (S (J)) +
621 Character'Pos ('a') -
622 Character'Pos ('A'));
626 end Canonical_Case_File_Name;
628 ---------------------------
629 -- Create_File_And_Check --
630 ---------------------------
632 procedure Create_File_And_Check
633 (Fdesc : out File_Descriptor;
637 Output_File_Name := Name_Enter;
638 Fdesc := Create_File (Name_Buffer'Address, Fmode);
640 if Fdesc = Invalid_FD then
641 Fail ("Cannot create: ", Name_Buffer (1 .. Name_Len));
643 end Create_File_And_Check;
645 ------------------------
646 -- Current_File_Index --
647 ------------------------
649 function Current_File_Index return Int is
651 return File_Indexes (Current_File_Name_Index);
652 end Current_File_Index;
654 --------------------------------
655 -- Current_Library_File_Stamp --
656 --------------------------------
658 function Current_Library_File_Stamp return Time_Stamp_Type is
660 return Current_Full_Lib_Stamp;
661 end Current_Library_File_Stamp;
663 -------------------------------
664 -- Current_Object_File_Stamp --
665 -------------------------------
667 function Current_Object_File_Stamp return Time_Stamp_Type is
669 return Current_Full_Obj_Stamp;
670 end Current_Object_File_Stamp;
672 -------------------------------
673 -- Current_Source_File_Stamp --
674 -------------------------------
676 function Current_Source_File_Stamp return Time_Stamp_Type is
678 return Current_Full_Source_Stamp;
679 end Current_Source_File_Stamp;
681 ----------------------------
682 -- Dir_In_Obj_Search_Path --
683 ----------------------------
685 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr is
687 if Opt.Look_In_Primary_Dir then
689 Lib_Search_Directories.Table (Primary_Directory + Position - 1);
691 return Lib_Search_Directories.Table (Primary_Directory + Position);
693 end Dir_In_Obj_Search_Path;
695 ----------------------------
696 -- Dir_In_Src_Search_Path --
697 ----------------------------
699 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr is
701 if Opt.Look_In_Primary_Dir then
703 Src_Search_Directories.Table (Primary_Directory + Position - 1);
705 return Src_Search_Directories.Table (Primary_Directory + Position);
707 end Dir_In_Src_Search_Path;
709 ---------------------
710 -- Executable_Name --
711 ---------------------
713 function Executable_Name (Name : File_Name_Type) return File_Name_Type is
714 Exec_Suffix : String_Access;
717 if Name = No_File then
721 if Executable_Extension_On_Target = No_Name then
722 Exec_Suffix := Get_Target_Executable_Suffix;
724 Get_Name_String (Executable_Extension_On_Target);
725 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
728 Get_Name_String (Name);
730 if Exec_Suffix'Length /= 0 then
732 Buffer : String := Name_Buffer (1 .. Name_Len);
735 -- Get the file name in canonical case to accept as is names
736 -- ending with ".EXE" on VMS and Windows.
738 Canonical_Case_File_Name (Buffer);
740 -- If Executable does not end with the executable suffix, add it
742 if Buffer'Length <= Exec_Suffix'Length
744 Buffer (Buffer'Last - Exec_Suffix'Length + 1 .. Buffer'Last)
747 Name_Buffer (Name_Len + 1 .. Name_Len + Exec_Suffix'Length) :=
749 Name_Len := Name_Len + Exec_Suffix'Length;
760 function Executable_Name (Name : String) return String is
761 Exec_Suffix : String_Access;
762 Canonical_Name : String := Name;
765 if Executable_Extension_On_Target = No_Name then
766 Exec_Suffix := Get_Target_Executable_Suffix;
768 Get_Name_String (Executable_Extension_On_Target);
769 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
773 Suffix : constant String := Exec_Suffix.all;
777 Canonical_Case_File_Name (Canonical_Name);
779 if Suffix'Length /= 0
781 (Canonical_Name'Length <= Suffix'Length
782 or else Canonical_Name (Canonical_Name'Last - Suffix'Length + 1
783 .. Canonical_Name'Last) /= Suffix)
786 Result : String (1 .. Name'Length + Suffix'Length);
788 Result (1 .. Name'Length) := Name;
789 Result (Name'Length + 1 .. Result'Last) := Suffix;
798 -----------------------
799 -- Executable_Prefix --
800 -----------------------
802 function Executable_Prefix return String_Ptr is
804 function Get_Install_Dir (Exec : String) return String_Ptr;
805 -- S is the executable name preceeded by the absolute or relative
806 -- path, e.g. "c:\usr\bin\gcc.exe" or "..\bin\gcc".
808 ---------------------
809 -- Get_Install_Dir --
810 ---------------------
812 function Get_Install_Dir (Exec : String) return String_Ptr is
813 Full_Path : constant String := Normalize_Pathname (Exec);
814 -- Use the full path, so that we find "lib" or "bin", even when
815 -- the tool has been invoked with a relative path, as in
816 -- "./gnatls -v" invoked in the GNAT bin directory.
819 for J in reverse Full_Path'Range loop
820 if Is_Directory_Separator (Full_Path (J)) then
821 if J < Full_Path'Last - 5 then
822 if (To_Lower (Full_Path (J + 1)) = 'l'
823 and then To_Lower (Full_Path (J + 2)) = 'i'
824 and then To_Lower (Full_Path (J + 3)) = 'b')
826 (To_Lower (Full_Path (J + 1)) = 'b'
827 and then To_Lower (Full_Path (J + 2)) = 'i'
828 and then To_Lower (Full_Path (J + 3)) = 'n')
830 return new String'(Full_Path (Full_Path'First .. J));
836 return new String'("");
839 -- Start of processing for Executable_Prefix
842 if Exec_Name = null then
843 Exec_Name := new String (1 .. Len_Arg (0));
844 Osint.Fill_Arg (Exec_Name (1)'Address, 0);
847 -- First determine if a path prefix was placed in front of the
850 for J in reverse Exec_Name'Range loop
851 if Is_Directory_Separator (Exec_Name (J)) then
852 return Get_Install_Dir (Exec_Name.all);
856 -- If we come here, the user has typed the executable name with no
859 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name.all).all);
860 end Executable_Prefix;
866 procedure Exit_Program (Exit_Code : Exit_Code_Type) is
868 -- The program will exit with the following status:
870 -- 0 if the object file has been generated (with or without warnings)
871 -- 1 if recompilation was not needed (smart recompilation)
872 -- 2 if gnat1 has been killed by a signal (detected by GCC)
873 -- 4 for a fatal error
874 -- 5 if there were errors
875 -- 6 if no code has been generated (spec)
877 -- Note that exit code 3 is not used and must not be used as this is
878 -- the code returned by a program aborted via C abort() routine on
879 -- Windows. GCC checks for that case and thinks that the child process
880 -- has been aborted. This code (exit code 3) used to be the code used
881 -- for E_No_Code, but E_No_Code was changed to 6 for this reason.
884 when E_Success => OS_Exit (0);
885 when E_Warnings => OS_Exit (0);
886 when E_No_Compile => OS_Exit (1);
887 when E_Fatal => OS_Exit (4);
888 when E_Errors => OS_Exit (5);
889 when E_No_Code => OS_Exit (6);
890 when E_Abort => OS_Abort;
898 procedure Fail (S1 : String; S2 : String := ""; S3 : String := "") is
900 -- We use Output in case there is a special output set up.
901 -- In this case Set_Standard_Error will have no immediate effect.
904 Osint.Write_Program_Name;
911 Exit_Program (E_Fatal);
918 function File_Hash (F : File_Name_Type) return File_Hash_Num is
920 return File_Hash_Num (Int (F) rem File_Hash_Num'Range_Length);
927 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type is
929 if Name = No_File then
930 return Empty_Time_Stamp;
933 Get_Name_String (Name);
935 if not Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
936 return Empty_Time_Stamp;
938 Name_Buffer (Name_Len + 1) := ASCII.NUL;
939 return OS_Time_To_GNAT_Time (File_Time_Stamp (Name_Buffer));
943 function File_Stamp (Name : Path_Name_Type) return Time_Stamp_Type is
945 return File_Stamp (File_Name_Type (Name));
954 T : File_Type) return File_Name_Type
960 File_Name : String renames Name_Buffer (1 .. Name_Len);
961 File : File_Name_Type := No_File;
965 -- If we are looking for a config file, look only in the current
966 -- directory, i.e. return input argument unchanged. Also look
967 -- only in the current directory if we are looking for a .dg
968 -- file (happens in -gnatD mode).
971 or else (Debug_Generated_Code
972 and then Name_Len > 3
974 (Name_Buffer (Name_Len - 2 .. Name_Len) = ".dg"
976 (Hostparm.OpenVMS and then
977 Name_Buffer (Name_Len - 2 .. Name_Len) = "_dg")))
981 -- If we are trying to find the current main file just look in the
982 -- directory where the user said it was.
984 elsif Look_In_Primary_Directory_For_Current_Main
985 and then Current_Main = N
987 return Locate_File (N, T, Primary_Directory, File_Name);
989 -- Otherwise do standard search for source file
992 -- Check the mapping of this file name
994 File := Mapped_Path_Name (N);
996 -- If the file name is mapped to a path name, return the
997 -- corresponding path name
999 if File /= No_File then
1001 -- For locally removed file, Error_Name is returned; then
1002 -- return No_File, indicating the file is not a source.
1004 if File = Error_File_Name then
1012 -- First place to look is in the primary directory (i.e. the same
1013 -- directory as the source) unless this has been disabled with -I-
1015 if Opt.Look_In_Primary_Dir then
1016 File := Locate_File (N, T, Primary_Directory, File_Name);
1018 if File /= No_File then
1023 -- Finally look in directories specified with switches -I/-aI/-aO
1026 Last_Dir := Lib_Search_Directories.Last;
1028 Last_Dir := Src_Search_Directories.Last;
1031 for D in Primary_Directory + 1 .. Last_Dir loop
1032 File := Locate_File (N, T, D, File_Name);
1034 if File /= No_File then
1044 -----------------------
1045 -- Find_Program_Name --
1046 -----------------------
1048 procedure Find_Program_Name is
1049 Command_Name : String (1 .. Len_Arg (0));
1050 Cindex1 : Integer := Command_Name'First;
1051 Cindex2 : Integer := Command_Name'Last;
1054 Fill_Arg (Command_Name'Address, 0);
1056 if Command_Name = "" then
1061 -- The program name might be specified by a full path name. However,
1062 -- we don't want to print that all out in an error message, so the
1063 -- path might need to be stripped away.
1065 for J in reverse Cindex1 .. Cindex2 loop
1066 if Is_Directory_Separator (Command_Name (J)) then
1072 -- Command_Name(Cindex1 .. Cindex2) is now the equivalent of the
1073 -- POSIX command "basename argv[0]"
1075 -- Strip off any versioning information such as found on VMS.
1076 -- This would take the form of TOOL.exe followed by a ";" or "."
1077 -- and a sequence of one or more numbers.
1079 if Command_Name (Cindex2) in '0' .. '9' then
1080 for J in reverse Cindex1 .. Cindex2 loop
1081 if Command_Name (J) = '.' or Command_Name (J) = ';' then
1086 exit when Command_Name (J) not in '0' .. '9';
1090 -- Strip off any executable extension (usually nothing or .exe)
1091 -- but formally reported by autoconf in the variable EXEEXT
1093 if Cindex2 - Cindex1 >= 4 then
1094 if To_Lower (Command_Name (Cindex2 - 3)) = '.'
1095 and then To_Lower (Command_Name (Cindex2 - 2)) = 'e'
1096 and then To_Lower (Command_Name (Cindex2 - 1)) = 'x'
1097 and then To_Lower (Command_Name (Cindex2)) = 'e'
1099 Cindex2 := Cindex2 - 4;
1103 Name_Len := Cindex2 - Cindex1 + 1;
1104 Name_Buffer (1 .. Name_Len) := Command_Name (Cindex1 .. Cindex2);
1105 end Find_Program_Name;
1107 ------------------------
1108 -- Full_Lib_File_Name --
1109 ------------------------
1111 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type is
1113 return Find_File (N, Library);
1114 end Full_Lib_File_Name;
1116 ----------------------------
1117 -- Full_Library_Info_Name --
1118 ----------------------------
1120 function Full_Library_Info_Name return File_Name_Type is
1122 return Current_Full_Lib_Name;
1123 end Full_Library_Info_Name;
1125 ---------------------------
1126 -- Full_Object_File_Name --
1127 ---------------------------
1129 function Full_Object_File_Name return File_Name_Type is
1131 return Current_Full_Obj_Name;
1132 end Full_Object_File_Name;
1134 ----------------------
1135 -- Full_Source_Name --
1136 ----------------------
1138 function Full_Source_Name return File_Name_Type is
1140 return Current_Full_Source_Name;
1141 end Full_Source_Name;
1143 ----------------------
1144 -- Full_Source_Name --
1145 ----------------------
1147 function Full_Source_Name (N : File_Name_Type) return File_Name_Type is
1149 return Smart_Find_File (N, Source);
1150 end Full_Source_Name;
1156 function Get_Directory (Name : File_Name_Type) return File_Name_Type is
1158 Get_Name_String (Name);
1160 for J in reverse 1 .. Name_Len loop
1161 if Is_Directory_Separator (Name_Buffer (J)) then
1167 Name_Len := Hostparm.Normalized_CWD'Length;
1168 Name_Buffer (1 .. Name_Len) := Hostparm.Normalized_CWD;
1172 --------------------------
1173 -- Get_Next_Dir_In_Path --
1174 --------------------------
1176 Search_Path_Pos : Integer;
1177 -- Keeps track of current position in search path. Initialized by the
1178 -- call to Get_Next_Dir_In_Path_Init, updated by Get_Next_Dir_In_Path.
1180 function Get_Next_Dir_In_Path
1181 (Search_Path : String_Access) return String_Access
1183 Lower_Bound : Positive := Search_Path_Pos;
1184 Upper_Bound : Positive;
1188 while Lower_Bound <= Search_Path'Last
1189 and then Search_Path.all (Lower_Bound) = Path_Separator
1191 Lower_Bound := Lower_Bound + 1;
1194 exit when Lower_Bound > Search_Path'Last;
1196 Upper_Bound := Lower_Bound;
1197 while Upper_Bound <= Search_Path'Last
1198 and then Search_Path.all (Upper_Bound) /= Path_Separator
1200 Upper_Bound := Upper_Bound + 1;
1203 Search_Path_Pos := Upper_Bound;
1204 return new String'(Search_Path.all (Lower_Bound .. Upper_Bound - 1));
1208 end Get_Next_Dir_In_Path;
1210 -------------------------------
1211 -- Get_Next_Dir_In_Path_Init --
1212 -------------------------------
1214 procedure Get_Next_Dir_In_Path_Init (Search_Path : String_Access) is
1216 Search_Path_Pos := Search_Path'First;
1217 end Get_Next_Dir_In_Path_Init;
1219 --------------------------------------
1220 -- Get_Primary_Src_Search_Directory --
1221 --------------------------------------
1223 function Get_Primary_Src_Search_Directory return String_Ptr is
1225 return Src_Search_Directories.Table (Primary_Directory);
1226 end Get_Primary_Src_Search_Directory;
1228 ------------------------
1229 -- Get_RTS_Search_Dir --
1230 ------------------------
1232 function Get_RTS_Search_Dir
1233 (Search_Dir : String;
1234 File_Type : Search_File_Type) return String_Ptr
1236 procedure Get_Current_Dir
1237 (Dir : System.Address;
1238 Length : System.Address);
1239 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1242 pragma Import (C, Max_Path, "__gnat_max_path_len");
1243 -- Maximum length of a path name
1245 Current_Dir : String_Ptr;
1246 Default_Search_Dir : String_Access;
1247 Default_Suffix_Dir : String_Access;
1248 Local_Search_Dir : String_Access;
1249 Norm_Search_Dir : String_Access;
1250 Result_Search_Dir : String_Access;
1251 Search_File : String_Access;
1252 Temp_String : String_Ptr;
1255 -- Add a directory separator at the end of the directory if necessary
1256 -- so that we can directly append a file to the directory
1258 if Search_Dir (Search_Dir'Last) /= Directory_Separator then
1260 new String'(Search_Dir & String'(1 => Directory_Separator));
1262 Local_Search_Dir := new String'(Search_Dir);
1265 if File_Type = Include then
1266 Search_File := Include_Search_File;
1267 Default_Suffix_Dir := new String'("adainclude");
1269 Search_File := Objects_Search_File;
1270 Default_Suffix_Dir := new String'("adalib");
1273 Norm_Search_Dir := To_Canonical_Path_Spec (Local_Search_Dir.all);
1275 if Is_Absolute_Path (Norm_Search_Dir.all) then
1277 -- We first verify if there is a directory Include_Search_Dir
1278 -- containing default search directories
1280 Result_Search_Dir :=
1281 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1282 Default_Search_Dir :=
1283 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1284 Free (Norm_Search_Dir);
1286 if Result_Search_Dir /= null then
1287 return String_Ptr (Result_Search_Dir);
1288 elsif Is_Directory (Default_Search_Dir.all) then
1289 return String_Ptr (Default_Search_Dir);
1294 -- Search in the current directory
1297 -- Get the current directory
1300 Buffer : String (1 .. Max_Path + 2);
1301 Path_Len : Natural := Max_Path;
1304 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1306 if Buffer (Path_Len) /= Directory_Separator then
1307 Path_Len := Path_Len + 1;
1308 Buffer (Path_Len) := Directory_Separator;
1311 Current_Dir := new String'(Buffer (1 .. Path_Len));
1315 new String'(Current_Dir.all & Local_Search_Dir.all);
1317 Result_Search_Dir :=
1318 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1320 Default_Search_Dir :=
1321 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1323 Free (Norm_Search_Dir);
1325 if Result_Search_Dir /= null then
1326 return String_Ptr (Result_Search_Dir);
1328 elsif Is_Directory (Default_Search_Dir.all) then
1329 return String_Ptr (Default_Search_Dir);
1332 -- Search in Search_Dir_Prefix/Search_Dir
1336 (Update_Path (Search_Dir_Prefix).all & Local_Search_Dir.all);
1338 Result_Search_Dir :=
1339 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1341 Default_Search_Dir :=
1342 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1344 Free (Norm_Search_Dir);
1346 if Result_Search_Dir /= null then
1347 return String_Ptr (Result_Search_Dir);
1349 elsif Is_Directory (Default_Search_Dir.all) then
1350 return String_Ptr (Default_Search_Dir);
1353 -- We finally search in Search_Dir_Prefix/rts-Search_Dir
1356 new String'(Update_Path (Search_Dir_Prefix).all & "rts-");
1359 new String'(Temp_String.all & Local_Search_Dir.all);
1361 Result_Search_Dir :=
1362 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1364 Default_Search_Dir :=
1365 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1366 Free (Norm_Search_Dir);
1368 if Result_Search_Dir /= null then
1369 return String_Ptr (Result_Search_Dir);
1371 elsif Is_Directory (Default_Search_Dir.all) then
1372 return String_Ptr (Default_Search_Dir);
1380 end Get_RTS_Search_Dir;
1382 --------------------------------
1383 -- Include_Dir_Default_Prefix --
1384 --------------------------------
1386 function Include_Dir_Default_Prefix return String is
1387 Include_Dir : String_Access :=
1388 String_Access (Update_Path (Include_Dir_Default_Name));
1391 if Include_Dir = null then
1396 Result : constant String := Include_Dir.all;
1402 end Include_Dir_Default_Prefix;
1408 procedure Initialize is
1410 Number_File_Names := 0;
1411 Current_File_Name_Index := 0;
1413 Src_Search_Directories.Init;
1414 Lib_Search_Directories.Init;
1416 -- Start off by setting all suppress options to False, these will
1417 -- be reset later (turning some on if -gnato is not specified, and
1418 -- turning all of them on if -gnatp is specified).
1420 Suppress_Options := (others => False);
1422 -- Reserve the first slot in the search paths table. This is the
1423 -- directory of the main source file or main library file and is filled
1424 -- in by each call to Next_Main_Source/Next_Main_Lib_File with the
1425 -- directory specified for this main source or library file. This is the
1426 -- directory which is searched first by default. This default search is
1427 -- inhibited by the option -I- for both source and library files.
1429 Src_Search_Directories.Set_Last (Primary_Directory);
1430 Src_Search_Directories.Table (Primary_Directory) := new String'("");
1432 Lib_Search_Directories.Set_Last (Primary_Directory);
1433 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
1436 ----------------------------
1437 -- Is_Directory_Separator --
1438 ----------------------------
1440 function Is_Directory_Separator (C : Character) return Boolean is
1442 -- In addition to the default directory_separator allow the '/' to
1443 -- act as separator since this is allowed in MS-DOS, Windows 95/NT,
1444 -- and OS2 ports. On VMS, the situation is more complicated because
1445 -- there are two characters to check for.
1448 C = Directory_Separator
1450 or else (Hostparm.OpenVMS
1451 and then (C = ']' or else C = ':'));
1452 end Is_Directory_Separator;
1454 -------------------------
1455 -- Is_Readonly_Library --
1456 -------------------------
1458 function Is_Readonly_Library (File : File_Name_Type) return Boolean is
1460 Get_Name_String (File);
1462 pragma Assert (Name_Buffer (Name_Len - 3 .. Name_Len) = ".ali");
1464 return not Is_Writable_File (Name_Buffer (1 .. Name_Len));
1465 end Is_Readonly_Library;
1471 function Lib_File_Name
1472 (Source_File : File_Name_Type;
1473 Munit_Index : Nat := 0) return File_Name_Type
1476 Get_Name_String (Source_File);
1478 for J in reverse 2 .. Name_Len loop
1479 if Name_Buffer (J) = '.' then
1485 if Munit_Index /= 0 then
1486 Add_Char_To_Name_Buffer (Multi_Unit_Index_Character);
1487 Add_Nat_To_Name_Buffer (Munit_Index);
1490 Add_Char_To_Name_Buffer ('.');
1491 Add_Str_To_Name_Buffer (ALI_Suffix.all);
1495 ------------------------
1496 -- Library_File_Stamp --
1497 ------------------------
1499 function Library_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
1501 return File_Stamp (Find_File (N, Library));
1502 end Library_File_Stamp;
1508 function Locate_File
1509 (N : File_Name_Type;
1512 Name : String) return File_Name_Type
1514 Dir_Name : String_Ptr;
1517 -- If Name is already an absolute path, do not look for a directory
1519 if Is_Absolute_Path (Name) then
1522 elsif T = Library then
1523 Dir_Name := Lib_Search_Directories.Table (Dir);
1525 else pragma Assert (T /= Config);
1526 Dir_Name := Src_Search_Directories.Table (Dir);
1530 Full_Name : String (1 .. Dir_Name'Length + Name'Length);
1533 Full_Name (1 .. Dir_Name'Length) := Dir_Name.all;
1534 Full_Name (Dir_Name'Length + 1 .. Full_Name'Length) := Name;
1536 if not Is_Regular_File (Full_Name) then
1540 -- If the file is in the current directory then return N itself
1542 if Dir_Name'Length = 0 then
1545 Name_Len := Full_Name'Length;
1546 Name_Buffer (1 .. Name_Len) := Full_Name;
1553 -------------------------------
1554 -- Matching_Full_Source_Name --
1555 -------------------------------
1557 function Matching_Full_Source_Name
1558 (N : File_Name_Type;
1559 T : Time_Stamp_Type) return File_Name_Type
1562 Get_Name_String (N);
1565 File_Name : constant String := Name_Buffer (1 .. Name_Len);
1566 File : File_Name_Type := No_File;
1570 if Opt.Look_In_Primary_Dir then
1571 File := Locate_File (N, Source, Primary_Directory, File_Name);
1573 if File /= No_File and then T = File_Stamp (N) then
1578 Last_Dir := Src_Search_Directories.Last;
1580 for D in Primary_Directory + 1 .. Last_Dir loop
1581 File := Locate_File (N, Source, D, File_Name);
1583 if File /= No_File and then T = File_Stamp (File) then
1590 end Matching_Full_Source_Name;
1596 function More_Files return Boolean is
1598 return (Current_File_Name_Index < Number_File_Names);
1601 -------------------------------
1602 -- Nb_Dir_In_Obj_Search_Path --
1603 -------------------------------
1605 function Nb_Dir_In_Obj_Search_Path return Natural is
1607 if Opt.Look_In_Primary_Dir then
1608 return Lib_Search_Directories.Last - Primary_Directory + 1;
1610 return Lib_Search_Directories.Last - Primary_Directory;
1612 end Nb_Dir_In_Obj_Search_Path;
1614 -------------------------------
1615 -- Nb_Dir_In_Src_Search_Path --
1616 -------------------------------
1618 function Nb_Dir_In_Src_Search_Path return Natural is
1620 if Opt.Look_In_Primary_Dir then
1621 return Src_Search_Directories.Last - Primary_Directory + 1;
1623 return Src_Search_Directories.Last - Primary_Directory;
1625 end Nb_Dir_In_Src_Search_Path;
1627 --------------------
1628 -- Next_Main_File --
1629 --------------------
1631 function Next_Main_File return File_Name_Type is
1632 File_Name : String_Ptr;
1633 Dir_Name : String_Ptr;
1637 pragma Assert (More_Files);
1639 Current_File_Name_Index := Current_File_Name_Index + 1;
1641 -- Get the file and directory name
1643 File_Name := File_Names (Current_File_Name_Index);
1644 Fptr := File_Name'First;
1646 for J in reverse File_Name'Range loop
1647 if File_Name (J) = Directory_Separator
1648 or else File_Name (J) = '/'
1650 if J = File_Name'Last then
1651 Fail ("File name missing");
1659 -- Save name of directory in which main unit resides for use in
1660 -- locating other units
1662 Dir_Name := new String'(File_Name (File_Name'First .. Fptr - 1));
1664 case Running_Program is
1667 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1668 Look_In_Primary_Directory_For_Current_Main := True;
1671 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1673 if Fptr > File_Name'First then
1674 Look_In_Primary_Directory_For_Current_Main := True;
1677 when Binder | Gnatls =>
1678 Dir_Name := Normalize_Directory_Name (Dir_Name.all);
1679 Lib_Search_Directories.Table (Primary_Directory) := Dir_Name;
1685 Name_Len := File_Name'Last - Fptr + 1;
1686 Name_Buffer (1 .. Name_Len) := File_Name (Fptr .. File_Name'Last);
1687 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1688 Current_Main := Name_Find;
1690 -- In the gnatmake case, the main file may have not have the
1691 -- extension. Try ".adb" first then ".ads"
1693 if Running_Program = Make then
1695 Orig_Main : constant File_Name_Type := Current_Main;
1698 if Strip_Suffix (Orig_Main) = Orig_Main then
1700 Append_Suffix_To_File_Name (Orig_Main, ".adb");
1702 if Full_Source_Name (Current_Main) = No_File then
1704 Append_Suffix_To_File_Name (Orig_Main, ".ads");
1706 if Full_Source_Name (Current_Main) = No_File then
1707 Current_Main := Orig_Main;
1714 return Current_Main;
1717 ------------------------------
1718 -- Normalize_Directory_Name --
1719 ------------------------------
1721 function Normalize_Directory_Name (Directory : String) return String_Ptr is
1723 function Is_Quoted (Path : String) return Boolean;
1724 pragma Inline (Is_Quoted);
1725 -- Returns true if Path is quoted (either double or single quotes)
1731 function Is_Quoted (Path : String) return Boolean is
1732 First : constant Character := Path (Path'First);
1733 Last : constant Character := Path (Path'Last);
1736 if (First = ''' and then Last = ''')
1738 (First = '"' and then Last = '"')
1746 Result : String_Ptr;
1748 -- Start of processing for Normalize_Directory_Name
1751 if Directory'Length = 0 then
1752 Result := new String'(Hostparm.Normalized_CWD);
1754 elsif Is_Directory_Separator (Directory (Directory'Last)) then
1755 Result := new String'(Directory);
1757 elsif Is_Quoted (Directory) then
1759 -- This is a quoted string, it certainly means that the directory
1760 -- contains some spaces for example. We can safely remove the quotes
1761 -- here as the OS_Lib.Normalize_Arguments will be called before any
1762 -- spawn routines. This ensure that quotes will be added when needed.
1764 Result := new String (1 .. Directory'Length - 1);
1765 Result (1 .. Directory'Length - 2) :=
1766 Directory (Directory'First + 1 .. Directory'Last - 1);
1767 Result (Result'Last) := Directory_Separator;
1770 Result := new String (1 .. Directory'Length + 1);
1771 Result (1 .. Directory'Length) := Directory;
1772 Result (Directory'Length + 1) := Directory_Separator;
1776 end Normalize_Directory_Name;
1778 ---------------------
1779 -- Number_Of_Files --
1780 ---------------------
1782 function Number_Of_Files return Int is
1784 return Number_File_Names;
1785 end Number_Of_Files;
1787 -------------------------------
1788 -- Object_Dir_Default_Prefix --
1789 -------------------------------
1791 function Object_Dir_Default_Prefix return String is
1792 Object_Dir : String_Access :=
1793 String_Access (Update_Path (Object_Dir_Default_Name));
1796 if Object_Dir = null then
1801 Result : constant String := Object_Dir.all;
1807 end Object_Dir_Default_Prefix;
1809 ----------------------
1810 -- Object_File_Name --
1811 ----------------------
1813 function Object_File_Name (N : File_Name_Type) return File_Name_Type is
1819 Get_Name_String (N);
1820 Name_Len := Name_Len - ALI_Suffix'Length - 1;
1822 for J in Target_Object_Suffix'Range loop
1823 Name_Len := Name_Len + 1;
1824 Name_Buffer (Name_Len) := Target_Object_Suffix (J);
1828 end Object_File_Name;
1830 -------------------------------
1831 -- OS_Exit_Through_Exception --
1832 -------------------------------
1834 procedure OS_Exit_Through_Exception (Status : Integer) is
1836 Current_Exit_Status := Status;
1837 raise Types.Terminate_Program;
1838 end OS_Exit_Through_Exception;
1840 --------------------------
1841 -- OS_Time_To_GNAT_Time --
1842 --------------------------
1844 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type is
1845 GNAT_Time : Time_Stamp_Type;
1855 GM_Split (T, Y, Mo, D, H, Mn, S);
1861 Minutes => Nat (Mn),
1866 end OS_Time_To_GNAT_Time;
1872 function Program_Name (Nam : String) return String_Access is
1873 Res : String_Access;
1876 -- Get the name of the current program being executed
1880 -- Find the target prefix if any, for the cross compilation case.
1881 -- For instance in "alpha-dec-vxworks-gcc" the target prefix is
1882 -- "alpha-dec-vxworks-"
1884 while Name_Len > 0 loop
1886 -- All done if we find the last hyphen
1888 if Name_Buffer (Name_Len) = '-' then
1891 -- If directory separator found, we don't want to look further
1892 -- since in this case, no prefix has been found.
1894 elsif Is_Directory_Separator (Name_Buffer (Name_Len)) then
1899 Name_Len := Name_Len - 1;
1902 -- Create the new program name
1904 Res := new String (1 .. Name_Len + Nam'Length);
1905 Res.all (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
1906 Res.all (Name_Len + 1 .. Name_Len + Nam'Length) := Nam;
1910 ------------------------------
1911 -- Read_Default_Search_Dirs --
1912 ------------------------------
1914 function Read_Default_Search_Dirs
1915 (Search_Dir_Prefix : String_Access;
1916 Search_File : String_Access;
1917 Search_Dir_Default_Name : String_Access) return String_Access
1919 Prefix_Len : constant Integer := Search_Dir_Prefix.all'Length;
1920 Buffer : String (1 .. Prefix_Len + Search_File.all'Length + 1);
1921 File_FD : File_Descriptor;
1922 S, S1 : String_Access;
1925 Actual_Len : Integer;
1928 Prev_Was_Separator : Boolean;
1929 Nb_Relative_Dir : Integer;
1931 function Is_Relative (S : String; K : Positive) return Boolean;
1932 pragma Inline (Is_Relative);
1933 -- Returns True if a relative directory specification is found
1934 -- in S at position K, False otherwise.
1940 function Is_Relative (S : String; K : Positive) return Boolean is
1942 return not Is_Absolute_Path (S (K .. S'Last));
1945 -- Start of processing for Read_Default_Search_Dirs
1948 -- Construct a C compatible character string buffer
1950 Buffer (1 .. Search_Dir_Prefix.all'Length)
1951 := Search_Dir_Prefix.all;
1952 Buffer (Search_Dir_Prefix.all'Length + 1 .. Buffer'Last - 1)
1954 Buffer (Buffer'Last) := ASCII.NUL;
1956 File_FD := Open_Read (Buffer'Address, Binary);
1957 if File_FD = Invalid_FD then
1958 return Search_Dir_Default_Name;
1961 Len := Integer (File_Length (File_FD));
1963 -- An extra character for a trailing Path_Separator is allocated
1965 S := new String (1 .. Len + 1);
1966 S (Len + 1) := Path_Separator;
1968 -- Read the file. Note that the loop is not necessary since the
1969 -- whole file is read at once except on VMS.
1973 while Actual_Len /= 0 loop
1974 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
1975 Curr := Curr + Actual_Len;
1978 -- Process the file, translating line and file ending
1979 -- control characters to a path separator character.
1981 Prev_Was_Separator := True;
1982 Nb_Relative_Dir := 0;
1983 for J in 1 .. Len loop
1984 if S (J) in ASCII.NUL .. ASCII.US or else S (J) = ' ' then
1985 S (J) := Path_Separator;
1988 if S (J) = Path_Separator then
1989 Prev_Was_Separator := True;
1992 if Prev_Was_Separator and then Is_Relative (S.all, J) then
1993 Nb_Relative_Dir := Nb_Relative_Dir + 1;
1996 Prev_Was_Separator := False;
2000 if Nb_Relative_Dir = 0 then
2004 -- Add the Search_Dir_Prefix to all relative paths
2006 S1 := new String (1 .. S'Length + Nb_Relative_Dir * Prefix_Len);
2008 Prev_Was_Separator := True;
2009 for J in 1 .. Len + 1 loop
2010 if S (J) = Path_Separator then
2011 Prev_Was_Separator := True;
2014 if Prev_Was_Separator and then Is_Relative (S.all, J) then
2015 S1 (J1 .. J1 + Prefix_Len - 1) := Search_Dir_Prefix.all;
2016 J1 := J1 + Prefix_Len;
2019 Prev_Was_Separator := False;
2027 end Read_Default_Search_Dirs;
2029 -----------------------
2030 -- Read_Library_Info --
2031 -----------------------
2033 function Read_Library_Info
2034 (Lib_File : File_Name_Type;
2035 Fatal_Err : Boolean := False) return Text_Buffer_Ptr
2037 Lib_FD : File_Descriptor;
2038 -- The file descriptor for the current library file. A negative value
2039 -- indicates failure to open the specified source file.
2041 Text : Text_Buffer_Ptr;
2042 -- Allocated text buffer
2045 -- For the calls to Close
2048 Current_Full_Lib_Name := Find_File (Lib_File, Library);
2049 Current_Full_Obj_Name := Object_File_Name (Current_Full_Lib_Name);
2051 if Current_Full_Lib_Name = No_File then
2053 Fail ("Cannot find: ", Name_Buffer (1 .. Name_Len));
2055 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2060 Get_Name_String (Current_Full_Lib_Name);
2061 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2063 -- Open the library FD, note that we open in binary mode, because as
2064 -- documented in the spec, the caller is expected to handle either
2065 -- DOS or Unix mode files, and there is no point in wasting time on
2066 -- text translation when it is not required.
2068 Lib_FD := Open_Read (Name_Buffer'Address, Binary);
2070 if Lib_FD = Invalid_FD then
2072 Fail ("Cannot open: ", Name_Buffer (1 .. Name_Len));
2074 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2079 -- Check for object file consistency if requested
2081 if Opt.Check_Object_Consistency then
2082 Current_Full_Lib_Stamp := File_Stamp (Current_Full_Lib_Name);
2083 Current_Full_Obj_Stamp := File_Stamp (Current_Full_Obj_Name);
2085 if Current_Full_Obj_Stamp (1) = ' ' then
2087 -- When the library is readonly always assume object is consistent
2089 if Is_Readonly_Library (Current_Full_Lib_Name) then
2090 Current_Full_Obj_Stamp := Current_Full_Lib_Stamp;
2092 elsif Fatal_Err then
2093 Get_Name_String (Current_Full_Obj_Name);
2094 Close (Lib_FD, Status);
2096 -- No need to check the status, we fail anyway
2098 Fail ("Cannot find: ", Name_Buffer (1 .. Name_Len));
2101 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2102 Close (Lib_FD, Status);
2104 -- No need to check the status, we return null anyway
2111 -- Read data from the file
2114 Len : constant Integer := Integer (File_Length (Lib_FD));
2115 -- Length of source file text. If it doesn't fit in an integer
2116 -- we're probably stuck anyway (>2 gigs of source seems a lot!)
2118 Actual_Len : Integer := 0;
2120 Lo : constant Text_Ptr := 0;
2121 -- Low bound for allocated text buffer
2123 Hi : Text_Ptr := Text_Ptr (Len);
2124 -- High bound for allocated text buffer. Note length is Len + 1
2125 -- which allows for extra EOF character at the end of the buffer.
2128 -- Allocate text buffer. Note extra character at end for EOF
2130 Text := new Text_Buffer (Lo .. Hi);
2132 -- Some systems (e.g. VMS) have file types that require one
2133 -- read per line, so read until we get the Len bytes or until
2134 -- there are no more characters.
2138 Actual_Len := Read (Lib_FD, Text (Hi)'Address, Len);
2139 Hi := Hi + Text_Ptr (Actual_Len);
2140 exit when Actual_Len = Len or Actual_Len <= 0;
2146 -- Read is complete, close file and we are done
2148 Close (Lib_FD, Status);
2149 -- The status should never be False. But, if it is, what can we do?
2150 -- So, we don't test it.
2154 end Read_Library_Info;
2156 ----------------------
2157 -- Read_Source_File --
2158 ----------------------
2160 procedure Read_Source_File
2161 (N : File_Name_Type;
2163 Hi : out Source_Ptr;
2164 Src : out Source_Buffer_Ptr;
2165 T : File_Type := Source)
2167 Source_File_FD : File_Descriptor;
2168 -- The file descriptor for the current source file. A negative value
2169 -- indicates failure to open the specified source file.
2172 -- Length of file. Assume no more than 2 gigabytes of source!
2174 Actual_Len : Integer;
2177 -- For the call to Close
2180 Current_Full_Source_Name := Find_File (N, T);
2181 Current_Full_Source_Stamp := File_Stamp (Current_Full_Source_Name);
2183 if Current_Full_Source_Name = No_File then
2185 -- If we were trying to access the main file and we could not find
2186 -- it, we have an error.
2188 if N = Current_Main then
2189 Get_Name_String (N);
2190 Fail ("Cannot find: ", Name_Buffer (1 .. Name_Len));
2198 Get_Name_String (Current_Full_Source_Name);
2199 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2201 -- Open the source FD, note that we open in binary mode, because as
2202 -- documented in the spec, the caller is expected to handle either
2203 -- DOS or Unix mode files, and there is no point in wasting time on
2204 -- text translation when it is not required.
2206 Source_File_FD := Open_Read (Name_Buffer'Address, Binary);
2208 if Source_File_FD = Invalid_FD then
2214 -- Prepare to read data from the file
2216 Len := Integer (File_Length (Source_File_FD));
2218 -- Set Hi so that length is one more than the physical length,
2219 -- allowing for the extra EOF character at the end of the buffer
2221 Hi := Lo + Source_Ptr (Len);
2223 -- Do the actual read operation
2226 subtype Actual_Source_Buffer is Source_Buffer (Lo .. Hi);
2227 -- Physical buffer allocated
2229 type Actual_Source_Ptr is access Actual_Source_Buffer;
2230 -- This is the pointer type for the physical buffer allocated
2232 Actual_Ptr : constant Actual_Source_Ptr := new Actual_Source_Buffer;
2233 -- And this is the actual physical buffer
2236 -- Allocate source buffer, allowing extra character at end for EOF
2238 -- Some systems (e.g. VMS) have file types that require one
2239 -- read per line, so read until we get the Len bytes or until
2240 -- there are no more characters.
2244 Actual_Len := Read (Source_File_FD, Actual_Ptr (Hi)'Address, Len);
2245 Hi := Hi + Source_Ptr (Actual_Len);
2246 exit when Actual_Len = Len or Actual_Len <= 0;
2249 Actual_Ptr (Hi) := EOF;
2251 -- Now we need to work out the proper virtual origin pointer to
2252 -- return. This is exactly Actual_Ptr (0)'Address, but we have
2253 -- to be careful to suppress checks to compute this address.
2256 pragma Suppress (All_Checks);
2258 pragma Warnings (Off);
2259 -- This use of unchecked conversion is aliasing safe
2261 function To_Source_Buffer_Ptr is new
2262 Unchecked_Conversion (Address, Source_Buffer_Ptr);
2264 pragma Warnings (On);
2267 Src := To_Source_Buffer_Ptr (Actual_Ptr (0)'Address);
2271 -- Read is complete, get time stamp and close file and we are done
2273 Close (Source_File_FD, Status);
2275 -- The status should never be False. But, if it is, what can we do?
2276 -- So, we don't test it.
2278 end Read_Source_File;
2284 function Relocate_Path
2286 Path : String) return String_Ptr
2290 procedure set_std_prefix (S : String; Len : Integer);
2291 pragma Import (C, set_std_prefix);
2294 if Std_Prefix = null then
2295 Std_Prefix := Executable_Prefix;
2297 if Std_Prefix.all /= "" then
2299 -- Remove trailing directory separator when calling set_std_prefix
2301 set_std_prefix (Std_Prefix.all, Std_Prefix'Length - 1);
2305 if Path (Prefix'Range) = Prefix then
2306 if Std_Prefix.all /= "" then
2308 (1 .. Std_Prefix'Length + Path'Last - Prefix'Last);
2309 S (1 .. Std_Prefix'Length) := Std_Prefix.all;
2310 S (Std_Prefix'Length + 1 .. S'Last) :=
2311 Path (Prefix'Last + 1 .. Path'Last);
2316 return new String'(Path);
2323 procedure Set_Program (P : Program_Type) is
2326 Fail ("Set_Program called twice");
2329 Program_Set := True;
2330 Running_Program := P;
2337 function Shared_Lib (Name : String) return String is
2338 Library : String (1 .. Name'Length + Library_Version'Length + 3);
2339 -- 3 = 2 for "-l" + 1 for "-" before lib version
2342 Library (1 .. 2) := "-l";
2343 Library (3 .. 2 + Name'Length) := Name;
2344 Library (3 + Name'Length) := '-';
2345 Library (4 + Name'Length .. Library'Last) := Library_Version;
2347 if OpenVMS_On_Target then
2348 for K in Library'First + 2 .. Library'Last loop
2349 if Library (K) = '.' or else Library (K) = '-' then
2358 ----------------------
2359 -- Smart_File_Stamp --
2360 ----------------------
2362 function Smart_File_Stamp
2363 (N : File_Name_Type;
2364 T : File_Type) return Time_Stamp_Type
2366 Time_Stamp : Time_Stamp_Type;
2369 if not File_Cache_Enabled then
2370 return File_Stamp (Find_File (N, T));
2373 Time_Stamp := File_Stamp_Hash_Table.Get (N);
2375 if Time_Stamp (1) = ' ' then
2376 Time_Stamp := File_Stamp (Smart_Find_File (N, T));
2377 File_Stamp_Hash_Table.Set (N, Time_Stamp);
2381 end Smart_File_Stamp;
2383 ---------------------
2384 -- Smart_Find_File --
2385 ---------------------
2387 function Smart_Find_File
2388 (N : File_Name_Type;
2389 T : File_Type) return File_Name_Type
2391 Full_File_Name : File_Name_Type;
2394 if not File_Cache_Enabled then
2395 return Find_File (N, T);
2398 Full_File_Name := File_Name_Hash_Table.Get (N);
2400 if Full_File_Name = No_File then
2401 Full_File_Name := Find_File (N, T);
2402 File_Name_Hash_Table.Set (N, Full_File_Name);
2405 return Full_File_Name;
2406 end Smart_Find_File;
2408 ----------------------
2409 -- Source_File_Data --
2410 ----------------------
2412 procedure Source_File_Data (Cache : Boolean) is
2414 File_Cache_Enabled := Cache;
2415 end Source_File_Data;
2417 -----------------------
2418 -- Source_File_Stamp --
2419 -----------------------
2421 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
2423 return Smart_File_Stamp (N, Source);
2424 end Source_File_Stamp;
2426 ---------------------
2427 -- Strip_Directory --
2428 ---------------------
2430 function Strip_Directory (Name : File_Name_Type) return File_Name_Type is
2432 Get_Name_String (Name);
2434 for J in reverse 1 .. Name_Len - 1 loop
2436 -- If we find the last directory separator
2438 if Is_Directory_Separator (Name_Buffer (J)) then
2440 -- Return the part of Name that follows this last directory
2443 Name_Buffer (1 .. Name_Len - J) := Name_Buffer (J + 1 .. Name_Len);
2444 Name_Len := Name_Len - J;
2449 -- There were no directory separator, just return Name
2452 end Strip_Directory;
2458 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type is
2460 Get_Name_String (Name);
2462 for J in reverse 2 .. Name_Len loop
2464 -- If we found the last '.', return part of Name that precedes it
2466 if Name_Buffer (J) = '.' then
2475 ---------------------------
2476 -- To_Canonical_Dir_Spec --
2477 ---------------------------
2479 function To_Canonical_Dir_Spec
2481 Prefix_Style : Boolean) return String_Access
2483 function To_Canonical_Dir_Spec
2484 (Host_Dir : Address;
2485 Prefix_Flag : Integer) return Address;
2486 pragma Import (C, To_Canonical_Dir_Spec, "__gnat_to_canonical_dir_spec");
2488 C_Host_Dir : String (1 .. Host_Dir'Length + 1);
2489 Canonical_Dir_Addr : Address;
2490 Canonical_Dir_Len : Integer;
2493 C_Host_Dir (1 .. Host_Dir'Length) := Host_Dir;
2494 C_Host_Dir (C_Host_Dir'Last) := ASCII.NUL;
2496 if Prefix_Style then
2497 Canonical_Dir_Addr := To_Canonical_Dir_Spec (C_Host_Dir'Address, 1);
2499 Canonical_Dir_Addr := To_Canonical_Dir_Spec (C_Host_Dir'Address, 0);
2501 Canonical_Dir_Len := C_String_Length (Canonical_Dir_Addr);
2503 if Canonical_Dir_Len = 0 then
2506 return To_Path_String_Access (Canonical_Dir_Addr, Canonical_Dir_Len);
2511 Fail ("erroneous directory spec: ", Host_Dir);
2513 end To_Canonical_Dir_Spec;
2515 ---------------------------
2516 -- To_Canonical_File_List --
2517 ---------------------------
2519 function To_Canonical_File_List
2520 (Wildcard_Host_File : String;
2521 Only_Dirs : Boolean) return String_Access_List_Access
2523 function To_Canonical_File_List_Init
2524 (Host_File : Address;
2525 Only_Dirs : Integer) return Integer;
2526 pragma Import (C, To_Canonical_File_List_Init,
2527 "__gnat_to_canonical_file_list_init");
2529 function To_Canonical_File_List_Next return Address;
2530 pragma Import (C, To_Canonical_File_List_Next,
2531 "__gnat_to_canonical_file_list_next");
2533 procedure To_Canonical_File_List_Free;
2534 pragma Import (C, To_Canonical_File_List_Free,
2535 "__gnat_to_canonical_file_list_free");
2537 Num_Files : Integer;
2538 C_Wildcard_Host_File : String (1 .. Wildcard_Host_File'Length + 1);
2541 C_Wildcard_Host_File (1 .. Wildcard_Host_File'Length) :=
2543 C_Wildcard_Host_File (C_Wildcard_Host_File'Last) := ASCII.NUL;
2545 -- Do the expansion and say how many there are
2547 Num_Files := To_Canonical_File_List_Init
2548 (C_Wildcard_Host_File'Address, Boolean'Pos (Only_Dirs));
2551 Canonical_File_List : String_Access_List (1 .. Num_Files);
2552 Canonical_File_Addr : Address;
2553 Canonical_File_Len : Integer;
2556 -- Retrieve the expanded directoy names and build the list
2558 for J in 1 .. Num_Files loop
2559 Canonical_File_Addr := To_Canonical_File_List_Next;
2560 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
2561 Canonical_File_List (J) := To_Path_String_Access
2562 (Canonical_File_Addr, Canonical_File_Len);
2565 -- Free up the storage
2567 To_Canonical_File_List_Free;
2569 return new String_Access_List'(Canonical_File_List);
2571 end To_Canonical_File_List;
2573 ----------------------------
2574 -- To_Canonical_File_Spec --
2575 ----------------------------
2577 function To_Canonical_File_Spec
2578 (Host_File : String) return String_Access
2580 function To_Canonical_File_Spec (Host_File : Address) return Address;
2582 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
2584 C_Host_File : String (1 .. Host_File'Length + 1);
2585 Canonical_File_Addr : Address;
2586 Canonical_File_Len : Integer;
2589 C_Host_File (1 .. Host_File'Length) := Host_File;
2590 C_Host_File (C_Host_File'Last) := ASCII.NUL;
2592 Canonical_File_Addr := To_Canonical_File_Spec (C_Host_File'Address);
2593 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
2595 if Canonical_File_Len = 0 then
2598 return To_Path_String_Access
2599 (Canonical_File_Addr, Canonical_File_Len);
2604 Fail ("erroneous file spec: ", Host_File);
2606 end To_Canonical_File_Spec;
2608 ----------------------------
2609 -- To_Canonical_Path_Spec --
2610 ----------------------------
2612 function To_Canonical_Path_Spec
2613 (Host_Path : String) return String_Access
2615 function To_Canonical_Path_Spec (Host_Path : Address) return Address;
2617 (C, To_Canonical_Path_Spec, "__gnat_to_canonical_path_spec");
2619 C_Host_Path : String (1 .. Host_Path'Length + 1);
2620 Canonical_Path_Addr : Address;
2621 Canonical_Path_Len : Integer;
2624 C_Host_Path (1 .. Host_Path'Length) := Host_Path;
2625 C_Host_Path (C_Host_Path'Last) := ASCII.NUL;
2627 Canonical_Path_Addr := To_Canonical_Path_Spec (C_Host_Path'Address);
2628 Canonical_Path_Len := C_String_Length (Canonical_Path_Addr);
2630 -- Return a null string (vice a null) for zero length paths, for
2631 -- compatibility with getenv().
2633 return To_Path_String_Access (Canonical_Path_Addr, Canonical_Path_Len);
2637 Fail ("erroneous path spec: ", Host_Path);
2639 end To_Canonical_Path_Spec;
2641 ---------------------------
2642 -- To_Host_Dir_Spec --
2643 ---------------------------
2645 function To_Host_Dir_Spec
2646 (Canonical_Dir : String;
2647 Prefix_Style : Boolean) return String_Access
2649 function To_Host_Dir_Spec
2650 (Canonical_Dir : Address;
2651 Prefix_Flag : Integer) return Address;
2652 pragma Import (C, To_Host_Dir_Spec, "__gnat_to_host_dir_spec");
2654 C_Canonical_Dir : String (1 .. Canonical_Dir'Length + 1);
2655 Host_Dir_Addr : Address;
2656 Host_Dir_Len : Integer;
2659 C_Canonical_Dir (1 .. Canonical_Dir'Length) := Canonical_Dir;
2660 C_Canonical_Dir (C_Canonical_Dir'Last) := ASCII.NUL;
2662 if Prefix_Style then
2663 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 1);
2665 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 0);
2667 Host_Dir_Len := C_String_Length (Host_Dir_Addr);
2669 if Host_Dir_Len = 0 then
2672 return To_Path_String_Access (Host_Dir_Addr, Host_Dir_Len);
2674 end To_Host_Dir_Spec;
2676 ----------------------------
2677 -- To_Host_File_Spec --
2678 ----------------------------
2680 function To_Host_File_Spec
2681 (Canonical_File : String) return String_Access
2683 function To_Host_File_Spec (Canonical_File : Address) return Address;
2684 pragma Import (C, To_Host_File_Spec, "__gnat_to_host_file_spec");
2686 C_Canonical_File : String (1 .. Canonical_File'Length + 1);
2687 Host_File_Addr : Address;
2688 Host_File_Len : Integer;
2691 C_Canonical_File (1 .. Canonical_File'Length) := Canonical_File;
2692 C_Canonical_File (C_Canonical_File'Last) := ASCII.NUL;
2694 Host_File_Addr := To_Host_File_Spec (C_Canonical_File'Address);
2695 Host_File_Len := C_String_Length (Host_File_Addr);
2697 if Host_File_Len = 0 then
2700 return To_Path_String_Access
2701 (Host_File_Addr, Host_File_Len);
2703 end To_Host_File_Spec;
2705 ---------------------------
2706 -- To_Path_String_Access --
2707 ---------------------------
2709 function To_Path_String_Access
2710 (Path_Addr : Address;
2711 Path_Len : Integer) return String_Access
2713 subtype Path_String is String (1 .. Path_Len);
2714 type Path_String_Access is access Path_String;
2716 function Address_To_Access is new
2717 Unchecked_Conversion (Source => Address,
2718 Target => Path_String_Access);
2720 Path_Access : constant Path_String_Access :=
2721 Address_To_Access (Path_Addr);
2723 Return_Val : String_Access;
2726 Return_Val := new String (1 .. Path_Len);
2728 for J in 1 .. Path_Len loop
2729 Return_Val (J) := Path_Access (J);
2733 end To_Path_String_Access;
2739 function Update_Path (Path : String_Ptr) return String_Ptr is
2741 function C_Update_Path (Path, Component : Address) return Address;
2742 pragma Import (C, C_Update_Path, "update_path");
2744 function Strlen (Str : Address) return Integer;
2745 pragma Import (C, Strlen, "strlen");
2747 procedure Strncpy (X : Address; Y : Address; Length : Integer);
2748 pragma Import (C, Strncpy, "strncpy");
2750 In_Length : constant Integer := Path'Length;
2751 In_String : String (1 .. In_Length + 1);
2752 Component_Name : aliased String := "GCC" & ASCII.NUL;
2753 Result_Ptr : Address;
2754 Result_Length : Integer;
2755 Out_String : String_Ptr;
2758 In_String (1 .. In_Length) := Path.all;
2759 In_String (In_Length + 1) := ASCII.NUL;
2760 Result_Ptr := C_Update_Path (In_String'Address, Component_Name'Address);
2761 Result_Length := Strlen (Result_Ptr);
2763 Out_String := new String (1 .. Result_Length);
2764 Strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
2772 procedure Write_Info (Info : String) is
2774 Write_With_Check (Info'Address, Info'Length);
2775 Write_With_Check (EOL'Address, 1);
2778 ------------------------
2779 -- Write_Program_Name --
2780 ------------------------
2782 procedure Write_Program_Name is
2783 Save_Buffer : constant String (1 .. Name_Len) :=
2784 Name_Buffer (1 .. Name_Len);
2789 -- Convert the name to lower case so error messages are the same on
2792 for J in 1 .. Name_Len loop
2793 if Name_Buffer (J) in 'A' .. 'Z' then
2795 Character'Val (Character'Pos (Name_Buffer (J)) + 32);
2799 Write_Str (Name_Buffer (1 .. Name_Len));
2801 -- Restore Name_Buffer which was clobbered by the call to
2802 -- Find_Program_Name
2804 Name_Len := Save_Buffer'Last;
2805 Name_Buffer (1 .. Name_Len) := Save_Buffer;
2806 end Write_Program_Name;
2808 ----------------------
2809 -- Write_With_Check --
2810 ----------------------
2812 procedure Write_With_Check (A : Address; N : Integer) is
2816 if N = Write (Output_FD, A, N) then
2820 Write_Str ("error: disk full writing ");
2821 Write_Name_Decoded (Output_File_Name);
2823 Name_Len := Name_Len + 1;
2824 Name_Buffer (Name_Len) := ASCII.NUL;
2825 Delete_File (Name_Buffer'Address, Ignore);
2826 Exit_Program (E_Fatal);
2828 end Write_With_Check;
2830 ----------------------------
2831 -- Package Initialization --
2832 ----------------------------
2835 Initialization : declare
2837 function Get_Default_Identifier_Character_Set return Character;
2838 pragma Import (C, Get_Default_Identifier_Character_Set,
2839 "__gnat_get_default_identifier_character_set");
2840 -- Function to determine the default identifier character set,
2841 -- which is system dependent. See Opt package spec for a list of
2842 -- the possible character codes and their interpretations.
2844 function Get_Maximum_File_Name_Length return Int;
2845 pragma Import (C, Get_Maximum_File_Name_Length,
2846 "__gnat_get_maximum_file_name_length");
2847 -- Function to get maximum file name length for system
2850 Identifier_Character_Set := Get_Default_Identifier_Character_Set;
2851 Maximum_File_Name_Length := Get_Maximum_File_Name_Length;
2853 -- Following should be removed by having above function return
2854 -- Integer'Last as indication of no maximum instead of -1 ???
2856 if Maximum_File_Name_Length = -1 then
2857 Maximum_File_Name_Length := Int'Last;
2860 Src_Search_Directories.Set_Last (Primary_Directory);
2861 Src_Search_Directories.Table (Primary_Directory) := new String'("");
2863 Lib_Search_Directories.Set_Last (Primary_Directory);
2864 Lib_Search_Directories.Table (Primary_Directory) := new String'("");