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/
26 let logger = fun mark ->
28 | `Error -> HLog.error
29 | `Warning -> HLog.warn
30 | `Debug -> HLog.debug
31 | `Message -> HLog.message
35 { root: string ; name: string }
37 let developments = ref []
39 let pool () = Helm_registry.get "matita.basedir" ^ "/matitamake/" ;;
40 let rootfile = "/root" ;;
44 let d = Unix.opendir dir in
45 let content = ref [] in
48 let name = Unix.readdir d in
49 if name <> "." && name <> ".." then
50 content := name :: !content
53 with End_of_file -> Unix.closedir d; Some !content
54 with Unix.Unix_error _ -> None
57 (* create a base env if none *)
58 HExtlib.mkdir (pool ());
59 (* load developments *)
60 match ls_dir (pool ()) with
61 | None -> logger `Error ("Unable to list directory " ^ pool ())
67 Some (HExtlib.input_file (pool () ^ name ^ rootfile))
68 with Unix.Unix_error _ ->
69 logger `Warning ("Malformed development " ^ name);
75 developments := {root = root ; name = name} :: !developments)
78 (* finds the makefile path for development devel *)
79 let makefile_for_development devel =
80 let develdir = pool () ^ devel.name in
81 develdir ^ "/makefile"
84 (* given a dir finds a development that is radicated in it or below *)
85 let development_for_dir dir =
86 let is_prefix_of d1 d2 =
87 let len1 = String.length d1 in
88 let len2 = String.length d2 in
92 let pref = String.sub d2 0 len1 in
95 (* it must be unique *)
97 Some (List.find (fun d -> is_prefix_of d.root dir) !developments)
98 with Not_found -> None
101 let development_for_name name =
103 Some (List.find (fun d -> d.name = name) !developments)
104 with Not_found -> None
106 (* dumps the deveopment to disk *)
107 let dump_development devel =
108 let devel_dir = pool () ^ devel.name in
109 HExtlib.mkdir devel_dir;
110 HExtlib.output_file ~filename:(devel_dir ^ rootfile) ~text:devel.root
113 let list_known_developments () =
114 List.map (fun r -> r.name,r.root) !developments
117 if Pcre.pmatch ~pat:"\\.opt$" Sys.argv.(0) then ".opt" else ""
119 let rebuild_makefile development =
120 let makefilepath = makefile_for_development development in
122 HExtlib.input_file BuildTimeConf.matitamake_makefile_template
124 let cc = BuildTimeConf.runtime_base_dir ^ "/matitac" ^ am_i_opt () in
125 let rm = BuildTimeConf.runtime_base_dir ^ "/matitaclean" ^ am_i_opt () in
126 let mm = BuildTimeConf.runtime_base_dir ^ "/matitadep" ^ am_i_opt () in
127 let df = pool () ^ development.name ^ "/depend" in
128 let template = Pcre.replace ~pat:"@ROOT@" ~templ:development.root template in
129 let template = Pcre.replace ~pat:"@CC@" ~templ:cc template in
130 let template = Pcre.replace ~pat:"@DEP@" ~templ:mm template in
131 let template = Pcre.replace ~pat:"@DEPFILE@" ~templ:df template in
132 let template = Pcre.replace ~pat:"@CLEAN@" ~templ:rm template in
133 HExtlib.output_file ~filename:makefilepath ~text:template
135 (* creates a new development if possible *)
136 let initialize_development name dir =
137 let name = Pcre.replace ~pat:" " ~templ:"_" name in
138 let dev = {name = name ; root = dir} in
139 match development_for_dir dir with
142 ("Directory " ^ dir ^ " is already handled by development " ^ d.name);
144 ("Development " ^ d.name ^ " is rooted in " ^ d.root);
146 (dir ^ " is a subdir of " ^ d.root);
149 dump_development dev;
150 rebuild_makefile dev;
151 developments := dev :: !developments;
154 let make chdir args =
155 let old = Unix.getcwd () in
160 (String.concat " " ("make"::(List.map Filename.quote args)))
164 | Unix.WEXITED 0 -> true
165 | Unix.WEXITED i -> logger `Error ("make returned " ^ string_of_int i);false
166 | _ -> logger `Error "make STOPPED or SIGNALED!";false
167 with Unix.Unix_error (_,cmd,err) ->
168 logger `Warning ("Unix Error: " ^ cmd ^ ": " ^ err);
171 let call_make development target make =
172 rebuild_makefile development;
173 let makefile = makefile_for_development development in
175 Helm_registry.get_opt_default Helm_registry.bool ~default:false "db.nodb"
177 let flags = if nodb then ["NODB=true"] else [] in
178 make development.root
179 (["--no-print-directory"; "-s"; "-k"; "-f"; makefile; target]
182 let build_development ?(target="all") development =
183 call_make development target make
185 (* not really good vt100 *)
187 let rex = Pcre.regexp "
\e\\[[0-9;]+m" in
188 let rex_i = Pcre.regexp "^Info" in
189 let rex_w = Pcre.regexp "^Warning" in
190 let rex_e = Pcre.regexp "^Error" in
191 let rex_d = Pcre.regexp "^Debug" in
192 let rex_noendline = Pcre.regexp "\\n" in
193 let s = Pcre.replace ~rex:rex_noendline s in
194 let tokens = Pcre.split ~rex s in
195 let logger = ref HLog.message in
200 (if Pcre.pmatch ~rex:rex_i s then
201 logger := HLog.message
202 else if Pcre.pmatch ~rex:rex_w s then
204 else if Pcre.pmatch ~rex:rex_e s then
206 else if Pcre.pmatch ~rex:rex_d s then
215 let mk_maker refresh_cb =
217 let out_r,out_w = Unix.pipe () in
218 let err_r,err_w = Unix.pipe () in
220 ignore(Sys.signal Sys.sigchld (Sys.Signal_ignore));
222 let argv = Array.of_list ("make"::args) in
223 pid := Unix.create_process "make" argv Unix.stdin out_w err_w;
226 let buf = String.create 1024 in
227 let rec aux = function
229 let len = Unix.read f buf 0 1024 in
233 (Unix.EPIPE,"read","len = 0 (matita internal)"));
234 vt100 (String.sub buf 0 len);
239 let r,_,_ = Unix.select [out_r; err_r] [] [] (-. 1.) in
245 | Unix.Unix_error (_,"read",_)
246 | Unix.Unix_error (_,"select",_) -> true)
248 let build_development_in_bg ?(target="all") refresh_cb development =
249 call_make development target (mk_maker refresh_cb)
252 let clean_development development =
253 call_make development "clean" make
255 let clean_development_in_bg refresh_cb development =
256 call_make development "clean" (mk_maker refresh_cb)
258 let destroy_development_aux development clean_development =
259 let delete_development development =
263 with Unix.Unix_error _ -> logger `Debug ("Unable to delete " ^ file)
268 with Unix.Unix_error _ ->
269 logger `Warning ("Unable to remove dir " ^ dir);
270 match ls_dir dir with
271 | None -> logger `Error ("Unable to list directory " ^ dir)
273 | Some l -> logger `Error ("The directory is not empty")
275 unlink (makefile_for_development development);
276 unlink (pool () ^ development.name ^ rootfile);
277 unlink (pool () ^ development.name ^ "/depend");
278 rmdir (pool () ^ development.name);
280 List.filter (fun d -> d.name <> development.name) !developments
282 if not(clean_development development) then
284 logger `Warning "Unable to clean the development problerly.";
285 logger `Warning "This may cause garbage."
287 delete_development development
289 let destroy_development development =
290 destroy_development_aux development clean_development
292 let destroy_development_in_bg refresh development =
293 destroy_development_aux development (clean_development_in_bg refresh)
295 let root_for_development development = development.root
296 let name_for_development development = development.name