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