]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/matitacLib.ml
- further simplifications (??) of the status dependencies
[helm.git] / matita / matita / matitacLib.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 open GrafiteTypes
31
32 exception AttemptToInsertAnAlias of GrafiteDisambiguate.status
33
34 let slash_n_RE = Pcre.regexp "\\n" ;;
35
36 let pp_ast_statement grafite_status stm =
37   let stm = GrafiteAstPp.pp_statement stm
38     ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
39   in
40   let stm = Pcre.replace ~rex:slash_n_RE stm in
41   let stm =
42       if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
43       else stm
44   in
45     HLog.debug ("Executing: ``" ^ stm ^ "''")
46 ;;
47
48 let clean_exit baseuri rc =
49   LibraryClean.clean_baseuris ~verbose:false [baseuri]; rc
50 ;;
51
52 let get_macro_context = function _ -> []
53 ;;
54    
55 let pp_times fname rc big_bang big_bang_u big_bang_s = 
56   if not (Helm_registry.get_bool "matita.verbose") then
57     let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
58     let r = Unix.gettimeofday () -. big_bang in
59     let u = u -. big_bang_u in
60     let s = s -. big_bang_s in
61     let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
62     let rc,rcascii = 
63       if rc then "\e[0;32mOK\e[0m","Ok" else "\e[0;31mFAIL\e[0m","Fail" in
64     let times = 
65       let fmt t = 
66         let seconds = int_of_float t in
67         let cents = int_of_float ((t -. floor t) *. 100.0) in
68         let minutes = seconds / 60 in
69         let seconds = seconds mod 60 in
70         Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
71       in
72       Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
73     in
74     let s = Printf.sprintf "%-4s %s %s" rc times extra in
75     print_endline s;
76     flush stdout;
77     HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
78 ;;
79
80 let cut prefix s = 
81   let lenp = String.length prefix in
82   let lens = String.length s in
83   assert (lens > lenp);
84   assert (String.sub s 0 lenp = prefix);
85   String.sub s lenp (lens-lenp)
86 ;;
87
88 let get_include_paths options =
89   let include_paths = 
90     try List.assoc "include_paths" options with Not_found -> "" 
91   in
92   let include_paths = Str.split (Str.regexp " ") include_paths in
93   let include_paths = 
94     include_paths @ 
95     Helm_registry.get_list Helm_registry.string "matita.includes" 
96   in
97     include_paths
98 ;;
99
100 let activate_extraction baseuri fname =
101   ()
102   (* MATITA 1.0
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
108   let f =
109     open_out
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);
114       flush f; []);
115       *)
116 ;;
117
118 let compile atstart options fname =
119   let matita_debug = Helm_registry.get_bool "matita.debug" in
120   let include_paths = get_include_paths options in
121   let root,baseuri,fname,_tgt = 
122     Librarian.baseuri_of_script ~include_paths fname in
123   if Http_getter_storage.is_read_only baseuri then assert false;
124   activate_extraction baseuri fname ;
125   let lexicon_status = new GrafiteDisambiguate.status in
126   atstart (); (* FG: do not invoke before loading the core notation script *)  
127   let grafite_status =
128    (new GrafiteTypes.status baseuri)#set_disambiguate_db lexicon_status#disambiguate_db in
129   let big_bang = Unix.gettimeofday () in
130   let { Unix.tms_utime = big_bang_u ; Unix.tms_stime = big_bang_s} = 
131     Unix.times () 
132   in
133   let time = Unix.time () in
134   try
135     (* cleanup of previously compiled objects *)
136     if (not (Http_getter_storage.is_empty ~local:true baseuri))
137       then begin
138       HLog.message ("baseuri " ^ baseuri ^ " is not empty");
139       HLog.message ("cleaning baseuri " ^ baseuri);
140       LibraryClean.clean_baseuris [baseuri];
141     end;
142     HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
143     if not (Helm_registry.get_bool "matita.verbose") then
144       (let cc = 
145         let rex = Str.regexp ".*opt$" in
146         if Str.string_match rex Sys.argv.(0) 0 then "matitac.opt"
147         else "matitac" 
148       in
149       let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
150       print_string s; flush stdout);
151     (* we dalay this error check until we print 'matitac file ' *)
152     assert (Http_getter_storage.is_empty ~local:true baseuri);
153     (* create dir for XML files *)
154     if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
155               ~default:false) 
156     then
157       HExtlib.mkdir 
158         (Filename.dirname 
159           (Http_getter.filename ~local:true ~writable:true (baseuri ^
160           "foo.con")));
161     let buf = Ulexing.from_utf8_channel (open_in fname) in
162     let print_cb =
163       if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
164       else pp_ast_statement
165     in
166     let grafite_status =
167      let rec aux_for_dump x grafite_status =
168       match
169        MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
170         grafite_status buf x
171       with
172       | [] -> grafite_status
173       | (g,None)::_ -> g
174       | (g,Some _)::_ ->
175          raise (AttemptToInsertAnAlias (g :> GrafiteDisambiguate.status))
176      in
177        aux_for_dump print_cb grafite_status
178     in
179     let elapsed = Unix.time () -. time in
180      (if Helm_registry.get_bool "matita.moo" then begin
181        GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
182         grafite_status#dump
183      end;
184      let tm = Unix.gmtime elapsed in
185      let sec = string_of_int tm.Unix.tm_sec ^ "''" in
186      let min = 
187        if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else "" 
188      in
189      let hou = 
190        if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
191      in
192      HLog.message 
193        (sprintf "execution of %s completed in %s." fname (hou^min^sec));
194      pp_times fname true big_bang big_bang_u big_bang_s;
195 (*
196      LexiconSync.time_travel 
197        ~present:lexicon_status ~past:initial_lexicon_status;
198 *)
199      true)
200   with 
201   (* all exceptions should be wrapped to allow lexicon-undo (LS.time_travel) *)
202   | AttemptToInsertAnAlias lexicon_status -> 
203      pp_times fname false big_bang big_bang_u big_bang_s;
204 (*
205      LexiconSync.time_travel 
206        ~present:lexicon_status ~past:initial_lexicon_status;
207 *)
208      clean_exit baseuri false
209   | MatitaEngine.EnrichedWithStatus (exn, _grafite) as exn' ->
210       (match exn with
211       | Sys.Break -> HLog.error "user break!"
212       | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
213           let (x, y) = HExtlib.loc_of_floc floc in
214           HLog.error (sprintf "Parse error at %d-%d: %s" x y err)
215       | exn when matita_debug -> raise exn'
216       | exn -> HLog.error (snd (MatitaExcPp.to_string exn))
217       );
218 (*       LexiconSync.time_travel ~present:lexicon ~past:initial_lexicon_status;
219  *       *)
220       pp_times fname false big_bang big_bang_u big_bang_s;
221       clean_exit baseuri false
222   | Sys.Break when not matita_debug ->
223      HLog.error "user break!";
224      pp_times fname false big_bang big_bang_u big_bang_s;
225      clean_exit baseuri false
226   | exn when not matita_debug ->
227        HLog.error 
228          ("Unwrapped exception, please fix: "^ snd (MatitaExcPp.to_string exn));
229        pp_times fname false big_bang big_bang_u big_bang_s;
230        clean_exit baseuri false
231
232 module F = 
233   struct 
234     type source_object = string
235     type target_object = string
236     let string_of_source_object s = s;;
237     let string_of_target_object s = s;;
238
239     let is_readonly_buri_of opts file = 
240      let buri = List.assoc "baseuri" opts in
241      Http_getter_storage.is_read_only (Librarian.mk_baseuri buri file)
242     ;;
243
244     let root_and_target_of opts mafile = 
245       try
246         let include_paths = get_include_paths opts in
247         let root,baseuri,_,relpath =
248           Librarian.baseuri_of_script ~include_paths mafile 
249         in
250         let obj_writeable, obj_read_only =
251            if Filename.check_suffix mafile ".mma" then 
252               Filename.chop_suffix mafile ".mma" ^ ".ma",
253               Filename.chop_suffix mafile ".mma" ^ ".ma"
254            else
255               LibraryMisc.obj_file_of_baseuri 
256                         ~must_exist:false ~baseuri ~writable:true,
257               LibraryMisc.obj_file_of_baseuri 
258                         ~must_exist:false ~baseuri ~writable:false
259         in
260         Some root, relpath, obj_writeable, obj_read_only
261       with Librarian.NoRootFor x -> None, "", "", ""
262     ;;
263
264     let mtime_of_source_object s =
265       try Some (Unix.stat s).Unix.st_mtime
266       with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
267     ;;
268
269     let mtime_of_target_object s =
270       try Some (Unix.stat s).Unix.st_mtime
271       with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = s -> None
272     ;;
273
274 (* FG: a problem was noticed in relising memory between subsequent *)
275 (*     invocations of the compiler. The following might help       *)
276     let compact r = Gc.compact (); r
277
278     let build options fname =
279       let matita_debug = Helm_registry.get_bool "matita.debug" in
280       let compile atstart opts fname =
281         try
282         (* MATITA 1.0: c'erano le push/pop per il parser; ma per
283          * l'environment nuovo? *)
284          compile atstart opts fname
285         with 
286         | Sys.Break -> false
287         | exn when not matita_debug ->
288             HLog.error ("Unexpected " ^ snd(MatitaExcPp.to_string exn));
289             assert false
290       in
291        compact (compile ignore options fname)
292     ;;
293
294     let load_deps_file = Librarian.load_deps_file;;
295
296   end 
297
298 module Make = Librarian.Make(F) 
299