1 (* Copyright (C) 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 class status uid baseuri =
34 inherit GrafiteTypes.status uid baseuri
35 inherit ApplyTransformation.status uid
38 exception TryingToAdd of string Lazy.t
39 exception EnrichedWithStatus of exn * status
40 exception AlreadyLoaded of string Lazy.t
41 exception FailureCompiling of string * exn
42 exception CircularDependency of string
45 let debug_print = if debug then prerr_endline else ignore ;;
47 let slash_n_RE = Pcre.regexp "\\n" ;;
49 let pp_ast_statement status stm =
50 let stm = GrafiteAstPp.pp_statement status stm
51 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
53 let stm = Pcre.replace ~rex:slash_n_RE stm in
55 if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
58 HLog.debug ("Executing: ``" ^ stm ^ "''")
61 let clean_exit baseuri exn =
62 LibraryClean.clean_baseuris ~verbose:false [baseuri];
63 raise (FailureCompiling (baseuri,exn))
67 let pp_times fname rc big_bang big_bang_u big_bang_s =
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 u = u -. big_bang_u in
72 let s = s -. big_bang_s in
73 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
75 if rc then "
\e[0;32mOK
\e[0m","Ok" else "
\e[0;31mFAIL
\e[0m","Fail" in
78 let seconds = int_of_float t in
79 let cents = int_of_float ((t -. floor t) *. 100.0) in
80 let minutes = seconds / 60 in
81 let seconds = seconds mod 60 in
82 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
84 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
86 let s = Printf.sprintf "%-4s %s %s" rc times extra in
89 HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
93 let lenp = String.length prefix in
94 let lens = String.length s in
96 assert (String.sub s 0 lenp = prefix);
97 String.sub s lenp (lens-lenp)
100 let activate_extraction baseuri fname =
103 if Helm_registry.get_bool "matita.extract" then
104 let mangled_baseuri =
105 let baseuri = String.sub baseuri 5 (String.length baseuri - 5) in
106 let baseuri = Pcre.replace ~pat:"/" ~templ:"_" baseuri in
107 String.uncapitalize baseuri in
110 (Filename.dirname fname ^ "/" ^ mangled_baseuri ^ ".ml") in
111 LibrarySync.add_object_declaration_hook
112 (fun ~add_obj ~add_coercion _ obj ->
113 output_string f (CicExportation.ppobj baseuri obj);
119 let eval_ast ~include_paths ?do_heavy_checks status (text,prefix_len,ast as ast') =
120 let baseuri = status#baseuri in
121 GrafiteEngine.eval_ast ~include_paths ?do_heavy_checks status ast'
124 let baseuri_of_script ~include_paths fname =
125 try Librarian.baseuri_of_script ~include_paths fname
127 Librarian.NoRootFor _ ->
128 HLog.error ("The included file '"^fname^"' has no root file,");
129 HLog.error "please create it.";
130 raise (Failure ("No root file for "^fname))
131 | Librarian.FileNotFound _ ->
132 raise (Failure ("File not found: "^fname))
135 let rec get_ast status ~compiling ~asserted ~include_paths strm =
136 match GrafiteParser.parse_statement status strm with
137 (GrafiteAst.Executable
138 (_,GrafiteAst.NCommand (_,GrafiteAst.Include (_,_,mafilename)))) as cmd
140 let already_included = NCicLibrary.get_transitively_included status in
142 assert_ng ~already_included ~compiling ~asserted ~include_paths
143 ?uid:status#user mafilename
146 | cmd -> asserted,cmd
148 and eval_from_stream ~compiling ~asserted ~include_paths ?do_heavy_checks status str cb =
149 let matita_debug = Helm_registry.get_bool "matita.debug" in
150 let rec loop asserted status =
151 let asserted,stop,status =
154 try Some (get_ast status ~compiling ~asserted ~include_paths str)
155 with End_of_file -> None in
157 | None -> asserted, true, status
158 | Some (asserted,ast) ->
159 (* pp_ast_statement status ast; *)
162 eval_ast ~include_paths ?do_heavy_checks status ("",0,ast)
164 asserted, false, status
165 with exn when not matita_debug ->
166 prerr_endline ("EnrichedWithStatus: " ^ Printexc.to_string exn);
167 raise (EnrichedWithStatus (exn, status))
169 if stop then asserted,status else loop asserted status
173 and compile ~compiling ~asserted ~include_paths ~outch ?uid fname =
174 if List.mem fname compiling then raise (CircularDependency fname);
175 let compiling = fname::compiling in
176 let matita_debug = Helm_registry.get_bool "matita.debug" in
177 let matita_debug = true in
178 let root,baseuri,fname,_tgt =
179 Librarian.baseuri_of_script ~include_paths fname in
180 if Http_getter_storage.is_read_only baseuri then assert false;
181 activate_extraction baseuri fname ;
182 (* MATITA 1.0: debbo fare time_travel sulla ng_library? *)
183 (* FIXME: currently hardcoded to single user mode *)
184 let status = new status uid baseuri in
185 let big_bang = Unix.gettimeofday () in
186 let { Unix.tms_utime = big_bang_u ; Unix.tms_stime = big_bang_s} =
189 let time = Unix.time () in
191 (* cleanup of previously compiled objects *)
192 if (not (Http_getter_storage.is_empty ~local:true baseuri))
194 HLog.message ("baseuri " ^ baseuri ^ " is not empty");
195 HLog.message ("cleaning baseuri " ^ baseuri);
196 LibraryClean.clean_baseuris [baseuri];
198 HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
199 if not (Helm_registry.get_bool "matita.verbose") then
201 let rex = Str.regexp ".*opt$" in
202 if Str.string_match rex Sys.argv.(0) 0 then "matitac.opt"
205 let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
206 print_string s; flush stdout);
207 (* we dalay this error check until we print 'matitac file ' *)
208 assert (Http_getter_storage.is_empty ~local:true baseuri);
209 (* create dir for XML files *)
210 if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
215 (Http_getter.filename ~local:true ~writable:true (baseuri ^
217 prerr_endline ("trying to compile " ^ fname);
219 GrafiteParser.parsable_statement status
220 (Ulexing.from_utf8_channel (open_in fname))
223 if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
224 else pp_ast_statement
226 let asserted, status =
227 eval_from_stream ~compiling ~asserted ~include_paths status buf print_cb in
228 prerr_endline ("end compile of " ^ fname);
229 let elapsed = Unix.time () -. time in
230 (if Helm_registry.get_bool "matita.moo" then begin
231 GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
234 let tm = Unix.gmtime elapsed in
235 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
237 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else ""
240 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
243 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
244 pp_times fname true big_bang big_bang_u big_bang_s;
245 prerr_endline ("now generating disambiguated script for " ^ fname);
246 let origbuf = Ulexing.from_utf8_channel (open_in fname) in
247 let interpr = GrafiteDisambiguate.get_interpr status#disambiguate_db in
248 let outstr = ref "" in
249 ignore (SmallLexer.mk_small_printer interpr outstr origbuf);
250 Printf.fprintf outch "%s" !outstr;
251 prerr_endline ("returning after compilation of " ^ fname);
253 (* MATITA 1.0: debbo fare time_travel sulla ng_library?
254 LexiconSync.time_travel
255 ~present:lexicon_status ~past:initial_lexicon_status;
258 (* all exceptions should be wrapped to allow lexicon-undo (LS.time_travel) *)
259 | exn when not matita_debug ->
260 (* MATITA 1.0: debbo fare time_travel sulla ng_library?
261 LexiconSync.time_travel ~present:lexicon ~past:initial_lexicon_status;
263 pp_times fname false big_bang big_bang_u big_bang_s;
264 clean_exit baseuri exn
266 and assert_ng ~already_included ~compiling ~asserted ~include_paths ?outch ?uid mapath =
267 let _,baseuri,fullmapath,_ = Librarian.baseuri_of_script ~include_paths mapath in
268 if List.mem fullmapath asserted then asserted,false
271 let baseuri = NUri.uri_of_string baseuri in
272 let ngtime_of baseuri =
273 let ngpath = NCicLibrary.ng_path_of_baseuri uid baseuri in
274 let uid = match uid with Some u -> "Some " ^ u | _ -> "None" in
275 prerr_endline ("uid = " ^ uid);
276 prerr_endline ("ngpath = " ^ ngpath);
278 Some (Unix.stat ngpath).Unix.st_mtime
279 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = ngpath -> None in
281 try (Unix.stat fullmapath).Unix.st_mtime
282 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = fullmapath -> assert false
284 let ngtime = ngtime_of baseuri in
285 let asserted,to_be_compiled =
288 let preamble = GrafiteTypes.Serializer.dependencies_of uid baseuri in
289 let asserted,children_bad =
291 (fun (asserted,b) mapath ->
293 assert_ng ~already_included ~compiling ~asserted ~include_paths
297 || let _,baseuri,_,_ =
298 Librarian.baseuri_of_script ~include_paths mapath in
299 let baseuri = NUri.uri_of_string baseuri in
300 (match ngtime_of baseuri with
301 Some child_ngtime -> child_ngtime > ngtime
302 | None -> assert false)
303 ) (asserted,false) preamble
305 asserted, children_bad || matime > ngtime
306 | None -> asserted,true
308 prerr_endline ("asserted = " ^ (String.concat "," asserted));
309 if not to_be_compiled then fullmapath::asserted,false
311 if List.mem baseuri already_included then
312 (* maybe recompiling it I would get the same... *)
313 raise (AlreadyLoaded (lazy mapath))
315 let outch, is_file_ch =
317 | None -> open_out (fullmapath ^ ".mad"), true
320 prerr_endline ("compiling " ^ fullmapath);
321 let asserted = compile ~compiling ~asserted ~include_paths ~outch ?uid fullmapath in
322 if is_file_ch then close_out outch;
323 fullmapath::asserted,true
328 let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" in
329 let rec is_there_only_comments status s =
330 if Pcre.pmatch ~rex:only_dust_RE s then raise End_of_file;
332 GrafiteParser.parsable_statement status
333 (Ulexing.from_utf8_string s) in
334 match GrafiteParser.parse_statement status strm with
335 | GrafiteAst.Comment (loc,_) ->
336 let _,parsed_text_length = HExtlib.utf8_parsed_text s loc in
337 (* CSC: why +1 in the following lines ???? *)
338 let parsed_text_length = parsed_text_length + 1 in
339 let remain_len = String.length s - parsed_text_length in
340 let next = String.sub s parsed_text_length remain_len in
341 is_there_only_comments status next
342 | GrafiteAst.Executable _ -> false
344 try is_there_only_comments status s
346 | NCicLibrary.IncludedFileNotCompiled _
347 | HExtlib.Localized _
348 | CicNotationParser.Parse_error _ -> false
349 | End_of_file -> true
350 | Invalid_argument "Array.make" -> false
354 let assert_ng ~include_paths ?outch mapath =
355 snd (assert_ng ~include_paths ~already_included:[] ~compiling:[] ~asserted:[]
357 let get_ast status ~include_paths strm =
358 snd (get_ast status ~compiling:[] ~asserted:[] ~include_paths strm)