@@ -24,14 +24,6 @@ type t =
24
24
; lib : Path .t
25
25
}
26
26
27
- (* fatal error *)
28
- let fail_msg =
29
- " cannot guess a path for Rocq libraries; please use -coqlib option \
30
- or ensure you have installed the package containing Rocq's prelude (rocq-core in OPAM) \
31
- If you intend to use Rocq without prelude, the -boot -noinit options must be used."
32
-
33
- let fail s = Format. eprintf " %s@\n %!" fail_msg; exit 1
34
-
35
27
(* This code needs to be refactored, for now it is just what used to be in envvars *)
36
28
37
29
let use_suffix prefix suffix =
@@ -54,21 +46,54 @@ let theories_dir = "theories"
54
46
let plugins_dir = " plugins"
55
47
let prelude = Filename. concat theories_dir " Init/Prelude.vo"
56
48
57
- let coqbin =
58
- canonical_path_name (Filename. dirname Sys. executable_name)
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)
59
66
60
67
(* * The following only makes sense when executables are running from
61
68
source tree (e.g. during build or in local mode). *)
62
- let coqroot =
63
- Filename. dirname coqbin
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
64
89
65
90
(* * [check_file_else ~dir ~file oth] checks if [file] exists in
66
91
the installation directory [dir] given relatively to [coqroot],
67
92
which maybe has been relocated.
68
93
If the check fails, then [oth ()] is evaluated.
69
94
Using file system equality seems well enough for this heuristic *)
70
95
let check_file_else ~dir ~file oth =
71
- let path = use_suffix coqroot dir in
96
+ let path = use_suffix rocqroot dir in
72
97
if Sys. file_exists (Filename. concat path file) then path else oth ()
73
98
74
99
let guess_coqlib () =
@@ -78,12 +103,10 @@ let guess_coqlib () =
78
103
check_file_else
79
104
~dir: Coq_config. coqlibsuffix
80
105
~file: prelude
81
- (fun () ->
82
- if Sys. file_exists (Filename. concat Coq_config. coqlib prelude)
83
- then Coq_config. coqlib
84
- else fail () )
106
+ (fun () -> relocate Coq_config. coqlib)
85
107
86
- (* Build layout uses coqlib = coqcorelib *)
108
+ (* Build layout uses coqlib = coqcorelib
109
+ XXX we should be using -boot in build layout so is that dead code? *)
87
110
let guess_coqcorelib lib =
88
111
if Sys. file_exists (Path. relative lib plugins_dir)
89
112
then lib
@@ -161,8 +184,8 @@ let ocamlfind () = match Util.getenv_opt "OCAMLFIND" with
161
184
162
185
let docdir () =
163
186
(* This assumes implicitly that the suffix is non-trivial *)
164
- let path = use_suffix coqroot Coq_config. docdirsuffix in
165
- if Sys. file_exists path then path else Coq_config. docdir
187
+ let path = use_suffix rocqroot Coq_config. docdirsuffix in
188
+ if Sys. file_exists path then path else relocate Coq_config. docdir
166
189
167
190
(* Print the configuration information *)
168
191
0 commit comments