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