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 (* Starts the target expressions running and waits for them to complete. *)
40 let rec run_targets_to_completion env exprs =
41 let group = Jobs.new_group () in
42 run_targets group env exprs;
45 (* This starts the targets, adding them to the jobs group, but does not
46 * wait for them to complete.
48 and run_targets group env exprs =
49 List.iter (run_target group env) exprs
51 (* This starts a single target, adding the (usually single but can
52 * be multiple) jobs to the jobs group. It does not wait for the
55 and run_target group env = function
56 | Ast.EGoalDefn _ | Ast.EFuncDefn _ | Ast.ETacticDefn _ -> assert false
58 (* Call a goal or function. *)
59 | Ast.ECall (loc, name, args) ->
60 let expr = Ast.getvar env loc name in
62 | Ast.EGoalDefn (_, goal) ->
63 let key = name, args in
64 Jobs.start group key (
65 fun () -> run_goal env loc name args goal []
67 | Ast.EFuncDefn (_, func) ->
68 let expr = Eval.call_function env loc name args func in
69 run_target group env expr
71 failwithf "%a: tried to call ‘%s’ which is not a goal or a function"
72 Ast.string_loc loc name
76 | Ast.ETacticCtor (loc, name, args) ->
77 (* All parameters of tactics must be simple constant expressions
78 * (strings, in future booleans, numbers, etc).
80 let args = List.map (Eval.to_constant env) args in
81 run_tactic group env loc name args
83 (* If this is a goal then it's the same as calling goal(). If not
84 * then look up the variable and substitute it.
86 | Ast.EVar (loc, name) ->
87 let expr = Ast.getvar env loc name in
89 | Ast.EGoalDefn (loc, ([], _, _, _)) ->
90 run_target group env (Ast.ECall (loc, name, []))
92 failwithf "%a: cannot call %s() since this goal has parameters"
93 Ast.string_loc loc name
95 run_target group env expr
98 (* Lists are inlined when found as a target. *)
99 | Ast.EList (loc, exprs) ->
100 run_targets group env exprs
102 (* A string (with or without substitutions) implies *file(filename). *)
103 | Ast.ESubsts (loc, str) ->
104 let str = Eval.substitute env loc str in
105 run_tactic group env loc "*file" [Ast.CString str]
107 | Ast.EConstant (loc, c) ->
108 run_tactic group env loc "*file" [c]
110 (* Run a goal by name. *)
111 and run_goal env loc name args (params, patterns, deps, code) extra_deps =
112 (* This is used to print the goal in debug and error messages only. *)
114 sprintf "%s (%s)" name
115 (String.concat ", " (List.map (Ast.string_expr ()) args)) in
116 Cmdline.debug "%a: running goal %s" Ast.string_loc loc debug_goal;
118 (* This is the point where we evaluate the goal arguments. We must
119 * do this before creating the new environment, because variables
120 * appearing in goal arguments don't refer to goal parameters.
122 let args = List.map (Eval.evaluate_goal_arg env) args in
124 (* Create a new environment which maps the parameter names to
129 try List.combine params args
130 with Invalid_argument _ ->
131 failwithf "%a: calling goal %s with wrong number of arguments, expecting %d args but got %d args"
132 Ast.string_loc loc debug_goal
133 (List.length params) (List.length args) in
134 List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
136 (* Check all dependencies have been updated. We must wait
137 * for these to complete before we can continue.
139 run_targets_to_completion env (deps @ extra_deps);
141 (* Check if any target (ie. pattern) needs to be rebuilt.
142 * As with make, a goal with no targets is always run.
146 List.exists (needs_rebuild env loc deps extra_deps) patterns in
149 (* Run the code (if any). *)
151 | None -> () (* No { CODE } section. *)
154 (* Add some standard variables to the environment. *)
155 let expr_of_substs s = Ast.ESubsts (Ast.noloc, s) in
156 let expr_of_pattern = function
157 | Ast.PTactic (loc, tactic, targs) ->
158 Ast.ETacticCtor (loc, tactic, List.map expr_of_substs targs)
160 let pexprs = List.map expr_of_pattern patterns in
161 let env = Ast.Env.add "@" (Ast.EList (Ast.noloc, pexprs)) env in
162 let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
164 (* NB: extra_deps are not added to %^ *)
167 | d :: _ -> Ast.Env.add "^" d env in
168 let r = Eval.run_code env loc code in
170 eprintf "*** goal ‘%s’ failed with exit code %d\n" 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 eprintf "*** tactic ‘%s’ failed with exit code %d\n" tactic r;
235 (* Find the goal which matches the given tactic and start it.
236 * cargs is a list of parameters (all constants).
238 and run_tactic group env loc tactic cargs =
239 (* This is used to print the tactic in debug and error messages only. *)
242 (Ast.ETacticCtor (loc, tactic,
243 List.map (fun c -> Ast.EConstant (loc, c)) cargs)) in
244 Cmdline.debug "%a: running tactic %s" Ast.string_loc loc debug_tactic;
246 (* Find all goals in the environment. Returns a list of (name, goal). *)
248 let env = Ast.Env.bindings env in
251 | name, Ast.EGoalDefn (loc, goal) -> Some (name, goal)
254 (* Find all patterns. Returns a list of (pattern, name, goal). *)
255 let patterns : (Ast.pattern * Ast.id * Ast.goal) list =
257 (List.map (fun (name, ((_, patterns, _, _) as goal)) ->
258 List.map (fun pattern -> (pattern, name, goal)) patterns) goals) in
260 (* Find any patterns (ie. tactics) which match the one we
261 * are searching for. This returns a binding for the goal args,
262 * so we end up with a list of (pattern, name, goal, args).
264 let patterns : (Ast.pattern * Ast.id * Ast.goal * Ast.expr list) list =
266 fun (pattern, name, ((params, _, _, _) as goal)) ->
267 match matching_pattern env loc tactic cargs pattern params with
269 | Some args -> Some (pattern, name, goal, args)
274 (* There's no matching goal, but we don't need one if
275 * the tactic doesn't need to be rebuilt.
277 let targs = List.map (function Ast.CString s -> [Ast.SString s]) cargs in
278 let p = Ast.PTactic (loc, tactic, targs) in
279 if needs_rebuild env loc [] [] p then
280 failwithf "%a: don't know how to build %s"
281 Ast.string_loc loc debug_tactic
283 | [_, name, goal, args] ->
284 (* Single goal matches, run it. *)
285 let key = name, args in
286 Jobs.start group key (
287 fun () -> run_goal env loc name args goal []
291 (* Two or more goals match. Only one must have a CODE section,
292 * and we combine the dependencies into a "supergoal".
294 let with_code, without_code =
296 fun (_, _, (_, _, _, code), _) -> code <> None
299 let (_, name, goal, args), extra_deps =
304 List.map (fun (_, _, (_, _, deps, _), _) -> deps) without_code
309 (* This is OK, it means we'll rebuild all dependencies
310 * but there is no code to run. Pick the first goal
311 * without code and the dependencies from the other goals.
313 let g = List.hd without_code in
316 List.map (fun (_, _, (_, _, deps, _), _) -> deps)
317 (List.tl without_code)
322 failwithf "%a: multiple goals found which match tactic %s, but more than one of these goals have {code} sections which is not allowed"
323 Ast.string_loc loc debug_tactic in
325 let key = name, args in
326 Jobs.start group key (fun () ->
327 run_goal env loc name args goal extra_deps
330 (* Test if pattern matches *tactic(cargs). If it does
331 * then we return Some args where args is the arguments that must
332 * be passed to the matching goal. The params parameter is
333 * the names of the parameters of that goal.
335 and matching_pattern env loc tactic cargs pattern params =
337 | Ast.PTactic (loc, ttactic, targs)
338 when tactic <> ttactic ||
339 List.length cargs <> List.length targs ->
340 None (* Can't possibly match if tactic name or #args is different. *)
341 | Ast.PTactic (loc, ttactic, targs) ->
342 (* Do the args match with a possible params binding? *)
343 try Some (matching_params env loc params targs cargs)
344 with Not_found -> None
346 (* Return a possible binding. For example the goal is:
347 * goal compile (name) = "%name.o": "%name.c" {}
348 * which means that params = ["name"] and targs = ["%name.o"].
350 * If we are called with cargs = ["file1.o"], we would
353 * On non-matching this raises Not_found.
355 and matching_params env loc params targs cargs =
356 (* This is going to record the resulting binding. *)
357 let res = ref Ast.Env.empty in
358 List.iter2 (matching_param env loc params res) targs cargs;
360 (* Rearrange the result into goal parameter order. Also this
361 * checks that every parameter got a binding.
365 (* Allow the Not_found exception to escape if no binding for this param. *)
366 fun param -> Ast.Env.find param res
369 (* If targ = "%name.o" and carg = "file.o" then this would set
370 * name => "file" in !res. If they don't match, raises Not_found.
372 and matching_param env loc params res targ carg =
374 | Ast.CString carg ->
375 (* Substitute any non parameters in targ from the environment. *)
379 | Ast.SString _ as s -> s
381 if not (List.mem name params) then (
383 let expr = Ast.getvar env loc name in
384 match Eval.to_constant env expr with
385 | Ast.CString s -> Ast.SString s
386 with Failure _ -> raise Not_found
392 (* Do the actual pattern matching. Any remaining SVar elements
393 * must refer to goal parameters.
395 let carg = ref carg in
396 let rec loop = function
398 (* End of targ, we must have matched all of carg. *)
399 if !carg <> "" then raise Not_found
400 | Ast.SString s :: rest ->
401 (* Does this match the first part of !carg? *)
402 let clen = String.length !carg in
403 let slen = String.length s in
404 if slen > clen || s <> String.sub !carg 0 slen then
406 (* Yes, so continue after the matching prefix. *)
407 carg := String.sub !carg slen (clen-slen);
409 | Ast.SVar name :: Ast.SString s :: rest ->
410 (* This is a goal parameter. Find s later in !carg. *)
411 let i = string_find !carg s in
412 if i = -1 then raise Not_found;
413 (* Set the binding in !res. *)
414 let r = Ast.EConstant (Ast.noloc,
415 Ast.CString (String.sub !carg 0 i)) in
416 res := Ast.Env.add name r !res;
417 (* Continue after the match. *)
418 let skip = i + String.length s in
419 carg := String.sub !carg skip (String.length !carg - skip);
421 | Ast.SVar name :: [] ->
422 (* Matches the whole remainder of the string. *)
423 let r = Ast.EConstant (Ast.noloc, Ast.CString !carg) in
424 res := Ast.Env.add name r !res
425 | Ast.SVar x :: Ast.SVar y :: _ ->
426 (* TODO! We cannot match a target like "%x%y". *)