1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004-2014, 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 No_Exit_Message_Option
: constant String := "--no-exit-message";
83 -- Switch to suppress exit error message when there are compilation
84 -- failures. This is useful when a tool, such as gnatprove, silently calls
85 -- the builder and does not want to pollute its output with error messages
86 -- coming from the builder. This is an internal switch.
88 Keep_Temp_Files_Option
: constant String := "--keep-temp-files";
89 -- Switch to suppress deletion of temp files created by the builder.
90 -- Note that debug switch -gnatdn also has this effect.
92 Load_Standard_Base
: Boolean := True;
93 -- False when gprbuild is called with --db-
95 package Db_Switch_Args
is new Table
.Table
96 (Table_Component_Type
=> Name_Id
,
97 Table_Index_Type
=> Integer,
100 Table_Increment
=> 100,
101 Table_Name
=> "Makegpr.Db_Switch_Args");
102 -- Table of all the arguments of --db switches of gprbuild
104 package Directories
is new Table
.Table
105 (Table_Component_Type
=> Path_Name_Type
,
106 Table_Index_Type
=> Integer,
107 Table_Low_Bound
=> 1,
108 Table_Initial
=> 200,
109 Table_Increment
=> 100,
110 Table_Name
=> "Makegpr.Directories");
111 -- Table of all the source or object directories, filled up by
115 (Option
: String_Access
;
116 To
: in out String_List_Access
;
117 Last
: in out Natural);
120 To
: in out String_List_Access
;
121 Last
: in out Natural);
122 -- Add a string to a list of strings
124 function Absolute_Path
125 (Path
: Path_Name_Type
;
126 Project
: Project_Id
) return String;
127 -- Returns an absolute path for a configuration pragmas file
129 function Create_Binder_Mapping_File
130 (Project_Tree
: Project_Tree_Ref
) return Path_Name_Type
;
131 -- Create a binder mapping file and returns its path name
133 function Create_Name
(Name
: String) return File_Name_Type
;
134 function Create_Name
(Name
: String) return Name_Id
;
135 function Create_Name
(Name
: String) return Path_Name_Type
;
136 -- Get an id for a name
138 function Base_Name_Index_For
141 Index_Separator
: Character) return File_Name_Type
;
142 -- Returns the base name of Main, without the extension, followed by the
143 -- Index_Separator followed by the Main_Index if it is non-zero.
145 function Executable_Prefix_Path
return String;
146 -- Return the absolute path parent directory of the directory where the
147 -- current executable resides, if its directory is named "bin", otherwise
148 -- return an empty string. When a directory is returned, it is guaranteed
149 -- to end with a directory separator.
151 procedure Inform
(N
: Name_Id
:= No_Name
; Msg
: String);
152 procedure Inform
(N
: File_Name_Type
; Msg
: String);
153 -- Prints out the program name followed by a colon, N and S
155 function File_Not_A_Source_Of
156 (Project_Tree
: Project_Tree_Ref
;
158 Sfile
: File_Name_Type
) return Boolean;
159 -- Check that file name Sfile is one of the source of unit Uname. Returns
160 -- True if the unit is in one of the project file, but the file name is not
161 -- one of its source. Returns False otherwise.
163 function Check_Source_Info_In_ALI
164 (The_ALI
: ALI
.ALI_Id
;
165 Tree
: Project_Tree_Ref
) return Name_Id
;
166 -- Check whether all file references in ALI are still valid (i.e. the
167 -- source files are still associated with the same units). Return the name
168 -- of the unit if everything is still valid. Return No_Name otherwise.
170 procedure Ensure_Absolute_Path
171 (Switch
: in out String_Access
;
174 For_Gnatbind
: Boolean := False;
175 Including_Non_Switch
: Boolean := True;
176 Including_RTS
: Boolean := False);
177 -- Do nothing if Switch is an absolute path switch. If relative, fail if
178 -- Parent is the empty string, otherwise prepend the path with Parent. This
179 -- subprogram is only used when using project files. If For_Gnatbind is
180 -- True, consider gnatbind specific syntax for -L (not a path, left
181 -- unchanged) and -A (path is optional, preceded with "=" if present).
182 -- If Including_RTS is True, process also switches --RTS=. Do_Fail is
183 -- called in case of error. Using Osint.Fail might be appropriate.
185 function Is_Subunit
(Source
: Source_Id
) return Boolean;
186 -- Return True if source is a subunit
188 procedure Initialize_Source_Record
(Source
: Source_Id
);
189 -- Get information either about the source file, or the object and
190 -- dependency file, as well as their timestamps.
192 function Is_External_Assignment
193 (Env
: Prj
.Tree
.Environment
;
194 Argv
: String) return Boolean;
195 -- Verify that an external assignment switch is syntactically correct
197 -- Correct forms are:
200 -- -X"name=other value"
202 -- Assumptions: 'First = 1, Argv (1 .. 2) = "-X"
204 -- When this function returns True, the external assignment has been
205 -- entered by a call to Prj.Ext.Add, so that in a project file, External
206 -- ("name") will return "value".
208 type Name_Ids
is array (Positive range <>) of Name_Id
;
209 No_Names
: constant Name_Ids
:= (1 .. 0 => No_Name
);
210 -- Name_Ids is used for list of language names in procedure Get_Directories
213 Ada_Only
: constant Name_Ids
:= (1 => Name_Ada
);
214 -- Used to invoke Get_Directories in gnatmake
216 type Activity_Type
is (Compilation
, Executable_Binding
, SAL_Binding
);
218 procedure Get_Directories
219 (Project_Tree
: Project_Tree_Ref
;
220 For_Project
: Project_Id
;
221 Activity
: Activity_Type
;
222 Languages
: Name_Ids
);
223 -- Put in table Directories the source (when Sources is True) or
224 -- object/library (when Sources is False) directories of project
225 -- For_Project and of all the project it imports directly or indirectly.
226 -- The source directories of imported projects are only included if one
227 -- of the declared languages is in the list Languages.
229 function Aggregate_Libraries_In
(Tree
: Project_Tree_Ref
) return Boolean;
230 -- Return True iff there is one or more aggregate library projects in
231 -- the project tree Tree.
233 procedure Write_Path_File
(FD
: File_Descriptor
);
234 -- Write in the specified open path file the directories in table
235 -- Directories, then closed the path file.
237 procedure Get_Switches
240 Project_Tree
: Project_Tree_Ref
;
241 Value
: out Variable_Value
;
242 Is_Default
: out Boolean);
243 procedure Get_Switches
244 (Source_File
: File_Name_Type
;
245 Source_Lang
: Name_Id
;
246 Source_Prj
: Project_Id
;
248 Project_Tree
: Project_Tree_Ref
;
249 Value
: out Variable_Value
;
250 Is_Default
: out Boolean;
251 Test_Without_Suffix
: Boolean := False;
252 Check_ALI_Suffix
: Boolean := False);
253 -- Compute the switches (Compilation switches for instance) for the given
254 -- file. This checks various attributes to see if there are file specific
255 -- switches, or else defaults on the switches for the corresponding
256 -- language. Is_Default is set to False if there were file-specific
257 -- switches. Source_File can be set to No_File to force retrieval of the
258 -- default switches. If Test_Without_Suffix is True, and there is no "for
259 -- Switches(Source_File) use", then this procedure also tests without the
260 -- extension of the filename. If Test_Without_Suffix is True and
261 -- Check_ALI_Suffix is True, then we also replace the file extension with
262 -- ".ali" when testing.
264 function Linker_Options_Switches
265 (Project
: Project_Id
;
267 In_Tree
: Project_Tree_Ref
) return String_List
;
268 -- Collect the options specified in the Linker'Linker_Options attributes
269 -- of project Project, in project tree In_Tree, and in the projects that
270 -- it imports directly or indirectly, and returns the result.
272 function Path_Or_File_Name
(Path
: Path_Name_Type
) return String;
273 -- Returns a file name if -df is used, otherwise return a path name
275 function Unit_Index_Of
(ALI_File
: File_Name_Type
) return Int
;
276 -- Find the index of a unit in a source file. Return zero if the file is
277 -- not a multi-unit source file.
279 procedure Verbose_Msg
282 N2
: Name_Id
:= No_Name
;
284 Prefix
: String := " -> ";
285 Minimum_Verbosity
: Opt
.Verbosity_Level_Type
:= Opt
.Low
);
286 procedure Verbose_Msg
287 (N1
: File_Name_Type
;
289 N2
: File_Name_Type
:= No_File
;
291 Prefix
: String := " -> ";
292 Minimum_Verbosity
: Opt
.Verbosity_Level_Type
:= Opt
.Low
);
293 -- If the verbose flag (Verbose_Mode) is set and the verbosity level is at
294 -- least equal to Minimum_Verbosity, then print Prefix to standard output
295 -- followed by N1 and S1. If N2 /= No_Name then N2 is printed after S1. S2
296 -- is printed last. Both N1 and N2 are printed in quotation marks. The two
297 -- forms differ only in taking Name_Id or File_name_Type arguments.
299 -------------------------
300 -- Program termination --
301 -------------------------
303 procedure Fail_Program
304 (Project_Tree
: Project_Tree_Ref
;
306 Flush_Messages
: Boolean := True);
307 -- Terminate program with a message and a fatal status code
309 procedure Finish_Program
310 (Project_Tree
: Project_Tree_Ref
;
311 Exit_Code
: Osint
.Exit_Code_Type
:= Osint
.E_Success
;
313 -- Terminate program, with or without a message, setting the status code
314 -- according to Fatal. This properly removes all temporary files.
321 with function Add_Switch
324 For_Builder
: Boolean;
325 Has_Global_Compilation_Switches
: Boolean) return Boolean;
326 -- For_Builder is true if we have a builder switch. This function
327 -- should return True in case of success (the switch is valid),
328 -- False otherwise. The error message will be displayed by
329 -- Compute_Builder_Switches itself.
331 -- Has_Global_Compilation_Switches is True if the attribute
332 -- Global_Compilation_Switches is defined in the project.
334 procedure Compute_Builder_Switches
335 (Project_Tree
: Project_Tree_Ref
;
336 Env
: in out Prj
.Tree
.Environment
;
337 Main_Project
: Project_Id
;
338 Only_For_Lang
: Name_Id
:= No_Name
);
339 -- Compute the builder switches and global compilation switches. Every time
340 -- a switch is found in the project, it is passed to Add_Switch. You can
341 -- provide a value for Only_For_Lang so that we only look for this language
342 -- when parsing the global compilation switches.
344 -----------------------
345 -- Project_Tree data --
346 -----------------------
348 -- The following types are specific to builders, and associated with each
349 -- of the loaded project trees.
351 type Binding_Data_Record
;
352 type Binding_Data
is access Binding_Data_Record
;
353 type Binding_Data_Record
is record
354 Language
: Language_Ptr
;
355 Language_Name
: Name_Id
;
356 Binder_Driver_Name
: File_Name_Type
;
357 Binder_Driver_Path
: String_Access
;
358 Binder_Prefix
: Name_Id
;
361 -- Data for a language that have a binder driver
363 type Builder_Project_Tree_Data
is new Project_Tree_Appdata
with record
364 Binding
: Binding_Data
;
366 There_Are_Binder_Drivers
: Boolean := False;
367 -- True when there is a binder driver. Set by Get_Configuration when
368 -- an attribute Language_Processing'Binder_Driver is declared.
369 -- Reset to False if there are no sources of the languages with binder
372 Number_Of_Mains
: Natural := 0;
373 -- Number of main units in this project tree
375 Closure_Needed
: Boolean := False;
376 -- If True, we need to add the closure of the file we just compiled to
377 -- the queue. If False, it is assumed that all files are already on the
378 -- queue so we do not waste time computing the closure.
380 Need_Compilation
: Boolean := True;
381 Need_Binding
: Boolean := True;
382 Need_Linking
: Boolean := True;
383 -- Which of the compilation phases are needed for this project tree
385 type Builder_Data_Access
is access all Builder_Project_Tree_Data
;
387 procedure Free
(Data
: in out Builder_Project_Tree_Data
);
388 -- Free all memory allocated for Data
390 function Builder_Data
(Tree
: Project_Tree_Ref
) return Builder_Data_Access
;
391 -- Return (allocate if needed) tree-specific data
393 procedure Compute_Compilation_Phases
394 (Tree
: Project_Tree_Ref
;
395 Root_Project
: Project_Id
;
396 Option_Unique_Compile
: Boolean := False; -- Was "-u" specified ?
397 Option_Compile_Only
: Boolean := False; -- Was "-c" specified ?
398 Option_Bind_Only
: Boolean := False;
399 Option_Link_Only
: Boolean := False);
400 -- Compute which compilation phases will be needed for Tree. This also does
401 -- the computation for aggregated trees. This also check whether we'll need
402 -- to check the closure of the files we have just compiled to add them to
409 -- Package Mains is used to store the mains specified on the command line
410 -- and to retrieve them when a project file is used, to verify that the
411 -- files exist and that they belong to a project file.
413 -- Mains are stored in a table. An index is used to retrieve the mains
416 type Main_Info
is record
417 File
: File_Name_Type
; -- Always canonical casing
419 Location
: Source_Ptr
:= No_Location
;
421 Source
: Prj
.Source_Id
:= No_Source
;
422 Project
: Project_Id
;
423 Tree
: Project_Tree_Ref
;
426 No_Main_Info
: constant Main_Info
:=
427 (No_File
, 0, No_Location
, No_Source
, No_Project
, null);
433 Location
: Source_Ptr
:= No_Location
;
434 Project
: Project_Id
:= No_Project
;
435 Tree
: Project_Tree_Ref
:= null);
436 -- Add one main to the table. This is in general used to add the main
437 -- files specified on the command line. Index is used for multi-unit
438 -- source files, and indicates which unit in the source is concerned.
439 -- Location is the location within the project file (if a project file
440 -- is used). Project and Tree indicate to which project the main should
441 -- belong. In particular, for aggregate projects, this isn't necessarily
442 -- the main project tree. These can be set to No_Project and null when
443 -- not using projects.
449 -- Reset the cursor to the beginning of the table
451 procedure Set_Multi_Unit_Index
452 (Project_Tree
: Project_Tree_Ref
:= null;
454 -- If a single main file was defined, this subprogram indicates which
455 -- unit inside it is the main (case of a multi-unit source files).
456 -- Errors are raised if zero or more than one main file was defined,
457 -- and Index is non-zaero. This subprogram is used for the handling
458 -- of the command line switch.
460 function Next_Main
return String;
461 function Next_Main
return Main_Info
;
462 -- Moves the cursor forward and returns the new current entry. Returns
463 -- No_Main_Info there are no more mains in the table.
465 function Number_Of_Mains
(Tree
: Project_Tree_Ref
) return Natural;
466 -- Returns the number of mains in this project tree (if Tree is null, it
467 -- returns the total number of project trees)
469 procedure Fill_From_Project
470 (Root_Project
: Project_Id
;
471 Project_Tree
: Project_Tree_Ref
);
472 -- If no main was already added (presumably from the command line), add
473 -- the main units from root_project (or in the case of an aggregate
474 -- project from all the aggregated projects).
476 procedure Complete_Mains
477 (Flags
: Processing_Flags
;
478 Root_Project
: Project_Id
;
479 Project_Tree
: Project_Tree_Ref
);
480 -- If some main units were already added from the command line, check
481 -- that they all belong to the root project, and that they are full
482 -- paths rather than (partial) base names (e.g. no body suffix was
491 type Source_Info_Format
is (Format_Gprbuild
, Format_Gnatmake
);
495 -- The queue of sources to be checked for compilation. There can be a
496 -- single such queue per application.
498 type Source_Info
(Format
: Source_Info_Format
:= Format_Gprbuild
) is
501 when Format_Gprbuild
=>
502 Tree
: Project_Tree_Ref
:= No_Project_Tree
;
503 Id
: Source_Id
:= No_Source
;
504 Closure
: Boolean := False;
506 when Format_Gnatmake
=>
507 File
: File_Name_Type
:= No_File
;
508 Unit
: Unit_Name_Type
:= No_Unit_Name
;
510 Project
: Project_Id
:= No_Project
;
511 Sid
: Source_Id
:= No_Source
;
514 -- Information about files stored in the queue. The exact information
515 -- depends on the builder, and in particular whether it only supports
516 -- project-based files (in which case we have a full Source_Id record).
518 No_Source_Info
: constant Source_Info
:=
519 (Format_Gprbuild
, null, null, False);
522 (Queue_Per_Obj_Dir
: Boolean;
523 Force
: Boolean := False);
524 -- Initialize the queue
526 -- Queue_Per_Obj_Dir matches the --single-compile-per-obj-dir switch:
527 -- when True, there cannot be simultaneous compilations with the object
528 -- files in the same object directory when project files are used.
530 -- Nothing is done if Force is False and the queue was already
533 procedure Remove_Marks
;
534 -- Remove all marks set for the files. This means that the files will be
535 -- handed to the compiler if they are added to the queue, and is mostly
536 -- useful when recompiling several executables in non-project mode, as
537 -- the switches may be different and -s may be in use.
539 function Is_Empty
return Boolean;
540 -- Returns True if the queue is empty
542 function Is_Virtually_Empty
return Boolean;
543 -- Returns True if queue is empty or if all object directories are busy
545 procedure Insert
(Source
: Source_Info
; With_Roots
: Boolean := False);
547 (Source
: Source_Info
; With_Roots
: Boolean := False) return Boolean;
548 -- Insert source in the queue. The second version returns False if the
549 -- Source was already marked in the queue. If With_Roots is True and the
550 -- source is in Format_Gprbuild mode (ie with a project), this procedure
551 -- also includes the "Roots" for this main, ie all the other files that
552 -- must be included in the library or binary (in particular to combine
553 -- Ada and C files connected through pragma Export/Import). When the
554 -- roots are computed, they are also stored in the corresponding
555 -- Source_Id for later reuse by the binder.
557 procedure Insert_Project_Sources
558 (Project
: Project_Id
;
559 Project_Tree
: Project_Tree_Ref
;
560 All_Projects
: Boolean;
561 Unique_Compile
: Boolean);
562 -- Insert all the compilable sources of the project in the queue. If
563 -- All_Project is true, then all sources from imported projects are also
564 -- inserted. Unique_Compile should be true if "-u" was specified on the
565 -- command line: if True and some files were given on the command line),
566 -- only those files will be compiled (so Insert_Project_Sources will do
567 -- nothing). If True and no file was specified on the command line, all
568 -- files of the project(s) will be compiled. This procedure also
569 -- processed aggregated projects.
571 procedure Insert_Withed_Sources_For
572 (The_ALI
: ALI
.ALI_Id
;
573 Project_Tree
: Project_Tree_Ref
;
574 Excluding_Shared_SALs
: Boolean := False);
575 -- Insert in the queue those sources withed by The_ALI, if there are not
576 -- already in the queue and Only_Interfaces is False or they are part of
577 -- the interfaces of their project.
580 (Found
: out Boolean;
581 Source
: out Source_Info
);
582 -- Get the first source that can be compiled from the queue. If no
583 -- source may be compiled, sets Found to False. In this case, the value
584 -- for Source is undefined.
586 function Size
return Natural;
587 -- Return the total size of the queue, including the sources already
590 function Processed
return Natural;
591 -- Return the number of source in the queue that have aready been
594 procedure Set_Obj_Dir_Busy
(Obj_Dir
: Path_Name_Type
);
595 procedure Set_Obj_Dir_Free
(Obj_Dir
: Path_Name_Type
);
596 -- Mark Obj_Dir as busy or free (see the parameter to Initialize)
598 function Element
(Rank
: Positive) return File_Name_Type
;
599 -- Get the file name for element of index Rank in the queue