Debian Codegraph @

tools/configure/util.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Printf
12
13 let debug = ref false
14
15 let red, yellow, reset =
16 if Unix.isatty Unix.stdout && Unix.isatty Unix.stderr && Sys.os_type = "Unix"
17 then "\027[31m", "\027[33m", "\027[0m"
18 else "", "", ""
19
20 (** * Utility functions *)
21 let cfprintf oc = kfprintf (fun oc -> fprintf oc "\n%!") oc
22 let cprintf s = cfprintf stdout s
23 let ceprintf s = cfprintf stderr s
24 let die msg = ceprintf "%s%s%s\nConfiguration script failed!" red msg reset; exit 1
25
26 let warn s = kfprintf (fun oc -> cfprintf oc "%s" reset) stdout ("%sWarning: " ^^ s) yellow
27
28 let i2s = string_of_int
29 let (/) x y = x ^ "/" ^ y
30
31 (** Remove the final '\r' that may exists on Win32 *)
32
33 let remove_final_cr s =
34 let n = String.length s in
35 if n<>0 && s.[n-1] = '\r' then String.sub s 0 (n-1)
36 else s
37
38 let check_exit_code (_,code) = match code with
39 | Unix.WEXITED 0 -> ()
40 | Unix.WEXITED 127 -> failwith "no such command"
41 | Unix.WEXITED n -> failwith ("exit code " ^ i2s n)
42 | Unix.WSIGNALED n -> failwith ("killed by signal " ^ i2s n)
43 | Unix.WSTOPPED n -> failwith ("stopped by signal " ^ i2s n)
44
45 (** As for Unix.close_process, our Unix.waipid will ignore all EINTR *)
46
47 let rec waitpid_non_intr pid =
48 try Unix.waitpid [] pid
49 with Unix.Unix_error (Unix.EINTR, _, _) -> waitpid_non_intr pid
50
51 (** Below, we'd better read all lines on a channel before closing it,
52 otherwise a SIGPIPE could be encountered by the sub-process *)
53
54 let read_lines_and_close cin =
55 let lines = ref [] in
56 begin
57 try
58 while true do
59 lines := remove_final_cr (input_line cin) :: !lines
60 done
61 with End_of_file -> ()
62 end;
63 close_in cin;
64 let lines = List.rev !lines in
65 try List.hd lines, lines with Failure _ -> "", []
66
67 let read_lines_and_close_fd fd =
68 read_lines_and_close (Unix.in_channel_of_descr fd)
69
70 (** Run some unix command and read the first line of its output.
71 We avoid Unix.open_process and its non-fully-portable /bin/sh,
72 especially when it comes to quoting the filenames.
73 Error messages:
74 - if err=StdErr, any error message goes in the stderr of our script.
75 - if err=StdOut, we merge stderr and stdout (just as 2>&1).
76 - if err=DevNull, we drop the error messages (same as 2>/dev/null). *)
77
78 type err = StdErr | StdOut | DevNull
79
80 let exe = ref "" (* Will be set later on, when the call to uname is done *)
81
82 let run ?(fatal=true) ?(verbose=false) ?(err=StdErr) prog args =
83 let prog = (* Ensure prog ends with exe *)
84 if Str.string_match (Str.regexp ("^.*" ^ !exe ^ "$")) prog 0
85 then prog else (prog ^ !exe) in
86 let argv = Array.of_list (prog::args) in
87 try
88 let out_r,out_w = Unix.pipe () in
89 let nul_r,nul_w = Unix.pipe () in
90 let () = Unix.set_close_on_exec out_r in
91 let () = Unix.set_close_on_exec nul_r in
92 let fd_err = match err with
93 | StdErr -> Unix.stderr
94 | StdOut -> out_w
95 | DevNull -> nul_w
96 in
97 let pid = Unix.create_process prog argv Unix.stdin out_w fd_err in
98 let () = Unix.close out_w in
99 let () = Unix.close nul_w in
100 let line, all = read_lines_and_close_fd out_r in
101 let _ = read_lines_and_close_fd nul_r in
102 let () = check_exit_code (waitpid_non_intr pid) in
103 line, all
104 with
105 | _ when not fatal && not verbose -> "", []
106 | e ->
107 let cmd = String.concat " " (prog::args) in
108 let exn = match e with Failure s -> s | _ -> Printexc.to_string e in
109 let msg = sprintf "Error while running '%s' (%s)" cmd exn in
110 if fatal then die msg else (warn "%s" msg; "", [])
111
112 let tryrun prog args = run ~fatal:false ~err:DevNull prog args
113
114 (** Splitting a string at some character *)
115
116 let string_split c s =
117 let len = String.length s in
118 let rec split n =
119 try
120 let pos = String.index_from s n c in
121 let dir = String.sub s n (pos-n) in
122 dir :: split (succ pos)
123 with
124 | Not_found -> [String.sub s n (len-n)]
125 in
126 if len = 0 then [] else split 0
127
128 (** String prefix test : does [s1] starts with [s2] ? *)
129
130 let starts_with s1 s2 =
131 let l1 = String.length s1 and l2 = String.length s2 in
132 l2 <= l1 && s2 = String.sub s1 0 l2
133
134 (** Turn a version string such as "4.01.0+rc2" into the list
135 ["4";"01";"1"], stopping at the first non-digit or "." *)
136
137 let numeric_prefix_list s =
138 let isnum c = (c = '.' || (c >= '0' && c <= '9')) in
139 let max = String.length s in
140 let i = ref 0 in
141 while !i < max && isnum s.[!i] do incr i done;
142 match string_split '.' (String.sub s 0 !i) with
143 | [v] -> [v;"0";"0"]
144 | [v1;v2] -> [v1;v2;"0"]
145 | [v1;v2;""] -> [v1;v2;"0"] (* e.g. because it ends with ".beta" *)
146 | v -> v
147
148 let generic_version_nums ~name version_string =
149 let version_list = numeric_prefix_list version_string in
150 if !debug then
151 begin
152 let pp_sep = Format.pp_print_space in
153 Format.(eprintf "Parsing version info for %s: @[raw: %s / split: %a@]@\n%!"
154 name version_string (pp_print_list ~pp_sep pp_print_string) version_list)
155 end;
156 try List.map int_of_string version_list
157 with _ ->
158 "I found " ^ name ^ " but cannot read its version number!\n" ^
159 "Is it installed properly?" |> die
160
161 (** Combined existence and directory tests *)
162
163 let dir_exists f = Sys.file_exists f && Sys.is_directory f
164
165 (** Does a file exist and is executable ? *)
166
167 let is_executable f =
168 try let () = Unix.access f [Unix.X_OK] in true
169 with Unix.Unix_error _ -> false
170
171 (** Equivalent of rm -f *)
172
173 let safe_remove f =
174 try Unix.chmod f 0o644; Sys.remove f with Unix.Unix_error _ | Sys_error _ -> ()
175
176 (** The PATH list for searching programs *)
177
178 let os_type_win32 = (Sys.os_type = "Win32")
179 let os_type_cygwin = (Sys.os_type = "Cygwin")
180
181 let global_path =
182 try string_split (if os_type_win32 then ';' else ':') (Sys.getenv "PATH")
183 with Not_found -> []
184
185 (** A "which" command. May raise [Not_found] *)
186
187 let which prog =
188 let rec search = function
189 | [] -> raise Not_found
190 | dir :: path ->
191 let file = if os_type_win32 then dir/prog^".exe" else dir/prog in
192 if is_executable file then file else search path
193 in search global_path
194
195 let program_in_path prog =
196 try let _ = which prog in true with Not_found -> false
197
198 (** * Architecture *)
199
200 let arch_progs =
201 [("/bin/uname",["-s"]);
202 ("/usr/bin/uname",["-s"]);
203 ("/bin/arch", []);
204 ("/usr/bin/arch", []);
205 ("/usr/ucb/arch", []) ]
206
207 let query_arch () =
208 cprintf "I can not automatically find the name of your architecture.";
209 cprintf "Give me a name, please [win32 for Win95, Win98 or WinNT]: %!";
210 read_line ()
211
212 let rec try_archs = function
213 | (prog,args)::rest when is_executable prog ->
214 let arch, _ = tryrun prog args in
215 if arch <> "" then arch else try_archs rest
216 | _ :: rest -> try_archs rest
217 | [] -> query_arch ()
218
219 let arch = function
220 | Some a -> a
221 | None ->
222 let arch,_ = tryrun "uname" ["-s"] in
223 if starts_with arch "CYGWIN" then "win32"
224 else if starts_with arch "MINGW32" then "win32"
225 else if arch <> "" then arch
226 else try_archs arch_progs
227
228 let write_config_file ~file ?(bin=false) action =
229 safe_remove file;
230 let o = if bin then open_out_bin file else open_out file in
231 try
232 action o;
233 close_out o;
234 Unix.chmod file 0o444
235 with exn ->
236 close_out o;
237 safe_remove file;
238 raise exn