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