Debian Codegraph @

tools/coqdep/lib/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 (* Common types *)
12 type basename = string
13 type dirname = string
14 type dir = string option
15 type dirpath = string list
16 type filename = string
17 type root = filename * dirpath
18
19 (** File operations *)
20 let absolute_dir dir =
21 let current = Sys.getcwd () in
22 Sys.chdir dir;
23 let dir' = Sys.getcwd () in
24 Sys.chdir current;
25 dir'
26
27 let absolute_file_name ~filename_concat basename odir =
28 let dir = match odir with Some dir -> dir | None -> "." in
29 (* XXX: Attention to System.(//) which does weird things and it is
30 not the same than Filename.concat ; using Filename.concat here
31 makes the windows build fail *)
32 filename_concat (absolute_dir dir) basename
33
34 (** [find_dir_logpath dir] Return the logical path of directory [dir]
35 if it has been given one. Raise [Not_found] otherwise. In
36 particular we can check if "." has been attributed a logical path
37 after processing all options and silently give the default one if
38 it hasn't. We may also use this to warn if a physical path is met
39 twice. *)
40 let register_dir_logpath, find_dir_logpath =
41 let tbl: (string, string list) Hashtbl.t = Hashtbl.create 19 in
42 let reg physdir logpath = Hashtbl.add tbl (absolute_dir physdir) logpath in
43 let fnd physdir = Hashtbl.find tbl (absolute_dir physdir) in
44 reg,fnd
45
46 (** Visit all the directories under [dir], including [dir], in the
47 same order as for [coqc]/[coqtop] in [System.all_subdirs], that
48 is, assuming Sys.readdir to have this structure:
49 ├── B
50 │ └── E.v
51 │ └── C1
52 │ │ └── E.v
53 │ │ └── D1
54 │ │ └── E.v
55 │ │ └── F.v
56 │ │ └── D2
57 │ │ └── E.v
58 │ │ └── G.v
59 │ └── F.v
60 │ └── C2
61 │ │ └── E.v
62 │ │ └── D1
63 │ │ └── E.v
64 │ │ └── F.v
65 │ │ └── D2
66 │ │ └── E.v
67 │ │ └── G.v
68 │ └── G.v
69 it goes in this (reverse) order:
70 B.C2.D1.E, B.C2.D2.E,
71 B.C2.E, B.C2.F, B.C2.G
72 B.C1.D1.E, B.C1.D2.E,
73 B.C1.E, B.C1.F, B.C1.G,
74 B.E, B.F, B.G,
75 (see discussion at PR #14718)
76 *)
77
78 let add_directory recur add_file phys_dir log_dir =
79 let root = (phys_dir, log_dir) in
80 let stack = ref [] in
81 let curdirfiles = ref [] in
82 let subdirfiles = ref [] in
83 let rec aux phys_dir log_dir =
84 if System.exists_dir phys_dir then
85 begin
86 register_dir_logpath phys_dir log_dir;
87 let f = function
88 | System.FileDir (phys_f,f) ->
89 if recur then begin
90 stack := (!curdirfiles, !subdirfiles) :: !stack;
91 curdirfiles := []; subdirfiles := [];
92 aux phys_f (log_dir @ [f]);
93 let curdirfiles', subdirfiles' = List.hd !stack in
94 subdirfiles := subdirfiles' @ !subdirfiles @ !curdirfiles;
95 curdirfiles := curdirfiles'; stack := List.tl !stack
96 end
97 | System.FileRegular f ->
98 curdirfiles := (phys_dir, log_dir, f) :: !curdirfiles
99 in
100 System.process_directory f phys_dir
101 end
102 else
103 System.warn_cannot_open_dir phys_dir
104 in
105 aux phys_dir log_dir;
106 List.iter (fun (phys_dir, log_dir, f) -> add_file root phys_dir log_dir f) !subdirfiles;
107 List.iter (fun (phys_dir, log_dir, f) -> add_file root phys_dir log_dir f) !curdirfiles
108
109 (** [get_extension f l] checks whether [f] has one of the extensions
110 listed in [l]. It returns [f] without its extension, alongside with
111 the extension. When no extension match, [(f,"")] is returned *)
112
113 let rec get_extension f = function
114 | [] -> (f, "")
115 | s :: _ when Filename.check_suffix f s -> (Filename.chop_suffix f s, s)
116 | _ :: l -> get_extension f l
117
118 (** Compute the suffixes of a logical path together with the length of the missing part *)
119 let rec suffixes full = function
120 | [] -> assert false
121 | [name] -> [full,[name]]
122 | dir::suffix as l -> (full,l)::suffixes false suffix
123
124 (** Compute all the pairs [(from,suffs)] such that a logical path
125 decomposes into [from @ ... @ suff] for some [suff] in [suffs],
126 i.e. such that once [from] is fixed, [From from Require suff]
127 refers (in the absence of ambiguity) to this logical path for
128 exactly the [suff] in [suffs] *)
129 let rec cuts recur = function
130 | [] -> []
131 | [dir] ->
132 [[],[true,[dir]]]
133 | dir::tail as l ->
134 ([],if recur then suffixes true l else [true,l]) ::
135 List.map (fun (fromtail,suffixes) -> (dir::fromtail,suffixes)) (cuts true tail)
136
137 module Filename =
138 struct
139
140 type t = {
141 user : filename;
142 absolute : filename;
143 }
144
145 let make s = {
146 user = s;
147 absolute = absolute_file_name ~filename_concat:Filename.concat (Filename.basename s) (Some (Filename.dirname s));
148 }
149
150 let compare f1 f2 = String.compare f1.absolute f2.absolute
151
152 let repr f = f.user
153
154 end
155
156 module FileSet = Set.Make(Filename)
157
158 type fileset = {
159 point : filename;
160 files : FileSet.t; (* guaranteed to contain [point] *)
161 }
162
163 type result =
164 | ExactMatches of fileset
165 | PartialMatchesInSameRoot of root * fileset
166
167 module State = struct
168
169 type t =
170 { vfiles : (dirpath * dirpath, result) Hashtbl.t
171 ; coqlib : (dirpath * dirpath, result) Hashtbl.t
172 ; other : (dirpath * dirpath, result) Hashtbl.t
173 ; coqlibother : (dirpath * dirpath, result) Hashtbl.t
174 ; boot : bool
175 ; mutable worker : string option
176 }
177
178 let make ~worker ~boot =
179 { vfiles = Hashtbl.create 4101
180 ; coqlib = Hashtbl.create 19
181 ; other = Hashtbl.create 17317
182 ; coqlibother = Hashtbl.create 17317
183 ; boot
184 ; worker
185 }
186
187 end
188
189 let get_worker_path st =
190 match st.State.worker with
191 | Some w -> w
192 | None ->
193 let w = Fl.Internal.get_worker_path () in
194 st.worker <- Some w;
195 w
196
197 let singleton f = { point = f; files = FileSet.singleton (Filename.make f) }
198 let add_set f l = { point = f; files = FileSet.add (Filename.make f) l.files }
199
200 let insert_key root (full,f) m =
201 (* An exact match takes precedence over non-exact matches *)
202 match full, m with
203 | true, ExactMatches l -> (* We add a conflict *) ExactMatches (add_set f l)
204 | true, PartialMatchesInSameRoot _ -> (* We give priority to exact match *) ExactMatches (singleton f)
205 | false, ExactMatches l -> (* We keep the exact match *) m
206 | false, PartialMatchesInSameRoot (root',l) ->
207 PartialMatchesInSameRoot (root, if root = root' then add_set f l else (singleton f))
208
209 let safe_add_key q root key (full,f as file) =
210 try
211 let l = Hashtbl.find q key in
212 Hashtbl.add q key (insert_key root file l)
213 with Not_found ->
214 Hashtbl.add q key (if full then ExactMatches (singleton f) else PartialMatchesInSameRoot (root, singleton f))
215
216 let safe_add q root ((from, suffixes), file) =
217 List.iter (fun (full,suff) -> safe_add_key q root (from,suff) (full,file)) suffixes
218
219 let search_table table ?(from=[]) s =
220 Hashtbl.find table (from, s)
221
222 let search_v_known st ?from s =
223 try Some (search_table st.State.vfiles ?from s)
224 with Not_found -> None
225
226 let search_other_known st ?from s =
227 try Some (search_table st.State.other ?from s)
228 with Not_found -> None
229
230 let is_in_coqlib st ?from s =
231 try let _ = search_table st.State.coqlib ?from s in true
232 with Not_found -> match from with Some _ -> false | None ->
233 try let _ = search_table st.State.coqlib ~from:["Stdlib"] s in true
234 with Not_found -> false
235
236 let is_other_in_coqlib st ~from s =
237 try let _ = search_table st.State.coqlibother ~from s in true
238 with Not_found -> false
239
240 let add_paths recur root table phys_dir log_dir basename =
241 let name = log_dir@[basename] in
242 let file = System.(phys_dir // basename) in
243 let paths = cuts recur name in
244 let iter n = safe_add table root (n, file) in
245 List.iter iter paths
246
247 let add_coqlib_known st recur root phys_dir log_dir f =
248 let root = (phys_dir, log_dir) in
249 match get_extension f [".vo"; ".vos"] with
250 | (basename, (".vo" | ".vos")) ->
251 add_paths recur root st.State.coqlib phys_dir log_dir basename
252 | (f,_) ->
253 add_paths recur root st.State.coqlibother phys_dir log_dir f
254
255 let add_known st recur root phys_dir log_dir f =
256 match get_extension f [".v"; ".vo"; ".vos"] with
257 | (basename,".v") ->
258 add_paths recur root st.State.vfiles phys_dir log_dir basename
259 | (basename, (".vo" | ".vos")) when not st.State.boot ->
260 add_paths recur root st.State.vfiles phys_dir log_dir basename
261 | (f,_) ->
262 add_paths recur root st.State.other phys_dir log_dir f
263
264 (** Simply add this directory and imports it, no subdirs. This is used
265 by the implicit adding of the current path (which is not recursive). *)
266 let add_norec_dir_import add_file phys_dir log_dir =
267 add_directory false (add_file true) phys_dir log_dir
268
269 (** -Q semantic: go in subdirs but only full logical paths are known. *)
270 let add_rec_dir_no_import add_file phys_dir log_dir =
271 add_directory true (add_file false) phys_dir log_dir
272
273 (** -R semantic: go in subdirs and suffixes of logical paths are known. *)
274 let add_rec_dir_import add_file phys_dir log_dir =
275 add_directory true (add_file true) phys_dir log_dir
276
277 let split_period = Str.split (Str.regexp (Str.quote "."))
278
279 let add_current_dir st dir = add_norec_dir_import (add_known st) dir []
280 let add_q_include st path l = add_rec_dir_no_import (add_known st) path (split_period l)
281 let add_r_include st path l = add_rec_dir_import (add_known st) path (split_period l)