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