Debian Codegraph @

vernac/loadpath.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 module DP = Names.DirPath
13
14 (** Load paths. Mapping from physical to logical paths. *)
15
16 type t = {
17 path_physical : CUnix.physical_path;
18 path_logical : DP.t;
19 path_implicit : bool; (* true for -R, false for -Q in command line *)
20 path_installed : bool; (* true for automatically added paths (assumed installed), false for command line paths *)
21 }
22
23 let load_vos_libraries = ref false
24
25 let load_paths = Summary.ref ([] : t list) ~stage:Summary.Stage.Synterp ~name:"LOADPATHS"
26
27 let logical p = p.path_logical
28 let physical p = p.path_physical
29
30 let pp p =
31 let installed = Pp.str (if p.path_installed then "i" else " ") in
32 let dir = DP.print p.path_logical in
33 let path = Pp.str (CUnix.escaped_string_of_physical_path p.path_physical) in
34 Pp.(hov 2 (installed ++ spc () ++ dir ++ spc () ++ path))
35
36 let get_load_paths () = !load_paths
37
38 let anomaly_too_many_paths path =
39 CErrors.anomaly Pp.(str "Several logical paths are associated with" ++ spc () ++ str path ++ str ".")
40
41 let find_load_path phys_dir =
42 let phys_dir = CUnix.canonical_path_name phys_dir in
43 let filter p = String.equal p.path_physical phys_dir in
44 let paths = List.filter filter !load_paths in
45 match paths with
46 | [] -> raise Not_found
47 | [p] -> p
48 | _ -> anomaly_too_many_paths phys_dir
49
50 (* get the list of load paths that correspond to a given logical path *)
51 let find_with_logical_path dirpath =
52 List.filter (fun p -> Names.DirPath.equal p.path_logical dirpath) !load_paths
53
54 let warn_file_found_multiple_times =
55 CWarnings.create ~name:"ambiguous-extra-dep" ~category:CWarnings.CoreCategories.filesystem
56 (fun (file,from,other,extra) ->
57 Pp.(str "File " ++ str file ++ str " found twice in " ++
58 Names.DirPath.print from ++ str":" ++ spc () ++ str other ++ str " (selected)," ++
59 spc() ++ str extra ++ str ".") )
60
61 let rec first_path_containing ?loc from file acc = function
62 | [] ->
63 begin match acc with
64 | Some x -> x
65 | None ->
66 CErrors.user_err Pp.(str "File " ++ str file ++ str " not found in " ++
67 Names.DirPath.print from ++ str".")
68 end
69 | x :: xs ->
70 let abspath = x ^ "/" ^ file in
71 if Sys.file_exists abspath then begin
72 match acc with
73 | None -> first_path_containing ?loc from file (Some abspath) xs
74 | Some other ->
75 warn_file_found_multiple_times ?loc (file,from,other,abspath);
76 first_path_containing ?loc from file acc xs
77 end else
78 first_path_containing ?loc from file acc xs
79
80 let find_extra_dep_with_logical_path ?loc ~from ~file () =
81 match find_with_logical_path from with
82 | _ :: _ as paths ->
83 let paths = List.map physical paths in
84 first_path_containing ?loc from file None paths
85 | [] -> CErrors.user_err
86 Pp.(str "No LoadPath found for " ++ Names.DirPath.print from ++ str".")
87
88
89 let remove_load_path dir =
90 let filter p = not (String.equal p.path_physical dir) in
91 load_paths := List.filter filter !load_paths
92
93 let warn_overriding_logical_loadpath =
94 CWarnings.create ~name:"overriding-logical-loadpath" ~category:CWarnings.CoreCategories.filesystem
95 (fun (phys_path, old_path, rocq_path) ->
96 Pp.(seq [str phys_path; strbrk " was previously bound to "
97 ; DP.print old_path; strbrk "; it is remapped to "
98 ; DP.print rocq_path]))
99
100 let add_load_path ~installed phys_path rocq_path ~implicit =
101 let phys_path = CUnix.canonical_path_name phys_path in
102 let filter p = String.equal p.path_physical phys_path in
103 let binding = {
104 path_logical = rocq_path;
105 path_physical = phys_path;
106 path_implicit = implicit;
107 path_installed = installed;
108 } in
109 match List.filter filter !load_paths with
110 | [] ->
111 load_paths := binding :: !load_paths
112 | [{ path_logical = old_path; path_implicit = old_implicit }] ->
113 let replace =
114 if DP.equal rocq_path old_path then
115 implicit <> old_implicit
116 else
117 let () =
118 (* Do not warn when overriding the default "-I ." path *)
119 if not (DP.equal old_path Libnames.default_root_prefix) then
120 warn_overriding_logical_loadpath (phys_path, old_path, rocq_path)
121 in
122 true in
123 if replace then
124 begin
125 remove_load_path phys_path;
126 load_paths := binding :: !load_paths;
127 end
128 | _ -> anomaly_too_many_paths phys_path
129
130 let filter_path f =
131 let rec aux = function
132 | [] -> []
133 | p :: l ->
134 if f p.path_logical then (p.path_physical, p.path_logical) :: aux l
135 else aux l
136 in
137 aux !load_paths
138
139 let add_path ~is_installed file (installed, local) =
140 if is_installed then file :: installed, local
141 else installed, file::local
142
143 let expand_path ?root dir =
144 let exact_path = match root with None -> dir | Some root -> Libnames.append_dirpath root dir in
145 let aux { path_physical = ph; path_logical = lg; path_implicit = implicit; path_installed } (full, others) =
146 if DP.equal exact_path lg then
147 (* Most recent full match comes first *)
148 let full_installed, full_local = full in
149 let v = (ph, lg) in
150 let full = if path_installed then v :: full_installed, full_local
151 else full_installed, v :: full_local
152 in
153 full, others
154 else
155 let success =
156 match root with
157 | None -> implicit && Libnames.is_dirpath_suffix_of dir lg
158 | Some root ->
159 Libnames.(is_dirpath_prefix_of root lg &&
160 is_dirpath_suffix_of dir (drop_dirpath_prefix root lg))
161 in
162 if success then
163 full, add_path ~is_installed:path_installed (ph, lg) others
164 else
165 full, others in
166 let full, others = List.fold_right aux !load_paths (([],[]), ([], [])) in
167 (* Returns the dirpath matching exactly and the ordered list of
168 -R/-Q blocks with subdirectories that matches *)
169 full, others
170
171 let locate_file fname =
172 let paths = List.map physical !load_paths in
173 let _,longfname =
174 System.find_file_in_path ~warn:(not !Flags.quiet) paths fname in
175 longfname
176
177 (************************************************************************)
178 (*s Locate absolute or partially qualified library names in the path *)
179
180 module Error = struct
181 type t = LibUnmappedDir | LibNotFound
182
183 let unmapped_dir qid =
184 let prefix, _ = Libnames.repr_qualid qid in
185 CErrors.user_err
186 Pp.(seq [ str "Cannot load "; Libnames.pr_qualid qid; str ":"; spc ()
187 ; str "no physical path bound to"; spc ()
188 ; Names.DirPath.print prefix; fnl ()
189 ])
190
191 let lib_not_found dir =
192 let vos = !load_vos_libraries in
193 let vos_msg = if vos then [Pp.str " (while searching for a .vos file)"] else [] in
194 CErrors.user_err
195 Pp.(seq ([ str "Cannot find library "; Names.DirPath.print dir; str" in loadpath"]@vos_msg))
196
197 let raise dp = function
198 | LibUnmappedDir ->
199 unmapped_dir (Libnames.qualid_of_dirpath dp)
200 | LibNotFound ->
201 lib_not_found dp
202 end
203
204 (* If [!Flags.load_vos_libraries]
205 and the .vos file exists
206 and this file is not empty
207 Then load this library
208 Else load the .vo file or raise error if both are missing *)
209 let select_vo_file ~find base =
210 let find ext =
211 try
212 let name = Names.Id.to_string base ^ ext in
213 let lpath, file = find name in
214 Some (lpath, file)
215 with Not_found -> None in
216 if !load_vos_libraries
217 then begin
218 match find ".vos" with
219 | Some (_, vos) as resvos when (Unix.stat vos).Unix.st_size > 0 -> resvos
220 | _ -> find ".vo"
221 end
222 else find ".vo"
223
224 let find_first loadpath base =
225 match System.all_in_path loadpath base with
226 | [] -> raise Not_found
227 | f :: _ -> f
228
229 let find_unique fullqid loadpath base =
230 match System.all_in_path loadpath base with
231 | [] -> raise Not_found
232 | [f] -> f
233 | _::_ as l ->
234 CErrors.user_err Pp.(str "Required library " ++ Libnames.pr_qualid fullqid ++
235 strbrk " matches several files in path (found " ++ pr_enum str (List.map snd l) ++ str ").")
236
237 let locate_absolute_library dir : (CUnix.physical_path, Error.t) Result.t =
238 (* Search in loadpath *)
239 let pref, base = Libnames.split_dirpath dir in
240 let loadpath = filter_path (fun dir -> DP.equal dir pref) in
241 match loadpath with
242 | [] -> Error LibUnmappedDir
243 | _ ->
244 match select_vo_file ~find:(find_first loadpath) base with
245 | Some (_, file) -> Ok file
246 | None -> Error Error.LibNotFound
247
248 let locate_qualified_library ?root qid :
249 (DP.t * CUnix.physical_path, Error.t) Result.t =
250 (* Search library in loadpath *)
251 let dir, base = Libnames.repr_qualid qid in
252 match expand_path ?root dir with
253 | ([], []), ([], []) -> Error LibUnmappedDir
254 | (full_installed, full_local), (installed, local) ->
255 let result =
256 (* Priority to exact matches, then priority to local matches *)
257 List.find_map (fun block -> select_vo_file ~find:(find_unique qid block) base)
258 [full_local; full_installed; local; installed]
259 in
260 match result with
261 | Some (dir,file) ->
262 let library = Libnames.add_dirpath_suffix dir base in
263 Ok (library, file)
264 | None -> Error Error.LibNotFound
265
266 let warn_deprecated_missing_stdlib =
267 CWarnings.create ~name:"deprecated-missing-stdlib"
268 ~category:Deprecation.Version.v9_0
269 (fun qid ->
270 Pp.(str "Loading Stdlib without prefix is deprecated." ++ spc ()
271 ++ str "Use \"From Stdlib Require " ++ Libnames.pr_qualid qid
272 ++ str "\"" ++ spc() ++ str "or the deprecated \"From Coq Require "
273 ++ Libnames.pr_qualid qid ++ str "\"" ++ spc ()
274 ++ str "for compatibility with older Coq versions."))
275
276 (* temporary handling deprecated loading of stdlib without root *)
277 let locate_qualified_library ?root qid =
278 let root_stdlib =
279 Names.(Libnames.add_dirpath_suffix DirPath.empty (Id.of_string "Stdlib")) in
280 match root, locate_qualified_library ?root qid with
281 | Some _, r | None, (Ok _ as r) -> r
282 | None, (Error _ as e) ->
283 match locate_qualified_library ~root:root_stdlib qid with
284 | Error _ -> e
285 | Ok _ as o -> warn_deprecated_missing_stdlib ?loc:qid.loc qid; o
286
287 (** { 5 Extending the load path } *)
288
289 type vo_path =
290 { unix_path : string
291 ; coq_path : DP.t
292 ; implicit : bool
293 ; recursive : bool
294 ; installed : bool
295 }
296
297 let warn_cannot_open_path =
298 CWarnings.create ~name:"cannot-open-path" ~category:CWarnings.CoreCategories.filesystem
299 (fun unix_path -> Pp.(str "Cannot open " ++ str unix_path))
300
301 let warn_cannot_use_directory =
302 CWarnings.create ~name:"cannot-use-directory" ~category:CWarnings.CoreCategories.filesystem
303 (fun d ->
304 Pp.(str "Directory " ++ str d ++
305 strbrk " cannot be used as a Rocq identifier (skipped)"))
306
307 let convert_string d =
308 try Names.Id.of_string d
309 with
310 | CErrors.UserError _ ->
311 let d = Unicode.escaped_if_non_utf8 d in
312 warn_cannot_use_directory d;
313 raise_notrace Exit
314
315 let add_vo_path lp =
316 let unix_path = lp.unix_path in
317 let implicit = lp.implicit in
318 let recursive = lp.recursive in
319 let installed = lp.installed in
320 if System.exists_dir unix_path then
321 let dirs = if recursive then System.all_subdirs ~unix_path else [] in
322 let dirs = List.sort (fun a b -> String.compare (fst a) (fst b)) dirs in
323 let prefix = DP.repr lp.coq_path in
324 let convert_dirs (lp, cp) =
325 try
326 let path = List.rev_map convert_string cp @ prefix in
327 Some (lp, DP.make path)
328 with Exit -> None
329 in
330 let dirs = List.map_filter convert_dirs dirs in
331 let add (path, dir) = add_load_path ~installed path ~implicit dir in
332 (* deeper dirs registered first and thus be found last *)
333 let dirs = List.rev dirs in
334 let () = List.iter add dirs in
335 add_load_path ~installed unix_path ~implicit lp.coq_path
336 else
337 warn_cannot_open_path unix_path