1 (* Copyright (C) 2004-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 exception AttemptToInsertAnAlias of LexiconEngine.status
35 let set_callback f = out := f
37 let slash_n_RE = Pcre.regexp "\\n" ;;
39 let pp_ast_statement grafite_status stm =
40 let stm = GrafiteAstPp.pp_statement
41 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
42 ~term_pp:CicNotationPp.pp_term
43 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:(CicNotationPp.pp_obj
44 CicNotationPp.pp_term) stm
46 let stm = Pcre.replace ~rex:slash_n_RE stm in
48 if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
51 HLog.debug ("Executing: ``" ^ stm ^ "''")
54 let clean_exit baseuri rc =
55 LibraryClean.clean_baseuris ~verbose:false [baseuri]; rc
58 let get_macro_context = function
59 | Some {GrafiteTypes.proof_status = GrafiteTypes.No_proof} -> []
61 let stack = GrafiteTypes.get_stack status in
62 let goal = Continuationals.Stack.find_goal stack in
63 GrafiteTypes.get_proof_context status goal
64 | None -> assert false
67 let pp_times fname rc big_bang big_bang_u big_bang_s =
68 if not (Helm_registry.get_bool "matita.verbose") then
69 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
70 let r = Unix.gettimeofday () -. big_bang in
71 let u = u -. big_bang_u in
72 let s = s -. big_bang_s in
73 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
75 if rc then "
\e[0;32mOK
\e[0m","Ok" else "
\e[0;31mFAIL
\e[0m","Fail" in
78 let seconds = int_of_float t in
79 let cents = int_of_float ((t -. floor t) *. 100.0) in
80 let minutes = seconds / 60 in
81 let seconds = seconds mod 60 in
82 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
84 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
86 let s = Printf.sprintf "%-4s %s %s" rc times extra in
89 HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
93 let lenp = String.length prefix in
94 let lens = String.length s in
96 assert (String.sub s 0 lenp = prefix);
97 String.sub s lenp (lens-lenp)
100 let get_include_paths options =
102 try List.assoc "include_paths" options with Not_found -> ""
104 let include_paths = Str.split (Str.regexp " ") include_paths in
107 Helm_registry.get_list Helm_registry.string "matita.includes"
112 let compile options fname =
113 let matita_debug = Helm_registry.get_bool "matita.debug" in
114 let include_paths = get_include_paths options in
115 let root,baseuri,fname,_tgt =
116 Librarian.baseuri_of_script ~include_paths fname in
117 if Http_getter_storage.is_read_only baseuri then assert false;
118 let grafite_status = GrafiteSync.init baseuri in
120 CicNotation2.load_notation ~include_paths:[]
121 BuildTimeConf.core_notation_script
123 let initial_lexicon_status = lexicon_status in
124 let big_bang = Unix.gettimeofday () in
125 let { Unix.tms_utime = big_bang_u ; Unix.tms_stime = big_bang_s} =
128 let time = Unix.time () in
132 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~baseuri ~writable:true
135 LibraryMisc.lexicon_file_of_baseuri
136 ~must_exist:false ~baseuri ~writable:true
138 (* cleanup of previously compiled objects *)
139 if (not (Http_getter_storage.is_empty ~local:true baseuri) ||
140 LibraryClean.db_uris_of_baseuri baseuri <> [])
142 HLog.message ("baseuri " ^ baseuri ^ " is not empty");
143 HLog.message ("cleaning baseuri " ^ baseuri);
144 LibraryClean.clean_baseuris [baseuri];
146 HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
147 if not (Helm_registry.get_bool "matita.verbose") then
149 let rex = Str.regexp ".*opt$" in
150 if Str.string_match rex Sys.argv.(0) 0 then "matitac.opt"
153 let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
154 print_string s; flush stdout);
155 (* we dalay this error check until we print 'matitac file ' *)
156 assert (Http_getter_storage.is_empty ~local:true baseuri);
157 (* create dir for XML files *)
158 if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
163 (Http_getter.filename ~local:true ~writable:true (baseuri ^
165 let buf = Ulexing.from_utf8_channel (open_in fname) in
167 if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
168 else pp_ast_statement
170 let grafite_status, lexicon_status =
171 let rec aux_for_dump x =
174 MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
175 lexicon_status grafite_status buf x
177 | [] -> grafite_status, lexicon_status
178 | ((grafite,lexicon),None)::_ -> grafite, lexicon
179 | ((_,l),Some _)::_ -> raise (AttemptToInsertAnAlias l)
181 with MatitaEngine.EnrichedWithLexiconStatus
182 (GrafiteEngine.Macro (floc, f), lex_status) as exn ->
183 match f (get_macro_context (Some grafite_status)) with
184 | _, GrafiteAst.Inline (_, style, suri, prefix) ->
186 ApplyTransformation.txt_of_inline_macro style suri prefix
187 ~map_unicode_to_tex:(Helm_registry.get_bool
188 "matita.paste_unicode_as_tex") in
193 aux_for_dump print_cb
195 let elapsed = Unix.time () -. time in
196 let proof_status,moo_content_rev,lexicon_content_rev =
197 grafite_status.proof_status, grafite_status.moo_content_rev,
198 lexicon_status.LexiconEngine.lexicon_content_rev
200 if proof_status <> GrafiteTypes.No_proof then
202 "there are still incomplete proofs at the end of the script";
203 pp_times fname false big_bang big_bang_u big_bang_s;
204 LexiconSync.time_travel
205 ~present:lexicon_status ~past:initial_lexicon_status;
206 clean_exit baseuri false)
208 (if not (Helm_registry.get_bool "matita.moo" &&
209 Filename.check_suffix fname ".mma") then begin
210 (* FG: we do not generate .moo when dumping .mma files *)
211 GrafiteMarshal.save_moo moo_fname moo_content_rev;
212 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
214 let tm = Unix.gmtime elapsed in
215 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
217 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else ""
220 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
223 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
224 pp_times fname true big_bang big_bang_u big_bang_s;
225 LexiconSync.time_travel
226 ~present:lexicon_status ~past:initial_lexicon_status;
229 (* all exceptions should be wrapped to allow lexicon-undo (LS.time_travel) *)
230 | AttemptToInsertAnAlias lexicon_status ->
231 pp_times fname false big_bang big_bang_u big_bang_s;
232 LexiconSync.time_travel
233 ~present:lexicon_status ~past:initial_lexicon_status;
234 clean_exit baseuri false
235 | MatitaEngine.EnrichedWithLexiconStatus (exn, lex_stat) ->
237 | Sys.Break -> HLog.error "user break!"
238 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
239 let (x, y) = HExtlib.loc_of_floc floc in
240 HLog.error (sprintf "Parse error at %d-%d: %s" x y err)
241 | exn -> HLog.error (snd (MatitaExcPp.to_string exn)));
242 LexiconSync.time_travel ~present:lex_stat ~past:initial_lexicon_status;
243 pp_times fname false big_bang big_bang_u big_bang_s;
244 clean_exit baseuri false
245 | Sys.Break as exn ->
246 if matita_debug then raise exn;
247 HLog.error "user break!";
248 pp_times fname false big_bang big_bang_u big_bang_s;
249 clean_exit baseuri false
251 if matita_debug then raise exn;
253 ("Unwrapped exception, please fix: "^ snd (MatitaExcPp.to_string exn));
254 pp_times fname false big_bang big_bang_u big_bang_s;
255 clean_exit baseuri false
260 type source_object = string
261 type target_object = string
262 let string_of_source_object s = s;;
263 let string_of_target_object s = s;;
265 let is_readonly_buri_of opts file =
266 let buri = List.assoc "baseuri" opts in
267 Http_getter_storage.is_read_only (Librarian.mk_baseuri buri file)
270 let root_and_target_of opts mafile =
272 let include_paths = get_include_paths opts in
273 let root,baseuri,_,_ =
274 Librarian.baseuri_of_script ~include_paths mafile
276 Some root, LibraryMisc.obj_file_of_baseuri
277 ~must_exist:false ~baseuri ~writable:true
278 with Librarian.NoRootFor x -> None, ""
281 let mtime_of_source_object s =
282 try Some (Unix.stat s).Unix.st_mtime
283 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
286 let mtime_of_target_object s =
287 try Some (Unix.stat s).Unix.st_mtime
288 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
291 let build = compile;;
293 let load_deps_file = Librarian.load_deps_file;;
297 module Make = Librarian.Make(F)