Debian Codegraph @

boot/env.ml

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 (** Locate the dune workspace root containing the given path, if any. *)
68 let dune_workspace_root : string -> string option = fun path ->
69 let rec workspace_root path =
70 let parent = Filename.dirname path in
71 if parent = path then
72 None
73 else if Filename.basename path = "_build" then
74 Some parent
75 else
76 workspace_root parent
77 in
78 match workspace_root path with
79 | None -> None
80 | Some root ->
81 (* Check that this indeed looks like a dune workspace. *)
82 let ok =
83 let has file = Sys.file_exists (Filename.concat root file) in
84 List.exists has ["dune-project"; "dune-workspace"]
85 in
86 if ok then Some root else None
87
88 (** The following only makes sense when executables are running from
89 source tree (e.g. during build or in local mode). *)
90 let rocqroot =
•91 match dune_workspace_root rocqbin with
92 | Some dir ->
93 let dir = Filename.concat dir "_build" in
94 let dir = Filename.concat dir "install" in
95 Filename.concat dir "default"
96 | None ->
97 let rec search = function
98 | [] ->
99 (* couldn't recognize the layout, guess the executable is 1 dir below the root
100 (eg "mybin/rocq")
101 XXX we should search only when we need the root
102 and produce an error if we can't find it *)
103 Filename.dirname rocqbin
104 | path :: rest ->
105 if Sys.file_exists (Filename.concat path "bin") then path
106 else search rest
107 in
108 (* we can be "bin/rocq" or "lib/rocq-runtime/rocqworker"
109 so rocqbin can be "bin/" or "lib/rocq-runtime/" *)
110 let dirname = Filename.dirname in
111 search [ dirname rocqbin; dirname @@ dirname rocqbin ]
112
113 let relocate = function
114 | Coq_config.NotRelocatable p -> p
115 | Coq_config.Relocatable p -> Filename.concat rocqroot p
116
117 (** [check_file_else ~dir ~file oth] checks if [file] exists in
118 the installation directory [dir] given relatively to [coqroot],
119 which maybe has been relocated.
120 If the check fails, then [oth ()] is evaluated.
121 Using file system equality seems well enough for this heuristic *)
122 let check_file_else ~dir ~file oth =
123 let path = use_suffix rocqroot dir in
124 if Sys.file_exists (Filename.concat path file) then path else oth ()
125
126 let guess_coqlib () =
127 match Util.getenv_rocq "LIB" with
128 | Some v -> v
129 | None ->
130 check_file_else
131 ~dir:Coq_config.coqlibsuffix
132 ~file:prelude
133 (fun () -> relocate Coq_config.coqlib)
134
135 (* Build layout uses coqlib = coqcorelib
136 XXX we should be using -boot in build layout so is that dead code? *)
137 let guess_coqcorelib lib =
138 match Util.getenv_rocq_gen ~rocq:"ROCQRUNTIMELIB" ~coq:"COQCORELIB" with
139 | Some v -> v
140 | None ->
141 if Sys.file_exists (Path.relative lib plugins_dir)
142 then lib
143 else Path.relative lib "../rocq-runtime"
144
145 let fail_lib lib =
146 let open Printf in
147 eprintf "File not found: %s\n" lib;
148 eprintf "The path for Rocq libraries is wrong.\n";
149 eprintf "Rocq prelude is shipped in the rocq-core package.\n";
150 eprintf "Please check the ROCQLIB env variable or the -coqlib option.\n";
151 exit 1
152
153 let fail_core plugin =
154 let open Printf in
155 eprintf "File not found: %s\n" plugin;
156 eprintf "The path for Rocq plugins is wrong.\n";
157 eprintf "Rocq plugins are shipped in the rocq-runtime package.\n";
158 eprintf "Please check the ROCQRUNTIMELIB env variable.\n";
159 exit 1
160
161 let validate_env ({ runtimelib; coqlib } as env) =
162 let coqlib = Filename.concat coqlib prelude in
163 if not (Sys.file_exists coqlib) then fail_lib coqlib;
164 let plugin = Filename.concat runtimelib plugins_dir in
165 if not (Sys.file_exists plugin) then fail_core plugin;
166 env
167
168 type maybe_env =
169 | Env of t
170 | Boot
171
172 let env_ref = ref None
173
174 (* Should we fail on double initialization? That seems a way to avoid
175 mis-use for example when we pass command line arguments *)
176 let init_with ~coqlib =
177 let coqlib = match coqlib with
178 | None -> guess_coqlib ()
179 | Some coqlib -> coqlib
180 in
181 let env = validate_env { coqlib; runtimelib = guess_coqcorelib coqlib } in
182 env_ref := Some (Env env);
183 env
184
185 let initialized () = !env_ref
186
187 let ignored_coqlib_msg = "Command line options -boot and -coqlib are incompatible, ignored -coqlib."
188
189 let maybe_init ~warn_ignored_coqlib ~boot ~coqlib =
190 match boot, coqlib with
191 | true, None -> Boot
192 | false, (None | Some _ as coqlib) ->
193 (Env (init_with ~coqlib))
194 | true, Some _ ->
195 warn_ignored_coqlib ();
196 Boot
197
198 let coqlib { coqlib; _ } = coqlib
199 let runtimelib { runtimelib; _ } = runtimelib
200 let plugins { runtimelib; _ } = Path.relative runtimelib plugins_dir
201 let corelib { coqlib; _ } = Path.relative coqlib theories_dir
202 let user_contrib { coqlib; _ } = Path.relative coqlib "user-contrib"
203 let tool { runtimelib; _ } tool = Path.(relative (relative runtimelib "tools") tool)
204 let revision { runtimelib; _ } = Path.relative runtimelib "revision"
205
206 let native_cmi { runtimelib; _ } lib =
207 let install_path = Path.relative runtimelib lib in
208 if Sys.file_exists install_path then
209 install_path
210 else
211 (* Dune build layout, we need to improve this *)
212 let obj_dir = Format.asprintf ".%s.objs" lib in
213 Filename.(concat (concat (concat runtimelib lib) obj_dir) "byte")
214
215 (** {2 Caml paths} *)
216
217 let ocamlfind () = match Util.getenv_opt "OCAMLFIND" with
218 | None -> Coq_config.ocamlfind
219 | Some v -> v
220
221 let docdir () =
222 (* This assumes implicitly that the suffix is non-trivial *)
223 let path = use_suffix rocqroot Coq_config.docdirsuffix in
224 if Sys.file_exists path then path else relocate Coq_config.docdir
225
226 (* Print the configuration information *)
227
228 let print_config ?(prefix_var_name="") env f =
229 let coqlib = coqlib env |> Path.to_string in
230 let runtimelib = runtimelib env |> Path.to_string in
231 let open Printf in
232 fprintf f "%sCOQLIB=%s/\n" prefix_var_name coqlib;
233 fprintf f "%sCOQCORELIB=%s/\n" prefix_var_name runtimelib;
234 fprintf f "%sDOCDIR=%s/\n" prefix_var_name (docdir ());
235 fprintf f "%sOCAMLFIND=%s\n" prefix_var_name (ocamlfind ());
236 fprintf f "%sCAMLFLAGS=%s\n" prefix_var_name Coq_config.caml_flags;
237 fprintf f "%sWARN=%s\n" prefix_var_name "-warn-error +a-3";
238 fprintf f "%sHASNATDYNLINK=%s\n" prefix_var_name
239 (if Coq_config.has_natdynlink then "true" else "false");
240 fprintf f "%sCOQ_SRC_SUBDIRS=%s\n" prefix_var_name (String.concat " " Coq_config.all_src_dirs);
241 fprintf f "%sCOQ_NATIVE_COMPILER_DEFAULT=%s\n" prefix_var_name
242 (match Coq_config.native_compiler with
243 | Coq_config.NativeOn {ondemand=false} -> "yes"
244 | Coq_config.NativeOff -> "no"
245 | Coq_config.NativeOn {ondemand=true} -> "ondemand")
246
247 let query_getenv = function
248 | Boot -> assert false
249 | Env v -> v
250
251 let print_query envopt usage : Usage.query -> unit = function
252 | PrintVersion -> Usage.version ()
253 | PrintMachineReadableVersion -> Usage.machine_readable_version ()
254 | PrintWhere ->
255 let env = query_getenv envopt in
256 let coqlib = coqlib env |> Path.to_string in
257 print_endline coqlib
258 | PrintHelp -> begin match usage with
259 | Some usage -> Usage.print_usage stderr usage
260 | None -> assert false
261 end
262 | PrintConfig ->
263 let env = query_getenv envopt in
264 print_config env stdout
265
266 let query_needs_env : Usage.query -> string option = function
267 | PrintVersion | PrintMachineReadableVersion | PrintHelp -> None
268 | PrintWhere -> Some "-where"
269 | PrintConfig -> Some "-config"
270
271 let print_queries_maybe_init ~warn_ignored_coqlib ~boot ~coqlib usage = function
272 | [] -> Ok (maybe_init ~warn_ignored_coqlib ~boot ~coqlib)
273 | _ :: _ as qs ->
274 let needs_env = CList.find_map query_needs_env qs in
275 let res = match boot, needs_env with
276 | true, Some q -> Error ("Command line option -boot is not compatible with " ^ q ^ ".")
277 | true, None ->
278 (* warn_ignored_coqlib if coqlib and boot used together *)
279 Ok (maybe_init ~warn_ignored_coqlib ~boot ~coqlib)
280 | false, None -> Ok Boot
281 | false, Some _ -> Ok (Env (init_with ~coqlib))
282 in
283 let () = match res with
284 | Error _ -> ()
285 | Ok envopt -> List.iter (print_query envopt usage) qs
286 in
287 res