| 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 |
module Path = struct |
| 12 |
|
| 13 |
type t = string |
| 14 |
|
| 15 |
let relative = Filename.concat |
| 16 |
let to_string x = x |
| 17 |
let exists x = Sys.file_exists x |
| 18 |
|
| 19 |
end |
| 20 |
|
| 21 |
(* For now just a pointer to coq/lib (for .vo) and rocq-runtime/lib (for plugins) *) |
| 22 |
type t = |
| 23 |
{ runtimelib: Path.t |
| 24 |
; coqlib : Path.t |
| 25 |
} |
| 26 |
|
| 27 |
(* This code needs to be refactored, for now it is just what used to be in envvars *) |
| 28 |
|
| 29 |
let use_suffix prefix suffix = |
| 30 |
if Filename.is_relative suffix |
| 31 |
then Filename.concat prefix suffix |
| 32 |
else suffix |
| 33 |
|
| 34 |
let canonical_path_name p = |
| 35 |
let current = Sys.getcwd () in |
| 36 |
try |
| 37 |
Sys.chdir p; |
| 38 |
let p' = Sys.getcwd () in |
| 39 |
Sys.chdir current; |
| 40 |
p' |
| 41 |
with Sys_error _ -> |
| 42 |
(* We give up to find a canonical name and just simplify it... *) |
| 43 |
Filename.concat current p |
| 44 |
|
| 45 |
let theories_dir = "theories" |
| 46 |
let plugins_dir = "plugins" |
| 47 |
let prelude = Filename.concat theories_dir "Init/Prelude.vo" |
| 48 |
|
| 49 |
let find_in_PATH f = |
| 50 |
match Sys.getenv_opt "PATH" with |
| 51 |
| None -> None |
| 52 |
| Some paths -> |
| 53 |
let sep = if Coq_config.arch_is_win32 then ';' else ':' in |
| 54 |
let paths = String.split_on_char sep paths in |
| 55 |
paths |> List.find_opt (fun path -> |
| 56 |
Sys.file_exists (if path = "" then f else Filename.concat path f)) |
| 57 |
|
| 58 |
let rocqbin = |
| 59 |
(* avoid following symlinks if possible (Sys.executable_name followed symlinks) *) |
| 60 |
if Filename.basename Sys.argv.(0) <> Sys.argv.(0) then |
| 61 |
(* explicit directory (maybe relative to current dir) *) |
| 62 |
canonical_path_name (Filename.dirname Sys.argv.(0)) |
| 63 |
else match find_in_PATH Sys.argv.(0) with |
| 64 |
| Some p -> p |
| 65 |
| None -> canonical_path_name (Filename.dirname Sys.executable_name) |
| 66 |
|
| 67 |
(** The following only makes sense when executables are running from |
| 68 |
source tree (e.g. during build or in local mode). *) |
| 69 |
let rocqroot = |
| 70 |
let rec search = function |
| 71 |
| [] -> |
| 72 |
(* couldn't recognize the layout, guess the executable is 1 dir below the root |
| 73 |
(eg "mybin/rocq") |
| 74 |
XXX we should search only when we need the root |
| 75 |
and produce an error if we can't find it *) |
| 76 |
Filename.dirname rocqbin |
| 77 |
| path :: rest -> |
| 78 |
if Sys.file_exists (Filename.concat path "bin") then path |
| 79 |
else search rest |
| 80 |
in |
| 81 |
(* we can be "bin/rocq" or "lib/rocq-runtime/rocqworker" |
| 82 |
so rocqbin can be "bin/" or "lib/rocq-runtime/" *) |
| 83 |
let dirname = Filename.dirname in |
| 84 |
search [ dirname rocqbin; dirname @@ dirname rocqbin ] |
| 85 |
|
| 86 |
let relocate = function |
| 87 |
| Coq_config.NotRelocatable p -> p |
| 88 |
| Coq_config.Relocatable p -> Filename.concat rocqroot p |
| 89 |
|
| 90 |
(** [check_file_else ~dir ~file oth] checks if [file] exists in |
| 91 |
the installation directory [dir] given relatively to [coqroot], |
| 92 |
which maybe has been relocated. |
| 93 |
If the check fails, then [oth ()] is evaluated. |
| 94 |
Using file system equality seems well enough for this heuristic *) |
| 95 |
let check_file_else ~dir ~file oth = |
| 96 |
let path = use_suffix rocqroot dir in |
| 97 |
if Sys.file_exists (Filename.concat path file) then path else oth () |
| 98 |
|
| 99 |
let guess_coqlib () = |
| 100 |
match Util.getenv_rocq "LIB" with |
| 101 |
| Some v -> v |
| 102 |
| None -> |
| 103 |
check_file_else |
| 104 |
~dir:Coq_config.coqlibsuffix |
| 105 |
~file:prelude |
| 106 |
(fun () -> relocate Coq_config.coqlib) |
| 107 |
|
| 108 |
(* Build layout uses coqlib = coqcorelib |
| 109 |
XXX we should be using -boot in build layout so is that dead code? *) |
| 110 |
let guess_coqcorelib lib = |
| 111 |
match Util.getenv_rocq_gen ~rocq:"ROCQRUNTIMELIB" ~coq:"COQCORELIB" with |
| 112 |
| Some v -> v |
| 113 |
| None -> |
| 114 |
if Sys.file_exists (Path.relative lib plugins_dir) |
| 115 |
then lib |
| 116 |
else Path.relative lib "../rocq-runtime" |
| 117 |
|
| 118 |
let fail_lib lib = |
| 119 |
let open Printf in |
| 120 |
eprintf "File not found: %s\n" lib; |
| 121 |
eprintf "The path for Rocq libraries is wrong.\n"; |
| 122 |
eprintf "Rocq prelude is shipped in the rocq-core package.\n"; |
| 123 |
eprintf "Please check the ROCQLIB env variable or the -coqlib option.\n"; |
| 124 |
exit 1 |
| 125 |
|
| 126 |
let fail_core plugin = |
| 127 |
let open Printf in |
| 128 |
eprintf "File not found: %s\n" plugin; |
| 129 |
eprintf "The path for Rocq plugins is wrong.\n"; |
| 130 |
eprintf "Rocq plugins are shipped in the rocq-runtime package.\n"; |
| 131 |
eprintf "Please check the ROCQRUNTIMELIB env variable.\n"; |
| 132 |
exit 1 |
| 133 |
|
| 134 |
let validate_env ({ runtimelib; coqlib } as env) = |
| 135 |
let coqlib = Filename.concat coqlib prelude in |
| 136 |
if not (Sys.file_exists coqlib) then fail_lib coqlib; |
| 137 |
let plugin = Filename.concat runtimelib plugins_dir in |
| 138 |
if not (Sys.file_exists plugin) then fail_core plugin; |
| 139 |
env |
| 140 |
|
| 141 |
type maybe_env = |
| 142 |
| Env of t |
| 143 |
| Boot |
| 144 |
|
| 145 |
let env_ref = ref None |
| 146 |
|
| 147 |
(* Should we fail on double initialization? That seems a way to avoid |
| 148 |
mis-use for example when we pass command line arguments *) |
| 149 |
let init_with ~coqlib = |
| 150 |
let coqlib = match coqlib with |
| 151 |
| None -> guess_coqlib () |
| 152 |
| Some coqlib -> coqlib |
| 153 |
in |
| 154 |
let env = validate_env { coqlib; runtimelib = guess_coqcorelib coqlib } in |
| 155 |
env_ref := Some (Env env); |
| 156 |
env |
| 157 |
|
| 158 |
let initialized () = !env_ref |
| 159 |
|
| 160 |
let ignored_coqlib_msg = "Command line options -boot and -coqlib are incompatible, ignored -coqlib." |
| 161 |
|
| 162 |
let maybe_init ~warn_ignored_coqlib ~boot ~coqlib = |
| 163 |
match boot, coqlib with |
| 164 |
| true, None -> Boot |
| 165 |
| false, (None | Some _ as coqlib) -> |
| 166 |
(Env (init_with ~coqlib)) |
| 167 |
| true, Some _ -> |
| 168 |
warn_ignored_coqlib (); |
| 169 |
Boot |
| 170 |
|
| 171 |
let coqlib { coqlib; _ } = coqlib |
| 172 |
let runtimelib { runtimelib; _ } = runtimelib |
| 173 |
let plugins { runtimelib; _ } = Path.relative runtimelib plugins_dir |
| 174 |
let corelib { coqlib; _ } = Path.relative coqlib theories_dir |
| 175 |
let user_contrib { coqlib; _ } = Path.relative coqlib "user-contrib" |
| 176 |
let tool { runtimelib; _ } tool = Path.(relative (relative runtimelib "tools") tool) |
| 177 |
let revision { runtimelib; _ } = Path.relative runtimelib "revision" |
| 178 |
|
| 179 |
let native_cmi { runtimelib; _ } lib = |
| 180 |
let install_path = Path.relative runtimelib lib in |
| 181 |
if Sys.file_exists install_path then |
| 182 |
install_path |
| 183 |
else |
| 184 |
(* Dune build layout, we need to improve this *) |
| 185 |
let obj_dir = Format.asprintf ".%s.objs" lib in |
| 186 |
Filename.(concat (concat (concat runtimelib lib) obj_dir) "byte") |
| 187 |
|
| 188 |
(** {2 Caml paths} *) |
| 189 |
|
| 190 |
let ocamlfind () = match Util.getenv_opt "OCAMLFIND" with |
| 191 |
| None -> Coq_config.ocamlfind |
| 192 |
| Some v -> v |
| 193 |
|
| 194 |
let docdir () = |
| 195 |
(* This assumes implicitly that the suffix is non-trivial *) |
| 196 |
let path = use_suffix rocqroot Coq_config.docdirsuffix in |
| 197 |
if Sys.file_exists path then path else relocate Coq_config.docdir |
| 198 |
|
| 199 |
(* Print the configuration information *) |
| 200 |
|
| 201 |
let print_config ?(prefix_var_name="") env f = |
| 202 |
let coqlib = coqlib env |> Path.to_string in |
| 203 |
let runtimelib = runtimelib env |> Path.to_string in |
| 204 |
let open Printf in |
| 205 |
fprintf f "%sCOQLIB=%s/\n" prefix_var_name coqlib; |
| 206 |
fprintf f "%sCOQCORELIB=%s/\n" prefix_var_name runtimelib; |
| 207 |
fprintf f "%sDOCDIR=%s/\n" prefix_var_name (docdir ()); |
| 208 |
fprintf f "%sOCAMLFIND=%s\n" prefix_var_name (ocamlfind ()); |
| 209 |
fprintf f "%sCAMLFLAGS=%s\n" prefix_var_name Coq_config.caml_flags; |
| 210 |
fprintf f "%sWARN=%s\n" prefix_var_name "-warn-error +a-3"; |
| 211 |
fprintf f "%sHASNATDYNLINK=%s\n" prefix_var_name |
| 212 |
(if Coq_config.has_natdynlink then "true" else "false"); |
| 213 |
fprintf f "%sCOQ_SRC_SUBDIRS=%s\n" prefix_var_name (String.concat " " Coq_config.all_src_dirs); |
| 214 |
fprintf f "%sCOQ_NATIVE_COMPILER_DEFAULT=%s\n" prefix_var_name |
| 215 |
(match Coq_config.native_compiler with |
| 216 |
| Coq_config.NativeOn {ondemand=false} -> "yes" |
| 217 |
| Coq_config.NativeOff -> "no" |
| 218 |
| Coq_config.NativeOn {ondemand=true} -> "ondemand") |
| 219 |
|
| 220 |
let query_getenv = function |
| 221 |
| Boot -> assert false |
| 222 |
| Env v -> v |
| 223 |
|
| 224 |
let print_query envopt usage : Usage.query -> unit = function |
| 225 |
| PrintVersion -> Usage.version () |
| 226 |
| PrintMachineReadableVersion -> Usage.machine_readable_version () |
| 227 |
| PrintWhere -> |
| 228 |
let env = query_getenv envopt in |
| 229 |
let coqlib = coqlib env |> Path.to_string in |
| 230 |
print_endline coqlib |
| 231 |
| PrintHelp -> begin match usage with |
| 232 |
| Some usage -> Usage.print_usage stderr usage |
| 233 |
| None -> assert false |
| 234 |
end |
| 235 |
| PrintConfig -> |
| 236 |
let env = query_getenv envopt in |
| 237 |
print_config env stdout |
| 238 |
|
| 239 |
let query_needs_env : Usage.query -> string option = function |
| 240 |
| PrintVersion | PrintMachineReadableVersion | PrintHelp -> None |
| 241 |
| PrintWhere -> Some "-where" |
| 242 |
| PrintConfig -> Some "-config" |
| 243 |
|
| 244 |
let print_queries_maybe_init ~warn_ignored_coqlib ~boot ~coqlib usage = function |
| 245 |
| [] -> Ok (maybe_init ~warn_ignored_coqlib ~boot ~coqlib) |
| 246 |
| _ :: _ as qs -> |
| 247 |
let needs_env = CList.find_map query_needs_env qs in |
| 248 |
let res = match boot, needs_env with |
| 249 |
| true, Some q -> Error ("Command line option -boot is not compatible with " ^ q ^ ".") |
| 250 |
| true, None -> |
| 251 |
(* warn_ignored_coqlib if coqlib and boot used together *) |
| 252 |
Ok (maybe_init ~warn_ignored_coqlib ~boot ~coqlib) |
| 253 |
| false, None -> Ok Boot |
| 254 |
| false, Some _ -> Ok (Env (init_with ~coqlib)) |
| 255 |
in |
| 256 |
let () = match res with |
| 257 |
| Error _ -> () |
| 258 |
| Ok envopt -> List.iter (print_query envopt usage) qs |
| 259 |
in |
| 260 |
res |