i386: Allow all register_operand SUBREGs in x86_ternlog_idx.
[official-gcc.git] / gcc / ada / osint.adb
blob046480b321bdcec9fba37ffac1050f8efc9e7de1
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- O S I N T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Alloc;
27 with Debug;
28 with Fmap; use Fmap;
29 with Gnatvsn; use Gnatvsn;
30 with Hostparm;
31 with Opt; use Opt;
32 with Output; use Output;
33 with Sdefault; use Sdefault;
34 with Table;
35 with Targparm; use Targparm;
37 with Ada.Unchecked_Conversion;
39 pragma Warnings (Off);
40 -- This package is used also by gnatcoll
41 with System.Case_Util; use System.Case_Util;
42 with System.CRTL;
43 pragma Warnings (On);
45 with GNAT.HTable;
47 package body Osint is
49 use type CRTL.size_t;
51 Running_Program : Program_Type := Unspecified;
52 -- Set by Set_Program to indicate which of Compiler, Binder, etc is
53 -- running.
55 Program_Set : Boolean := False;
56 -- True if Set_Program has been called; used to detect duplicate calls.
58 Std_Prefix : String_Ptr;
59 -- Standard prefix, computed dynamically the first time Relocate_Path
60 -- is called, and cached for subsequent calls.
62 Empty : aliased String := "";
63 No_Dir : constant String_Ptr := Empty'Access;
64 -- Used in Locate_File as a fake directory when Name is already an
65 -- absolute path.
67 -------------------------------------
68 -- Use of Name_Find and Name_Enter --
69 -------------------------------------
71 -- This package creates a number of source, ALI and object file names
72 -- that are used to locate the actual file and for the purpose of message
73 -- construction. These names need not be accessible by Name_Find, and can
74 -- be therefore created by using routine Name_Enter. The files in question
75 -- are file names with a prefix directory (i.e., the files not in the
76 -- current directory). File names without a prefix directory are entered
77 -- with Name_Find because special values might be attached to the various
78 -- Info fields of the corresponding name table entry.
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Append_Suffix_To_File_Name
85 (Name : File_Name_Type;
86 Suffix : String) return File_Name_Type;
87 -- Appends Suffix to Name and returns the new name
89 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type;
90 -- Convert OS format time to GNAT format time stamp. If T is Invalid_Time,
91 -- then returns Empty_Time_Stamp.
92 -- Round to even seconds on Windows before conversion.
93 -- Windows ALI files had timestamps rounded to even seconds historically.
94 -- The rounding was originally done in GM_Split. Now that GM_Split no
95 -- longer does it, we are rounding it here only for ALI files.
97 function Executable_Prefix return String_Ptr;
98 -- Returns the name of the root directory where the executable is stored.
99 -- The executable must be located in a directory called "bin", or under
100 -- root/lib/gcc-lib/..., or under root/libexec/gcc/... For example, if
101 -- executable is stored in directory "/foo/bar/bin", this routine returns
102 -- "/foo/bar/". Return "" if location is not recognized as described above.
104 function File_Names_Equal (File1, File2 : String) return Boolean;
105 -- Compare File1 and File2 taking into account the case insensitivity
106 -- of the OS.
108 function Update_Path (Path : String_Ptr) return String_Ptr;
109 -- Update the specified path to replace the prefix with the location where
110 -- GNAT is installed. See the file prefix.c in GCC for details.
112 procedure Locate_File
113 (N : File_Name_Type;
114 T : File_Type;
115 Dir : Natural;
116 Name : String;
117 Found : out File_Name_Type;
118 Attr : access File_Attributes);
119 -- See if the file N whose name is Name exists in directory Dir. Dir is an
120 -- index into the Lib_Search_Directories table if T = Library. Otherwise
121 -- if T = Source, Dir is an index into the Src_Search_Directories table.
122 -- Returns the File_Name_Type of the full file name if file found, or
123 -- No_File if not found.
125 -- On exit, Found is set to the file that was found, and Attr to a cache of
126 -- its attributes (at least those that have been computed so far). Reusing
127 -- the cache will save some system calls.
129 -- Attr is always reset in this call to Unknown_Attributes, even in case of
130 -- failure
132 procedure Find_File
133 (N : File_Name_Type;
134 T : File_Type;
135 Found : out File_Name_Type;
136 Attr : access File_Attributes;
137 Full_Name : Boolean := False);
138 -- A version of Find_File that also returns a cache of the file attributes
139 -- for later reuse
141 procedure Smart_Find_File
142 (N : File_Name_Type;
143 T : File_Type;
144 Found : out File_Name_Type;
145 Attr : out File_Attributes);
146 -- A version of Smart_Find_File that also returns a cache of the file
147 -- attributes for later reuse
149 function C_String_Length (S : Address) return CRTL.size_t;
150 -- Returns length of a C string (zero for a null address)
152 function To_Path_String_Access
153 (Path_Addr : Address;
154 Path_Len : CRTL.size_t) return String_Access;
155 -- Converts a C String to an Ada String. We don't use a more general
156 -- purpose facility, because we are dealing with low-level types like
157 -- Address. Caller must free result.
159 function Include_Dir_Default_Prefix return String_Access;
160 -- Same as exported version, except returns a String_Access
162 ------------------------------
163 -- Other Local Declarations --
164 ------------------------------
166 EOL : constant Character := ASCII.LF;
167 -- End of line character
169 Number_File_Names : Nat := 0;
170 -- Number of file names found on command line and placed in File_Names
172 Look_In_Primary_Directory_For_Current_Main : Boolean := False;
173 -- When this variable is True, Find_File only looks in Primary_Directory
174 -- for the Current_Main file. This variable is always set to True for the
175 -- compiler. It is also True for gnatmake, when the source name given on
176 -- the command line has directory information.
178 Current_Full_Source_Name : File_Name_Type := No_File;
179 Current_Full_Source_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
180 Current_Full_Lib_Name : File_Name_Type := No_File;
181 Current_Full_Lib_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
182 Current_Full_Obj_Name : File_Name_Type := No_File;
183 Current_Full_Obj_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
184 -- Respectively full name (with directory info) and time stamp of the
185 -- latest source, library and object files opened by Read_Source_File and
186 -- Read_Library_Info.
188 package File_Name_Chars is new Table.Table (
189 Table_Component_Type => Character,
190 Table_Index_Type => Int,
191 Table_Low_Bound => 1,
192 Table_Initial => Alloc.File_Name_Chars_Initial,
193 Table_Increment => Alloc.File_Name_Chars_Increment,
194 Table_Name => "File_Name_Chars");
195 -- Table to store text to be printed by Dump_Source_File_Names
197 The_Include_Dir_Default_Prefix : String_Access := null;
198 -- Value returned by Include_Dir_Default_Prefix. We don't initialize it
199 -- here, because that causes an elaboration cycle with Sdefault; we
200 -- initialize it lazily instead.
202 ------------------
203 -- Search Paths --
204 ------------------
206 Primary_Directory : constant := 0;
207 -- This is index in the tables created below for the first directory to
208 -- search in for source or library information files. This is the directory
209 -- containing the latest main input file (a source file for the compiler or
210 -- a library file for the binder).
212 package Src_Search_Directories is new Table.Table (
213 Table_Component_Type => String_Ptr,
214 Table_Index_Type => Integer,
215 Table_Low_Bound => Primary_Directory,
216 Table_Initial => 10,
217 Table_Increment => 100,
218 Table_Name => "Osint.Src_Search_Directories");
219 -- Table of names of directories in which to search for source (Compiler)
220 -- files. This table is filled in the order in which the directories are
221 -- to be searched, and then used in that order.
223 package Lib_Search_Directories is new Table.Table (
224 Table_Component_Type => String_Ptr,
225 Table_Index_Type => Integer,
226 Table_Low_Bound => Primary_Directory,
227 Table_Initial => 10,
228 Table_Increment => 100,
229 Table_Name => "Osint.Lib_Search_Directories");
230 -- Table of names of directories in which to search for library (Binder)
231 -- files. This table is filled in the order in which the directories are
232 -- to be searched and then used in that order. The reason for having two
233 -- distinct tables is that we need them both in gnatmake.
235 ---------------------
236 -- File Hash Table --
237 ---------------------
239 -- The file hash table is provided to free the programmer from any
240 -- efficiency concern when retrieving full file names or time stamps of
241 -- source files. If the programmer calls Source_File_Data (Cache => True)
242 -- he is guaranteed that the price to retrieve the full name (i.e. with
243 -- directory info) or time stamp of the file will be payed only once, the
244 -- first time the full name is actually searched (or the first time the
245 -- time stamp is actually retrieved). This is achieved by employing a hash
246 -- table that stores as a key the File_Name_Type of the file and associates
247 -- to that File_Name_Type the full file name and time stamp of the file.
249 File_Cache_Enabled : Boolean := False;
250 -- Set to true if you want the enable the file data caching mechanism
252 type File_Hash_Num is range 0 .. 1020;
254 function File_Hash (F : File_Name_Type) return File_Hash_Num;
255 -- Compute hash index for use by Simple_HTable
257 type File_Info_Cache is record
258 File : File_Name_Type;
259 Attr : aliased File_Attributes;
260 end record;
262 No_File_Info_Cache : constant File_Info_Cache := (No_File, (others => 0));
264 package File_Name_Hash_Table is new GNAT.HTable.Simple_HTable (
265 Header_Num => File_Hash_Num,
266 Element => File_Info_Cache,
267 No_Element => No_File_Info_Cache,
268 Key => File_Name_Type,
269 Hash => File_Hash,
270 Equal => "=");
272 function Smart_Find_File
273 (N : File_Name_Type;
274 T : File_Type) return File_Name_Type;
275 -- Exactly like Find_File except that if File_Cache_Enabled is True this
276 -- routine looks first in the hash table to see if the full name of the
277 -- file is already available.
279 function Smart_File_Stamp
280 (N : File_Name_Type;
281 T : File_Type) return Time_Stamp_Type;
282 -- Takes the same parameter as the routine above (N is a file name without
283 -- any prefix directory information) and behaves like File_Stamp except
284 -- that if File_Cache_Enabled is True this routine looks first in the hash
285 -- table to see if the file stamp of the file is already available.
287 -----------------------------
288 -- Add_Default_Search_Dirs --
289 -----------------------------
291 procedure Add_Default_Search_Dirs is
292 Search_Dir : String_Access;
293 Search_Path : String_Access;
294 Path_File_Name : String_Access;
296 procedure Add_Search_Dir
297 (Search_Dir : String;
298 Additional_Source_Dir : Boolean);
299 procedure Add_Search_Dir
300 (Search_Dir : String_Access;
301 Additional_Source_Dir : Boolean);
302 -- Add a source search dir or a library search dir, depending on the
303 -- value of Additional_Source_Dir.
305 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean);
306 -- Open a path file and read the directory to search, one per line
308 function Get_Libraries_From_Registry return String_Ptr;
309 -- On Windows systems, get the list of installed standard libraries
310 -- from the registry key:
312 -- HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\
313 -- GNAT\Standard Libraries
314 -- Return an empty string on other systems.
316 -- Note that this is an undocumented legacy feature, and that it
317 -- works only when using the default runtime library (i.e. no --RTS=
318 -- command line switch).
320 --------------------
321 -- Add_Search_Dir --
322 --------------------
324 procedure Add_Search_Dir
325 (Search_Dir : String;
326 Additional_Source_Dir : Boolean)
328 begin
329 if Additional_Source_Dir then
330 Add_Src_Search_Dir (Search_Dir);
331 else
332 Add_Lib_Search_Dir (Search_Dir);
333 end if;
334 end Add_Search_Dir;
336 procedure Add_Search_Dir
337 (Search_Dir : String_Access;
338 Additional_Source_Dir : Boolean)
340 begin
341 if Additional_Source_Dir then
342 Add_Src_Search_Dir (Search_Dir.all);
343 else
344 Add_Lib_Search_Dir (Search_Dir.all);
345 end if;
346 end Add_Search_Dir;
348 ------------------------
349 -- Get_Dirs_From_File --
350 ------------------------
352 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean) is
353 File_FD : File_Descriptor;
354 Buffer : constant String := Path_File_Name.all & ASCII.NUL;
355 Len : Natural;
356 Actual_Len : Natural;
357 S : String_Access;
358 Curr : Natural;
359 First : Natural;
360 Ch : Character;
362 Status : Boolean;
363 pragma Warnings (Off, Status);
364 -- For the call to Close where status is ignored
366 begin
367 File_FD := Open_Read (Buffer'Address, Binary);
369 -- If we cannot open the file, we ignore it, we don't fail
371 if File_FD = Invalid_FD then
372 return;
373 end if;
375 Len := Integer (File_Length (File_FD));
377 S := new String (1 .. Len);
379 -- Read the file. Note that the loop is probably not necessary any
380 -- more since the whole file is read in at once on all targets. But
381 -- it is harmless and might be needed in future.
383 Curr := 1;
384 Actual_Len := Len;
385 while Curr <= Len and then Actual_Len /= 0 loop
386 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
387 Curr := Curr + Actual_Len;
388 end loop;
390 -- We are done with the file, so we close it (ignore any error on
391 -- the close, since we have successfully read the file).
393 Close (File_FD, Status);
395 -- Now, we read line by line
397 First := 1;
398 Curr := 0;
399 while Curr < Len loop
400 Ch := S (Curr + 1);
402 if Ch = ASCII.CR or else Ch = ASCII.LF
403 or else Ch = ASCII.FF or else Ch = ASCII.VT
404 then
405 if First <= Curr then
406 Add_Search_Dir (S (First .. Curr), Additional_Source_Dir);
407 end if;
409 First := Curr + 2;
410 end if;
412 Curr := Curr + 1;
413 end loop;
415 -- Last line is a special case, if the file does not end with
416 -- an end of line mark.
418 if First <= S'Last then
419 Add_Search_Dir (S (First .. S'Last), Additional_Source_Dir);
420 end if;
421 end Get_Dirs_From_File;
423 ---------------------------------
424 -- Get_Libraries_From_Registry --
425 ---------------------------------
427 function Get_Libraries_From_Registry return String_Ptr is
428 function C_Get_Libraries_From_Registry return Address;
429 pragma Import (C, C_Get_Libraries_From_Registry,
430 "__gnat_get_libraries_from_registry");
432 Result_Ptr : Address;
433 Result_Length : CRTL.size_t;
434 Out_String : String_Ptr;
436 begin
437 Result_Ptr := C_Get_Libraries_From_Registry;
438 Result_Length := CRTL.strlen (Result_Ptr);
440 Out_String := new String (1 .. Integer (Result_Length));
441 CRTL.strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
443 CRTL.free (Result_Ptr);
445 return Out_String;
446 end Get_Libraries_From_Registry;
448 -- Start of processing for Add_Default_Search_Dirs
450 begin
451 -- If there was a -gnateO switch, add all object directories from the
452 -- file given in argument to the library search list.
454 if Object_Path_File_Name /= null then
455 Path_File_Name := String_Access (Object_Path_File_Name);
456 pragma Assert (Path_File_Name'Length > 0);
457 Get_Dirs_From_File (Additional_Source_Dir => False);
458 end if;
460 -- After the locations specified on the command line, the next places
461 -- to look for files are the directories specified by the appropriate
462 -- environment variable. Get this value, extract the directory names
463 -- and store in the tables.
465 -- Check for eventual project path file env vars
467 Path_File_Name := Getenv (Project_Include_Path_File);
469 if Path_File_Name'Length > 0 then
470 Get_Dirs_From_File (Additional_Source_Dir => True);
471 end if;
473 Path_File_Name := Getenv (Project_Objects_Path_File);
475 if Path_File_Name'Length > 0 then
476 Get_Dirs_From_File (Additional_Source_Dir => False);
477 end if;
479 -- Put path name in canonical form
481 for Additional_Source_Dir in False .. True loop
482 if Additional_Source_Dir then
483 Search_Path := Getenv (Ada_Include_Path);
485 else
486 Search_Path := Getenv (Ada_Objects_Path);
488 end if;
490 Get_Next_Dir_In_Path_Init (Search_Path);
491 loop
492 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
493 exit when Search_Dir = null;
494 Add_Search_Dir (Search_Dir, Additional_Source_Dir);
495 end loop;
496 end loop;
498 -- For the compiler, if --RTS= was specified, add the runtime
499 -- directories.
501 if RTS_Src_Path_Name /= null and then RTS_Lib_Path_Name /= null then
502 Add_Search_Dirs (RTS_Src_Path_Name, Include);
503 Add_Search_Dirs (RTS_Lib_Path_Name, Objects);
505 else
506 if not Opt.No_Stdinc then
508 -- For WIN32 systems, look for any system libraries defined in
509 -- the registry. These are added to both source and object
510 -- directories.
512 Search_Path := String_Access (Get_Libraries_From_Registry);
514 Get_Next_Dir_In_Path_Init (Search_Path);
515 loop
516 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
517 exit when Search_Dir = null;
518 Add_Search_Dir (Search_Dir, False);
519 Add_Search_Dir (Search_Dir, True);
520 end loop;
522 -- The last place to look are the defaults
524 Search_Path :=
525 Read_Default_Search_Dirs
526 (String_Access (Update_Path (Search_Dir_Prefix)),
527 Include_Search_File,
528 String_Access (Update_Path (Include_Dir_Default_Name)));
530 Get_Next_Dir_In_Path_Init (Search_Path);
531 loop
532 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
533 exit when Search_Dir = null;
534 Add_Search_Dir (Search_Dir, True);
535 end loop;
536 end if;
538 -- Even when -nostdlib is used, we still want to have visibility on
539 -- the run-time object directory, as it is used by gnatbind to find
540 -- the run-time ALI files in "real" ZFP set up.
542 if not Opt.RTS_Switch then
543 Search_Path :=
544 Read_Default_Search_Dirs
545 (String_Access (Update_Path (Search_Dir_Prefix)),
546 Objects_Search_File,
547 String_Access (Update_Path (Object_Dir_Default_Name)));
549 Get_Next_Dir_In_Path_Init (Search_Path);
550 loop
551 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
552 exit when Search_Dir = null;
553 Add_Search_Dir (Search_Dir, False);
554 end loop;
555 end if;
556 end if;
557 end Add_Default_Search_Dirs;
559 --------------
560 -- Add_File --
561 --------------
563 procedure Add_File (File_Name : String; Index : Int := No_Index) is
564 begin
565 Number_File_Names := Number_File_Names + 1;
567 -- As Add_File may be called for mains specified inside a project file,
568 -- File_Names may be too short and needs to be extended.
570 if Number_File_Names > File_Names'Last then
571 File_Names := new File_Name_Array'(File_Names.all & File_Names.all);
572 File_Indexes :=
573 new File_Index_Array'(File_Indexes.all & File_Indexes.all);
574 end if;
576 File_Names (Number_File_Names) := new String'(File_Name);
577 File_Indexes (Number_File_Names) := Index;
578 end Add_File;
580 ------------------------
581 -- Add_Lib_Search_Dir --
582 ------------------------
584 procedure Add_Lib_Search_Dir (Dir : String) is
585 begin
586 if Dir'Length = 0 then
587 Fail ("missing library directory name");
588 end if;
590 declare
591 Norm : String_Ptr := Normalize_Directory_Name (Dir);
593 begin
594 -- Do nothing if the directory is already in the list. This saves
595 -- system calls and avoid unneeded work
597 for D in Lib_Search_Directories.First ..
598 Lib_Search_Directories.Last
599 loop
600 if Lib_Search_Directories.Table (D).all = Norm.all then
601 Free (Norm);
602 return;
603 end if;
604 end loop;
606 Lib_Search_Directories.Increment_Last;
607 Lib_Search_Directories.Table (Lib_Search_Directories.Last) := Norm;
608 end;
609 end Add_Lib_Search_Dir;
611 ---------------------
612 -- Add_Search_Dirs --
613 ---------------------
615 procedure Add_Search_Dirs
616 (Search_Path : String_Ptr;
617 Path_Type : Search_File_Type)
619 Current_Search_Path : String_Access;
621 begin
622 Get_Next_Dir_In_Path_Init (String_Access (Search_Path));
623 loop
624 Current_Search_Path :=
625 Get_Next_Dir_In_Path (String_Access (Search_Path));
626 exit when Current_Search_Path = null;
628 if Path_Type = Include then
629 Add_Src_Search_Dir (Current_Search_Path.all);
630 else
631 Add_Lib_Search_Dir (Current_Search_Path.all);
632 end if;
633 end loop;
634 end Add_Search_Dirs;
636 ------------------------
637 -- Add_Src_Search_Dir --
638 ------------------------
640 procedure Add_Src_Search_Dir (Dir : String) is
641 begin
642 if Dir'Length = 0 then
643 Fail ("missing source directory name");
644 end if;
646 Src_Search_Directories.Increment_Last;
647 Src_Search_Directories.Table (Src_Search_Directories.Last) :=
648 Normalize_Directory_Name (Dir);
649 end Add_Src_Search_Dir;
651 --------------------------------
652 -- Append_Suffix_To_File_Name --
653 --------------------------------
655 function Append_Suffix_To_File_Name
656 (Name : File_Name_Type;
657 Suffix : String) return File_Name_Type
659 begin
660 Get_Name_String (Name);
661 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
662 Name_Len := Name_Len + Suffix'Length;
663 return Name_Find;
664 end Append_Suffix_To_File_Name;
666 ---------------------
667 -- C_String_Length --
668 ---------------------
670 function C_String_Length (S : Address) return CRTL.size_t is
671 begin
672 if S = Null_Address then
673 return 0;
674 else
675 return CRTL.strlen (S);
676 end if;
677 end C_String_Length;
679 ------------------------------
680 -- Canonical_Case_File_Name --
681 ------------------------------
683 procedure Canonical_Case_File_Name (S : in out String) is
684 begin
685 if not File_Names_Case_Sensitive then
686 To_Lower (S);
687 end if;
688 end Canonical_Case_File_Name;
690 ---------------------------------
691 -- Canonical_Case_Env_Var_Name --
692 ---------------------------------
694 procedure Canonical_Case_Env_Var_Name (S : in out String) is
695 begin
696 if not Env_Vars_Case_Sensitive then
697 To_Lower (S);
698 end if;
699 end Canonical_Case_Env_Var_Name;
701 ---------------------------
702 -- Create_File_And_Check --
703 ---------------------------
705 procedure Create_File_And_Check
706 (Fdesc : out File_Descriptor;
707 Fmode : Mode)
709 begin
710 Output_File_Name := Name_Enter;
711 Fdesc := Create_File (Name_Buffer'Address, Fmode);
713 if Fdesc = Invalid_FD then
714 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
715 end if;
716 end Create_File_And_Check;
718 -----------------------------------
719 -- Open_File_To_Append_And_Check --
720 -----------------------------------
722 procedure Open_File_To_Append_And_Check
723 (Fdesc : out File_Descriptor;
724 Fmode : Mode)
726 begin
727 Output_File_Name := Name_Enter;
728 Fdesc := Open_Append (Name_Buffer'Address, Fmode);
730 if Fdesc = Invalid_FD then
731 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
732 end if;
733 end Open_File_To_Append_And_Check;
735 ------------------------
736 -- Current_File_Index --
737 ------------------------
739 function Current_File_Index return Int is
740 begin
741 return File_Indexes (Current_File_Name_Index);
742 end Current_File_Index;
744 --------------------------------
745 -- Current_Library_File_Stamp --
746 --------------------------------
748 function Current_Library_File_Stamp return Time_Stamp_Type is
749 begin
750 return Current_Full_Lib_Stamp;
751 end Current_Library_File_Stamp;
753 -------------------------------
754 -- Current_Object_File_Stamp --
755 -------------------------------
757 function Current_Object_File_Stamp return Time_Stamp_Type is
758 begin
759 return Current_Full_Obj_Stamp;
760 end Current_Object_File_Stamp;
762 -------------------------------
763 -- Current_Source_File_Stamp --
764 -------------------------------
766 function Current_Source_File_Stamp return Time_Stamp_Type is
767 begin
768 return Current_Full_Source_Stamp;
769 end Current_Source_File_Stamp;
771 ----------------------------
772 -- Dir_In_Obj_Search_Path --
773 ----------------------------
775 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr is
776 begin
777 if Opt.Look_In_Primary_Dir then
778 return
779 Lib_Search_Directories.Table (Primary_Directory + Position - 1);
780 else
781 return Lib_Search_Directories.Table (Primary_Directory + Position);
782 end if;
783 end Dir_In_Obj_Search_Path;
785 ----------------------------
786 -- Dir_In_Src_Search_Path --
787 ----------------------------
789 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr is
790 begin
791 if Opt.Look_In_Primary_Dir then
792 return
793 Src_Search_Directories.Table (Primary_Directory + Position - 1);
794 else
795 return Src_Search_Directories.Table (Primary_Directory + Position);
796 end if;
797 end Dir_In_Src_Search_Path;
799 -----------------------------------------
800 -- Dump_Command_Line_Source_File_Names --
801 -----------------------------------------
803 procedure Dump_Command_Line_Source_File_Names is
804 begin
805 for J in 1 .. Number_Of_Files loop
806 Write_Str (File_Names (J).all & " ");
807 end loop;
808 end Dump_Command_Line_Source_File_Names;
810 ----------------------------
811 -- Dump_Source_File_Names --
812 ----------------------------
814 procedure Dump_Source_File_Names is
815 subtype Rng is Int range File_Name_Chars.First .. File_Name_Chars.Last;
816 begin
817 Write_Str (String (File_Name_Chars.Table (Rng)));
818 end Dump_Source_File_Names;
820 ---------------------
821 -- Executable_Name --
822 ---------------------
824 function Executable_Name
825 (Name : File_Name_Type;
826 Only_If_No_Suffix : Boolean := False) return File_Name_Type
828 Exec_Suffix : String_Access;
829 Add_Suffix : Boolean;
831 begin
832 if Name = No_File then
833 return No_File;
834 end if;
836 if Executable_Extension_On_Target = No_Name then
837 Exec_Suffix := Get_Target_Executable_Suffix;
838 else
839 Get_Name_String (Executable_Extension_On_Target);
840 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
841 end if;
843 if Exec_Suffix'Length /= 0 then
844 Get_Name_String (Name);
846 Add_Suffix := True;
847 if Only_If_No_Suffix then
848 for J in reverse 1 .. Name_Len loop
849 if Name_Buffer (J) = '.' then
850 Add_Suffix := False;
851 exit;
853 elsif Is_Directory_Separator (Name_Buffer (J)) then
854 exit;
855 end if;
856 end loop;
857 end if;
859 if Add_Suffix then
860 -- If Executable doesn't end with the executable suffix, add it
862 if Name_Len <= Exec_Suffix'Length
863 or else not
864 File_Names_Equal
865 (Name_Buffer
866 (Name_Len - Exec_Suffix'Length + 1 .. Name_Len),
867 Exec_Suffix.all)
868 then
869 Name_Buffer
870 (Name_Len + 1 .. Name_Len + Exec_Suffix'Length) :=
871 Exec_Suffix.all;
872 Name_Len := Name_Len + Exec_Suffix'Length;
873 Free (Exec_Suffix);
874 return Name_Find;
875 end if;
876 end if;
877 end if;
879 Free (Exec_Suffix);
880 return Name;
881 end Executable_Name;
883 function Executable_Name
884 (Name : String;
885 Only_If_No_Suffix : Boolean := False) return String
887 Exec_Suffix : String_Access;
888 Add_Suffix : Boolean;
890 begin
891 if Executable_Extension_On_Target = No_Name then
892 Exec_Suffix := Get_Target_Executable_Suffix;
893 else
894 Get_Name_String (Executable_Extension_On_Target);
895 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
896 end if;
898 if Exec_Suffix'Length = 0 then
899 Free (Exec_Suffix);
900 return Name;
902 else
903 declare
904 Suffix : constant String := Exec_Suffix.all;
906 begin
907 Free (Exec_Suffix);
908 Add_Suffix := True;
910 if Only_If_No_Suffix then
911 for J in reverse Name'Range loop
912 if Name (J) = '.' then
913 Add_Suffix := False;
914 exit;
916 elsif Is_Directory_Separator (Name (J)) then
917 exit;
918 end if;
919 end loop;
920 end if;
922 if Add_Suffix and then
923 (Name'Length <= Suffix'Length
924 or else not
925 File_Names_Equal
926 (Name (Name'Last - Suffix'Length + 1 .. Name'Last),
927 Suffix))
928 then
929 declare
930 Result : String (1 .. Name'Length + Suffix'Length);
931 begin
932 Result (1 .. Name'Length) := Name;
933 Result (Name'Length + 1 .. Result'Last) := Suffix;
934 return Result;
935 end;
936 else
937 return Name;
938 end if;
939 end;
940 end if;
941 end Executable_Name;
943 -----------------------
944 -- Executable_Prefix --
945 -----------------------
947 function Executable_Prefix return String_Ptr is
949 function Get_Install_Dir (Exec : String) return String_Ptr;
950 -- S is the executable name preceded by the absolute or relative
951 -- path, e.g. "c:\usr\bin\gcc.exe" or "..\bin\gcc".
953 ---------------------
954 -- Get_Install_Dir --
955 ---------------------
957 function Get_Install_Dir (Exec : String) return String_Ptr is
958 Full_Path : constant String := Normalize_Pathname (Exec);
959 -- Use the full path, so that we find "lib" or "bin", even when
960 -- the tool has been invoked with a relative path, as in
961 -- "./gnatls -v" invoked in the GNAT bin directory.
963 begin
964 for J in reverse Full_Path'Range loop
965 if Is_Directory_Separator (Full_Path (J)) then
966 if J < Full_Path'Last - 5 then
967 if (To_Lower (Full_Path (J + 1)) = 'l'
968 and then To_Lower (Full_Path (J + 2)) = 'i'
969 and then To_Lower (Full_Path (J + 3)) = 'b')
970 or else
971 (To_Lower (Full_Path (J + 1)) = 'b'
972 and then To_Lower (Full_Path (J + 2)) = 'i'
973 and then To_Lower (Full_Path (J + 3)) = 'n')
974 then
975 return new String'(Full_Path (Full_Path'First .. J));
976 end if;
977 end if;
978 end if;
979 end loop;
981 return new String'("");
982 end Get_Install_Dir;
984 -- Start of processing for Executable_Prefix
986 begin
987 if Exec_Name = null then
988 Exec_Name := new String (1 .. Len_Arg (0));
989 Osint.Fill_Arg (Exec_Name (1)'Address, 0);
990 end if;
992 -- First determine if a path prefix was placed in front of the
993 -- executable name.
995 for J in reverse Exec_Name'Range loop
996 if Is_Directory_Separator (Exec_Name (J)) then
997 return Get_Install_Dir (Exec_Name.all);
998 end if;
999 end loop;
1001 -- If we come here, the user has typed the executable name with no
1002 -- directory prefix.
1004 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name.all).all);
1005 end Executable_Prefix;
1007 ------------------
1008 -- Exit_Program --
1009 ------------------
1011 procedure Exit_Program (Exit_Code : Exit_Code_Type) is
1012 begin
1013 -- The program will exit with the following status:
1015 -- 0 if the object file has been generated (with or without warnings)
1016 -- 1 if recompilation was not needed (smart recompilation)
1017 -- 2 if gnat1 has been killed by a signal (detected by GCC)
1018 -- 4 for a fatal error
1019 -- 5 if there were errors
1020 -- 6 if no code has been generated (spec)
1022 -- Note that exit code 3 is not used and must not be used as this is
1023 -- the code returned by a program aborted via C abort() routine on
1024 -- Windows. GCC checks for that case and thinks that the child process
1025 -- has been aborted. This code (exit code 3) used to be the code used
1026 -- for E_No_Code, but E_No_Code was changed to 6 for this reason.
1028 case Exit_Code is
1029 when E_Success => OS_Exit (0);
1030 when E_Warnings => OS_Exit (0);
1031 when E_No_Compile => OS_Exit (1);
1032 when E_Fatal => OS_Exit (4);
1033 when E_Errors => OS_Exit (5);
1034 when E_No_Code => OS_Exit (6);
1035 when E_Abort => OS_Abort;
1036 end case;
1037 end Exit_Program;
1039 ----------
1040 -- Fail --
1041 ----------
1043 procedure Fail (S : String) is
1044 begin
1045 -- We use Output in case there is a special output set up. In this case
1046 -- Set_Standard_Error will have no immediate effect.
1048 Set_Standard_Error;
1049 Osint.Write_Program_Name;
1050 Write_Str (": ");
1051 Write_Str (S);
1052 Write_Eol;
1054 Exit_Program (E_Fatal);
1055 end Fail;
1057 ----------------------
1058 -- File_Names_Equal --
1059 ----------------------
1061 function File_Names_Equal (File1, File2 : String) return Boolean is
1063 function To_Lower (A : String) return String;
1064 -- For bootstrap reasons, we cannot use To_Lower function from
1065 -- System.Case_Util.
1067 --------------
1068 -- To_Lower --
1069 --------------
1071 function To_Lower (A : String) return String is
1072 Result : String := A;
1073 begin
1074 To_Lower (Result);
1075 return Result;
1076 end To_Lower;
1078 -- Start of processing for File_Names_Equal
1080 begin
1081 if File_Names_Case_Sensitive then
1082 return File1 = File2;
1083 else
1084 return To_Lower (File1) = To_Lower (File2);
1085 end if;
1086 end File_Names_Equal;
1088 ---------------
1089 -- File_Hash --
1090 ---------------
1092 function File_Hash (F : File_Name_Type) return File_Hash_Num is
1093 begin
1094 return File_Hash_Num (Int (F) mod File_Hash_Num'Range_Length);
1095 end File_Hash;
1097 -----------------
1098 -- File_Length --
1099 -----------------
1101 function File_Length
1102 (Name : C_File_Name;
1103 Attr : access File_Attributes) return Long_Integer
1105 function Internal
1106 (F : Integer;
1107 N : C_File_Name;
1108 A : System.Address) return CRTL.int64;
1109 pragma Import (C, Internal, "__gnat_file_length_attr");
1111 begin
1112 -- The conversion from int64 to Long_Integer is ok here as this
1113 -- routine is only to be used by the compiler and we do not expect
1114 -- a unit to be larger than a 32bit integer.
1116 return Long_Integer (Internal (-1, Name, Attr.all'Address));
1117 end File_Length;
1119 ---------------------
1120 -- File_Time_Stamp --
1121 ---------------------
1123 function File_Time_Stamp
1124 (Name : C_File_Name;
1125 Attr : access File_Attributes) return OS_Time
1127 function Internal (N : C_File_Name; A : System.Address) return OS_Time;
1128 pragma Import (C, Internal, "__gnat_file_time_name_attr");
1129 begin
1130 return Internal (Name, Attr.all'Address);
1131 end File_Time_Stamp;
1133 function File_Time_Stamp
1134 (Name : Path_Name_Type;
1135 Attr : access File_Attributes) return Time_Stamp_Type
1137 begin
1138 if Name = No_Path then
1139 return Empty_Time_Stamp;
1140 end if;
1142 Get_Name_String (Name);
1143 Name_Buffer (Name_Len + 1) := ASCII.NUL;
1144 return OS_Time_To_GNAT_Time
1145 (File_Time_Stamp (Name_Buffer'Address, Attr));
1146 end File_Time_Stamp;
1148 ----------------
1149 -- File_Stamp --
1150 ----------------
1152 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type is
1153 begin
1154 if Name = No_File then
1155 return Empty_Time_Stamp;
1156 end if;
1158 Get_Name_String (Name);
1160 -- File_Time_Stamp will always return Invalid_Time if the file does
1161 -- not exist, and OS_Time_To_GNAT_Time will convert this value to
1162 -- Empty_Time_Stamp. Therefore we do not need to first test whether
1163 -- the file actually exists, which saves a system call.
1165 return OS_Time_To_GNAT_Time
1166 (File_Time_Stamp (Name_Buffer (1 .. Name_Len)));
1167 end File_Stamp;
1169 function File_Stamp (Name : Path_Name_Type) return Time_Stamp_Type is
1170 begin
1171 return File_Stamp (File_Name_Type (Name));
1172 end File_Stamp;
1174 ---------------
1175 -- Find_File --
1176 ---------------
1178 function Find_File
1179 (N : File_Name_Type;
1180 T : File_Type;
1181 Full_Name : Boolean := False) return File_Name_Type
1183 Attr : aliased File_Attributes;
1184 Found : File_Name_Type;
1185 begin
1186 Find_File (N, T, Found, Attr'Access, Full_Name);
1187 return Found;
1188 end Find_File;
1190 ---------------
1191 -- Find_File --
1192 ---------------
1194 procedure Find_File
1195 (N : File_Name_Type;
1196 T : File_Type;
1197 Found : out File_Name_Type;
1198 Attr : access File_Attributes;
1199 Full_Name : Boolean := False)
1201 begin
1202 Get_Name_String (N);
1204 declare
1205 File_Name : String renames Name_Buffer (1 .. Name_Len);
1206 File : File_Name_Type := No_File;
1207 Last_Dir : Natural;
1209 begin
1210 -- If we are looking for a config file, look only in the current
1211 -- directory, i.e. return input argument unchanged. Also look only in
1212 -- the current directory if we are looking for a .dg file (happens in
1213 -- -gnatD mode).
1215 if T = Config
1216 or else (Debug_Generated_Code
1217 and then Name_Len > 3
1218 and then Name_Buffer (Name_Len - 2 .. Name_Len) = ".dg")
1219 then
1220 Found := N;
1221 Attr.all := Unknown_Attributes;
1223 if T = Config then
1224 if Full_Name then
1225 declare
1226 Full_Path : constant String :=
1227 Normalize_Pathname (Get_Name_String (N));
1228 Full_Size : constant Natural := Full_Path'Length;
1230 begin
1231 Name_Buffer (1 .. Full_Size) := Full_Path;
1232 Name_Len := Full_Size;
1233 Found := Name_Find;
1234 end;
1235 end if;
1237 -- Check that it is a file, not a directory
1239 if not Is_Regular_File (Get_Name_String (Found)) then
1240 Found := No_File;
1241 end if;
1242 end if;
1244 return;
1246 -- If we are trying to find the current main file just look in the
1247 -- directory where the user said it was.
1249 elsif Look_In_Primary_Directory_For_Current_Main
1250 and then Current_Main = N
1251 then
1252 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1253 return;
1255 -- Otherwise do standard search for source file
1257 else
1258 -- Check the mapping of this file name
1260 File := Mapped_Path_Name (N);
1262 -- If the file name is mapped to a path name, return the
1263 -- corresponding path name
1265 if File /= No_File then
1267 -- For locally removed file, Error_Name is returned; then
1268 -- return No_File, indicating the file is not a source.
1270 if File = Error_File_Name then
1271 Found := No_File;
1272 else
1273 Found := File;
1274 end if;
1276 Attr.all := Unknown_Attributes;
1277 return;
1278 end if;
1280 -- First place to look is in the primary directory (i.e. the same
1281 -- directory as the source) unless this has been disabled with -I-
1283 if Opt.Look_In_Primary_Dir then
1284 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1286 if Found /= No_File then
1287 return;
1288 end if;
1289 end if;
1291 -- Finally look in directories specified with switches -I/-aI/-aO
1293 if T = Library then
1294 Last_Dir := Lib_Search_Directories.Last;
1295 else
1296 Last_Dir := Src_Search_Directories.Last;
1297 end if;
1299 for D in Primary_Directory + 1 .. Last_Dir loop
1300 Locate_File (N, T, D, File_Name, Found, Attr);
1302 if Found /= No_File then
1303 return;
1304 end if;
1305 end loop;
1307 Attr.all := Unknown_Attributes;
1308 Found := No_File;
1309 end if;
1310 end;
1311 end Find_File;
1313 -----------------------
1314 -- Find_Program_Name --
1315 -----------------------
1317 procedure Find_Program_Name is
1318 Command_Name : String (1 .. Len_Arg (0));
1319 Cindex1 : Integer := Command_Name'First;
1320 Cindex2 : Integer := Command_Name'Last;
1322 begin
1323 Fill_Arg (Command_Name'Address, 0);
1325 if Command_Name = "" then
1326 Name_Len := 0;
1327 return;
1328 end if;
1330 -- The program name might be specified by a full path name. However,
1331 -- we don't want to print that all out in an error message, so the
1332 -- path might need to be stripped away.
1334 for J in reverse Cindex1 .. Cindex2 loop
1335 if Is_Directory_Separator (Command_Name (J)) then
1336 Cindex1 := J + 1;
1337 exit;
1338 end if;
1339 end loop;
1341 -- Command_Name(Cindex1 .. Cindex2) is now the equivalent of the
1342 -- POSIX command "basename argv[0]"
1344 -- Strip off any executable extension (usually nothing or .exe)
1345 -- but formally reported by autoconf in the variable EXEEXT
1347 if Cindex2 - Cindex1 >= 4 then
1348 if To_Lower (Command_Name (Cindex2 - 3)) = '.'
1349 and then To_Lower (Command_Name (Cindex2 - 2)) = 'e'
1350 and then To_Lower (Command_Name (Cindex2 - 1)) = 'x'
1351 and then To_Lower (Command_Name (Cindex2)) = 'e'
1352 then
1353 Cindex2 := Cindex2 - 4;
1354 end if;
1355 end if;
1357 Name_Len := Cindex2 - Cindex1 + 1;
1358 Name_Buffer (1 .. Name_Len) := Command_Name (Cindex1 .. Cindex2);
1359 end Find_Program_Name;
1361 ------------------------
1362 -- Full_Lib_File_Name --
1363 ------------------------
1365 procedure Full_Lib_File_Name
1366 (N : File_Name_Type;
1367 Lib_File : out File_Name_Type;
1368 Attr : out File_Attributes)
1370 begin
1371 Smart_Find_File (N, Library, Lib_File, Attr);
1372 end Full_Lib_File_Name;
1374 ------------------------
1375 -- Full_Lib_File_Name --
1376 ------------------------
1378 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type is
1379 Attr : File_Attributes;
1380 File : File_Name_Type;
1381 begin
1382 Full_Lib_File_Name (N, File, Attr);
1383 return File;
1384 end Full_Lib_File_Name;
1386 ----------------------------
1387 -- Full_Library_Info_Name --
1388 ----------------------------
1390 function Full_Library_Info_Name return File_Name_Type is
1391 begin
1392 return Current_Full_Lib_Name;
1393 end Full_Library_Info_Name;
1395 ---------------------------
1396 -- Full_Object_File_Name --
1397 ---------------------------
1399 function Full_Object_File_Name return File_Name_Type is
1400 begin
1401 return Current_Full_Obj_Name;
1402 end Full_Object_File_Name;
1404 ----------------------
1405 -- Full_Source_Name --
1406 ----------------------
1408 function Full_Source_Name return File_Name_Type is
1409 begin
1410 return Current_Full_Source_Name;
1411 end Full_Source_Name;
1413 ----------------------
1414 -- Full_Source_Name --
1415 ----------------------
1417 function Full_Source_Name (N : File_Name_Type) return File_Name_Type is
1418 begin
1419 return Smart_Find_File (N, Source);
1420 end Full_Source_Name;
1422 ----------------------
1423 -- Full_Source_Name --
1424 ----------------------
1426 procedure Full_Source_Name
1427 (N : File_Name_Type;
1428 Full_File : out File_Name_Type;
1429 Attr : access File_Attributes) is
1430 begin
1431 Smart_Find_File (N, Source, Full_File, Attr.all);
1432 end Full_Source_Name;
1434 -------------------
1435 -- Get_Directory --
1436 -------------------
1438 function Get_Directory (Name : File_Name_Type) return File_Name_Type is
1439 begin
1440 Get_Name_String (Name);
1442 for J in reverse 1 .. Name_Len loop
1443 if Is_Directory_Separator (Name_Buffer (J)) then
1444 Name_Len := J;
1445 return Name_Find;
1446 end if;
1447 end loop;
1449 Name_Len := Hostparm.Normalized_CWD'Length;
1450 Name_Buffer (1 .. Name_Len) := Hostparm.Normalized_CWD;
1451 return Name_Find;
1452 end Get_Directory;
1454 ------------------------------
1455 -- Get_First_Main_File_Name --
1456 ------------------------------
1458 function Get_First_Main_File_Name return String is
1459 begin
1460 return File_Names (1).all;
1461 end Get_First_Main_File_Name;
1463 --------------------------
1464 -- Get_Next_Dir_In_Path --
1465 --------------------------
1467 Search_Path_Pos : Integer;
1468 -- Keeps track of current position in search path. Initialized by the
1469 -- call to Get_Next_Dir_In_Path_Init, updated by Get_Next_Dir_In_Path.
1471 function Get_Next_Dir_In_Path
1472 (Search_Path : String_Access) return String_Access
1474 Lower_Bound : Positive := Search_Path_Pos;
1475 Upper_Bound : Positive;
1477 begin
1478 loop
1479 while Lower_Bound <= Search_Path'Last
1480 and then Search_Path.all (Lower_Bound) = Path_Separator
1481 loop
1482 Lower_Bound := Lower_Bound + 1;
1483 end loop;
1485 exit when Lower_Bound > Search_Path'Last;
1487 Upper_Bound := Lower_Bound;
1488 while Upper_Bound <= Search_Path'Last
1489 and then Search_Path.all (Upper_Bound) /= Path_Separator
1490 loop
1491 Upper_Bound := Upper_Bound + 1;
1492 end loop;
1494 Search_Path_Pos := Upper_Bound;
1495 return new String'(Search_Path.all (Lower_Bound .. Upper_Bound - 1));
1496 end loop;
1498 return null;
1499 end Get_Next_Dir_In_Path;
1501 -------------------------------
1502 -- Get_Next_Dir_In_Path_Init --
1503 -------------------------------
1505 procedure Get_Next_Dir_In_Path_Init (Search_Path : String_Access) is
1506 begin
1507 Search_Path_Pos := Search_Path'First;
1508 end Get_Next_Dir_In_Path_Init;
1510 --------------------------------------
1511 -- Get_Primary_Src_Search_Directory --
1512 --------------------------------------
1514 function Get_Primary_Src_Search_Directory return String_Ptr is
1515 begin
1516 return Src_Search_Directories.Table (Primary_Directory);
1517 end Get_Primary_Src_Search_Directory;
1519 ------------------------
1520 -- Get_RTS_Search_Dir --
1521 ------------------------
1523 function Get_RTS_Search_Dir
1524 (Search_Dir : String;
1525 File_Type : Search_File_Type) return String_Ptr
1527 procedure Get_Current_Dir
1528 (Dir : System.Address;
1529 Length : System.Address);
1530 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1532 Max_Path : Integer;
1533 pragma Import (C, Max_Path, "__gnat_max_path_len");
1534 -- Maximum length of a path name
1536 Current_Dir : String_Ptr;
1537 Default_Search_Dir : String_Access;
1538 Default_Suffix_Dir : String_Access;
1539 Local_Search_Dir : String_Access;
1540 Norm_Search_Dir : String_Access;
1541 Result_Search_Dir : String_Access;
1542 Search_File : String_Access;
1543 Temp_String : String_Ptr;
1545 begin
1546 -- Add a directory separator at the end of the directory if necessary
1547 -- so that we can directly append a file to the directory
1549 if not Is_Directory_Separator (Search_Dir (Search_Dir'Last)) then
1550 Local_Search_Dir :=
1551 new String'(Search_Dir & String'(1 => Directory_Separator));
1552 else
1553 Local_Search_Dir := new String'(Search_Dir);
1554 end if;
1556 if File_Type = Include then
1557 Search_File := Include_Search_File;
1558 Default_Suffix_Dir := new String'("adainclude");
1559 else
1560 Search_File := Objects_Search_File;
1561 Default_Suffix_Dir := new String'("adalib");
1562 end if;
1564 Norm_Search_Dir := Local_Search_Dir;
1566 if Is_Absolute_Path (Norm_Search_Dir.all) then
1568 -- We first verify if there is a directory Include_Search_Dir
1569 -- containing default search directories
1571 Result_Search_Dir :=
1572 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1573 Default_Search_Dir :=
1574 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1575 Free (Norm_Search_Dir);
1577 if Result_Search_Dir /= null then
1578 return String_Ptr (Result_Search_Dir);
1579 elsif Is_Directory (Default_Search_Dir.all) then
1580 return String_Ptr (Default_Search_Dir);
1581 else
1582 return null;
1583 end if;
1585 -- Search in the current directory
1587 else
1588 -- Get the current directory
1590 declare
1591 Buffer : String (1 .. Max_Path + 2);
1592 Path_Len : Natural := Max_Path;
1594 begin
1595 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1597 if Path_Len = 0 then
1598 raise Program_Error;
1599 end if;
1601 if not Is_Directory_Separator (Buffer (Path_Len)) then
1602 Path_Len := Path_Len + 1;
1603 Buffer (Path_Len) := Directory_Separator;
1604 end if;
1606 Current_Dir := new String'(Buffer (1 .. Path_Len));
1607 end;
1609 Norm_Search_Dir :=
1610 new String'(Current_Dir.all & Local_Search_Dir.all);
1612 Result_Search_Dir :=
1613 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1615 Default_Search_Dir :=
1616 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1618 Free (Norm_Search_Dir);
1620 if Result_Search_Dir /= null then
1621 return String_Ptr (Result_Search_Dir);
1623 elsif Is_Directory (Default_Search_Dir.all) then
1624 return String_Ptr (Default_Search_Dir);
1626 else
1627 -- Search in Search_Dir_Prefix/Search_Dir
1629 Norm_Search_Dir :=
1630 new String'
1631 (Update_Path (Search_Dir_Prefix).all & Local_Search_Dir.all);
1633 Result_Search_Dir :=
1634 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1636 Default_Search_Dir :=
1637 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1639 Free (Norm_Search_Dir);
1641 if Result_Search_Dir /= null then
1642 return String_Ptr (Result_Search_Dir);
1644 elsif Is_Directory (Default_Search_Dir.all) then
1645 return String_Ptr (Default_Search_Dir);
1647 else
1648 -- We finally search in Search_Dir_Prefix/rts-Search_Dir
1650 Temp_String :=
1651 new String'(Update_Path (Search_Dir_Prefix).all & "rts-");
1653 Norm_Search_Dir :=
1654 new String'(Temp_String.all & Local_Search_Dir.all);
1656 Result_Search_Dir :=
1657 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1659 Default_Search_Dir :=
1660 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1661 Free (Norm_Search_Dir);
1663 if Result_Search_Dir /= null then
1664 return String_Ptr (Result_Search_Dir);
1666 elsif Is_Directory (Default_Search_Dir.all) then
1667 return String_Ptr (Default_Search_Dir);
1669 else
1670 return null;
1671 end if;
1672 end if;
1673 end if;
1674 end if;
1675 end Get_RTS_Search_Dir;
1677 --------------------------------
1678 -- Include_Dir_Default_Prefix --
1679 --------------------------------
1681 function Include_Dir_Default_Prefix return String_Access is
1682 begin
1683 if The_Include_Dir_Default_Prefix = null then
1684 The_Include_Dir_Default_Prefix :=
1685 String_Access (Update_Path (Include_Dir_Default_Name));
1686 end if;
1688 return The_Include_Dir_Default_Prefix;
1689 end Include_Dir_Default_Prefix;
1691 function Include_Dir_Default_Prefix return String is
1692 begin
1693 return Include_Dir_Default_Prefix.all;
1694 end Include_Dir_Default_Prefix;
1696 ----------------
1697 -- Initialize --
1698 ----------------
1700 procedure Initialize is
1701 begin
1702 Number_File_Names := 0;
1703 Current_File_Name_Index := 0;
1705 Src_Search_Directories.Init;
1706 Lib_Search_Directories.Init;
1708 -- Start off by setting all suppress options, to False. The special
1709 -- overflow fields are set to Not_Set (they will be set by -gnatp, or
1710 -- by -gnato, or, if neither of these appear, in Adjust_Global_Switches
1711 -- in Gnat1drv).
1713 Suppress_Options := ((others => False), Not_Set, Not_Set);
1715 -- Reserve the first slot in the search paths table. This is the
1716 -- directory of the main source file or main library file and is filled
1717 -- in by each call to Next_Main_Source/Next_Main_Lib_File with the
1718 -- directory specified for this main source or library file. This is the
1719 -- directory which is searched first by default. This default search is
1720 -- inhibited by the option -I- for both source and library files.
1722 Src_Search_Directories.Set_Last (Primary_Directory);
1723 Src_Search_Directories.Table (Primary_Directory) := new String'("");
1725 Lib_Search_Directories.Set_Last (Primary_Directory);
1726 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
1727 end Initialize;
1729 ------------------
1730 -- Is_Directory --
1731 ------------------
1733 function Is_Directory
1734 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1736 function Internal (N : C_File_Name; A : System.Address) return Integer;
1737 pragma Import (C, Internal, "__gnat_is_directory_attr");
1738 begin
1739 return Internal (Name, Attr.all'Address) /= 0;
1740 end Is_Directory;
1742 ----------------------------
1743 -- Is_Directory_Separator --
1744 ----------------------------
1746 function Is_Directory_Separator (C : Character) return Boolean is
1747 begin
1748 -- In addition to the default directory_separator allow the '/' to
1749 -- act as separator since this is allowed in MS-DOS and Windows.
1751 return C = Directory_Separator or else C = '/';
1752 end Is_Directory_Separator;
1754 -------------------------
1755 -- Is_Readonly_Library --
1756 -------------------------
1758 function Is_Readonly_Library (File : File_Name_Type) return Boolean is
1759 begin
1760 Get_Name_String (File);
1762 pragma Assert (Name_Buffer (Name_Len - 3 .. Name_Len) = ".ali");
1764 return not Is_Writable_File (Name_Buffer (1 .. Name_Len));
1765 end Is_Readonly_Library;
1767 ------------------------
1768 -- Is_Executable_File --
1769 ------------------------
1771 function Is_Executable_File
1772 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1774 function Internal (N : C_File_Name; A : System.Address) return Integer;
1775 pragma Import (C, Internal, "__gnat_is_executable_file_attr");
1776 begin
1777 return Internal (Name, Attr.all'Address) /= 0;
1778 end Is_Executable_File;
1780 ----------------------
1781 -- Is_Readable_File --
1782 ----------------------
1784 function Is_Readable_File
1785 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1787 function Internal (N : C_File_Name; A : System.Address) return Integer;
1788 pragma Import (C, Internal, "__gnat_is_readable_file_attr");
1789 begin
1790 return Internal (Name, Attr.all'Address) /= 0;
1791 end Is_Readable_File;
1793 ---------------------
1794 -- Is_Regular_File --
1795 ---------------------
1797 function Is_Regular_File
1798 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1800 function Internal (N : C_File_Name; A : System.Address) return Integer;
1801 pragma Import (C, Internal, "__gnat_is_regular_file_attr");
1802 begin
1803 return Internal (Name, Attr.all'Address) /= 0;
1804 end Is_Regular_File;
1806 ----------------------
1807 -- Is_Symbolic_Link --
1808 ----------------------
1810 function Is_Symbolic_Link
1811 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1813 function Internal (N : C_File_Name; A : System.Address) return Integer;
1814 pragma Import (C, Internal, "__gnat_is_symbolic_link_attr");
1815 begin
1816 return Internal (Name, Attr.all'Address) /= 0;
1817 end Is_Symbolic_Link;
1819 ----------------------
1820 -- Is_Writable_File --
1821 ----------------------
1823 function Is_Writable_File
1824 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1826 function Internal (N : C_File_Name; A : System.Address) return Integer;
1827 pragma Import (C, Internal, "__gnat_is_writable_file_attr");
1828 begin
1829 return Internal (Name, Attr.all'Address) /= 0;
1830 end Is_Writable_File;
1832 -------------------
1833 -- Lib_File_Name --
1834 -------------------
1836 function Lib_File_Name
1837 (Source_File : File_Name_Type;
1838 Munit_Index : Nat := 0) return File_Name_Type
1840 begin
1841 Get_Name_String (Source_File);
1843 for J in reverse 2 .. Name_Len loop
1844 if Name_Buffer (J) = '.' then
1845 Name_Len := J - 1;
1846 exit;
1847 end if;
1848 end loop;
1850 if Munit_Index /= 0 then
1851 Add_Char_To_Name_Buffer (Multi_Unit_Index_Character);
1852 Add_Nat_To_Name_Buffer (Munit_Index);
1853 end if;
1855 Add_Char_To_Name_Buffer ('.');
1856 Add_Str_To_Name_Buffer (ALI_Suffix.all);
1857 return Name_Find;
1858 end Lib_File_Name;
1860 -----------------
1861 -- Locate_File --
1862 -----------------
1864 procedure Locate_File
1865 (N : File_Name_Type;
1866 T : File_Type;
1867 Dir : Natural;
1868 Name : String;
1869 Found : out File_Name_Type;
1870 Attr : access File_Attributes)
1872 Dir_Name : String_Ptr;
1874 begin
1875 -- If Name is already an absolute path, do not look for a directory
1877 if Is_Absolute_Path (Name) then
1878 Dir_Name := No_Dir;
1880 elsif T = Library then
1881 Dir_Name := Lib_Search_Directories.Table (Dir);
1883 else
1884 pragma Assert (T /= Config);
1885 Dir_Name := Src_Search_Directories.Table (Dir);
1886 end if;
1888 declare
1889 Full_Name :
1890 constant String (1 .. Dir_Name'Length + Name'Length + 1) :=
1891 Dir_Name.all & Name & ASCII.NUL;
1892 -- Use explicit bounds, because Dir_Name might be a substring whose
1893 -- 'First is not 1.
1895 begin
1896 Attr.all := Unknown_Attributes;
1898 if not Is_Regular_File (Full_Name'Address, Attr) then
1899 Found := No_File;
1901 else
1902 -- If the file is in the current directory then return N itself
1904 if Dir_Name'Length = 0 then
1905 Found := N;
1906 else
1907 Found :=
1908 Name_Find (Full_Name (Full_Name'First .. Full_Name'Last - 1));
1909 end if;
1910 end if;
1911 end;
1912 end Locate_File;
1914 -------------------------------
1915 -- Matching_Full_Source_Name --
1916 -------------------------------
1918 function Matching_Full_Source_Name
1919 (N : File_Name_Type;
1920 T : Time_Stamp_Type) return File_Name_Type
1922 begin
1923 Get_Name_String (N);
1925 declare
1926 File_Name : constant String := Name_Buffer (1 .. Name_Len);
1927 File : File_Name_Type := No_File;
1928 Attr : aliased File_Attributes;
1929 Last_Dir : Natural;
1931 begin
1932 if Opt.Look_In_Primary_Dir then
1933 Locate_File
1934 (N, Source, Primary_Directory, File_Name, File,
1935 Attr'Unchecked_Access);
1937 if File /= No_File and then T = File_Stamp (N) then
1938 return File;
1939 end if;
1940 end if;
1942 Last_Dir := Src_Search_Directories.Last;
1944 for D in Primary_Directory + 1 .. Last_Dir loop
1945 Locate_File (N, Source, D, File_Name, File, Attr'Unchecked_Access);
1947 if File /= No_File and then T = File_Stamp (File) then
1948 return File;
1949 end if;
1950 end loop;
1952 return No_File;
1953 end;
1954 end Matching_Full_Source_Name;
1956 ----------------
1957 -- More_Files --
1958 ----------------
1960 function More_Files return Boolean is
1961 begin
1962 return (Current_File_Name_Index < Number_File_Names);
1963 end More_Files;
1965 -------------------------------
1966 -- Nb_Dir_In_Obj_Search_Path --
1967 -------------------------------
1969 function Nb_Dir_In_Obj_Search_Path return Natural is
1970 begin
1971 if Opt.Look_In_Primary_Dir then
1972 return Lib_Search_Directories.Last - Primary_Directory + 1;
1973 else
1974 return Lib_Search_Directories.Last - Primary_Directory;
1975 end if;
1976 end Nb_Dir_In_Obj_Search_Path;
1978 -------------------------------
1979 -- Nb_Dir_In_Src_Search_Path --
1980 -------------------------------
1982 function Nb_Dir_In_Src_Search_Path return Natural is
1983 begin
1984 if Opt.Look_In_Primary_Dir then
1985 return Src_Search_Directories.Last - Primary_Directory + 1;
1986 else
1987 return Src_Search_Directories.Last - Primary_Directory;
1988 end if;
1989 end Nb_Dir_In_Src_Search_Path;
1991 --------------------
1992 -- Next_Main_File --
1993 --------------------
1995 function Next_Main_File return File_Name_Type is
1996 File_Name : String_Ptr;
1997 Dir_Name : String_Ptr;
1998 Fptr : Natural;
2000 begin
2001 pragma Assert (More_Files);
2003 Current_File_Name_Index := Current_File_Name_Index + 1;
2005 -- Get the file and directory name
2007 File_Name := File_Names (Current_File_Name_Index);
2008 Fptr := File_Name'First;
2010 for J in reverse File_Name'Range loop
2011 if Is_Directory_Separator (File_Name (J)) then
2012 if J = File_Name'Last then
2013 Fail ("File name missing");
2014 end if;
2016 Fptr := J + 1;
2017 exit;
2018 end if;
2019 end loop;
2021 -- Save name of directory in which main unit resides for use in
2022 -- locating other units
2024 Dir_Name := new String'(File_Name (File_Name'First .. Fptr - 1));
2026 case Running_Program is
2027 when Compiler =>
2028 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
2029 Look_In_Primary_Directory_For_Current_Main := True;
2031 when Make =>
2032 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
2034 if Fptr > File_Name'First then
2035 Look_In_Primary_Directory_For_Current_Main := True;
2036 end if;
2038 when Binder
2039 | Gnatls
2041 Dir_Name := Normalize_Directory_Name (Dir_Name.all);
2042 Lib_Search_Directories.Table (Primary_Directory) := Dir_Name;
2044 when Unspecified =>
2045 null;
2046 end case;
2048 Name_Len := File_Name'Last - Fptr + 1;
2049 Name_Buffer (1 .. Name_Len) := File_Name (Fptr .. File_Name'Last);
2050 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2051 Current_Main := Name_Find;
2053 -- In the gnatmake case, the main file may have not have the
2054 -- extension. Try ".adb" first then ".ads"
2056 if Running_Program = Make then
2057 declare
2058 Orig_Main : constant File_Name_Type := Current_Main;
2060 begin
2061 if Strip_Suffix (Orig_Main) = Orig_Main then
2062 Current_Main :=
2063 Append_Suffix_To_File_Name (Orig_Main, ".adb");
2065 if Full_Source_Name (Current_Main) = No_File then
2066 Current_Main :=
2067 Append_Suffix_To_File_Name (Orig_Main, ".ads");
2069 if Full_Source_Name (Current_Main) = No_File then
2070 Current_Main := Orig_Main;
2071 end if;
2072 end if;
2073 end if;
2074 end;
2075 end if;
2077 return Current_Main;
2078 end Next_Main_File;
2080 ------------------------------
2081 -- Normalize_Directory_Name --
2082 ------------------------------
2084 function Normalize_Directory_Name (Directory : String) return String_Ptr is
2086 function Is_Quoted (Path : String) return Boolean;
2087 pragma Inline (Is_Quoted);
2088 -- Returns true if Path is quoted (either double or single quotes)
2090 ---------------
2091 -- Is_Quoted --
2092 ---------------
2094 function Is_Quoted (Path : String) return Boolean is
2095 First : constant Character := Path (Path'First);
2096 Last : constant Character := Path (Path'Last);
2098 begin
2099 if (First = ''' and then Last = ''')
2100 or else
2101 (First = '"' and then Last = '"')
2102 then
2103 return True;
2104 else
2105 return False;
2106 end if;
2107 end Is_Quoted;
2109 Result : String_Ptr;
2111 -- Start of processing for Normalize_Directory_Name
2113 begin
2114 if Directory'Length = 0 then
2115 Result := new String'(Hostparm.Normalized_CWD);
2117 elsif Is_Directory_Separator (Directory (Directory'Last)) then
2118 Result := new String'(Directory);
2120 elsif Is_Quoted (Directory) then
2122 -- This is a quoted string, it certainly means that the directory
2123 -- contains some spaces for example. We can safely remove the quotes
2124 -- here as the OS_Lib.Normalize_Arguments will be called before any
2125 -- spawn routines. This ensure that quotes will be added when needed.
2127 Result := new String (1 .. Directory'Length - 1);
2128 Result (1 .. Directory'Length - 2) :=
2129 Directory (Directory'First + 1 .. Directory'Last - 1);
2130 Result (Result'Last) := Directory_Separator;
2132 else
2133 Result := new String (1 .. Directory'Length + 1);
2134 Result (1 .. Directory'Length) := Directory;
2135 Result (Directory'Length + 1) := Directory_Separator;
2136 end if;
2138 return Result;
2139 end Normalize_Directory_Name;
2141 ---------------------
2142 -- Number_Of_Files --
2143 ---------------------
2145 function Number_Of_Files return Nat is
2146 begin
2147 return Number_File_Names;
2148 end Number_Of_Files;
2150 -------------------------------
2151 -- Object_Dir_Default_Prefix --
2152 -------------------------------
2154 function Object_Dir_Default_Prefix return String is
2155 Object_Dir : String_Access :=
2156 String_Access (Update_Path (Object_Dir_Default_Name));
2158 begin
2159 if Object_Dir = null then
2160 return "";
2162 else
2163 declare
2164 Result : constant String := Object_Dir.all;
2165 begin
2166 Free (Object_Dir);
2167 return Result;
2168 end;
2169 end if;
2170 end Object_Dir_Default_Prefix;
2172 ----------------------
2173 -- Object_File_Name --
2174 ----------------------
2176 function Object_File_Name (N : File_Name_Type) return File_Name_Type is
2177 begin
2178 if N = No_File then
2179 return No_File;
2180 end if;
2182 Get_Name_String (N);
2183 Name_Len := Name_Len - ALI_Suffix'Length - 1;
2185 for J in Target_Object_Suffix'Range loop
2186 Name_Len := Name_Len + 1;
2187 Name_Buffer (Name_Len) := Target_Object_Suffix (J);
2188 end loop;
2190 return Name_Enter;
2191 end Object_File_Name;
2193 -------------------------------
2194 -- OS_Exit_Through_Exception --
2195 -------------------------------
2197 procedure OS_Exit_Through_Exception (Status : Integer) is
2198 begin
2199 Current_Exit_Status := Status;
2200 raise Types.Terminate_Program;
2201 end OS_Exit_Through_Exception;
2203 --------------------------
2204 -- OS_Time_To_GNAT_Time --
2205 --------------------------
2207 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type is
2208 GNAT_Time : Time_Stamp_Type;
2210 type Underlying_OS_Time is
2211 range -(2 ** 63) .. +(2 ** 63 - 1);
2212 -- Underlying_OS_Time is a redeclaration of OS_Time to allow integer
2213 -- manipulation. Remove this in favor of To_Ada/To_C once newer
2214 -- GNAT releases are available with these functions.
2216 function To_Int is
2217 new Ada.Unchecked_Conversion (OS_Time, Underlying_OS_Time);
2218 function From_Int is
2219 new Ada.Unchecked_Conversion (Underlying_OS_Time, OS_Time);
2221 TI : Underlying_OS_Time := To_Int (T);
2222 Y : Year_Type;
2223 Mo : Month_Type;
2224 D : Day_Type;
2225 H : Hour_Type;
2226 Mn : Minute_Type;
2227 S : Second_Type;
2229 begin
2230 if T = Invalid_Time then
2231 return Empty_Time_Stamp;
2232 end if;
2234 if On_Windows and then TI mod 2 > 0 then
2235 -- Windows ALI files had timestamps rounded to even seconds
2236 -- historically. The rounding was originally done in GM_Split.
2237 -- Now that GM_Split no longer does it, we are rounding it here
2238 -- only for ALI files.
2240 TI := TI + 1;
2241 end if;
2243 GM_Split (From_Int (TI), Y, Mo, D, H, Mn, S);
2245 Make_Time_Stamp
2246 (Year => Nat (Y),
2247 Month => Nat (Mo),
2248 Day => Nat (D),
2249 Hour => Nat (H),
2250 Minutes => Nat (Mn),
2251 Seconds => Nat (S),
2252 TS => GNAT_Time);
2254 return GNAT_Time;
2255 end OS_Time_To_GNAT_Time;
2257 -----------------
2258 -- Prep_Suffix --
2259 -----------------
2261 function Prep_Suffix return String is
2262 begin
2263 return ".prep";
2264 end Prep_Suffix;
2266 ------------------
2267 -- Program_Name --
2268 ------------------
2270 function Program_Name (Nam : String; Prog : String) return String_Access is
2271 End_Of_Prefix : Natural := 0;
2272 Start_Of_Prefix : Positive := 1;
2273 Start_Of_Suffix : Positive;
2275 begin
2276 -- Get the name of the current program being executed
2278 Find_Program_Name;
2280 -- Find the target prefix if any, for the cross compilation case.
2281 -- For instance in "powerpc-elf-gcc" the target prefix is
2282 -- "powerpc-elf-"
2283 -- Ditto for suffix, e.g. in "gcc-4.1", the suffix is "-4.1"
2285 for J in reverse 1 .. Name_Len loop
2286 if Is_Directory_Separator (Name_Buffer (J))
2287 or else Name_Buffer (J) = ':'
2288 then
2289 Start_Of_Prefix := J + 1;
2290 exit;
2291 end if;
2292 end loop;
2294 -- Find End_Of_Prefix
2296 for J in Start_Of_Prefix .. Name_Len - Prog'Length + 1 loop
2297 if Name_Buffer (J .. J + Prog'Length - 1) = Prog then
2298 End_Of_Prefix := J - 1;
2299 exit;
2300 end if;
2301 end loop;
2303 Start_Of_Suffix := End_Of_Prefix + Prog'Length + 1;
2305 -- Create the new program name
2307 return new String'
2308 (Name_Buffer (Start_Of_Prefix .. End_Of_Prefix)
2309 & Nam
2310 & Name_Buffer (Start_Of_Suffix .. Name_Len));
2311 end Program_Name;
2313 ------------------------------
2314 -- Read_Default_Search_Dirs --
2315 ------------------------------
2317 function Read_Default_Search_Dirs
2318 (Search_Dir_Prefix : String_Access;
2319 Search_File : String_Access;
2320 Search_Dir_Default_Name : String_Access) return String_Access
2322 Prefix_Len : constant Integer := Search_Dir_Prefix.all'Length;
2323 Buffer : String (1 .. Prefix_Len + Search_File.all'Length + 1);
2324 File_FD : File_Descriptor;
2325 S, S1 : String_Access;
2326 Len : Integer;
2327 Curr : Integer;
2328 Actual_Len : Integer;
2329 J1 : Integer;
2331 Prev_Was_Separator : Boolean;
2332 Nb_Relative_Dir : Integer;
2334 function Is_Relative (S : String; K : Positive) return Boolean;
2335 pragma Inline (Is_Relative);
2336 -- Returns True if a relative directory specification is found
2337 -- in S at position K, False otherwise.
2339 -----------------
2340 -- Is_Relative --
2341 -----------------
2343 function Is_Relative (S : String; K : Positive) return Boolean is
2344 begin
2345 return not Is_Absolute_Path (S (K .. S'Last));
2346 end Is_Relative;
2348 -- Start of processing for Read_Default_Search_Dirs
2350 begin
2351 -- Construct a C compatible character string buffer
2353 Buffer (1 .. Search_Dir_Prefix.all'Length)
2354 := Search_Dir_Prefix.all;
2355 Buffer (Search_Dir_Prefix.all'Length + 1 .. Buffer'Last - 1)
2356 := Search_File.all;
2357 Buffer (Buffer'Last) := ASCII.NUL;
2359 File_FD := Open_Read (Buffer'Address, Binary);
2360 if File_FD = Invalid_FD then
2361 return Search_Dir_Default_Name;
2362 end if;
2364 Len := Integer (File_Length (File_FD));
2366 -- An extra character for a trailing Path_Separator is allocated
2368 S := new String (1 .. Len + 1);
2369 S (Len + 1) := Path_Separator;
2371 -- Read the file. Note that the loop is probably not necessary since the
2372 -- whole file is read at once but the loop is harmless and that way we
2373 -- are sure to accommodate systems where this is not the case.
2375 Curr := 1;
2376 Actual_Len := Len;
2377 while Actual_Len /= 0 loop
2378 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
2379 Curr := Curr + Actual_Len;
2380 end loop;
2382 -- Process the file, dealing with path separators
2384 Prev_Was_Separator := True;
2385 Nb_Relative_Dir := 0;
2386 for J in 1 .. Len loop
2388 -- Treat any EOL character as a path separator. Note that we do
2389 -- not treat space as a path separator (we used to treat space as a
2390 -- path separator in an earlier version). That way space can appear
2391 -- as a legitimate character in a path name.
2393 if S (J) = ASCII.LF or else S (J) = ASCII.CR then
2394 S (J) := Path_Separator;
2395 end if;
2397 -- Test for explicit path separator (or control char as above)
2399 if S (J) = Path_Separator then
2400 Prev_Was_Separator := True;
2402 -- If not path separator, register use of relative directory
2404 else
2405 if Prev_Was_Separator and then Is_Relative (S.all, J) then
2406 Nb_Relative_Dir := Nb_Relative_Dir + 1;
2407 end if;
2409 Prev_Was_Separator := False;
2410 end if;
2411 end loop;
2413 if Nb_Relative_Dir = 0 then
2414 return S;
2415 end if;
2417 -- Add the Search_Dir_Prefix to all relative paths
2419 S1 := new String (1 .. S'Length + Nb_Relative_Dir * Prefix_Len);
2420 J1 := 1;
2421 Prev_Was_Separator := True;
2422 for J in 1 .. Len + 1 loop
2423 if S (J) = Path_Separator then
2424 Prev_Was_Separator := True;
2426 else
2427 if Prev_Was_Separator and then Is_Relative (S.all, J) then
2428 S1 (J1 .. J1 + Prefix_Len - 1) := Search_Dir_Prefix.all;
2429 J1 := J1 + Prefix_Len;
2430 end if;
2432 Prev_Was_Separator := False;
2433 end if;
2434 S1 (J1) := S (J);
2435 J1 := J1 + 1;
2436 end loop;
2438 Free (S);
2439 return S1;
2440 end Read_Default_Search_Dirs;
2442 -----------------------
2443 -- Read_Library_Info --
2444 -----------------------
2446 function Read_Library_Info
2447 (Lib_File : File_Name_Type;
2448 Fatal_Err : Boolean := False) return Text_Buffer_Ptr
2450 File : File_Name_Type;
2451 Attr : aliased File_Attributes;
2452 begin
2453 Find_File (Lib_File, Library, File, Attr'Access);
2454 return Read_Library_Info_From_Full
2455 (Full_Lib_File => File,
2456 Lib_File_Attr => Attr'Access,
2457 Fatal_Err => Fatal_Err);
2458 end Read_Library_Info;
2460 ---------------------------------
2461 -- Read_Library_Info_From_Full --
2462 ---------------------------------
2464 function Read_Library_Info_From_Full
2465 (Full_Lib_File : File_Name_Type;
2466 Lib_File_Attr : access File_Attributes;
2467 Fatal_Err : Boolean := False) return Text_Buffer_Ptr
2469 Lib_FD : File_Descriptor;
2470 -- The file descriptor for the current library file. A negative value
2471 -- indicates failure to open the specified source file.
2473 Len : Integer;
2474 -- Length of source file text (ALI). If it doesn't fit in an integer
2475 -- we're probably stuck anyway (>2 gigs of source seems a lot, and
2476 -- there are other places in the compiler that make this assumption).
2478 Text : Text_Buffer_Ptr;
2479 -- Allocated text buffer
2481 Status : Boolean;
2482 pragma Warnings (Off, Status);
2483 -- For the calls to Close
2485 begin
2486 Current_Full_Lib_Name := Full_Lib_File;
2487 Current_Full_Obj_Name := Object_File_Name (Current_Full_Lib_Name);
2489 if Current_Full_Lib_Name = No_File then
2490 if Fatal_Err then
2491 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2492 else
2493 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2494 return null;
2495 end if;
2496 end if;
2498 Get_Name_String (Current_Full_Lib_Name);
2499 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2501 -- Open the library FD, note that we open in binary mode, because as
2502 -- documented in the spec, the caller is expected to handle either
2503 -- DOS or Unix mode files, and there is no point in wasting time on
2504 -- text translation when it is not required.
2506 Lib_FD := Open_Read (Name_Buffer'Address, Binary);
2508 if Lib_FD = Invalid_FD then
2509 if Fatal_Err then
2510 Fail ("Cannot open: " & Name_Buffer (1 .. Name_Len));
2511 else
2512 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2513 return null;
2514 end if;
2515 end if;
2517 -- Compute the length of the file (potentially also preparing other data
2518 -- like the timestamp and whether the file is read-only, for future use)
2520 Len := Integer (File_Length (Name_Buffer'Address, Lib_File_Attr));
2522 -- Check for object file consistency if requested
2524 if Opt.Check_Object_Consistency then
2525 -- On most systems, this does not result in an extra system call
2527 Current_Full_Lib_Stamp :=
2528 OS_Time_To_GNAT_Time
2529 (File_Time_Stamp (Name_Buffer'Address, Lib_File_Attr));
2531 -- ??? One system call here
2533 Current_Full_Obj_Stamp := File_Stamp (Current_Full_Obj_Name);
2535 if Current_Full_Obj_Stamp (1) = ' ' then
2537 -- When the library is readonly always assume object is consistent
2538 -- The call to Is_Writable_File only results in a system call on
2539 -- some systems, but in most cases it has already been computed as
2540 -- part of the call to File_Length above.
2542 Get_Name_String (Current_Full_Lib_Name);
2543 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2545 if not Is_Writable_File (Name_Buffer'Address, Lib_File_Attr) then
2546 Current_Full_Obj_Stamp := Current_Full_Lib_Stamp;
2548 elsif Fatal_Err then
2549 Get_Name_String (Current_Full_Obj_Name);
2550 Close (Lib_FD, Status);
2552 -- No need to check the status, we fail anyway
2554 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2556 else
2557 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2558 Close (Lib_FD, Status);
2560 -- No need to check the status, we return null anyway
2562 return null;
2563 end if;
2565 elsif Current_Full_Obj_Stamp < Current_Full_Lib_Stamp then
2566 Close (Lib_FD, Status);
2568 -- No need to check the status, we return null anyway
2570 return null;
2571 end if;
2572 end if;
2574 -- Read data from the file
2576 declare
2577 Actual_Len : Integer;
2579 Lo : constant Text_Ptr := 0;
2580 -- Low bound for allocated text buffer
2582 Hi : Text_Ptr := Text_Ptr (Len);
2583 -- High bound for allocated text buffer. Note length is Len + 1
2584 -- which allows for extra EOF character at the end of the buffer.
2586 begin
2587 -- Allocate text buffer. Note extra character at end for EOF
2589 Text := new Text_Buffer (Lo .. Hi);
2591 -- Some systems have file types that require one read per line,
2592 -- so read until we get the Len bytes or until there are no more
2593 -- characters.
2595 Hi := Lo;
2596 loop
2597 Actual_Len := Read (Lib_FD, Text (Hi)'Address, Len);
2598 Hi := Hi + Text_Ptr (Actual_Len);
2599 exit when Actual_Len = Len or else Actual_Len <= 0;
2600 end loop;
2602 Text (Hi) := EOF;
2603 end;
2605 -- Read is complete, close file and we are done
2607 Close (Lib_FD, Status);
2608 -- The status should never be False. But, if it is, what can we do?
2609 -- So, we don't test it.
2611 return Text;
2613 end Read_Library_Info_From_Full;
2615 ----------------------
2616 -- Read_Source_File --
2617 ----------------------
2619 procedure Read_Source_File
2620 (N : File_Name_Type;
2621 Lo : Source_Ptr;
2622 Hi : out Source_Ptr;
2623 Src : out Source_Buffer_Ptr;
2624 FD : out File_Descriptor;
2625 T : File_Type := Source)
2627 Len : Integer;
2628 -- Length of file, assume no more than 2 gigabytes of source
2630 Actual_Len : Integer;
2632 Status : Boolean;
2633 pragma Warnings (Off, Status);
2634 -- For the call to Close
2636 begin
2637 Current_Full_Source_Name := Find_File (N, T, Full_Name => True);
2638 Current_Full_Source_Stamp := File_Stamp (Current_Full_Source_Name);
2640 if Current_Full_Source_Name = No_File then
2642 -- If we were trying to access the main file and we could not find
2643 -- it, we have an error.
2645 if N = Current_Main then
2646 Get_Name_String (N);
2647 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2648 end if;
2650 FD := Null_FD;
2651 Src := null;
2652 Hi := No_Location;
2653 return;
2654 end if;
2656 Get_Name_String (Current_Full_Source_Name);
2657 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2659 -- Open the source FD, note that we open in binary mode, because as
2660 -- documented in the spec, the caller is expected to handle either
2661 -- DOS or Unix mode files, and there is no point in wasting time on
2662 -- text translation when it is not required.
2664 FD := Open_Read (Name_Buffer'Address, Binary);
2666 if FD = Invalid_FD then
2667 Src := null;
2668 Hi := No_Location;
2669 return;
2670 end if;
2672 -- If it's a Source file, print out the file name, if requested, and if
2673 -- it's not part of the runtimes, store it in File_Name_Chars. We don't
2674 -- want to print non-Source files, like GNAT-TEMP-000001.TMP used to
2675 -- pass information from gprbuild to gcc. We don't want to save runtime
2676 -- file names, because we don't want users to send them in bug reports.
2678 if T = Source then
2679 declare
2680 Name : String renames Name_Buffer (1 .. Name_Len);
2681 Inc : String renames Include_Dir_Default_Prefix.all;
2683 Part_Of_Runtimes : constant Boolean :=
2684 Inc /= ""
2685 and then Inc'Length < Name_Len
2686 and then Name_Buffer (1 .. Inc'Length) = Inc;
2688 begin
2689 if Debug.Debug_Flag_Dot_N then
2690 Write_Line (Name);
2691 end if;
2693 if not Part_Of_Runtimes then
2694 File_Name_Chars.Append_All (File_Name_Chars.Table_Type (Name));
2695 File_Name_Chars.Append (ASCII.LF);
2696 end if;
2697 end;
2698 end if;
2700 -- Prepare to read data from the file
2702 Len := Integer (File_Length (FD));
2704 -- Set Hi so that length is one more than the physical length,
2705 -- allowing for the extra EOF character at the end of the buffer
2707 Hi := Lo + Source_Ptr (Len);
2709 -- Do the actual read operation
2711 declare
2712 Var_Ptr : constant Source_Buffer_Ptr_Var :=
2713 new Source_Buffer (Lo .. Hi);
2714 -- Allocate source buffer, allowing extra character at end for EOF
2715 begin
2716 -- Some systems have file types that require one read per line,
2717 -- so read until we get the Len bytes or until there are no more
2718 -- characters.
2720 Hi := Lo;
2721 loop
2722 Actual_Len := Read (FD, Var_Ptr (Hi)'Address, Len);
2723 Hi := Hi + Source_Ptr (Actual_Len);
2724 exit when Actual_Len = Len or else Actual_Len <= 0;
2725 end loop;
2727 Var_Ptr (Hi) := EOF;
2728 Src := Var_Ptr.all'Access;
2729 end;
2731 -- Read is complete, get time stamp and close file and we are done
2733 Close (FD, Status);
2735 -- The status should never be False. But, if it is, what can we do?
2736 -- So, we don't test it.
2738 -- ???We don't really need to return Hi anymore; We could get rid of
2739 -- it. We could also make this into a function.
2741 pragma Assert (Hi = Src'Last);
2742 end Read_Source_File;
2744 -------------------
2745 -- Relocate_Path --
2746 -------------------
2748 function Relocate_Path
2749 (Prefix : String;
2750 Path : String) return String_Ptr
2752 S : String_Ptr;
2754 procedure set_std_prefix (S : String; Len : Integer);
2755 pragma Import (C, set_std_prefix);
2757 begin
2758 if Std_Prefix = null then
2759 Std_Prefix := String_Ptr (Getenv ("GNSA_ROOT"));
2761 if Std_Prefix.all = "" then
2762 Std_Prefix := Executable_Prefix;
2764 elsif not Is_Directory_Separator (Std_Prefix (Std_Prefix'Last)) then
2766 -- The remainder of this function assumes that Std_Prefix
2767 -- terminates with a dir separator, so we force this here.
2769 declare
2770 Old_Prefix : String_Ptr := Std_Prefix;
2771 begin
2772 Std_Prefix := new String (1 .. Old_Prefix'Length + 1);
2773 Std_Prefix (1 .. Old_Prefix'Length) := Old_Prefix.all;
2774 Std_Prefix (Old_Prefix'Length + 1) := Directory_Separator;
2775 Free (Old_Prefix);
2776 end;
2777 end if;
2779 if Std_Prefix.all /= "" then
2781 -- Remove trailing directory separator when calling set_std_prefix
2783 set_std_prefix (Std_Prefix.all, Std_Prefix'Length - 1);
2784 end if;
2785 end if;
2787 if Path'Last >= Prefix'Last and then Path (Prefix'Range) = Prefix then
2788 if Std_Prefix.all /= "" then
2789 S := new String
2790 (1 .. Std_Prefix'Length + Path'Last - Prefix'Last);
2791 S (1 .. Std_Prefix'Length) := Std_Prefix.all;
2792 S (Std_Prefix'Length + 1 .. S'Last) :=
2793 Path (Prefix'Last + 1 .. Path'Last);
2794 return S;
2795 end if;
2796 end if;
2798 return new String'(Path);
2799 end Relocate_Path;
2801 -----------------
2802 -- Set_Program --
2803 -----------------
2805 procedure Set_Program (P : Program_Type) is
2806 begin
2807 if Program_Set then
2808 Fail ("Set_Program called twice");
2809 end if;
2811 Program_Set := True;
2812 Running_Program := P;
2813 end Set_Program;
2815 ----------------
2816 -- Shared_Lib --
2817 ----------------
2819 function Shared_Lib (Name : String) return String is
2820 Library : String (1 .. Name'Length + Library_Version'Length + 3);
2821 -- 3 = 2 for "-l" + 1 for "-" before lib version
2823 begin
2824 Library (1 .. 2) := "-l";
2825 Library (3 .. 2 + Name'Length) := Name;
2826 Library (3 + Name'Length) := '-';
2827 Library (4 + Name'Length .. Library'Last) := Library_Version;
2828 return Library;
2829 end Shared_Lib;
2831 ----------------------
2832 -- Smart_File_Stamp --
2833 ----------------------
2835 function Smart_File_Stamp
2836 (N : File_Name_Type;
2837 T : File_Type) return Time_Stamp_Type
2839 File : File_Name_Type;
2840 Attr : aliased File_Attributes;
2842 begin
2843 if not File_Cache_Enabled then
2844 Find_File (N, T, File, Attr'Access);
2845 else
2846 Smart_Find_File (N, T, File, Attr);
2847 end if;
2849 if File = No_File then
2850 return Empty_Time_Stamp;
2851 else
2852 Get_Name_String (File);
2853 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2854 return
2855 OS_Time_To_GNAT_Time
2856 (File_Time_Stamp (Name_Buffer'Address, Attr'Access));
2857 end if;
2858 end Smart_File_Stamp;
2860 ---------------------
2861 -- Smart_Find_File --
2862 ---------------------
2864 function Smart_Find_File
2865 (N : File_Name_Type;
2866 T : File_Type) return File_Name_Type
2868 File : File_Name_Type;
2869 Attr : File_Attributes;
2870 begin
2871 Smart_Find_File (N, T, File, Attr);
2872 return File;
2873 end Smart_Find_File;
2875 ---------------------
2876 -- Smart_Find_File --
2877 ---------------------
2879 procedure Smart_Find_File
2880 (N : File_Name_Type;
2881 T : File_Type;
2882 Found : out File_Name_Type;
2883 Attr : out File_Attributes)
2885 Info : File_Info_Cache;
2887 begin
2888 if not File_Cache_Enabled then
2889 Find_File (N, T, Info.File, Info.Attr'Access);
2891 else
2892 Info := File_Name_Hash_Table.Get (N);
2894 if Info.File = No_File then
2895 Find_File (N, T, Info.File, Info.Attr'Access);
2896 File_Name_Hash_Table.Set (N, Info);
2897 end if;
2898 end if;
2900 Found := Info.File;
2901 Attr := Info.Attr;
2902 end Smart_Find_File;
2904 ----------------------
2905 -- Source_File_Data --
2906 ----------------------
2908 procedure Source_File_Data (Cache : Boolean) is
2909 begin
2910 File_Cache_Enabled := Cache;
2911 end Source_File_Data;
2913 -----------------------
2914 -- Source_File_Stamp --
2915 -----------------------
2917 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
2918 begin
2919 return Smart_File_Stamp (N, Source);
2920 end Source_File_Stamp;
2922 ---------------------
2923 -- Strip_Directory --
2924 ---------------------
2926 function Strip_Directory (Name : File_Name_Type) return File_Name_Type is
2927 begin
2928 Get_Name_String (Name);
2930 for J in reverse 1 .. Name_Len - 1 loop
2932 -- If we find the last directory separator
2934 if Is_Directory_Separator (Name_Buffer (J)) then
2936 -- Return part of Name that follows this last directory separator
2938 Name_Buffer (1 .. Name_Len - J) := Name_Buffer (J + 1 .. Name_Len);
2939 Name_Len := Name_Len - J;
2940 return Name_Find;
2941 end if;
2942 end loop;
2944 -- There were no directory separator, just return Name
2946 return Name;
2947 end Strip_Directory;
2949 ------------------
2950 -- Strip_Suffix --
2951 ------------------
2953 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type is
2954 begin
2955 Get_Name_String (Name);
2957 for J in reverse 2 .. Name_Len loop
2959 -- If we found the last '.', return part of Name that precedes it
2961 if Name_Buffer (J) = '.' then
2962 Name_Len := J - 1;
2963 return Name_Enter;
2964 end if;
2965 end loop;
2967 return Name;
2968 end Strip_Suffix;
2970 ---------------------------
2971 -- To_Canonical_File_List --
2972 ---------------------------
2974 function To_Canonical_File_List
2975 (Wildcard_Host_File : String;
2976 Only_Dirs : Boolean) return String_Access_List_Access
2978 function To_Canonical_File_List_Init
2979 (Host_File : Address;
2980 Only_Dirs : Integer) return Integer;
2981 pragma Import (C, To_Canonical_File_List_Init,
2982 "__gnat_to_canonical_file_list_init");
2984 function To_Canonical_File_List_Next return Address;
2985 pragma Import (C, To_Canonical_File_List_Next,
2986 "__gnat_to_canonical_file_list_next");
2988 procedure To_Canonical_File_List_Free;
2989 pragma Import (C, To_Canonical_File_List_Free,
2990 "__gnat_to_canonical_file_list_free");
2992 Num_Files : Integer;
2993 C_Wildcard_Host_File : String (1 .. Wildcard_Host_File'Length + 1);
2995 begin
2996 C_Wildcard_Host_File (1 .. Wildcard_Host_File'Length) :=
2997 Wildcard_Host_File;
2998 C_Wildcard_Host_File (C_Wildcard_Host_File'Last) := ASCII.NUL;
3000 -- Do the expansion and say how many there are
3002 Num_Files := To_Canonical_File_List_Init
3003 (C_Wildcard_Host_File'Address, Boolean'Pos (Only_Dirs));
3005 declare
3006 Canonical_File_List : String_Access_List (1 .. Num_Files);
3007 Canonical_File_Addr : Address;
3008 Canonical_File_Len : CRTL.size_t;
3010 begin
3011 -- Retrieve the expanded directory names and build the list
3013 for J in 1 .. Num_Files loop
3014 Canonical_File_Addr := To_Canonical_File_List_Next;
3015 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
3016 Canonical_File_List (J) := To_Path_String_Access
3017 (Canonical_File_Addr, Canonical_File_Len);
3018 end loop;
3020 -- Free up the storage
3022 To_Canonical_File_List_Free;
3024 return new String_Access_List'(Canonical_File_List);
3025 end;
3026 end To_Canonical_File_List;
3028 ----------------------
3029 -- To_Host_Dir_Spec --
3030 ----------------------
3032 function To_Host_Dir_Spec
3033 (Canonical_Dir : String;
3034 Prefix_Style : Boolean) return String_Access
3036 function To_Host_Dir_Spec
3037 (Canonical_Dir : Address;
3038 Prefix_Flag : Integer) return Address;
3039 pragma Import (C, To_Host_Dir_Spec, "__gnat_to_host_dir_spec");
3041 C_Canonical_Dir : String (1 .. Canonical_Dir'Length + 1);
3042 Host_Dir_Addr : Address;
3043 Host_Dir_Len : CRTL.size_t;
3045 begin
3046 C_Canonical_Dir (1 .. Canonical_Dir'Length) := Canonical_Dir;
3047 C_Canonical_Dir (C_Canonical_Dir'Last) := ASCII.NUL;
3049 if Prefix_Style then
3050 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 1);
3051 else
3052 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 0);
3053 end if;
3054 Host_Dir_Len := C_String_Length (Host_Dir_Addr);
3056 if Host_Dir_Len = 0 then
3057 return null;
3058 else
3059 return To_Path_String_Access (Host_Dir_Addr, Host_Dir_Len);
3060 end if;
3061 end To_Host_Dir_Spec;
3063 -----------------------
3064 -- To_Host_File_Spec --
3065 -----------------------
3067 function To_Host_File_Spec
3068 (Canonical_File : String) return String_Access
3070 function To_Host_File_Spec (Canonical_File : Address) return Address;
3071 pragma Import (C, To_Host_File_Spec, "__gnat_to_host_file_spec");
3073 C_Canonical_File : String (1 .. Canonical_File'Length + 1);
3074 Host_File_Addr : Address;
3075 Host_File_Len : CRTL.size_t;
3077 begin
3078 C_Canonical_File (1 .. Canonical_File'Length) := Canonical_File;
3079 C_Canonical_File (C_Canonical_File'Last) := ASCII.NUL;
3081 Host_File_Addr := To_Host_File_Spec (C_Canonical_File'Address);
3082 Host_File_Len := C_String_Length (Host_File_Addr);
3084 if Host_File_Len = 0 then
3085 return null;
3086 else
3087 return To_Path_String_Access
3088 (Host_File_Addr, Host_File_Len);
3089 end if;
3090 end To_Host_File_Spec;
3092 ---------------------------
3093 -- To_Path_String_Access --
3094 ---------------------------
3096 function To_Path_String_Access
3097 (Path_Addr : Address;
3098 Path_Len : CRTL.size_t) return String_Access
3100 subtype Path_String is String (1 .. Integer (Path_Len));
3101 type Path_String_Access is access Path_String;
3103 function Address_To_Access is new
3104 Ada.Unchecked_Conversion (Source => Address,
3105 Target => Path_String_Access);
3107 Path_Access : constant Path_String_Access :=
3108 Address_To_Access (Path_Addr);
3110 Return_Val : String_Access;
3112 begin
3113 Return_Val := new String (1 .. Integer (Path_Len));
3115 for J in 1 .. Integer (Path_Len) loop
3116 Return_Val (J) := Path_Access (J);
3117 end loop;
3119 return Return_Val;
3120 end To_Path_String_Access;
3122 -----------------
3123 -- Update_Path --
3124 -----------------
3126 function Update_Path (Path : String_Ptr) return String_Ptr is
3128 function C_Update_Path (Path, Component : Address) return Address;
3129 pragma Import (C, C_Update_Path, "update_path");
3131 In_Length : constant Integer := Path'Length;
3132 In_String : String (1 .. In_Length + 1);
3133 Component_Name : aliased String := "GCC" & ASCII.NUL;
3134 Result_Ptr : Address;
3135 Result_Length : CRTL.size_t;
3136 Out_String : String_Ptr;
3138 begin
3139 In_String (1 .. In_Length) := Path.all;
3140 In_String (In_Length + 1) := ASCII.NUL;
3141 Result_Ptr := C_Update_Path (In_String'Address, Component_Name'Address);
3142 Result_Length := CRTL.strlen (Result_Ptr);
3144 Out_String := new String (1 .. Integer (Result_Length));
3145 CRTL.strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
3146 return Out_String;
3147 end Update_Path;
3149 ----------------
3150 -- Write_Info --
3151 ----------------
3153 procedure Write_Info (Info : String) is
3154 begin
3155 Write_With_Check (Info'Address, Info'Length);
3156 Write_With_Check (EOL'Address, 1);
3157 end Write_Info;
3159 ------------------------
3160 -- Write_Program_Name --
3161 ------------------------
3163 procedure Write_Program_Name is
3164 Save_Buffer : constant String (1 .. Name_Len) :=
3165 Name_Buffer (1 .. Name_Len);
3167 begin
3168 Find_Program_Name;
3170 -- Convert the name to lower case so error messages are the same on
3171 -- all systems.
3173 for J in 1 .. Name_Len loop
3174 if Name_Buffer (J) in 'A' .. 'Z' then
3175 Name_Buffer (J) :=
3176 Character'Val (Character'Pos (Name_Buffer (J)) + 32);
3177 end if;
3178 end loop;
3180 Write_Str (Name_Buffer (1 .. Name_Len));
3182 -- Restore Name_Buffer which was clobbered by the call to
3183 -- Find_Program_Name
3185 Name_Len := Save_Buffer'Last;
3186 Name_Buffer (1 .. Name_Len) := Save_Buffer;
3187 end Write_Program_Name;
3189 ----------------------
3190 -- Write_With_Check --
3191 ----------------------
3193 procedure Write_With_Check (A : Address; N : Integer) is
3194 Ignore : Boolean;
3195 begin
3196 if N = Write (Output_FD, A, N) then
3197 return;
3198 else
3199 Write_Str ("error: disk full writing ");
3200 Write_Name_Decoded (Output_File_Name);
3201 Write_Eol;
3202 Name_Len := Name_Len + 1;
3203 Name_Buffer (Name_Len) := ASCII.NUL;
3204 Delete_File (Name_Buffer'Address, Ignore);
3205 Exit_Program (E_Fatal);
3206 end if;
3207 end Write_With_Check;
3209 ----------------------------
3210 -- Package Initialization --
3211 ----------------------------
3213 procedure Reset_File_Attributes (Attr : System.Address);
3214 pragma Import (C, Reset_File_Attributes, "__gnat_reset_attributes");
3216 begin
3217 Initialization : declare
3219 function Get_Default_Identifier_Character_Set return Character;
3220 pragma Import (C, Get_Default_Identifier_Character_Set,
3221 "__gnat_get_default_identifier_character_set");
3222 -- Function to determine the default identifier character set,
3223 -- which is system dependent. See Opt package spec for a list of
3224 -- the possible character codes and their interpretations.
3226 function Get_Maximum_File_Name_Length return Int;
3227 pragma Import (C, Get_Maximum_File_Name_Length,
3228 "__gnat_get_maximum_file_name_length");
3229 -- Function to get maximum file name length for system
3231 Sizeof_File_Attributes : Integer;
3232 pragma Import (C, Sizeof_File_Attributes,
3233 "__gnat_size_of_file_attributes");
3235 begin
3236 pragma Assert (Sizeof_File_Attributes <= File_Attributes_Size);
3238 Reset_File_Attributes (Unknown_Attributes'Address);
3240 Identifier_Character_Set := Get_Default_Identifier_Character_Set;
3241 Maximum_File_Name_Length := Get_Maximum_File_Name_Length;
3243 -- Following should be removed by having above function return
3244 -- Integer'Last as indication of no maximum instead of -1 ???
3246 if Maximum_File_Name_Length = -1 then
3247 Maximum_File_Name_Length := Int'Last;
3248 end if;
3250 Src_Search_Directories.Set_Last (Primary_Directory);
3251 Src_Search_Directories.Table (Primary_Directory) := new String'("");
3253 Lib_Search_Directories.Set_Last (Primary_Directory);
3254 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
3256 Osint.Initialize;
3257 end Initialization;
3259 end Osint;