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