1 (***********************************************************************)
4 (* Nicolas Pouillard, Berke Durak, projet Gallium, INRIA Rocquencourt *)
6 (* Copyright 2007 Institut National de Recherche en Informatique et *)
7 (* en Automatique. All rights reserved. This file is distributed *)
8 (* under the terms of the Q Public License version 1.0. *)
10 (***********************************************************************)
13 (* Original author: Nicolas Pouillard *)
18 module Resources = Resource.Resources
20 exception Exit_rule_error of string
23 type env = Pathname.t -> Pathname.t
24 type builder = Pathname.t list list -> (Pathname.t, exn) Outcome.t list
25 type action = env -> builder -> Command.t
27 type digest_command = { digest : string; command : Command.t }
32 deps : Pathname.t list; (* These pathnames must be normalized *)
33 prods : 'a list; (* Note that prods also contains stamp *)
35 code : env -> builder -> digest_command }
37 type rule = Pathname.t gen_rule
38 type rule_scheme = Resource.resource_pattern gen_rule
40 let name_of_rule r = r.name
41 let deps_of_rule r = r.deps
42 let prods_of_rule r = r.prods
43 let stamp_of_rule r = r.stamp
45 type 'a rule_printer = (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a gen_rule -> unit
47 let compare _ _ = assert false
49 let print_rule_name f r = pp_print_string f r.name
51 let print_resource_list = List.print Resource.print
53 let print_rule_contents ppelt f r =
54 fprintf f "@[<v2>{@ @[<2>name =@ %S@];@ @[<2>tags =@ %a@];@ @[<2>deps =@ %a@];@ @[<2>prods = %a@];@ @[<2>code = <fun>@]@]@ }"
55 r.name Tags.print r.tags print_resource_list r.deps (List.print ppelt) r.prods
57 let pretty_print ppelt f r =
58 fprintf f "@[<hv2>rule@ %S@ ~deps:%a@ ~prods:%a@ <fun>@]"
59 r.name print_resource_list r.deps (List.print ppelt) r.prods
61 let print = print_rule_name
64 let subst_resources = List.map (Resource.subst env) in
65 let subst_resource_patterns = List.map (Resource.subst_pattern env) in
66 let finder next_finder p = next_finder (Resource.subst_any env p) in
67 let stamp = match rule.stamp with None -> None | Some x -> Some (Resource.subst_pattern env x) in
68 let prods = subst_resource_patterns rule.prods in
69 { (rule) with name = sbprintf "%s (%a)" rule.name Resource.print_env env;
71 deps = subst_resources rule.deps; (* The substition should preserve normalization of pathnames *)
73 code = (fun env -> rule.code (finder env)) }
75 exception Can_produce of rule
77 let can_produce target rule =
79 List.iter begin fun resource ->
80 match Resource.matchit resource target with
81 | Some env -> raise (Can_produce (subst env rule))
84 with Can_produce r -> Some r
86 (* let tags_matches tags r = if Tags.does_match tags r.tags then Some r else None *)
89 List.fold_right begin fun p acc ->
90 let f = Pathname.to_string (Resource.in_build_dir p) in
91 if sys_file_exists f then (f, Digest.file f) :: acc else acc
94 let digest_deps r dyndeps =
95 let buf = Buffer.create 1024 in
96 let add_resource r = Buffer.add_string buf (Digest.to_hex (Resource.digest r)) in
97 Buffer.add_string buf "deps:";
98 List.iter add_resource r.deps;
99 Buffer.add_string buf "dyndeps:";
100 Resources.iter add_resource dyndeps;
101 Digest.to_hex (Digest.string (Buffer.contents buf))
103 let digest_rule r dyndeps action =
104 let buf = Buffer.create 1024 in
105 Buffer.add_string buf action.digest;
106 let add_resource r = Buffer.add_string buf (Resource.digest r) in
107 Buffer.add_string buf "prods:";
108 List.iter add_resource r.prods;
109 Buffer.add_string buf "deps:";
110 List.iter add_resource r.deps;
111 Buffer.add_string buf "dyndeps:";
112 Resources.iter add_resource dyndeps;
113 Digest.string (Buffer.contents buf)
115 let cached_digest r =
116 try Some (Digest_cache.get ("Rule: " ^ r.name))
117 with Not_found -> None
119 let store_digest r digest = Digest_cache.put ("Rule: " ^ r.name) digest
121 let print_digest f x = pp_print_string f (Digest.to_hex x)
123 let exists2 find p rs =
124 try Some (find p rs) with Not_found -> None
126 let build_deps_of_tags builder tags =
127 match Command.deps_of_tags tags with
129 | deps -> List.map Outcome.good (builder (List.map (fun x -> [x]) deps))
131 let build_deps_of_tags_on_cmd builder =
132 Command.iter_tags begin fun tags ->
133 match Command.deps_of_tags tags with
135 | deps -> List.iter ignore_good (builder (List.map (fun x -> [x]) deps))
139 let dyndeps = ref Resources.empty in
141 let results = builder rs in
142 List.map begin fun res ->
145 let () = dprintf 10 "new dyndep for %S(%a): %S" r.name print_resource_list r.prods res' in
146 dyndeps := Resources.add res' !dyndeps;
147 List.iter (fun x -> Resource.Cache.add_dependency x res') r.prods;
151 let () = dprintf 5 "start rule %a" print r in
152 let action = r.code (fun x -> x) builder in
153 build_deps_of_tags_on_cmd builder action.command;
154 let dyndeps = !dyndeps in
155 let () = dprintf 10 "dyndeps: %a" Resources.print dyndeps in
156 let (reason, cached) =
157 match exists2 List.find (fun r -> not (Resource.exists_in_build_dir r)) r.prods with
158 | Some r -> (`cache_miss_missing_prod r, false)
160 begin match exists2 List.find Resource.Cache.resource_has_changed r.deps with
161 | Some r -> (`cache_miss_changed_dep r, false)
163 begin match exists2 Resources.find Resource.Cache.resource_has_changed dyndeps with
164 | Some r -> (`cache_miss_changed_dyn_dep r, false)
166 begin match cached_digest r with
167 | None -> (`cache_miss_no_digest, false)
169 let rule_digest = digest_rule r dyndeps action in
170 if d = rule_digest then (`cache_hit, true)
171 else (`cache_miss_digest_changed(d, rule_digest), false)
176 let explain_reason l =
177 raw_dprintf (l+1) "mid rule %a: " print r;
179 | `cache_miss_missing_prod r ->
180 dprintf l "cache miss: a product is not in build dir (%a)" Resource.print r
181 | `cache_miss_changed_dep r ->
182 dprintf l "cache miss: a dependency has changed (%a)" Resource.print r
183 | `cache_miss_changed_dyn_dep r ->
184 dprintf l "cache miss: a dynamic dependency has changed (%a)" Resource.print r
185 | `cache_miss_no_digest ->
186 dprintf l "cache miss: no digest found for %S (the command, a dependency, or a product)"
188 | `cache_hit -> dprintf (l+1) "cache hit"
189 | `cache_miss_digest_changed(old_d, new_d) ->
190 dprintf l "cache miss: the digest has changed for %S (the command, a dependency, or a product: %a <> %a)"
191 r.name print_digest old_d print_digest new_d
193 let prod_digests = digest_prods r in
194 (if not cached then List.iter Resource.clean r.prods);
195 (if !Options.nothing_should_be_rebuilt && not cached then
196 (explain_reason (-1);
197 let msg = sbprintf "Need to rebuild %a through the rule `%a'" print_resource_list r.prods print r in
198 raise (Exit_rule_error msg)));
202 if cached then Command.execute ~pretend:true action.command
204 begin match r.stamp with
206 reset_filesys_cache ();
207 let digest_deps = digest_deps r dyndeps in
208 with_output_file stamp (fun oc -> output_string oc digest_deps)
211 List.iter (fun r -> Resource.Cache.resource_built r) r.prods;
213 let new_rule_digest = digest_rule r dyndeps action in
214 let new_prod_digests = digest_prods r in
215 let () = store_digest r new_rule_digest in
216 List.iter begin fun p ->
217 let f = Pathname.to_string (Resource.in_build_dir p) in
218 (try let digest = List.assoc f prod_digests in
219 let new_digest = List.assoc f new_prod_digests in
220 if digest <> new_digest then raise Not_found
221 with Not_found -> Resource.Cache.resource_changed p)
223 dprintf 5 "end rule %a" print r
224 with exn -> (List.iter Resource.clean r.prods; raise exn)
228 else List.iter (fun x -> Resource.Cache.suspend_resource x action.command thunk r.prods) r.prods
230 let (get_rules, add_rule, clear_rules) =
231 let rules = ref [] in
235 let _ = List.find (fun x -> x.name = r.name) !rules in
236 raise (Exit_rule_error (sbprintf "Rule.add_rule: already exists: (%a)" print r))
239 | `bottom -> rules := !rules @ [r]
240 | `top -> rules := r :: !rules
243 List.fold_right begin fun x acc ->
244 if x.name = s then x :: r :: acc else x :: acc
248 List.fold_right begin fun x acc ->
249 if x.name = s then r :: x :: acc else x :: acc
252 (fun () -> rules := [])
254 let rule name ?(tags=[]) ?(prods=[]) ?(deps=[]) ?prod ?dep ?stamp ?(insert = `bottom) code =
255 let res_add import xs xopt =
259 | Some r -> [import r]
261 List.fold_right begin fun x acc ->
263 if List.mem r acc then
264 failwith (sprintf "in rule %s, multiple occurences of the resource %s" name x)
268 if prods = [] && prod = None && stamp = None then raise (Exit_rule_error "Can't make a rule that produce nothing");
271 | None -> None, prods
273 Some (Resource.import_pattern stamp), stamp :: prods
275 let prods = res_add Resource.import_pattern prods prod in
277 let cmd = code env build in
278 { digest = Command.digest cmd
283 tags = List.fold_right Tags.add tags Tags.empty;
284 deps = res_add Resource.import (* should normalize *) deps dep;
289 module Common_commands = struct
291 let mv src dest = Cmd (S [A"mv"; P src; Px dest])
292 let cp src dest = Cmd (S [A"cp"; P src; Px dest])
293 let cp_p src dest = Cmd (S [A"cp"; A"-p"; P src; Px dest])
294 let ln_f pointed pointer = Cmd (S [A"ln"; A"-f"; P pointed; Px pointer])
295 let ln_s pointed pointer = Cmd (S[A"ln"; A"-s"; P pointed; Px pointer])
296 let rm_f x = Cmd (S [A"rm"; A"-f"; Px x])
297 let chmod opts file = Cmd (S[A"chmod"; opts; Px file])
298 let cmp a b = Cmd (S[A"cmp"; P a; Px b])
302 let copy_rule name ?insert src dest =
303 rule name ?insert ~prod:dest ~dep:src
305 let src = env src and dest = env dest in
306 Shell.mkdir_p (Pathname.dirname dest);