let debug = ref false let execvp cmd arg = match fork() with | 0 -> Unix.execvp cmd (Array.concat [ [| cmd |]; arg ]) | p -> snd(waitpid [] p) let cleanup_ocaml_aux file = let ml = ".ml" in if Filename.check_suffix file ".ml" then let basename = Filename.chop_suffix file ml in let suffixes = [ ".cmi"; ".cmo" ] in let files = List.map (fun suf -> basename ^ suf) suffixes in List.iter unlink_f files let can_compile file = let res = match execvp_redirect [ Silent ] "ocamlc" [| "-c"; file |] with WEXITED 0 -> true | _ -> false in cleanup_ocaml_aux file; res ;; let ocaml_test compile prog = handle_unix_error (fun () -> let file = Filename.temp_file "foo" ".ml" in file_of_string prog file; let unlink = if !debug then fun _ -> () else unlink_f in try_finalize compile file unlink file) () ;; let ocaml_defined v = handle_unix_error (fun () -> let prog = Printf.sprintf "let _ = %s;;\n" v in ocaml_test can_compile prog) () ;; let ocaml_value_has_type v t = handle_unix_error (fun () -> let prog = Printf.sprintf "let (_ : %s) = %s;;\n" t v in ocaml_test can_compile prog) () ;; let ocaml_prog file = handle_unix_error (fun () -> List.hd (find_in_path (testfile [Fexecutable]) (get_path()) file)) () ;; let ocaml_version ocamlc = handle_unix_error (fun () -> List.hd (execvp_to_list ocamlc [| "-version" |])) () let ocaml_output substs files = let regexps = List.map (fun (orig, repl) -> Str.regexp orig, repl) substs in List.iter (fun (file_in, file_out) -> let ic = open_in file_in in let oc = open_out file_out in try while true do let line = input_line ic in let rec iter regexps line = match regexps with [] -> line | (reg, repl) :: tail -> iter tail (Str.global_replace reg repl line) in output_string oc (iter regexps line); output_char oc '\n'; done with End_of_file -> close_in ic; close_out oc) files ;; |