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
39 { root: string ; name: string }
41 let developments = ref []
43 let pool () = Helm_registry.get "matita.basedir" ^ "/matitamake/" ;;
44 let rootfile = "/root" ;;
48 let d = Unix.opendir dir in
49 let content = ref [] in
52 let name = Unix.readdir d in
53 if name <> "." && name <> ".." then
54 content := name :: !content
57 with End_of_file -> Unix.closedir d; Some !content
58 with Unix.Unix_error _ -> None
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 ())
71 Some (HExtlib.input_file (pool () ^ name ^ rootfile))
72 with Unix.Unix_error _ ->
73 logger `Warning ("Malformed development " ^ name);
79 developments := {root = root ; name = name} :: !developments)
82 (* finds the makefile path for development devel *)
83 let makefile_for_development devel =
84 let develdir = pool () ^ devel.name in
85 develdir ^ "/makefile"
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
96 let pref = String.sub d2 0 len1 in
99 (* it must be unique *)
101 Some (List.find (fun d -> is_prefix_of d.root dir) !developments)
102 with Not_found -> None
105 let development_for_name name =
107 Some (List.find (fun d -> d.name = name) !developments)
108 with Not_found -> None
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
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 (* 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
146 if dir <> dev.root then begin
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);
153 end else (* requirement development alreay exists, do nothing *)
156 dump_development dev;
157 rebuild_makefile dev;
158 developments := dev :: !developments;
161 let make chdir args =
162 let old = Unix.getcwd () in
165 let cmd = String.concat " " ("make" :: List.map Filename.quote args) in
166 let rc = Unix.system cmd in
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);
176 let call_make ?matita_flags development target make =
178 let already_defined =
179 match matita_flags with
180 | None -> (try Sys.getenv "MATITA_FLAGS" with Not_found -> "")
184 if Helm_registry.get_bool "matita.bench" then "-bench" else ""
186 rebuild_makefile development;
187 let makefile = makefile_for_development development in
189 Helm_registry.get_opt_default Helm_registry.bool ~default:false "db.nodb"
192 let flags = flags @ if nodb then ["NODB=true"] else [] in
195 flags @ [ sprintf "MATITA_FLAGS=\"%s\"" matita_flags ]
196 with Not_found -> flags in
198 ["--no-print-directory"; "-s"; "-k"; "-f"; makefile; target] @ flags
200 make development.root args
202 let build_development ?matita_flags ?(target="all") development =
203 call_make ?matita_flags development target make
205 (* not really good vt100 *)
207 let rex = Pcre.regexp "
\e\\[[0-9;]+m" in
208 let rex_i = Pcre.regexp "^Info" in
209 let rex_w = Pcre.regexp "^Warning" in
210 let rex_e = Pcre.regexp "^Error" in
211 let rex_d = Pcre.regexp "^Debug" in
212 let rex_noendline = Pcre.regexp "\\n" in
213 let s = Pcre.replace ~rex:rex_noendline s in
214 let tokens = Pcre.split ~rex s in
215 let logger = ref HLog.message in
220 (if Pcre.pmatch ~rex:rex_i s then
221 logger := HLog.message
222 else if Pcre.pmatch ~rex:rex_w s then
224 else if Pcre.pmatch ~rex:rex_e s then
226 else if Pcre.pmatch ~rex:rex_d s then
235 let mk_maker refresh_cb =
237 let out_r,out_w = Unix.pipe () in
238 let err_r,err_w = Unix.pipe () in
240 ignore(Sys.signal Sys.sigchld (Sys.Signal_ignore));
242 let argv = Array.of_list ("make"::args) in
243 pid := Unix.create_process "make" argv Unix.stdin out_w err_w;
246 let buf = String.create 1024 in
247 let rec aux = function
249 let len = Unix.read f buf 0 1024 in
253 (Unix.EPIPE,"read","len = 0 (matita internal)"));
254 vt100 (String.sub buf 0 len);
259 let r,_,_ = Unix.select [out_r; err_r] [] [] (-. 1.) in
265 | Unix.Unix_error (_,"read",_)
266 | Unix.Unix_error (_,"select",_) -> true)
268 let build_development_in_bg ?matita_flags ?(target="all") refresh_cb development =
269 call_make ?matita_flags development target (mk_maker refresh_cb)
272 let clean_development ?matita_flags development =
273 call_make ?matita_flags development "clean" make
275 let clean_development_in_bg ?matita_flags refresh_cb development =
276 call_make development ?matita_flags "clean" (mk_maker refresh_cb)
278 let destroy_development_aux development clean_development =
279 let delete_development development =
283 with Unix.Unix_error _ -> logger `Debug ("Unable to delete " ^ file)
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 rmdir (pool () ^ development.name);
300 List.filter (fun d -> d.name <> development.name) !developments
302 if not(clean_development development) then
304 logger `Warning "Unable to clean the development problerly.";
305 logger `Warning "This may cause garbage."
307 delete_development development
309 let destroy_development ?matita_flags development =
310 destroy_development_aux development (clean_development ?matita_flags)
312 let destroy_development_in_bg ?matita_flags refresh development =
313 destroy_development_aux development
314 (clean_development_in_bg refresh ?matita_flags )
316 let root_for_development development = development.root
317 let name_for_development development = development.name
319 let publish_development_bstract build clean devel =
320 let matita_flags = "\"-system\"" in
321 HLog.message "cleaning the development before publishing";
322 if clean ~matita_flags:"" devel then
324 HLog.message "rebuilding the development in 'system' space";
325 if build ~matita_flags devel then
327 HLog.message "publishing succeded";
332 HLog.error "building process failed, reverting";
333 if not (clean ~matita_flags devel) then
334 HLog.error "cleaning failed, end of the world (2)";
339 (HLog.error "unable to clean the development, publishing failed"; false)
341 let publish_development devel =
342 publish_development_bstract
343 (fun ~matita_flags devel -> build_development ~matita_flags devel)
344 (fun ~matita_flags devel -> clean_development ~matita_flags devel) devel
345 let publish_development_in_bg cb devel =
346 publish_development_bstract
347 (fun ~matita_flags devel -> build_development_in_bg cb ~matita_flags devel)
348 (fun ~matita_flags devel -> clean_development_in_bg cb ~matita_flags devel)