| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
open Util |
| 11 |
open Nativecode |
| 12 |
open CErrors |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
let get_load_paths = |
| 18 |
ref (fun _ -> anomaly (Pp.str "get_load_paths not initialized.") : unit -> string list) |
| 19 |
|
| 20 |
let open_header = ["Nativevalues"; |
| 21 |
"Nativecode"; |
| 22 |
"Nativelib"; |
| 23 |
"Nativeconv"] |
| 24 |
let open_header = List.map mk_open open_header |
| 25 |
|
| 26 |
|
| 27 |
let dft_output_dir = ".coq-native" |
| 28 |
let output_dir = ref dft_output_dir |
| 29 |
|
| 30 |
|
| 31 |
let source_ext = ".native" |
| 32 |
|
| 33 |
let ( / ) = Filename.concat |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
let my_temp_dir = lazy (CUnix.mktemp_dir "Coq_native" "") |
| 38 |
|
| 39 |
let () = at_exit (fun () -> |
| 40 |
if not (keep_debug_files ()) && Lazy.is_val my_temp_dir then |
| 41 |
try |
| 42 |
let d = Lazy.force my_temp_dir in |
| 43 |
Array.iter (fun f -> Sys.remove (Filename.concat d f)) (Sys.readdir d); |
| 44 |
Unix.rmdir d |
| 45 |
with (Unix.Unix_error _ | Sys_error _) as e -> |
| 46 |
Feedback.msg_warning |
| 47 |
Pp.(str "Native compile: failed to cleanup: " ++ |
| 48 |
str(Printexc.to_string e) ++ fnl())) |
| 49 |
|
| 50 |
let delay_cleanup_file = |
| 51 |
let toclean = ref [] in |
| 52 |
let () = at_exit (fun () -> List.iter (fun f -> if Sys.file_exists f then Sys.remove f) !toclean) in |
| 53 |
fun f -> if not (keep_debug_files()) then toclean := f :: !toclean |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
let include_dirs = ref [] |
| 60 |
|
| 61 |
let default_include_dirs env = [ Boot.Env.(Path.to_string (native_cmi env "kernel")) ] |
| 62 |
|
| 63 |
let get_include_dirs () = |
| 64 |
let base = match !include_dirs with |
| 65 |
| [] -> |
| 66 |
CErrors.user_err |
| 67 |
Pp.(str "Native compute with -boot: you must also give -nI pointing to the kernel.") |
| 68 |
| _::_ as l -> l |
| 69 |
in |
| 70 |
if Lazy.is_val my_temp_dir |
| 71 |
then (Lazy.force my_temp_dir) :: base |
| 72 |
else base |
| 73 |
|
| 74 |
|
| 75 |
let load_obj = ref (fun _x -> () : string -> unit) |
| 76 |
|
| 77 |
let rsymbols = ref Nativevalues.empty_symbols |
| 78 |
let rt1 = ref None |
| 79 |
let rt2 = ref None |
| 80 |
|
| 81 |
let get_symbols () = !rsymbols |
| 82 |
|
| 83 |
let get_ml_filename () = |
| 84 |
let temp_dir = Lazy.force my_temp_dir in |
| 85 |
let filename = Filename.temp_file ~temp_dir "Coq_native" source_ext in |
| 86 |
let prefix = Filename.chop_extension (Filename.basename filename) ^ "." in |
| 87 |
filename, prefix |
| 88 |
|
| 89 |
let write_ml_code fn ?(header=[]) code = |
| 90 |
let header = open_header@header in |
| 91 |
let ch_out = open_out fn in |
| 92 |
let fmt = Format.formatter_of_out_channel ch_out in |
| 93 |
List.iter (pp_global fmt) (header@code); |
| 94 |
close_out ch_out |
| 95 |
|
| 96 |
let error_native_compiler_failed e = |
| 97 |
let msg = match e with |
| 98 |
| Inl (Unix.WEXITED 127) -> Pp.(strbrk "The OCaml compiler was not found. Make sure it is installed, together with findlib.") |
| 99 |
| Inl (Unix.WEXITED n) -> |
| 100 |
Pp.(strbrk "Native compiler exited with status" ++ str" " ++ int n |
| 101 |
++ strbrk (if n = 2 then " (in case of stack overflow, increasing stack size (typically with \"ulimit -s\") often helps)" else "")) |
| 102 |
| Inl (Unix.WSIGNALED n) -> Pp.(strbrk "Native compiler killed by signal" ++ str" " ++ int n) |
| 103 |
| Inl (Unix.WSTOPPED n) -> Pp.(strbrk "Native compiler stopped by signal" ++ str" " ++ int n) |
| 104 |
| Inr e -> Pp.(strbrk "Native compiler failed with error: " ++ strbrk (Unix.error_message e)) |
| 105 |
in |
| 106 |
CErrors.user_err msg |
| 107 |
|
| 108 |
let call_compiler ?profile:(profile=false) ml_filename = |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
let require_load_path = !get_load_paths () in |
| 114 |
|
| 115 |
|
| 116 |
let install_load_path = List.map (fun dn -> dn / dft_output_dir) require_load_path @ require_load_path in |
| 117 |
let include_dirs = List.flatten (List.map (fun x -> ["-I"; x]) (get_include_dirs () @ install_load_path)) in |
| 118 |
let f = Filename.chop_extension ml_filename in |
| 119 |
let link_filename = f ^ ".cmo" in |
| 120 |
let link_filename = Dynlink.adapt_filename link_filename in |
| 121 |
let remove f = if Sys.file_exists f then Sys.remove f in |
| 122 |
remove link_filename; |
| 123 |
remove (f ^ ".cmi"); |
| 124 |
let initial_args = |
| 125 |
if Dynlink.is_native then |
| 126 |
["opt"; "-shared"] |
| 127 |
else |
| 128 |
["ocamlc"; "-c"] |
| 129 |
in |
| 130 |
let profile_args = |
| 131 |
if profile then |
| 132 |
["-g"] |
| 133 |
else |
| 134 |
[] |
| 135 |
in |
| 136 |
let flambda_args = if Sys.(backend_type = Native) then ["-Oclassic"] else [] in |
| 137 |
let args = |
| 138 |
initial_args @ |
| 139 |
profile_args @ |
| 140 |
flambda_args @ |
| 141 |
("-o"::link_filename |
| 142 |
::"-rectypes" |
| 143 |
::"-w"::"a" |
| 144 |
::include_dirs) @ |
| 145 |
["-impl"; ml_filename] in |
| 146 |
let ocamlfind = Boot.Env.ocamlfind () in |
| 147 |
debug_native_compiler (fun () -> Pp.str (ocamlfind ^ " " ^ (String.concat " " args))); |
| 148 |
try |
| 149 |
let res = CUnix.sys_command ocamlfind args in |
| 150 |
match res with |
| 151 |
| Unix.WEXITED 0 -> link_filename |
| 152 |
| Unix.WEXITED _n | Unix.WSIGNALED _n | Unix.WSTOPPED _n -> |
| 153 |
error_native_compiler_failed (Inl res) |
| 154 |
with Unix.Unix_error (e,_,_) -> |
| 155 |
error_native_compiler_failed (Inr e) |
| 156 |
|
| 157 |
let compile fn code ~profile:profile = |
| 158 |
write_ml_code fn code; |
| 159 |
let r = call_compiler ~profile fn in |
| 160 |
|
| 161 |
|
| 162 |
delay_cleanup_file fn; |
| 163 |
r |
| 164 |
|
| 165 |
type native_library = Nativecode.global list * Nativevalues.symbols |
| 166 |
|
| 167 |
let compile_library (code, symb) fn = |
| 168 |
let header = mk_library_header symb in |
| 169 |
let fn = fn ^ source_ext in |
| 170 |
let basename = Filename.basename fn in |
| 171 |
let dirname = |
| 172 |
if Filename.is_relative !output_dir then Filename.dirname fn / !output_dir else !output_dir in |
| 173 |
let () = |
| 174 |
try Unix.mkdir dirname 0o755 |
| 175 |
with Unix.Unix_error (Unix.EEXIST, _, _) -> () |
| 176 |
in |
| 177 |
let fn = dirname / basename in |
| 178 |
write_ml_code fn ~header code; |
| 179 |
let _ = call_compiler fn in |
| 180 |
delay_cleanup_file fn |
| 181 |
|
| 182 |
let execute_library ~prefix f symbols upds = |
| 183 |
let () = rt1 := None in |
| 184 |
let () = rt2 := None in |
| 185 |
let () = rsymbols := symbols in |
| 186 |
if not (Sys.file_exists f) then |
| 187 |
CErrors.user_err Pp.(str "Cannot find native compiler file " ++ str f); |
| 188 |
if Dynlink.is_native then Dynlink.loadfile f else !load_obj f; |
| 189 |
register_native_file prefix; |
| 190 |
update_locations upds; |
| 191 |
(!rt1, !rt2) |
| 192 |
|
| 193 |
let link_library dirname prefix = |
| 194 |
let basename = Dynlink.adapt_filename (prefix ^ "cmo") in |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
let install_location = dirname / dft_output_dir / basename in |
| 199 |
let build_location = dirname / !output_dir / basename in |
| 200 |
let f = if Sys.file_exists build_location then build_location else install_location in |
| 201 |
try |
| 202 |
if Dynlink.is_native then Dynlink.loadfile f else !load_obj f; |
| 203 |
register_native_file prefix |
| 204 |
with |
| 205 |
| Dynlink.Error _ as exn -> |
| 206 |
debug_native_compiler (fun () -> CErrors.iprint (Exninfo.capture exn)) |
| 207 |
|
| 208 |
let delayed_link = ref [] |
| 209 |
|
| 210 |
let link_libraries () = |
| 211 |
let delayed = List.rev !delayed_link in |
| 212 |
delayed_link := []; |
| 213 |
List.iter (fun (dirname, libname) -> |
| 214 |
let prefix = mod_uid_of_dirpath libname ^ "." in |
| 215 |
if not (Nativecode.is_loaded_native_file prefix) then |
| 216 |
link_library dirname prefix |
| 217 |
) delayed |
| 218 |
|
| 219 |
let enable_library dirname libname = |
| 220 |
delayed_link := (dirname, libname) :: !delayed_link |