]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitadep.ml
ported to the new parser interface (Ulexing.lexbuf instead of char Stream.t)
[helm.git] / helm / 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 let paths_to_search_in = ref [];;
27
28 let add_l l = fun s -> l := s :: !l ;;
29
30 let arg_spec = [
31   "-I", Arg.String (add_l paths_to_search_in), 
32     "<path> Adds path to the list of searched paths for the include command";
33 ]
34 let usage =
35   Printf.sprintf "MatitaDep v%s\nUsage: matitadep [options] file...\nOptions:"
36     BuildTimeConf.version
37
38 module GA = GrafiteAst 
39 module U = UriManager
40
41 (* all are maps from "file" to "something" *)
42 let include_deps = Hashtbl.create (Array.length Sys.argv)
43 let baseuri_of = Hashtbl.create (Array.length Sys.argv)
44 let uri_deps = Hashtbl.create (Array.length Sys.argv)
45
46 let buri alias = 
47   U.buri_of_uri (U.uri_of_string alias)
48
49 let resolve alias current_buri=
50  let buri = buri alias in
51   if buri <> current_buri then Some buri else None
52  
53 let find path = 
54   let rec aux = function
55   | [] -> close_in (open_in path); path
56   | p :: tl ->
57       try
58         close_in (open_in (p ^ "/" ^ path)); p ^ "/" ^ path
59       with Sys_error _ -> aux tl
60   in
61   let paths = !paths_to_search_in in
62   try
63     aux paths
64   with Sys_error _ as exc ->
65     MatitaLog.error ("Unable to read " ^ path);
66     MatitaLog.error ("opts.include_paths was " ^ String.concat ":" paths);
67     raise exc
68 ;;
69
70 let main () =
71   MatitaInit.load_config_only ();
72   let files = ref [] in
73   Arg.parse arg_spec (add_l files) usage;
74   List.iter
75    (fun file -> 
76     let ic = open_in file in
77     let istream = Ulexing.from_utf8_channel ic in
78     let dependencies = GrafiteParser.parse_dependencies istream in
79     close_in ic;
80     List.iter 
81       (function
82       | GrafiteAst.UriDep uri -> 
83           let uri = UriManager.string_of_uri uri in
84           Hashtbl.add uri_deps file uri
85       | GrafiteAst.BaseuriDep uri -> 
86           let uri = MatitaMisc.strip_trailing_slash uri in
87           Hashtbl.add baseuri_of file uri
88       | GrafiteAst.IncludeDep path -> 
89           try 
90             let ma_file = if path <> "coq.ma" then find path else path in
91             let moo_file = MatitaMisc.obj_file_of_script ma_file in
92             Hashtbl.add include_deps file moo_file
93           with Sys_error _ -> 
94             MatitaLog.warn 
95               ("Unable to find " ^ path ^ " that is included in " ^ file))
96     dependencies)
97   !files;
98   Hashtbl.iter 
99     (fun file alias -> 
100       let dep = resolve alias (Hashtbl.find baseuri_of file) in
101       match dep with 
102       | None -> ()
103       | Some u -> 
104           Hashtbl.add include_deps file (MatitaMisc.obj_file_of_baseuri u))
105   uri_deps;
106   List.iter
107    (fun file -> 
108     let deps = Hashtbl.find_all include_deps file in
109     let deps = List.fast_sort Pervasives.compare deps in
110     let deps = MatitaMisc.list_uniq deps in
111     let deps = file :: deps in
112     let moo = MatitaMisc.obj_file_of_script file in
113      Printf.printf "%s: %s\n" moo (String.concat " " deps);
114      Printf.printf "%s: %s\n" (Pcre.replace ~pat:"ma$" ~templ:"mo" file) moo)
115   !files
116 ;;
117   
118 let _ =
119   main ()