]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitamakeLib.ml
better handling of backgroud compiler process
[helm.git] / helm / matita / matitamakeLib.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 logger = fun mark -> 
27   match mark with 
28   | `Error -> MatitaLog.error
29   | `Warning -> MatitaLog.warn
30   | `Debug -> MatitaLog.debug
31   | `Message -> MatitaLog.message
32 ;;
33
34 type development = 
35   { root: string ; name: string }
36
37 let developments = ref []
38   
39 let pool () = Helm_registry.get "matita.basedir" ^ "/matitamake/" ;;
40 let rootfile = "/root" ;;
41
42 let ls_dir dir = 
43   try
44     let d = Unix.opendir dir in
45     let content = ref [] in
46     try
47       while true do
48         let name = Unix.readdir d in
49         if name <> "." && name <> ".." then
50           content := name :: !content
51       done;
52       Some []
53     with End_of_file -> Unix.closedir d; Some !content
54   with Unix.Unix_error _ -> None
55
56 let initialize () = 
57   (* create a base env if none *)
58   MatitaMisc.mkdir (pool ());
59   (* load developments *)
60   match ls_dir (pool ()) with
61   | None -> logger `Error ("Unable to list directory " ^ pool ()) 
62   | Some l -> 
63       List.iter 
64         (fun name -> 
65           let root = 
66             try 
67               Some (MatitaMisc.input_file (pool () ^ name ^ rootfile))
68             with Unix.Unix_error _ -> 
69               logger `Warning ("Malformed development " ^ name);
70               None
71           in 
72           match root with 
73           | None -> ()
74           | Some root -> 
75               developments := {root = root ; name = name} :: !developments) 
76       l
77
78 (* finds the makefile path for development devel *)
79 let makefile_for_development devel =
80   let develdir = pool () ^ devel.name in
81   develdir ^ "/makefile"
82 ;;
83
84 (* given a dir finds a development that is radicated in it or below *)
85 let development_for_dir dir =
86   let is_prefix_of d1 d2 =
87     let len1 = String.length d1 in
88     let len2 = String.length d2 in
89     if len2 < len1 then 
90       false
91     else
92       let pref = String.sub d2 0 len1 in
93       pref = d1
94   in
95   (* it must be unique *)
96   try
97     Some (List.find (fun d -> is_prefix_of d.root dir) !developments)
98   with Not_found -> None
99 ;;
100
101 let development_for_name name =
102   try 
103     Some (List.find (fun d -> d.name = name) !developments)
104   with Not_found -> None
105
106 (* dumps the deveopment to disk *)
107 let dump_development devel =
108   let devel_dir = pool () ^ devel.name in 
109   MatitaMisc.mkdir devel_dir;
110   MatitaMisc.output_file devel.root (devel_dir ^ rootfile);
111 ;;
112
113 let list_known_developments () = 
114   List.map (fun r -> r.name,r.root) !developments
115
116 let am_i_opt () = 
117   if Pcre.pmatch ~pat:"\\.opt$" Sys.argv.(0) then ".opt" else ""
118   
119 let rebuild_makefile development = 
120   let makefilepath = makefile_for_development development in
121   let template = 
122     MatitaMisc.input_file BuildTimeConf.matitamake_makefile_template 
123   in
124   let cc = BuildTimeConf.runtime_base_dir ^ "/matitac" ^ am_i_opt () in
125   let rm = BuildTimeConf.runtime_base_dir ^ "/matitaclean" ^ am_i_opt () in
126   let mm = BuildTimeConf.runtime_base_dir ^ "/matitadep" ^ am_i_opt () in
127   let df = pool () ^ development.name ^ "/depend" in
128   let dfs = pool () ^ development.name ^ "/depend.short" in
129   let template = Pcre.replace ~pat:"@ROOT@" ~templ:development.root template in
130   let template = Pcre.replace ~pat:"@CC@" ~templ:cc template in
131   let template = Pcre.replace ~pat:"@DEP@" ~templ:mm template in
132   let template = Pcre.replace ~pat:"@DEPFILE@" ~templ:df template in
133   let template = Pcre.replace ~pat:"@DEPFILESHORT@" ~templ:dfs template in
134   let template = Pcre.replace ~pat:"@CLEAN@" ~templ:rm template in
135   MatitaMisc.output_file template makefilepath
136   
137 (* creates a new development if possible *)
138 let initialize_development name dir =
139   let dev = {name = name ; root = dir} in
140   match development_for_dir dir with
141   | Some d ->
142       logger `Error 
143         ("Directory " ^ dir ^ " is already handled by development " ^ d.name);
144       logger `Error
145         ("Development " ^ d.name ^ " is rooted in " ^ d.root); 
146       logger `Error
147         (dir ^ " is a subdir of " ^ d.root);
148       None
149   | None -> 
150       dump_development dev;
151       rebuild_makefile dev;
152       developments := dev :: !developments;
153       Some dev
154
155 let make chdir args = 
156   let old = Unix.getcwd () in
157   try
158     Unix.chdir chdir;
159     let rc = Unix.system (String.concat " " ("make"::args)) in
160     Unix.chdir old;
161     match rc with
162     | Unix.WEXITED 0 -> true
163     | Unix.WEXITED i -> logger `Error ("make returned " ^ string_of_int i);false
164     | _ -> logger `Error "make STOPPED or SIGNALED!";false
165   with Unix.Unix_error (_,cmd,err) -> 
166     logger `Warning ("Unix Error: " ^ cmd ^ ": " ^ err);
167     false
168       
169 let call_make development target make =
170   rebuild_makefile development;
171   let makefile = makefile_for_development development in
172   make development.root 
173     ["--no-print-directory"; "-s"; "-k"; "-f"; makefile; target]
174       
175 let build_development ?(target="all") development =
176   call_make development target make
177
178 (* not really good vt100 *)
179 let vt100 s =
180   let rex = Pcre.regexp "\e\\[[0-9;]+m" in
181   let rex_i = Pcre.regexp "^Info" in
182   let rex_w = Pcre.regexp "^Warning" in
183   let rex_e = Pcre.regexp "^Error" in
184   let rex_d = Pcre.regexp "^Debug" in
185   let rex_noendline = Pcre.regexp "\\n" in
186   let s = Pcre.replace ~rex:rex_noendline s in
187   let len = String.length s in
188   let tokens = Pcre.split ~rex s in
189   let logger = ref MatitaLog.message in
190   let rec aux = 
191     function
192     | [] -> ()
193     | s::tl ->
194         (if Pcre.pmatch ~rex:rex_i s then
195           logger := MatitaLog.message
196         else if Pcre.pmatch ~rex:rex_w s then
197           logger := MatitaLog.warn
198         else if Pcre.pmatch ~rex:rex_e s then
199           logger := MatitaLog.error
200         else if Pcre.pmatch ~rex:rex_d s then
201           logger := MatitaLog.debug
202         else 
203           !logger s);
204         aux tl
205   in
206   aux tokens
207   
208
209 let mk_maker refresh_cb =
210   (fun chdir args ->
211     let out_r,out_w = Unix.pipe () in
212     let err_r,err_w = Unix.pipe () in
213     let pid = ref ~-1 in
214     ignore(Sys.signal Sys.sigchld (Sys.Signal_ignore));
215     try 
216       let argv = Array.of_list ("make"::args) in
217       pid := Unix.create_process "make" argv Unix.stdin out_w err_w;
218       Unix.close out_w;
219       Unix.close err_w;
220       let buf = String.create 1024 in
221       let rec aux = function 
222         | f::tl ->
223             let len = Unix.read f buf 0 1024 in
224             if len = 0 then 
225               raise 
226                 (Unix.Unix_error 
227                   (Unix.EPIPE,"read","len = 0 (matita internal)"));
228             vt100 (String.sub buf 0 len);
229             aux tl
230         | _ -> ()
231       in
232       while true do
233         let r,_,_ = Unix.select [out_r; err_r] [] [] (-. 1.) in
234         aux r;
235         refresh_cb ()
236       done;
237       true
238     with 
239     | Unix.Unix_error (_,"read",_)
240     | Unix.Unix_error (_,"select",_) -> true)
241
242 let build_development_in_bg ?(target="all") refresh_cb development =
243   call_make development target (mk_maker refresh_cb)
244 ;;
245
246 let clean_development development =
247   call_make development "clean" make
248
249 let clean_development_in_bg refresh_cb development =
250   call_make development "clean" (mk_maker refresh_cb)
251
252 let destroy_development development =
253   let delete_development development = 
254     let unlink file =
255       try 
256         Unix.unlink file 
257       with Unix.Unix_error _ -> logger `Debug ("Unable to delete " ^ file)
258     in
259     let rmdir dir =
260       try
261         Unix.rmdir dir 
262       with Unix.Unix_error _ -> 
263         logger `Warning ("Unable to remove dir " ^ dir);
264         match ls_dir dir with
265         | None -> logger `Error ("Unable to list directory " ^ dir) 
266         | Some [] -> ()
267         | Some l -> logger `Error ("The directory is not empty")
268     in
269     unlink (makefile_for_development development);
270     unlink (pool () ^ development.name ^ rootfile);
271     unlink (pool () ^ development.name ^ "/depend");
272     unlink (pool () ^ development.name ^ "/depend.short");
273     rmdir (pool () ^ development.name);
274     developments := 
275       List.filter (fun d -> d.name <> development.name) !developments
276   in
277   if not(clean_development development) then
278     begin
279       logger `Warning "Unable to clean the development problerly.";
280       logger `Warning "This may cause garbage."
281     end;
282   delete_development development 
283   
284 let root_for_development development = development.root
285 let name_for_development development = development.name
286