1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                                O S I N T                                 --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2021, 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--  This package contains the low level, operating system routines used in the
27--  compiler and binder for command line processing and file input output.
28
29with Namet; use Namet;
30with Types; use Types;
31
32with System; use System;
33
34pragma Warnings (Off);
35--  This package is used also by gnatcoll
36with System.OS_Lib; use System.OS_Lib;
37pragma Warnings (On);
38
39with System.Storage_Elements;
40
41pragma Elaborate_All (System.OS_Lib);
42--  For the call to function Get_Target_Object_Suffix in the private part
43
44package Osint is
45
46   Multi_Unit_Index_Character : constant Character := '~';
47   --  The character before the index of the unit in a multi-unit source in ALI
48   --  and object file names.
49
50   Ada_Include_Path          : constant String := "ADA_INCLUDE_PATH";
51   Ada_Objects_Path          : constant String := "ADA_OBJECTS_PATH";
52   Project_Include_Path_File : constant String := "ADA_PRJ_INCLUDE_FILE";
53   Project_Objects_Path_File : constant String := "ADA_PRJ_OBJECTS_FILE";
54
55   Output_FD : File_Descriptor;
56   --  File descriptor for current library info, list, tree, C, H, or binder
57   --  output. Only one of these is open at a time, so we need only one FD.
58
59   On_Windows : constant Boolean := Directory_Separator = '\';
60   --  True when on Windows
61
62   procedure Initialize;
63   --  Initialize internal tables
64
65   function Normalize_Directory_Name (Directory : String) return String_Ptr;
66   --  Verify and normalize a directory name. If directory name is invalid,
67   --  this will return an empty string. Otherwise it will insure a trailing
68   --  slash and make other normalizations.
69
70   type File_Type is (Source, Library, Config, Definition, Preprocessing_Data);
71
72   function Find_File
73     (N         : File_Name_Type;
74      T         : File_Type;
75      Full_Name : Boolean := False) return File_Name_Type;
76   --  Finds a source, library or config file depending on the value of T
77   --  following the directory search order rules unless N is the name of the
78   --  file just read with Next_Main_File and already contains directory
79   --  information, in which case just look in the Primary_Directory. Returns
80   --  File_Name_Type of the full file name if found, No_File if file not
81   --  found. Note that for the special case of gnat.adc, only the compilation
82   --  environment directory is searched, i.e. the directory where the ali and
83   --  object files are written. Another special case is Debug_Generated_Code
84   --  set and the file name ends in ".dg", in which case we look for the
85   --  generated file only in the current directory, since that is where it is
86   --  always built.
87   --
88   --  In the case of configuration files, full path names are needed for some
89   --  ASIS queries. The flag Full_Name indicates that the name of the file
90   --  should be normalized to include a full path.
91
92   function Get_File_Names_Case_Sensitive return Int;
93   pragma Import (C, Get_File_Names_Case_Sensitive,
94                  "__gnat_get_file_names_case_sensitive");
95   File_Names_Case_Sensitive : constant Boolean :=
96                                 Get_File_Names_Case_Sensitive /= 0;
97   --  Set to indicate whether the operating system convention is for file
98   --  names to be case sensitive (e.g., in Unix, set True), or non case
99   --  sensitive (e.g., in Windows, set False).
100
101   procedure Canonical_Case_File_Name (S : in out String);
102   --  Given a file name, converts it to canonical case form. For systems
103   --  where file names are case sensitive, this procedure has no effect.
104   --  If file names are not case sensitive (i.e. for example if you have
105   --  the file "xyz.adb", you can refer to it as XYZ.adb or XyZ.AdB), then
106   --  this call converts the given string to canonical all lower case form,
107   --  so that two file names compare equal if they refer to the same file.
108
109   function Get_Env_Vars_Case_Sensitive return Int;
110   pragma Import (C, Get_Env_Vars_Case_Sensitive,
111                  "__gnat_get_env_vars_case_sensitive");
112   Env_Vars_Case_Sensitive : constant Boolean :=
113                                 Get_Env_Vars_Case_Sensitive /= 0;
114   --  Set to indicate whether the operating system convention is for
115   --  environment variable names to be case sensitive (e.g., in Unix, set
116   --  True), or non case sensitive (e.g., in Windows, set False).
117
118   procedure Canonical_Case_Env_Var_Name (S : in out String);
119   --  Given an environment variable name, converts it to canonical case form.
120   --  For systems where environment variable names are case sensitive, this
121   --  procedure has no effect. If environment variable names are not case
122   --  sensitive, then this call converts the given string to canonical all
123   --  lower case form, so that two environment variable names compare equal if
124   --  they refer to the same environment variable.
125
126   function Number_Of_Files return Nat;
127   --  Gives the total number of filenames found on the command line
128
129   No_Index : constant := -1;
130   --  Value used in Add_File to indicate no index is specified for main
131
132   procedure Add_File (File_Name : String; Index : Int := No_Index);
133   --  Called by the subprogram processing the command line for each file name
134   --  found. The index, when not defaulted to No_Index is the index of the
135   --  subprogram in its source, zero indicating that the source is not
136   --  multi-unit.
137
138   procedure Find_Program_Name;
139   --  Put simple name of current program being run (excluding the directory
140   --  path) in Name_Buffer, with the length in Name_Len.
141
142   function Program_Name (Nam : String; Prog : String) return String_Access;
143   --  In the native compilation case, creates a string containing Nam. In the
144   --  cross compilation case, looks at the prefix of the current program being
145   --  run and prepends it to Nam. For instance if the program being run is
146   --  <target>-gnatmake and Nam is "gcc", the returned value will be a pointer
147   --  to "<target>-gcc". This function clobbers Name_Buffer and Name_Len.
148   --  Also looks at any suffix, e.g. gnatmake-4.1 -> "gcc-4.1". Prog is the
149   --  default name of the current program being executed, e.g. "gnatmake",
150   --  "gnatlink".
151
152   procedure Write_Program_Name;
153   --  Writes name of program as invoked to the current output (normally
154   --  standard output).
155
156   procedure Fail (S : String);
157   pragma No_Return (Fail);
158   --  Outputs error message S preceded by the name of the executing program
159   --  and exits with E_Fatal. The output goes to standard error, except if
160   --  special output is in effect (see Output).
161
162   function Is_Directory_Separator (C : Character) return Boolean;
163   --  Returns True if C is a directory separator
164
165   function Get_Directory (Name : File_Name_Type) return File_Name_Type;
166   --  Get the prefix directory name (if any) from Name. The last separator
167   --  is preserved. Return the normalized current directory if there is no
168   --  directory part in the name.
169
170   function Is_Readonly_Library (File : File_Name_Type) return Boolean;
171   --  Check if this library file is a read-only file
172
173   function Strip_Directory (Name : File_Name_Type) return File_Name_Type;
174   --  Strips the prefix directory name (if any) from Name. Returns the
175   --  stripped name. Name cannot end with a directory separator.
176
177   function Strip_Suffix (Name : File_Name_Type) return File_Name_Type;
178   --  Strips the suffix (the last '.' and whatever comes after it) from Name.
179   --  Returns the stripped name.
180
181   function Executable_Name
182     (Name              : File_Name_Type;
183      Only_If_No_Suffix : Boolean := False) return File_Name_Type;
184   --  Given a file name it adds the appropriate suffix at the end so that
185   --  it becomes the name of the executable on the system at end. For
186   --  instance under DOS it adds the ".exe" suffix, whereas under UNIX no
187   --  suffix is added.
188
189   function Executable_Name
190     (Name              : String;
191      Only_If_No_Suffix : Boolean := False) return String;
192   --  Same as above, with String parameters
193
194   function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type;
195   --  Returns the time stamp of file Name. Name should include relative path
196   --  information in order to locate it. If the source file cannot be opened,
197   --  or Name = No_File, and all blank time stamp is returned (this is not an
198   --  error situation).
199
200   function File_Stamp (Name : Path_Name_Type) return Time_Stamp_Type;
201   --  Same as above for a path name
202
203   type String_Access_List is array (Positive range <>) of String_Access;
204   --  Dereferenced type used to return a list of file specs in
205   --  To_Canonical_File_List.
206
207   type String_Access_List_Access is access all String_Access_List;
208   --  Type used to return a String_Access_List without dragging in secondary
209   --  stack.
210
211   function To_Canonical_File_List
212     (Wildcard_Host_File : String;
213      Only_Dirs          : Boolean) return String_Access_List_Access;
214   --  Expand a wildcard host syntax file or directory specification and return
215   --  a list of valid Unix syntax file or directory specs. If Only_Dirs is
216   --  True, then only return directories.
217
218   function To_Host_Dir_Spec
219     (Canonical_Dir : String;
220      Prefix_Style  : Boolean) return String_Access;
221   --  Convert a canonical syntax directory specification to host syntax. The
222   --  Prefix_Style flag is currently ignored but should be set to False.
223   --  Note that the caller must free result.
224
225   function To_Host_File_Spec
226     (Canonical_File : String) return String_Access;
227   --  Convert a canonical syntax file specification to host syntax
228
229   function Relocate_Path
230     (Prefix : String;
231      Path   : String) return String_Ptr;
232   --  Given an absolute path and a prefix, if Path starts with Prefix,
233   --  replace the Prefix substring with the root installation directory.
234   --  By default, try to compute the root installation directory by looking
235   --  at the executable name as it was typed on the command line and, if
236   --  needed, use the PATH environment variable. If the above computation
237   --  fails, return Path. This function assumes Prefix'First = Path'First.
238
239   function Shared_Lib (Name : String) return String;
240   --  Returns the runtime shared library in the form -l<name>-<version> where
241   --  version is the GNAT runtime library option for the platform. For example
242   --  this routine called with Name set to "gnat" will return "-lgnat-5.02"
243   --  on UNIX and Windows.
244
245   ---------------------
246   -- File attributes --
247   ---------------------
248
249   --  The following subprograms offer services similar to those found in
250   --  System.OS_Lib, but with the ability to extra multiple information from
251   --  a single system call, depending on the system. This can result in fewer
252   --  system calls when reused.
253
254   --  In all these subprograms, the requested value is either read from the
255   --  File_Attributes parameter (resulting in no system call), or computed
256   --  from the disk and then cached in the File_Attributes parameter (possibly
257   --  along with other values).
258
259   File_Attributes_Size : constant Natural := 32;
260   --  This should be big enough to fit a "struct file_attributes" on any
261   --  system. It doesn't cause any malfunction if it is too big (which avoids
262   --  the need for either mapping the struct exactly or importing the sizeof
263   --  from C, which would result in dynamic code). However, it does waste
264   --  space (e.g. when a component of this type appears in a record, if it is
265   --  unnecessarily large). Note: for runtime units, use System.OS_Constants.
266   --  SIZEOF_struct_file_attributes instead, which has the exact value.
267
268   type File_Attributes is
269     array (1 .. File_Attributes_Size)
270       of System.Storage_Elements.Storage_Element;
271   for File_Attributes'Alignment use Standard'Maximum_Alignment;
272
273   Unknown_Attributes : File_Attributes;
274   --  A cache for various attributes for a file (length, accessibility,...)
275   --  Will be initialized properly at elaboration (for efficiency later on,
276   --  avoid function calls every time we want to reset the attributes) prior
277   --  to the first usage. We cannot make it constant since the compiler may
278   --  put it in a read-only section.
279
280   function Is_Directory
281     (Name : C_File_Name;
282      Attr : access File_Attributes) return Boolean;
283   function Is_Regular_File
284     (Name : C_File_Name;
285      Attr : access File_Attributes) return Boolean;
286   function Is_Symbolic_Link
287     (Name : C_File_Name;
288      Attr : access File_Attributes) return Boolean;
289   --  Return the type of the file,
290
291   function File_Length
292     (Name : C_File_Name;
293      Attr : access File_Attributes) return Long_Integer;
294   --  Return the length (number of bytes) of the file
295
296   function File_Time_Stamp
297     (Name : C_File_Name;
298      Attr : access File_Attributes) return OS_Time;
299   function File_Time_Stamp
300     (Name : Path_Name_Type;
301      Attr : access File_Attributes) return Time_Stamp_Type;
302   --  Return the time stamp of the file
303
304   function Is_Readable_File
305     (Name : C_File_Name;
306      Attr : access File_Attributes) return Boolean;
307   function Is_Executable_File
308     (Name : C_File_Name;
309      Attr : access File_Attributes) return Boolean;
310   function Is_Writable_File
311     (Name : C_File_Name;
312      Attr : access File_Attributes) return Boolean;
313   --  Return the access rights for the file
314
315   -------------------------
316   -- Search Dir Routines --
317   -------------------------
318
319   function Include_Dir_Default_Prefix return String;
320   --  Return the directory of the run-time library sources, as modified
321   --  by update_path.
322
323   function Object_Dir_Default_Prefix return String;
324   --  Return the directory of the run-time library ALI and object files, as
325   --  modified by update_path.
326
327   procedure Add_Default_Search_Dirs;
328   --  This routine adds the default search dirs indicated by the environment
329   --  variables and sdefault package, as well as the library search dirs set
330   --  by option -gnateO for GNAT2WHY.
331
332   procedure Add_Lib_Search_Dir (Dir : String);
333   --  Add Dir at the end of the library file search path
334
335   procedure Add_Src_Search_Dir (Dir : String);
336   --  Add Dir at the end of the source file search path
337
338   procedure Get_Next_Dir_In_Path_Init
339     (Search_Path : String_Access);
340   function Get_Next_Dir_In_Path
341     (Search_Path : String_Access) return String_Access;
342   --  These subprograms are used to parse out the directory names in a search
343   --  path specified by a Search_Path argument. The procedure initializes an
344   --  internal pointer to point to the initial directory name, and calls to
345   --  the function return successive directory names, with a null pointer
346   --  marking the end of the list.
347
348   type Search_File_Type is (Include, Objects);
349
350   procedure Add_Search_Dirs
351     (Search_Path : String_Ptr;
352      Path_Type   : Search_File_Type);
353   --  These procedure adds all the search directories that are in Search_Path
354   --  in the proper file search path (library or source)
355
356   function Get_Primary_Src_Search_Directory return String_Ptr;
357   --  Retrieved the primary directory (directory containing the main source
358   --  file for Gnatmake.
359
360   function Nb_Dir_In_Src_Search_Path return Natural;
361   function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr;
362   --  Functions to access the directory names in the source search path
363
364   function Nb_Dir_In_Obj_Search_Path return Natural;
365   function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr;
366   --  Functions to access the directory names in the Object search path
367
368   Include_Search_File : constant String_Access :=
369                           new String'("ada_source_path");
370   Objects_Search_File : constant String_Access :=
371                           new String'("ada_object_path");
372   --  Names of the files containing the default include or objects search
373   --  directories. These files, located in Sdefault.Search_Dir_Prefix, do
374   --  not necessarily exist.
375
376   Exec_Name : String_Ptr;
377   --  Executable name as typed by the user (used to compute the
378   --  executable prefix).
379
380   function Read_Default_Search_Dirs
381     (Search_Dir_Prefix       : String_Access;
382      Search_File             : String_Access;
383      Search_Dir_Default_Name : String_Access) return String_Access;
384   --  Read and return the default search directories from the file located
385   --  in Search_Dir_Prefix (as modified by update_path) and named Search_File.
386   --  If no such file exists or an error occurs then instead return the
387   --  Search_Dir_Default_Name (as modified by update_path).
388
389   function Get_RTS_Search_Dir
390     (Search_Dir : String;
391      File_Type  : Search_File_Type) return String_Ptr;
392   --  This function retrieves the paths to the search (resp. lib) dirs and
393   --  return them. The search dir can be absolute or relative. If the search
394   --  dir contains Include_Search_File (resp. Object_Search_File), then this
395   --  function reads and returns the default search directories from the file.
396   --  Otherwise, if the directory is absolute, it will try to find 'adalib'
397   --  (resp. 'adainclude'). If found, null is returned. If the directory is
398   --  relative, the following directories for the directories 'adalib' and
399   --  'adainclude' will be scanned:
400   --
401   --   - current directory (from which the tool has been spawned)
402   --   - $GNAT_ROOT/gcc/gcc-lib/$targ/$vers/
403   --   - $GNAT_ROOT/gcc/gcc-lib/$targ/$vers/rts-
404   --
405   --  The scan will stop as soon as the directory being searched for (adalib
406   --  or adainclude) is found. If the scan fails, null is returned.
407
408   -----------------------
409   -- Source File Input --
410   -----------------------
411
412   --  Source file input routines are used by the compiler to read the main
413   --  source files and the subsidiary source files (e.g. with'ed units), and
414   --  also by the binder to check presence/time stamps of sources.
415
416   procedure Read_Source_File
417     (N   : File_Name_Type;
418      Lo  : Source_Ptr;
419      Hi  : out Source_Ptr;
420      Src : out Source_Buffer_Ptr;
421      FD  : out File_Descriptor;
422      T   : File_Type := Source);
423   --  Allocates a Source_Buffer of appropriate length and then reads the
424   --  entire contents of the source file N into the buffer. The address of
425   --  the allocated buffer is returned in Src. FD is used for extended error
426   --  information in the case the read fails.
427   --
428   --  Each line of text is terminated by one of the sequences:
429   --
430   --    CR
431   --    CR/LF
432   --    LF
433
434   --  The source is terminated by an EOF (16#1A#) character, which is the last
435   --  character of the returned source buffer (note that any EOF characters in
436   --  positions other than the last source character are treated as blanks).
437   --
438   --  The logical lower bound of the source buffer is the input value of Lo,
439   --  and on exit Hi is set to the logical upper bound of the source buffer,
440   --  which is redundant with Src'Last.
441   --
442   --  If the given file cannot be opened, then the action depends on whether
443   --  this file is the current main unit (i.e. its name matches the name
444   --  returned by the most recent call to Next_Main_Source). If so, then the
445   --  failure to find the file is a fatal error, an error message is output,
446   --  and program execution is terminated. Otherwise (for the case of a
447   --  subsidiary source loaded directly or indirectly using with), a file
448   --  not found condition causes null to be set as the result value and a
449   --  value of No_Source_File (0) to be set as the FD value. In the related
450   --  case of a file with no read permissions the result is the same except FD
451   --  is set to No_Access_To_Source_File (-1). Upon success FD is set to a
452   --  positive Source_File_Index.
453   --
454   --  Note that the name passed to this function is the simple file name,
455   --  without any directory information. The implementation is responsible
456   --  for searching for the file in the appropriate directories.
457   --
458   --  Note the special case that if the file name is gnat.adc, then the search
459   --  for the file is done ONLY in the directory corresponding to the current
460   --  compilation environment, i.e. in the same directory where the ali and
461   --  object files will be written.
462
463   function Full_Source_Name return File_Name_Type;
464   function Current_Source_File_Stamp return Time_Stamp_Type;
465   --  Returns the full name/time stamp of the source file most recently read
466   --  using Read_Source_File. Calling this routine entails no source file
467   --  directory lookup penalty.
468
469   procedure Full_Source_Name
470     (N         : File_Name_Type;
471      Full_File : out File_Name_Type;
472      Attr      : access File_Attributes);
473   function Full_Source_Name (N : File_Name_Type) return File_Name_Type;
474   function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type;
475   --  Returns the full name/time stamp of the source file whose simple name
476   --  is N which should not include path information. Note that if the file
477   --  cannot be located No_File is returned for the first routine and an all
478   --  blank time stamp is returned for the second (this is not an error
479   --  situation). The full name includes appropriate directory information.
480   --  The source file directory lookup penalty is incurred every single time
481   --  the routines are called unless you have previously called
482   --  Source_File_Data (Cache => True). See below.
483   --
484   --  The procedural version also returns some file attributes for the ALI
485   --  file (to save on system calls later on).
486
487   function Current_File_Index return Int;
488   --  Return the index in its source file of the current main unit
489
490   function Matching_Full_Source_Name
491     (N : File_Name_Type;
492      T : Time_Stamp_Type) return File_Name_Type;
493   --  Same semantics than Full_Source_Name but will search on the source path
494   --  until a source file with time stamp matching T is found. If none is
495   --  found returns No_File.
496
497   procedure Source_File_Data (Cache : Boolean);
498   --  By default source file data (full source file name and time stamp)
499   --  are looked up every time a call to Full_Source_Name (N) or
500   --  Source_File_Stamp (N) is made. This may be undesirable in certain
501   --  applications as this is uselessly slow if source file data does not
502   --  change during program execution. When this procedure is called with
503   --  Cache => True access to source file data does not incur a penalty if
504   --  this data was previously retrieved.
505
506   procedure Dump_Source_File_Names;
507   --  Prints out the names of all source files that have been read by
508   --  Read_Source_File, except those that come from the run-time library
509   --  (i.e. Include_Dir_Default_Prefix). The text is sent to whatever Output
510   --  is currently using (e.g. standard output or standard error).
511
512   procedure Dump_Command_Line_Source_File_Names;
513   --  Prints out the names of all source files on the command-line
514
515   function Get_First_Main_File_Name return String;
516   --  Return the file name of the first main file
517
518   -------------------------------------------
519   -- Representation of Library Information --
520   -------------------------------------------
521
522   --  Associated with each compiled source file is library information, a
523   --  string of bytes whose exact format is described in the body of Lib.Writ.
524   --  Compiling a source file generates this library information for the
525   --  compiled unit, and access the library information for units that were
526   --  compiled previously on which the unit being compiled depends.
527
528   --  How this information is stored is up to the implementation of this
529   --  package. At the interface level, this information is simply associated
530   --  with its corresponding source.
531
532   --  Several different implementations are possible:
533
534   --    1. The information could be directly associated with the source file,
535   --       e.g. placed in a resource fork of this file on the Mac, or on
536   --       MS-DOS, written to the source file after the end of file mark.
537
538   --    2. The information could be written into the generated object module
539   --       if the system supports the inclusion of arbitrary informational
540   --       byte streams into object files. In this case there must be a naming
541   --       convention that allows object files to be located given the name of
542   --       the corresponding source file.
543
544   --    3. The information could be written to a separate file, whose name is
545   --       related to the name of the source file by a fixed convention.
546
547   --  Which of these three methods is chosen depends on the constraints of the
548   --  host operating system. The interface described here is independent of
549   --  which of these approaches is used. Currently all versions of GNAT use
550   --  the third approach with a file name of xxx.ali where xxx is the source
551   --  file name.
552
553   -------------------------------
554   -- Library Information Input --
555   -------------------------------
556
557   --  These subprograms are used by the binder to read library information
558   --  files, see section above for representation of these files.
559
560   function Read_Library_Info
561     (Lib_File  : File_Name_Type;
562      Fatal_Err : Boolean := False) return Text_Buffer_Ptr;
563   --  Allocates a Text_Buffer of appropriate length and reads in the entire
564   --  source of the library information from the library information file
565   --  whose name is given by the parameter Name.
566   --
567   --  See description of Read_Source_File for details on the format of the
568   --  returned text buffer (the format is identical). The lower bound of
569   --  the Text_Buffer is always zero
570   --
571   --  If the specified file cannot be opened, then the action depends on
572   --  Fatal_Err. If Fatal_Err is True, an error message is given and the
573   --  compilation is abandoned. Otherwise if Fatal_Err is False, then null
574   --  is returned. Note that the Lib_File is a simple name which does not
575   --  include any directory information. The implementation is responsible
576   --  for searching for the file in appropriate directories.
577   --
578   --  If Opt.Check_Object_Consistency is set to True then this routine checks
579   --  whether the object file corresponding to the Lib_File is consistent with
580   --  it. The object file is inconsistent if the object does not exist or if
581   --  it has an older time stamp than Lib_File. This check is not performed
582   --  when the Lib_File is "locked" (i.e. read/only) because in this case the
583   --  object file may be buried in a library. In case of inconsistencies
584   --  Read_Library_Info behaves as if it did not find Lib_File (namely if
585   --  Fatal_Err is False, null is returned).
586
587   function Read_Library_Info_From_Full
588     (Full_Lib_File : File_Name_Type;
589      Lib_File_Attr : access File_Attributes;
590      Fatal_Err     : Boolean := False) return Text_Buffer_Ptr;
591   --  Same as Read_Library_Info, except Full_Lib_File must contains the full
592   --  path to the library file (instead of having Read_Library_Info recompute
593   --  it).
594   --  Lib_File_Attr should be an initialized set of attributes for the
595   --  library file (it can be initialized to Unknown_Attributes, but in
596   --  general will have been initialized by a previous call to Find_File).
597
598   function Full_Library_Info_Name return File_Name_Type;
599   function Full_Object_File_Name return File_Name_Type;
600   --  Returns the full name of the library/object file most recently read
601   --  using Read_Library_Info, including appropriate directory information.
602   --  Calling this routine entails no library file directory lookup
603   --  penalty. Note that the object file corresponding to a library file
604   --  is not actually read. Its time stamp is affected when the flag
605   --  Opt.Check_Object_Consistency is set.
606
607   function Current_Library_File_Stamp return Time_Stamp_Type;
608   function Current_Object_File_Stamp return Time_Stamp_Type;
609   --  The time stamps of the files returned by the previous two routines.
610   --  It is an error to call Current_Object_File_Stamp if
611   --  Opt.Check_Object_Consistency is set to False.
612
613   procedure Full_Lib_File_Name
614     (N        : File_Name_Type;
615      Lib_File : out File_Name_Type;
616      Attr     : out File_Attributes);
617   function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type;
618   --  Returns the full name of library file N. N should not include
619   --  path information. Note that if the file cannot be located No_File is
620   --  returned for the first routine and an all blank time stamp is returned
621   --  for the second (this is not an error situation). The full name includes
622   --  the appropriate directory information. The library file directory lookup
623   --  penalty is incurred every single time this routine is called.
624   --  The procedural version also returns some file attributes for the ALI
625   --  file (to save on system calls later on).
626
627   function Lib_File_Name
628     (Source_File : File_Name_Type;
629      Munit_Index : Nat := 0) return File_Name_Type;
630   --  Given the name of a source file, returns the name of the corresponding
631   --  library information file. This may be the name of the object file or of
632   --  a separate file used to store the library information. In the current
633   --  implementation, a separate file (the ALI file) is always used. In either
634   --  case the returned result is suitable for calling Read_Library_Info. The
635   --  Munit_Index is the unit index in multiple unit per file mode, or zero in
636   --  normal single unit per file mode (used to add ~nnn suffix). Note: this
637   --  subprogram is in this section because it is used by the compiler to
638   --  determine the proper library information names to be placed in the
639   --  generated library information file.
640
641   -----------------
642   -- Termination --
643   -----------------
644
645   Current_Exit_Status : Integer := 0;
646   --  Exit status that is set with procedure OS_Exit_Through_Exception below
647   --  and can be used in exception handler for Types.Terminate_Program to call
648   --  Set_Exit_Status as the last action of the program.
649
650   procedure OS_Exit_Through_Exception (Status : Integer);
651   pragma No_Return (OS_Exit_Through_Exception);
652   --  Set the Current_Exit_Status, then raise Types.Terminate_Program
653
654   type Exit_Code_Type is (
655      E_Success,    -- No warnings or errors
656      E_Warnings,   -- Compiler warnings generated
657      E_No_Code,    -- No code generated
658      E_No_Compile, -- Compilation not needed (smart recompilation)
659      E_Errors,     -- Compiler error messages generated
660      E_Fatal,      -- Fatal (serious) error, e.g. source file not found
661      E_Abort);     -- Internally detected compiler error
662
663   procedure Exit_Program (Exit_Code : Exit_Code_Type);
664   pragma No_Return (Exit_Program);
665   --  A call to Exit_Program terminates execution with the given status. A
666   --  status of zero indicates normal completion, a non-zero status indicates
667   --  abnormal termination.
668
669   -------------------------
670   -- Command Line Access --
671   -------------------------
672
673   --  Direct interface to command line parameters. (We don't want to use
674   --  the predefined command line package because it defines functions
675   --  returning string)
676
677   function Arg_Count return Natural;
678   pragma Import (C, Arg_Count, "__gnat_arg_count");
679   --  Get number of arguments (note: optional globbing may be enabled)
680
681   procedure Fill_Arg (A : System.Address; Arg_Num : Integer);
682   pragma Import (C, Fill_Arg, "__gnat_fill_arg");
683   --  Store one argument
684
685   function Len_Arg (Arg_Num : Integer) return Integer;
686   pragma Import (C, Len_Arg, "__gnat_len_arg");
687   --  Get length of argument
688
689   ALI_Default_Suffix : constant String_Ptr := new String'("ali");
690   ALI_Suffix         : String_Ptr          := ALI_Default_Suffix;
691   --  The suffixes used for the ALI files
692
693   function Prep_Suffix return String;
694   --  The suffix used for preprocessed files
695
696private
697
698   Current_Main : File_Name_Type := No_File;
699   --  Used to save a simple file name between calls to Next_Main_Source and
700   --  Read_Source_File. If the file name argument to Read_Source_File is
701   --  No_File, that indicates that the file whose name was returned by the
702   --  last call to Next_Main_Source (and stored here) is to be read.
703
704   Target_Object_Suffix : constant String := Get_Target_Object_Suffix.all;
705   --  The suffix used for the target object files
706
707   Output_File_Name : File_Name_Type;
708   --  File_Name_Type for name of open file whose FD is in Output_FD, the name
709   --  stored does not include the trailing NUL character.
710
711   Argument_Count : constant Integer := Arg_Count - 1;
712   --  Number of arguments (excluding program name)
713
714   type File_Name_Array is array (Int range <>) of String_Ptr;
715   type File_Name_Array_Ptr is access File_Name_Array;
716   File_Names : File_Name_Array_Ptr :=
717                  new File_Name_Array (1 .. Int (Argument_Count) + 2);
718   --  As arguments are scanned, file names are stored in this array. The
719   --  strings do not have terminating NULs. The array is extensible, because
720   --  when using project files, there may be more files than arguments on the
721   --  command line.
722
723   type File_Index_Array is array (Int range <>) of Int;
724   type File_Index_Array_Ptr is access File_Index_Array;
725   File_Indexes : File_Index_Array_Ptr :=
726                    new File_Index_Array (1 .. Int (Argument_Count) + 2);
727
728   Current_File_Name_Index : Int := 0;
729   --  The index in File_Names of the last file opened by Next_Main_Source
730   --  or Next_Main_Lib_File. The value 0 indicates that no files have been
731   --  opened yet.
732
733   procedure Create_File_And_Check
734     (Fdesc : out File_Descriptor;
735      Fmode : Mode);
736   --  Create file whose name (NUL terminated) is in Name_Buffer (with the
737   --  length in Name_Len), and place the resulting descriptor in Fdesc. Issue
738   --  message and exit with fatal error if file cannot be created. The Fmode
739   --  parameter is set to either Text or Binary (for details see description
740   --  of System.OS_Lib.Create_File).
741
742   procedure Open_File_To_Append_And_Check
743     (Fdesc : out File_Descriptor;
744      Fmode : Mode);
745   --  Opens the file whose name (NUL terminated) is in Name_Buffer (with the
746   --  length in Name_Len), and place the resulting descriptor in Fdesc. Issue
747   --  message and exit with fatal error if file cannot be opened. The Fmode
748   --  parameter is set to either Text or Binary (for details see description
749   --  of System.OS_Lib.Open_Append).
750
751   type Program_Type is (Compiler, Binder, Make, Gnatls, Unspecified);
752   --  Program currently running
753   procedure Set_Program (P : Program_Type);
754   --  Indicates to the body of Osint the program currently running. This
755   --  procedure is called by the child packages of Osint. A check is made
756   --  that this procedure is not called more than once.
757
758   function More_Files return Boolean;
759   --  Implements More_Source_Files and More_Lib_Files
760
761   function Next_Main_File return File_Name_Type;
762   --  Implements Next_Main_Source and Next_Main_Lib_File
763
764   function Object_File_Name (N : File_Name_Type) return File_Name_Type;
765   --  Constructs the name of the object file corresponding to library file N.
766   --  If N is a full file name than the returned file name will also be a full
767   --  file name. Note that no lookup in the library file directories is done
768   --  for this file. This routine merely constructs the name.
769
770   procedure Write_Info (Info : String);
771   --  Implements Write_Binder_Info, Write_Debug_Info, and Write_Library_Info
772
773   procedure Write_With_Check (A : Address; N : Integer);
774   --  Writes N bytes from buffer starting at address A to file whose FD is
775   --  stored in Output_FD, and whose file name is stored as a File_Name_Type
776   --  in Output_File_Name. A check is made for disk full, and if this is
777   --  detected, the file being written is deleted, and a fatal error is
778   --  signalled.
779
780end Osint;
781