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 -> MatitaLog.error
29 | `Warning -> MatitaLog.warn
30 | `Debug -> MatitaLog.debug
31 | `Message -> MatitaLog.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 MatitaMisc.mkdir (pool ());
59 (* load developments *)
60 match ls_dir (pool ()) with
61 | None -> logger `Error ("Unable to list directory " ^ pool ())
67 Some (MatitaMisc.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 MatitaMisc.mkdir devel_dir;
110 MatitaMisc.output_file devel.root (devel_dir ^ rootfile);
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 MatitaMisc.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 dfs = pool () ^ development.name ^ "/depend.short" in
129 let template = Pcre.replace ~pat:"@ROOT@" ~templ:development.root template in
130 let template = Pcre.replace ~pat:"@CC@" ~templ:cc template in
131 let template = Pcre.replace ~pat:"@DEP@" ~templ:mm template in
132 let template = Pcre.replace ~pat:"@DEPFILE@" ~templ:df template in
133 let template = Pcre.replace ~pat:"@DEPFILESHORT@" ~templ:dfs template in
134 let template = Pcre.replace ~pat:"@CLEAN@" ~templ:rm template in
135 MatitaMisc.output_file template makefilepath
137 (* creates a new development if possible *)
138 let initialize_development name dir =
139 let dev = {name = name ; root = dir} in
140 match development_for_dir dir with
143 ("Directory " ^ dir ^ " is already handled by development " ^ d.name);
145 ("Development " ^ d.name ^ " is rooted in " ^ d.root);
147 (dir ^ " is a subdir of " ^ d.root);
150 dump_development dev;
151 rebuild_makefile dev;
154 let make chdir args =
155 let old = Unix.getcwd () in
157 (* prerr_endline (String.concat " " ("make"::args));*)
158 let rc = Unix.system (String.concat " " ("make"::args)) in
161 | Unix.WEXITED 0 -> true
162 | Unix.WEXITED i -> logger `Error ("make returned " ^ string_of_int i);false
163 | _ -> logger `Error "make STOPPED or SIGNALED!";false
165 let call_make development target =
166 rebuild_makefile development;
167 let makefile = makefile_for_development development in
168 make development.root
169 ["--no-print-directory"; "-s"; "-k"; "-f"; makefile; target]
171 let build_development ?(target="all") development =
172 call_make development target
174 let clean_development development =
175 call_make development "clean"
177 let destroy_development development =
178 let delete_development development =
182 with Unix.Unix_error _ -> logger `Debug ("Unable to delete " ^ file)
187 with Unix.Unix_error _ ->
188 logger `Warning ("Unable to remove dir " ^ dir);
189 match ls_dir dir with
190 | None -> logger `Error ("Unable to list directory " ^ dir)
192 | Some l -> logger `Error ("The directory is not empty")
194 unlink (makefile_for_development development);
195 unlink (pool () ^ development.name ^ rootfile);
196 unlink (pool () ^ development.name ^ "/depend");
197 unlink (pool () ^ development.name ^ "/depend.short");
198 rmdir (pool () ^ development.name)
200 if not(clean_development development) then
202 logger `Warning "Unable to clean the development problerly.";
203 logger `Warning "This may cause garbage."
205 delete_development development