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" ;;
45 (* /foo/./bar/..//baz -> /foo/baz *)
46 let normalize_path s =
47 let s = Str.global_replace (Str.regexp "//") "/" s in
48 let l = Str.split (Str.regexp "/") s in
49 let rec aux = function
51 | he::".."::tl -> aux tl
52 | he::"."::tl -> aux (he::tl)
53 | he::tl -> he :: aux tl
55 (if Str.string_match (Str.regexp "^/") s 0 then "/" else "") ^
56 String.concat "/" (aux l)
57 ^ (if Str.string_match (Str.regexp "/$") s 0 then "/" else "")
62 let d = Unix.opendir dir in
63 let content = ref [] in
66 let name = Unix.readdir d in
67 if name <> "." && name <> ".." then
68 content := name :: !content
71 with End_of_file -> Unix.closedir d; Some !content
72 with Unix.Unix_error _ -> None
75 (* create a base env if none *)
76 HExtlib.mkdir (pool ());
77 (* load developments *)
78 match ls_dir (pool ()) with
79 | None -> logger `Error ("Unable to list directory " ^ pool ())
85 Some (HExtlib.input_file (pool () ^ name ^ rootfile))
86 with Unix.Unix_error _ ->
87 logger `Warning ("Malformed development " ^ name);
93 developments := {root = root ; name = name} :: !developments;
94 let inc = Helm_registry.get_list
95 Helm_registry.string "matita.includes" in
96 Helm_registry.set_list Helm_registry.of_string
97 ~key:"matita.includes" ~value:(inc @ [root])
101 (* finds the makefile path for development devel *)
102 let makefile_for_development devel =
103 let develdir = pool () ^ devel.name in
104 develdir ^ "/makefile"
106 let dot_for_development devel =
107 let dot_fname = pool () ^ devel.name ^ "/depend.dot" in
108 if Sys.file_exists dot_fname then Some dot_fname else None
110 (* given a dir finds a development that is radicated in it or below *)
111 let development_for_dir dir =
112 let dir = normalize_path dir in
113 let is_prefix_of d1 d2 =
114 let len1 = String.length d1 in
115 let len2 = String.length d2 in
119 let pref = String.sub d2 0 len1 in
120 pref = d1 && (len1 = len2 || d2.[len1] = '/')
123 Some (List.find (fun d -> is_prefix_of d.root dir) !developments)
124 with Not_found | Failure _ -> None
126 let development_for_name name =
128 Some (List.find (fun d -> d.name = name) !developments)
129 with Not_found -> None
131 (* dumps the deveopment to disk *)
132 let dump_development devel =
133 let devel_dir = pool () ^ devel.name in
134 HExtlib.mkdir devel_dir;
135 HExtlib.output_file ~filename:(devel_dir ^ rootfile) ~text:devel.root
137 let list_known_developments () =
138 List.map (fun r -> r.name,r.root) !developments
140 let am_i_opt = lazy (
141 if Pcre.pmatch ~pat:"\\.opt$" Sys.argv.(0) then ".opt" else "")
143 let rebuild_makefile development =
144 let makefilepath = makefile_for_development development in
146 HExtlib.input_file BuildTimeConf.matitamake_makefile_template
148 let ext = Lazy.force am_i_opt in
149 let cc = BuildTimeConf.runtime_base_dir ^ "/matitac" ^ ext in
150 let rm = BuildTimeConf.runtime_base_dir ^ "/matitaclean" ^ ext in
151 let mm = BuildTimeConf.runtime_base_dir ^ "/matitadep" ^ ext in
152 let df = pool () ^ development.name ^ "/depend" in
153 let template = Pcre.replace ~pat:"@ROOT@" ~templ:development.root template in
154 let template = Pcre.replace ~pat:"@CC@" ~templ:cc template in
155 let template = Pcre.replace ~pat:"@DEP@" ~templ:mm template in
156 let template = Pcre.replace ~pat:"@DEPFILE@" ~templ:df template in
157 let template = Pcre.replace ~pat:"@CLEAN@" ~templ:rm template in
158 HExtlib.output_file ~filename:makefilepath ~text:template
160 let rebuild_makefile_devel development =
161 let path = development.root ^ "/makefile" in
162 if not (Sys.file_exists path) then
165 HExtlib.input_file BuildTimeConf.matitamake_makefile_template_devel
168 Pcre.replace ~pat:"@MATITA_RT_BASE_DIR@"
169 ~templ:BuildTimeConf.runtime_base_dir template
171 HExtlib.output_file ~filename:path ~text:template
174 (* creates a new development if possible *)
175 let initialize_development name dir =
176 let dir = normalize_path dir in
177 let name = Pcre.replace ~pat:" " ~templ:"_" name in
178 let dev = {name = name ; root = dir} in
179 dump_development dev;
180 rebuild_makefile dev;
181 rebuild_makefile_devel dev;
182 developments := dev :: !developments;
185 let make chdir args =
186 let old = Unix.getcwd () in
189 let cmd = String.concat " " ("make" :: List.map Filename.quote args) in
190 let rc = Unix.system cmd in
193 | Unix.WEXITED 0 -> true
194 | Unix.WEXITED i -> logger `Error ("make returned " ^ string_of_int i);false
195 | _ -> logger `Error "make STOPPED or SIGNALED!";false
196 with Unix.Unix_error (_,cmd,err) ->
197 logger `Warning ("Unix Error: " ^ cmd ^ ": " ^ err);
200 let call_make ?matita_flags development target make =
202 let already_defined =
203 match matita_flags with
204 | None -> (try Sys.getenv "MATITA_FLAGS" with Not_found -> "")
208 if Helm_registry.get_bool "matita.bench" then "-bench" else ""
210 let csc = try ["SRC=" ^ Sys.getenv "SRC"] with Not_found -> [] in
211 rebuild_makefile development;
212 let makefile = makefile_for_development development in
216 flags @ [ sprintf "MATITA_FLAGS=\"%s\"" matita_flags ]
217 with Not_found -> flags in
218 let flags = flags @ csc in
220 ["--no-print-directory"; "-s"; "-k"; "-f"; makefile; target] @ flags
222 (* prerr_endline (String.concat " " args); *)
223 make development.root args
225 let build_development ?matita_flags ?(target="all") development =
226 call_make ?matita_flags development target make
228 (* not really good vt100 *)
230 let rex = Pcre.regexp "
\e\\[[0-9;]+m" in
231 let rex_i = Pcre.regexp "^Info" in
232 let rex_w = Pcre.regexp "^Warning" in
233 let rex_e = Pcre.regexp "^Error" in
234 let rex_d = Pcre.regexp "^Debug" in
235 let rex_noendline = Pcre.regexp "\\n" in
236 let s = Pcre.replace ~rex:rex_noendline s in
237 let tokens = Pcre.split ~rex s in
238 let logger = ref HLog.message in
243 (if Pcre.pmatch ~rex:rex_i s then
244 logger := HLog.message
245 else if Pcre.pmatch ~rex:rex_w s then
247 else if Pcre.pmatch ~rex:rex_e s then
249 else if Pcre.pmatch ~rex:rex_d s then
258 let mk_maker refresh_cb =
260 let out_r,out_w = Unix.pipe () in
261 let err_r,err_w = Unix.pipe () in
263 let oldhandler = Sys.signal Sys.sigchld (Sys.Signal_ignore) in
265 (* prerr_endline (String.concat " " args); *)
266 let argv = Array.of_list ("make"::args) in
267 pid := Unix.create_process "make" argv Unix.stdin out_w err_w;
270 let buf = String.create 1024 in
271 let rec aux = function
273 let len = Unix.read f buf 0 1024 in
277 (Unix.EPIPE,"read","len = 0 (matita internal)"));
278 vt100 (String.sub buf 0 len);
283 let r,_,_ = Unix.select [out_r; err_r] [] [] (-. 1.) in
287 ignore(Sys.signal Sys.sigchld oldhandler);
290 | Unix.Unix_error (_,"read",_)
291 | Unix.Unix_error (_,"select",_) ->
292 ignore(Sys.signal Sys.sigchld oldhandler);
295 let build_development_in_bg ?matita_flags ?(target="all") refresh_cb development =
296 call_make ?matita_flags development target (mk_maker refresh_cb)
298 let clean_development ?matita_flags development =
299 call_make ?matita_flags development "clean" make
301 let clean_development_in_bg ?matita_flags refresh_cb development =
302 call_make development ?matita_flags "clean" (mk_maker refresh_cb)
304 let destroy_development_aux development clean_development =
305 let delete_development development =
306 let unlink = HExtlib.safe_remove in
310 with Unix.Unix_error _ ->
311 logger `Warning ("Unable to remove dir " ^ dir);
312 match ls_dir dir with
313 | None -> logger `Error ("Unable to list directory " ^ dir)
315 | Some l -> logger `Error ("The directory is not empty")
317 unlink (makefile_for_development development);
318 unlink (pool () ^ development.name ^ rootfile);
319 unlink (pool () ^ development.name ^ "/depend");
320 unlink (pool () ^ development.name ^ "/depend.errors");
321 unlink (pool () ^ development.name ^ "/depend.dot");
322 rmdir (pool () ^ development.name);
324 List.filter (fun d -> d.name <> development.name) !developments
326 if not(clean_development development) then
328 logger `Warning "Unable to clean the development problerly.";
329 logger `Warning "This may cause garbage."
331 delete_development development
333 let destroy_development ?matita_flags development =
334 destroy_development_aux development (clean_development ?matita_flags)
336 let destroy_development_in_bg ?matita_flags refresh development =
337 destroy_development_aux development
338 (clean_development_in_bg refresh ?matita_flags )
340 let root_for_development development = development.root
341 let name_for_development development = development.name
343 let publish_development_bstract build clean devel =
344 let matita_flags, matita_flags_system =
345 let orig_matita_flags =
346 try Sys.getenv "MATITA_FLAGS" with Not_found -> ""
348 "\"" ^ orig_matita_flags ^ "\"", "\"" ^ orig_matita_flags ^ " -system\""
350 HLog.message "cleaning the development before publishing";
351 if clean ~matita_flags devel then
353 HLog.message "rebuilding the development in 'system' space";
354 (* here we should use pristine metadata if we use sqlite *)
355 if build ~matita_flags:matita_flags_system devel then
357 HLog.message "publishing succeded";
362 HLog.error "building process failed, reverting";
363 if not (clean ~matita_flags devel) then
364 HLog.error "cleaning failed, end of the world (2)";
369 (HLog.error "unable to clean the development, publishing failed"; false)
371 let publish_development devel =
372 publish_development_bstract
373 (fun ~matita_flags devel -> build_development ~matita_flags devel)
374 (fun ~matita_flags devel -> clean_development ~matita_flags devel) devel
375 let publish_development_in_bg cb devel =
376 publish_development_bstract
377 (fun ~matita_flags devel -> build_development_in_bg cb ~matita_flags devel)
378 (fun ~matita_flags devel -> clean_development_in_bg cb ~matita_flags devel)