1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004-2012, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This package contains various subprograms used by the builders, in
27 -- particular those subprograms related to project management and build
31 with Namet
; use Namet
;
36 with Snames
; use Snames
;
38 with Types
; use Types
;
40 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
44 type Fail_Proc
is access procedure (S
: String);
45 -- Pointer to procedure which outputs a failure message
47 Root_Environment
: Prj
.Tree
.Environment
;
48 -- The environment coming from environment variables and command line
49 -- switches. When we do not have an aggregate project, this is used for
50 -- parsing the project tree. When we have an aggregate project, this is
51 -- used to parse the aggregate project; the latter then generates another
52 -- environment (with additional external values and project path) to parse
53 -- the aggregated projects.
55 Default_Config_Name
: constant String := "default.cgpr";
56 -- Name of the configuration file used by gprbuild and generated by
57 -- gprconfig by default.
59 On_Windows
: constant Boolean := Directory_Separator
= '\';
60 -- True when on Windows
62 Source_Info_Option
: constant String := "--source-info=";
63 -- Switch to indicate the source info file
65 Subdirs_Option
: constant String := "--subdirs=";
66 -- Switch used to indicate that the real directories (object, exec,
67 -- library, ...) are subdirectories of those in the project file.
69 Unchecked_Shared_Lib_Imports
: constant String :=
70 "--unchecked-shared-lib-imports";
71 -- Command line switch to allow shared library projects to import projects
72 -- that are not shared library projects.
74 Single_Compile_Per_Obj_Dir_Switch
: constant String :=
75 "--single-compile-per-obj-dir";
76 -- Switch to forbid simultaneous compilations for the same object directory
77 -- when project files are used.
79 Create_Map_File_Switch
: constant String := "--create-map-file";
80 -- Switch to create a map file when an executable is linked
82 Load_Standard_Base
: Boolean := True;
83 -- False when gprbuild is called with --db-
85 package Db_Switch_Args
is new Table
.Table
86 (Table_Component_Type
=> Name_Id
,
87 Table_Index_Type
=> Integer,
90 Table_Increment
=> 100,
91 Table_Name
=> "Makegpr.Db_Switch_Args");
92 -- Table of all the arguments of --db switches of gprbuild
94 package Directories
is new Table
.Table
95 (Table_Component_Type
=> Path_Name_Type
,
96 Table_Index_Type
=> Integer,
99 Table_Increment
=> 100,
100 Table_Name
=> "Makegpr.Directories");
101 -- Table of all the source or object directories, filled up by
105 (Option
: String_Access
;
106 To
: in out String_List_Access
;
107 Last
: in out Natural);
110 To
: in out String_List_Access
;
111 Last
: in out Natural);
112 -- Add a string to a list of strings
114 function Absolute_Path
115 (Path
: Path_Name_Type
;
116 Project
: Project_Id
) return String;
117 -- Returns an absolute path for a configuration pragmas file
119 function Create_Binder_Mapping_File
120 (Project_Tree
: Project_Tree_Ref
) return Path_Name_Type
;
121 -- Create a binder mapping file and returns its path name
123 function Create_Name
(Name
: String) return File_Name_Type
;
124 function Create_Name
(Name
: String) return Name_Id
;
125 function Create_Name
(Name
: String) return Path_Name_Type
;
126 -- Get an id for a name
128 function Base_Name_Index_For
131 Index_Separator
: Character) return File_Name_Type
;
132 -- Returns the base name of Main, without the extension, followed by the
133 -- Index_Separator followed by the Main_Index if it is non-zero.
135 function Executable_Prefix_Path
return String;
136 -- Return the absolute path parent directory of the directory where the
137 -- current executable resides, if its directory is named "bin", otherwise
138 -- return an empty string. When a directory is returned, it is guaranteed
139 -- to end with a directory separator.
141 procedure Inform
(N
: Name_Id
:= No_Name
; Msg
: String);
142 procedure Inform
(N
: File_Name_Type
; Msg
: String);
143 -- Prints out the program name followed by a colon, N and S
145 function File_Not_A_Source_Of
146 (Project_Tree
: Project_Tree_Ref
;
148 Sfile
: File_Name_Type
) return Boolean;
149 -- Check that file name Sfile is one of the source of unit Uname. Returns
150 -- True if the unit is in one of the project file, but the file name is not
151 -- one of its source. Returns False otherwise.
153 function Check_Source_Info_In_ALI
154 (The_ALI
: ALI
.ALI_Id
;
155 Tree
: Project_Tree_Ref
) return Name_Id
;
156 -- Check whether all file references in ALI are still valid (i.e. the
157 -- source files are still associated with the same units). Return the name
158 -- of the unit if everything is still valid. Return No_Name otherwise.
160 procedure Ensure_Absolute_Path
161 (Switch
: in out String_Access
;
164 For_Gnatbind
: Boolean := False;
165 Including_Non_Switch
: Boolean := True;
166 Including_RTS
: Boolean := False);
167 -- Do nothing if Switch is an absolute path switch. If relative, fail if
168 -- Parent is the empty string, otherwise prepend the path with Parent. This
169 -- subprogram is only used when using project files. If For_Gnatbind is
170 -- True, consider gnatbind specific syntax for -L (not a path, left
171 -- unchanged) and -A (path is optional, preceded with "=" if present).
172 -- If Including_RTS is True, process also switches --RTS=. Do_Fail is
173 -- called in case of error. Using Osint.Fail might be appropriate.
175 function Is_Subunit
(Source
: Source_Id
) return Boolean;
176 -- Return True if source is a subunit
178 procedure Initialize_Source_Record
(Source
: Source_Id
);
179 -- Get information either about the source file, or the object and
180 -- dependency file, as well as their timestamps.
182 function Is_External_Assignment
183 (Env
: Prj
.Tree
.Environment
;
184 Argv
: String) return Boolean;
185 -- Verify that an external assignment switch is syntactically correct
187 -- Correct forms are:
190 -- -X"name=other value"
192 -- Assumptions: 'First = 1, Argv (1 .. 2) = "-X"
194 -- When this function returns True, the external assignment has been
195 -- entered by a call to Prj.Ext.Add, so that in a project file, External
196 -- ("name") will return "value".
198 type Name_Ids
is array (Positive range <>) of Name_Id
;
199 No_Names
: constant Name_Ids
:= (1 .. 0 => No_Name
);
200 -- Name_Ids is used for list of language names in procedure Get_Directories
203 Ada_Only
: constant Name_Ids
:= (1 => Name_Ada
);
204 -- Used to invoke Get_Directories in gnatmake
206 type Activity_Type
is (Compilation
, Executable_Binding
, SAL_Binding
);
208 procedure Get_Directories
209 (Project_Tree
: Project_Tree_Ref
;
210 For_Project
: Project_Id
;
211 Activity
: Activity_Type
;
212 Languages
: Name_Ids
);
213 -- Put in table Directories the source (when Sources is True) or
214 -- object/library (when Sources is False) directories of project
215 -- For_Project and of all the project it imports directly or indirectly.
216 -- The source directories of imported projects are only included if one
217 -- of the declared languages is in the list Languages.
219 procedure Write_Path_File
(FD
: File_Descriptor
);
220 -- Write in the specified open path file the directories in table
221 -- Directories, then closed the path file.
223 procedure Get_Switches
226 Project_Tree
: Project_Tree_Ref
;
227 Value
: out Variable_Value
;
228 Is_Default
: out Boolean);
229 procedure Get_Switches
230 (Source_File
: File_Name_Type
;
231 Source_Lang
: Name_Id
;
232 Source_Prj
: Project_Id
;
234 Project_Tree
: Project_Tree_Ref
;
235 Value
: out Variable_Value
;
236 Is_Default
: out Boolean;
237 Test_Without_Suffix
: Boolean := False;
238 Check_ALI_Suffix
: Boolean := False);
239 -- Compute the switches (Compilation switches for instance) for the given
240 -- file. This checks various attributes to see if there are file specific
241 -- switches, or else defaults on the switches for the corresponding
242 -- language. Is_Default is set to False if there were file-specific
243 -- switches Source_File can be set to No_File to force retrieval of the
244 -- default switches. If Test_Without_Suffix is True, and there is no " for
245 -- Switches(Source_File) use", then this procedure also tests without the
246 -- extension of the filename. If Test_Without_Suffix is True and
247 -- Check_ALI_Suffix is True, then we also replace the file extension with
248 -- ".ali" when testing.
250 function Linker_Options_Switches
251 (Project
: Project_Id
;
253 In_Tree
: Project_Tree_Ref
) return String_List
;
254 -- Collect the options specified in the Linker'Linker_Options attributes
255 -- of project Project, in project tree In_Tree, and in the projects that
256 -- it imports directly or indirectly, and returns the result.
258 function Path_Or_File_Name
(Path
: Path_Name_Type
) return String;
259 -- Returns a file name if -df is used, otherwise return a path name
261 function Unit_Index_Of
(ALI_File
: File_Name_Type
) return Int
;
262 -- Find the index of a unit in a source file. Return zero if the file is
263 -- not a multi-unit source file.
265 procedure Verbose_Msg
268 N2
: Name_Id
:= No_Name
;
270 Prefix
: String := " -> ";
271 Minimum_Verbosity
: Opt
.Verbosity_Level_Type
:= Opt
.Low
);
272 procedure Verbose_Msg
273 (N1
: File_Name_Type
;
275 N2
: File_Name_Type
:= No_File
;
277 Prefix
: String := " -> ";
278 Minimum_Verbosity
: Opt
.Verbosity_Level_Type
:= Opt
.Low
);
279 -- If the verbose flag (Verbose_Mode) is set and the verbosity level is at
280 -- least equal to Minimum_Verbosity, then print Prefix to standard output
281 -- followed by N1 and S1. If N2 /= No_Name then N2 is printed after S1. S2
282 -- is printed last. Both N1 and N2 are printed in quotation marks. The two
283 -- forms differ only in taking Name_Id or File_name_Type arguments.
285 -------------------------
286 -- Program termination --
287 -------------------------
289 procedure Fail_Program
290 (Project_Tree
: Project_Tree_Ref
;
292 Flush_Messages
: Boolean := True);
293 -- Terminate program with a message and a fatal status code
295 procedure Finish_Program
296 (Project_Tree
: Project_Tree_Ref
;
297 Exit_Code
: Osint
.Exit_Code_Type
:= Osint
.E_Success
;
299 -- Terminate program, with or without a message, setting the status code
300 -- according to Fatal. This properly removes all temporary files.
307 with function Add_Switch
310 For_Builder
: Boolean;
311 Has_Global_Compilation_Switches
: Boolean) return Boolean;
312 -- For_Builder is true if we have a builder switch. This function
313 -- should return True in case of success (the switch is valid),
314 -- False otherwise. The error message will be displayed by
315 -- Compute_Builder_Switches itself.
317 -- Has_Global_Compilation_Switches is True if the attribute
318 -- Global_Compilation_Switches is defined in the project.
320 procedure Compute_Builder_Switches
321 (Project_Tree
: Project_Tree_Ref
;
322 Root_Environment
: in out Prj
.Tree
.Environment
;
323 Main_Project
: Project_Id
;
324 Only_For_Lang
: Name_Id
:= No_Name
);
325 -- Compute the builder switches and global compilation switches. Every time
326 -- a switch is found in the project, it is passed to Add_Switch. You can
327 -- provide a value for Only_For_Lang so that we only look for this language
328 -- when parsing the global compilation switches.
330 -----------------------
331 -- Project_Tree data --
332 -----------------------
334 -- The following types are specific to builders, and associated with each
335 -- of the loaded project trees.
337 type Binding_Data_Record
;
338 type Binding_Data
is access Binding_Data_Record
;
339 type Binding_Data_Record
is record
340 Language
: Language_Ptr
;
341 Language_Name
: Name_Id
;
342 Binder_Driver_Name
: File_Name_Type
;
343 Binder_Driver_Path
: String_Access
;
344 Binder_Prefix
: Name_Id
;
347 -- Data for a language that have a binder driver
349 type Builder_Project_Tree_Data
is new Project_Tree_Appdata
with record
350 Binding
: Binding_Data
;
352 There_Are_Binder_Drivers
: Boolean := False;
353 -- True when there is a binder driver. Set by Get_Configuration when
354 -- an attribute Language_Processing'Binder_Driver is declared.
355 -- Reset to False if there are no sources of the languages with binder
358 Number_Of_Mains
: Natural := 0;
359 -- Number of main units in this project tree
361 Closure_Needed
: Boolean := False;
362 -- If True, we need to add the closure of the file we just compiled to
363 -- the queue. If False, it is assumed that all files are already on the
364 -- queue so we do not waste time computing the closure.
366 Need_Compilation
: Boolean := True;
367 Need_Binding
: Boolean := True;
368 Need_Linking
: Boolean := True;
369 -- Which of the compilation phases are needed for this project tree
371 type Builder_Data_Access
is access all Builder_Project_Tree_Data
;
373 procedure Free
(Data
: in out Builder_Project_Tree_Data
);
374 -- Free all memory allocated for Data
376 function Builder_Data
(Tree
: Project_Tree_Ref
) return Builder_Data_Access
;
377 -- Return (allocate if needed) tree-specific data
379 procedure Compute_Compilation_Phases
380 (Tree
: Project_Tree_Ref
;
381 Root_Project
: Project_Id
;
382 Option_Unique_Compile
: Boolean := False; -- Was "-u" specified ?
383 Option_Compile_Only
: Boolean := False; -- Was "-c" specified ?
384 Option_Bind_Only
: Boolean := False;
385 Option_Link_Only
: Boolean := False);
386 -- Compute which compilation phases will be needed for Tree. This also does
387 -- the computation for aggregated trees. This also check whether we'll need
388 -- to check the closure of the files we have just compiled to add them to
395 -- Package Mains is used to store the mains specified on the command line
396 -- and to retrieve them when a project file is used, to verify that the
397 -- files exist and that they belong to a project file.
399 -- Mains are stored in a table. An index is used to retrieve the mains
402 type Main_Info
is record
403 File
: File_Name_Type
; -- Always canonical casing
405 Location
: Source_Ptr
:= No_Location
;
407 Source
: Prj
.Source_Id
:= No_Source
;
408 Project
: Project_Id
;
409 Tree
: Project_Tree_Ref
;
412 No_Main_Info
: constant Main_Info
:=
413 (No_File
, 0, No_Location
, No_Source
, No_Project
, null);
419 Location
: Source_Ptr
:= No_Location
;
420 Project
: Project_Id
:= No_Project
;
421 Tree
: Project_Tree_Ref
:= null);
422 -- Add one main to the table. This is in general used to add the main
423 -- files specified on the command line. Index is used for multi-unit
424 -- source files, and indicates which unit in the source is concerned.
425 -- Location is the location within the project file (if a project file
426 -- is used). Project and Tree indicate to which project the main should
427 -- belong. In particular, for aggregate projects, this isn't necessarily
428 -- the main project tree. These can be set to No_Project and null when
429 -- not using projects.
435 -- Reset the cursor to the beginning of the table
437 procedure Set_Multi_Unit_Index
438 (Project_Tree
: Project_Tree_Ref
:= null;
440 -- If a single main file was defined, this subprogram indicates which
441 -- unit inside it is the main (case of a multi-unit source files).
442 -- Errors are raised if zero or more than one main file was defined,
443 -- and Index is non-zaero. This subprogram is used for the handling
444 -- of the command line switch.
446 function Next_Main
return String;
447 function Next_Main
return Main_Info
;
448 -- Moves the cursor forward and returns the new current entry. Returns
449 -- No_Main_Info there are no more mains in the table.
451 function Number_Of_Mains
(Tree
: Project_Tree_Ref
) return Natural;
452 -- Returns the number of mains in this project tree (if Tree is null, it
453 -- returns the total number of project trees)
455 procedure Fill_From_Project
456 (Root_Project
: Project_Id
;
457 Project_Tree
: Project_Tree_Ref
);
458 -- If no main was already added (presumably from the command line), add
459 -- the main units from root_project (or in the case of an aggregate
460 -- project from all the aggregated projects).
462 procedure Complete_Mains
463 (Flags
: Processing_Flags
;
464 Root_Project
: Project_Id
;
465 Project_Tree
: Project_Tree_Ref
);
466 -- If some main units were already added from the command line, check
467 -- that they all belong to the root project, and that they are full
468 -- paths rather than (partial) base names (e.g. no body suffix was
477 type Source_Info_Format
is (Format_Gprbuild
, Format_Gnatmake
);
481 -- The queue of sources to be checked for compilation. There can be a
482 -- single such queue per application.
484 type Source_Info
(Format
: Source_Info_Format
:= Format_Gprbuild
) is
487 when Format_Gprbuild
=>
488 Tree
: Project_Tree_Ref
:= No_Project_Tree
;
489 Id
: Source_Id
:= No_Source
;
491 when Format_Gnatmake
=>
492 File
: File_Name_Type
:= No_File
;
493 Unit
: Unit_Name_Type
:= No_Unit_Name
;
495 Project
: Project_Id
:= No_Project
;
496 Sid
: Source_Id
:= No_Source
;
499 -- Information about files stored in the queue. The exact information
500 -- depends on the builder, and in particular whether it only supports
501 -- project-based files (in which case we have a full Source_Id record).
503 No_Source_Info
: constant Source_Info
:= (Format_Gprbuild
, null, null);
506 (Queue_Per_Obj_Dir
: Boolean;
507 Force
: Boolean := False);
508 -- Initialize the queue
510 -- Queue_Per_Obj_Dir matches the --single-compile-per-obj-dir switch:
511 -- when True, there cannot be simultaneous compilations with the object
512 -- files in the same object directory when project files are used.
514 -- Nothing is done if Force is False and the queue was already
517 procedure Remove_Marks
;
518 -- Remove all marks set for the files. This means that the files will be
519 -- handed to the compiler if they are added to the queue, and is mostly
520 -- useful when recompiling several executables in non-project mode, as
521 -- the switches may be different and -s may be in use.
523 function Is_Empty
return Boolean;
524 -- Returns True if the queue is empty
526 function Is_Virtually_Empty
return Boolean;
527 -- Returns True if queue is empty or if all object directories are busy
529 procedure Insert
(Source
: Source_Info
; With_Roots
: Boolean := False);
531 (Source
: Source_Info
; With_Roots
: Boolean := False) return Boolean;
532 -- Insert source in the queue. The second version returns False if the
533 -- Source was already marked in the queue. If With_Roots is True and the
534 -- source is in Format_Gprbuild mode (ie with a project), this procedure
535 -- also includes the "Roots" for this main, ie all the other files that
536 -- must be included in the library or binary (in particular to combine
537 -- Ada and C files connected through pragma Export/Import). When the
538 -- roots are computed, they are also stored in the corresponding
539 -- Source_Id for later reuse by the binder.
541 procedure Insert_Project_Sources
542 (Project
: Project_Id
;
543 Project_Tree
: Project_Tree_Ref
;
544 All_Projects
: Boolean;
545 Unique_Compile
: Boolean);
546 -- Insert all the compilable sources of the project in the queue. If
547 -- All_Project is true, then all sources from imported projects are also
548 -- inserted. Unique_Compile should be true if "-u" was specified on the
549 -- command line: if True and some files were given on the command line),
550 -- only those files will be compiled (so Insert_Project_Sources will do
551 -- nothing). If True and no file was specified on the command line, all
552 -- files of the project(s) will be compiled. This procedure also
553 -- processed aggregated projects.
555 procedure Insert_Withed_Sources_For
556 (The_ALI
: ALI
.ALI_Id
;
557 Project_Tree
: Project_Tree_Ref
;
558 Excluding_Shared_SALs
: Boolean := False);
559 -- Insert in the queue those sources withed by The_ALI, if there are not
560 -- already in the queue and Only_Interfaces is False or they are part of
561 -- the interfaces of their project.
564 (Found
: out Boolean;
565 Source
: out Source_Info
);
566 -- Get the first source that can be compiled from the queue. If no
567 -- source may be compiled, sets Found to False. In this case, the value
568 -- for Source is undefined.
570 function Size
return Natural;
571 -- Return the total size of the queue, including the sources already
574 function Processed
return Natural;
575 -- Return the number of source in the queue that have aready been
578 procedure Set_Obj_Dir_Busy
(Obj_Dir
: Path_Name_Type
);
579 procedure Set_Obj_Dir_Free
(Obj_Dir
: Path_Name_Type
);
580 -- Mark Obj_Dir as busy or free (see the parameter to Initialize)
582 function Element
(Rank
: Positive) return File_Name_Type
;
583 -- Get the file name for element of index Rank in the queue