Debian Codegraph @

lib/envars.ml

raw symbols SCIP
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 open Util
12
13 (** {1 Helper functions} *)
14
15 let warn_deprecated_coq_var = CWarnings.create ~name:"deprecated-coq-env-var" ~category:Deprecation.Version.v9_0
16 Pp.(fun (rocq,coq) ->
17 str "Deprecated environment variable " ++ str coq ++ pr_comma() ++
18 str "use " ++ str rocq ++ str " instead.")
19
20 let warn_deprecated_coq_var ?loc ~rocq ~coq () = warn_deprecated_coq_var ?loc (rocq,coq)
21
22 let () = Boot.Util.set_warn_deprecated_coq_var (fun ~rocq ~coq ->
23 warn_deprecated_coq_var ~rocq ~coq ())
24
25 let getenv_else s dft =
26 match Boot.Util.getenv_opt s with
27 | Some v -> v
28 | None -> dft ()
29
30 let getenv_rocq = Boot.Util.getenv_rocq
31
32 let safe_getenv warning n =
33 getenv_else n (fun () ->
34 warning ("Environment variable "^n^" not found: using '$"^n^"' .");
35 ("$"^n)
36 )
37
38 let ( / ) a b =
39 if Filename.is_relative b then a ^ "/" ^ b else b
40
41 let coqify d = d / "coq"
42
43 let home ~warn =
44 getenv_else "HOME" (fun () ->
45 try (Sys.getenv "HOMEDRIVE")^(Sys.getenv "HOMEPATH") with Not_found ->
46 getenv_else "USERPROFILE" (fun () ->
47 warn ("Cannot determine user home directory, using '.' .");
48 Filename.current_dir_name))
49
50 let path_to_list p =
51 let sep = if String.equal Sys.os_type "Win32" then ';' else ':' in
52 String.split_on_char sep p
53
54 let expand_path_macros ~warn s =
55 let rec expand_atom s i =
56 let l = String.length s in
57 if i<l && (Util.is_digit s.[i] || Util.is_letter s.[i] || s.[i] == '_')
58 then expand_atom s (i+1)
59 else i in
60 let rec expand_macros s i =
61 let l = String.length s in
62 if Int.equal i l then s else
63 match s.[i] with
64 | '$' ->
65 let n = expand_atom s (i+1) in
66 let v = safe_getenv warn (String.sub s (i+1) (n-i-1)) in
67 let s = (String.sub s 0 i)^v^(String.sub s n (l-n)) in
68 expand_macros s (i + String.length v)
69 | '~' when Int.equal i 0 ->
70 let n = expand_atom s (i+1) in
71 let v =
72 if Int.equal n (i + 1) then home ~warn
73 else (Unix.getpwnam (String.sub s (i+1) (n-i-1))).Unix.pw_dir
74 in
75 let s = v^(String.sub s n (l-n)) in
76 expand_macros s (String.length v)
77 | c -> expand_macros s (i+1)
78 in expand_macros s 0
79
80 (** {1 Paths} *)
81
82 (** {2 Rocq paths} *)
83
84 (** Add a local installation suffix (unless the suffix is itself
85 absolute in which case the prefix does not matter) *)
86 let use_suffix suffix =
87 if String.length suffix > 0 && suffix.[0] = '/' then suffix
88 else Boot.Env.relocate (Relocatable suffix)
89
90 (* XXX do we actually need the local_suffix? *)
91 let relocate local_suffix path =
92 (* This assumes implicitly that the suffix is non-trivial *)
93 let local_path = use_suffix local_suffix in
94 if Sys.file_exists local_path
95 then local_path
96 else Boot.Env.relocate path
97
98 let datadir () = relocate Coq_config.datadirsuffix Coq_config.datadir
99
100 let configdir () = relocate Coq_config.configdirsuffix Coq_config.configdir
101
102 let coqpath () =
103 let make_search_path path =
104 let paths = path_to_list path in
105 let valid_paths = List.filter Sys.file_exists paths in
106 List.rev valid_paths
107 in
108 let rocqpath = getenv_else "ROCQPATH" (fun () -> "") in
109 let coqpath = getenv_else "COQPATH" (fun () -> "") in
110 let coqpath = if String.equal coqpath rocqpath then "" else coqpath in
111 let () = if coqpath <> "" then warn_deprecated_coq_var ~rocq:"ROCQPATH" ~coq:"COQPATH" () in
112 make_search_path rocqpath @ make_search_path coqpath
113
114 (** {1 XDG utilities} *)
115
116 let xdg_data_home warn =
117 coqify
118 (getenv_else "XDG_DATA_HOME" (fun () -> (home ~warn) / ".local/share"))
119
120 let xdg_config_home warn =
121 coqify
122 (getenv_else "XDG_CONFIG_HOME" (fun () -> (home ~warn) / ".config"))
123
124 let xdg_data_dirs warn =
125 let sys_dirs =
126 try
127 List.map coqify (path_to_list (Sys.getenv "XDG_DATA_DIRS"))
128 with
129 | Not_found -> [datadir ()]
130 in
131 xdg_data_home warn :: sys_dirs
132
133 let xdg_dirs ~warn =
134 List.filter Sys.file_exists (xdg_data_dirs warn)