]> matita.cs.unibo.it Git - helm.git/blob - matita/matitacLib.ml
one more step toward release and bench reorganization
[helm.git] / 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
33
34 let pp_ast_statement =
35   GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
36     ~lazy_term_pp:CicNotationPp.pp_term ~obj_pp:CicNotationPp.pp_obj
37
38 (** {2 Initialization} *)
39
40 let grafite_status = (ref None : GrafiteTypes.status option ref)
41 let lexicon_status = (ref None : LexiconEngine.status option ref)
42
43 let run_script is eval_function  =
44   let lexicon_status',grafite_status' = 
45     match !lexicon_status,!grafite_status with
46     | Some ss, Some s -> ss,s
47     | _,_ -> assert false
48   in
49   let slash_n_RE = Pcre.regexp "\\n" in
50   let cb = 
51     if Helm_registry.get_int "matita.verbosity" < 1 then 
52       (fun _ _ -> ())
53     else 
54       (fun grafite_status stm ->
55         (* dump_status grafite_status; *)
56         let stm = pp_ast_statement stm in
57         let stm = Pcre.replace ~rex:slash_n_RE stm in
58         let stm =
59           if String.length stm > 50 then
60             String.sub stm 0 50 ^ " ..."
61           else
62             stm
63         in
64         HLog.debug ("Executing: ``" ^ stm ^ "''"))
65   in
66   try
67    let grafite_status'', lexicon_status'' =
68     match eval_function lexicon_status' grafite_status' is cb with
69        [] -> assert false
70      | (s,None)::_ -> s
71      | (s,Some _)::_ -> raise AttemptToInsertAnAlias
72    in
73     lexicon_status := Some lexicon_status'';
74     grafite_status := Some grafite_status''
75   with
76   | GrafiteEngine.Drop  
77   | End_of_file
78   | CicNotationParser.Parse_error _ as exn -> raise exn
79   | exn -> 
80       HLog.error (snd (MatitaExcPp.to_string exn));
81       raise exn
82
83 let fname () =
84   match Helm_registry.get_list Helm_registry.string "matita.args" with
85   | [x] -> x
86   | l -> 
87       prerr_endline ("Wrong commands: " ^ String.concat " " l);
88       MatitaInit.die_usage ()
89
90 let pp_ocaml_mode () = 
91   HLog.message "";
92   HLog.message "                      ** Entering Ocaml mode ** ";
93   HLog.message "";
94   HLog.message "Type 'go ();;' to enter an interactive matitac";
95   HLog.message ""
96   
97 let clean_exit n =
98  let opt_exit =
99   function
100      None -> ()
101    | Some n -> exit n
102  in
103   match !grafite_status with
104      None -> opt_exit n
105    | Some grafite_status ->
106       try
107        let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
108        LibraryClean.clean_baseuris ~verbose:false [baseuri];
109        opt_exit n
110       with GrafiteTypes.Option_error("baseuri", "not found") ->
111        (* no baseuri ==> nothing to clean yet *)
112        opt_exit n
113   
114 let rec interactive_loop () = 
115   let str = Ulexing.from_utf8_channel stdin in
116   try
117     run_script str 
118       (MatitaEngine.eval_from_stream ~first_statement_only:false ~prompt:true
119       ~include_paths:(Helm_registry.get_list Helm_registry.string
120         "matita.includes"))
121   with 
122   | GrafiteEngine.Drop -> pp_ocaml_mode ()
123   | GrafiteEngine.Macro (floc,_) ->
124      let x, y = HExtlib.loc_of_floc floc in
125       HLog.error
126        (sprintf "A macro has been found in a script at %d-%d" x y);
127       interactive_loop ()
128   | Sys.Break -> HLog.error "user break!"; interactive_loop ()
129   | GrafiteTypes.Command_error _ -> interactive_loop ()
130   | End_of_file ->
131      print_newline ();
132      clean_exit (Some 0)
133   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
134      let x, y = HExtlib.loc_of_floc floc in
135       HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
136       interactive_loop ()
137   | exn -> HLog.error (Printexc.to_string exn); interactive_loop ()
138
139 let go () =
140   Helm_registry.load_from BuildTimeConf.matita_conf;
141   Http_getter.init ();
142   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
143   LibraryDb.create_owner_environment ();
144   CicEnvironment.set_trust (* environment trust *)
145     (let trust =
146       Helm_registry.get_opt_default Helm_registry.get_bool
147         ~default:true "matita.environment_trust" in
148      fun _ -> trust);
149   let include_paths =
150    Helm_registry.get_list Helm_registry.string "matita.includes" in
151   grafite_status := Some (GrafiteSync.init ());
152   lexicon_status :=
153    Some (CicNotation2.load_notation ~include_paths
154     BuildTimeConf.core_notation_script);
155   Sys.catch_break true;
156   interactive_loop ()
157
158 let pp_times fname bench_mode rc big_bang = 
159   if bench_mode then
160     begin
161       let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
162       let r = Unix.gettimeofday () -. big_bang in
163       let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
164       let cc = 
165         if Str.string_match (Str.regexp ".*opt$") Sys.argv.(0) 0 then 
166           "matitac.opt" 
167         else 
168           "matitac" 
169       in
170       let rc = if rc then "OK" else "FAIL" in
171       let times = 
172         let fmt t = 
173           let seconds = int_of_float t in
174           let cents = int_of_float ((t -. floor t) *. 100.0) in
175           let minutes = seconds / 60 in
176           let seconds = seconds mod 60 in
177           Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
178         in
179         Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
180       in
181       let fname = 
182         match MatitamakeLib.development_for_dir (Filename.dirname fname) with
183         | None -> fname
184         | Some d -> 
185             let rootlen = String.length(MatitamakeLib.root_for_development d)in
186             let fnamelen = String.length fname in
187             assert (fnamelen > rootlen); 
188             String.sub fname rootlen (fnamelen - rootlen)           
189       in
190       let s = 
191         Printf.sprintf "%s %-35s %-4s %s %s" cc fname rc times extra 
192       in
193       print_endline s;
194       flush stdout
195     end
196 ;;
197
198 let main ~mode = 
199   let big_bang = Unix.gettimeofday () in
200   MatitaInit.initialize_all ();
201   (* must be called after init since args are set by cmdline parsing *)
202   let fname = fname () in
203   let system_mode =  Helm_registry.get_bool "matita.system" in
204   let bench_mode =  Helm_registry.get_bool "matita.bench" in
205   if bench_mode then
206     Helm_registry.set_int "matita.verbosity" 0;
207   let include_paths =
208    Helm_registry.get_list Helm_registry.string "matita.includes" in
209   grafite_status := Some (GrafiteSync.init ());
210   lexicon_status :=
211    Some (CicNotation2.load_notation ~include_paths
212     BuildTimeConf.core_notation_script);
213   Sys.catch_break true;
214   let origcb = HLog.get_log_callback () in
215   let origcb t s = origcb t ((if system_mode then "[S] " else "") ^ s) in
216   let newcb tag s =
217     match tag with
218     | `Debug | `Message -> ()
219     | `Warning | `Error -> origcb tag s
220   in
221   if Helm_registry.get_int "matita.verbosity" < 1 then
222     HLog.set_log_callback newcb;
223   if bench_mode then MatitaMisc.shutup ();
224   let matita_debug = Helm_registry.get_bool "matita.debug" in
225   try
226     let time = Unix.time () in
227     if Helm_registry.get_int "matita.verbosity" < 1 && not bench_mode then
228       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
229     else
230       HLog.message (sprintf "execution of %s started:" fname);
231     let is =
232       Ulexing.from_utf8_channel
233         (match fname with
234         | "stdin" -> stdin
235         | fname -> open_in fname) in
236     let include_paths =
237      Helm_registry.get_list Helm_registry.string "matita.includes" in
238     (try
239       run_script is 
240        (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
241          ~clean_baseuri:(not (Helm_registry.get_bool "matita.preserve")))
242      with End_of_file -> ());
243     let elapsed = Unix.time () -. time in
244     let tm = Unix.gmtime elapsed in
245     let sec = string_of_int tm.Unix.tm_sec ^ "''" in
246     let min = 
247       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else "" 
248     in
249     let hou = 
250       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
251     in
252     let proof_status,moo_content_rev,metadata,lexicon_content_rev = 
253       match !lexicon_status,!grafite_status with
254       | Some ss, Some s ->
255          s.proof_status, s.moo_content_rev, ss.LexiconEngine.metadata,
256           ss.LexiconEngine.lexicon_content_rev
257       | _,_ -> assert false
258     in
259     if proof_status <> GrafiteTypes.No_proof then
260      begin
261       HLog.error
262        "there are still incomplete proofs at the end of the script";
263       pp_times fname bench_mode true big_bang;
264       clean_exit (Some 2)
265      end
266     else
267      begin
268        let baseuri =
269         DependenciesParser.baseuri_of_script ~include_paths fname in
270        let moo_fname = 
271          LibraryMisc.obj_file_of_baseuri ~baseuri ~writable:true 
272        in
273        let lexicon_fname= 
274          LibraryMisc.lexicon_file_of_baseuri ~baseuri ~writable:true 
275        in
276        let metadata_fname =
277         LibraryMisc.metadata_file_of_baseuri ~baseuri ~writable:true
278        in
279        GrafiteMarshal.save_moo moo_fname moo_content_rev;
280        LibraryNoDb.save_metadata metadata_fname metadata;
281        LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
282        HLog.message 
283          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
284        pp_times fname bench_mode true big_bang;
285        exit 0
286      end
287   with 
288   | Sys.Break ->
289       HLog.error "user break!";
290       pp_times fname bench_mode false big_bang;
291       if mode = `COMPILER then
292         clean_exit (Some ~-1)
293       else
294         pp_ocaml_mode ()
295   | GrafiteEngine.Drop ->
296       if mode = `COMPILER then 
297         begin
298           pp_times fname bench_mode false big_bang;
299           clean_exit (Some 1)
300         end
301       else 
302         pp_ocaml_mode ()
303   | GrafiteEngine.Macro (floc,_) ->
304      let x, y = HExtlib.loc_of_floc floc in
305       HLog.error
306        (sprintf "A macro has been found in a script at %d-%d" x y);
307       if mode = `COMPILER then 
308         begin
309           pp_times fname bench_mode false big_bang;
310           clean_exit (Some 1)
311         end
312       else 
313         pp_ocaml_mode ()
314   | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
315      let (x, y) = HExtlib.loc_of_floc floc in
316      HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
317      if mode = `COMPILER then
318        begin
319          pp_times fname bench_mode false big_bang;
320          clean_exit (Some 1)
321        end
322      else
323        pp_ocaml_mode ()
324   | exn ->
325       if matita_debug then raise exn;
326       if mode = `COMPILER then 
327         begin
328           pp_times fname bench_mode false big_bang;
329           clean_exit (Some 3)
330         end
331       else 
332         pp_ocaml_mode ()
333