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;
79 let inc = Helm_registry.get_list
80 Helm_registry.string "matita.includes" in
81 Helm_registry.set_list Helm_registry.of_string
82 ~key:"matita.includes" ~value:(inc @ [root])
86 (* finds the makefile path for development devel *)
87 let makefile_for_development devel =
88 let develdir = pool () ^ devel.name in
89 develdir ^ "/makefile"
91 let dot_for_development devel =
92 let dot_fname = pool () ^ devel.name ^ "/depend.dot" in
93 if Sys.file_exists dot_fname then Some dot_fname else None
95 (* given a dir finds a development that is radicated in it or below *)
96 let development_for_dir dir =
97 let is_prefix_of d1 d2 =
98 let len1 = String.length d1 in
99 let len2 = String.length d2 in
103 let pref = String.sub d2 0 len1 in
104 pref = d1 && (len1 = len2 || d2.[len1] = '/')
107 Some (List.find (fun d -> is_prefix_of d.root dir) !developments)
108 with Not_found | Failure _ -> None
110 let development_for_name name =
112 Some (List.find (fun d -> d.name = name) !developments)
113 with Not_found -> None
115 (* dumps the deveopment to disk *)
116 let dump_development devel =
117 let devel_dir = pool () ^ devel.name in
118 HExtlib.mkdir devel_dir;
119 HExtlib.output_file ~filename:(devel_dir ^ rootfile) ~text:devel.root
121 let list_known_developments () =
122 List.map (fun r -> r.name,r.root) !developments
124 let am_i_opt = lazy (
125 if Pcre.pmatch ~pat:"\\.opt$" Sys.argv.(0) then ".opt" else "")
127 let rebuild_makefile development =
128 let makefilepath = makefile_for_development development in
130 HExtlib.input_file BuildTimeConf.matitamake_makefile_template
132 let ext = Lazy.force am_i_opt in
133 let cc = BuildTimeConf.runtime_base_dir ^ "/matitac" ^ ext in
134 let rm = BuildTimeConf.runtime_base_dir ^ "/matitaclean" ^ ext in
135 let mm = BuildTimeConf.runtime_base_dir ^ "/matitadep" ^ ext in
136 let df = pool () ^ development.name ^ "/depend" in
137 let template = Pcre.replace ~pat:"@ROOT@" ~templ:development.root template in
138 let template = Pcre.replace ~pat:"@CC@" ~templ:cc template in
139 let template = Pcre.replace ~pat:"@DEP@" ~templ:mm template in
140 let template = Pcre.replace ~pat:"@DEPFILE@" ~templ:df template in
141 let template = Pcre.replace ~pat:"@CLEAN@" ~templ:rm template in
142 HExtlib.output_file ~filename:makefilepath ~text:template
144 let rebuild_makefile_devel development =
145 let path = development.root ^ "/makefile" in
146 if not (Sys.file_exists path) then
149 HExtlib.input_file BuildTimeConf.matitamake_makefile_template_devel
152 Pcre.replace ~pat:"@MATITA_RT_BASE_DIR@"
153 ~templ:BuildTimeConf.runtime_base_dir template
155 HExtlib.output_file ~filename:path ~text:template
158 (* creates a new development if possible *)
159 let initialize_development name dir =
160 let name = Pcre.replace ~pat:" " ~templ:"_" name in
161 let dev = {name = name ; root = dir} in
162 dump_development dev;
163 rebuild_makefile dev;
164 rebuild_makefile_devel dev;
165 developments := dev :: !developments;
168 let make chdir args =
169 let old = Unix.getcwd () in
172 let cmd = String.concat " " ("make" :: List.map Filename.quote args) in
173 let rc = Unix.system cmd in
176 | Unix.WEXITED 0 -> true
177 | Unix.WEXITED i -> logger `Error ("make returned " ^ string_of_int i);false
178 | _ -> logger `Error "make STOPPED or SIGNALED!";false
179 with Unix.Unix_error (_,cmd,err) ->
180 logger `Warning ("Unix Error: " ^ cmd ^ ": " ^ err);
183 let call_make ?matita_flags development target make =
185 let already_defined =
186 match matita_flags with
187 | None -> (try Sys.getenv "MATITA_FLAGS" with Not_found -> "")
191 if Helm_registry.get_bool "matita.bench" then "-bench" else ""
193 let csc = try ["SRC=" ^ Sys.getenv "SRC"] with Not_found -> [] in
194 rebuild_makefile development;
195 let makefile = makefile_for_development development in
199 flags @ [ sprintf "MATITA_FLAGS=\"%s\"" matita_flags ]
200 with Not_found -> flags in
201 let flags = flags @ csc in
203 ["--no-print-directory"; "-s"; "-k"; "-f"; makefile; target] @ flags
205 (* prerr_endline (String.concat " " args); *)
206 make development.root args
208 let build_development ?matita_flags ?(target="all") development =
209 call_make ?matita_flags development target make
211 (* not really good vt100 *)
213 let rex = Pcre.regexp "
\e\\[[0-9;]+m" in
214 let rex_i = Pcre.regexp "^Info" in
215 let rex_w = Pcre.regexp "^Warning" in
216 let rex_e = Pcre.regexp "^Error" in
217 let rex_d = Pcre.regexp "^Debug" in
218 let rex_noendline = Pcre.regexp "\\n" in
219 let s = Pcre.replace ~rex:rex_noendline s in
220 let tokens = Pcre.split ~rex s in
221 let logger = ref HLog.message in
226 (if Pcre.pmatch ~rex:rex_i s then
227 logger := HLog.message
228 else if Pcre.pmatch ~rex:rex_w s then
230 else if Pcre.pmatch ~rex:rex_e s then
232 else if Pcre.pmatch ~rex:rex_d s then
241 let mk_maker refresh_cb =
243 let out_r,out_w = Unix.pipe () in
244 let err_r,err_w = Unix.pipe () in
246 ignore(Sys.signal Sys.sigchld (Sys.Signal_ignore));
248 (* prerr_endline (String.concat " " args); *)
249 let argv = Array.of_list ("make"::args) in
250 pid := Unix.create_process "make" argv Unix.stdin out_w err_w;
253 let buf = String.create 1024 in
254 let rec aux = function
256 let len = Unix.read f buf 0 1024 in
260 (Unix.EPIPE,"read","len = 0 (matita internal)"));
261 vt100 (String.sub buf 0 len);
266 let r,_,_ = Unix.select [out_r; err_r] [] [] (-. 1.) in
272 | Unix.Unix_error (_,"read",_)
273 | Unix.Unix_error (_,"select",_) -> true)
275 let build_development_in_bg ?matita_flags ?(target="all") refresh_cb development =
276 call_make ?matita_flags development target (mk_maker refresh_cb)
278 let clean_development ?matita_flags development =
279 call_make ?matita_flags development "clean" make
281 let clean_development_in_bg ?matita_flags refresh_cb development =
282 call_make development ?matita_flags "clean" (mk_maker refresh_cb)
284 let destroy_development_aux development clean_development =
285 let delete_development development =
286 let unlink = HExtlib.safe_remove in
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)
295 | Some l -> logger `Error ("The directory is not empty")
297 unlink (makefile_for_development development);
298 unlink (pool () ^ development.name ^ rootfile);
299 unlink (pool () ^ development.name ^ "/depend");
300 unlink (pool () ^ development.name ^ "/depend.errors");
301 unlink (pool () ^ development.name ^ "/depend.dot");
302 rmdir (pool () ^ development.name);
304 List.filter (fun d -> d.name <> development.name) !developments
306 if not(clean_development development) then
308 logger `Warning "Unable to clean the development problerly.";
309 logger `Warning "This may cause garbage."
311 delete_development development
313 let destroy_development ?matita_flags development =
314 destroy_development_aux development (clean_development ?matita_flags)
316 let destroy_development_in_bg ?matita_flags refresh development =
317 destroy_development_aux development
318 (clean_development_in_bg refresh ?matita_flags )
320 let root_for_development development = development.root
321 let name_for_development development = development.name
323 let publish_development_bstract build clean devel =
324 let matita_flags, matita_flags_system =
325 let orig_matita_flags =
326 try Sys.getenv "MATITA_FLAGS" with Not_found -> ""
328 "\"" ^ orig_matita_flags ^ "\"", "\"" ^ orig_matita_flags ^ " -system\""
330 HLog.message "cleaning the development before publishing";
331 if clean ~matita_flags devel then
333 HLog.message "rebuilding the development in 'system' space";
334 (* here we should use pristine metadata if we use sqlite *)
335 if build ~matita_flags:matita_flags_system devel then
337 HLog.message "publishing succeded";
342 HLog.error "building process failed, reverting";
343 if not (clean ~matita_flags devel) then
344 HLog.error "cleaning failed, end of the world (2)";
349 (HLog.error "unable to clean the development, publishing failed"; false)
351 let publish_development devel =
352 publish_development_bstract
353 (fun ~matita_flags devel -> build_development ~matita_flags devel)
354 (fun ~matita_flags devel -> clean_development ~matita_flags devel) devel
355 let publish_development_in_bg cb devel =
356 publish_development_bstract
357 (fun ~matita_flags devel -> build_development_in_bg cb ~matita_flags devel)
358 (fun ~matita_flags devel -> clean_development_in_bg cb ~matita_flags devel)