]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitadep.ml
matitamake stuff:
[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 TA = TacticAst 
39 module U = UriManager
40
41 let deps = Hashtbl.create (Array.length Sys.argv)
42 let baseuri = ref []
43 let aliases = Hashtbl.create (Array.length Sys.argv)
44
45 (*
46 let uri_of_alias = function 
47   | Ident_alias (_, uri)
48   | Symbol_alias (_, _, uri) 
49   | Number_alias (_, uri) -> uri 
50 *)
51
52 let buri alias = 
53   U.buri_of_uri (U.uri_of_string alias)
54
55 let resolve alias =
56   try
57     Some (snd(List.find (fun (u, f) -> u = (buri alias)) !baseuri))
58   with
59   | Not_found -> None
60  
61   (*** TODO MANCANO LE URI VERBATIM DENTRO GLI AST DEI TERMINI ****)
62
63 let find path = 
64   let rec aux = function
65   | [] -> close_in (open_in path); path
66   | p :: tl ->
67       try
68         close_in (open_in (p ^ "/" ^ path)); p ^ "/" ^ path
69       with Sys_error _ -> aux tl
70   in
71   let paths = !paths_to_search_in in
72   try
73     aux paths
74   with Sys_error _ as exc ->
75     MatitaLog.error ("Unable to read " ^ path);
76     MatitaLog.error ("opts.include_paths was " ^ String.concat ":" paths);
77     raise exc
78 ;;
79
80 let main () =
81   let files = ref [] in
82   Arg.parse arg_spec (add_l files) usage;
83   List.iter (fun file -> 
84     let ic = open_in file in
85     let stms =
86       try
87         CicTextualParser2.parse_statements (Stream.of_channel ic)
88       with
89         (CicTextualParser2.Parse_error _) as exc ->
90           prerr_endline ("Unable to parse: " ^ file);
91           prerr_endline (MatitaExcPp.to_string exc);
92           exit 1
93     in
94     close_in ic;
95     List.iter 
96       (function
97       | TA.Executable (_, TA.Command (_, TA.Set (_, "baseuri", uri))) ->
98           let uri = MatitaMisc.strip_trailing_slash uri in
99           baseuri := (uri, file) :: !baseuri
100       | TA.Executable (_, TA.Command 
101           (_, TA.Alias (_, TA.Ident_alias(_, uri)))) -> 
102             Hashtbl.add aliases file uri
103       | TA.Executable (_, TA.Command (_, TA.Include (_, path))) ->
104             Hashtbl.add deps file (find path)
105       | _ -> ()) 
106       stms; 
107     Hashtbl.iter 
108       (fun file alias -> 
109         let dep = resolve alias in
110         match dep with 
111         | None -> ()
112         | Some d -> Hashtbl.add deps file d)
113       aliases;)
114   !files;
115   List.iter (fun file -> 
116     let deps = Hashtbl.find_all deps file in
117     let deps = List.fast_sort Pervasives.compare deps in
118     let deps = MatitaMisc.list_uniq deps in
119     let deps = 
120       List.map (fun x -> Pcre.replace ~pat:"ma$" ~templ:"moo" x) deps 
121     in
122     let deps = file :: deps in
123     Printf.printf "%s: %s\n" (MatitaMisc.obj_file_of_script file)
124       (String.concat " " deps))
125     !files
126 ;;
127   
128
129 let _ = main ()