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 _
82 | GrafiteEngine.Macro _ as exn -> raise exn
84 if not matita_debug then
85 HLog.error (snd (MatitaExcPp.to_string exn)) ;
89 let rec aux = function
92 | [] -> MatitaInit.die_usage ()
96 String.concat " " (List.map (fun x -> "'" ^ x ^ "'") l));
97 MatitaInit.die_usage ()
99 aux (Helm_registry.get_list Helm_registry.string "matita.args")
101 let pp_ocaml_mode () =
103 HLog.message " ** Entering Ocaml mode ** ";
105 HLog.message "Type 'go ();;' to enter an interactive matitac";
114 match !grafite_status with
116 | Some grafite_status ->
118 let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
119 LibraryClean.clean_baseuris ~verbose:false [baseuri];
121 with GrafiteTypes.Option_error("baseuri", "not found") ->
122 (* no baseuri ==> nothing to clean yet *)
125 let get_macro_context = function
126 | Some {GrafiteTypes.proof_status = GrafiteTypes.No_proof} -> []
128 let stack = GrafiteTypes.get_stack status in
129 let goal = Continuationals.Stack.find_goal stack in
130 GrafiteTypes.get_proof_context status goal
131 | None -> assert false
133 let rec interactive_loop () =
134 let str = Ulexing.from_utf8_channel stdin in
137 (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
138 ~include_paths:(Helm_registry.get_list Helm_registry.string
141 | GrafiteEngine.Drop -> pp_ocaml_mode ()
142 | GrafiteEngine.Macro (floc, f) ->
143 begin match f (get_macro_context !grafite_status) with
144 | _, GrafiteAst.Inline (_, style, suri, prefix) ->
145 let str = ApplyTransformation.txt_of_inline_macro style suri prefix in
149 let x, y = HExtlib.loc_of_floc floc in
150 HLog.error (sprintf "A macro has been found in a script at %d-%d" x y);
153 | Sys.Break -> HLog.error "user break!"; interactive_loop ()
154 | GrafiteTypes.Command_error _ -> interactive_loop ()
158 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
159 let x, y = HExtlib.loc_of_floc floc in
160 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
162 | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
165 Helm_registry.load_from BuildTimeConf.matita_conf;
167 MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
168 LibraryDb.create_owner_environment ();
169 CicEnvironment.set_trust (* environment trust *)
171 Helm_registry.get_opt_default Helm_registry.get_bool
172 ~default:true "matita.environment_trust" in
175 Helm_registry.get_list Helm_registry.string "matita.includes" in
176 grafite_status := Some (GrafiteSync.init ());
178 Some (CicNotation2.load_notation ~include_paths
179 BuildTimeConf.core_notation_script);
180 Sys.catch_break true;
183 let pp_times fname bench_mode rc big_bang =
186 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
187 let r = Unix.gettimeofday () -. big_bang in
188 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
190 if Str.string_match (Str.regexp ".*opt$") Sys.argv.(0) 0 then
195 let rc = if rc then "
\e[0;32mOK
\e[0m" else "
\e[0;31mFAIL
\e[0m" in
198 let seconds = int_of_float t in
199 let cents = int_of_float ((t -. floor t) *. 100.0) in
200 let minutes = seconds / 60 in
201 let seconds = seconds mod 60 in
202 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
204 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
207 match MatitamakeLib.development_for_dir (Filename.dirname fname) with
210 let rootlen = String.length(MatitamakeLib.root_for_development d)in
211 let fnamelen = String.length fname in
212 assert (fnamelen > rootlen);
213 String.sub fname rootlen (fnamelen - rootlen)
216 if fname.[0] = '/' then
217 String.sub fname 1 (String.length fname - 1)
221 let s = Printf.sprintf "%s %-35s %-4s %s %s" cc fname rc times extra in
227 let rec compiler_loop fname big_bang mode buf =
229 Helm_registry.get_list Helm_registry.string "matita.includes" in
230 let clean_baseuri = not (Helm_registry.get_bool "matita.preserve") in
231 let matita_debug = Helm_registry.get_bool "matita.debug" in
232 let bench_mode = Helm_registry.get_bool "matita.bench" in
235 (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
239 | Sys.Break as exn ->
240 if matita_debug then raise exn;
241 HLog.error "user break!";
242 pp_times fname bench_mode false big_bang;
243 if mode = `COMPILER then
244 clean_exit (Some ~-1)
247 | GrafiteEngine.Drop ->
248 if mode = `COMPILER then
250 pp_times fname bench_mode false big_bang;
255 | GrafiteEngine.Macro (floc, f) ->
256 begin match f (get_macro_context !grafite_status) with
257 | _, GrafiteAst.Inline (_, style, suri, prefix) ->
258 let str = ApplyTransformation.txt_of_inline_macro style suri prefix in
260 compiler_loop fname big_bang mode buf
262 let x, y = HExtlib.loc_of_floc floc in
263 HLog.error (sprintf "A macro has been found in a script at %d-%d" x y);
264 if mode = `COMPILER then
266 pp_times fname bench_mode false big_bang;
272 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
273 let (x, y) = HExtlib.loc_of_floc floc in
274 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
275 if mode = `COMPILER then
277 pp_times fname bench_mode false big_bang;
283 if matita_debug then raise exn;
284 if mode = `COMPILER then
286 pp_times fname bench_mode false big_bang;
293 let big_bang = Unix.gettimeofday () in
294 MatitaInit.initialize_all ();
295 (* must be called after init since args are set by cmdline parsing *)
296 let fname = fname () in
297 let system_mode = Helm_registry.get_bool "matita.system" in
298 let bench_mode = Helm_registry.get_bool "matita.bench" in
300 Helm_registry.set_int "matita.verbosity" 0;
302 Helm_registry.get_list Helm_registry.string "matita.includes" in
303 grafite_status := Some (GrafiteSync.init ());
305 Some (CicNotation2.load_notation ~include_paths
306 BuildTimeConf.core_notation_script);
307 Sys.catch_break true;
308 let origcb = HLog.get_log_callback () in
309 let origcb t s = origcb t ((if system_mode then "[S] " else "") ^ s) in
312 | `Debug | `Message -> ()
313 | `Warning | `Error -> origcb tag s
315 if Helm_registry.get_int "matita.verbosity" < 1 then
316 HLog.set_log_callback newcb;
317 if bench_mode then MatitaMisc.shutup ();
318 let time = Unix.time () in
319 if Helm_registry.get_int "matita.verbosity" < 1 && not bench_mode then
320 origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
322 HLog.message (sprintf "execution of %s started:" fname);
323 let ich = match fname with
325 | fname -> open_in fname
327 let buf = Ulexing.from_utf8_channel ich in
328 compiler_loop fname big_bang mode buf;
329 let elapsed = Unix.time () -. time in
330 let tm = Unix.gmtime elapsed in
331 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
333 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else ""
336 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
338 let proof_status,moo_content_rev,metadata,lexicon_content_rev =
339 match !lexicon_status,!grafite_status with
341 s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
342 ss.LexiconEngine.lexicon_content_rev
343 | _,_ -> assert false
345 if proof_status <> GrafiteTypes.No_proof then
348 "there are still incomplete proofs at the end of the script";
349 pp_times fname bench_mode true big_bang;
354 let baseuri, _fullpathforfname =
355 DependenciesParser.baseuri_of_script ~include_paths fname in
357 LibraryMisc.obj_file_of_baseuri
358 ~must_exist:false ~baseuri ~writable:true
361 LibraryMisc.lexicon_file_of_baseuri
362 ~must_exist:false ~baseuri ~writable:true
365 LibraryMisc.metadata_file_of_baseuri
366 ~must_exist:false ~baseuri ~writable:true
368 GrafiteMarshal.save_moo moo_fname moo_content_rev;
369 LibraryNoDb.save_metadata metadata_fname metadata;
370 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
372 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
373 pp_times fname bench_mode true big_bang;