]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/matitadep.ml
urimanager removed
[helm.git] / matita / matita / matitadep.ml
1 (* Copyright (C) 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 module S = Set.Make (String)
29
30 module GA = GrafiteAst 
31 module HR = Helm_registry
32
33 let print_times msg = 
34    let times = Unix.times () in
35    let stamp = times.Unix.tms_utime +. times.Unix.tms_utime in
36    Printf.printf "TIME STAMP: %s: %f\n" msg stamp; flush stdout; stamp
37
38 let fix_name f =
39    let f = 
40       if Pcre.pmatch ~pat:"^\\./" f then String.sub f 2 (String.length f - 2)
41       else f
42    in 
43    HExtlib.normalize_path f
44
45 (* FG: old function left for reference *)
46 let exclude excluded_files files =
47    let map file = not (List.mem (fix_name file) excluded_files) in
48    List.filter map files
49
50 let generate_theory theory_file deps =
51    if theory_file = "" then deps else
52    let map (files, deps) (t, d) =
53       if t = theory_file then files, deps else
54       S.add t files, List.fold_left (fun deps dep -> S.add dep deps) deps d
55    in
56    let out_include och dep = 
57       Printf.fprintf och "include \"%s\".\n\n" dep  
58    in
59    let fileset, depset = List.fold_left map (S.empty, S.empty) deps in
60    let top_depset = S.diff fileset depset in
61    let och = open_out theory_file in
62    begin
63       MatitaMisc.out_preamble och;
64       S.iter (out_include och) top_depset; 
65       close_out och;
66       (theory_file, S.elements top_depset) :: deps
67    end
68
69 let main () =
70   let _ = print_times "inizio" in
71   let include_paths = ref [] in
72   let use_stdout = ref false in
73   let theory_file = ref "" in
74   (* all are maps from "file" to "something" *)
75   let include_deps = Hashtbl.create 13 in
76   let baseuri_of = Hashtbl.create 13 in
77   let baseuri_of_inv = Hashtbl.create 13 in
78   let dot_name = "depends" in 
79   let dot_file = ref "" in
80   let set_dot_file () = dot_file := dot_name^".dot" in
81   let set_theory_file s = theory_file := s ^ ".ma" in
82   (* helpers *)
83   let rec baseuri_of_script s = 
84      try Hashtbl.find baseuri_of s 
85      with Not_found -> 
86        let _,b,_,_ =  
87          Librarian.baseuri_of_script ~include_paths:!include_paths s 
88        in
89        Hashtbl.add baseuri_of s b; 
90        Hashtbl.add baseuri_of_inv b s; 
91        let _ =
92           if Filename.check_suffix s ".mma" then
93              let generated = Filename.chop_suffix s ".mma" ^ ".ma" in
94              ignore (baseuri_of_script generated)
95        in
96        b
97   in
98   let script_of_baseuri ma b =
99     try Some (Hashtbl.find baseuri_of_inv b)
100     with Not_found -> 
101       HLog.error ("Skipping dependency of '"^ma^"' over '"^b^"'");
102       HLog.error ("Please include the file defining such baseuri, or fix");
103       HLog.error ("possibly incorrect verbatim URIs in the .ma file.");
104       None
105   in
106   let buri alias = NUri.baseuri_of_uri (NUri.uri_of_string alias) in
107   let resolve alias current_buri =
108     let buri = buri alias in
109     if buri <> current_buri then Some buri else None 
110   in
111   (* initialization *)
112   MatitaInit.add_cmdline_spec 
113     ["-dot", Arg.Unit set_dot_file,
114         "Save dependency graph in dot format and generate a png";
115      "-stdout", Arg.Set use_stdout,
116         "Print dependences on stdout";
117      "-theory <name>", Arg.String set_theory_file,
118         "generate a theory file <name>.ma (it includes all other files)"
119     ];
120   MatitaInit.parse_cmdline_and_configuration_file ();
121   MatitaInit.initialize_environment ();
122   if not (Helm_registry.get_bool "matita.verbose") then MatitaMisc.shutup ();
123   let cmdline_args = HR.get_list HR.string "matita.args" in
124   let test x =
125      Filename.check_suffix x ".ma" || Filename.check_suffix x ".mma"
126   in
127   let files = fun () -> match cmdline_args with
128      | [] -> HExtlib.find ~test "."
129      | _  -> cmdline_args
130   in
131   let args = 
132       let roots = Librarian.find_roots_in_dir (Sys.getcwd ()) in
133       match roots with
134       | [] -> 
135          prerr_endline ("No roots found in " ^ Sys.getcwd ());
136          exit 1
137       | [x] -> 
138          Sys.chdir (Filename.dirname x);
139          let opts = Librarian.load_root_file "root" in
140          include_paths := 
141            (try Str.split (Str.regexp " ") (List.assoc "include_paths" opts)
142            with Not_found -> []) @ 
143            (HR.get_list HR.string "matita.includes");
144          files ()
145       | _ ->
146          let roots = List.map (HExtlib.chop_prefix (Sys.getcwd()^"/")) roots in
147          prerr_endline ("Too many roots found:\n\t"^String.concat "\n\t" roots);
148          prerr_endline ("\nEnter one of these directories and retry");
149          exit 1
150   in
151   let ma_files = args in
152   (* here we go *)
153   (* fills:
154               Hashtbl.add include_deps     ma_file ma_file
155               Hashtbl.add include_deps_dot ma_file baseuri
156   *)
157   let _ = print_times "prima di iter1" in 
158   List.iter (fun ma_file -> ignore (baseuri_of_script ma_file)) ma_files;
159   let _ = print_times "in mezzo alle due iter" in 
160   let map s _ l = s :: l in
161   let ma_files = Hashtbl.fold map baseuri_of [] in
162   List.iter
163    (fun ma_file -> 
164       let _ = if Filename.check_suffix ma_file ".mma" then
165          let generated = Filename.chop_suffix ma_file ".mma" ^ ".ma" in
166          Hashtbl.add include_deps generated ma_file;
167       in
168       let ma_baseuri = baseuri_of_script ma_file in
169       let dependencies = 
170   let _ = print_times "prima deps_of_iter" in 
171          try DependenciesParser.deps_of_file ma_file
172          with Sys_error _ -> []
173       in
174   let _ = print_times "dopo deps_of_iter" in 
175       let handle_uri uri =
176          if not (Http_getter_storage.is_legacy uri) then
177          let dep = resolve uri ma_baseuri in
178          match dep with 
179             | None   -> ()
180             | Some u -> 
181                  match script_of_baseuri ma_file u with
182                       | Some d -> Hashtbl.add include_deps ma_file d
183                       | None   -> ()
184       in
185       let handle_script path =
186          ignore (baseuri_of_script path);
187          Hashtbl.add include_deps ma_file path
188       in
189       List.iter 
190        (function
191          | DependenciesParser.UriDep uri      ->
192             let uri = NUri.string_of_uri uri in
193             handle_uri uri 
194          | DependenciesParser.InlineDep path  ->
195             if Librarian.is_uri path
196             then handle_uri path else handle_script path
197          | DependenciesParser.IncludeDep path ->
198             handle_script path) 
199        dependencies)
200    ma_files;
201   (* generate regular depend output *)
202   let _ = print_times "dopo di iter2" in 
203   let deps =
204     List.fold_left
205      (fun acc ma_file -> 
206       let deps = Hashtbl.find_all include_deps ma_file in
207       let deps = List.fast_sort Pervasives.compare deps in
208       let deps = HExtlib.list_uniq deps in
209       let deps = List.map fix_name deps in
210       (fix_name ma_file, deps) :: acc)
211      [] ma_files
212   in
213   let extern = 
214     List.fold_left
215       (fun acc (_,d) -> 
216         List.fold_left 
217           (fun a x -> 
218              if List.exists (fun (t,_) -> x=t) deps then a 
219              else x::a) 
220           acc d)
221       [] deps
222   in
223   let where = if !use_stdout then None else Some (Sys.getcwd()) in
224   let all_deps = 
225      deps @ 
226      HExtlib.list_uniq (List.sort Pervasives.compare (List.map (fun x -> x,[]) extern))
227   in  
228   (* theory generation *)
229   let all_deps_and_theory = generate_theory !theory_file all_deps in 
230   (* matita depend file generation *)
231   Librarian.write_deps_file where all_deps_and_theory;
232   (* dot generation *)
233   if !dot_file <> "" then
234     begin
235       let oc = open_out !dot_file in
236       let fmt = Format.formatter_of_out_channel oc in 
237       GraphvizPp.Dot.header fmt;
238       List.iter
239        (fun (ma_file,deps) -> 
240         GraphvizPp.Dot.node ma_file fmt;
241         List.iter (fun dep -> GraphvizPp.Dot.edge ma_file dep fmt) deps)
242        deps;
243       List.iter 
244         (fun x -> GraphvizPp.Dot.node ~attrs:["style","dashed"] x fmt) 
245         extern; 
246       GraphvizPp.Dot.trailer fmt;
247       close_out oc;
248       ignore(Sys.command ("tred "^ !dot_file^"| dot -Tpng -o"^dot_name^".png"));
249       HLog.message ("Type 'eog "^dot_name^".png' to view the graph"); 
250     end;
251     let _ = print_times "fine" in ()