2 * Copyright (C) 2019 Richard W.M. Jones
3 * Copyright (C) 2019 Red Hat Inc.
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.
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.
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.
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
29 module Jobs = Jobs.Make (
31 type t = string * Ast.expr list
33 let to_string (name, args) =
34 sprintf "%s (%s)" name
35 (String.concat ", " (List.map (Ast.string_expr ()) args))
39 let stop_all = Jobs.stop_all
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;
47 (* This starts the targets, adding them to the jobs group, but does not
48 * wait for them to complete.
50 and run_targets group env exprs =
51 List.iter (run_target group env) exprs
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
57 and run_target group env = function
58 | Ast.EGoalDefn _ | Ast.EFuncDefn _ | Ast.ETacticDefn _ -> assert false
60 (* Call a goal or function. *)
61 | Ast.ECall (loc, name, args) ->
62 let expr = Ast.getvar env loc name in
64 | Ast.EGoalDefn (_, goal) ->
65 let key = name, args in
66 Jobs.start group key (
67 fun () -> run_goal env loc name args goal []
69 | Ast.EFuncDefn (_, func) ->
70 let expr = Eval.call_function env loc name args func in
71 run_target group env expr
73 failwithf "%a: tried to call ‘%s’ which is not a goal or a function"
74 Ast.string_loc loc name
78 | Ast.ETacticCtor (loc, name, args) ->
79 (* All parameters of tactics must be simple constant expressions
80 * (strings, in future booleans, numbers, etc).
82 let args = List.map (Eval.to_constant env) args in
83 run_tactic group env loc name args
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.
88 | Ast.EVar (loc, name) ->
89 let expr = Ast.getvar env loc name in
91 | Ast.EGoalDefn (loc, ([], _, _, _)) ->
92 run_target group env (Ast.ECall (loc, name, []))
94 failwithf "%a: cannot call %s() since this goal has parameters"
95 Ast.string_loc loc name
97 run_target group env expr
100 (* Lists are inlined when found as a target. *)
101 | Ast.EList (loc, exprs) ->
102 run_targets group env exprs
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]
109 | Ast.EConstant (loc, c) ->
110 run_tactic group env loc "*file" [c]
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. *)
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;
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.
124 let args = List.map (Eval.evaluate_goal_arg env) args in
126 (* Create a new environment which maps the parameter names to
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
138 (* Check all dependencies have been updated. We must wait
139 * for these to complete before we can continue.
141 run_targets_to_completion env (deps @ extra_deps);
143 (* Check if any target (ie. pattern) needs to be rebuilt.
144 * As with make, a goal with no targets is always run.
148 List.exists (needs_rebuild env loc deps extra_deps) patterns in
151 (* Run the code (if any). *)
153 | None -> () (* No { CODE } section. *)
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)
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
166 (* NB: extra_deps are not added to %^ *)
169 | d :: _ -> Ast.Env.add "^" d env in
170 let r = Eval.run_code env loc code in
172 failwithf "goal ‘%s’ failed with exit code %d" name r;
174 (* Check all targets were updated after the code was
175 * run (else it's an error).
177 let pattern_still_needs_rebuild =
179 Some (List.find (needs_rebuild env loc deps extra_deps) patterns)
182 match pattern_still_needs_rebuild with
185 failwithf "%a: goal %s ran successfully but it did not rebuild %a"
186 Ast.string_loc loc debug_goal Ast.string_pattern pattern
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;
196 | Ast.PTactic (loc, tactic, targs) ->
197 (* Look up the tactic. *)
198 let params, code = Ast.gettactic env loc tactic in
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.
204 let targs = List.map (Eval.substitute env loc) targs in
206 List.map (fun targ ->
207 Ast.EConstant (Ast.noloc, Ast.CString targ)) targs in
209 (* Create a new environment binding parameter names
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
220 (* Add some standard variables to the environment. *)
221 let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
223 (* NB: extra_deps are not added to %^ *)
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
231 failwithf "tactic ‘%s’ failed with exit code %d" tactic r
233 (* Find the goal which matches the given tactic and start it.
234 * cargs is a list of parameters (all constants).
236 and run_tactic group env loc tactic cargs =
237 (* This is used to print the tactic in debug and error messages only. *)
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;
244 (* Find all goals in the environment. Returns a list of (name, goal). *)
246 let env = Ast.Env.bindings env in
249 | name, Ast.EGoalDefn (loc, goal) -> Some (name, goal)
252 (* Find all patterns. Returns a list of (pattern, name, goal). *)
253 let patterns : (Ast.pattern * Ast.id * Ast.goal) list =
255 (List.map (fun (name, ((_, patterns, _, _) as goal)) ->
256 List.map (fun pattern -> (pattern, name, goal)) patterns) goals) in
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).
262 let patterns : (Ast.pattern * Ast.id * Ast.goal * Ast.expr list) list =
264 fun (pattern, name, ((params, _, _, _) as goal)) ->
265 match matching_pattern env loc tactic cargs pattern params with
267 | Some args -> Some (pattern, name, goal, args)
272 (* There's no matching goal, but we don't need one if
273 * the tactic doesn't need to be rebuilt.
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
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 []
289 (* Two or more goals match. Only one must have a CODE section,
290 * and we combine the dependencies into a "supergoal".
292 let with_code, without_code =
294 fun (_, _, (_, _, _, code), _) -> code <> None
297 let (_, name, goal, args), extra_deps =
302 List.map (fun (_, _, (_, _, deps, _), _) -> deps) without_code
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.
311 let g = List.hd without_code in
314 List.map (fun (_, _, (_, _, deps, _), _) -> deps)
315 (List.tl without_code)
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
323 let key = name, args in
324 Jobs.start group key (fun () ->
325 run_goal env loc name args goal extra_deps
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.
333 and matching_pattern env loc tactic cargs pattern params =
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
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"].
348 * If we are called with cargs = ["file1.o"], we would
351 * On non-matching this raises Not_found.
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;
358 (* Rearrange the result into goal parameter order. Also this
359 * checks that every parameter got a binding.
363 (* Allow the Not_found exception to escape if no binding for this param. *)
364 fun param -> Ast.Env.find param res
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.
370 and matching_param env loc params res targ carg =
372 | Ast.CString carg ->
373 (* Substitute any non parameters in targ from the environment. *)
377 | Ast.SString _ as s -> s
379 if not (List.mem name params) then (
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
390 (* Do the actual pattern matching. Any remaining SVar elements
391 * must refer to goal parameters.
393 let carg = ref carg in
394 let rec loop = function
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
404 (* Yes, so continue after the matching prefix. *)
405 carg := String.sub !carg slen (clen-slen);
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);
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". *)