cmdline: Make sure all errors go through main () function.
[goals.git] / src / run.ml
1 (* Goalfile run
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *)
19
20 open Printf
21
22 open Utils
23
24 (* Goals uses the goal (name + parameters) as the key to
25  * ensure you cannot have two jobs running at the same time
26  * which would interfere with each other by trying to build
27  * the same target.
28  *)
29 module Jobs = Jobs.Make (
30   struct
31     type t = string * Ast.expr list
32     let compare = compare
33     let to_string (name, args) =
34       sprintf "%s (%s)" name
35         (String.concat ", " (List.map (Ast.string_expr ()) args))
36   end
37 )
38
39 let stop_all = Jobs.stop_all
40
41 (* Starts the target expressions running and waits for them to complete. *)
42 let rec run_targets_to_completion env exprs =
43   let group = Jobs.new_group () in
44   run_targets group env exprs;
45   Jobs.wait group
46
47 (* This starts the targets, adding them to the jobs group, but does not
48  * wait for them to complete.
49  *)
50 and run_targets group env exprs =
51   List.iter (run_target group env) exprs
52
53 (* This starts a single target, adding the (usually single but can
54  * be multiple) jobs to the jobs group.  It does not wait for the
55  * jobs to complete.
56  *)
57 and run_target group env = function
58   | Ast.EGoalDefn _ | Ast.EFuncDefn _ | Ast.ETacticDefn _ -> assert false
59
60   (* Call a goal or function. *)
61   | Ast.ECall (loc, name, args) ->
62      let expr = Ast.getvar env loc name in
63      (match expr with
64       | Ast.EGoalDefn (_, goal) ->
65          let key = name, args in
66          Jobs.start group key (
67            fun () -> run_goal env loc name args goal []
68          )
69       | Ast.EFuncDefn (_, func) ->
70          let expr = Eval.call_function env loc name args func in
71          run_target group env expr
72       | _ ->
73          failwithf "%a: tried to call ‘%s’ which is not a goal or a function"
74            Ast.string_loc loc name
75      )
76
77   (* Call a tactic. *)
78   | Ast.ETacticCtor (loc, name, args) ->
79      (* All parameters of tactics must be simple constant expressions
80       * (strings, in future booleans, numbers, etc).
81       *)
82      let args = List.map (Eval.to_constant env) args in
83      run_tactic group env loc name args
84
85   (* If this is a goal then it's the same as calling goal().  If not
86    * then look up the variable and substitute it.
87    *)
88   | Ast.EVar (loc, name) ->
89      let expr = Ast.getvar env loc name in
90      (match expr with
91       | Ast.EGoalDefn (loc, ([], _, _, _)) ->
92          run_target group env (Ast.ECall (loc, name, []))
93       | EGoalDefn _ ->
94          failwithf "%a: cannot call %s() since this goal has parameters"
95            Ast.string_loc loc name
96       | _ ->
97          run_target group env expr
98      )
99
100   (* Lists are inlined when found as a target. *)
101   | Ast.EList (loc, exprs) ->
102      run_targets group env exprs
103
104   (* A string (with or without substitutions) implies *file(filename). *)
105   | Ast.ESubsts (loc, str) ->
106      let str = Eval.substitute env loc str in
107      run_tactic group env loc "*file" [Ast.CString str]
108
109   | Ast.EConstant (loc, c) ->
110      run_tactic group env loc "*file" [c]
111
112 (* Run a goal by name. *)
113 and run_goal env loc name args (params, patterns, deps, code) extra_deps =
114   (* This is used to print the goal in debug and error messages only. *)
115   let debug_goal =
116     sprintf "%s (%s)" name
117       (String.concat ", " (List.map (Ast.string_expr ()) args)) in
118   Cmdline.debug "%a: running goal %s" Ast.string_loc loc debug_goal;
119
120   (* This is the point where we evaluate the goal arguments.  We must
121    * do this before creating the new environment, because variables
122    * appearing in goal arguments don't refer to goal parameters.
123    *)
124   let args = List.map (Eval.evaluate_goal_arg env) args in
125
126   (* Create a new environment which maps the parameter names to
127    * the args.
128    *)
129   let env =
130     let params =
131       try List.combine params args
132       with Invalid_argument _ ->
133         failwithf "%a: calling goal %s with wrong number of arguments, expecting %d args but got %d args"
134           Ast.string_loc loc debug_goal
135           (List.length params) (List.length args) in
136     List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
137
138   (* Check all dependencies have been updated.  We must wait
139    * for these to complete before we can continue.
140    *)
141   run_targets_to_completion env (deps @ extra_deps);
142
143   (* Check if any target (ie. pattern) needs to be rebuilt.
144    * As with make, a goal with no targets is always run.
145    *)
146   let rebuild =
147     patterns = [] ||
148     List.exists (needs_rebuild env loc deps extra_deps) patterns in
149
150   if rebuild then (
151     (* Run the code (if any). *)
152     (match code with
153      | None -> () (* No { CODE } section. *)
154
155      | Some code ->
156         (* Add some standard variables to the environment. *)
157         let expr_of_substs s = Ast.ESubsts (Ast.noloc, s) in
158         let expr_of_pattern = function
159           | Ast.PTactic (loc, tactic, targs) ->
160              Ast.ETacticCtor (loc, tactic, List.map expr_of_substs targs)
161         in
162         let pexprs = List.map expr_of_pattern patterns in
163         let env = Ast.Env.add "@" (Ast.EList (Ast.noloc, pexprs)) env in
164         let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
165         let env =
166           (* NB: extra_deps are not added to %^ *)
167           match deps with
168           | [] -> env
169           | d :: _ -> Ast.Env.add "^" d env in
170         let r = Eval.run_code env loc code in
171         if r <> 0 then
172           failwithf "goal ‘%s’ failed with exit code %d" name r;
173
174         (* Check all targets were updated after the code was
175          * run (else it's an error).
176          *)
177         let pattern_still_needs_rebuild =
178           try
179             Some (List.find (needs_rebuild env loc deps extra_deps) patterns)
180           with
181             Not_found -> None in
182         match pattern_still_needs_rebuild with
183         | None -> ()
184         | Some pattern ->
185            failwithf "%a: goal %s ran successfully but it did not rebuild %a"
186              Ast.string_loc loc debug_goal Ast.string_pattern pattern
187     )
188   )
189
190 (* Return whether the target (pattern) needs to be rebuilt. *)
191 and needs_rebuild env loc deps extra_deps pattern =
192   Cmdline.debug "%a: testing if %a needs rebuild"
193     Ast.string_loc loc Ast.string_pattern pattern;
194
195   match pattern with
196   | Ast.PTactic (loc, tactic, targs) ->
197      (* Look up the tactic. *)
198      let params, code = Ast.gettactic env loc tactic in
199
200      (* Resolve the targs down to constants.  Since needs_rebuild
201       * should be called with env containing the goal params, this
202       * should substitute any parameters in the tactic arguments.
203       *)
204      let targs = List.map (Eval.substitute env loc) targs in
205      let targs =
206        List.map (fun targ ->
207            Ast.EConstant (Ast.noloc, Ast.CString targ)) targs in
208
209      (* Create a new environment binding parameter names
210       * to tactic args.
211       *)
212      let env =
213        let params =
214          try List.combine params targs
215          with Invalid_argument _ ->
216            failwithf "%a: calling tactic ‘%s’ with wrong number of arguments"
217              Ast.string_loc loc tactic in
218        List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
219
220      (* Add some standard variables to the environment. *)
221      let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
222      let env =
223        (* NB: extra_deps are not added to %^ *)
224        match deps with
225        | [] -> env
226        | d :: _ -> Ast.Env.add "^" d env in
227      let r = Eval.run_code env loc code in
228      if r = 99 (* means "needs rebuild" *) then true
229      else if r = 0 (* means "doesn't need rebuild" *) then false
230      else
231        failwithf "tactic ‘%s’ failed with exit code %d" tactic r
232
233 (* Find the goal which matches the given tactic and start it.
234  * cargs is a list of parameters (all constants).
235  *)
236 and run_tactic group env loc tactic cargs =
237   (* This is used to print the tactic in debug and error messages only. *)
238   let debug_tactic =
239     Ast.string_expr ()
240       (Ast.ETacticCtor (loc, tactic,
241                         List.map (fun c -> Ast.EConstant (loc, c)) cargs)) in
242   Cmdline.debug "%a: running tactic %s" Ast.string_loc loc debug_tactic;
243
244   (* Find all goals in the environment.  Returns a list of (name, goal). *)
245   let goals =
246     let env = Ast.Env.bindings env in
247     filter_map
248       (function
249        | name, Ast.EGoalDefn (loc, goal) -> Some (name, goal)
250        | _ -> None) env in
251
252   (* Find all patterns.  Returns a list of (pattern, name, goal). *)
253   let patterns : (Ast.pattern * Ast.id * Ast.goal) list =
254     List.flatten
255       (List.map (fun (name, ((_, patterns, _, _) as goal)) ->
256            List.map (fun pattern -> (pattern, name, goal)) patterns) goals) in
257
258   (* Find any patterns (ie. tactics) which match the one we
259    * are searching for.  This returns a binding for the goal args,
260    * so we end up with a list of (pattern, name, goal, args).
261    *)
262   let patterns : (Ast.pattern * Ast.id * Ast.goal * Ast.expr list) list =
263     filter_map (
264       fun (pattern, name, ((params, _, _, _) as goal)) ->
265         match matching_pattern env loc tactic cargs pattern params with
266         | None -> None
267         | Some args -> Some (pattern, name, goal, args)
268     ) patterns in
269
270   match patterns with
271   | [] ->
272      (* There's no matching goal, but we don't need one if
273       * the tactic doesn't need to be rebuilt.
274       *)
275      let targs = List.map (function Ast.CString s -> [Ast.SString s]) cargs in
276      let p = Ast.PTactic (loc, tactic, targs) in
277      if needs_rebuild env loc [] [] p then
278        failwithf "%a: don't know how to build %s"
279          Ast.string_loc loc debug_tactic
280
281   | [_, name, goal, args] ->
282      (* Single goal matches, run it. *)
283      let key = name, args in
284      Jobs.start group key (
285        fun () -> run_goal env loc name args goal []
286      )
287
288   | goals ->
289      (* Two or more goals match.  Only one must have a CODE section,
290       * and we combine the dependencies into a "supergoal".
291       *)
292      let with_code, without_code =
293        List.partition (
294          fun (_, _, (_, _, _, code), _) -> code <> None
295        ) goals in
296
297      let (_, name, goal, args), extra_deps =
298        match with_code with
299        | [g] ->
300           let extra_deps =
301             List.flatten (
302               List.map (fun (_, _, (_, _, deps, _), _) -> deps) without_code
303             ) in
304           (g, extra_deps)
305
306        | [] ->
307           (* This is OK, it means we'll rebuild all dependencies
308            * but there is no code to run.  Pick the first goal
309            * without code and the dependencies from the other goals.
310            *)
311           let g = List.hd without_code in
312           let extra_deps =
313             List.flatten (
314               List.map (fun (_, _, (_, _, deps, _), _) -> deps)
315                 (List.tl without_code)
316             ) in
317           (g, extra_deps)
318
319        | _ :: _ ->
320           failwithf "%a: multiple goals found which match tactic %s, but more than one of these goals have {code} sections which is not allowed"
321             Ast.string_loc loc debug_tactic in
322
323      let key = name, args in
324      Jobs.start group key (fun () ->
325        run_goal env loc name args goal extra_deps
326      )
327
328 (* Test if pattern matches *tactic(cargs).  If it does
329  * then we return Some args where args is the arguments that must
330  * be passed to the matching goal.  The params parameter is
331  * the names of the parameters of that goal.
332  *)
333 and matching_pattern env loc tactic cargs pattern params =
334   match pattern with
335   | Ast.PTactic (loc, ttactic, targs)
336        when tactic <> ttactic ||
337             List.length cargs <> List.length targs ->
338      None (* Can't possibly match if tactic name or #args is different. *)
339   | Ast.PTactic (loc, ttactic, targs) ->
340      (* Do the args match with a possible params binding? *)
341      try Some (matching_params env loc params targs cargs)
342      with Not_found -> None
343
344 (* Return a possible binding.  For example the goal is:
345  *   goal compile (name) = "%name.o": "%name.c" {}
346  * which means that params = ["name"] and targs = ["%name.o"].
347  *
348  * If we are called with cargs = ["file1.o"], we would
349  * return ["file1"].
350  *
351  * On non-matching this raises Not_found.
352  *)
353 and matching_params env loc params targs cargs =
354   (* This is going to record the resulting binding. *)
355   let res = ref Ast.Env.empty in
356   List.iter2 (matching_param env loc params res) targs cargs;
357
358   (* Rearrange the result into goal parameter order.  Also this
359    * checks that every parameter got a binding.
360    *)
361   let res = !res in
362   List.map (
363     (* Allow the Not_found exception to escape if no binding for this param. *)
364     fun param -> Ast.Env.find param res
365   ) params
366
367 (* If targ = "%name.o" and carg = "file.o" then this would set
368  * name => "file" in !res.  If they don't match, raises Not_found.
369  *)
370 and matching_param env loc params res targ carg =
371   match carg with
372   | Ast.CString carg ->
373      (* Substitute any non parameters in targ from the environment. *)
374      let targ =
375        List.map (
376          function
377          | Ast.SString _ as s -> s
378          | Ast.SVar name ->
379             if not (List.mem name params) then (
380               try
381                 let expr = Ast.getvar env loc name in
382                 match Eval.to_constant env expr with
383                 | Ast.CString s -> Ast.SString s
384               with Failure _ -> raise Not_found
385             )
386             else
387               Ast.SVar name
388        ) targ in
389
390      (* Do the actual pattern matching.  Any remaining SVar elements
391       * must refer to goal parameters.
392       *)
393      let carg = ref carg in
394      let rec loop = function
395        | [] ->
396           (* End of targ, we must have matched all of carg. *)
397           if !carg <> "" then raise Not_found
398        | Ast.SString s :: rest ->
399           (* Does this match the first part of !carg? *)
400           let clen = String.length !carg in
401           let slen = String.length s in
402           if slen > clen || s <> String.sub !carg 0 slen then
403             raise Not_found;
404           (* Yes, so continue after the matching prefix. *)
405           carg := String.sub !carg slen (clen-slen);
406           loop rest
407        | Ast.SVar name :: Ast.SString s :: rest ->
408           (* This is a goal parameter.  Find s later in !carg. *)
409           let i = string_find !carg s in
410           if i = -1 then raise Not_found;
411           (* Set the binding in !res. *)
412           let r = Ast.EConstant (Ast.noloc,
413                                  Ast.CString (String.sub !carg 0 i)) in
414           res := Ast.Env.add name r !res;
415           (* Continue after the match. *)
416           let skip = i + String.length s in
417           carg := String.sub !carg skip (String.length !carg - skip);
418           loop rest
419        | Ast.SVar name :: [] ->
420           (* Matches the whole remainder of the string. *)
421           let r = Ast.EConstant (Ast.noloc, Ast.CString !carg) in
422           res := Ast.Env.add name r !res
423        | Ast.SVar x :: Ast.SVar y :: _ ->
424           (* TODO! We cannot match a target like "%x%y". *)
425           assert false
426      in
427      loop targ