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