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
38 let slash_n_RE = Pcre.regexp "\\n" ;;
40 let pp_ast_statement grafite_status stm =
41 let stm = GrafiteAstPp.pp_statement
42 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
43 ~term_pp:CicNotationPp.pp_term
44 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:(CicNotationPp.pp_obj
45 CicNotationPp.pp_term) stm
47 let stm = Pcre.replace ~rex:slash_n_RE stm in
49 if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
52 HLog.debug ("Executing: ``" ^ stm ^ "''")
55 let clean_exit baseuri rc =
56 LibraryClean.clean_baseuris ~verbose:false [baseuri]; rc
60 let module G = GrafiteAst in
61 let module L = LexiconAst in
62 let module H = HExtlib in
63 Helm_registry.set_bool "matita.moo" false;
64 let floc = H.dummy_floc in
65 let nl_ast = G.Comment (floc, G.Note (floc, "")) in
66 let pp_statement stm =
67 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
68 ~map_unicode_to_tex:(Helm_registry.get_bool
69 "matita.paste_unicode_as_tex")
70 ~lazy_term_pp:CicNotationPp.pp_term
71 ~obj_pp:(CicNotationPp.pp_obj CicNotationPp.pp_term) stm
73 let pp_lexicon = LexiconAstPp.pp_command in
74 let och = open_out f in
75 let nl () = output_string och (pp_statement nl_ast) in
76 MatitaMisc.out_preamble och;
77 let grafite_parser_cb status = function
78 | G.Executable (_, G.Macro (_, G.Inline _)) -> ()
80 output_string och (pp_statement stm); nl (); nl ()
82 let lexicon_parser_cb status cmd =
83 output_string och (pp_lexicon cmd); nl (); nl ()
86 let matita_engine_cb = function
87 | G.Executable (_, G.Macro (_, G.Inline _))
88 | G.Executable (_, G.Command (_, G.Include _)) -> ()
91 let matitac_lib_cb = output_string och in
93 GrafiteParser.set_grafite_callback grafite_parser_cb;
94 GrafiteParser.set_lexicon_callback lexicon_parser_cb;
96 MatitaEngine.set_callback matita_engine_cb;
98 set_callback matitac_lib_cb
102 GrafiteParser.set_grafite_callback (fun _ _ -> ());
103 GrafiteParser.set_lexicon_callback (fun _ _ -> ());
104 set_callback ignore; x
108 let get_macro_context = function
109 | Some {GrafiteTypes.proof_status = GrafiteTypes.No_proof} -> []
111 let stack = GrafiteTypes.get_stack status in
112 let goal = Continuationals.Stack.find_goal stack in
113 GrafiteTypes.get_proof_context status goal
114 | None -> assert false
117 let pp_times fname rc big_bang big_bang_u big_bang_s =
118 if not (Helm_registry.get_bool "matita.verbose") then
119 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
120 let r = Unix.gettimeofday () -. big_bang in
121 let u = u -. big_bang_u in
122 let s = s -. big_bang_s in
123 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
125 if rc then "
\e[0;32mOK
\e[0m","Ok" else "
\e[0;31mFAIL
\e[0m","Fail" in
128 let seconds = int_of_float t in
129 let cents = int_of_float ((t -. floor t) *. 100.0) in
130 let minutes = seconds / 60 in
131 let seconds = seconds mod 60 in
132 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
134 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
136 let s = Printf.sprintf "%-4s %s %s" rc times extra in
139 HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
143 let lenp = String.length prefix in
144 let lens = String.length s in
145 assert (lens > lenp);
146 assert (String.sub s 0 lenp = prefix);
147 String.sub s lenp (lens-lenp)
150 let get_include_paths options =
152 try List.assoc "include_paths" options with Not_found -> ""
154 let include_paths = Str.split (Str.regexp " ") include_paths in
157 Helm_registry.get_list Helm_registry.string "matita.includes"
162 let activate_extraction baseuri fname =
163 if Helm_registry.get_bool "matita.extract" then
164 let mangled_baseuri =
165 let baseuri = String.sub baseuri 5 (String.length baseuri - 5) in
166 let baseuri = Pcre.replace ~pat:"/" ~templ:"_" baseuri in
167 String.uncapitalize baseuri in
170 (Filename.dirname fname ^ "/" ^ mangled_baseuri ^ ".ml") in
171 LibrarySync.add_object_declaration_hook
172 (fun ~add_obj ~add_coercion _ obj ->
173 output_string f (CicExportation.ppobj baseuri obj);
177 let compile atstart options fname =
178 let matita_debug = Helm_registry.get_bool "matita.debug" in
179 let include_paths = get_include_paths options in
180 let root,baseuri,fname,_tgt =
181 Librarian.baseuri_of_script ~include_paths fname in
182 if Http_getter_storage.is_read_only baseuri then assert false;
183 activate_extraction baseuri fname ;
185 CicNotation2.load_notation ~include_paths:[]
186 BuildTimeConf.core_notation_script
189 let grafite_status = GrafiteSync.init lexicon_status baseuri in
190 let big_bang = Unix.gettimeofday () in
191 let { Unix.tms_utime = big_bang_u ; Unix.tms_stime = big_bang_s} =
194 let time = Unix.time () in
198 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~baseuri ~writable:true
201 LibraryMisc.lexicon_file_of_baseuri
202 ~must_exist:false ~baseuri ~writable:true
204 (* cleanup of previously compiled objects *)
205 if (not (Http_getter_storage.is_empty ~local:true baseuri) ||
206 LibraryClean.db_uris_of_baseuri baseuri <> [])
208 HLog.message ("baseuri " ^ baseuri ^ " is not empty");
209 HLog.message ("cleaning baseuri " ^ baseuri);
210 LibraryClean.clean_baseuris [baseuri];
212 HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
213 if not (Helm_registry.get_bool "matita.verbose") then
215 let rex = Str.regexp ".*opt$" in
216 if Str.string_match rex Sys.argv.(0) 0 then "matitac.opt"
219 let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
220 print_string s; flush stdout);
221 (* we dalay this error check until we print 'matitac file ' *)
222 assert (Http_getter_storage.is_empty ~local:true baseuri);
223 (* create dir for XML files *)
224 if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
229 (Http_getter.filename ~local:true ~writable:true (baseuri ^
231 let buf = Ulexing.from_utf8_channel (open_in fname) in
233 if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
234 else pp_ast_statement
236 let grafite_status, lexicon_status =
237 let rec aux_for_dump x =
240 MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
241 lexicon_status grafite_status buf x
243 | [] -> grafite_status, lexicon_status
244 | ((grafite,lexicon),None)::_ -> grafite, lexicon
245 | ((_,l),Some _)::_ -> raise (AttemptToInsertAnAlias l)
247 with MatitaEngine.EnrichedWithLexiconStatus
248 (GrafiteEngine.Macro (floc, f), lex_status) as exn ->
249 match f (get_macro_context (Some grafite_status)) with
250 | _, GrafiteAst.Inline (_, style, suri, prefix, flavour) ->
252 ApplyTransformation.txt_of_inline_macro style prefix suri
254 ~map_unicode_to_tex:(Helm_registry.get_bool
255 "matita.paste_unicode_as_tex")
261 aux_for_dump print_cb
263 let elapsed = Unix.time () -. time in
264 let proof_status,moo_content_rev,lexicon_content_rev =
265 grafite_status.proof_status, grafite_status.moo_content_rev,
266 lexicon_status.LexiconEngine.lexicon_content_rev
268 if proof_status <> GrafiteTypes.No_proof then
270 "there are still incomplete proofs at the end of the script";
271 pp_times fname false big_bang big_bang_u big_bang_s;
273 LexiconSync.time_travel
274 ~present:lexicon_status ~past:initial_lexicon_status;
276 clean_exit baseuri false)
278 (if not (Helm_registry.get_bool "matita.moo" &&
279 Filename.check_suffix fname ".mma") then begin
280 (* FG: we do not generate .moo when dumping .mma files *)
281 GrafiteMarshal.save_moo moo_fname moo_content_rev;
282 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
284 let tm = Unix.gmtime elapsed in
285 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
287 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else ""
290 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
293 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
294 pp_times fname true big_bang big_bang_u big_bang_s;
296 LexiconSync.time_travel
297 ~present:lexicon_status ~past:initial_lexicon_status;
301 (* all exceptions should be wrapped to allow lexicon-undo (LS.time_travel) *)
302 | AttemptToInsertAnAlias lexicon_status ->
303 pp_times fname false big_bang big_bang_u big_bang_s;
305 LexiconSync.time_travel
306 ~present:lexicon_status ~past:initial_lexicon_status;
308 clean_exit baseuri false
309 | MatitaEngine.EnrichedWithLexiconStatus (exn, lex_stat) as exn' ->
311 | Sys.Break -> HLog.error "user break!"
312 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
313 let (x, y) = HExtlib.loc_of_floc floc in
314 HLog.error (sprintf "Parse error at %d-%d: %s" x y err)
315 | exn when matita_debug -> raise exn'
316 | exn -> HLog.error (snd (MatitaExcPp.to_string exn))
318 (* LexiconSync.time_travel ~present:lex_stat ~past:initial_lexicon_status;
320 pp_times fname false big_bang big_bang_u big_bang_s;
321 clean_exit baseuri false
322 | Sys.Break when not matita_debug ->
323 HLog.error "user break!";
324 pp_times fname false big_bang big_bang_u big_bang_s;
325 clean_exit baseuri false
326 | exn when not matita_debug ->
328 ("Unwrapped exception, please fix: "^ snd (MatitaExcPp.to_string exn));
329 pp_times fname false big_bang big_bang_u big_bang_s;
330 clean_exit baseuri false
334 type source_object = string
335 type target_object = string
336 let string_of_source_object s = s;;
337 let string_of_target_object s = s;;
339 let is_readonly_buri_of opts file =
340 let buri = List.assoc "baseuri" opts in
341 Http_getter_storage.is_read_only (Librarian.mk_baseuri buri file)
344 let root_and_target_of opts mafile =
346 let include_paths = get_include_paths opts in
347 let root,baseuri,_,_ =
348 Librarian.baseuri_of_script ~include_paths mafile
350 let obj_writeable, obj_read_only =
351 if Filename.check_suffix mafile ".mma" then
352 Filename.chop_suffix mafile ".mma" ^ ".ma",
353 Filename.chop_suffix mafile ".mma" ^ ".ma"
355 LibraryMisc.obj_file_of_baseuri
356 ~must_exist:false ~baseuri ~writable:true,
357 LibraryMisc.obj_file_of_baseuri
358 ~must_exist:false ~baseuri ~writable:false
360 Some root, obj_writeable, obj_read_only
361 with Librarian.NoRootFor x -> None, "", ""
364 let mtime_of_source_object s =
365 try Some (Unix.stat s).Unix.st_mtime
366 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
369 let mtime_of_target_object s =
370 try Some (Unix.stat s).Unix.st_mtime
371 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
374 (* FG: a problem was noticed in relising memory between subsequent *)
375 (* invocations of the compiler. The following might help *)
376 let compact r = Gc.compact (); r
378 let build options fname =
379 let matita_debug = Helm_registry.get_bool "matita.debug" in
380 let compile atstart opts fname =
383 GrafiteParser.push ();
384 let rc = compile atstart opts fname in
385 GrafiteParser.pop ();
390 GrafiteParser.pop ();
393 | exn when not matita_debug ->
394 HLog.error ("Unexpected " ^ snd(MatitaExcPp.to_string exn));
397 if Filename.check_suffix fname ".mma" then
398 let generated = Filename.chop_suffix fname ".mma" ^ ".ma" in
399 let atstart, atexit = dump generated in
400 let res = compile atstart options fname in
401 let r = compact (atexit res) in
402 if r then r else begin
403 (* Sys.remove generated; *)
404 Printf.printf "rm %s\n" generated; flush stdout; r
407 compile ignore options fname
410 let load_deps_file = Librarian.load_deps_file;;
414 module Make = Librarian.Make(F)