Debian Codegraph @

gcc/ada/osint.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- O S I N T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 ------------------------------------------------------------------------------
25
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;
36
37 with Ada.Unchecked_Conversion;
38
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);
44
45 with GNAT.HTable;
46
47 package body Osint is
48
49 use type CRTL.size_t;
50
51 Running_Program : Program_Type := Unspecified;
52 -- Set by Set_Program to indicate which of Compiler, Binder, etc is
53 -- running.
54
55 Program_Set : Boolean := False;
56 -- True if Set_Program has been called; used to detect duplicate calls.
57
58 Std_Prefix : String_Ptr;
59 -- Standard prefix, computed dynamically the first time Relocate_Path
60 -- is called, and cached for subsequent calls.
61
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.
66
67 -------------------------------------
68 -- Use of Name_Find and Name_Enter --
69 -------------------------------------
70
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.
79
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
83
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
88
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.
96
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.
103
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.
107
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.
111
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.
124 --
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.
128 --
129 -- Attr is always reset in this call to Unknown_Attributes, even in case of
130 -- failure
131
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
140
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
148
149 function C_String_Length (S : Address) return CRTL.size_t;
150 -- Returns length of a C string (zero for a null address)
151
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.
158
159 function Include_Dir_Default_Prefix return String_Access;
160 -- Same as exported version, except returns a String_Access
161
162 ------------------------------
163 -- Other Local Declarations --
164 ------------------------------
165
166 EOL : constant Character := ASCII.LF;
167 -- End of line character
168
169 Number_File_Names : Nat := 0;
170 -- Number of file names found on command line and placed in File_Names
171
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.
177
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.
187
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
196
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.
201
202 ------------------
203 -- Search Paths --
204 ------------------
205
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).
211
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.
222
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.
234
235 ---------------------
236 -- File Hash Table --
237 ---------------------
238
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.
248
249 File_Cache_Enabled : Boolean := False;
250 -- Set to true if you want the enable the file data caching mechanism
251
252 type File_Hash_Num is range 0 .. 1020;
253
254 function File_Hash (F : File_Name_Type) return File_Hash_Num;
255 -- Compute hash index for use by Simple_HTable
256
257 type File_Info_Cache is record
258 File : File_Name_Type;
259 Attr : aliased File_Attributes;
260 end record;
261
262 No_File_Info_Cache : constant File_Info_Cache := (No_File, (others => 0));
263
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 => "=");
271
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.
278
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.
286
287 -----------------------------
288 -- Add_Default_Search_Dirs --
289 -----------------------------
290
291 procedure Add_Default_Search_Dirs is
292 Search_Dir : String_Access;
293 Search_Path : String_Access;
294 Path_File_Name : String_Access;
295
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.
304
305 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean);
306 -- Open a path file and read the directory to search, one per line
307
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:
311 --
312 -- HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\
313 -- GNAT\Standard Libraries
314 -- Return an empty string on other systems.
315 --
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).
319
320 --------------------
321 -- Add_Search_Dir --
322 --------------------
323
324 procedure Add_Search_Dir
325 (Search_Dir : String;
326 Additional_Source_Dir : Boolean)
327 is
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;
335
336 procedure Add_Search_Dir
337 (Search_Dir : String_Access;
338 Additional_Source_Dir : Boolean)
339 is
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;
347
348 ------------------------
349 -- Get_Dirs_From_File --
350 ------------------------
351
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;
361
362 Status : Boolean;
363 pragma Warnings (Off, Status);
364 -- For the call to Close where status is ignored
365
366 begin
367 File_FD := Open_Read (Buffer'Address, Binary);
368
369 -- If we cannot open the file, we ignore it, we don't fail
370
371 if File_FD = Invalid_FD then
372 return;
373 end if;
374
375 Len := Integer (File_Length (File_FD));
376
377 S := new String (1 .. Len);
378
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.
382
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;
389
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).
392
393 Close (File_FD, Status);
394
395 -- Now, we read line by line
396
397 First := 1;
398 Curr := 0;
399 while Curr < Len loop
400 Ch := S (Curr + 1);
401
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;
408
409 First := Curr + 2;
410 end if;
411
412 Curr := Curr + 1;
413 end loop;
414
415 -- Last line is a special case, if the file does not end with
416 -- an end of line mark.
417
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;
422
423 ---------------------------------
424 -- Get_Libraries_From_Registry --
425 ---------------------------------
426
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");
431
432 Result_Ptr : Address;
433 Result_Length : CRTL.size_t;
434 Out_String : String_Ptr;
435
436 begin
437 Result_Ptr := C_Get_Libraries_From_Registry;
438 Result_Length := CRTL.strlen (Result_Ptr);
439
440 Out_String := new String (1 .. Integer (Result_Length));
441 CRTL.strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
442
443 CRTL.free (Result_Ptr);
444
445 return Out_String;
446 end Get_Libraries_From_Registry;
447
448 -- Start of processing for Add_Default_Search_Dirs
449
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.
453
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;
459
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.
464
465 -- Check for eventual project path file env vars
466
467 Path_File_Name := Getenv (Project_Include_Path_File);
468
469 if Path_File_Name'Length > 0 then
470 Get_Dirs_From_File (Additional_Source_Dir => True);
471 end if;
472
473 Path_File_Name := Getenv (Project_Objects_Path_File);
474
475 if Path_File_Name'Length > 0 then
476 Get_Dirs_From_File (Additional_Source_Dir => False);
477 end if;
478
479 -- Put path name in canonical form
480
481 for Additional_Source_Dir in False .. True loop
482 if Additional_Source_Dir then
483 Search_Path := Getenv (Ada_Include_Path);
484
485 else
486 Search_Path := Getenv (Ada_Objects_Path);
487
488 end if;
489
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;
497
498 -- For the compiler, if --RTS= was specified, add the runtime
499 -- directories.
500
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);
504
505 else
506 if not Opt.No_Stdinc then
507
508 -- For WIN32 systems, look for any system libraries defined in
509 -- the registry. These are added to both source and object
510 -- directories.
511
512 Search_Path := String_Access (Get_Libraries_From_Registry);
513
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;
521
522 -- The last place to look are the defaults
523
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)));
529
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;
537
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.
541
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)));
548
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;
558
559 --------------
560 -- Add_File --
561 --------------
562
563 procedure Add_File (File_Name : String; Index : Int := No_Index) is
564 begin
565 Number_File_Names := Number_File_Names + 1;
566
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.
569
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;
575
576 File_Names (Number_File_Names) := new String'(File_Name);
577 File_Indexes (Number_File_Names) := Index;
578 end Add_File;
579
580 ------------------------
581 -- Add_Lib_Search_Dir --
582 ------------------------
583
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;
589
590 declare
591 Norm : String_Ptr := Normalize_Directory_Name (Dir);
592
593 begin
594 -- Do nothing if the directory is already in the list. This saves
595 -- system calls and avoid unneeded work
596
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;
605
606 Lib_Search_Directories.Increment_Last;
607 Lib_Search_Directories.Table (Lib_Search_Directories.Last) := Norm;
608 end;
609 end Add_Lib_Search_Dir;
610
611 ---------------------
612 -- Add_Search_Dirs --
613 ---------------------
614
615 procedure Add_Search_Dirs
616 (Search_Path : String_Ptr;
617 Path_Type : Search_File_Type)
618 is
619 Current_Search_Path : String_Access;
620
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;
627
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;
635
636 ------------------------
637 -- Add_Src_Search_Dir --
638 ------------------------
639
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;
645
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;
650
651 --------------------------------
652 -- Append_Suffix_To_File_Name --
653 --------------------------------
654
655 function Append_Suffix_To_File_Name
656 (Name : File_Name_Type;
657 Suffix : String) return File_Name_Type
658 is
659 begin
660 Get_Name_String (Name);
661 Add_Str_To_Name_Buffer (Suffix);
662 return Name_Find;
663 end Append_Suffix_To_File_Name;
664
665 ---------------------
666 -- C_String_Length --
667 ---------------------
668
669 function C_String_Length (S : Address) return CRTL.size_t is
670 begin
671 if S = Null_Address then
672 return 0;
673 else
674 return CRTL.strlen (S);
675 end if;
676 end C_String_Length;
677
678 ------------------------------
679 -- Canonical_Case_File_Name --
680 ------------------------------
681
682 procedure Canonical_Case_File_Name (S : in out String) is
683 begin
684 if not File_Names_Case_Sensitive then
685 To_Lower (S);
686 end if;
687 end Canonical_Case_File_Name;
688
689 ---------------------------------
690 -- Canonical_Case_Env_Var_Name --
691 ---------------------------------
692
693 procedure Canonical_Case_Env_Var_Name (S : in out String) is
694 begin
695 if not Env_Vars_Case_Sensitive then
696 To_Lower (S);
697 end if;
698 end Canonical_Case_Env_Var_Name;
699
700 ---------------------------
701 -- Create_File_And_Check --
702 ---------------------------
703
704 procedure Create_File_And_Check
705 (Fdesc : out File_Descriptor;
706 Fmode : Mode)
707 is
708 begin
709 Output_File_Name := Name_Enter;
710 Fdesc := Create_File (Name_Buffer'Address, Fmode);
711
712 if Fdesc = Invalid_FD then
713 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
714 end if;
715 end Create_File_And_Check;
716
717 -----------------------------------
718 -- Open_File_To_Append_And_Check --
719 -----------------------------------
720
721 procedure Open_File_To_Append_And_Check
722 (Fdesc : out File_Descriptor;
723 Fmode : Mode)
724 is
725 begin
726 Output_File_Name := Name_Enter;
727 Fdesc := Open_Append (Name_Buffer'Address, Fmode);
728
729 if Fdesc = Invalid_FD then
730 Fail ("Cannot create: " & Name_Buffer (1 .. Name_Len));
731 end if;
732 end Open_File_To_Append_And_Check;
733
734 ------------------------
735 -- Current_File_Index --
736 ------------------------
737
738 function Current_File_Index return Int is
739 begin
740 return File_Indexes (Current_File_Name_Index);
741 end Current_File_Index;
742
743 --------------------------------
744 -- Current_Library_File_Stamp --
745 --------------------------------
746
747 function Current_Library_File_Stamp return Time_Stamp_Type is
748 begin
749 return Current_Full_Lib_Stamp;
750 end Current_Library_File_Stamp;
751
752 -------------------------------
753 -- Current_Object_File_Stamp --
754 -------------------------------
755
756 function Current_Object_File_Stamp return Time_Stamp_Type is
757 begin
758 return Current_Full_Obj_Stamp;
759 end Current_Object_File_Stamp;
760
761 -------------------------------
762 -- Current_Source_File_Stamp --
763 -------------------------------
764
765 function Current_Source_File_Stamp return Time_Stamp_Type is
766 begin
767 return Current_Full_Source_Stamp;
768 end Current_Source_File_Stamp;
769
770 ----------------------------
771 -- Dir_In_Obj_Search_Path --
772 ----------------------------
773
774 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr is
775 begin
776 if Opt.Look_In_Primary_Dir then
777 return
778 Lib_Search_Directories.Table (Primary_Directory + Position - 1);
779 else
780 return Lib_Search_Directories.Table (Primary_Directory + Position);
781 end if;
782 end Dir_In_Obj_Search_Path;
783
784 ----------------------------
785 -- Dir_In_Src_Search_Path --
786 ----------------------------
787
788 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr is
789 begin
790 if Opt.Look_In_Primary_Dir then
791 return
792 Src_Search_Directories.Table (Primary_Directory + Position - 1);
793 else
794 return Src_Search_Directories.Table (Primary_Directory + Position);
795 end if;
796 end Dir_In_Src_Search_Path;
797
798 -----------------------------------------
799 -- Dump_Command_Line_Source_File_Names --
800 -----------------------------------------
801
802 procedure Dump_Command_Line_Source_File_Names is
803 begin
804 for J in 1 .. Number_Of_Files loop
805 Write_Str (File_Names (J).all & " ");
806 end loop;
807 end Dump_Command_Line_Source_File_Names;
808
809 ----------------------------
810 -- Dump_Source_File_Names --
811 ----------------------------
812
813 procedure Dump_Source_File_Names is
814 subtype Rng is Int range File_Name_Chars.First .. File_Name_Chars.Last;
815 begin
816 Write_Str (String (File_Name_Chars.Table (Rng)));
817 end Dump_Source_File_Names;
818
819 ---------------------
820 -- Executable_Name --
821 ---------------------
822
823 function Executable_Name
824 (Name : File_Name_Type;
825 Only_If_No_Suffix : Boolean := False) return File_Name_Type
826 is
827 Exec_Suffix : String_Access;
828 Add_Suffix : Boolean;
829
830 begin
831 if Name = No_File then
832 return No_File;
833 end if;
834
835 if Executable_Extension_On_Target = No_Name then
836 Exec_Suffix := Get_Target_Executable_Suffix;
837 else
838 Get_Name_String (Executable_Extension_On_Target);
839 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
840 end if;
841
842 if Exec_Suffix'Length /= 0 then
843 Get_Name_String (Name);
844
845 Add_Suffix := True;
846 if Only_If_No_Suffix then
847 for J in reverse 1 .. Name_Len loop
848 if Name_Buffer (J) = '.' then
849 Add_Suffix := False;
850 exit;
851
852 elsif Is_Directory_Separator (Name_Buffer (J)) then
853 exit;
854 end if;
855 end loop;
856 end if;
857
858 if Add_Suffix then
859 -- If Executable doesn't end with the executable suffix, add it
860
861 if Name_Len <= Exec_Suffix'Length
862 or else not
863 File_Names_Equal
864 (Name_Buffer
865 (Name_Len - Exec_Suffix'Length + 1 .. Name_Len),
866 Exec_Suffix.all)
867 then
868 Name_Buffer
869 (Name_Len + 1 .. Name_Len + Exec_Suffix'Length) :=
870 Exec_Suffix.all;
871 Name_Len := Name_Len + Exec_Suffix'Length;
872 Free (Exec_Suffix);
873 return Name_Find;
874 end if;
875 end if;
876 end if;
877
878 Free (Exec_Suffix);
879 return Name;
880 end Executable_Name;
881
882 function Executable_Name
883 (Name : String;
884 Only_If_No_Suffix : Boolean := False) return String
885 is
886 Exec_Suffix : String_Access;
887 Add_Suffix : Boolean;
888
889 begin
890 if Executable_Extension_On_Target = No_Name then
891 Exec_Suffix := Get_Target_Executable_Suffix;
892 else
893 Get_Name_String (Executable_Extension_On_Target);
894 Exec_Suffix := new String'(Name_Buffer (1 .. Name_Len));
895 end if;
896
897 if Exec_Suffix'Length = 0 then
898 Free (Exec_Suffix);
899 return Name;
900
901 else
902 declare
903 Suffix : constant String := Exec_Suffix.all;
904
905 begin
906 Free (Exec_Suffix);
907 Add_Suffix := True;
908
909 if Only_If_No_Suffix then
910 for J in reverse Name'Range loop
911 if Name (J) = '.' then
912 Add_Suffix := False;
913 exit;
914
915 elsif Is_Directory_Separator (Name (J)) then
916 exit;
917 end if;
918 end loop;
919 end if;
920
921 if Add_Suffix and then
922 (Name'Length <= Suffix'Length
923 or else not
924 File_Names_Equal
925 (Name (Name'Last - Suffix'Length + 1 .. Name'Last),
926 Suffix))
927 then
928 declare
929 Result : String (1 .. Name'Length + Suffix'Length);
930 begin
931 Result (1 .. Name'Length) := Name;
932 Result (Name'Length + 1 .. Result'Last) := Suffix;
933 return Result;
934 end;
935 else
936 return Name;
937 end if;
938 end;
939 end if;
940 end Executable_Name;
941
942 -----------------------
943 -- Executable_Prefix --
944 -----------------------
945
946 function Executable_Prefix return String_Ptr is
947
948 function Get_Install_Dir (Exec : String) return String_Ptr;
949 -- S is the executable name preceded by the absolute or relative
950 -- path, e.g. "c:\usr\bin\gcc.exe" or "..\bin\gcc".
951
952 ---------------------
953 -- Get_Install_Dir --
954 ---------------------
955
956 function Get_Install_Dir (Exec : String) return String_Ptr is
957 Full_Path : constant String := Normalize_Pathname (Exec);
958 -- Use the full path, so that we find "lib" or "bin", even when
959 -- the tool has been invoked with a relative path, as in
960 -- "./gnatls -v" invoked in the GNAT bin directory.
961
962 begin
963 for J in reverse Full_Path'Range loop
964 if Is_Directory_Separator (Full_Path (J)) then
965 if J < Full_Path'Last - 5 then
966 if (To_Lower (Full_Path (J + 1)) = 'l'
967 and then To_Lower (Full_Path (J + 2)) = 'i'
968 and then To_Lower (Full_Path (J + 3)) = 'b')
969 or else
970 (To_Lower (Full_Path (J + 1)) = 'b'
971 and then To_Lower (Full_Path (J + 2)) = 'i'
972 and then To_Lower (Full_Path (J + 3)) = 'n')
973 then
974 return new String'(Full_Path (Full_Path'First .. J));
975 end if;
976 end if;
977 end if;
978 end loop;
979
980 return new String'("");
981 end Get_Install_Dir;
982
983 -- Start of processing for Executable_Prefix
984
985 begin
986 if Exec_Name = null then
987 Exec_Name := new String (1 .. Len_Arg (0));
988 Osint.Fill_Arg (Exec_Name (1)'Address, 0);
989 end if;
990
991 -- First determine if a path prefix was placed in front of the
992 -- executable name.
993
994 for J in reverse Exec_Name'Range loop
995 if Is_Directory_Separator (Exec_Name (J)) then
996 return Get_Install_Dir (Exec_Name.all);
997 end if;
998 end loop;
999
1000 -- If we come here, the user has typed the executable name with no
1001 -- directory prefix.
1002
1003 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name.all).all);
1004 end Executable_Prefix;
1005
1006 ------------------
1007 -- Exit_Program --
1008 ------------------
1009
1010 procedure Exit_Program (Exit_Code : Exit_Code_Type) is
1011 begin
1012 -- The program will exit with the following status:
1013
1014 -- 0 if the object file has been generated (with or without warnings)
1015 -- 1 if recompilation was not needed (smart recompilation)
1016 -- 2 if gnat1 has been killed by a signal (detected by GCC)
1017 -- 4 for a fatal error
1018 -- 5 if there were errors
1019 -- 6 if no code has been generated (spec)
1020
1021 -- Note that exit code 3 is not used and must not be used as this is
1022 -- the code returned by a program aborted via C abort() routine on
1023 -- Windows. GCC checks for that case and thinks that the child process
1024 -- has been aborted. This code (exit code 3) used to be the code used
1025 -- for E_No_Code, but E_No_Code was changed to 6 for this reason.
1026
1027 case Exit_Code is
1028 when E_Success => OS_Exit (0);
1029 when E_Warnings => OS_Exit (0);
1030 when E_No_Compile => OS_Exit (1);
1031 when E_Fatal => OS_Exit (4);
1032 when E_Errors => OS_Exit (5);
1033 when E_No_Code => OS_Exit (6);
1034 when E_Abort => OS_Abort;
1035 end case;
1036 end Exit_Program;
1037
1038 ----------
1039 -- Fail --
1040 ----------
1041
1042 procedure Fail (S : String) is
1043 begin
1044 -- We use Output in case there is a special output set up. In this case
1045 -- Set_Standard_Error will have no immediate effect.
1046
1047 Set_Standard_Error;
1048 Osint.Write_Program_Name;
1049 Write_Str (": ");
1050 Write_Str (S);
1051 Write_Eol;
1052
1053 Exit_Program (E_Fatal);
1054 end Fail;
1055
1056 ----------------------
1057 -- File_Names_Equal --
1058 ----------------------
1059
1060 function File_Names_Equal (File1, File2 : String) return Boolean is
1061
1062 function To_Lower (A : String) return String;
1063 -- For bootstrap reasons, we cannot use To_Lower function from
1064 -- System.Case_Util.
1065
1066 --------------
1067 -- To_Lower --
1068 --------------
1069
1070 function To_Lower (A : String) return String is
1071 Result : String := A;
1072 begin
1073 To_Lower (Result);
1074 return Result;
1075 end To_Lower;
1076
1077 -- Start of processing for File_Names_Equal
1078
1079 begin
1080 if File_Names_Case_Sensitive then
1081 return File1 = File2;
1082 else
1083 return To_Lower (File1) = To_Lower (File2);
1084 end if;
1085 end File_Names_Equal;
1086
1087 ---------------
1088 -- File_Hash --
1089 ---------------
1090
1091 function File_Hash (F : File_Name_Type) return File_Hash_Num is
1092 begin
1093 return File_Hash_Num (Int (F) mod File_Hash_Num'Range_Length);
1094 end File_Hash;
1095
1096 -----------------
1097 -- File_Length --
1098 -----------------
1099
1100 function File_Length
1101 (Name : C_File_Name;
1102 Attr : access File_Attributes) return Long_Integer
1103 is
1104 function Internal
1105 (F : Integer;
1106 N : C_File_Name;
1107 A : System.Address) return CRTL.int64;
1108 pragma Import (C, Internal, "__gnat_file_length_attr");
1109
1110 begin
1111 -- The conversion from int64 to Long_Integer is ok here as this
1112 -- routine is only to be used by the compiler and we do not expect
1113 -- a unit to be larger than a 32bit integer.
1114
1115 return Long_Integer (Internal (-1, Name, Attr.all'Address));
1116 end File_Length;
1117
1118 ---------------------
1119 -- File_Time_Stamp --
1120 ---------------------
1121
1122 function File_Time_Stamp
1123 (Name : C_File_Name;
1124 Attr : access File_Attributes) return OS_Time
1125 is
1126 function Internal (N : C_File_Name; A : System.Address) return OS_Time;
1127 pragma Import (C, Internal, "__gnat_file_time_name_attr");
1128 begin
1129 return Internal (Name, Attr.all'Address);
1130 end File_Time_Stamp;
1131
1132 function File_Time_Stamp
1133 (Name : Path_Name_Type;
1134 Attr : access File_Attributes) return Time_Stamp_Type
1135 is
1136 begin
1137 if Name = No_Path then
1138 return Empty_Time_Stamp;
1139 end if;
1140
1141 Get_Name_String (Name);
1142 Name_Buffer (Name_Len + 1) := ASCII.NUL;
1143 return OS_Time_To_GNAT_Time
1144 (File_Time_Stamp (Name_Buffer'Address, Attr));
1145 end File_Time_Stamp;
1146
1147 ----------------
1148 -- File_Stamp --
1149 ----------------
1150
1151 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type is
1152 begin
1153 if Name = No_File then
1154 return Empty_Time_Stamp;
1155 end if;
1156
1157 Get_Name_String (Name);
1158
1159 -- File_Time_Stamp will always return Invalid_Time if the file does
1160 -- not exist, and OS_Time_To_GNAT_Time will convert this value to
1161 -- Empty_Time_Stamp. Therefore we do not need to first test whether
1162 -- the file actually exists, which saves a system call.
1163
1164 return OS_Time_To_GNAT_Time
1165 (File_Time_Stamp (Name_Buffer (1 .. Name_Len)));
1166 end File_Stamp;
1167
1168 function File_Stamp (Name : Path_Name_Type) return Time_Stamp_Type is
1169 begin
1170 return File_Stamp (File_Name_Type (Name));
1171 end File_Stamp;
1172
1173 ---------------
1174 -- Find_File --
1175 ---------------
1176
1177 function Find_File
1178 (N : File_Name_Type;
1179 T : File_Type;
1180 Full_Name : Boolean := False) return File_Name_Type
1181 is
1182 Attr : aliased File_Attributes;
1183 Found : File_Name_Type;
1184 begin
1185 Find_File (N, T, Found, Attr'Access, Full_Name);
1186 return Found;
1187 end Find_File;
1188
1189 ---------------
1190 -- Find_File --
1191 ---------------
1192
1193 procedure Find_File
1194 (N : File_Name_Type;
1195 T : File_Type;
1196 Found : out File_Name_Type;
1197 Attr : access File_Attributes;
1198 Full_Name : Boolean := False)
1199 is
1200 begin
1201 Get_Name_String (N);
1202
1203 declare
1204 File_Name : String renames Name_Buffer (1 .. Name_Len);
1205 File : File_Name_Type := No_File;
1206 Last_Dir : Natural;
1207
1208 begin
1209 -- If we are looking for a config file, look only in the current
1210 -- directory, i.e. return input argument unchanged. Also look only in
1211 -- the current directory if we are looking for a .dg file (happens in
1212 -- -gnatD mode).
1213
1214 if T = Config
1215 or else (Debug_Generated_Code
1216 and then Name_Len > 3
1217 and then Name_Buffer (Name_Len - 2 .. Name_Len) = ".dg")
1218 then
1219 Found := N;
1220 Attr.all := Unknown_Attributes;
1221
1222 if T = Config then
1223 if Full_Name then
1224 declare
1225 Full_Path : constant String :=
1226 Normalize_Pathname (Get_Name_String (N));
1227 begin
1228 Found := Name_Find (Full_Path);
1229 end;
1230 end if;
1231
1232 -- Check that it is a file, not a directory
1233
1234 if not Is_Regular_File (Get_Name_String (Found)) then
1235 Found := No_File;
1236 end if;
1237 end if;
1238
1239 return;
1240
1241 -- If we are trying to find the current main file just look in the
1242 -- directory where the user said it was.
1243
1244 elsif Look_In_Primary_Directory_For_Current_Main
1245 and then Current_Main = N
1246 then
1247 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1248 return;
1249
1250 -- Otherwise do standard search for source file
1251
1252 else
1253 -- Check the mapping of this file name
1254
1255 File := Mapped_Path_Name (N);
1256
1257 -- If the file name is mapped to a path name, return the
1258 -- corresponding path name
1259
1260 if File /= No_File then
1261
1262 -- For locally removed file, Error_Name is returned; then
1263 -- return No_File, indicating the file is not a source.
1264
1265 if File = Error_File_Name then
1266 Found := No_File;
1267 else
1268 Found := File;
1269 end if;
1270
1271 Attr.all := Unknown_Attributes;
1272 return;
1273 end if;
1274
1275 -- First place to look is in the primary directory (i.e. the same
1276 -- directory as the source) unless this has been disabled with -I-
1277
1278 if Opt.Look_In_Primary_Dir then
1279 Locate_File (N, T, Primary_Directory, File_Name, Found, Attr);
1280
1281 if Found /= No_File then
1282 return;
1283 end if;
1284 end if;
1285
1286 -- Finally look in directories specified with switches -I/-aI/-aO
1287
1288 if T = Library then
1289 Last_Dir := Lib_Search_Directories.Last;
1290 else
1291 Last_Dir := Src_Search_Directories.Last;
1292 end if;
1293
1294 for D in Primary_Directory + 1 .. Last_Dir loop
1295 Locate_File (N, T, D, File_Name, Found, Attr);
1296
1297 if Found /= No_File then
1298 return;
1299 end if;
1300 end loop;
1301
1302 Attr.all := Unknown_Attributes;
1303 Found := No_File;
1304 end if;
1305 end;
1306 end Find_File;
1307
1308 -----------------------
1309 -- Find_Program_Name --
1310 -----------------------
1311
1312 procedure Find_Program_Name is
1313 Command_Name : String (1 .. Len_Arg (0));
1314 Cindex1 : Integer := Command_Name'First;
1315 Cindex2 : Integer := Command_Name'Last;
1316
1317 begin
1318 Fill_Arg (Command_Name'Address, 0);
1319
1320 if Command_Name = "" then
1321 Name_Len := 0;
1322 return;
1323 end if;
1324
1325 -- The program name might be specified by a full path name. However,
1326 -- we don't want to print that all out in an error message, so the
1327 -- path might need to be stripped away.
1328
1329 for J in reverse Cindex1 .. Cindex2 loop
1330 if Is_Directory_Separator (Command_Name (J)) then
1331 Cindex1 := J + 1;
1332 exit;
1333 end if;
1334 end loop;
1335
1336 -- Command_Name(Cindex1 .. Cindex2) is now the equivalent of the
1337 -- POSIX command "basename argv[0]"
1338
1339 -- Strip off any executable extension (usually nothing or .exe)
1340 -- but formally reported by autoconf in the variable EXEEXT
1341
1342 if Cindex2 - Cindex1 >= 4 then
1343 if To_Lower (Command_Name (Cindex2 - 3)) = '.'
1344 and then To_Lower (Command_Name (Cindex2 - 2)) = 'e'
1345 and then To_Lower (Command_Name (Cindex2 - 1)) = 'x'
1346 and then To_Lower (Command_Name (Cindex2)) = 'e'
1347 then
1348 Cindex2 := Cindex2 - 4;
1349 end if;
1350 end if;
1351
1352 Name_Len := Cindex2 - Cindex1 + 1;
1353 Name_Buffer (1 .. Name_Len) := Command_Name (Cindex1 .. Cindex2);
1354 end Find_Program_Name;
1355
1356 ------------------------
1357 -- Full_Lib_File_Name --
1358 ------------------------
1359
1360 procedure Full_Lib_File_Name
1361 (N : File_Name_Type;
1362 Lib_File : out File_Name_Type;
1363 Attr : out File_Attributes)
1364 is
1365 begin
1366 Smart_Find_File (N, Library, Lib_File, Attr);
1367 end Full_Lib_File_Name;
1368
1369 ------------------------
1370 -- Full_Lib_File_Name --
1371 ------------------------
1372
1373 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type is
1374 Attr : File_Attributes;
1375 File : File_Name_Type;
1376 begin
1377 Full_Lib_File_Name (N, File, Attr);
1378 return File;
1379 end Full_Lib_File_Name;
1380
1381 ----------------------------
1382 -- Full_Library_Info_Name --
1383 ----------------------------
1384
1385 function Full_Library_Info_Name return File_Name_Type is
1386 begin
1387 return Current_Full_Lib_Name;
1388 end Full_Library_Info_Name;
1389
1390 ---------------------------
1391 -- Full_Object_File_Name --
1392 ---------------------------
1393
1394 function Full_Object_File_Name return File_Name_Type is
1395 begin
1396 return Current_Full_Obj_Name;
1397 end Full_Object_File_Name;
1398
1399 ----------------------
1400 -- Full_Source_Name --
1401 ----------------------
1402
1403 function Full_Source_Name return File_Name_Type is
1404 begin
1405 return Current_Full_Source_Name;
1406 end Full_Source_Name;
1407
1408 ----------------------
1409 -- Full_Source_Name --
1410 ----------------------
1411
1412 function Full_Source_Name (N : File_Name_Type) return File_Name_Type is
1413 begin
1414 return Smart_Find_File (N, Source);
1415 end Full_Source_Name;
1416
1417 ----------------------
1418 -- Full_Source_Name --
1419 ----------------------
1420
1421 procedure Full_Source_Name
1422 (N : File_Name_Type;
1423 Full_File : out File_Name_Type;
1424 Attr : access File_Attributes) is
1425 begin
1426 Smart_Find_File (N, Source, Full_File, Attr.all);
1427 end Full_Source_Name;
1428
1429 -------------------
1430 -- Get_Directory --
1431 -------------------
1432
1433 function Get_Directory (Name : File_Name_Type) return File_Name_Type is
1434 begin
1435 Get_Name_String (Name);
1436
1437 for J in reverse 1 .. Name_Len loop
1438 if Is_Directory_Separator (Name_Buffer (J)) then
1439 Name_Len := J;
1440 return Name_Find;
1441 end if;
1442 end loop;
1443
1444 return Name_Find (Hostparm.Normalized_CWD);
1445 end Get_Directory;
1446
1447 ------------------------------
1448 -- Get_First_Main_File_Name --
1449 ------------------------------
1450
1451 function Get_First_Main_File_Name return String is
1452 begin
1453 return File_Names (1).all;
1454 end Get_First_Main_File_Name;
1455
1456 --------------------------
1457 -- Get_Next_Dir_In_Path --
1458 --------------------------
1459
1460 Search_Path_Pos : Integer;
1461 -- Keeps track of current position in search path. Initialized by the
1462 -- call to Get_Next_Dir_In_Path_Init, updated by Get_Next_Dir_In_Path.
1463
1464 function Get_Next_Dir_In_Path
1465 (Search_Path : String_Access) return String_Access
1466 is
1467 Lower_Bound : Positive := Search_Path_Pos;
1468 Upper_Bound : Positive;
1469
1470 begin
1471 loop
1472 while Lower_Bound <= Search_Path'Last
1473 and then Search_Path.all (Lower_Bound) = Path_Separator
1474 loop
1475 Lower_Bound := Lower_Bound + 1;
1476 end loop;
1477
1478 exit when Lower_Bound > Search_Path'Last;
1479
1480 Upper_Bound := Lower_Bound;
1481 while Upper_Bound <= Search_Path'Last
1482 and then Search_Path.all (Upper_Bound) /= Path_Separator
1483 loop
1484 Upper_Bound := Upper_Bound + 1;
1485 end loop;
1486
1487 Search_Path_Pos := Upper_Bound;
1488 return new String'(Search_Path.all (Lower_Bound .. Upper_Bound - 1));
1489 end loop;
1490
1491 return null;
1492 end Get_Next_Dir_In_Path;
1493
1494 -------------------------------
1495 -- Get_Next_Dir_In_Path_Init --
1496 -------------------------------
1497
1498 procedure Get_Next_Dir_In_Path_Init (Search_Path : String_Access) is
1499 begin
1500 Search_Path_Pos := Search_Path'First;
1501 end Get_Next_Dir_In_Path_Init;
1502
1503 --------------------------------------
1504 -- Get_Primary_Src_Search_Directory --
1505 --------------------------------------
1506
1507 function Get_Primary_Src_Search_Directory return String_Ptr is
1508 begin
1509 return Src_Search_Directories.Table (Primary_Directory);
1510 end Get_Primary_Src_Search_Directory;
1511
1512 ------------------------
1513 -- Get_RTS_Search_Dir --
1514 ------------------------
1515
1516 function Get_RTS_Search_Dir
1517 (Search_Dir : String;
1518 File_Type : Search_File_Type) return String_Ptr
1519 is
1520 procedure Get_Current_Dir
1521 (Dir : System.Address;
1522 Length : System.Address);
1523 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1524
1525 Max_Path : Integer;
1526 pragma Import (C, Max_Path, "__gnat_max_path_len");
1527 -- Maximum length of a path name
1528
1529 Current_Dir : String_Ptr;
1530 Default_Search_Dir : String_Access;
1531 Default_Suffix_Dir : String_Access;
1532 Local_Search_Dir : String_Access;
1533 Norm_Search_Dir : String_Access;
1534 Result_Search_Dir : String_Access;
1535 Search_File : String_Access;
1536 Temp_String : String_Ptr;
1537
1538 begin
1539 -- Add a directory separator at the end of the directory if necessary
1540 -- so that we can directly append a file to the directory
1541
1542 if not Is_Directory_Separator (Search_Dir (Search_Dir'Last)) then
1543 Local_Search_Dir :=
1544 new String'(Search_Dir & String'(1 => Directory_Separator));
1545 else
1546 Local_Search_Dir := new String'(Search_Dir);
1547 end if;
1548
1549 if File_Type = Include then
1550 Search_File := Include_Search_File;
1551 Default_Suffix_Dir := new String'("adainclude");
1552 else
1553 Search_File := Objects_Search_File;
1554 Default_Suffix_Dir := new String'("adalib");
1555 end if;
1556
1557 Norm_Search_Dir := Local_Search_Dir;
1558
1559 if Is_Absolute_Path (Norm_Search_Dir.all) then
1560
1561 -- We first verify if there is a directory Include_Search_Dir
1562 -- containing default search directories
1563
1564 Result_Search_Dir :=
1565 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1566 Default_Search_Dir :=
1567 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1568 Free (Norm_Search_Dir);
1569
1570 if Result_Search_Dir /= null then
1571 return String_Ptr (Result_Search_Dir);
1572 elsif Is_Directory (Default_Search_Dir.all) then
1573 return String_Ptr (Default_Search_Dir);
1574 else
1575 return null;
1576 end if;
1577
1578 -- Search in the current directory
1579
1580 else
1581 -- Get the current directory
1582
1583 declare
1584 Buffer : String (1 .. Max_Path + 2);
1585 Path_Len : Natural := Max_Path;
1586
1587 begin
1588 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1589
1590 if Path_Len = 0 then
1591 raise Program_Error;
1592 end if;
1593
1594 if not Is_Directory_Separator (Buffer (Path_Len)) then
1595 Path_Len := Path_Len + 1;
1596 Buffer (Path_Len) := Directory_Separator;
1597 end if;
1598
1599 Current_Dir := new String'(Buffer (1 .. Path_Len));
1600 end;
1601
1602 Norm_Search_Dir :=
1603 new String'(Current_Dir.all & Local_Search_Dir.all);
1604
1605 Result_Search_Dir :=
1606 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1607
1608 Default_Search_Dir :=
1609 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1610
1611 Free (Norm_Search_Dir);
1612
1613 if Result_Search_Dir /= null then
1614 return String_Ptr (Result_Search_Dir);
1615
1616 elsif Is_Directory (Default_Search_Dir.all) then
1617 return String_Ptr (Default_Search_Dir);
1618
1619 else
1620 -- Search in Search_Dir_Prefix/Search_Dir
1621
1622 Norm_Search_Dir :=
1623 new String'
1624 (Update_Path (Search_Dir_Prefix).all & Local_Search_Dir.all);
1625
1626 Result_Search_Dir :=
1627 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1628
1629 Default_Search_Dir :=
1630 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1631
1632 Free (Norm_Search_Dir);
1633
1634 if Result_Search_Dir /= null then
1635 return String_Ptr (Result_Search_Dir);
1636
1637 elsif Is_Directory (Default_Search_Dir.all) then
1638 return String_Ptr (Default_Search_Dir);
1639
1640 else
1641 -- We finally search in Search_Dir_Prefix/rts-Search_Dir
1642
1643 Temp_String :=
1644 new String'(Update_Path (Search_Dir_Prefix).all & "rts-");
1645
1646 Norm_Search_Dir :=
1647 new String'(Temp_String.all & Local_Search_Dir.all);
1648
1649 Result_Search_Dir :=
1650 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1651
1652 Default_Search_Dir :=
1653 new String'(Norm_Search_Dir.all & Default_Suffix_Dir.all);
1654 Free (Norm_Search_Dir);
1655
1656 if Result_Search_Dir /= null then
1657 return String_Ptr (Result_Search_Dir);
1658
1659 elsif Is_Directory (Default_Search_Dir.all) then
1660 return String_Ptr (Default_Search_Dir);
1661
1662 else
1663 return null;
1664 end if;
1665 end if;
1666 end if;
1667 end if;
1668 end Get_RTS_Search_Dir;
1669
1670 --------------------------------
1671 -- Include_Dir_Default_Prefix --
1672 --------------------------------
1673
1674 function Include_Dir_Default_Prefix return String_Access is
1675 begin
1676 if The_Include_Dir_Default_Prefix = null then
1677 The_Include_Dir_Default_Prefix :=
1678 String_Access (Update_Path (Include_Dir_Default_Name));
1679 end if;
1680
1681 return The_Include_Dir_Default_Prefix;
1682 end Include_Dir_Default_Prefix;
1683
1684 function Include_Dir_Default_Prefix return String is
1685 begin
1686 return Include_Dir_Default_Prefix.all;
1687 end Include_Dir_Default_Prefix;
1688
1689 ----------------
1690 -- Initialize --
1691 ----------------
1692
1693 procedure Initialize is
1694 begin
1695 Number_File_Names := 0;
1696 Current_File_Name_Index := 0;
1697
1698 Src_Search_Directories.Init;
1699 Lib_Search_Directories.Init;
1700
1701 -- Start off by setting all suppress options, to False. The special
1702 -- overflow fields are set to Not_Set (they will be set by -gnatp, or
1703 -- by -gnato, or, if neither of these appear, in Adjust_Global_Switches
1704 -- in Gnat1drv).
1705
1706 Suppress_Options := ((others => False), Not_Set, Not_Set);
1707
1708 -- Reserve the first slot in the search paths table. This is the
1709 -- directory of the main source file or main library file and is filled
1710 -- in by each call to Next_Main_Source/Next_Main_Lib_File with the
1711 -- directory specified for this main source or library file. This is the
1712 -- directory which is searched first by default. This default search is
1713 -- inhibited by the option -I- for both source and library files.
1714
1715 Src_Search_Directories.Set_Last (Primary_Directory);
1716 Src_Search_Directories.Table (Primary_Directory) := new String'("");
1717
1718 Lib_Search_Directories.Set_Last (Primary_Directory);
1719 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
1720 end Initialize;
1721
1722 ------------------
1723 -- Is_Directory --
1724 ------------------
1725
1726 function Is_Directory
1727 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1728 is
1729 function Internal (N : C_File_Name; A : System.Address) return Integer;
1730 pragma Import (C, Internal, "__gnat_is_directory_attr");
1731 begin
1732 return Internal (Name, Attr.all'Address) /= 0;
1733 end Is_Directory;
1734
1735 ----------------------------
1736 -- Is_Directory_Separator --
1737 ----------------------------
1738
1739 function Is_Directory_Separator (C : Character) return Boolean is
1740 begin
1741 -- In addition to the default directory_separator allow the '/' to
1742 -- act as separator since this is allowed in MS-DOS and Windows.
1743
1744 return C = Directory_Separator or else C = '/';
1745 end Is_Directory_Separator;
1746
1747 -------------------------
1748 -- Is_Readonly_Library --
1749 -------------------------
1750
1751 function Is_Readonly_Library (File : File_Name_Type) return Boolean is
1752 begin
1753 Get_Name_String (File);
1754
1755 pragma Assert (Name_Buffer (Name_Len - 3 .. Name_Len) = ".ali");
1756
1757 return not Is_Writable_File (Name_Buffer (1 .. Name_Len));
1758 end Is_Readonly_Library;
1759
1760 ------------------------
1761 -- Is_Executable_File --
1762 ------------------------
1763
1764 function Is_Executable_File
1765 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1766 is
1767 function Internal (N : C_File_Name; A : System.Address) return Integer;
1768 pragma Import (C, Internal, "__gnat_is_executable_file_attr");
1769 begin
1770 return Internal (Name, Attr.all'Address) /= 0;
1771 end Is_Executable_File;
1772
1773 ----------------------
1774 -- Is_Readable_File --
1775 ----------------------
1776
1777 function Is_Readable_File
1778 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1779 is
1780 function Internal (N : C_File_Name; A : System.Address) return Integer;
1781 pragma Import (C, Internal, "__gnat_is_readable_file_attr");
1782 begin
1783 return Internal (Name, Attr.all'Address) /= 0;
1784 end Is_Readable_File;
1785
1786 ---------------------
1787 -- Is_Regular_File --
1788 ---------------------
1789
1790 function Is_Regular_File
1791 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1792 is
1793 function Internal (N : C_File_Name; A : System.Address) return Integer;
1794 pragma Import (C, Internal, "__gnat_is_regular_file_attr");
1795 begin
1796 return Internal (Name, Attr.all'Address) /= 0;
1797 end Is_Regular_File;
1798
1799 ----------------------
1800 -- Is_Symbolic_Link --
1801 ----------------------
1802
1803 function Is_Symbolic_Link
1804 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1805 is
1806 function Internal (N : C_File_Name; A : System.Address) return Integer;
1807 pragma Import (C, Internal, "__gnat_is_symbolic_link_attr");
1808 begin
1809 return Internal (Name, Attr.all'Address) /= 0;
1810 end Is_Symbolic_Link;
1811
1812 ----------------------
1813 -- Is_Writable_File --
1814 ----------------------
1815
1816 function Is_Writable_File
1817 (Name : C_File_Name; Attr : access File_Attributes) return Boolean
1818 is
1819 function Internal (N : C_File_Name; A : System.Address) return Integer;
1820 pragma Import (C, Internal, "__gnat_is_writable_file_attr");
1821 begin
1822 return Internal (Name, Attr.all'Address) /= 0;
1823 end Is_Writable_File;
1824
1825 -------------------
1826 -- Lib_File_Name --
1827 -------------------
1828
1829 function Lib_File_Name
1830 (Source_File : File_Name_Type;
1831 Munit_Index : Nat := 0) return File_Name_Type
1832 is
1833 begin
1834 Get_Name_String (Source_File);
1835
1836 for J in reverse 2 .. Name_Len loop
1837 if Name_Buffer (J) = '.' then
1838 Name_Len := J - 1;
1839 exit;
1840 end if;
1841 end loop;
1842
1843 if Munit_Index /= 0 then
1844 Add_Char_To_Name_Buffer (Multi_Unit_Index_Character);
1845 Add_Nat_To_Name_Buffer (Munit_Index);
1846 end if;
1847
1848 Add_Char_To_Name_Buffer ('.');
1849 Add_Str_To_Name_Buffer (ALI_Suffix.all);
1850 return Name_Find;
1851 end Lib_File_Name;
1852
1853 -----------------
1854 -- Locate_File --
1855 -----------------
1856
1857 procedure Locate_File
1858 (N : File_Name_Type;
1859 T : File_Type;
1860 Dir : Natural;
1861 Name : String;
1862 Found : out File_Name_Type;
1863 Attr : access File_Attributes)
1864 is
1865 Dir_Name : String_Ptr;
1866
1867 begin
1868 -- If Name is already an absolute path, do not look for a directory
1869
1870 if Is_Absolute_Path (Name) then
1871 Dir_Name := No_Dir;
1872
1873 elsif T = Library then
1874 Dir_Name := Lib_Search_Directories.Table (Dir);
1875
1876 else
1877 pragma Assert (T /= Config);
1878 Dir_Name := Src_Search_Directories.Table (Dir);
1879 end if;
1880
1881 declare
1882 Full_Name :
1883 constant String (1 .. Dir_Name'Length + Name'Length + 1) :=
1884 Dir_Name.all & Name & ASCII.NUL;
1885 -- Use explicit bounds, because Dir_Name might be a substring whose
1886 -- 'First is not 1.
1887
1888 begin
1889 Attr.all := Unknown_Attributes;
1890
1891 if not Is_Regular_File (Full_Name'Address, Attr) then
1892 Found := No_File;
1893
1894 else
1895 -- If the file is in the current directory then return N itself
1896
1897 if Dir_Name'Length = 0 then
1898 Found := N;
1899 else
1900 Found :=
1901 Name_Find (Full_Name (Full_Name'First .. Full_Name'Last - 1));
1902 end if;
1903 end if;
1904 end;
1905 end Locate_File;
1906
1907 -------------------------------
1908 -- Matching_Full_Source_Name --
1909 -------------------------------
1910
1911 function Matching_Full_Source_Name
1912 (N : File_Name_Type;
1913 T : Time_Stamp_Type) return File_Name_Type
1914 is
1915 begin
1916 Get_Name_String (N);
1917
1918 declare
1919 File_Name : constant String := Name_Buffer (1 .. Name_Len);
1920 File : File_Name_Type := No_File;
1921 Attr : aliased File_Attributes;
1922 Last_Dir : Natural;
1923
1924 begin
1925 if Opt.Look_In_Primary_Dir then
1926 Locate_File
1927 (N, Source, Primary_Directory, File_Name, File,
1928 Attr'Unchecked_Access);
1929
1930 if File /= No_File and then T = File_Stamp (N) then
1931 return File;
1932 end if;
1933 end if;
1934
1935 Last_Dir := Src_Search_Directories.Last;
1936
1937 for D in Primary_Directory + 1 .. Last_Dir loop
1938 Locate_File (N, Source, D, File_Name, File, Attr'Unchecked_Access);
1939
1940 if File /= No_File and then T = File_Stamp (File) then
1941 return File;
1942 end if;
1943 end loop;
1944
1945 return No_File;
1946 end;
1947 end Matching_Full_Source_Name;
1948
1949 ----------------
1950 -- More_Files --
1951 ----------------
1952
1953 function More_Files return Boolean is
1954 begin
1955 return (Current_File_Name_Index < Number_File_Names);
1956 end More_Files;
1957
1958 -------------------------------
1959 -- Nb_Dir_In_Obj_Search_Path --
1960 -------------------------------
1961
1962 function Nb_Dir_In_Obj_Search_Path return Natural is
1963 begin
1964 if Opt.Look_In_Primary_Dir then
1965 return Lib_Search_Directories.Last - Primary_Directory + 1;
1966 else
1967 return Lib_Search_Directories.Last - Primary_Directory;
1968 end if;
1969 end Nb_Dir_In_Obj_Search_Path;
1970
1971 -------------------------------
1972 -- Nb_Dir_In_Src_Search_Path --
1973 -------------------------------
1974
1975 function Nb_Dir_In_Src_Search_Path return Natural is
1976 begin
1977 if Opt.Look_In_Primary_Dir then
1978 return Src_Search_Directories.Last - Primary_Directory + 1;
1979 else
1980 return Src_Search_Directories.Last - Primary_Directory;
1981 end if;
1982 end Nb_Dir_In_Src_Search_Path;
1983
1984 --------------------
1985 -- Next_Main_File --
1986 --------------------
1987
1988 function Next_Main_File return File_Name_Type is
1989 File_Name : String_Ptr;
1990 Dir_Name : String_Ptr;
1991 Fptr : Natural;
1992
1993 begin
1994 pragma Assert (More_Files);
1995
1996 Current_File_Name_Index := Current_File_Name_Index + 1;
1997
1998 -- Get the file and directory name
1999
2000 File_Name := File_Names (Current_File_Name_Index);
2001 Fptr := File_Name'First;
2002
2003 for J in reverse File_Name'Range loop
2004 if Is_Directory_Separator (File_Name (J)) then
2005 if J = File_Name'Last then
2006 Fail ("File name missing");
2007 end if;
2008
2009 Fptr := J + 1;
2010 exit;
2011 end if;
2012 end loop;
2013
2014 -- Save name of directory in which main unit resides for use in
2015 -- locating other units
2016
2017 Dir_Name := new String'(File_Name (File_Name'First .. Fptr - 1));
2018
2019 case Running_Program is
2020 when Compiler =>
2021 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
2022 Look_In_Primary_Directory_For_Current_Main := True;
2023
2024 when Make =>
2025 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
2026
2027 if Fptr > File_Name'First then
2028 Look_In_Primary_Directory_For_Current_Main := True;
2029 end if;
2030
2031 when Binder
2032 | Gnatls
2033 =>
2034 Dir_Name := Normalize_Directory_Name (Dir_Name.all);
2035 Lib_Search_Directories.Table (Primary_Directory) := Dir_Name;
2036
2037 when Unspecified =>
2038 null;
2039 end case;
2040
2041 Name_Len := File_Name'Last - Fptr + 1;
2042 Name_Buffer (1 .. Name_Len) := File_Name (Fptr .. File_Name'Last);
2043 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2044 Current_Main := Name_Find;
2045
2046 -- In the gnatmake case, the main file may have not have the
2047 -- extension. Try ".adb" first then ".ads"
2048
2049 if Running_Program = Make then
2050 declare
2051 Orig_Main : constant File_Name_Type := Current_Main;
2052
2053 begin
2054 if Strip_Suffix (Orig_Main) = Orig_Main then
2055 Current_Main :=
2056 Append_Suffix_To_File_Name (Orig_Main, ".adb");
2057
2058 if Full_Source_Name (Current_Main) = No_File then
2059 Current_Main :=
2060 Append_Suffix_To_File_Name (Orig_Main, ".ads");
2061
2062 if Full_Source_Name (Current_Main) = No_File then
2063 Current_Main := Orig_Main;
2064 end if;
2065 end if;
2066 end if;
2067 end;
2068 end if;
2069
2070 return Current_Main;
2071 end Next_Main_File;
2072
2073 ------------------------------
2074 -- Normalize_Directory_Name --
2075 ------------------------------
2076
2077 function Normalize_Directory_Name (Directory : String) return String_Ptr is
2078
2079 function Is_Quoted (Path : String) return Boolean;
2080 pragma Inline (Is_Quoted);
2081 -- Returns true if Path is quoted (either double or single quotes)
2082
2083 ---------------
2084 -- Is_Quoted --
2085 ---------------
2086
2087 function Is_Quoted (Path : String) return Boolean is
2088 First : constant Character := Path (Path'First);
2089 Last : constant Character := Path (Path'Last);
2090
2091 begin
2092 if (First = ''' and then Last = ''')
2093 or else
2094 (First = '"' and then Last = '"')
2095 then
2096 return True;
2097 else
2098 return False;
2099 end if;
2100 end Is_Quoted;
2101
2102 Result : String_Ptr;
2103
2104 -- Start of processing for Normalize_Directory_Name
2105
2106 begin
2107 if Directory'Length = 0 then
2108 Result := new String'(Hostparm.Normalized_CWD);
2109
2110 elsif Is_Directory_Separator (Directory (Directory'Last)) then
2111 Result := new String'(Directory);
2112
2113 elsif Is_Quoted (Directory) then
2114
2115 -- This is a quoted string, it certainly means that the directory
2116 -- contains some spaces for example. We can safely remove the quotes
2117 -- here as the OS_Lib.Normalize_Arguments will be called before any
2118 -- spawn routines. This ensure that quotes will be added when needed.
2119
2120 Result := new String (1 .. Directory'Length - 1);
2121 Result (1 .. Directory'Length - 2) :=
2122 Directory (Directory'First + 1 .. Directory'Last - 1);
2123 Result (Result'Last) := Directory_Separator;
2124
2125 else
2126 Result := new String (1 .. Directory'Length + 1);
2127 Result (1 .. Directory'Length) := Directory;
2128 Result (Directory'Length + 1) := Directory_Separator;
2129 end if;
2130
2131 return Result;
2132 end Normalize_Directory_Name;
2133
2134 ---------------------
2135 -- Number_Of_Files --
2136 ---------------------
2137
2138 function Number_Of_Files return Nat is
2139 begin
2140 return Number_File_Names;
2141 end Number_Of_Files;
2142
2143 -------------------------------
2144 -- Object_Dir_Default_Prefix --
2145 -------------------------------
2146
2147 function Object_Dir_Default_Prefix return String is
2148 Object_Dir : String_Access :=
2149 String_Access (Update_Path (Object_Dir_Default_Name));
2150
2151 begin
2152 if Object_Dir = null then
2153 return "";
2154
2155 else
2156 declare
2157 Result : constant String := Object_Dir.all;
2158 begin
2159 Free (Object_Dir);
2160 return Result;
2161 end;
2162 end if;
2163 end Object_Dir_Default_Prefix;
2164
2165 ----------------------
2166 -- Object_File_Name --
2167 ----------------------
2168
2169 function Object_File_Name (N : File_Name_Type) return File_Name_Type is
2170 begin
2171 if N = No_File then
2172 return No_File;
2173 end if;
2174
2175 Get_Name_String (N);
2176 Name_Len := Name_Len - ALI_Suffix'Length - 1;
2177
2178 Add_Str_To_Name_Buffer (Target_Object_Suffix);
2179
2180 return Name_Enter;
2181 end Object_File_Name;
2182
2183 -------------------------------
2184 -- OS_Exit_Through_Exception --
2185 -------------------------------
2186
2187 procedure OS_Exit_Through_Exception (Status : Integer) is
2188 begin
2189 Current_Exit_Status := Status;
2190 raise Types.Terminate_Program;
2191 end OS_Exit_Through_Exception;
2192
2193 --------------------------
2194 -- OS_Time_To_GNAT_Time --
2195 --------------------------
2196
2197 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type is
2198 GNAT_Time : Time_Stamp_Type;
2199
2200 type Underlying_OS_Time is
2201 range -(2 ** 63) .. +(2 ** 63 - 1);
2202 -- Underlying_OS_Time is a redeclaration of OS_Time to allow integer
2203 -- manipulation. Remove this in favor of To_Ada/To_C once newer
2204 -- GNAT releases are available with these functions.
2205
2206 function To_Int is
2207 new Ada.Unchecked_Conversion (OS_Time, Underlying_OS_Time);
2208 function From_Int is
2209 new Ada.Unchecked_Conversion (Underlying_OS_Time, OS_Time);
2210
2211 TI : Underlying_OS_Time := To_Int (T);
2212 Y : Year_Type;
2213 Mo : Month_Type;
2214 D : Day_Type;
2215 H : Hour_Type;
2216 Mn : Minute_Type;
2217 S : Second_Type;
2218
2219 begin
2220 if T = Invalid_Time then
2221 return Empty_Time_Stamp;
2222 end if;
2223
2224 if On_Windows and then TI mod 2 > 0 then
2225 -- Windows ALI files had timestamps rounded to even seconds
2226 -- historically. The rounding was originally done in GM_Split.
2227 -- Now that GM_Split no longer does it, we are rounding it here
2228 -- only for ALI files.
2229
2230 TI := TI + 1;
2231 end if;
2232
2233 GM_Split (From_Int (TI), Y, Mo, D, H, Mn, S);
2234
2235 Make_Time_Stamp
2236 (Year => Nat (Y),
2237 Month => Nat (Mo),
2238 Day => Nat (D),
2239 Hour => Nat (H),
2240 Minutes => Nat (Mn),
2241 Seconds => Nat (S),
2242 TS => GNAT_Time);
2243
2244 return GNAT_Time;
2245 end OS_Time_To_GNAT_Time;
2246
2247 -----------------
2248 -- Prep_Suffix --
2249 -----------------
2250
2251 function Prep_Suffix return String is
2252 begin
2253 return ".prep";
2254 end Prep_Suffix;
2255
2256 ------------------
2257 -- Program_Name --
2258 ------------------
2259
2260 function Program_Name (Nam : String; Prog : String) return String_Access is
2261 End_Of_Prefix : Natural := 0;
2262 Start_Of_Prefix : Positive := 1;
2263 Start_Of_Suffix : Positive;
2264
2265 begin
2266 -- Get the name of the current program being executed
2267
2268 Find_Program_Name;
2269
2270 -- Find the target prefix if any, for the cross compilation case.
2271 -- For instance in "powerpc-elf-gcc" the target prefix is
2272 -- "powerpc-elf-"
2273 -- Ditto for suffix, e.g. in "gcc-4.1", the suffix is "-4.1"
2274
2275 for J in reverse 1 .. Name_Len loop
2276 if Is_Directory_Separator (Name_Buffer (J))
2277 or else Name_Buffer (J) = ':'
2278 then
2279 Start_Of_Prefix := J + 1;
2280 exit;
2281 end if;
2282 end loop;
2283
2284 -- Find End_Of_Prefix
2285
2286 for J in Start_Of_Prefix .. Name_Len - Prog'Length + 1 loop
2287 if Name_Buffer (J .. J + Prog'Length - 1) = Prog then
2288 End_Of_Prefix := J - 1;
2289 exit;
2290 end if;
2291 end loop;
2292
2293 Start_Of_Suffix := End_Of_Prefix + Prog'Length + 1;
2294
2295 -- Create the new program name
2296
2297 return new String'
2298 (Name_Buffer (Start_Of_Prefix .. End_Of_Prefix)
2299 & Nam
2300 & Name_Buffer (Start_Of_Suffix .. Name_Len));
2301 end Program_Name;
2302
2303 ------------------------------
2304 -- Read_Default_Search_Dirs --
2305 ------------------------------
2306
2307 function Read_Default_Search_Dirs
2308 (Search_Dir_Prefix : String_Access;
2309 Search_File : String_Access;
2310 Search_Dir_Default_Name : String_Access) return String_Access
2311 is
2312 Prefix_Len : constant Integer := Search_Dir_Prefix.all'Length;
2313 Buffer : String (1 .. Prefix_Len + Search_File.all'Length + 1);
2314 File_FD : File_Descriptor;
2315 S, S1 : String_Access;
2316 Len : Integer;
2317 Curr : Integer;
2318 Actual_Len : Integer;
2319 J1 : Integer;
2320
2321 Prev_Was_Separator : Boolean;
2322 Nb_Relative_Dir : Integer;
2323
2324 function Is_Relative (S : String; K : Positive) return Boolean;
2325 pragma Inline (Is_Relative);
2326 -- Returns True if a relative directory specification is found
2327 -- in S at position K, False otherwise.
2328
2329 -----------------
2330 -- Is_Relative --
2331 -----------------
2332
2333 function Is_Relative (S : String; K : Positive) return Boolean is
2334 begin
2335 return not Is_Absolute_Path (S (K .. S'Last));
2336 end Is_Relative;
2337
2338 -- Start of processing for Read_Default_Search_Dirs
2339
2340 begin
2341 -- Construct a C compatible character string buffer
2342
2343 Buffer (1 .. Search_Dir_Prefix.all'Length) :=
2344 Search_Dir_Prefix.all;
2345 Buffer (Search_Dir_Prefix.all'Length + 1 .. Buffer'Last - 1) :=
2346 Search_File.all;
2347 Buffer (Buffer'Last) := ASCII.NUL;
2348
2349 File_FD := Open_Read (Buffer'Address, Binary);
2350 if File_FD = Invalid_FD then
2351 return Search_Dir_Default_Name;
2352 end if;
2353
2354 Len := Integer (File_Length (File_FD));
2355
2356 -- An extra character for a trailing Path_Separator is allocated
2357
2358 S := new String (1 .. Len + 1);
2359 S (Len + 1) := Path_Separator;
2360
2361 -- Read the file. Note that the loop is probably not necessary since the
2362 -- whole file is read at once but the loop is harmless and that way we
2363 -- are sure to accommodate systems where this is not the case.
2364
2365 Curr := 1;
2366 Actual_Len := Len;
2367 while Actual_Len /= 0 loop
2368 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
2369 Curr := Curr + Actual_Len;
2370 end loop;
2371
2372 -- Process the file, dealing with path separators
2373
2374 Prev_Was_Separator := True;
2375 Nb_Relative_Dir := 0;
2376 for J in 1 .. Len loop
2377
2378 -- Treat any EOL character as a path separator. Note that we do
2379 -- not treat space as a path separator (we used to treat space as a
2380 -- path separator in an earlier version). That way space can appear
2381 -- as a legitimate character in a path name.
2382
2383 if S (J) = ASCII.LF or else S (J) = ASCII.CR then
2384 S (J) := Path_Separator;
2385 end if;
2386
2387 -- Test for explicit path separator (or control char as above)
2388
2389 if S (J) = Path_Separator then
2390 Prev_Was_Separator := True;
2391
2392 -- If not path separator, register use of relative directory
2393
2394 else
2395 if Prev_Was_Separator and then Is_Relative (S.all, J) then
2396 Nb_Relative_Dir := Nb_Relative_Dir + 1;
2397 end if;
2398
2399 Prev_Was_Separator := False;
2400 end if;
2401 end loop;
2402
2403 if Nb_Relative_Dir = 0 then
2404 return S;
2405 end if;
2406
2407 -- Add the Search_Dir_Prefix to all relative paths
2408
2409 S1 := new String (1 .. S'Length + Nb_Relative_Dir * Prefix_Len);
2410 J1 := 1;
2411 Prev_Was_Separator := True;
2412 for J in 1 .. Len + 1 loop
2413 if S (J) = Path_Separator then
2414 Prev_Was_Separator := True;
2415
2416 else
2417 if Prev_Was_Separator and then Is_Relative (S.all, J) then
2418 S1 (J1 .. J1 + Prefix_Len - 1) := Search_Dir_Prefix.all;
2419 J1 := J1 + Prefix_Len;
2420 end if;
2421
2422 Prev_Was_Separator := False;
2423 end if;
2424 S1 (J1) := S (J);
2425 J1 := J1 + 1;
2426 end loop;
2427
2428 Free (S);
2429 return S1;
2430 end Read_Default_Search_Dirs;
2431
2432 -----------------------
2433 -- Read_Library_Info --
2434 -----------------------
2435
2436 function Read_Library_Info
2437 (Lib_File : File_Name_Type;
2438 Fatal_Err : Boolean := False) return Text_Buffer_Ptr
2439 is
2440 File : File_Name_Type;
2441 Attr : aliased File_Attributes;
2442 begin
2443 Find_File (Lib_File, Library, File, Attr'Access);
2444 return Read_Library_Info_From_Full
2445 (Full_Lib_File => File,
2446 Lib_File_Attr => Attr'Access,
2447 Fatal_Err => Fatal_Err);
2448 end Read_Library_Info;
2449
2450 ---------------------------------
2451 -- Read_Library_Info_From_Full --
2452 ---------------------------------
2453
2454 function Read_Library_Info_From_Full
2455 (Full_Lib_File : File_Name_Type;
2456 Lib_File_Attr : access File_Attributes;
2457 Fatal_Err : Boolean := False) return Text_Buffer_Ptr
2458 is
2459 Lib_FD : File_Descriptor;
2460 -- The file descriptor for the current library file. A negative value
2461 -- indicates failure to open the specified source file.
2462
2463 Len : Integer;
2464 -- Length of source file text (ALI). If it doesn't fit in an integer
2465 -- we're probably stuck anyway (>2 gigs of source seems a lot, and
2466 -- there are other places in the compiler that make this assumption).
2467
2468 Text : Text_Buffer_Ptr;
2469 -- Allocated text buffer
2470
2471 Status : Boolean;
2472 pragma Warnings (Off, Status);
2473 -- For the calls to Close
2474
2475 begin
2476 Current_Full_Lib_Name := Full_Lib_File;
2477 Current_Full_Obj_Name := Object_File_Name (Current_Full_Lib_Name);
2478
2479 if Current_Full_Lib_Name = No_File then
2480 if Fatal_Err then
2481 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2482 else
2483 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2484 return null;
2485 end if;
2486 end if;
2487
2488 Get_Name_String (Current_Full_Lib_Name);
2489 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2490
2491 -- Open the library FD, note that we open in binary mode, because as
2492 -- documented in the spec, the caller is expected to handle either
2493 -- DOS or Unix mode files, and there is no point in wasting time on
2494 -- text translation when it is not required.
2495
2496 Lib_FD := Open_Read (Name_Buffer'Address, Binary);
2497
2498 if Lib_FD = Invalid_FD then
2499 if Fatal_Err then
2500 Fail ("Cannot open: " & Name_Buffer (1 .. Name_Len));
2501 else
2502 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2503 return null;
2504 end if;
2505 end if;
2506
2507 -- Compute the length of the file (potentially also preparing other data
2508 -- like the timestamp and whether the file is read-only, for future use)
2509
2510 Len := Integer (File_Length (Name_Buffer'Address, Lib_File_Attr));
2511
2512 -- Check for object file consistency if requested
2513
2514 if Opt.Check_Object_Consistency then
2515 -- On most systems, this does not result in an extra system call
2516
2517 Current_Full_Lib_Stamp :=
2518 OS_Time_To_GNAT_Time
2519 (File_Time_Stamp (Name_Buffer'Address, Lib_File_Attr));
2520
2521 -- ??? One system call here
2522
2523 Current_Full_Obj_Stamp := File_Stamp (Current_Full_Obj_Name);
2524
2525 if Current_Full_Obj_Stamp (1) = ' ' then
2526
2527 -- When the library is readonly always assume object is consistent
2528 -- The call to Is_Writable_File only results in a system call on
2529 -- some systems, but in most cases it has already been computed as
2530 -- part of the call to File_Length above.
2531
2532 Get_Name_String (Current_Full_Lib_Name);
2533 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2534
2535 if not Is_Writable_File (Name_Buffer'Address, Lib_File_Attr) then
2536 Current_Full_Obj_Stamp := Current_Full_Lib_Stamp;
2537
2538 elsif Fatal_Err then
2539 Get_Name_String (Current_Full_Obj_Name);
2540 Close (Lib_FD, Status);
2541
2542 -- No need to check the status, we fail anyway
2543
2544 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2545
2546 else
2547 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2548 Close (Lib_FD, Status);
2549
2550 -- No need to check the status, we return null anyway
2551
2552 return null;
2553 end if;
2554
2555 elsif Current_Full_Obj_Stamp < Current_Full_Lib_Stamp then
2556 Close (Lib_FD, Status);
2557
2558 -- No need to check the status, we return null anyway
2559
2560 return null;
2561 end if;
2562 end if;
2563
2564 -- Read data from the file
2565
2566 declare
2567 Actual_Len : Integer;
2568
2569 Lo : constant Text_Ptr := 0;
2570 -- Low bound for allocated text buffer
2571
2572 Hi : Text_Ptr := Text_Ptr (Len);
2573 -- High bound for allocated text buffer. Note length is Len + 1
2574 -- which allows for extra EOF character at the end of the buffer.
2575
2576 begin
2577 -- Allocate text buffer. Note extra character at end for EOF
2578
2579 Text := new Text_Buffer (Lo .. Hi);
2580
2581 -- Some systems have file types that require one read per line,
2582 -- so read until we get the Len bytes or until there are no more
2583 -- characters.
2584
2585 Hi := Lo;
2586 loop
2587 Actual_Len := Read (Lib_FD, Text (Hi)'Address, Len);
2588 Hi := Hi + Text_Ptr (Actual_Len);
2589 exit when Actual_Len = Len or else Actual_Len <= 0;
2590 end loop;
2591
2592 Text (Hi) := EOF;
2593 end;
2594
2595 -- Read is complete, close file and we are done
2596
2597 Close (Lib_FD, Status);
2598 -- The status should never be False. But, if it is, what can we do?
2599 -- So, we don't test it.
2600
2601 return Text;
2602
2603 end Read_Library_Info_From_Full;
2604
2605 ----------------------
2606 -- Read_Source_File --
2607 ----------------------
2608
2609 procedure Read_Source_File
2610 (N : File_Name_Type;
2611 Lo : Source_Ptr;
2612 Hi : out Source_Ptr;
2613 Src : out Source_Buffer_Ptr;
2614 FD : out File_Descriptor;
2615 T : File_Type := Source)
2616 is
2617 Len : Integer;
2618 -- Length of file, assume no more than 2 gigabytes of source
2619
2620 Actual_Len : Integer;
2621
2622 Status : Boolean;
2623 pragma Warnings (Off, Status);
2624 -- For the call to Close
2625
2626 begin
2627 Current_Full_Source_Name := Find_File (N, T, Full_Name => True);
2628 Current_Full_Source_Stamp := File_Stamp (Current_Full_Source_Name);
2629
2630 if Current_Full_Source_Name = No_File then
2631
2632 -- If we were trying to access the main file and we could not find
2633 -- it, we have an error.
2634
2635 if N = Current_Main then
2636 Get_Name_String (N);
2637 Fail ("Cannot find: " & Name_Buffer (1 .. Name_Len));
2638 end if;
2639
2640 FD := Null_FD;
2641 Src := null;
2642 Hi := No_Location;
2643 return;
2644 end if;
2645
2646 Get_Name_String (Current_Full_Source_Name);
2647 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2648
2649 -- Open the source FD, note that we open in binary mode, because as
2650 -- documented in the spec, the caller is expected to handle either
2651 -- DOS or Unix mode files, and there is no point in wasting time on
2652 -- text translation when it is not required.
2653
2654 FD := Open_Read (Name_Buffer'Address, Binary);
2655
2656 if FD = Invalid_FD then
2657 Src := null;
2658 Hi := No_Location;
2659 return;
2660 end if;
2661
2662 -- If it's a Source file, print out the file name, if requested, and if
2663 -- it's not part of the runtimes, store it in File_Name_Chars. We don't
2664 -- want to print non-Source files, like GNAT-TEMP-000001.TMP used to
2665 -- pass information from gprbuild to gcc. We don't want to save runtime
2666 -- file names, because we don't want users to send them in bug reports.
2667
2668 if T = Source then
2669 declare
2670 Name : String renames Name_Buffer (1 .. Name_Len);
2671 Inc : String renames Include_Dir_Default_Prefix.all;
2672
2673 Part_Of_Runtimes : constant Boolean :=
2674 Inc /= ""
2675 and then Inc'Length < Name_Len
2676 and then Name_Buffer (1 .. Inc'Length) = Inc;
2677
2678 begin
2679 if Debug.Debug_Flag_Dot_N then
2680 Write_Line (Name);
2681 end if;
2682
2683 if not Part_Of_Runtimes then
2684 File_Name_Chars.Append_All (File_Name_Chars.Table_Type (Name));
2685 File_Name_Chars.Append (ASCII.LF);
2686 end if;
2687 end;
2688 end if;
2689
2690 -- Prepare to read data from the file
2691
2692 Len := Integer (File_Length (FD));
2693
2694 -- Set Hi so that length is one more than the physical length,
2695 -- allowing for the extra EOF character at the end of the buffer
2696
2697 Hi := Lo + Source_Ptr (Len);
2698
2699 -- Do the actual read operation
2700
2701 declare
2702 Var_Ptr : constant Source_Buffer_Ptr_Var :=
2703 new Source_Buffer (Lo .. Hi);
2704 -- Allocate source buffer, allowing extra character at end for EOF
2705 begin
2706 -- Some systems have file types that require one read per line,
2707 -- so read until we get the Len bytes or until there are no more
2708 -- characters.
2709
2710 Hi := Lo;
2711 loop
2712 Actual_Len := Read (FD, Var_Ptr (Hi)'Address, Len);
2713 Hi := Hi + Source_Ptr (Actual_Len);
2714 exit when Actual_Len = Len or else Actual_Len <= 0;
2715 end loop;
2716
2717 Var_Ptr (Hi) := EOF;
2718 Src := Var_Ptr.all'Access;
2719 end;
2720
2721 -- Read is complete, get time stamp and close file and we are done
2722
2723 Close (FD, Status);
2724
2725 -- The status should never be False. But, if it is, what can we do?
2726 -- So, we don't test it.
2727
2728 -- ???We don't really need to return Hi anymore; We could get rid of
2729 -- it. We could also make this into a function.
2730
2731 pragma Assert (Hi = Src'Last);
2732 end Read_Source_File;
2733
2734 -------------------
2735 -- Relocate_Path --
2736 -------------------
2737
2738 function Relocate_Path
2739 (Prefix : String;
2740 Path : String) return String_Ptr
2741 is
2742 S : String_Ptr;
2743
2744 procedure set_std_prefix (S : String; Len : Integer);
2745 pragma Import (C, set_std_prefix);
2746
2747 begin
2748 if Std_Prefix = null then
2749 Std_Prefix := String_Ptr (Getenv ("GNSA_ROOT"));
2750
2751 if Std_Prefix.all = "" then
2752 Std_Prefix := Executable_Prefix;
2753
2754 elsif not Is_Directory_Separator (Std_Prefix (Std_Prefix'Last)) then
2755
2756 -- The remainder of this function assumes that Std_Prefix
2757 -- terminates with a dir separator, so we force this here.
2758
2759 declare
2760 Old_Prefix : String_Ptr := Std_Prefix;
2761 begin
2762 Std_Prefix := new String (1 .. Old_Prefix'Length + 1);
2763 Std_Prefix (1 .. Old_Prefix'Length) := Old_Prefix.all;
2764 Std_Prefix (Old_Prefix'Length + 1) := Directory_Separator;
2765 Free (Old_Prefix);
2766 end;
2767 end if;
2768
2769 if Std_Prefix.all /= "" then
2770
2771 -- Remove trailing directory separator when calling set_std_prefix
2772
2773 set_std_prefix (Std_Prefix.all, Std_Prefix'Length - 1);
2774 end if;
2775 end if;
2776
2777 if Path'Last >= Prefix'Last and then Path (Prefix'Range) = Prefix then
2778 if Std_Prefix.all /= "" then
2779 S := new String
2780 (1 .. Std_Prefix'Length + Path'Last - Prefix'Last);
2781 S (1 .. Std_Prefix'Length) := Std_Prefix.all;
2782 S (Std_Prefix'Length + 1 .. S'Last) :=
2783 Path (Prefix'Last + 1 .. Path'Last);
2784 return S;
2785 end if;
2786 end if;
2787
2788 return new String'(Path);
2789 end Relocate_Path;
2790
2791 -----------------
2792 -- Set_Program --
2793 -----------------
2794
2795 procedure Set_Program (P : Program_Type) is
2796 begin
2797 if Program_Set then
2798 Fail ("Set_Program called twice");
2799 end if;
2800
2801 Program_Set := True;
2802 Running_Program := P;
2803 end Set_Program;
2804
2805 ----------------
2806 -- Shared_Lib --
2807 ----------------
2808
2809 function Shared_Lib (Name : String) return String is
2810 Library : String (1 .. Name'Length + Library_Version'Length + 3);
2811 -- 3 = 2 for "-l" + 1 for "-" before lib version
2812
2813 begin
2814 Library (1 .. 2) := "-l";
2815 Library (3 .. 2 + Name'Length) := Name;
2816 Library (3 + Name'Length) := '-';
2817 Library (4 + Name'Length .. Library'Last) := Library_Version;
2818 return Library;
2819 end Shared_Lib;
2820
2821 ----------------------
2822 -- Smart_File_Stamp --
2823 ----------------------
2824
2825 function Smart_File_Stamp
2826 (N : File_Name_Type;
2827 T : File_Type) return Time_Stamp_Type
2828 is
2829 File : File_Name_Type;
2830 Attr : aliased File_Attributes;
2831
2832 begin
2833 if not File_Cache_Enabled then
2834 Find_File (N, T, File, Attr'Access);
2835 else
2836 Smart_Find_File (N, T, File, Attr);
2837 end if;
2838
2839 if File = No_File then
2840 return Empty_Time_Stamp;
2841 else
2842 Get_Name_String (File);
2843 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2844 return
2845 OS_Time_To_GNAT_Time
2846 (File_Time_Stamp (Name_Buffer'Address, Attr'Access));
2847 end if;
2848 end Smart_File_Stamp;
2849
2850 ---------------------
2851 -- Smart_Find_File --
2852 ---------------------
2853
2854 function Smart_Find_File
2855 (N : File_Name_Type;
2856 T : File_Type) return File_Name_Type
2857 is
2858 File : File_Name_Type;
2859 Attr : File_Attributes;
2860 begin
2861 Smart_Find_File (N, T, File, Attr);
2862 return File;
2863 end Smart_Find_File;
2864
2865 ---------------------
2866 -- Smart_Find_File --
2867 ---------------------
2868
2869 procedure Smart_Find_File
2870 (N : File_Name_Type;
2871 T : File_Type;
2872 Found : out File_Name_Type;
2873 Attr : out File_Attributes)
2874 is
2875 Info : File_Info_Cache;
2876
2877 begin
2878 if not File_Cache_Enabled then
2879 Find_File (N, T, Info.File, Info.Attr'Access);
2880
2881 else
2882 Info := File_Name_Hash_Table.Get (N);
2883
2884 if Info.File = No_File then
2885 Find_File (N, T, Info.File, Info.Attr'Access);
2886 File_Name_Hash_Table.Set (N, Info);
2887 end if;
2888 end if;
2889
2890 Found := Info.File;
2891 Attr := Info.Attr;
2892 end Smart_Find_File;
2893
2894 ----------------------
2895 -- Source_File_Data --
2896 ----------------------
2897
2898 procedure Source_File_Data (Cache : Boolean) is
2899 begin
2900 File_Cache_Enabled := Cache;
2901 end Source_File_Data;
2902
2903 -----------------------
2904 -- Source_File_Stamp --
2905 -----------------------
2906
2907 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
2908 begin
2909 return Smart_File_Stamp (N, Source);
2910 end Source_File_Stamp;
2911
2912 ---------------------
2913 -- Strip_Directory --
2914 ---------------------
2915
2916 function Strip_Directory (Name : File_Name_Type) return File_Name_Type is
2917 begin
2918 Get_Name_String (Name);
2919
2920 for J in reverse 1 .. Name_Len - 1 loop
2921
2922 -- If we find the last directory separator
2923
2924 if Is_Directory_Separator (Name_Buffer (J)) then
2925
2926 -- Return part of Name that follows this last directory separator
2927
2928 return Name_Find (Name_Buffer (J + 1 .. Name_Len));
2929 end if;
2930 end loop;
2931
2932 -- There were no directory separator, just return Name
2933
2934 return Name;
2935 end Strip_Directory;
2936
2937 ------------------
2938 -- Strip_Suffix --
2939 ------------------
2940
2941 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type is
2942 begin
2943 Get_Name_String (Name);
2944
2945 for J in reverse 2 .. Name_Len loop
2946
2947 -- If we found the last '.', return part of Name that precedes it
2948
2949 if Name_Buffer (J) = '.' then
2950 Name_Len := J - 1;
2951 return Name_Enter;
2952 end if;
2953 end loop;
2954
2955 return Name;
2956 end Strip_Suffix;
2957
2958 ---------------------------
2959 -- To_Canonical_File_List --
2960 ---------------------------
2961
2962 function To_Canonical_File_List
2963 (Wildcard_Host_File : String;
2964 Only_Dirs : Boolean) return String_Access_List_Access
2965 is
2966 function To_Canonical_File_List_Init
2967 (Host_File : Address;
2968 Only_Dirs : Integer) return Integer;
2969 pragma Import (C, To_Canonical_File_List_Init,
2970 "__gnat_to_canonical_file_list_init");
2971
2972 function To_Canonical_File_List_Next return Address;
2973 pragma Import (C, To_Canonical_File_List_Next,
2974 "__gnat_to_canonical_file_list_next");
2975
2976 procedure To_Canonical_File_List_Free;
2977 pragma Import (C, To_Canonical_File_List_Free,
2978 "__gnat_to_canonical_file_list_free");
2979
2980 Num_Files : Integer;
2981 C_Wildcard_Host_File : String (1 .. Wildcard_Host_File'Length + 1);
2982
2983 begin
2984 C_Wildcard_Host_File (1 .. Wildcard_Host_File'Length) :=
2985 Wildcard_Host_File;
2986 C_Wildcard_Host_File (C_Wildcard_Host_File'Last) := ASCII.NUL;
2987
2988 -- Do the expansion and say how many there are
2989
2990 Num_Files := To_Canonical_File_List_Init
2991 (C_Wildcard_Host_File'Address, Boolean'Pos (Only_Dirs));
2992
2993 declare
2994 Canonical_File_List : String_Access_List (1 .. Num_Files);
2995 Canonical_File_Addr : Address;
2996 Canonical_File_Len : CRTL.size_t;
2997
2998 begin
2999 -- Retrieve the expanded directory names and build the list
3000
3001 for J in 1 .. Num_Files loop
3002 Canonical_File_Addr := To_Canonical_File_List_Next;
3003 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
3004 Canonical_File_List (J) := To_Path_String_Access
3005 (Canonical_File_Addr, Canonical_File_Len);
3006 end loop;
3007
3008 -- Free up the storage
3009
3010 To_Canonical_File_List_Free;
3011
3012 return new String_Access_List'(Canonical_File_List);
3013 end;
3014 end To_Canonical_File_List;
3015
3016 ----------------------
3017 -- To_Host_Dir_Spec --
3018 ----------------------
3019
3020 function To_Host_Dir_Spec
3021 (Canonical_Dir : String;
3022 Prefix_Style : Boolean) return String_Access
3023 is
3024 function To_Host_Dir_Spec
3025 (Canonical_Dir : Address;
3026 Prefix_Flag : Integer) return Address;
3027 pragma Import (C, To_Host_Dir_Spec, "__gnat_to_host_dir_spec");
3028
3029 C_Canonical_Dir : String (1 .. Canonical_Dir'Length + 1);
3030 Host_Dir_Addr : Address;
3031 Host_Dir_Len : CRTL.size_t;
3032
3033 begin
3034 C_Canonical_Dir (1 .. Canonical_Dir'Length) := Canonical_Dir;
3035 C_Canonical_Dir (C_Canonical_Dir'Last) := ASCII.NUL;
3036
3037 if Prefix_Style then
3038 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 1);
3039 else
3040 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 0);
3041 end if;
3042 Host_Dir_Len := C_String_Length (Host_Dir_Addr);
3043
3044 if Host_Dir_Len = 0 then
3045 return null;
3046 else
3047 return To_Path_String_Access (Host_Dir_Addr, Host_Dir_Len);
3048 end if;
3049 end To_Host_Dir_Spec;
3050
3051 -----------------------
3052 -- To_Host_File_Spec --
3053 -----------------------
3054
3055 function To_Host_File_Spec
3056 (Canonical_File : String) return String_Access
3057 is
3058 function To_Host_File_Spec (Canonical_File : Address) return Address;
3059 pragma Import (C, To_Host_File_Spec, "__gnat_to_host_file_spec");
3060
3061 C_Canonical_File : String (1 .. Canonical_File'Length + 1);
3062 Host_File_Addr : Address;
3063 Host_File_Len : CRTL.size_t;
3064
3065 begin
3066 C_Canonical_File (1 .. Canonical_File'Length) := Canonical_File;
3067 C_Canonical_File (C_Canonical_File'Last) := ASCII.NUL;
3068
3069 Host_File_Addr := To_Host_File_Spec (C_Canonical_File'Address);
3070 Host_File_Len := C_String_Length (Host_File_Addr);
3071
3072 if Host_File_Len = 0 then
3073 return null;
3074 else
3075 return To_Path_String_Access
3076 (Host_File_Addr, Host_File_Len);
3077 end if;
3078 end To_Host_File_Spec;
3079
3080 ---------------------------
3081 -- To_Path_String_Access --
3082 ---------------------------
3083
3084 function To_Path_String_Access
3085 (Path_Addr : Address;
3086 Path_Len : CRTL.size_t) return String_Access
3087 is
3088 subtype Path_String is String (1 .. Integer (Path_Len));
3089 type Path_String_Access is access Path_String;
3090
3091 function Address_To_Access is new
3092 Ada.Unchecked_Conversion (Source => Address,
3093 Target => Path_String_Access);
3094
3095 Path_Access : constant Path_String_Access :=
3096 Address_To_Access (Path_Addr);
3097
3098 Return_Val : String_Access;
3099
3100 begin
3101 Return_Val := new String (1 .. Integer (Path_Len));
3102
3103 for J in 1 .. Integer (Path_Len) loop
3104 Return_Val (J) := Path_Access (J);
3105 end loop;
3106
3107 return Return_Val;
3108 end To_Path_String_Access;
3109
3110 -----------------
3111 -- Update_Path --
3112 -----------------
3113
3114 function Update_Path (Path : String_Ptr) return String_Ptr is
3115
3116 function C_Update_Path (Path, Component : Address) return Address;
3117 pragma Import (C, C_Update_Path, "update_path");
3118
3119 In_Length : constant Integer := Path'Length;
3120 In_String : String (1 .. In_Length + 1);
3121 Component_Name : aliased String := "GCC" & ASCII.NUL;
3122 Result_Ptr : Address;
3123 Result_Length : CRTL.size_t;
3124 Out_String : String_Ptr;
3125
3126 begin
3127 In_String (1 .. In_Length) := Path.all;
3128 In_String (In_Length + 1) := ASCII.NUL;
3129 Result_Ptr := C_Update_Path (In_String'Address, Component_Name'Address);
3130 Result_Length := CRTL.strlen (Result_Ptr);
3131
3132 Out_String := new String (1 .. Integer (Result_Length));
3133 CRTL.strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
3134 return Out_String;
3135 end Update_Path;
3136
3137 ----------------
3138 -- Write_Info --
3139 ----------------
3140
3141 procedure Write_Info (Info : String) is
3142 begin
3143 Write_With_Check (Info'Address, Info'Length);
3144 Write_With_Check (EOL'Address, 1);
3145 end Write_Info;
3146
3147 ------------------------
3148 -- Write_Program_Name --
3149 ------------------------
3150
3151 procedure Write_Program_Name is
3152 Save_Buffer : constant String (1 .. Name_Len) :=
3153 Name_Buffer (1 .. Name_Len);
3154
3155 begin
3156 Find_Program_Name;
3157
3158 -- Convert the name to lower case so error messages are the same on
3159 -- all systems.
3160
3161 for J in 1 .. Name_Len loop
3162 if Name_Buffer (J) in 'A' .. 'Z' then
3163 Name_Buffer (J) :=
3164 Character'Val (Character'Pos (Name_Buffer (J)) + 32);
3165 end if;
3166 end loop;
3167
3168 Write_Str (Name_Buffer (1 .. Name_Len));
3169
3170 -- Restore Name_Buffer which was clobbered by the call to
3171 -- Find_Program_Name
3172
3173 Name_Len := Save_Buffer'Last;
3174 Name_Buffer (1 .. Name_Len) := Save_Buffer;
3175 end Write_Program_Name;
3176
3177 ----------------------
3178 -- Write_With_Check --
3179 ----------------------
3180
3181 procedure Write_With_Check (A : Address; N : Integer) is
3182 Ignore : Boolean;
3183 begin
3184 if N = Write (Output_FD, A, N) then
3185 return;
3186 else
3187 Write_Str ("error: disk full writing ");
3188 Write_Name_Decoded (Output_File_Name);
3189 Write_Eol;
3190 Name_Len := Name_Len + 1;
3191 Name_Buffer (Name_Len) := ASCII.NUL;
3192 Delete_File (Name_Buffer'Address, Ignore);
3193 Exit_Program (E_Fatal);
3194 end if;
3195 end Write_With_Check;
3196
3197 ----------------------------
3198 -- Package Initialization --
3199 ----------------------------
3200
3201 procedure Reset_File_Attributes (Attr : System.Address);
3202 pragma Import (C, Reset_File_Attributes, "__gnat_reset_attributes");
3203
3204 begin
3205 Initialization : declare
3206
3207 function Get_Default_Identifier_Character_Set return Character;
3208 pragma Import (C, Get_Default_Identifier_Character_Set,
3209 "__gnat_get_default_identifier_character_set");
3210 -- Function to determine the default identifier character set,
3211 -- which is system dependent. See Opt package spec for a list of
3212 -- the possible character codes and their interpretations.
3213
3214 function Get_Maximum_File_Name_Length return Int;
3215 pragma Import (C, Get_Maximum_File_Name_Length,
3216 "__gnat_get_maximum_file_name_length");
3217 -- Function to get maximum file name length for system
3218
3219 Sizeof_File_Attributes : Integer;
3220 pragma Import (C, Sizeof_File_Attributes,
3221 "__gnat_size_of_file_attributes");
3222
3223 begin
3224 pragma Assert (Sizeof_File_Attributes <= File_Attributes_Size);
3225
3226 Reset_File_Attributes (Unknown_Attributes'Address);
3227
3228 Identifier_Character_Set := Get_Default_Identifier_Character_Set;
3229 Maximum_File_Name_Length := Get_Maximum_File_Name_Length;
3230
3231 -- Following should be removed by having above function return
3232 -- Integer'Last as indication of no maximum instead of -1 ???
3233
3234 if Maximum_File_Name_Length = -1 then
3235 Maximum_File_Name_Length := Int'Last;
3236 end if;
3237
3238 Src_Search_Directories.Set_Last (Primary_Directory);
3239 Src_Search_Directories.Table (Primary_Directory) := new String'("");
3240
3241 Lib_Search_Directories.Set_Last (Primary_Directory);
3242 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
3243
3244 Osint.Initialize;
3245 end Initialization;
3246
3247 end Osint;