1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
32 let times = Unix.times () in
33 let stamp = times.Unix.tms_utime +. times.Unix.tms_stime in
34 let lap = stamp -. !old in
35 Printf.eprintf "TIME STAMP (%s): %f\n" msg lap; flush stderr;
39 exception NoRootFor of string
43 if String.length path > 0 && path.[0] <> '/' then
44 Sys.getcwd () ^ "/" ^ path
48 HExtlib.normalize_path path
53 let path = absolutize path in
54 let paths = List.rev (Str.split (Str.regexp "/") path) in
55 let rec build = function
56 | he::tl as l -> ("/" ^ String.concat "/" (List.rev l) ^ "/") :: build tl
59 let paths = List.map HExtlib.normalize_path (build paths) in
60 try HExtlib.find_in paths "root"
61 with Failure "find_in" ->
62 raise (NoRootFor (path ^ " (" ^ String.concat ", " paths ^ ")"))
65 let ensure_trailing_slash s =
66 if s = "" then "/" else
67 if s.[String.length s-1] <> '/' then s^"/" else s
70 let remove_trailing_slash s =
71 if s = "" then "" else
72 let len = String.length s in
73 if s.[len-1] = '/' then String.sub s 0 (len-1) else s
76 let load_root_file rootpath =
77 let data = HExtlib.input_file rootpath in
78 let lines = Str.split (Str.regexp "\n") data in
80 Pcre.replace ~pat:"[ \t]+" ~templ:" "
81 (Pcre.replace ~pat:"^ *" (Pcre.replace ~pat:" *$" s))
85 match Str.split (Str.regexp "=") l with
86 | [k;v] -> clean k, Http_getter_misc.strip_trailing_slash (clean v)
87 | _ -> raise (Failure ("Malformed root file: " ^ rootpath)))
91 let rec find_root_for ~include_paths file =
92 let include_paths = "" :: Sys.getcwd () :: include_paths in
94 let path = HExtlib.find_in include_paths file in
95 let path = absolutize path in
96 (* HLog.debug ("file "^file^" resolved as "^path); *)
97 let rootpath, root, buri =
99 let mburi = Helm_registry.get "matita.baseuri" in
100 match Str.split (Str.regexp " ") mburi with
101 | [root; buri] when HExtlib.is_prefix_of root path ->
102 ":registry:", root, buri
103 | _ -> raise (Helm_registry.Key_not_found "matita.baseuri")
104 with Helm_registry.Key_not_found "matita.baseuri" ->
105 let rootpath = find_root path in
106 let buri = List.assoc "baseuri" (load_root_file rootpath) in
107 rootpath, Filename.dirname rootpath, buri
109 (* HLog.debug ("file "^file^" rooted by "^rootpath^""); *)
110 let uri = Http_getter_misc.strip_trailing_slash buri in
111 if String.length uri < 5 || String.sub uri 0 5 <> "cic:/" then
112 HLog.error (rootpath ^ " sets an incorrect baseuri: " ^ buri);
113 ensure_trailing_slash root, remove_trailing_slash uri, path
114 with Failure "find_in" ->
115 if Filename.check_suffix file ".ma" then begin
116 let mma = Filename.chop_suffix file ".ma" ^ ".mma" in
117 HLog.warn ("We look for: " ^ mma);
118 find_root_for ~include_paths mma
120 HLog.error ("We are in: " ^ Sys.getcwd ());
121 HLog.error ("Unable to find: "^file^"\nPaths explored:");
122 List.iter (fun x -> HLog.error (" - "^x)) include_paths;
123 raise (NoRootFor file)
127 let mk_baseuri root extra =
129 assert(Filename.check_suffix name ".ma" ||
130 Filename.check_suffix name ".mma");
131 try Filename.chop_extension name
132 with Invalid_argument "Filename.chop_extension" -> name
134 remove_trailing_slash (HExtlib.normalize_path (root ^ "/" ^ chop extra))
137 let baseuri_of_script ~include_paths file =
138 let root, buri, path = find_root_for ~include_paths file in
139 let path = HExtlib.normalize_path path in
140 let root = HExtlib.normalize_path root in
141 let lpath = Str.split (Str.regexp "/") path in
142 let lroot = Str.split (Str.regexp "/") root in
143 let rec substract l1 l2 =
145 | h1::tl1,h2::tl2 when h1 = h2 -> substract tl1 tl2
147 | _ -> raise (NoRootFor (file ^" "^path^" "^root))
149 let extra_buri = substract lpath lroot in
150 let extra = String.concat "/" extra_buri in
152 mk_baseuri buri extra,
157 let find_roots_in_dir dir =
158 HExtlib.find ~test:(fun f ->
159 Filename.basename f = "root" &&
160 try (Unix.stat f).Unix.st_kind = Unix.S_REG
161 with Unix.Unix_error _ -> false)
166 let load_deps_file f =
168 let ic = open_in f in
172 let l = input_line ic in
173 match Str.split (Str.regexp " ") l with
175 HLog.error ("Malformed deps file: " ^ f);
176 raise (Failure ("Malformed deps file: " ^ f))
177 | he::tl -> deps := (he,tl) :: !deps
180 with End_of_file -> !deps
183 type options = (string * string) list
189 val load_deps_file: string -> (source_object * source_object list) list
190 val string_of_source_object: source_object -> string
191 val string_of_target_object: target_object -> string
192 val build: options -> source_object -> bool
193 val root_and_target_of:
194 options -> source_object -> string option * target_object
195 val mtime_of_source_object: source_object -> float option
196 val mtime_of_target_object: target_object -> float option
197 val is_readonly_buri_of: options -> source_object -> bool
200 module Make = functor (F:Format) -> struct
202 type status = Done of bool
205 let say s = if !debug then prerr_endline ("make: "^s);;
207 let unopt_or_call x f y = match x with Some _ -> x | None -> f y;;
209 let fst4 = function (x,_,_,_) -> x;;
211 let modified_before_s_t (_,cs, ct, _, _) a b =
212 prerr_endline ("L s_t: a " ^ F.string_of_source_object a);
213 prerr_endline ("L s_t: b " ^ F.string_of_target_object b);
214 let a = try Hashtbl.find cs a with Not_found -> assert false in
217 match Hashtbl.find ct b with
220 match F.mtime_of_target_object b with
223 Hashtbl.add ct b x; x
225 with Not_found -> assert false
227 let r = match a, b with
228 | Some a, Some b -> a <= b
231 prerr_endline ("L s_t: " ^ string_of_bool r); r
233 let modified_before_t_t (_,_,ct, _, _) a b =
235 prerr_endline ("L t_t: a " ^ F.string_of_target_object a);
236 prerr_endline ("L t_t: b " ^ F.string_of_target_object b);
239 match Hashtbl.find ct a with
242 match F.mtime_of_target_object a with
245 Hashtbl.add ct a x; x
247 with Not_found -> assert false
251 match Hashtbl.find ct b with
254 match F.mtime_of_target_object b with
257 Hashtbl.add ct b x; x
259 with Not_found -> assert false
261 let r = match a, b with
264 prerr_endline ("tt: a " ^ string_of_float a);
265 prerr_endline ("tt: b " ^ string_of_float b);
270 prerr_endline ("L t_t: " ^ string_of_bool r); r
272 let rec purge_unwanted_roots wanted deps =
276 not (List.exists (fun (_,d1,_,_) -> List.mem t d1) deps))
279 let newroots = List.filter (fun (t,_,_,_) -> List.mem t wanted) roots in
280 if newroots = roots then
283 purge_unwanted_roots wanted (newroots @ rest)
286 let is_not_ro (opts,_,_,_,_) (f,_,r,_) =
288 | Some root -> not (F.is_readonly_buri_of opts f)
289 | None -> assert false
291 (* FG: Old sorting algorithm ************************************************)
293 let rec get_status opts what =
294 let _, _, _, cc, cd = opts in
295 let t, dependencies, root, tgt = what in
296 try Done (Hashtbl.find cc t)
297 (* say "already built" *)
299 let map st d = match st with
303 let whatd = Hashtbl.find cd d in
304 let _, _, _, tgtd = whatd in
305 begin match st, get_status opts whatd with
306 | _, Done false -> Hashtbl.add cc t false; Done false
307 | Done true, Done true ->
308 if modified_before_t_t opts tgt tgtd then Ready true else Done true
309 (* say (F.string_of_source_object t^" depends on "^F.string_of_target_object unsat^" and "^F.string_of_source_object t^".o is younger than "^ F.string_of_target_object unsat^", thus needs to be built") *)
310 | Done true, Ready _ -> Ready false
311 | Ready true, Ready _ -> Ready false
312 (* say (F.string_of_source_object t^" depends on "^ F.string_of_source_object (fst4 unsat)^ " that is not built, thus is not ready") *)
313 | Ready true, Done true -> Ready true
317 let st = if modified_before_s_t opts t tgt then Done true else Ready true in
318 match List.fold_left map st dependencies with
319 | Done true -> Hashtbl.add cc t true; Done true
320 (* say(F.string_of_source_object t^" is built" *)
323 let list_partition_filter_rev filter l =
324 let rec aux l1 l2 = function
327 begin match filter hd with
328 | None -> aux l1 l2 tl
329 | Some true -> aux (hd :: l1) l2 tl
330 | Some false -> aux l1 (hd :: l2) tl
335 let rec make_aux root (lo,_,ct, cc, _ as opts) ok deps =
336 time_stamp "filter get_status: begin";
337 let map what = match get_status opts what with
341 let todo, deps = list_partition_filter_rev map deps in
342 time_stamp "filter get_status: end";
345 List.partition (fun (_,_,froot,_) -> froot = Some root) todo
347 let local, skipped = List.partition (is_not_ro opts) local in
350 HLog.warn("Read only baseuri for: "^F.string_of_source_object(fst4 x)))
354 if todo <> [] then begin
355 let ok = List.fold_left
356 (fun ok (file,_,froot,tgt) ->
359 | Some froot when froot = root ->
360 Hashtbl.remove ct tgt;
361 Hashtbl.add ct tgt None;
362 time_stamp "building";
363 let r = F.build lo file in
365 | Some froot -> make froot [file]
367 HLog.error ("No root for: "^F.string_of_source_object file);
370 Hashtbl.add cc file rc;
375 make_aux root opts ok (List.rev deps)
380 (* FG: new sorting algorithm ************************************************)
382 let rec make_aux root opts ok deps =
383 List.fold_left (make_one root opts) ok deps
385 and make_one root opts ok what =
386 let lo, _, ct, cc, cd = opts in
387 let t, deps, froot, tgt = what in
388 let str = F.string_of_source_object t in
389 let map (okd, okt) d =
390 let (_, _, _, tgtd) as whatd = (Hashtbl.find cd d) in
391 let r = make_one root opts okd whatd in
392 r, okt && modified_before_t_t opts tgtd tgt
394 prerr_endline ("L : processing " ^ str);
396 let r = Hashtbl.find cc t in
397 prerr_endline ("L : " ^ string_of_bool r ^ " " ^ str);
399 (* say "already built" *)
401 let okd, okt = List.fold_left map (true, modified_before_s_t opts t tgt) deps in
404 if okt then true else
406 | Some froot when froot = root ->
407 if is_not_ro opts what then begin
408 Hashtbl.remove ct tgt;
409 Hashtbl.add ct tgt None;
410 time_stamp ("L : BUILDING " ^ str);
411 let res = F.build lo t in
412 time_stamp ("L : DONE " ^ str); res
414 HLog.warn("Read only baseuri for: "^ str); false
416 | Some froot -> make froot [t]
418 HLog.error ("No root for: " ^ str); false
421 prerr_endline ("L : " ^ string_of_bool res ^ " " ^ str);
422 Hashtbl.add cc t res; ok && res
424 (****************************************************************************)
426 and make root targets =
427 time_stamp "L : ENTERING";
428 HLog.debug ("Entering directory '"^root^"'");
429 let old_root = Sys.getcwd () in
431 let deps = F.load_deps_file (root^"/depends") in
432 let local_options = load_root_file (root^"/root") in
433 let caches,cachet,cachec,cached =
434 Hashtbl.create 73, Hashtbl.create 73, Hashtbl.create 73, Hashtbl.create 73
436 (* deps are enriched with these informations to sped up things later *)
440 let r,tgt = F.root_and_target_of local_options file in
441 Hashtbl.add caches file (F.mtime_of_source_object file);
442 Hashtbl.add cachet tgt (F.mtime_of_target_object tgt);
443 Hashtbl.add cached file (file, d, r, tgt);
448 let opts = local_options, caches, cachet, cachec, cached in
451 make_aux root opts true deps
453 make_aux root opts true
454 (purge_unwanted_roots targets deps)
456 HLog.debug ("Leaving directory '"^root^"'");
458 time_stamp "L : LEAVING";
464 let write_deps_file where deps = match where with
466 let oc = open_out (root ^ "/depends") in
467 let map (t, d) = output_string oc (t^" "^String.concat " " d^"\n") in
468 List.iter map deps; close_out oc;
469 HLog.message ("Generated: " ^ root ^ "/depends")
471 print_endline (String.concat " " (List.flatten (List.map snd deps)))
473 (* FG ***********************************************************************)
475 (* scheme uri part as defined in URI Generic Syntax (RFC 3986) *)
476 let uri_scheme_rex = Pcre.regexp "^[[:alpha:]][[:alnum:]\-+.]*:"
479 Pcre.pmatch ~rex:uri_scheme_rex str