1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              P R J . E N V                               --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2001-2014, 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 implements services for Project-aware tools, mostly related
27--  to the environment (configuration pragma files, path files, mapping files).
28
29with GNAT.Dynamic_HTables;
30with GNAT.OS_Lib;
31
32package Prj.Env is
33
34   procedure Initialize (In_Tree : Project_Tree_Ref);
35   --  Initialize global components relative to environment variables
36
37   procedure Print_Sources (In_Tree : Project_Tree_Ref);
38   --  Output the list of sources after Project files have been scanned
39
40   procedure Create_Mapping (In_Tree : Project_Tree_Ref);
41   --  Create in memory mapping from the sources of all the projects (in body
42   --  of package Fmap), so that Osint.Find_File will find the correct path
43   --  corresponding to a source.
44
45   procedure Create_Temp_File
46     (Shared    : Shared_Project_Tree_Data_Access;
47      Path_FD   : out File_Descriptor;
48      Path_Name : out Path_Name_Type;
49      File_Use  : String);
50   --  Create temporary file, fail with an error if it could not be created
51
52   procedure Create_Mapping_File
53     (Project  : Project_Id;
54      Language : Name_Id;
55      In_Tree  : Project_Tree_Ref;
56      Name     : out Path_Name_Type);
57   --  Create a temporary mapping file for project Project. For each source or
58   --  template of Language in the Project, put the mapping of its file name
59   --  and path name in this file. See fmap for a description of the format
60   --  of the mapping file.
61   --
62   --  Implementation note: we pass a language name, not a language_index here,
63   --  since the latter would have to match exactly the index of that language
64   --  for the specified project, and that is not information available in
65   --  buildgpr.adb.
66
67   procedure Create_Config_Pragmas_File
68     (For_Project : Project_Id;
69      In_Tree     : Project_Tree_Ref);
70   --  If we need SFN pragmas, either for non standard naming schemes or for
71   --  individual units.
72
73   procedure Create_New_Path_File
74     (Shared    : Shared_Project_Tree_Data_Access;
75      Path_FD   : out File_Descriptor;
76      Path_Name : out Path_Name_Type);
77   --  Create a new temporary path file, placing file name in Path_Name
78
79   function Ada_Include_Path
80     (Project   : Project_Id;
81      In_Tree   : Project_Tree_Ref;
82      Recursive : Boolean := False) return String;
83   --  Get the source search path of a Project file. If Recursive it True, get
84   --  all the source directories of the imported and modified project files
85   --  (recursively). If Recursive is False, just get the path for the source
86   --  directories of Project. Note: the resulting String may be empty if there
87   --  is no source directory in the project file.
88
89   function Ada_Objects_Path
90     (Project             : Project_Id;
91      In_Tree             : Project_Tree_Ref;
92      Including_Libraries : Boolean := True) return String_Access;
93   --  Get the ADA_OBJECTS_PATH of a Project file. For the first call with the
94   --  exact same parameters, compute it and cache it. When Including_Libraries
95   --  is True, the object directory of a library project is replaced with the
96   --  library ALI directory of this project (usually the library directory of
97   --  the project, except when attribute Library_ALI_Dir is declared) except
98   --  when the library ALI directory does not contain any ALI file.
99
100   procedure Set_Ada_Paths
101     (Project             : Project_Id;
102      In_Tree             : Project_Tree_Ref;
103      Including_Libraries : Boolean;
104      Include_Path        : Boolean := True;
105      Objects_Path        : Boolean := True);
106   --  Set the environment variables for additional project path files, after
107   --  creating the path files if necessary.
108
109   function File_Name_Of_Library_Unit_Body
110     (Name              : String;
111      Project           : Project_Id;
112      In_Tree           : Project_Tree_Ref;
113      Main_Project_Only : Boolean := True;
114      Full_Path         : Boolean := False) return String;
115   --  Returns the file name of a library unit, in canonical case. Name may or
116   --  may not have an extension (corresponding to the naming scheme of the
117   --  project). If there is no body with this name, but there is a spec, the
118   --  name of the spec is returned.
119   --
120   --  If Full_Path is False (the default), the simple file name is returned.
121   --  If Full_Path is True, the absolute path name is returned.
122   --
123   --  If neither a body nor a spec can be found, an empty string is returned.
124   --  If Main_Project_Only is True, the unit must be an immediate source of
125   --  Project. If it is False, it may be a source of one of its imported
126   --  projects.
127
128   function Project_Of
129     (Name         : String;
130      Main_Project : Project_Id;
131      In_Tree      : Project_Tree_Ref) return Project_Id;
132   --  Get the project of a source. The source file name may be truncated
133   --  (".adb" or ".ads" may be missing). If the source is in a project being
134   --  extended, return the ultimate extending project. If it is not a source
135   --  of any project, return No_Project.
136
137   procedure Get_Reference
138     (Source_File_Name : String;
139      In_Tree          : Project_Tree_Ref;
140      Project          : out Project_Id;
141      Path             : out Path_Name_Type);
142   --  Returns the project of a source and its path in displayable form
143
144   generic
145      with procedure Action (Path : String);
146   procedure For_All_Source_Dirs
147     (Project : Project_Id;
148      In_Tree : Project_Tree_Ref);
149   --  Iterate through all the source directories of a project, including those
150   --  of imported or modified projects. Only returns those directories that
151   --  potentially contain Ada sources (ie ignore projects that have no Ada
152   --  sources
153
154   generic
155      with procedure Action (Path : String);
156   procedure For_All_Object_Dirs
157     (Project : Project_Id;
158      Tree    : Project_Tree_Ref);
159   --  Iterate through all the object directories of a project, including those
160   --  of imported or modified projects.
161
162   ------------------
163   -- Project Path --
164   ------------------
165
166   type Project_Search_Path is private;
167   --  An abstraction of the project path. This object provides subprograms
168   --  to search for projects on the path (and caches the results to improve
169   --  efficiency).
170
171   No_Project_Search_Path : constant Project_Search_Path;
172
173   procedure Initialize_Default_Project_Path
174     (Self         : in out Project_Search_Path;
175      Target_Name  : String;
176      Runtime_Name : String := "");
177   --  Initialize Self. It will then contain the default project path on
178   --  the given target and runtime (including directories specified by the
179   --  environment variables GPR_PROJECT_PATH_FILE, GPR_PROJECT_PATH and
180   --  ADA_PROJECT_PATH). If one of the directory or Target_Name is "-", then
181   --  the path contains only those directories specified by the environment
182   --  variables (except "-"). This does nothing if Self has already been
183   --  initialized.
184
185   procedure Copy (From : Project_Search_Path; To : out Project_Search_Path);
186   --  Copy From into To
187
188   procedure Initialize_Empty (Self : in out Project_Search_Path);
189   --  Initialize self with an empty list of directories. If Self had already
190   --  been set, it is reset.
191
192   function Is_Initialized (Self : Project_Search_Path) return Boolean;
193   --  Whether Self has been initialized
194
195   procedure Free (Self : in out Project_Search_Path);
196   --  Free the memory used by Self
197
198   procedure Add_Directories
199     (Self    : in out Project_Search_Path;
200      Path    : String;
201      Prepend : Boolean := False);
202   --  Add one or more directories to the path. Directories added with this
203   --  procedure are added in order after the current directory and before the
204   --  path given by the environment variable GPR_PROJECT_PATH. A value of "-"
205   --  will remove the default project directory from the project path.
206   --
207   --  Calls to this subprogram must be performed before the first call to
208   --  Find_Project below, or PATH will be added at the end of the search path.
209
210   procedure Get_Path (Self : Project_Search_Path; Path : out String_Access);
211   --  Return the current value of the project path, either the value set
212   --  during elaboration of the package or, if procedure Set_Project_Path has
213   --  been called, the value set by the last call to Set_Project_Path. The
214   --  returned value must not be modified.
215   --  Self must have been initialized first.
216
217   procedure Set_Path (Self : in out Project_Search_Path; Path : String);
218   --  Override the value of the project path. This also removes the implicit
219   --  default search directories.
220
221   generic
222      with function Check_Filename (Name : String) return Boolean;
223   function Find_Name_In_Path
224     (Self : Project_Search_Path;
225      Path : String) return String_Access;
226   --  Find a name in the project search path of Self. Check_Filename is
227   --  the predicate to valid the search.  If Path is an absolute filename,
228   --  simply calls the predicate with Path. Otherwise, calls the predicate
229   --  for each component of the path. Stops as soon as the predicate
230   --  returns True and returns the name, or returns null in case of failure.
231
232   procedure Find_Project
233     (Self               : in out Project_Search_Path;
234      Project_File_Name  : String;
235      Directory          : String;
236      Path               : out Namet.Path_Name_Type);
237   --  Search for a project with the given name either in Directory (which
238   --  often will be the directory contain the project we are currently parsing
239   --  and which we found a reference to another project), or in the project
240   --  path Self. Self must have been initialized first.
241   --
242   --  Project_File_Name can optionally contain directories, and the extension
243   --  (.gpr) for the file name is optional.
244   --
245   --  Returns No_Name if no such project was found
246
247   function Get_Runtime_Path
248     (Self : Project_Search_Path;
249      Name : String) return String_Access;
250   --  Compute the full path for the project-based runtime name.
251   --  Name is simply searched on the project path.
252
253private
254   package Projects_Paths is new GNAT.Dynamic_HTables.Simple_HTable
255     (Header_Num => Header_Num,
256      Element    => Path_Name_Type,
257      No_Element => No_Path,
258      Key        => Name_Id,
259      Hash       => Hash,
260      Equal      => "=");
261
262   type Project_Search_Path is record
263      Path : GNAT.OS_Lib.String_Access;
264      --  As a special case, if the first character is '#:" or this variable
265      --  is unset, this means that the PATH has not been fully initialized
266      --  yet (although subprograms above will properly take care of that).
267
268      Cache : Projects_Paths.Instance;
269   end record;
270
271   No_Project_Search_Path : constant Project_Search_Path :=
272                              (Path  => null,
273                               Cache => Projects_Paths.Nil);
274
275end Prj.Env;
276