]> matita.cs.unibo.it Git - helm.git/blob - matita/matitadep.ml
get rid of gragrep, matitamake(Lib) and development windows,
[helm.git] / 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                 
33 let main () =
34   (* all are maps from "file" to "something" *)
35   let include_deps = Hashtbl.create 13 in
36   let baseuri_of = Hashtbl.create 13 in
37   let baseuri_of_inv = Hashtbl.create 13 in
38   let dot_file = ref "" in
39   (* helpers *)
40   let include_paths = 
41     Helm_registry.get_list Helm_registry.string "matita.includes" 
42   in
43   let baseuri_of_script s = 
44      try Hashtbl.find baseuri_of s 
45      with Not_found -> 
46        let _,b,_ = Librarian.baseuri_of_script ~include_paths s in
47        Hashtbl.add baseuri_of s b; 
48        Hashtbl.add baseuri_of_inv b s; 
49        b
50   in
51   let script_of_baseuri b =
52     try Hashtbl.find baseuri_of_inv b
53     with Not_found -> 
54      assert false 
55     (* should be called only after baseuri_of_script is
56      * called on every file *)
57   in
58   let buri alias = U.buri_of_uri (U.uri_of_string alias) in
59   let resolve alias current_buri =
60     let buri = buri alias in
61     if buri <> current_buri then Some buri else None 
62   in
63   (* initialization *)
64   MatitaInit.add_cmdline_spec 
65     ["-dot", Arg.Set_string dot_file,
66     "<file> Save dependency graph in dot format to the given file";];
67   MatitaInit.parse_cmdline_and_configuration_file ();
68   MatitaInit.initialize_environment ();
69   let args = Helm_registry.get_list Helm_registry.string "matita.args" in
70   let args = 
71     if args = [] then
72       let roots = Librarian.find_roots_in_dir (Sys.getcwd ()) in
73       match roots with
74       | [] -> 
75          prerr_endline ("No roots found in " ^ Sys.getcwd ());
76          exit 1
77       | [x] -> 
78          Sys.chdir (Filename.dirname x);
79          HExtlib.find ~test:(fun x -> Filename.check_suffix x ".ma") "."
80       | _ ->
81          prerr_endline ("Too many roots: " ^ String.concat ", " roots);
82          prerr_endline ("Enter one of these directories and retry");
83          exit 1
84     else
85       args
86   in
87   let ma_files = args in
88   (* here we go *)
89   (* fills:
90               Hashtbl.add include_deps     ma_file ma_file
91               Hashtbl.add include_deps_dot ma_file baseuri
92   *)
93   List.iter (fun ma_file -> ignore (baseuri_of_script ma_file)) ma_files;
94   List.iter
95    (fun ma_file -> 
96       let ma_baseuri = baseuri_of_script ma_file in
97       let dependencies = DependenciesParser.deps_of_file ma_file in
98       List.iter 
99        (function
100          | DependenciesParser.UriDep uri -> 
101             let uri = UriManager.string_of_uri uri in
102             if not (Http_getter_storage.is_legacy uri) then
103               let dep = resolve uri ma_baseuri in
104               (match dep with 
105               | None -> ()
106               | Some u -> Hashtbl.add include_deps ma_file (script_of_baseuri u))
107          | DependenciesParser.IncludeDep path -> 
108                 Hashtbl.add include_deps ma_file path)
109        dependencies)
110    ma_files;
111   (* dot generation *)
112   if !dot_file <> "" then 
113     begin
114       let oc = open_out !dot_file in
115       let fmt = Format.formatter_of_out_channel oc in 
116       GraphvizPp.Dot.header fmt;
117       List.iter
118        (fun ma_file -> 
119         let deps = Hashtbl.find_all include_deps ma_file in
120         let deps = 
121           HExtlib.filter_map 
122             (fun u -> 
123               try Some (Hashtbl.find baseuri_of_inv u) 
124               with Not_found -> None) 
125             deps 
126         in
127         let deps = List.fast_sort Pervasives.compare deps in
128         let deps = HExtlib.list_uniq deps in
129         GraphvizPp.Dot.node ma_file fmt;
130         List.iter (fun dep -> GraphvizPp.Dot.edge ma_file dep fmt) deps)
131        ma_files;
132       GraphvizPp.Dot.trailer fmt;
133       close_out oc
134     end;
135   (* generate regular depend output *)
136   let oc = open_out "depends" in
137   List.iter 
138    (fun ma_file -> 
139       let deps = Hashtbl.find_all include_deps ma_file in
140       let deps = List.fast_sort Pervasives.compare deps in
141       let deps = HExtlib.list_uniq deps in
142       let deps = ma_file :: deps in
143       let deps = 
144         List.map (fun f ->
145                 let f = 
146                   if Pcre.pmatch ~pat:"^\\./" f then
147                     String.sub f 2 (String.length f - 2)
148                   else 
149                     f
150                 in HExtlib.normalize_path f) deps 
151       in
152       output_string oc (String.concat " " deps ^ "\n"))
153    ma_files;
154   close_out oc;
155   HLog.message ("Generated " ^ Sys.getcwd () ^ "/depends")
156