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