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