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
36 let set_callback f = out := f
38 let pp_ast_statement st =
39 GrafiteAstPp.pp_statement
40 ~map_unicode_to_tex:(Helm_registry.get_bool
41 "matita.paste_unicode_as_tex")
42 ~term_pp:CicNotationPp.pp_term
43 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:(CicNotationPp.pp_obj CicNotationPp.pp_term) st
45 (** {2 Initialization} *)
47 let grafite_status = (ref None : GrafiteTypes.status option ref)
48 let lexicon_status = (ref None : LexiconEngine.status option ref)
50 let run_script is eval_function =
51 let lexicon_status',grafite_status' =
52 match !lexicon_status,!grafite_status with
53 | Some ss, Some s -> ss,s
56 let slash_n_RE = Pcre.regexp "\\n" in
58 if Helm_registry.get_int "matita.verbosity" < 1 then
61 (fun grafite_status stm ->
62 (* dump_status grafite_status; *)
63 let stm = pp_ast_statement stm in
64 let stm = Pcre.replace ~rex:slash_n_RE stm in
66 if String.length stm > 50 then
67 String.sub stm 0 50 ^ " ..."
71 HLog.debug ("Executing: ``" ^ stm ^ "''"))
73 let matita_debug = Helm_registry.get_bool "matita.debug" in
75 match eval_function lexicon_status' grafite_status' is cb with
76 [] -> raise End_of_file
77 | ((grafite_status'',lexicon_status''),None)::_ ->
78 lexicon_status := Some lexicon_status'';
79 grafite_status := Some grafite_status''
80 | (s,Some _)::_ -> raise AttemptToInsertAnAlias
84 | CicNotationParser.Parse_error _
85 | GrafiteEngine.Macro _ as exn -> raise exn
87 if not matita_debug then
88 HLog.error (snd (MatitaExcPp.to_string exn)) ;
92 let rec aux = function
95 | [] -> MatitaInit.die_usage ()
99 String.concat " " (List.map (fun x -> "'" ^ x ^ "'") l));
100 MatitaInit.die_usage ()
102 aux (Helm_registry.get_list Helm_registry.string "matita.args")
104 let pp_ocaml_mode () =
106 HLog.message " ** Entering Ocaml mode ** ";
108 HLog.message "Type 'go ();;' to enter an interactive matitac";
117 match !grafite_status with
119 | Some grafite_status ->
121 let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
122 LibraryClean.clean_baseuris ~verbose:false [baseuri];
124 with GrafiteTypes.Option_error("baseuri", "not found") ->
125 (* no baseuri ==> nothing to clean yet *)
128 let get_macro_context = function
129 | Some {GrafiteTypes.proof_status = GrafiteTypes.No_proof} -> []
131 let stack = GrafiteTypes.get_stack status in
132 let goal = Continuationals.Stack.find_goal stack in
133 GrafiteTypes.get_proof_context status goal
134 | None -> assert false
136 let rec interactive_loop () =
137 let str = Ulexing.from_utf8_channel stdin in
140 (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
141 ~include_paths:(Helm_registry.get_list Helm_registry.string
144 | GrafiteEngine.Drop -> pp_ocaml_mode ()
145 | GrafiteEngine.Macro (floc, f) ->
146 begin match f (get_macro_context !grafite_status) with
147 | _, GrafiteAst.Inline (_, style, suri, prefix) ->
149 ApplyTransformation.txt_of_inline_macro style suri prefix
150 ~map_unicode_to_tex:(Helm_registry.get_bool
151 "matita.paste_unicode_as_tex")
156 let x, y = HExtlib.loc_of_floc floc in
157 HLog.error (sprintf "A macro has been found in a script at %d-%d" x y);
160 | Sys.Break -> HLog.error "user break!"; interactive_loop ()
161 | GrafiteTypes.Command_error _ -> interactive_loop ()
165 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
166 let x, y = HExtlib.loc_of_floc floc in
167 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
169 | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
172 Helm_registry.load_from BuildTimeConf.matita_conf;
174 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
175 LibraryDb.create_owner_environment ();
176 CicEnvironment.set_trust (* environment trust *)
178 Helm_registry.get_opt_default Helm_registry.get_bool
179 ~default:true "matita.environment_trust" in
182 Helm_registry.get_list Helm_registry.string "matita.includes" in
183 grafite_status := Some (GrafiteSync.init ());
185 Some (CicNotation2.load_notation ~include_paths
186 BuildTimeConf.core_notation_script);
187 Sys.catch_break true;
190 let pp_times fname bench_mode rc big_bang =
193 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
194 let r = Unix.gettimeofday () -. big_bang in
195 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
197 if Str.string_match (Str.regexp ".*opt$") Sys.argv.(0) 0 then
202 let rc = if rc then "
\e[0;32mOK
\e[0m" else "
\e[0;31mFAIL
\e[0m" in
205 let seconds = int_of_float t in
206 let cents = int_of_float ((t -. floor t) *. 100.0) in
207 let minutes = seconds / 60 in
208 let seconds = seconds mod 60 in
209 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
211 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
214 match MatitamakeLib.development_for_dir (Filename.dirname fname) with
217 let rootlen = String.length(MatitamakeLib.root_for_development d)in
218 let fnamelen = String.length fname in
219 assert (fnamelen > rootlen);
220 String.sub fname rootlen (fnamelen - rootlen)
223 if fname.[0] = '/' then
224 String.sub fname 1 (String.length fname - 1)
228 let s = Printf.sprintf "%s %-35s %-4s %s %s" cc fname rc times extra in
234 let rec compiler_loop fname big_bang mode buf =
236 Helm_registry.get_list Helm_registry.string "matita.includes" in
237 let clean_baseuri = not (Helm_registry.get_bool "matita.preserve") in
238 let matita_debug = Helm_registry.get_bool "matita.debug" in
239 let bench_mode = Helm_registry.get_bool "matita.bench" in
242 (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
246 | Sys.Break as exn ->
247 if matita_debug then raise exn;
248 HLog.error "user break!";
249 pp_times fname bench_mode false big_bang;
250 if mode = `COMPILER then
251 clean_exit (Some ~-1)
254 | GrafiteEngine.Drop ->
255 if mode = `COMPILER then
257 pp_times fname bench_mode false big_bang;
262 | GrafiteEngine.Macro (floc, f) ->
263 begin match f (get_macro_context !grafite_status) with
264 | _, GrafiteAst.Inline (_, style, suri, prefix) ->
266 ApplyTransformation.txt_of_inline_macro style suri prefix
267 ~map_unicode_to_tex:(Helm_registry.get_bool
268 "matita.paste_unicode_as_tex") in
270 compiler_loop fname big_bang mode buf
272 let x, y = HExtlib.loc_of_floc floc in
273 HLog.error (sprintf "A macro has been found in a script at %d-%d" x y);
274 if mode = `COMPILER then
276 pp_times fname bench_mode false big_bang;
282 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
283 let (x, y) = HExtlib.loc_of_floc floc in
284 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
285 if mode = `COMPILER then
287 pp_times fname bench_mode false big_bang;
293 if matita_debug then raise exn;
294 if mode = `COMPILER then
296 pp_times fname bench_mode false big_bang;
302 exception ReadOnlyUri of string
305 let big_bang = Unix.gettimeofday () in
306 MatitaInit.initialize_all ();
307 (* must be called after init since args are set by cmdline parsing *)
308 let fname = fname () in
311 (* This does not work yet :-(
313 GrafiteTypes.get_string_option
314 (match !grafite_status with None -> assert false | Some s -> s)
317 (let _root, buri, _fname = Librarian.baseuri_of_script ~include_paths:[] fname in
320 let mangled_baseuri =
322 ( let baseuri = Lazy.force baseuri in
323 let baseuri = String.sub baseuri 5 (String.length baseuri - 5) in
324 let baseuri = Pcre.replace ~pat:"/" ~templ:"_" baseuri in
325 String.uncapitalize baseuri
330 (Filename.dirname fname ^ "/" ^ Lazy.force mangled_baseuri ^ ".ml")) in
331 LibrarySync.set_object_declaration_hook
333 output_string (Lazy.force f)
334 (CicExportation.ppobj (Lazy.force baseuri) obj);
335 flush (Lazy.force f)));
336 let system_mode = Helm_registry.get_bool "matita.system" in
337 let bench_mode = Helm_registry.get_bool "matita.bench" in
339 Helm_registry.set_int "matita.verbosity" 0;
341 Helm_registry.get_list Helm_registry.string "matita.includes" in
342 grafite_status := Some (GrafiteSync.init ());
344 Some (CicNotation2.load_notation ~include_paths
345 BuildTimeConf.core_notation_script);
346 Sys.catch_break true;
347 let origcb = HLog.get_log_callback () in
348 let origcb t s = origcb t ((if system_mode then "[S] " else "") ^ s) in
351 | `Debug | `Message -> ()
352 | `Warning | `Error -> origcb tag s
354 if Helm_registry.get_int "matita.verbosity" < 1 then
355 HLog.set_log_callback newcb;
356 if bench_mode then MatitaMisc.shutup ();
357 let ich, fname, baseuri =
358 if fname = "stdin" || fname = "-" then
359 let fname = "/dev/fd/0" in
360 let _,baseuri,fname =
361 Librarian.baseuri_of_script ~include_paths:[] fname
363 stdin, fname, baseuri
365 let _,baseuri,fname =
366 Librarian.baseuri_of_script ~include_paths fname
368 open_in fname, fname, baseuri
370 (* PUT THIS IN A FUNCTION *)
371 if Http_getter_storage.is_read_only baseuri then begin
372 HLog.error (Printf.sprintf "uri %s belongs to a read-only repository"
374 raise (ReadOnlyUri baseuri)
376 if (not (Http_getter_storage.is_empty ~local:true baseuri) ||
377 LibraryClean.db_uris_of_baseuri baseuri <> [])
379 HLog.message ("baseuri " ^ baseuri ^ " is not empty");
380 HLog.message ("cleaning baseuri " ^ baseuri);
381 LibraryClean.clean_baseuris [baseuri];
382 assert (Http_getter_storage.is_empty ~local:true baseuri);
384 if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
389 (Http_getter.filename ~local:true ~writable:true (baseuri ^
391 (* PUT THIS IN A FUNCTION *)
392 let time = Unix.time () in
393 if Helm_registry.get_int "matita.verbosity" < 1 && not bench_mode then
394 origcb `Message ("compiling " ^
395 Filename.basename fname ^ " in " ^ baseuri ^ " ...")
397 HLog.message (sprintf "execution of %s started in %s:"
398 (Filename.basename fname) baseuri);
399 let buf = Ulexing.from_utf8_channel ich in
400 compiler_loop fname big_bang mode buf;
401 let elapsed = Unix.time () -. time in
402 let tm = Unix.gmtime elapsed in
403 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
405 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
408 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
410 let proof_status,moo_content_rev,lexicon_content_rev =
411 match !lexicon_status,!grafite_status with
413 s.proof_status, s.moo_content_rev,
414 ss.LexiconEngine.lexicon_content_rev
415 | _,_ -> assert false
417 if proof_status <> GrafiteTypes.No_proof then
420 "there are still incomplete proofs at the end of the script";
421 pp_times fname bench_mode true big_bang;
427 LibraryMisc.obj_file_of_baseuri
428 ~must_exist:false ~baseuri ~writable:true
431 LibraryMisc.lexicon_file_of_baseuri
432 ~must_exist:false ~baseuri ~writable:true
434 (* FG: we do not generate .moo when dumping .mma files *)
435 if Helm_registry.get_bool "matita.moo" then begin
436 GrafiteMarshal.save_moo moo_fname moo_content_rev;
437 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
440 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
441 pp_times fname bench_mode true big_bang;