1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              M A K E U T L                               --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2013, 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 project management and build
28--  queue management.
29
30with ALI;
31with Namet;    use Namet;
32with Opt;
33with Osint;
34with Prj;      use Prj;
35with Prj.Tree;
36with Snames;   use Snames;
37with Table;
38with Types;    use Types;
39
40with GNAT.OS_Lib; use GNAT.OS_Lib;
41
42package Makeutl is
43
44   type Fail_Proc is access procedure (S : String);
45   --  Pointer to procedure which outputs a failure message
46
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.
54
55   Default_Config_Name : constant String := "default.cgpr";
56   --  Name of the configuration file used by gprbuild and generated by
57   --  gprconfig by default.
58
59   On_Windows : constant Boolean := Directory_Separator = '\';
60   --  True when on Windows
61
62   Source_Info_Option : constant String := "--source-info=";
63   --  Switch to indicate the source info file
64
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.
68
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.
73
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.
78
79   Create_Map_File_Switch : constant String := "--create-map-file";
80   --  Switch to create a map file when an executable is linked
81
82   Load_Standard_Base : Boolean := True;
83   --  False when gprbuild is called with --db-
84
85   package Db_Switch_Args is new Table.Table
86     (Table_Component_Type => Name_Id,
87      Table_Index_Type     => Integer,
88      Table_Low_Bound      => 1,
89      Table_Initial        => 200,
90      Table_Increment      => 100,
91      Table_Name           => "Makegpr.Db_Switch_Args");
92   --  Table of all the arguments of --db switches of gprbuild
93
94   package Directories is new Table.Table
95     (Table_Component_Type => Path_Name_Type,
96      Table_Index_Type     => Integer,
97      Table_Low_Bound      => 1,
98      Table_Initial        => 200,
99      Table_Increment      => 100,
100      Table_Name           => "Makegpr.Directories");
101   --  Table of all the source or object directories, filled up by
102   --  Get_Directories.
103
104   procedure Add
105     (Option : String_Access;
106      To     : in out String_List_Access;
107      Last   : in out Natural);
108   procedure Add
109     (Option : String;
110      To     : in out String_List_Access;
111      Last   : in out Natural);
112   --  Add a string to a list of strings
113
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
118
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
122
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
127
128   function Base_Name_Index_For
129     (Main            : String;
130      Main_Index      : Int;
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.
134
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.
140
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
144
145   function File_Not_A_Source_Of
146     (Project_Tree : Project_Tree_Ref;
147      Uname        : Name_Id;
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.
152
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.
159
160   procedure Ensure_Absolute_Path
161     (Switch               : in out String_Access;
162      Parent               : String;
163      Do_Fail              : Fail_Proc;
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.
174
175   function Is_Subunit (Source : Source_Id) return Boolean;
176   --  Return True if source is a subunit
177
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.
181
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
186   --
187   --  Correct forms are:
188   --
189   --      -Xname=value
190   --      -X"name=other value"
191   --
192   --  Assumptions: 'First = 1, Argv (1 .. 2) = "-X"
193   --
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".
197
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
201   --  below.
202
203   Ada_Only : constant Name_Ids := (1 => Name_Ada);
204   --  Used to invoke Get_Directories in gnatmake
205
206   type Activity_Type is (Compilation, Executable_Binding, SAL_Binding);
207
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.
218
219   function Aggregate_Libraries_In (Tree : Project_Tree_Ref) return Boolean;
220   --  Return True iff there is one or more aggregate library projects in
221   --  the project tree Tree.
222
223   procedure Write_Path_File (FD : File_Descriptor);
224   --  Write in the specified open path file the directories in table
225   --  Directories, then closed the path file.
226
227   procedure Get_Switches
228     (Source       : Source_Id;
229      Pkg_Name     : Name_Id;
230      Project_Tree : Project_Tree_Ref;
231      Value        : out Variable_Value;
232      Is_Default   : out Boolean);
233   procedure Get_Switches
234     (Source_File         : File_Name_Type;
235      Source_Lang         : Name_Id;
236      Source_Prj          : Project_Id;
237      Pkg_Name            : Name_Id;
238      Project_Tree        : Project_Tree_Ref;
239      Value               : out Variable_Value;
240      Is_Default          : out Boolean;
241      Test_Without_Suffix : Boolean := False;
242      Check_ALI_Suffix    : Boolean := False);
243   --  Compute the switches (Compilation switches for instance) for the given
244   --  file. This checks various attributes to see if there are file specific
245   --  switches, or else defaults on the switches for the corresponding
246   --  language. Is_Default is set to False if there were file-specific
247   --  switches Source_File can be set to No_File to force retrieval of the
248   --  default switches. If Test_Without_Suffix is True, and there is no " for
249   --  Switches(Source_File) use", then this procedure also tests without the
250   --  extension of the filename. If Test_Without_Suffix is True and
251   --  Check_ALI_Suffix is True, then we also replace the file extension with
252   --  ".ali" when testing.
253
254   function Linker_Options_Switches
255     (Project  : Project_Id;
256      Do_Fail  : Fail_Proc;
257      In_Tree  : Project_Tree_Ref) return String_List;
258   --  Collect the options specified in the Linker'Linker_Options attributes
259   --  of project Project, in project tree In_Tree, and in the projects that
260   --  it imports directly or indirectly, and returns the result.
261
262   function Path_Or_File_Name (Path : Path_Name_Type) return String;
263   --  Returns a file name if -df is used, otherwise return a path name
264
265   function Unit_Index_Of (ALI_File : File_Name_Type) return Int;
266   --  Find the index of a unit in a source file. Return zero if the file is
267   --  not a multi-unit source file.
268
269   procedure Verbose_Msg
270     (N1                : Name_Id;
271      S1                : String;
272      N2                : Name_Id := No_Name;
273      S2                : String  := "";
274      Prefix            : String  := "  -> ";
275      Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low);
276   procedure Verbose_Msg
277     (N1                : File_Name_Type;
278      S1                : String;
279      N2                : File_Name_Type := No_File;
280      S2                : String  := "";
281      Prefix            : String  := "  -> ";
282      Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low);
283   --  If the verbose flag (Verbose_Mode) is set and the verbosity level is at
284   --  least equal to Minimum_Verbosity, then print Prefix to standard output
285   --  followed by N1 and S1. If N2 /= No_Name then N2 is printed after S1. S2
286   --  is printed last. Both N1 and N2 are printed in quotation marks. The two
287   --  forms differ only in taking Name_Id or File_name_Type arguments.
288
289   -------------------------
290   -- Program termination --
291   -------------------------
292
293   procedure Fail_Program
294     (Project_Tree   : Project_Tree_Ref;
295      S              : String;
296      Flush_Messages : Boolean := True);
297   --  Terminate program with a message and a fatal status code
298
299   procedure Finish_Program
300     (Project_Tree : Project_Tree_Ref;
301      Exit_Code    : Osint.Exit_Code_Type := Osint.E_Success;
302      S            : String := "");
303   --  Terminate program, with or without a message, setting the status code
304   --  according to Fatal. This properly removes all temporary files.
305
306   --------------
307   -- Switches --
308   --------------
309
310   generic
311      with function Add_Switch
312        (Switch      : String;
313         For_Lang    : Name_Id;
314         For_Builder : Boolean;
315         Has_Global_Compilation_Switches : Boolean) return Boolean;
316      --  For_Builder is true if we have a builder switch. This function
317      --  should return True in case of success (the switch is valid),
318      --  False otherwise. The error message will be displayed by
319      --  Compute_Builder_Switches itself.
320      --
321      --  Has_Global_Compilation_Switches is True if the attribute
322      --  Global_Compilation_Switches is defined in the project.
323
324   procedure Compute_Builder_Switches
325     (Project_Tree     : Project_Tree_Ref;
326      Root_Environment : in out Prj.Tree.Environment;
327      Main_Project     : Project_Id;
328      Only_For_Lang    : Name_Id := No_Name);
329   --  Compute the builder switches and global compilation switches. Every time
330   --  a switch is found in the project, it is passed to Add_Switch. You can
331   --  provide a value for Only_For_Lang so that we only look for this language
332   --  when parsing the global compilation switches.
333
334   -----------------------
335   -- Project_Tree data --
336   -----------------------
337
338   --  The following types are specific to builders, and associated with each
339   --  of the loaded project trees.
340
341   type Binding_Data_Record;
342   type Binding_Data is access Binding_Data_Record;
343   type Binding_Data_Record is record
344      Language           : Language_Ptr;
345      Language_Name      : Name_Id;
346      Binder_Driver_Name : File_Name_Type;
347      Binder_Driver_Path : String_Access;
348      Binder_Prefix      : Name_Id;
349      Next               : Binding_Data;
350   end record;
351   --  Data for a language that have a binder driver
352
353   type Builder_Project_Tree_Data is new Project_Tree_Appdata with record
354      Binding : Binding_Data;
355
356      There_Are_Binder_Drivers : Boolean := False;
357      --  True when there is a binder driver. Set by Get_Configuration when
358      --  an attribute Language_Processing'Binder_Driver is declared.
359      --  Reset to False if there are no sources of the languages with binder
360      --  drivers.
361
362      Number_Of_Mains : Natural := 0;
363      --  Number of main units in this project tree
364
365      Closure_Needed : Boolean := False;
366      --  If True, we need to add the closure of the file we just compiled to
367      --  the queue. If False, it is assumed that all files are already on the
368      --  queue so we do not waste time computing the closure.
369
370      Need_Compilation : Boolean := True;
371      Need_Binding     : Boolean := True;
372      Need_Linking     : Boolean := True;
373      --  Which of the compilation phases are needed for this project tree
374   end record;
375   type Builder_Data_Access is access all Builder_Project_Tree_Data;
376
377   procedure Free (Data : in out Builder_Project_Tree_Data);
378   --  Free all memory allocated for Data
379
380   function Builder_Data (Tree : Project_Tree_Ref) return Builder_Data_Access;
381   --  Return (allocate if needed) tree-specific data
382
383   procedure Compute_Compilation_Phases
384     (Tree                  : Project_Tree_Ref;
385      Root_Project          : Project_Id;
386      Option_Unique_Compile : Boolean := False;   --  Was "-u" specified ?
387      Option_Compile_Only   : Boolean := False;   --  Was "-c" specified ?
388      Option_Bind_Only      : Boolean := False;
389      Option_Link_Only      : Boolean := False);
390   --  Compute which compilation phases will be needed for Tree. This also does
391   --  the computation for aggregated trees. This also check whether we'll need
392   --  to check the closure of the files we have just compiled to add them to
393   --  the queue.
394
395   -----------
396   -- Mains --
397   -----------
398
399   --  Package Mains is used to store the mains specified on the command line
400   --  and to retrieve them when a project file is used, to verify that the
401   --  files exist and that they belong to a project file.
402
403   --  Mains are stored in a table. An index is used to retrieve the mains
404   --  from the table.
405
406   type Main_Info is record
407      File      : File_Name_Type;  --  Always canonical casing
408      Index     : Int := 0;
409      Location  : Source_Ptr := No_Location;
410
411      Source    : Prj.Source_Id := No_Source;
412      Project   : Project_Id;
413      Tree      : Project_Tree_Ref;
414   end record;
415
416   No_Main_Info : constant Main_Info :=
417                    (No_File, 0, No_Location, No_Source, No_Project, null);
418
419   package Mains is
420      procedure Add_Main
421        (Name     : String;
422         Index    : Int := 0;
423         Location : Source_Ptr := No_Location;
424         Project  : Project_Id := No_Project;
425         Tree     : Project_Tree_Ref := null);
426      --  Add one main to the table. This is in general used to add the main
427      --  files specified on the command line. Index is used for multi-unit
428      --  source files, and indicates which unit in the source is concerned.
429      --  Location is the location within the project file (if a project file
430      --  is used). Project and Tree indicate to which project the main should
431      --  belong. In particular, for aggregate projects, this isn't necessarily
432      --  the main project tree. These can be set to No_Project and null when
433      --  not using projects.
434
435      procedure Delete;
436      --  Empty the table
437
438      procedure Reset;
439      --  Reset the cursor to the beginning of the table
440
441      procedure Set_Multi_Unit_Index
442        (Project_Tree : Project_Tree_Ref := null;
443         Index        : Int := 0);
444      --  If a single main file was defined, this subprogram indicates which
445      --  unit inside it is the main (case of a multi-unit source files).
446      --  Errors are raised if zero or more than one main file was defined,
447      --  and Index is non-zaero. This subprogram is used for the handling
448      --  of the command line switch.
449
450      function Next_Main return String;
451      function Next_Main return Main_Info;
452      --  Moves the cursor forward and returns the new current entry. Returns
453      --  No_Main_Info there are no more mains in the table.
454
455      function Number_Of_Mains (Tree : Project_Tree_Ref) return Natural;
456      --  Returns the number of mains in this project tree (if Tree is null, it
457      --  returns the total number of project trees)
458
459      procedure Fill_From_Project
460        (Root_Project : Project_Id;
461         Project_Tree : Project_Tree_Ref);
462      --  If no main was already added (presumably from the command line), add
463      --  the main units from root_project (or in the case of an aggregate
464      --  project from all the aggregated projects).
465
466      procedure Complete_Mains
467        (Flags        : Processing_Flags;
468         Root_Project : Project_Id;
469         Project_Tree : Project_Tree_Ref);
470      --  If some main units were already added from the command line, check
471      --  that they all belong to the root project, and that they are full
472      --  paths rather than (partial) base names (e.g. no body suffix was
473      --  specified).
474
475   end Mains;
476
477   -----------
478   -- Queue --
479   -----------
480
481   type Source_Info_Format is (Format_Gprbuild, Format_Gnatmake);
482
483   package Queue is
484
485      --  The queue of sources to be checked for compilation. There can be a
486      --  single such queue per application.
487
488      type Source_Info (Format : Source_Info_Format := Format_Gprbuild) is
489         record
490            case Format is
491               when Format_Gprbuild =>
492                  Tree : Project_Tree_Ref := No_Project_Tree;
493                  Id   : Source_Id        := No_Source;
494
495               when Format_Gnatmake =>
496                  File    : File_Name_Type := No_File;
497                  Unit    : Unit_Name_Type := No_Unit_Name;
498                  Index   : Int            := 0;
499                  Project : Project_Id     := No_Project;
500                  Sid     : Source_Id      := No_Source;
501            end case;
502         end record;
503      --  Information about files stored in the queue. The exact information
504      --  depends on the builder, and in particular whether it only supports
505      --  project-based files (in which case we have a full Source_Id record).
506
507      No_Source_Info : constant Source_Info := (Format_Gprbuild, null, null);
508
509      procedure Initialize
510        (Queue_Per_Obj_Dir : Boolean;
511         Force             : Boolean := False);
512      --  Initialize the queue
513      --
514      --  Queue_Per_Obj_Dir matches the --single-compile-per-obj-dir switch:
515      --  when True, there cannot be simultaneous compilations with the object
516      --  files in the same object directory when project files are used.
517      --
518      --  Nothing is done if Force is False and the queue was already
519      --  initialized.
520
521      procedure Remove_Marks;
522      --  Remove all marks set for the files. This means that the files will be
523      --  handed to the compiler if they are added to the queue, and is mostly
524      --  useful when recompiling several executables in non-project mode, as
525      --  the switches may be different and -s may be in use.
526
527      function Is_Empty return Boolean;
528      --  Returns True if the queue is empty
529
530      function Is_Virtually_Empty return Boolean;
531      --  Returns True if queue is empty or if all object directories are busy
532
533      procedure Insert (Source  : Source_Info; With_Roots : Boolean := False);
534      function Insert
535        (Source  : Source_Info; With_Roots : Boolean := False) return Boolean;
536      --  Insert source in the queue. The second version returns False if the
537      --  Source was already marked in the queue. If With_Roots is True and the
538      --  source is in Format_Gprbuild mode (ie with a project), this procedure
539      --  also includes the "Roots" for this main, ie all the other files that
540      --  must be included in the library or binary (in particular to combine
541      --  Ada and C files connected through pragma Export/Import). When the
542      --  roots are computed, they are also stored in the corresponding
543      --  Source_Id for later reuse by the binder.
544
545      procedure Insert_Project_Sources
546        (Project        : Project_Id;
547         Project_Tree   : Project_Tree_Ref;
548         All_Projects   : Boolean;
549         Unique_Compile : Boolean);
550      --  Insert all the compilable sources of the project in the queue. If
551      --  All_Project is true, then all sources from imported projects are also
552      --  inserted. Unique_Compile should be true if "-u" was specified on the
553      --  command line: if True and some files were given on the command line),
554      --  only those files will be compiled (so Insert_Project_Sources will do
555      --  nothing). If True and no file was specified on the command line, all
556      --  files of the project(s) will be compiled. This procedure also
557      --  processed aggregated projects.
558
559      procedure Insert_Withed_Sources_For
560        (The_ALI               : ALI.ALI_Id;
561         Project_Tree          : Project_Tree_Ref;
562         Excluding_Shared_SALs : Boolean := False);
563      --  Insert in the queue those sources withed by The_ALI, if there are not
564      --  already in the queue and Only_Interfaces is False or they are part of
565      --  the interfaces of their project.
566
567      procedure Extract
568        (Found  : out Boolean;
569         Source : out Source_Info);
570      --  Get the first source that can be compiled from the queue. If no
571      --  source may be compiled, sets Found to False. In this case, the value
572      --  for Source is undefined.
573
574      function Size return Natural;
575      --  Return the total size of the queue, including the sources already
576      --  extracted.
577
578      function Processed return Natural;
579      --  Return the number of source in the queue that have aready been
580      --  processed.
581
582      procedure Set_Obj_Dir_Busy (Obj_Dir : Path_Name_Type);
583      procedure Set_Obj_Dir_Free (Obj_Dir : Path_Name_Type);
584      --  Mark Obj_Dir as busy or free (see the parameter to Initialize)
585
586      function Element (Rank : Positive) return File_Name_Type;
587      --  Get the file name for element of index Rank in the queue
588
589   end Queue;
590
591end Makeutl;
592