1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                            M A K E _ U T I L                             --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2018, 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 various subprograms used by the builders, in
27--  particular those subprograms related to build queue management.
28
29with Namet; use Namet;
30with Opt;
31with Osint;
32with Types; use Types;
33
34with GNAT.OS_Lib; use GNAT.OS_Lib;
35
36package Make_Util is
37
38   type Fail_Proc is access procedure (S : String);
39   --  Pointer to procedure which outputs a failure message
40
41   --  Root_Environment : Prj.Tree.Environment;
42   --  The environment coming from environment variables and command line
43   --  switches. When we do not have an aggregate project, this is used for
44   --  parsing the project tree. When we have an aggregate project, this is
45   --  used to parse the aggregate project; the latter then generates another
46   --  environment (with additional external values and project path) to parse
47   --  the aggregated projects.
48
49   --  Default_Config_Name : constant String := "default.cgpr";
50   --  Name of the configuration file used by gprbuild and generated by
51   --  gprconfig by default.
52
53   On_Windows : constant Boolean := Directory_Separator = '\';
54   --  True when on Windows
55
56   Source_Info_Option : constant String := "--source-info=";
57   --  Switch to indicate the source info file
58
59   Subdirs_Option : constant String := "--subdirs=";
60   --  Switch used to indicate that the real directories (object, exec,
61   --  library, ...) are subdirectories of those in the project file.
62
63   Relocate_Build_Tree_Option : constant String := "--relocate-build-tree";
64   --  Switch to build out-of-tree. In this context the object, exec and
65   --  library directories are relocated to the current working directory
66   --  or the directory specified as parameter to this option.
67
68   Unchecked_Shared_Lib_Imports : constant String :=
69                                    "--unchecked-shared-lib-imports";
70   --  Command line switch to allow shared library projects to import projects
71   --  that are not shared library projects.
72
73   Single_Compile_Per_Obj_Dir_Switch : constant String :=
74                                         "--single-compile-per-obj-dir";
75   --  Switch to forbid simultaneous compilations for the same object directory
76   --  when project files are used.
77
78   Create_Map_File_Switch : constant String := "--create-map-file";
79   --  Switch to create a map file when an executable is linked
80
81   No_Exit_Message_Option : constant String := "--no-exit-message";
82   --  Switch to suppress exit error message when there are compilation
83   --  failures. This is useful when a tool, such as gnatprove, silently calls
84   --  the builder and does not want to pollute its output with error messages
85   --  coming from the builder. This is an internal switch.
86
87   Keep_Temp_Files_Option : constant String := "--keep-temp-files";
88   --  Switch to suppress deletion of temp files created by the builder.
89   --  Note that debug switch -gnatdn also has this effect.
90
91   procedure Add
92     (Option : String_Access;
93      To     : in out String_List_Access;
94      Last   : in out Natural);
95   procedure Add
96     (Option : String;
97      To     : in out String_List_Access;
98      Last   : in out Natural);
99   --  Add a string to a list of strings
100
101   function Create_Name (Name : String) return File_Name_Type;
102   function Create_Name (Name : String) return Name_Id;
103   function Create_Name (Name : String) return Path_Name_Type;
104   --  Get an id for a name
105
106   function Base_Name_Index_For
107     (Main            : String;
108      Main_Index      : Int;
109      Index_Separator : Character) return File_Name_Type;
110   --  Returns the base name of Main, without the extension, followed by the
111   --  Index_Separator followed by the Main_Index if it is non-zero.
112
113   function Executable_Prefix_Path return String;
114   --  Return the absolute path parent directory of the directory where the
115   --  current executable resides, if its directory is named "bin", otherwise
116   --  return an empty string. When a directory is returned, it is guaranteed
117   --  to end with a directory separator.
118
119   procedure Inform (N : Name_Id := No_Name; Msg : String);
120   procedure Inform (N : File_Name_Type; Msg : String);
121   --  Prints out the program name followed by a colon, N and S
122
123   procedure Ensure_Absolute_Path
124     (Switch               : in out String_Access;
125      Parent               : String;
126      Do_Fail              : Fail_Proc;
127      For_Gnatbind         : Boolean := False;
128      Including_Non_Switch : Boolean := True;
129      Including_RTS        : Boolean := False);
130   --  Do nothing if Switch is an absolute path switch. If relative, fail if
131   --  Parent is the empty string, otherwise prepend the path with Parent. This
132   --  subprogram is only used when using project files. If For_Gnatbind is
133   --  True, consider gnatbind specific syntax for -L (not a path, left
134   --  unchanged) and -A (path is optional, preceded with "=" if present).
135   --  If Including_RTS is True, process also switches --RTS=. Do_Fail is
136   --  called in case of error. Using Osint.Fail might be appropriate.
137
138   type Name_Ids is array (Positive range <>) of Name_Id;
139   No_Names : constant Name_Ids := (1 .. 0 => No_Name);
140   --  Name_Ids is used for list of language names in procedure Get_Directories
141   --  below.
142
143   function Path_Or_File_Name (Path : Path_Name_Type) return String;
144   --  Returns a file name if -df is used, otherwise return a path name
145
146   function Unit_Index_Of (ALI_File : File_Name_Type) return Int;
147   --  Find the index of a unit in a source file. Return zero if the file is
148   --  not a multi-unit source file.
149
150   procedure Verbose_Msg
151     (N1                : Name_Id;
152      S1                : String;
153      N2                : Name_Id := No_Name;
154      S2                : String  := "";
155      Prefix            : String  := "  -> ";
156      Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low);
157   procedure Verbose_Msg
158     (N1                : File_Name_Type;
159      S1                : String;
160      N2                : File_Name_Type := No_File;
161      S2                : String  := "";
162      Prefix            : String  := "  -> ";
163      Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low);
164   --  If the verbose flag (Verbose_Mode) is set and the verbosity level is at
165   --  least equal to Minimum_Verbosity, then print Prefix to standard output
166   --  followed by N1 and S1. If N2 /= No_Name then N2 is printed after S1. S2
167   --  is printed last. Both N1 and N2 are printed in quotation marks. The two
168   --  forms differ only in taking Name_Id or File_Name_Type arguments.
169
170   Max_Header_Num : constant := 6150;
171   type Header_Num is range 0 .. Max_Header_Num;
172   --  Size for hash table below. The upper bound is an arbitrary value, the
173   --  value here was chosen after testing to determine a good compromise
174   --  between speed of access and memory usage.
175
176   function Hash (Name : Name_Id)        return Header_Num;
177   function Hash (Name : File_Name_Type) return Header_Num;
178   function Hash (Name : Path_Name_Type) return Header_Num;
179
180   -------------------------
181   -- Program termination --
182   -------------------------
183
184   procedure Fail_Program
185     (S              : String;
186      Flush_Messages : Boolean := True);
187   --  Terminate program with a message and a fatal status code
188
189   procedure Finish_Program
190     (Exit_Code    : Osint.Exit_Code_Type := Osint.E_Success;
191      S            : String := "");
192   --  Terminate program, with or without a message, setting the status code
193   --  according to Fatal. This properly removes all temporary files.
194
195   -----------
196   -- Mains --
197   -----------
198
199   --  Package Mains is used to store the mains specified on the command line
200   --  and to retrieve them when a project file is used, to verify that the
201   --  files exist and that they belong to a project file.
202
203   --  Mains are stored in a table. An index is used to retrieve the mains
204   --  from the table.
205
206   type Main_Info is record
207      File      : File_Name_Type;  --  Always canonical casing
208      Index     : Int := 0;
209   end record;
210
211   No_Main_Info : constant Main_Info := (No_File, 0);
212
213   package Mains is
214      procedure Add_Main (Name : String; Index : Int := 0);
215      --  Add one main to the table. This is in general used to add the main
216      --  files specified on the command line. Index is used for multi-unit
217      --  source files, and indicates which unit in the source is concerned.
218
219      procedure Delete;
220      --  Empty the table
221
222      procedure Reset;
223      --  Reset the cursor to the beginning of the table
224
225      procedure Set_Multi_Unit_Index
226        (Index        : Int := 0);
227      --  If a single main file was defined, this subprogram indicates which
228      --  unit inside it is the main (case of a multi-unit source files).
229      --  Errors are raised if zero or more than one main file was defined,
230      --  and Index is non-zaero. This subprogram is used for the handling
231      --  of the command line switch.
232
233      function Next_Main return String;
234      function Next_Main return Main_Info;
235      --  Moves the cursor forward and returns the new current entry. Returns
236      --  No_Main_Info there are no more mains in the table.
237
238      function Number_Of_Mains return Natural;
239      --  Returns the number of main.
240
241   end Mains;
242
243   -----------
244   -- Queue --
245   -----------
246
247   package Queue is
248
249      --  The queue of sources to be checked for compilation. There can be a
250      --  single such queue per application.
251
252      type Source_Info is
253         record
254            File    : File_Name_Type := No_File;
255            Unit    : Unit_Name_Type := No_Unit_Name;
256            Index   : Int            := 0;
257         end record;
258      --  Information about files stored in the queue.
259
260      No_Source_Info : constant Source_Info := (No_File, No_Unit_Name, 0);
261
262      procedure Initialize (Force : Boolean := False);
263      --  Initialize the queue
264
265      procedure Remove_Marks;
266      --  Remove all marks set for the files. This means that the files will be
267      --  handed to the compiler if they are added to the queue, and is mostly
268      --  useful when recompiling several executables as the switches may be
269      --  different and -s may be in use.
270
271      function Is_Empty return Boolean;
272      --  Returns True if the queue is empty
273
274      procedure Insert (Source : Source_Info);
275      function Insert (Source  : Source_Info) return Boolean;
276      --  Insert source in the queue. The second version returns False if the
277      --  Source was already marked in the queue.
278
279      procedure Extract
280        (Found  : out Boolean;
281         Source : out Source_Info);
282      --  Get the first source that can be compiled from the queue. If no
283      --  source may be compiled, sets Found to False. In this case, the value
284      --  for Source is undefined.
285
286      function Size return Natural;
287      --  Return the total size of the queue, including the sources already
288      --  extracted.
289
290      function Processed return Natural;
291      --  Return the number of source in the queue that have aready been
292      --  processed.
293
294      function Element (Rank : Positive) return File_Name_Type;
295      --  Get the file name for element of index Rank in the queue
296
297   end Queue;
298
299end Make_Util;
300