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
34 let slash_n_RE = Pcre.regexp "\\n" ;;
36 let pp_ast_statement grafite_status stm =
37 let stm = GrafiteAstPp.pp_statement
38 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
39 ~term_pp:CicNotationPp.pp_term
40 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:(CicNotationPp.pp_obj
41 CicNotationPp.pp_term) stm
43 let stm = Pcre.replace ~rex:slash_n_RE stm in
45 if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
48 HLog.debug ("Executing: ``" ^ stm ^ "''")
51 let clean_exit baseuri rc =
52 LibraryClean.clean_baseuris ~verbose:false [baseuri]; rc
56 let module G = GrafiteAst in
57 let module L = LexiconAst in
58 let module H = HExtlib in
59 let floc = H.dummy_floc in
60 let nl_ast = G.Comment (floc, G.Note (floc, "")) in
61 let pp_statement stm =
62 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
63 ~map_unicode_to_tex:(Helm_registry.get_bool
64 "matita.paste_unicode_as_tex")
65 ~lazy_term_pp:CicNotationPp.pp_term
66 ~obj_pp:(CicNotationPp.pp_obj CicNotationPp.pp_term) stm
68 let pp_lexicon = LexiconAstPp.pp_command in
69 let och = open_out f in
70 let nl () = output_string och (pp_statement nl_ast) in
71 MatitaMisc.out_preamble och;
72 let grafite_parser_cb status = function
73 | G.Executable (_, G.Macro (_, G.Inline (_, uri, params))) ->
75 ApplyTransformation.txt_of_inline_macro params uri
77 (Helm_registry.get_bool "matita.paste_unicode_as_tex")
80 | G.Executable (loc, G.Command (_, G.Include (_, true, _))) -> ()
82 output_string och (pp_statement stm); nl (); nl ()
84 let lexicon_parser_cb status cmd =
85 output_string och (pp_lexicon cmd); nl (); nl ()
88 Helm_registry.set_bool "matita.moo" false;
89 GrafiteParser.set_grafite_callback grafite_parser_cb;
90 GrafiteParser.set_lexicon_callback lexicon_parser_cb
94 GrafiteParser.set_grafite_callback (fun _ _ -> ());
95 GrafiteParser.set_lexicon_callback (fun _ _ -> ());
96 Helm_registry.set_bool "matita.moo" true;
101 let get_macro_context = function
102 | Some {GrafiteTypes.proof_status = GrafiteTypes.No_proof} -> []
104 let stack = GrafiteTypes.get_stack status in
105 let goal = Continuationals.Stack.find_goal stack in
106 GrafiteTypes.get_proof_context status goal
107 | None -> assert false
110 let pp_times fname rc big_bang big_bang_u big_bang_s =
111 if not (Helm_registry.get_bool "matita.verbose") then
112 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
113 let r = Unix.gettimeofday () -. big_bang in
114 let u = u -. big_bang_u in
115 let s = s -. big_bang_s in
116 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
118 if rc then "
\e[0;32mOK
\e[0m","Ok" else "
\e[0;31mFAIL
\e[0m","Fail" in
121 let seconds = int_of_float t in
122 let cents = int_of_float ((t -. floor t) *. 100.0) in
123 let minutes = seconds / 60 in
124 let seconds = seconds mod 60 in
125 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
127 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
129 let s = Printf.sprintf "%-4s %s %s" rc times extra in
132 HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
136 let lenp = String.length prefix in
137 let lens = String.length s in
138 assert (lens > lenp);
139 assert (String.sub s 0 lenp = prefix);
140 String.sub s lenp (lens-lenp)
143 let get_include_paths options =
145 try List.assoc "include_paths" options with Not_found -> ""
147 let include_paths = Str.split (Str.regexp " ") include_paths in
150 Helm_registry.get_list Helm_registry.string "matita.includes"
155 let activate_extraction baseuri fname =
156 if Helm_registry.get_bool "matita.extract" then
157 let mangled_baseuri =
158 let baseuri = String.sub baseuri 5 (String.length baseuri - 5) in
159 let baseuri = Pcre.replace ~pat:"/" ~templ:"_" baseuri in
160 String.uncapitalize baseuri in
163 (Filename.dirname fname ^ "/" ^ mangled_baseuri ^ ".ml") in
164 LibrarySync.add_object_declaration_hook
165 (fun ~add_obj ~add_coercion _ obj ->
166 output_string f (CicExportation.ppobj baseuri obj);
170 let compile atstart options fname =
171 let matita_debug = Helm_registry.get_bool "matita.debug" in
172 let include_paths = get_include_paths options in
173 let root,baseuri,fname,_tgt =
174 Librarian.baseuri_of_script ~include_paths fname in
175 if Http_getter_storage.is_read_only baseuri then assert false;
176 activate_extraction baseuri fname ;
178 CicNotation2.load_notation ~include_paths:[]
179 BuildTimeConf.core_notation_script
181 atstart (); (* FG: do not invoke before loading the core notation script *)
182 let grafite_status = GrafiteSync.init lexicon_status baseuri in
183 let big_bang = Unix.gettimeofday () in
184 let { Unix.tms_utime = big_bang_u ; Unix.tms_stime = big_bang_s} =
187 let time = Unix.time () in
191 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~baseuri ~writable:true
194 LibraryMisc.lexicon_file_of_baseuri
195 ~must_exist:false ~baseuri ~writable:true
197 (* cleanup of previously compiled objects *)
198 if (not (Http_getter_storage.is_empty ~local:true baseuri) ||
199 LibraryClean.db_uris_of_baseuri baseuri <> [])
201 HLog.message ("baseuri " ^ baseuri ^ " is not empty");
202 HLog.message ("cleaning baseuri " ^ baseuri);
203 LibraryClean.clean_baseuris [baseuri];
205 HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
206 if not (Helm_registry.get_bool "matita.verbose") then
208 let rex = Str.regexp ".*opt$" in
209 if Str.string_match rex Sys.argv.(0) 0 then "matitac.opt"
212 let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
213 print_string s; flush stdout);
214 (* we dalay this error check until we print 'matitac file ' *)
215 assert (Http_getter_storage.is_empty ~local:true baseuri);
216 (* create dir for XML files *)
217 if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
222 (Http_getter.filename ~local:true ~writable:true (baseuri ^
224 let buf = Ulexing.from_utf8_channel (open_in fname) in
226 if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
227 else pp_ast_statement
229 let lexicon_status, grafite_status =
230 let rec aux_for_dump x lexicon_status grafite_status =
233 MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
234 lexicon_status grafite_status buf x
236 | [] -> lexicon_status, grafite_status
237 | ((grafite,lexicon),None)::_ -> lexicon, grafite
238 | ((_,l),Some _)::_ -> raise (AttemptToInsertAnAlias l)
239 with MatitaEngine.EnrichedWithStatus
240 (GrafiteEngine.Macro (floc, f), lexicon, grafite) as exn ->
241 match f (get_macro_context (Some grafite)) with
242 | _, GrafiteAst.Inline (_, _suri, _params) ->
245 ApplyTransformation.txt_of_inline_macro style prefix suri
247 ~map_unicode_to_tex:(Helm_registry.get_bool
248 "matita.paste_unicode_as_tex")
252 aux_for_dump x lexicon grafite
255 aux_for_dump print_cb lexicon_status grafite_status
257 let elapsed = Unix.time () -. time in
258 let proof_status,moo_content_rev,lexicon_content_rev =
259 grafite_status.proof_status, grafite_status.moo_content_rev,
260 lexicon_status.LexiconEngine.lexicon_content_rev
262 if proof_status <> GrafiteTypes.No_proof then
264 "there are still incomplete proofs at the end of the script";
265 pp_times fname false big_bang big_bang_u big_bang_s;
267 LexiconSync.time_travel
268 ~present:lexicon_status ~past:initial_lexicon_status;
270 clean_exit baseuri false)
272 (if Helm_registry.get_bool "matita.moo" then begin
273 (* FG: we do not generate .moo when dumping .mma files *)
274 GrafiteMarshal.save_moo moo_fname moo_content_rev;
275 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
277 let tm = Unix.gmtime elapsed in
278 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
280 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else ""
283 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
286 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
287 pp_times fname true big_bang big_bang_u big_bang_s;
289 LexiconSync.time_travel
290 ~present:lexicon_status ~past:initial_lexicon_status;
294 (* all exceptions should be wrapped to allow lexicon-undo (LS.time_travel) *)
295 | AttemptToInsertAnAlias lexicon_status ->
296 pp_times fname false big_bang big_bang_u big_bang_s;
298 LexiconSync.time_travel
299 ~present:lexicon_status ~past:initial_lexicon_status;
301 clean_exit baseuri false
302 | MatitaEngine.EnrichedWithStatus (exn, _lexicon, _grafite) as exn' ->
304 | Sys.Break -> HLog.error "user break!"
305 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
306 let (x, y) = HExtlib.loc_of_floc floc in
307 HLog.error (sprintf "Parse error at %d-%d: %s" x y err)
308 | exn when matita_debug -> raise exn'
309 | exn -> HLog.error (snd (MatitaExcPp.to_string exn))
311 (* LexiconSync.time_travel ~present:lexicon ~past:initial_lexicon_status;
313 pp_times fname false big_bang big_bang_u big_bang_s;
314 clean_exit baseuri false
315 | Sys.Break when not matita_debug ->
316 HLog.error "user break!";
317 pp_times fname false big_bang big_bang_u big_bang_s;
318 clean_exit baseuri false
319 | exn when not matita_debug ->
321 ("Unwrapped exception, please fix: "^ snd (MatitaExcPp.to_string exn));
322 pp_times fname false big_bang big_bang_u big_bang_s;
323 clean_exit baseuri false
327 type source_object = string
328 type target_object = string
329 let string_of_source_object s = s;;
330 let string_of_target_object s = s;;
332 let is_readonly_buri_of opts file =
333 let buri = List.assoc "baseuri" opts in
334 Http_getter_storage.is_read_only (Librarian.mk_baseuri buri file)
337 let root_and_target_of opts mafile =
339 let include_paths = get_include_paths opts in
340 let root,baseuri,_,relpath =
341 Librarian.baseuri_of_script ~include_paths mafile
343 let obj_writeable, obj_read_only =
344 if Filename.check_suffix mafile ".mma" then
345 Filename.chop_suffix mafile ".mma" ^ ".ma",
346 Filename.chop_suffix mafile ".mma" ^ ".ma"
348 LibraryMisc.obj_file_of_baseuri
349 ~must_exist:false ~baseuri ~writable:true,
350 LibraryMisc.obj_file_of_baseuri
351 ~must_exist:false ~baseuri ~writable:false
353 Some root, relpath, obj_writeable, obj_read_only
354 with Librarian.NoRootFor x -> None, "", "", ""
357 let mtime_of_source_object s =
358 try Some (Unix.stat s).Unix.st_mtime
359 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
362 let mtime_of_target_object s =
363 try Some (Unix.stat s).Unix.st_mtime
364 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
367 (* FG: a problem was noticed in relising memory between subsequent *)
368 (* invocations of the compiler. The following might help *)
369 let compact r = Gc.compact (); r
371 let build options fname =
372 let matita_debug = Helm_registry.get_bool "matita.debug" in
373 let compile atstart opts fname =
376 GrafiteParser.push ();
377 let rc = compile atstart opts fname in
378 GrafiteParser.pop ();
383 GrafiteParser.pop ();
386 | exn when not matita_debug ->
387 HLog.error ("Unexpected " ^ snd(MatitaExcPp.to_string exn));
390 if Filename.check_suffix fname ".mma" then
391 let generated = Filename.chop_suffix fname ".mma" ^ ".ma" in
392 let atstart, atexit = dump generated in
393 let res = compile atstart options fname in
394 let r = compact (atexit res) in
395 if r then r else begin
396 (* Sys.remove generated; *)
397 Printf.printf "rm %s\n" generated; flush stdout; r
400 compile ignore options fname
403 let load_deps_file = Librarian.load_deps_file;;
407 module Make = Librarian.Make(F)