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 =
39 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
40 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:(CicNotationPp.pp_obj CicNotationPp.pp_term)
42 (** {2 Initialization} *)
44 let grafite_status = (ref None : GrafiteTypes.status option ref)
45 let lexicon_status = (ref None : LexiconEngine.status option ref)
47 let run_script is eval_function =
48 let lexicon_status',grafite_status' =
49 match !lexicon_status,!grafite_status with
50 | Some ss, Some s -> ss,s
53 let slash_n_RE = Pcre.regexp "\\n" in
55 if Helm_registry.get_int "matita.verbosity" < 1 then
58 (fun grafite_status stm ->
59 (* dump_status grafite_status; *)
60 let stm = pp_ast_statement stm in
61 let stm = Pcre.replace ~rex:slash_n_RE stm in
63 if String.length stm > 50 then
64 String.sub stm 0 50 ^ " ..."
68 HLog.debug ("Executing: ``" ^ stm ^ "''"))
70 let matita_debug = Helm_registry.get_bool "matita.debug" in
72 match eval_function lexicon_status' grafite_status' is cb with
73 [] -> raise End_of_file
74 | ((grafite_status'',lexicon_status''),None)::_ ->
75 lexicon_status := Some lexicon_status'';
76 grafite_status := Some grafite_status''
77 | (s,Some _)::_ -> raise AttemptToInsertAnAlias
81 | CicNotationParser.Parse_error _ as exn -> raise exn
83 if not matita_debug then
84 HLog.error (snd (MatitaExcPp.to_string exn)) ;
88 let rec aux = function
91 | [] -> MatitaInit.die_usage ()
95 String.concat " " (List.map (fun x -> "'" ^ x ^ "'") l));
96 MatitaInit.die_usage ()
98 aux (Helm_registry.get_list Helm_registry.string "matita.args")
100 let pp_ocaml_mode () =
102 HLog.message " ** Entering Ocaml mode ** ";
104 HLog.message "Type 'go ();;' to enter an interactive matitac";
113 match !grafite_status with
115 | Some grafite_status ->
117 let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
118 LibraryClean.clean_baseuris ~verbose:false [baseuri];
120 with GrafiteTypes.Option_error("baseuri", "not found") ->
121 (* no baseuri ==> nothing to clean yet *)
124 let rec interactive_loop () =
125 let str = Ulexing.from_utf8_channel stdin in
128 (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
129 ~include_paths:(Helm_registry.get_list Helm_registry.string
132 | GrafiteEngine.Drop -> pp_ocaml_mode ()
133 | GrafiteEngine.Macro (floc, f) ->
134 begin match snd (f []) with
135 | GrafiteAst.Inline (_, style, suri, prefix) ->
136 let str = ApplyTransformation.txt_of_inline_macro style suri prefix in
140 let x, y = HExtlib.loc_of_floc floc in
142 (sprintf "A macro has been found in a script at %d-%d" x y);
144 | Sys.Break -> HLog.error "user break!"; interactive_loop ()
145 | GrafiteTypes.Command_error _ -> interactive_loop ()
149 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
150 let x, y = HExtlib.loc_of_floc floc in
151 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
153 | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
156 Helm_registry.load_from BuildTimeConf.matita_conf;
158 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
159 LibraryDb.create_owner_environment ();
160 CicEnvironment.set_trust (* environment trust *)
162 Helm_registry.get_opt_default Helm_registry.get_bool
163 ~default:true "matita.environment_trust" in
166 Helm_registry.get_list Helm_registry.string "matita.includes" in
167 grafite_status := Some (GrafiteSync.init ());
169 Some (CicNotation2.load_notation ~include_paths
170 BuildTimeConf.core_notation_script);
171 Sys.catch_break true;
174 let pp_times fname bench_mode rc big_bang =
177 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
178 let r = Unix.gettimeofday () -. big_bang in
179 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
181 if Str.string_match (Str.regexp ".*opt$") Sys.argv.(0) 0 then
186 let rc = if rc then "
\e[0;32mOK
\e[0m" else "
\e[0;31mFAIL
\e[0m" in
189 let seconds = int_of_float t in
190 let cents = int_of_float ((t -. floor t) *. 100.0) in
191 let minutes = seconds / 60 in
192 let seconds = seconds mod 60 in
193 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
195 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
198 match MatitamakeLib.development_for_dir (Filename.dirname fname) with
201 let rootlen = String.length(MatitamakeLib.root_for_development d)in
202 let fnamelen = String.length fname in
203 assert (fnamelen > rootlen);
204 String.sub fname rootlen (fnamelen - rootlen)
207 if fname.[0] = '/' then
208 String.sub fname 1 (String.length fname - 1)
212 let s = Printf.sprintf "%s %-35s %-4s %s %s" cc fname rc times extra in
219 let big_bang = Unix.gettimeofday () in
220 MatitaInit.initialize_all ();
221 (* must be called after init since args are set by cmdline parsing *)
222 let fname = fname () in
223 let system_mode = Helm_registry.get_bool "matita.system" in
224 let bench_mode = Helm_registry.get_bool "matita.bench" in
226 Helm_registry.set_int "matita.verbosity" 0;
228 Helm_registry.get_list Helm_registry.string "matita.includes" in
229 grafite_status := Some (GrafiteSync.init ());
231 Some (CicNotation2.load_notation ~include_paths
232 BuildTimeConf.core_notation_script);
233 Sys.catch_break true;
234 let origcb = HLog.get_log_callback () in
235 let origcb t s = origcb t ((if system_mode then "[S] " else "") ^ s) in
238 | `Debug | `Message -> ()
239 | `Warning | `Error -> origcb tag s
241 if Helm_registry.get_int "matita.verbosity" < 1 then
242 HLog.set_log_callback newcb;
243 if bench_mode then MatitaMisc.shutup ();
244 let matita_debug = Helm_registry.get_bool "matita.debug" in
246 let time = Unix.time () in
247 if Helm_registry.get_int "matita.verbosity" < 1 && not bench_mode then
248 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
250 HLog.message (sprintf "execution of %s started:" fname);
252 Ulexing.from_utf8_channel
255 | fname -> open_in fname) in
257 Helm_registry.get_list Helm_registry.string "matita.includes" in
260 (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
261 ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
262 with End_of_file -> ());
263 let elapsed = Unix.time () -. time in
264 let tm = Unix.gmtime elapsed in
265 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
267 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
270 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
272 let proof_status,moo_content_rev,metadata,lexicon_content_rev =
273 match !lexicon_status,!grafite_status with
275 s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
276 ss.LexiconEngine.lexicon_content_rev
277 | _,_ -> assert false
279 if proof_status <> GrafiteTypes.No_proof then
282 "there are still incomplete proofs at the end of the script";
283 pp_times fname bench_mode true big_bang;
288 let baseuri, _fullpathforfname =
289 DependenciesParser.baseuri_of_script ~include_paths fname in
291 LibraryMisc.obj_file_of_baseuri
292 ~must_exist:false ~baseuri ~writable:true
295 LibraryMisc.lexicon_file_of_baseuri
296 ~must_exist:false ~baseuri ~writable:true
299 LibraryMisc.metadata_file_of_baseuri
300 ~must_exist:false ~baseuri ~writable:true
302 GrafiteMarshal.save_moo moo_fname moo_content_rev;
303 LibraryNoDb.save_metadata metadata_fname metadata;
304 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
306 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
307 pp_times fname bench_mode true big_bang;
311 | Sys.Break as exn ->
312 if matita_debug then raise exn;
313 HLog.error "user break!";
314 pp_times fname bench_mode false big_bang;
315 if mode = `COMPILER then
316 clean_exit (Some ~-1)
319 | GrafiteEngine.Drop ->
320 if mode = `COMPILER then
322 pp_times fname bench_mode false big_bang;
327 | GrafiteEngine.Macro (floc,_) ->
328 let x, y = HExtlib.loc_of_floc floc in
330 (sprintf "A macro has been found in a script at %d-%d" x y);
331 if mode = `COMPILER then
333 pp_times fname bench_mode false big_bang;
338 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
339 let (x, y) = HExtlib.loc_of_floc floc in
340 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
341 if mode = `COMPILER then
343 pp_times fname bench_mode false big_bang;
349 if matita_debug then raise exn;
350 if mode = `COMPILER then
352 pp_times fname bench_mode false big_bang;