Split implementation into dependency analysis and traversal.
[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 let rec goal_runner env loc name args
25                     (params, patterns, deps, code) extra_deps debug_goal =
26   Cmdline.debug "%a: running goal %s" Ast.string_loc loc debug_goal;
27
28   (* Check if any target (ie. pattern) needs to be rebuilt.
29    * As with make, a goal with no targets is always run.
30    *)
31   let rebuild =
32     patterns = [] ||
33     List.exists (needs_rebuild env loc deps extra_deps) patterns in
34
35   if rebuild then (
36     (* Run the code (if any). *)
37     (match code with
38      | None -> () (* No { CODE } section. *)
39
40      | Some code ->
41         (* Add some standard variables to the environment. *)
42         let expr_of_substs s = Ast.ESubsts (Ast.noloc, s) in
43         let expr_of_pattern = function
44           | Ast.PTactic (loc, tactic, targs) ->
45              Ast.ETacticCtor (loc, tactic, List.map expr_of_substs targs)
46         in
47         let pexprs = List.map expr_of_pattern patterns in
48         let env = Ast.Env.add "@" (Ast.EList (Ast.noloc, pexprs)) env in
49         let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
50         let env =
51           (* NB: extra_deps are not added to %^ *)
52           match deps with
53           | [] -> env
54           | d :: _ -> Ast.Env.add "^" d env in
55         let r = Eval.run_code env loc code in
56         if r <> 0 then
57           failwithf "goal ‘%s’ failed with exit code %d" name r;
58
59         (* Check all targets were updated after the code was
60          * run (else it's an error).
61          *)
62         let pattern_still_needs_rebuild =
63           try
64             Some (List.find (needs_rebuild env loc deps extra_deps) patterns)
65           with
66             Not_found -> None in
67         match pattern_still_needs_rebuild with
68         | None -> ()
69         | Some pattern ->
70            failwithf "%a: goal %s ran successfully but it did not rebuild %a"
71              Ast.string_loc loc debug_goal Ast.string_pattern pattern
72     )
73   )
74
75 (* Return whether the target (pattern) needs to be rebuilt. *)
76 and needs_rebuild env loc deps extra_deps pattern =
77   Cmdline.debug "%a: testing if %a needs rebuild"
78     Ast.string_loc loc Ast.string_pattern pattern;
79
80   match pattern with
81   | Ast.PTactic (loc, tactic, targs) ->
82      (* Look up the tactic. *)
83      let params, code = Ast.gettactic env loc tactic in
84
85      (* Resolve the targs down to constants.  Since needs_rebuild
86       * should be called with env containing the goal params, this
87       * should substitute any parameters in the tactic arguments.
88       *)
89      let targs = List.map (Eval.substitute env loc) targs in
90      let targs =
91        List.map (fun targ ->
92            Ast.EConstant (Ast.noloc, Ast.CString targ)) targs in
93
94      (* Create a new environment binding parameter names
95       * to tactic args.
96       *)
97      let env =
98        let params =
99          try List.combine params targs
100          with Invalid_argument _ ->
101            failwithf "%a: calling tactic ‘%s’ with wrong number of arguments"
102              Ast.string_loc loc tactic in
103        List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
104
105      (* Add some standard variables to the environment. *)
106      let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
107      let env =
108        (* NB: extra_deps are not added to %^ *)
109        match deps with
110        | [] -> env
111        | d :: _ -> Ast.Env.add "^" d env in
112      let r = Eval.run_code env loc code in
113      if r = 99 (* means "needs rebuild" *) then true
114      else if r = 0 (* means "doesn't need rebuild" *) then false
115      else
116        failwithf "tactic ‘%s’ failed with exit code %d" tactic r
117
118 and exists_runner env loc p debug_tactic =
119   Cmdline.debug "%a: running implicit existence rule for tactic %s"
120     Ast.string_loc loc debug_tactic;
121
122   if needs_rebuild env loc [] [] p then
123     failwithf "%a: don't know how to build %s" Ast.string_loc loc debug_tactic