1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This package contains the low level, operating system routines used in
28 -- the GNAT compiler and binder for command line processing and file input
31 with GNAT.OS_Lib; use GNAT.OS_Lib;
32 with System; use System;
33 with Types; use Types;
35 pragma Elaborate (GNAT.OS_Lib);
39 Ada_Include_Path : constant String := "ADA_INCLUDE_PATH";
40 Ada_Objects_Path : constant String := "ADA_OBJECTS_PATH";
41 Project_Include_Path_File : constant String := "ADA_PRJ_INCLUDE_FILE";
42 Project_Objects_Path_File : constant String := "ADA_PRJ_OBJECTS_FILE";
45 -- Initialize internal tables
47 function Normalize_Directory_Name (Directory : String) return String_Ptr;
48 -- Verify and normalize a directory name. If directory name is invalid,
49 -- this will return an empty string. Otherwise it will insure a trailing
50 -- slash and make other normalizations.
52 type File_Type is (Source, Library, Config, Definition, Preprocessing_Data);
57 return File_Name_Type;
58 -- Finds a source, library or config file depending on the value
59 -- of T following the directory search order rules unless N is the
60 -- name of the file just read with Next_Main_File and already
61 -- contains directiory information, in which case just look in the
62 -- Primary_Directory. Returns File_Name_Type of the full file name
63 -- if found, No_File if file not found. Note that for the special
64 -- case of gnat.adc, only the compilation environment directory is
65 -- searched, i.e. the directory where the ali and object files are
66 -- written. Another special case is when Debug_Generated_Code is
67 -- set and the file name ends on ".dg", in which case we look for
68 -- the generated file only in the current directory, since that is
69 -- where it is always built.
71 function Get_File_Names_Case_Sensitive return Int;
72 pragma Import (C, Get_File_Names_Case_Sensitive,
73 "__gnat_get_file_names_case_sensitive");
74 File_Names_Case_Sensitive : constant Boolean :=
75 Get_File_Names_Case_Sensitive /= 0;
76 -- Set to indicate whether the operating system convention is for file
77 -- names to be case sensitive (e.g., in Unix, set True), or non case
78 -- sensitive (e.g., in OS/2, set False).
80 procedure Canonical_Case_File_Name (S : in out String);
81 -- Given a file name, converts it to canonical case form. For systems
82 -- where file names are case sensitive, this procedure has no effect.
83 -- If file names are not case sensitive (i.e. for example if you have
84 -- the file "xyz.adb", you can refer to it as XYZ.adb or XyZ.AdB), then
85 -- this call converts the given string to canonical all lower case form,
86 -- so that two file names compare equal if they refer to the same file.
88 function Number_Of_Files return Int;
89 -- gives the total number of filenames found on the command line.
91 procedure Add_File (File_Name : String);
92 -- Called by the subprogram processing the command line for each
95 procedure Find_Program_Name;
96 -- Put simple name of current program being run (excluding the directory
97 -- path) in Name_Buffer, with the length in Name_Len.
99 function Program_Name (Nam : String) return String_Access;
100 -- In the native compilation case, Create a string containing Nam. In
101 -- the cross compilation case, looks at the prefix of the current
102 -- program being run and prepend it to Nam. For instance if the program
103 -- being run is <target>-gnatmake and Nam is "gcc", the returned value
104 -- will be a pointer to "<target>-gcc". This function clobbers
105 -- Name_Buffer and Name_Len.
107 procedure Write_Program_Name;
108 -- Writes name of program as invoked to the current output
109 -- (normally standard output).
111 procedure Fail (S1 : String; S2 : String := ""; S3 : String := "");
112 pragma No_Return (Fail);
113 -- Outputs error messages S1 & S2 & S3 preceded by the name of the
114 -- executing program and exits with E_Fatal. The output goes to
115 -- standard error, except if special output is in effect (see Output).
117 function Is_Directory_Separator (C : Character) return Boolean;
118 -- Returns True if C is a directory separator
120 function Get_Directory (Name : File_Name_Type) return File_Name_Type;
121 -- Get the prefix directory name (if any) from Name. The last separator
122 -- is preserved. Return the normalized current directory if there is no
123 -- directory part in the name.
125 function Is_Readonly_Library (File : File_Name_Type) return Boolean;
126 -- Check if this library file is a read-only file.
128 function Strip_Directory (Name : File_Name_Type) return File_Name_Type;
129 -- Strips the prefix directory name (if any) from Name. Returns the
130 -- stripped name. Name cannot end with a directory separator.
132 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type;
133 -- Strips the suffix (the last '.' and whatever comes after it) from Name.
134 -- Returns the stripped name.
136 function Executable_Name (Name : File_Name_Type) return File_Name_Type;
137 -- Given a file name it adds the appropriate suffix at the end so that
138 -- it becomes the name of the executable on the system at end. For
139 -- instance under DOS it adds the ".exe" suffix, whereas under UNIX no
142 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type;
143 -- Returns the time stamp of file Name. Name should include relative
144 -- path information in order to locate it. If the source file cannot be
145 -- opened, or Name = No_File, and all blank time stamp is returned (this is
146 -- not an error situation).
148 type String_Access_List is array (Positive range <>) of String_Access;
149 -- Deferenced type used to return a list of file specs in
150 -- To_Canonical_File_List.
152 type String_Access_List_Access is access all String_Access_List;
153 -- Type used to return a String_Access_List without dragging in secondary
156 function To_Canonical_File_List
157 (Wildcard_Host_File : String;
159 return String_Access_List_Access;
160 -- Expand a wildcard host syntax file or directory specification (e.g. on
161 -- a VMS host, any file or directory spec that contains:
162 -- "*", or "%", or "...")
163 -- and return a list of valid Unix syntax file or directory specs.
164 -- If Only_Dirs is True, then only return directories.
166 function To_Canonical_Dir_Spec
168 Prefix_Style : Boolean)
169 return String_Access;
170 -- Convert a host syntax directory specification (e.g. on a VMS host:
171 -- "SYS$DEVICE:[DIR]") to canonical (Unix) syntax (e.g. "/sys$device/dir").
172 -- If Prefix_Style then make it a valid file specification prefix.
173 -- A file specification prefix is a directory specification that
174 -- can be appended with a simple file specification to yield a valid
175 -- absolute or relative path to a file. On a conversion to Unix syntax
176 -- this simply means the spec has a trailing slash ("/").
178 function To_Canonical_File_Spec
180 return String_Access;
181 -- Convert a host syntax file specification (e.g. on a VMS host:
182 -- "SYS$DEVICE:[DIR]FILE.EXT;69 to canonical (Unix) syntax (e.g.
183 -- "/sys$device/dir/file.ext.69").
185 function To_Canonical_Path_Spec
187 return String_Access;
188 -- Convert a host syntax Path specification (e.g. on a VMS host:
189 -- "SYS$DEVICE:[BAR],DISK$USER:[FOO] to canonical (Unix) syntax (e.g.
190 -- "/sys$device/foo:disk$user/foo").
192 function To_Host_Dir_Spec
193 (Canonical_Dir : String;
194 Prefix_Style : Boolean)
195 return String_Access;
196 -- Convert a canonical syntax directory specification to host syntax.
197 -- The Prefix_Style flag is currently ignored but should be set to
200 function To_Host_File_Spec
201 (Canonical_File : String)
202 return String_Access;
203 -- Convert a canonical syntax file specification to host syntax.
205 function Relocate_Path
207 Path : String) return String_Ptr;
208 -- Given an absolute path and a prefix, if Path starts with Prefix,
209 -- replace the Prefix substring with the root installation directory.
210 -- By default, try to compute the root installation directory by looking
211 -- at the executable name as it was typed on the command line and, if
212 -- needed, use the PATH environment variable.
213 -- If the above computation fails, return Path.
214 -- This function assumes that Prefix'First = Path'First
216 -------------------------
217 -- Search Dir Routines --
218 -------------------------
220 function Include_Dir_Default_Prefix return String;
221 -- Return the directory of the run-time library sources, as modified
224 function Object_Dir_Default_Prefix return String;
225 -- Return the directory of the run-time library ALI and object files, as
226 -- modified by update_path.
228 procedure Add_Default_Search_Dirs;
229 -- This routine adds the default search dirs indicated by the
230 -- environment variables and sdefault package.
232 procedure Add_Lib_Search_Dir (Dir : String);
233 -- Add Dir at the end of the library file search path
235 procedure Add_Src_Search_Dir (Dir : String);
236 -- Add Dir at the end of the source file search path
238 procedure Get_Next_Dir_In_Path_Init
239 (Search_Path : String_Access);
240 function Get_Next_Dir_In_Path
241 (Search_Path : String_Access)
242 return String_Access;
243 -- These subprograms are used to parse out the directory names in a
244 -- search path specified by a Search_Path argument. The procedure
245 -- initializes an internal pointer to point to the initial directory
246 -- name, and calls to the function return successive directory names,
247 -- with a null pointer marking the end of the list.
249 type Search_File_Type is (Include, Objects);
251 procedure Add_Search_Dirs
252 (Search_Path : String_Ptr;
253 Path_Type : Search_File_Type);
254 -- These procedure adds all the search directories that are in Search_Path
255 -- in the proper file search path (library or source)
257 function Get_Primary_Src_Search_Directory return String_Ptr;
258 -- Retrieved the primary directory (directory containing the main source
259 -- file for Gnatmake.
261 function Nb_Dir_In_Src_Search_Path return Natural;
262 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr;
263 -- Functions to access the directory names in the source search path
265 function Nb_Dir_In_Obj_Search_Path return Natural;
266 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr;
267 -- Functions to access the directory names in the Object search path
269 Include_Search_File : constant String_Access :=
270 new String'("ada_source_path");
271 Objects_Search_File : constant String_Access :=
272 new String'("ada_object_path");
273 -- Names of the files containg the default include or objects search
274 -- directories. These files, located in Sdefault.Search_Dir_Prefix, do
275 -- not necessarily exist.
277 function Read_Default_Search_Dirs
278 (Search_Dir_Prefix : String_Access;
279 Search_File : String_Access;
280 Search_Dir_Default_Name : String_Access)
281 return String_Access;
282 -- Read and return the default search directories from the file located
283 -- in Search_Dir_Prefix (as modified by update_path) and named Search_File.
284 -- If no such file exists or an error occurs then instead return the
285 -- Search_Dir_Default_Name (as modified by update_path).
287 function Get_RTS_Search_Dir
288 (Search_Dir : String;
289 File_Type : Search_File_Type)
291 -- This function retrieves the paths to the search (resp. lib) dirs and
292 -- return them. The search dir can be absolute or relative. If the search
293 -- dir contains Include_Search_File (resp. Object_Search_File), then this
294 -- function reads and returns the default search directories from the file.
295 -- Otherwise, if the directory is absolute, it will try to find 'adalib'
296 -- (resp. 'adainclude'). If found, null is returned. If the directory is
297 -- relative, the following directories for the directories 'adalib' and
298 -- 'adainclude' will be scanned:
300 -- - current directory (from which the tool has been spawned)
301 -- - $GNAT_ROOT/gcc/gcc-lib/$targ/$vers/
302 -- - $GNAT_ROOT/gcc/gcc-lib/$targ/$vers/rts-
304 -- The scan will stop as soon as the directory being searched for (adalib
305 -- or adainclude) is found. If the scan fails, null is returned.
307 -----------------------
308 -- Source File Input --
309 -----------------------
311 -- Source file input routines are used by the compiler to read the main
312 -- source files and the subsidiary source files (e.g. with'ed units), and
313 -- also by the binder to check presence/time stamps of sources.
315 procedure Read_Source_File
319 Src : out Source_Buffer_Ptr;
320 T : File_Type := Source);
321 -- Allocates a Source_Buffer of appropriate length and then reads the
322 -- entire contents of the source file N into the buffer. The address of
323 -- the allocated buffer is returned in Src.
325 -- Each line of text is terminated by one of the sequences:
332 -- The source is terminated by an EOF (16#1A#) character, which is
333 -- the last charcater of the returned source bufer (note that any
334 -- EOF characters in positions other than the last source character
335 -- are treated as representing blanks).
337 -- The logical lower bound of the source buffer is the input value of Lo,
338 -- and on exit Hi is set to the logical upper bound of the source buffer.
339 -- Note that the returned value in Src points to an array with a physical
340 -- lower bound of zero. This virtual origin addressing approach means that
341 -- a constrained array pointer can be used with a low bound of zero which
342 -- results in more efficient code.
344 -- If the given file cannot be opened, then the action depends on whether
345 -- this file is the current main unit (i.e. its name matches the name
346 -- returned by the most recent call to Next_Main_Source). If so, then the
347 -- failure to find the file is a fatal error, an error message is output,
348 -- and program execution is terminated. Otherwise (for the case of a
349 -- subsidiary source loaded directly or indirectly using with), a file
350 -- not found condition causes null to be set as the result value.
352 -- Note that the name passed to this function is the simple file name,
353 -- without any directory information. The implementation is responsible
354 -- for searching for the file in the appropriate directories.
356 -- Note the special case that if the file name is gnat.adc, then the
357 -- search for the file is done ONLY in the directory corresponding to
358 -- the current compilation environment, i.e. in the same directory
359 -- where the ali and object files will be written.
361 function Full_Source_Name return File_Name_Type;
362 function Current_Source_File_Stamp return Time_Stamp_Type;
363 -- Returns the full name/time stamp of the source file most recently read
364 -- using Read_Source_File. Calling this routine entails no source file
365 -- directory lookup penalty.
367 function Full_Source_Name (N : File_Name_Type) return File_Name_Type;
368 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type;
369 -- Returns the full name/time stamp of the source file whose simple name
370 -- is N which should not include path information. Note that if the file
371 -- cannot be located No_File is returned for the first routine and an
372 -- all blank time stamp is returned for the second (this is not an error
373 -- situation). The full name includes the appropriate directory
374 -- information. The source file directory lookup penalty is incurred
375 -- every single time the routines are called unless you have previously
376 -- called Source_File_Data (Cache => True). See below.
378 function Matching_Full_Source_Name
381 return File_Name_Type;
382 -- Same semantics than Full_Source_Name but will search on the source
383 -- path until a source file with time stamp matching T is found. If
384 -- none is found returns No_File.
386 procedure Source_File_Data (Cache : Boolean);
387 -- By default source file data (full source file name and time stamp)
388 -- are looked up every time a call to Full_Source_Name (N) or
389 -- Source_File_Stamp (N) is made. This may be undesirable in certain
390 -- applications as this is uselessly slow if source file data does not
391 -- change during program execution. When this procedure is called with
392 -- Cache => True access to source file data does not encurr a penalty if
393 -- this data was previously retrieved.
395 -------------------------------------------
396 -- Representation of Library Information --
397 -------------------------------------------
399 -- Associated with each compiled source file is library information,
400 -- a string of bytes whose exact format is described in the body of
401 -- Lib.Writ. Compiling a source file generates this library information
402 -- for the compiled unit, and access the library information for units
403 -- that were compiled previously on which the unit being compiled depends.
405 -- How this information is stored is up to the implementation of this
406 -- package. At the interface level, this information is simply associated
407 -- with its corresponding source.
409 -- Several different implementations are possible:
411 -- 1. The information could be directly associated with the source file,
412 -- e.g. placed in a resource fork of this file on the Mac, or on
413 -- MS-DOS, written to the source file after the end of file mark.
415 -- 2. The information could be written into the generated object module
416 -- if the system supports the inclusion of arbitrary informational
417 -- byte streams into object files. In this case there must be a naming
418 -- convention that allows object files to be located given the name of
419 -- the corresponding source file.
421 -- 3. The information could be written to a separate file, whose name is
422 -- related to the name of the source file by a fixed convention.
424 -- Which of these three methods is chosen depends on the constraints of the
425 -- host operating system. The interface described here is independent of
426 -- which of these approaches is used.
428 -------------------------------
429 -- Library Information Input --
430 -------------------------------
432 -- These subprograms are used by the binder to read library information
433 -- files, see section above for representation of these files.
435 function Read_Library_Info
436 (Lib_File : File_Name_Type;
437 Fatal_Err : Boolean := False)
438 return Text_Buffer_Ptr;
439 -- Allocates a Text_Buffer of appropriate length and reads in the entire
440 -- source of the library information from the library information file
441 -- whose name is given by the parameter Name.
443 -- See description of Read_Source_File for details on the format of the
444 -- returned text buffer (the format is identical). THe lower bound of
445 -- the Text_Buffer is always zero
447 -- If the specified file cannot be opened, then the action depends on
448 -- Fatal_Err. If Fatal_Err is True, an error message is given and the
449 -- compilation is abandoned. Otherwise if Fatal_Err is False, then null
450 -- is returned. Note that the Lib_File is a simple name which does not
451 -- include any directory information. The implementation is responsible
452 -- for searching for the file in appropriate directories.
454 -- If Opt.Check_Object_Consistency is set to True then this routine
455 -- checks whether the object file corresponding to the Lib_File is
456 -- consistent with it. The object file is inconsistent if the object
457 -- does not exist or if it has an older time stamp than Lib_File.
458 -- This check is not performed when the Lib_File is "locked" (i.e.
459 -- read/only) because in this case the object file may be buried
460 -- in a library. In case of inconsistencies Read_Library_Info
461 -- behaves as if it did not find Lib_File (namely if Fatal_Err is
462 -- False, null is returned).
464 function Full_Library_Info_Name return File_Name_Type;
465 function Full_Object_File_Name return File_Name_Type;
466 -- Returns the full name of the library/object file most recently read
467 -- using Read_Library_Info, including appropriate directory information.
468 -- Calling this routine entails no library file directory lookup
469 -- penalty. Note that the object file corresponding to a library file
470 -- is not actually read. Its time stamp is fected when the flag
471 -- Opt.Check_Object_Consistency is set.
473 function Current_Library_File_Stamp return Time_Stamp_Type;
474 function Current_Object_File_Stamp return Time_Stamp_Type;
475 -- The time stamps of the files returned by the previous two routines.
476 -- It is an error to call Current_Object_File_Stamp if
477 -- Opt.Check_Object_Consistency is set to False.
479 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type;
480 function Library_File_Stamp (N : File_Name_Type) return Time_Stamp_Type;
481 -- Returns the full name/time stamp of library file N. N should not
482 -- include path information. Note that if the file cannot be located
483 -- No_File is returned for the first routine and an all blank time stamp
484 -- is returned for the second (this is not an error situation). The
485 -- full name includes the appropriate directory information. The library
486 -- file directory lookup penalty is incurred every single time this
487 -- routine is called.
489 function Lib_File_Name (Source_File : File_Name_Type) return File_Name_Type;
490 -- Given the name of a source file, returns the name of the corresponding
491 -- library information file. This may be the name of the object file, or
492 -- of a separate file used to store the library information. In either case
493 -- the returned result is suitable for use in a call to Read_Library_Info.
494 -- Note: this subprogram is in this section because it is used by the
495 -- compiler to determine the proper library information names to be placed
496 -- in the generated library information file.
502 type Exit_Code_Type is (
503 E_Success, -- No warnings or errors
504 E_Warnings, -- Compiler warnings generated
505 E_No_Code, -- No code generated
506 E_No_Compile, -- Compilation not needed (smart recompilation)
507 E_Errors, -- Compiler error messages generated
508 E_Fatal, -- Fatal (serious) error, e.g. source file not found
509 E_Abort); -- Internally detected compiler error
511 procedure Exit_Program (Exit_Code : Exit_Code_Type);
512 pragma No_Return (Exit_Program);
513 -- A call to Exit_Program terminates execution with the given status.
514 -- A status of zero indicates normal completion, a non-zero status
515 -- indicates abnormal termination.
517 -------------------------
518 -- Command Line Access --
519 -------------------------
521 -- Direct interface to command line parameters. (We don't want to use
522 -- the predefined command line package because it defines functions
525 function Arg_Count return Natural;
526 pragma Import (C, Arg_Count, "__gnat_arg_count");
527 -- Get number of arguments (note: optional globbing may be enabled)
529 procedure Fill_Arg (A : System.Address; Arg_Num : Integer);
530 pragma Import (C, Fill_Arg, "__gnat_fill_arg");
531 -- Store one argument
533 function Len_Arg (Arg_Num : Integer) return Integer;
534 pragma Import (C, Len_Arg, "__gnat_len_arg");
535 -- Get length of argument
539 ALI_Suffix : constant String_Ptr := new String'("ali");
540 -- The suffix used for the library files (also known as ALI files).
542 Current_Main : File_Name_Type := No_File;
543 -- Used to save a simple file name between calls to Next_Main_Source and
544 -- Read_Source_File. If the file name argument to Read_Source_File is
545 -- No_File, that indicates that the file whose name was returned by the
546 -- last call to Next_Main_Source (and stored here) is to be read.
548 Object_Suffix : constant String := Get_Object_Suffix.all;
549 -- The suffix used for the object files.
551 Output_FD : File_Descriptor;
552 -- The file descriptor for the current library info, tree or binder output
554 Output_File_Name : File_Name_Type;
555 -- File_Name_Type for name of open file whose FD is in Output_FD, the name
556 -- stored does not include the trailing NUL character.
558 Argument_Count : constant Integer := Arg_Count - 1;
559 -- Number of arguments (excluding program name)
561 type File_Name_Array is array (Int range <>) of String_Ptr;
562 type File_Name_Array_Ptr is access File_Name_Array;
563 File_Names : File_Name_Array_Ptr :=
564 new File_Name_Array (1 .. Int (Argument_Count) + 2);
565 -- As arguments are scanned, file names are stored in this array
566 -- The strings do not have terminating NUL files. The array is
567 -- extensible, because when using project files, there may be
568 -- more files than arguments on the command line.
570 Current_File_Name_Index : Int := 0;
571 -- The index in File_Names of the last file opened by Next_Main_Source
572 -- or Next_Main_Lib_File. The value 0 indicates that no files have been
575 procedure Create_File_And_Check
576 (Fdesc : out File_Descriptor;
578 -- Create file whose name (NUL terminated) is in Name_Buffer (with the
579 -- length in Name_Len), and place the resulting descriptor in Fdesc.
580 -- Issue message and exit with fatal error if file cannot be created.
581 -- The Fmode parameter is set to either Text or Binary (see description
582 -- of GNAT.OS_Lib.Create_File).
584 type Program_Type is (Compiler, Binder, Make, Gnatls, Unspecified);
585 -- Program currently running
586 procedure Set_Program (P : Program_Type);
587 -- Indicates to the body of Osint the program currently running.
588 -- This procedure is called by the child packages of Osint.
589 -- A check is made that this procedure is not called several times.
591 function More_Files return Boolean;
592 -- Implements More_Source_Files and More_Lib_Files.
594 function Next_Main_File return File_Name_Type;
595 -- Implements Next_Main_Source and Next_Main_Lib_File.
597 function Object_File_Name (N : File_Name_Type) return File_Name_Type;
598 -- Constructs the name of the object file corresponding to library
599 -- file N. If N is a full file name than the returned file name will
600 -- also be a full file name. Note that no lookup in the library file
601 -- directories is done for this file. This routine merely constructs
604 procedure Write_Info (Info : String);
605 -- Implementation of Write_Binder_Info, Write_Debug_Info and
606 -- Write_Library_Info (identical)