1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 let logger = fun mark ->
32 | `Error -> HLog.error
33 | `Warning -> HLog.warn
34 | `Debug -> HLog.debug
35 | `Message -> HLog.message
38 { root: string ; name: string }
40 let developments = ref []
42 let pool () = Helm_registry.get "matita.basedir" ^ "/matitamake/" ;;
43 let rootfile = "/root" ;;
47 let d = Unix.opendir dir in
48 let content = ref [] in
51 let name = Unix.readdir d in
52 if name <> "." && name <> ".." then
53 content := name :: !content
56 with End_of_file -> Unix.closedir d; Some !content
57 with Unix.Unix_error _ -> None
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 ())
70 Some (HExtlib.input_file (pool () ^ name ^ rootfile))
71 with Unix.Unix_error _ ->
72 logger `Warning ("Malformed development " ^ name);
78 developments := {root = root ; name = name} :: !developments)
81 (* finds the makefile path for development devel *)
82 let makefile_for_development devel =
83 let develdir = pool () ^ devel.name in
84 develdir ^ "/makefile"
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
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
98 let pref = String.sub d2 0 len1 in
101 (* it must be unique *)
103 Some (List.find (fun d -> is_prefix_of d.root dir) !developments)
104 with Not_found -> None
106 let development_for_name name =
108 Some (List.find (fun d -> d.name = name) !developments)
109 with Not_found -> None
111 (* dumps the deveopment to disk *)
112 let dump_development devel =
113 let devel_dir = pool () ^ devel.name in
114 HExtlib.mkdir devel_dir;
115 HExtlib.output_file ~filename:(devel_dir ^ rootfile) ~text:devel.root
117 let list_known_developments () =
118 List.map (fun r -> r.name,r.root) !developments
120 let am_i_opt = lazy (
121 if Pcre.pmatch ~pat:"\\.opt$" Sys.argv.(0) then ".opt" else "")
123 let rebuild_makefile development =
124 let makefilepath = makefile_for_development development in
126 HExtlib.input_file BuildTimeConf.matitamake_makefile_template
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
140 let rebuild_makefile_devel development =
141 let path = development.root ^ "/makefile" in
142 if not (Sys.file_exists path) then
145 HExtlib.input_file BuildTimeConf.matitamake_makefile_template_devel
148 Pcre.replace ~pat:"@MATITA_RT_BASE_DIR@"
149 ~templ:BuildTimeConf.runtime_base_dir template
151 HExtlib.output_file ~filename:path ~text:template
154 (* creates a new development if possible *)
155 let initialize_development name dir =
156 let name = Pcre.replace ~pat:" " ~templ:"_" name in
157 let dev = {name = name ; root = dir} in
158 dump_development dev;
159 rebuild_makefile dev;
160 rebuild_makefile_devel dev;
161 developments := dev :: !developments;
164 let make chdir args =
165 let old = Unix.getcwd () in
168 let cmd = String.concat " " ("make" :: List.map Filename.quote args) in
169 let rc = Unix.system cmd in
172 | Unix.WEXITED 0 -> true
173 | Unix.WEXITED i -> logger `Error ("make returned " ^ string_of_int i);false
174 | _ -> logger `Error "make STOPPED or SIGNALED!";false
175 with Unix.Unix_error (_,cmd,err) ->
176 logger `Warning ("Unix Error: " ^ cmd ^ ": " ^ err);
179 let call_make ?matita_flags development target make =
181 let already_defined =
182 match matita_flags with
183 | None -> (try Sys.getenv "MATITA_FLAGS" with Not_found -> "")
187 if Helm_registry.get_bool "matita.bench" then "-bench" else ""
189 rebuild_makefile development;
190 let makefile = makefile_for_development development in
192 Helm_registry.get_opt_default Helm_registry.bool ~default:false "db.nodb"
195 let flags = flags @ if nodb then ["NODB=true"] else [] in
198 flags @ [ sprintf "MATITA_FLAGS=\"%s\"" matita_flags ]
199 with Not_found -> flags in
201 ["--no-print-directory"; "-s"; "-k"; "-f"; makefile; target] @ flags
203 (* prerr_endline (String.concat " " args); *)
204 make development.root args
206 let build_development ?matita_flags ?(target="all") development =
207 call_make ?matita_flags development target make
209 (* not really good vt100 *)
211 let rex = Pcre.regexp "
\e\\[[0-9;]+m" in
212 let rex_i = Pcre.regexp "^Info" in
213 let rex_w = Pcre.regexp "^Warning" in
214 let rex_e = Pcre.regexp "^Error" in
215 let rex_d = Pcre.regexp "^Debug" in
216 let rex_noendline = Pcre.regexp "\\n" in
217 let s = Pcre.replace ~rex:rex_noendline s in
218 let tokens = Pcre.split ~rex s in
219 let logger = ref HLog.message in
224 (if Pcre.pmatch ~rex:rex_i s then
225 logger := HLog.message
226 else if Pcre.pmatch ~rex:rex_w s then
228 else if Pcre.pmatch ~rex:rex_e s then
230 else if Pcre.pmatch ~rex:rex_d s then
239 let mk_maker refresh_cb =
241 let out_r,out_w = Unix.pipe () in
242 let err_r,err_w = Unix.pipe () in
244 ignore(Sys.signal Sys.sigchld (Sys.Signal_ignore));
246 (* prerr_endline (String.concat " " args); *)
247 let argv = Array.of_list ("make"::args) in
248 pid := Unix.create_process "make" argv Unix.stdin out_w err_w;
251 let buf = String.create 1024 in
252 let rec aux = function
254 let len = Unix.read f buf 0 1024 in
258 (Unix.EPIPE,"read","len = 0 (matita internal)"));
259 vt100 (String.sub buf 0 len);
264 let r,_,_ = Unix.select [out_r; err_r] [] [] (-. 1.) in
270 | Unix.Unix_error (_,"read",_)
271 | Unix.Unix_error (_,"select",_) -> true)
273 let build_development_in_bg ?matita_flags ?(target="all") refresh_cb development =
274 call_make ?matita_flags development target (mk_maker refresh_cb)
276 let clean_development ?matita_flags development =
277 call_make ?matita_flags development "clean" make
279 let clean_development_in_bg ?matita_flags refresh_cb development =
280 call_make development ?matita_flags "clean" (mk_maker refresh_cb)
282 let destroy_development_aux development clean_development =
283 let delete_development development =
284 let unlink = HExtlib.safe_remove in
288 with Unix.Unix_error _ ->
289 logger `Warning ("Unable to remove dir " ^ dir);
290 match ls_dir dir with
291 | None -> logger `Error ("Unable to list directory " ^ dir)
293 | Some l -> logger `Error ("The directory is not empty")
295 unlink (makefile_for_development development);
296 unlink (pool () ^ development.name ^ rootfile);
297 unlink (pool () ^ development.name ^ "/depend");
298 unlink (pool () ^ development.name ^ "/depend.errors");
299 rmdir (pool () ^ development.name);
301 List.filter (fun d -> d.name <> development.name) !developments
303 if not(clean_development development) then
305 logger `Warning "Unable to clean the development problerly.";
306 logger `Warning "This may cause garbage."
308 delete_development development
310 let destroy_development ?matita_flags development =
311 destroy_development_aux development (clean_development ?matita_flags)
313 let destroy_development_in_bg ?matita_flags refresh development =
314 destroy_development_aux development
315 (clean_development_in_bg refresh ?matita_flags )
317 let root_for_development development = development.root
318 let name_for_development development = development.name
320 let publish_development_bstract build clean devel =
321 let matita_flags = "\"-system\"" in
322 HLog.message "cleaning the development before publishing";
323 if clean ~matita_flags:"" devel then
325 HLog.message "rebuilding the development in 'system' space";
326 if build ~matita_flags devel then
328 HLog.message "publishing succeded";
333 HLog.error "building process failed, reverting";
334 if not (clean ~matita_flags devel) then
335 HLog.error "cleaning failed, end of the world (2)";
340 (HLog.error "unable to clean the development, publishing failed"; false)
342 let publish_development devel =
343 publish_development_bstract
344 (fun ~matita_flags devel -> build_development ~matita_flags devel)
345 (fun ~matita_flags devel -> clean_development ~matita_flags devel) devel
346 let publish_development_in_bg cb devel =
347 publish_development_bstract
348 (fun ~matita_flags devel -> build_development_in_bg cb ~matita_flags devel)
349 (fun ~matita_flags devel -> clean_development_in_bg cb ~matita_flags devel)