| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
open Pp |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
type unix_path = string |
| 18 |
|
| 19 |
type file_kind = |
| 20 |
| FileDir of unix_path * string |
| 21 |
| FileRegular of string |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
let (//) dirname filename = |
| 26 |
let l = String.length dirname in |
| 27 |
if l = 0 || dirname.[l-1] = '/' |
| 28 |
then dirname ^ filename |
| 29 |
else dirname ^ "/" ^ filename |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
let skipped_dirnames = ref ["CVS"; "_darcs"] |
| 35 |
|
| 36 |
let exclude_directory f = skipped_dirnames := f :: !skipped_dirnames |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
let ok_dirname f = |
| 44 |
not (f = "") && f.[0] != '.' && |
| 45 |
not (List.mem f !skipped_dirnames) && |
| 46 |
match Unicode.ident_refutation f with None -> true | _ -> false |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
let exists_dir dir = |
| 51 |
|
| 52 |
let rec strip_trailing_slash dir = |
| 53 |
let len = String.length dir in |
| 54 |
if len > 0 && (dir.[len-1] = '/' || dir.[len-1] = '\\') |
| 55 |
then strip_trailing_slash (String.sub dir 0 (len-1)) else dir in |
| 56 |
let dir = if Sys.os_type = "Win32" then strip_trailing_slash dir else dir in |
| 57 |
try Sys.is_directory dir with Sys_error _ -> false |
| 58 |
|
| 59 |
let rec mkdir dir = |
| 60 |
if not (exists_dir dir) then |
| 61 |
begin |
| 62 |
mkdir (Filename.dirname dir); |
| 63 |
Unix.mkdir dir 0o755; |
| 64 |
end |
| 65 |
|
| 66 |
let apply_subdir f path name = |
| 67 |
|
| 68 |
|
| 69 |
let base = try Filename.chop_extension name with Invalid_argument _ -> name in |
| 70 |
if ok_dirname base then |
| 71 |
let path = if path = "." then name else path//name in |
| 72 |
match try (Unix.stat path).Unix.st_kind with Unix.Unix_error _ -> Unix.S_BLK with |
| 73 |
| Unix.S_DIR when name = base -> f (FileDir (path,name)) |
| 74 |
| Unix.S_REG -> f (FileRegular name) |
| 75 |
| _ -> () |
| 76 |
|
| 77 |
let readdir dir = try Sys.readdir dir with Sys_error _ -> [||] |
| 78 |
|
| 79 |
let process_directory f path = |
| 80 |
Array.iter (apply_subdir f path) (readdir path) |
| 81 |
|
| 82 |
let process_subdirectories f path = |
| 83 |
let f = function FileDir (path,base) -> f path base | FileRegular _ -> () in |
| 84 |
process_directory f path |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
let warn_cannot_open_dir = |
| 91 |
CWarnings.create ~name:"cannot-open-dir" ~category:CWarnings.CoreCategories.filesystem |
| 92 |
(fun dir -> str ("Cannot open directory " ^ dir)) |
| 93 |
|
| 94 |
let all_subdirs ~unix_path:root = |
| 95 |
let l = ref [] in |
| 96 |
let add f rel = l := (f, rel) :: !l in |
| 97 |
let rec traverse path rel = |
| 98 |
let f = function |
| 99 |
| FileDir (path,f) -> |
| 100 |
let newrel = rel @ [f] in |
| 101 |
add path newrel; |
| 102 |
traverse path newrel |
| 103 |
| _ -> () |
| 104 |
in process_directory f path |
| 105 |
in |
| 106 |
if exists_dir root then traverse root [] |
| 107 |
else warn_cannot_open_dir root; |
| 108 |
List.rev !l |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
let dirmap = ref CString.Map.empty |
| 114 |
|
| 115 |
let make_dir_table dir = |
| 116 |
let entries = |
| 117 |
try |
| 118 |
Sys.readdir dir |
| 119 |
with Sys_error _ -> |
| 120 |
warn_cannot_open_dir dir; |
| 121 |
[||] in |
| 122 |
let filter_dotfiles s f = if f.[0] = '.' then s else CString.Set.add f s in |
| 123 |
Array.fold_left filter_dotfiles CString.Set.empty entries |
| 124 |
|
| 125 |
|
| 126 |
let trust_file_cache = ref false |
| 127 |
|
| 128 |
let exists_in_dir_respecting_case dir bf = |
| 129 |
let cache_dir dir = |
| 130 |
let contents = make_dir_table dir in |
| 131 |
dirmap := CString.Map.add dir contents !dirmap; |
| 132 |
contents in |
| 133 |
let contents, fresh = |
| 134 |
try |
| 135 |
|
| 136 |
CString.Map.find dir !dirmap, !trust_file_cache |
| 137 |
with Not_found -> |
| 138 |
|
| 139 |
if !trust_file_cache && not (exists_dir dir) then CString.Set.empty, true |
| 140 |
else cache_dir dir, true in |
| 141 |
CString.Set.mem bf contents || |
| 142 |
not fresh && |
| 143 |
|
| 144 |
CString.Set.mem bf (cache_dir dir) |
| 145 |
|
| 146 |
let file_exists_respecting_case path f = |
| 147 |
|
| 148 |
|
| 149 |
let rec aux f = |
| 150 |
let bf = Filename.basename f in |
| 151 |
let df = Filename.dirname f in |
| 152 |
|
| 153 |
|
| 154 |
(String.equal df "." || String.equal f df || aux df) |
| 155 |
&& exists_in_dir_respecting_case (Filename.concat path df) bf |
| 156 |
in (!trust_file_cache || Sys.file_exists (Filename.concat path f)) && aux f |
| 157 |
|
| 158 |
let search paths test = List.concat_map test paths |
| 159 |
|
| 160 |
let warn_ambiguous_file_name = |
| 161 |
CWarnings.create ~name:"ambiguous-file-name" ~category:CWarnings.CoreCategories.filesystem |
| 162 |
(fun (filename,l,f) -> str filename ++ str " has been found in" ++ spc () ++ |
| 163 |
hov 0 (str "[ " ++ |
| 164 |
hv 0 (prlist_with_sep (fun () -> str " " ++ pr_semicolon()) |
| 165 |
(fun (lpe,_) -> str lpe) l) |
| 166 |
++ str " ];") ++ fnl () ++ |
| 167 |
str "loading " ++ str f) |
| 168 |
|
| 169 |
|
| 170 |
let where_in_path ?(warn=true) path filename = |
| 171 |
let check_and_warn l = match l with |
| 172 |
| [] -> raise Not_found |
| 173 |
| (lpe, f) :: l' -> |
| 174 |
let () = match l' with |
| 175 |
| _ :: _ when warn -> warn_ambiguous_file_name (filename,l,f) |
| 176 |
| _ -> () |
| 177 |
in |
| 178 |
(lpe, f) |
| 179 |
in |
| 180 |
check_and_warn (search path (fun lpe -> |
| 181 |
let f = Filename.concat lpe filename in |
| 182 |
if file_exists_respecting_case lpe filename then [lpe,f] else [])) |
| 183 |
|
| 184 |
let all_in_path path filename = |
| 185 |
search path (fun (physicaldir,logicaldir) -> |
| 186 |
let f = Filename.concat physicaldir filename in |
| 187 |
if file_exists_respecting_case physicaldir filename then [logicaldir,f] else []) |
| 188 |
|
| 189 |
let find_file_in_path ?(warn=true) paths filename = |
| 190 |
if not (Filename.is_implicit filename) then |
| 191 |
|
| 192 |
|
| 193 |
if Sys.file_exists filename then |
| 194 |
let root = Filename.dirname filename in |
| 195 |
root, filename |
| 196 |
else |
| 197 |
CErrors.user_err |
| 198 |
(hov 0 (str "Can't find file" ++ spc () ++ str filename ++ str ".")) |
| 199 |
else |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
try where_in_path ~warn paths filename |
| 204 |
with Not_found -> |
| 205 |
CErrors.user_err |
| 206 |
(hov 0 (str "Can't find file" ++ spc () ++ str filename ++ spc () ++ |
| 207 |
str "on loadpath.")) |
| 208 |
|
| 209 |
let is_in_path lpath filename = |
| 210 |
try ignore (where_in_path ~warn:false lpath filename); true |
| 211 |
with Not_found -> false |
| 212 |
|
| 213 |
let warn_path_not_found = |
| 214 |
CWarnings.create ~name:"PATH-not-found" |
| 215 |
(fun () -> str "Environment variable PATH not set") |
| 216 |
|
| 217 |
let is_in_system_path filename = |
| 218 |
try |
| 219 |
let lpath = CUnix.path_to_list (Sys.getenv "PATH") in |
| 220 |
is_in_path lpath filename |
| 221 |
with Not_found -> |
| 222 |
warn_path_not_found (); |
| 223 |
false |
| 224 |
|
| 225 |
let warn_using_current_directory = |
| 226 |
CWarnings.create ~name:"default-output-directory" ~category:CWarnings.CoreCategories.filesystem |
| 227 |
Pp.(fun s -> |
| 228 |
strbrk "Output directory is unset, using \"" ++ str s ++ str "\"." ++ spc () ++ |
| 229 |
strbrk "Use command line option \"-output-directory to set a default directory.") |
| 230 |
|
| 231 |
let output_directory = ref None |
| 232 |
|
| 233 |
let get_output_path filename = |
| 234 |
if not (Filename.is_relative filename) then filename |
| 235 |
else match !output_directory with |
| 236 |
| None -> |
| 237 |
let pwd = Sys.getcwd () in |
| 238 |
warn_using_current_directory pwd; |
| 239 |
Filename.concat pwd filename |
| 240 |
| Some dir -> Filename.concat dir filename |
| 241 |
|
| 242 |
let error_corrupted file s = |
| 243 |
CErrors.user_err (str file ++ str ": " ++ str s ++ str ". Try to rebuild it.") |
| 244 |
|
| 245 |
let check_caml_version ~caml:s ~file:f = |
| 246 |
if not (String.equal Coq_config.caml_version s) then |
| 247 |
CErrors.user_err (str ("The file " ^ f ^ " was compiled with OCaml") ++ |
| 248 |
spc () ++ str s ++ spc () ++ str "while this instance of Rocq was compiled \ |
| 249 |
with OCaml" ++ spc() ++ str Coq_config.caml_version ++ str "." ++ spc () ++ |
| 250 |
str "Rocq object files need to be compiled with the same OCaml toolchain to \ |
| 251 |
be compatible.") |
| 252 |
else () |
| 253 |
|
| 254 |
let marshal_out ch v = Marshal.to_channel ch v []; flush ch |
| 255 |
let marshal_in filename ch = |
| 256 |
try Marshal.from_channel ch |
| 257 |
with |
| 258 |
| End_of_file -> error_corrupted filename "premature end of file" |
| 259 |
| Failure s -> error_corrupted filename s |
| 260 |
|
| 261 |
type magic_number_error = {filename: string; actual: int32; expected: int32} |
| 262 |
exception Bad_magic_number of magic_number_error |
| 263 |
exception Bad_version_number of magic_number_error |
| 264 |
|
| 265 |
let with_magic_number_check ?loc f a = |
| 266 |
try f a |
| 267 |
with |
| 268 |
| Bad_magic_number {filename=fname; actual; expected} -> |
| 269 |
CErrors.user_err ?loc |
| 270 |
(str"File " ++ str fname ++ strbrk" has bad magic number " ++ |
| 271 |
(str @@ Int32.to_string actual) ++ str" (expected " ++ (str @@ Int32.to_string expected) ++ str")." ++ |
| 272 |
spc () ++ |
| 273 |
strbrk "It is corrupted or was compiled with another version of Rocq.") |
| 274 |
| Bad_version_number {filename=fname;actual=actual;expected=expected} -> |
| 275 |
CErrors.user_err ?loc |
| 276 |
(str"File " ++ str fname ++ strbrk" has bad version number " ++ |
| 277 |
(str @@ Int32.to_string actual) ++ str" (expected " ++ (str @@ Int32.to_string expected) ++ str")." ++ |
| 278 |
spc () ++ |
| 279 |
strbrk "It is corrupted or was compiled with another version of Rocq.") |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
let input_int32 ch = |
| 284 |
let accu = ref 0l in |
| 285 |
for _i = 0 to 3 do |
| 286 |
let c = input_byte ch in |
| 287 |
accu := Int32.add (Int32.shift_left !accu 8) (Int32.of_int c) |
| 288 |
done; |
| 289 |
!accu |
| 290 |
|
| 291 |
let input_int64 ch = |
| 292 |
let accu = ref 0L in |
| 293 |
for _i = 0 to 7 do |
| 294 |
let c = input_byte ch in |
| 295 |
accu := Int64.add (Int64.shift_left !accu 8) (Int64.of_int c) |
| 296 |
done; |
| 297 |
!accu |
| 298 |
|
| 299 |
let output_int32 ch n = |
| 300 |
for i = 0 to 3 do |
| 301 |
output_byte ch (Int32.to_int (Int32.shift_right_logical n (24 - 8 * i))) |
| 302 |
done |
| 303 |
|
| 304 |
let output_int64 ch n = |
| 305 |
for i = 0 to 7 do |
| 306 |
output_byte ch (Int64.to_int (Int64.shift_right_logical n (56 - 8 * i))) |
| 307 |
done |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
type time = {real: float; user: float; system: float; } |
| 313 |
type duration = time |
| 314 |
|
| 315 |
let empty_duration = { real = 0.; user = 0.; system = 0. } |
| 316 |
|
| 317 |
let get_time () = |
| 318 |
let t = Unix.times () in |
| 319 |
{ real = Unix.gettimeofday(); |
| 320 |
user = t.Unix.tms_utime; |
| 321 |
system = t.Unix.tms_stime; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
let round f = (floor (f *. 1e3)) *. 1e-3 |
| 326 |
|
| 327 |
let duration_between ~start ~stop = { |
| 328 |
real = stop.real -. start.real; |
| 329 |
user = stop.user -. start.user; |
| 330 |
system = stop.system -. start.system; |
| 331 |
} |
| 332 |
|
| 333 |
let duration_add t1 t2 = { |
| 334 |
real = t1.real +. t2.real; |
| 335 |
user = t1.user +. t2.user; |
| 336 |
system = t1.system +. t2.system; |
| 337 |
} |
| 338 |
|
| 339 |
let diff1 proj ~start ~stop = round (proj stop -. proj start) |
| 340 |
|
| 341 |
let time_difference t1 t2 = diff1 (fun t -> t.real) ~start:t1 ~stop:t2 |
| 342 |
|
| 343 |
let duration_real { real; _ } = real |
| 344 |
|
| 345 |
let fmt_duration { real = treal; user; system } = |
| 346 |
real (round treal) ++ str " secs " ++ |
| 347 |
str "(" ++ |
| 348 |
real (round user) ++ str "u" ++ |
| 349 |
str "," ++ |
| 350 |
real (round system) ++ str "s" ++ |
| 351 |
str ")" |
| 352 |
|
| 353 |
let fmt_time_difference start stop = |
| 354 |
fmt_duration (duration_between ~start ~stop) |
| 355 |
|
| 356 |
type 'a transaction_result = (('a * duration), (Exninfo.iexn * duration)) Result.t |
| 357 |
|
| 358 |
let measure_duration f x = |
| 359 |
let start = get_time() in |
| 360 |
try |
| 361 |
let y = f x in |
| 362 |
let stop = get_time() in |
| 363 |
Ok (y, duration_between ~start ~stop) |
| 364 |
with e -> |
| 365 |
let stop = get_time() in |
| 366 |
let exn = Exninfo.capture e in |
| 367 |
Error (exn, duration_between ~start ~stop) |
| 368 |
|
| 369 |
let fmt_transaction_result x = |
| 370 |
let msg, msg2, duration = match x with |
| 371 |
| Ok(_, duration) -> |
| 372 |
"Finished transaction in ", " (successful)", duration |
| 373 |
| Error(_,duration) -> |
| 374 |
"Finished failing transaction in ", " (failure)", duration |
| 375 |
in |
| 376 |
str msg ++ fmt_duration duration ++ str msg2 |
| 377 |
|
| 378 |
type instruction_count = (Int64.t, string) Result.t |
| 379 |
|
| 380 |
let instruction_count_add c1 c2 = |
| 381 |
match (c1, c2) with |
| 382 |
| (Ok i1, Ok i2) -> Ok (Int64.add i1 i2) |
| 383 |
| (Error _, _) -> c1 |
| 384 |
| (_, Error _) -> c2 |
| 385 |
|
| 386 |
type 'a instructions_result = |
| 387 |
(('a * instruction_count), (Exninfo.iexn * instruction_count)) Result.t |
| 388 |
|
| 389 |
let instructions_between ~c_start ~c_end = |
| 390 |
match (c_start, c_end) with |
| 391 |
| (Error _, _) -> c_start |
| 392 |
| (_, Error _) -> c_end |
| 393 |
| (Ok c0, Ok c1) -> Ok (Int64.sub c1 c0) |
| 394 |
|
| 395 |
let count_instructions f x = |
| 396 |
let c_start = Instr.read_counter () in |
| 397 |
try |
| 398 |
let y = f x in |
| 399 |
let c_end = Instr.read_counter () in |
| 400 |
Ok(y, instructions_between ~c_start ~c_end) |
| 401 |
with e -> |
| 402 |
let exn = Exninfo.capture e in |
| 403 |
let c_end = Instr.read_counter () in |
| 404 |
Error(exn, instructions_between ~c_start ~c_end) |
| 405 |
|
| 406 |
let fmt_instructions_result r = |
| 407 |
let (failing, count, status) = |
| 408 |
match r with |
| 409 |
| Ok(_, count) -> ("", count, " (successful)") |
| 410 |
| Error(_, count) -> ("failing ", count, " (failure)" ) |
| 411 |
in |
| 412 |
match count with |
| 413 |
| Ok i -> |
| 414 |
str "Finished " ++ str failing ++ str "transaction in " ++ int64 i ++ |
| 415 |
str " instructions" ++ str status |
| 416 |
| Error m -> |
| 417 |
str "Finished " ++ str failing ++ |
| 418 |
str "transaction with instruction count error \"" ++ |
| 419 |
str m ++ str "\"" ++ str status |
| 420 |
|
| 421 |
|
| 422 |
let get_toplevel_path top = |
| 423 |
let open Filename in |
| 424 |
let dir = if String.equal (basename Sys.argv.(0)) Sys.argv.(0) |
| 425 |
then Boot.Env.rocqbin ^ dir_sep else dirname Sys.argv.(0) ^ dir_sep in |
| 426 |
let exe = if Sys.(os_type = "Win32" || os_type = "Cygwin") then ".exe" else "" in |
| 427 |
dir ^ top ^ exe |