libgpr2_24.0.0_eda3c693/tools/src/gpr2-compilation-process.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
------------------------------------------------------------------------------
--                                                                          --
--                           GPR2 PROJECT MANAGER                           --
--                                                                          --
--                     Copyright (C) 2019-2023, AdaCore                     --
--                                                                          --
-- This is  free  software;  you can redistribute it and/or modify it under --
-- terms of the  GNU  General Public License as published by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details.  You should have received  a copy of the  GNU  --
-- General Public License distributed with GNAT; see file  COPYING. If not, --
-- see <http://www.gnu.org/licenses/>.                                      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Indefinite_Ordered_Maps;

with GPR2.Containers;

package body GPR2.Compilation.Process is

   use type Containers.Count_Type;

   package Env_Maps renames GPR2.Containers.Name_Value_Map_Package;
   --  A set of key=value

   package Prj_Maps is new Ada.Containers.Indefinite_Ordered_Maps
     (Name_Type, Env_Maps.Map, Env_Maps."<", Env_Maps."=");
   --  A set of project+language=map

   function "<" (Left, Right : Id) return Boolean is
     (Left.R_Pid < Right.R_Pid);

   package Failures_Slave_Set is
     new Ada.Containers.Indefinite_Ordered_Maps (Id, Optional_Name_Type);

   --  function Get_Env
   --    (Project  : GPR2.Project.View.Object;
   --     Language : Language_Id) return String;
   --  --  Get the environment for a specific project and language
   --
   --  function To_Argument_List
   --    (List : Containers.Value_List) return GNAT.OS_Lib.Argument_List;
   --  --  Returns an argument list representation for the list of values

   Environments : Prj_Maps.Map;

   type Process_Data is record
      Process : Id;
      Status  : Boolean;
   end record;

   package Endded_Process is
     new Ada.Containers.Doubly_Linked_Lists (Process_Data);

   protected Results is
      procedure Add (Result : Process_Data);
      --  entry Get (Result : out Process_Data);

      procedure Record_Remote_Failure (Pid : Id; Slave : Name_Type);
      --  This is to be able to display on which slaves a specific compilation
      --  has failed.

      function Get_Slave_For (Pid : Id) return Optional_Name_Type;
      --  Returns the remote slave for the given compilation, or the empty
      --  string if the compilation was successful.

   private
      List        : Endded_Process.List;
      Failed_Proc : Failures_Slave_Set.Map;
   end Results;

   ----------------
   -- Add_Result --
   ----------------

   procedure Add_Result
     (Process : Id;
      Status  : Boolean;
      Slave   : Optional_Name_Type := "") is
   begin
      Results.Add (Process_Data'(Process, Status));

      --  For a compilation failure records the slave to be able to report it

      if not Status and then Slave /= "" then
         Results.Record_Remote_Failure (Process, Slave);
      end if;
   end Add_Result;

   ------------------
   -- Create_Local --
   ------------------

   function Create_Local (Pid : GNAT.OS_Lib.Process_Id) return Id is
   begin
      return Id'(Local, Pid);
   end Create_Local;

   -------------------
   -- Create_Remote --
   -------------------

   function Create_Remote (Pid : Remote_Id) return Id is
   begin
      return Id'(Remote, Pid);
   end Create_Remote;

   -------------
   -- Get_Env --
   -------------

   --  function Get_Env
   --    (Project  : GPR2.Project.View.Object;
   --     Language : Language_Id) return String
   --  is
   --     Key  : constant Name_Type :=
   --              Name_Type (String (Project.Name) & "+" &
   --                           String (Name (Language)));
   --     Res  : Unbounded_String;
   --  begin
   --     if Environments.Contains (Key) then
   --        for C in Environments (Key).Iterate loop
   --           if Res /= Null_Unbounded_String then
   --              Res := Res & Opts_Sep;
   --           end if;
   --
   --           Append
   --             (Res,
   --              String (Env_Maps.Key (C)) & '=' & Env_Maps.Element (C));
   --        end loop;
   --     end if;
   --
   --     return To_String (Res);
   --  end Get_Env;

   -------------------
   -- Get_Slave_For --
   -------------------

   function Get_Slave_For (Pid : Id) return Optional_Name_Type is
   begin
      if Pid.Kind = Local then
         return "";

      else
         return Results.Get_Slave_For (Pid);
      end if;
   end Get_Slave_For;

   ----------
   -- Hash --
   ----------

   function Hash (Process : Id) return Header_Num is
      use GNAT;
      Modulo : constant Integer := Integer (Header_Num'Last) + 1;
   begin
      if Process.Kind = Local then
         return Header_Num (OS_Lib.Pid_To_Integer (Process.Pid) mod Modulo);
      else
         return Header_Num (Process.R_Pid mod Remote_Id (Modulo));
      end if;
   end Hash;

   ------------------------
   -- Record_Environment --
   ------------------------

   procedure Record_Environment
     (Project  : GPR2.Project.View.Object;
      Language : Name_Type;
      Name     : Name_Type;
      Value    : Value_Type)
   is
      Key      : constant Name_Type :=
                   Name_Type (String (Project.Name) & "+" & String (Language));
      New_Item : Env_Maps.Map;
   begin
      --  Create new item, variable association

      New_Item.Include (Name, Value);

      if Environments.Contains (Key) then
         if Environments (Key).Contains (Name) then
            Environments (Key).Replace (Name, Value);
         else
            Environments (Key).Insert (Name, Value);
         end if;

      else
         Environments.Insert (Key, New_Item);
      end if;
   end Record_Environment;

   -------------
   -- Results --
   -------------

   protected body Results is

      ---------
      -- Add --
      ---------

      procedure Add (Result : Process_Data) is
      begin
         List.Append (Result);
      end Add;

      ---------
      -- Get --
      ---------

      --  entry Get (Result : out Process_Data) when List.Length /= 0 is
      --  begin
      --     Result := List.First_Element;
      --     List.Delete_First;
      --  end Get;

      -------------------
      -- Get_Slave_For --
      -------------------

      function Get_Slave_For (Pid : Id) return Optional_Name_Type is
         use type Failures_Slave_Set.Cursor;
         Pos : constant Failures_Slave_Set.Cursor := Failed_Proc.Find (Pid);
      begin
         if Pos = Failures_Slave_Set.No_Element then
            return "";
         else
            return Failures_Slave_Set.Element (Pos);
         end if;
      end Get_Slave_For;

      ---------------------------
      -- Record_Remote_Failure --
      ---------------------------

      procedure Record_Remote_Failure (Pid : Id; Slave : Name_Type) is
      begin
         Failed_Proc.Insert (Pid, Slave);
      end Record_Remote_Failure;

   end Results;

   --  ---------
   --  -- Run --
   --  ---------
   --
   --  function Run
   --    (Executable    : Name_Type;
   --     Options       : GPR2.Containers.Value_List;
   --     Project       : GPR2.Project.View.Object;
   --     GPR_Options   : GPRtools.Options.Object;
   --     Obj_Name      : Name_Type;
   --     Source        : String := "";
   --     Language      : Language_Id := No_Language;
   --     Dep_Name      : String := "";
   --     Output_File   : String := "";
   --     Err_To_Out    : Boolean := False;
   --     Force_Local   : Boolean := False;
   --     Response_File : Path_Name.Object := Path_Name.Undefined) return Id
   --  is
   --     use GNAT.OS_Lib;
   --
   --     Env : constant String := Get_Env (Project, Language);
   --     Success : Boolean;
   --
   --  begin
   --     --  Run locally first, then send jobs to remote slaves. Note that to
   --     --  build remotely we need an output file and a language, if one of
   --    --  this requirement is not fulfilled we just run the process locally.
   --
   --     if Force_Local
   --       or else not GPR_Options.Distributed_Mode
   --       or else Local_Process.Count < GPR_Options.Maximum_Processes
   --       or else Output_File /= ""
   --       or else Language = No_Language
   --     then
   --        Run_Local : declare
   --           P    : Id (Local);
   --           Args : String_List_Access :=
   --                    new String_List'(To_Argument_List (Options));
   --        begin
   --           Set_Env (Env, Fail => True);
   --
   --           if Response_File.Is_Defined then
   --              declare
   --                 Opts : constant GNAT.OS_Lib.Argument_List :=
   --                          (1 => new String'("@" & Response_File.Value));
   --              begin
   --                 P.Pid := Non_Blocking_Spawn (String (Executable), Opts);
   --              end;
   --
   --           elsif Output_File /= "" then
   --              P.Pid := Non_Blocking_Spawn
   --                (String (Executable), Args.all, Output_File, Err_To_Out);
   --
   --           elsif Source /= "" then -- and then not No_Complete_Output then
   --              P.Pid := Non_Blocking_Spawn
   --                (String (Executable), Args.all,
   --                 Stdout_File => Source & ".stdout",
   --                 Stderr_File => Source & ".stderr");
   --
   --           else
   --              if Source /= "" then
   --                 Delete_File (Source & ".stdout", Success);
   --                 Delete_File (Source & ".stderr", Success);
   --              end if;
   --
   --              P.Pid := Non_Blocking_Spawn (String (Executable), Args.all);
   --           end if;
   --
   --           --  ??? to be written
   --           --  Script_Write (String (Executable), Options);
   --           Free (Args);
   --
   --           Local_Process.Increment;
   --
   --           return P;
   --        end Run_Local;
   --
   --     else
   --        --  Even if the compilation is done remotely make sure that any
   --        --  .stderr/.stdout from a previous local compilation are removed.
   --
   --        if Source /= "" then
   --           Delete_File (Source & ".stdout", Success);
   --           Delete_File (Source & ".stderr", Success);
   --        end if;
   --
   --        return Registry.Run
   --          (Project, Language, Options, Obj_Name, Dep_Name, Env);
   --     end if;
   --  end Run;

   ----------------------
   -- To_Argument_List --
   ----------------------

   --  function To_Argument_List
   --    (List : Containers.Value_List) return GNAT.OS_Lib.Argument_List
   --  is
   --     Ret : GNAT.OS_Lib.Argument_List (1 .. Natural (List.Length));
   --  begin
   --     for J in 1 .. List.Last_Index loop
   --        Ret (J) := new String'(List (J));
   --     end loop;
   --
   --     return Ret;
   --  end To_Argument_List;

   -----------------
   -- Wait_Result --
   -----------------

   --  procedure Wait_Result (Process : out Id; Status : out Boolean) is
   --     Data : Process_Data;
   --  begin
   --     Results.Get (Data);
   --     Process := Data.Process;
   --     Status := Data.Status;
   --  end Wait_Result;

end GPR2.Compilation.Process;