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
35 let set_callback f = out := f
37 let slash_n_RE = Pcre.regexp "\\n" ;;
39 let pp_ast_statement grafite_status stm =
40 let stm = GrafiteAstPp.pp_statement
41 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
42 ~term_pp:CicNotationPp.pp_term
43 ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:(CicNotationPp.pp_obj
44 CicNotationPp.pp_term) stm
46 let stm = Pcre.replace ~rex:slash_n_RE stm in
48 if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
51 HLog.debug ("Executing: ``" ^ stm ^ "''")
54 let clean_exit baseuri rc =
55 LibraryClean.clean_baseuris ~verbose:false [baseuri]; rc
58 let get_macro_context = function
59 | Some {GrafiteTypes.proof_status = GrafiteTypes.No_proof} -> []
61 let stack = GrafiteTypes.get_stack status in
62 let goal = Continuationals.Stack.find_goal stack in
63 GrafiteTypes.get_proof_context status goal
64 | None -> assert false
67 let pp_times fname rc big_bang =
68 if not (Helm_registry.get_bool "matita.verbose") then
69 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
70 let r = Unix.gettimeofday () -. big_bang in
71 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
73 if rc then "
\e[0;32mOK
\e[0m","Ok" else "
\e[0;31mFAIL
\e[0m","Fail" in
76 let seconds = int_of_float t in
77 let cents = int_of_float ((t -. floor t) *. 100.0) in
78 let minutes = seconds / 60 in
79 let seconds = seconds mod 60 in
80 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
82 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
84 let s = Printf.sprintf "%-4s %s %s" rc times extra in
87 HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
91 let lenp = String.length prefix in
92 let lens = String.length s in
94 assert (String.sub s 0 lenp = prefix);
95 String.sub s lenp (lens-lenp)
98 let rec compile fname =
99 (* initialization, MOVE OUTSIDE *)
100 let matita_debug = Helm_registry.get_bool "matita.debug" in
101 let clean_baseuri = not (Helm_registry.get_bool "matita.preserve") in
103 Helm_registry.get_list Helm_registry.string "matita.includes"
106 let root,baseuri,fname = Librarian.baseuri_of_script ~include_paths fname in
108 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~baseuri ~writable:true
111 LibraryMisc.lexicon_file_of_baseuri ~must_exist:false ~baseuri ~writable:true
113 if Http_getter_storage.is_read_only baseuri then
115 (Printf.sprintf "uri %s belongs to a read-only repository" baseuri);
116 (* cleanup of previously compiled objects *)
117 if (not (Http_getter_storage.is_empty ~local:true baseuri) ||
118 LibraryClean.db_uris_of_baseuri baseuri <> []) && clean_baseuri
120 HLog.message ("baseuri " ^ baseuri ^ " is not empty");
121 HLog.message ("cleaning baseuri " ^ baseuri);
122 LibraryClean.clean_baseuris [baseuri];
123 assert (Http_getter_storage.is_empty ~local:true baseuri);
125 (* create dir for XML files *)
126 if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
131 (Http_getter.filename ~local:true ~writable:true (baseuri ^
133 (* begin of compilation *)
134 let grafite_status = GrafiteSync.init baseuri in
136 CicNotation2.load_notation ~include_paths:[]
137 BuildTimeConf.core_notation_script
139 let big_bang = Unix.gettimeofday () in
140 let time = Unix.time () in
141 HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
142 if not (Helm_registry.get_bool "matita.verbose") then
144 if Str.string_match(Str.regexp ".*opt$") Sys.argv.(0) 0 then "matitac.opt"
147 let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
148 print_string s; flush stdout);
149 let buf = Ulexing.from_utf8_channel (open_in fname) in
151 if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
152 else pp_ast_statement
155 let grafite_status, lexicon_status =
157 MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
158 lexicon_status grafite_status buf print_cb
160 | [] -> raise End_of_file
161 | ((grafite,lexicon),None)::_ -> grafite, lexicon
162 | (s,Some _)::_ -> raise AttemptToInsertAnAlias
164 let elapsed = Unix.time () -. time in
165 let proof_status,moo_content_rev,lexicon_content_rev =
166 grafite_status.proof_status, grafite_status.moo_content_rev,
167 lexicon_status.LexiconEngine.lexicon_content_rev
169 if proof_status <> GrafiteTypes.No_proof then
171 "there are still incomplete proofs at the end of the script";
172 pp_times fname false big_bang;
173 clean_exit baseuri false)
175 (if Helm_registry.get_bool "matita.moo" then begin
176 (* FG: we do not generate .moo when dumping .mma files *)
177 GrafiteMarshal.save_moo moo_fname moo_content_rev;
178 LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
180 let tm = Unix.gmtime elapsed in
181 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
183 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else ""
186 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
189 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
190 pp_times fname true big_bang;
193 | End_of_file -> HLog.error "End_of_file?!"; clean_exit baseuri false
194 | Sys.Break as exn ->
195 if matita_debug then raise exn;
196 HLog.error "user break!";
197 pp_times fname false big_bang;
198 clean_exit baseuri false
199 | GrafiteEngine.Macro (floc, f) ->
200 (match f (get_macro_context (Some grafite_status)) with
201 | _, GrafiteAst.Inline (_, style, suri, prefix) ->
203 ApplyTransformation.txt_of_inline_macro style suri prefix
204 ~map_unicode_to_tex:(Helm_registry.get_bool
205 "matita.paste_unicode_as_tex") in
209 let x, y = HExtlib.loc_of_floc floc in
210 HLog.error (sprintf "A macro has been found at %d-%d" x y);
211 pp_times fname false big_bang;
212 clean_exit baseuri false)
213 | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
214 let (x, y) = HExtlib.loc_of_floc floc in
215 HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
216 pp_times fname false big_bang;
217 clean_exit baseuri false
219 if matita_debug then raise exn;
220 HLog.error (snd (MatitaExcPp.to_string exn));
221 pp_times fname false big_bang;
222 clean_exit baseuri false
227 type source_object = string
228 type target_object = string
229 let string_of_source_object s = s;;
230 let string_of_target_object s = s;;
232 let target_of mafile =
233 let _,baseuri,_ = Librarian.baseuri_of_script mafile in
234 LibraryMisc.obj_file_of_baseuri
235 ~must_exist:false ~baseuri ~writable:true
238 let mtime_of_source_object s =
239 try Some (Unix.stat s).Unix.st_mtime
240 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s ->
241 raise (Failure ("Unable to stat a source file: " ^ s))
244 let mtime_of_target_object s =
245 try Some (Unix.stat s).Unix.st_mtime
246 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
249 let build = compile;;
252 module Make = Make.Make(F)