| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type vAccu = { acc : (string * string) list; map : string list CString.Map.t } |
| 16 |
|
| 17 |
let add_vAccu (f, f') vAccu = |
| 18 |
let acc = (f, f') :: vAccu.acc in |
| 19 |
let old = try CString.Map.find f' vAccu.map with Not_found -> [] in |
| 20 |
let map = CString.Map.add f' (f :: old) vAccu.map in |
| 21 |
{ acc; map } |
| 22 |
|
| 23 |
let empty_vAccu = { acc = []; map = CString.Map.empty } |
| 24 |
|
| 25 |
let filename_concat ~separator_hack dir name = |
| 26 |
if separator_hack |
| 27 |
then System.(dir // name) |
| 28 |
else Filename.concat dir name |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
let canonize ~separator_hack vAccu f = |
| 35 |
let f' = filename_concat ~separator_hack |
| 36 |
(Loadpath.absolute_dir (Filename.dirname f)) |
| 37 |
(Filename.basename f) |
| 38 |
in |
| 39 |
match CString.Map.find_opt f' vAccu.map with |
| 40 |
| None | Some [] -> f |
| 41 |
| Some (f :: _) -> f |
| 42 |
|
| 43 |
type what = Library | External |
| 44 |
let str_of_what = function Library -> "library" | External -> "external file" |
| 45 |
|
| 46 |
let warning_module_notfound = |
| 47 |
let warn (what, from, f, s) = |
| 48 |
let open Pp in |
| 49 |
str "in file " ++ str f ++ str ", " ++ |
| 50 |
str (str_of_what what) ++ spc () ++ str (String.concat "." s) ++ str " is required" ++ |
| 51 |
pr_opt (fun pth -> str "from root " ++ str (String.concat "." pth)) from ++ |
| 52 |
str " and has not been found in the loadpath!" |
| 53 |
in |
| 54 |
CWarnings.create ~name:"module-not-found" |
| 55 |
~category:CWarnings.CoreCategories.filesystem warn |
| 56 |
|
| 57 |
let warn_if_clash ?(what=Library) exact file dir f1 = let open Format in function |
| 58 |
| f2::fl -> |
| 59 |
let f = |
| 60 |
match what with |
| 61 |
| Library -> Filename.basename f1 ^ ".v" |
| 62 |
| External -> Filename.basename f1 in |
| 63 |
let what = str_of_what what in |
| 64 |
let d1 = Filename.dirname f1 in |
| 65 |
let d2 = Filename.dirname f2 in |
| 66 |
let dl = List.rev_map Filename.dirname fl in |
| 67 |
if exact then |
| 68 |
begin |
| 69 |
eprintf |
| 70 |
"*** Warning: in file %s, \n required %s %s exactly matches several files in path\n (found %s in " |
| 71 |
file what (String.concat "." dir) f; |
| 72 |
List.iter (fun s -> eprintf "%s, " s) dl; |
| 73 |
eprintf "%s and %s; used the latter).\n" d2 d1 |
| 74 |
end |
| 75 |
else |
| 76 |
begin |
| 77 |
eprintf |
| 78 |
"*** Warning: in file %s, \n required %s %s matches several files in path\n (found %s in " |
| 79 |
file what (String.concat "." dir) f; |
| 80 |
List.iter (fun s -> eprintf "%s, " s) dl; |
| 81 |
eprintf "%s and %s; Require will fail).\n" d2 d1 |
| 82 |
end |
| 83 |
| [] -> () |
| 84 |
|
| 85 |
let safe_assoc ?(warn_clashes=true) st ?(what=Library) from file k = |
| 86 |
let search = |
| 87 |
match what with |
| 88 |
| Library -> Loadpath.search_v_known st |
| 89 |
| External -> Loadpath.search_other_known st in |
| 90 |
match search ?from k with |
| 91 |
| None -> None |
| 92 |
| Some (Loadpath.ExactMatches fs) -> |
| 93 |
let f = fs.Loadpath.point in |
| 94 |
let l = Loadpath.FileSet.elements fs.files in |
| 95 |
let l = List.map Loadpath.Filename.repr l in |
| 96 |
let l = List.filter (fun f' -> not (String.equal f f')) l in |
| 97 |
if warn_clashes then warn_if_clash ~what true file k f l; |
| 98 |
Some [f] |
| 99 |
| Some (Loadpath.PartialMatchesInSameRoot (root, l)) -> |
| 100 |
let l = Loadpath.FileSet.elements l.files in |
| 101 |
let l = List.map Loadpath.Filename.repr l in |
| 102 |
(match List.sort String.compare l with [] -> assert false | f :: l as all -> |
| 103 |
|
| 104 |
|
| 105 |
if warn_clashes then warn_if_clash ~what false file k f l; |
| 106 |
Some all) |
| 107 |
|
| 108 |
let file_name ~separator_hack s = function |
| 109 |
| None -> s |
| 110 |
| Some d -> filename_concat ~separator_hack d s |
| 111 |
|
| 112 |
module VData = struct |
| 113 |
type t = string list option * string list |
| 114 |
let compare = compare |
| 115 |
end |
| 116 |
|
| 117 |
module VCache = Set.Make(VData) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
let declare_ml_to_file file (decl : string) = |
| 129 |
let legacy_decl = String.split_on_char ':' decl in |
| 130 |
match legacy_decl with |
| 131 |
| [package] -> |
| 132 |
Fl.findlib_deep_resolve ~file ~package |
| 133 |
| [cmxs; package] -> |
| 134 |
|
| 135 |
Fl.findlib_deep_resolve ~file ~package |
| 136 |
| bad_pkg -> |
| 137 |
CErrors.user_err Pp.(str "Failed to resolve plugin: " ++ str decl) |
| 138 |
|
| 139 |
let coq_to_stdlib from strl = |
| 140 |
let tr_qualid = function |
| 141 |
| "Coq" :: l -> "Stdlib" :: l |
| 142 |
| l -> l in |
| 143 |
match from with |
| 144 |
| Some from -> Some (tr_qualid from), strl |
| 145 |
| None -> None, List.map tr_qualid strl |
| 146 |
|
| 147 |
let with_in_channel ~fname f = |
| 148 |
let chan = try open_in fname |
| 149 |
with Sys_error msg -> Error.cannot_open fname msg |
| 150 |
in |
| 151 |
Util.try_finally f chan close_in chan |
| 152 |
|
| 153 |
module State = struct |
| 154 |
type t = { |
| 155 |
loadpath : Loadpath.State.t; |
| 156 |
separator_hack : bool; |
| 157 |
vAccu : vAccu; |
| 158 |
} |
| 159 |
let loadpath x = x.loadpath |
| 160 |
end |
| 161 |
|
| 162 |
exception SyntaxErrorInFile of string |
| 163 |
|
| 164 |
|
| 165 |
let rec find_dependencies ({State.vAccu; separator_hack; loadpath} as st) basename = |
| 166 |
|
| 167 |
let visited_ml = ref CString.Set.empty in |
| 168 |
let visited_v = ref VCache.empty in |
| 169 |
let should_visit_v_and_mark from str = |
| 170 |
if not (VCache.mem (from, str) !visited_v) then begin |
| 171 |
visited_v := VCache.add (from, str) !visited_v; |
| 172 |
true |
| 173 |
end else false |
| 174 |
in |
| 175 |
|
| 176 |
let module DepSet = Dep_info.Dep.Set in |
| 177 |
let dependencies = ref DepSet.empty in |
| 178 |
let add_dep dep = dependencies := DepSet.add dep !dependencies in |
| 179 |
let add_dep_other s = add_dep (Dep_info.Dep.Other s) in |
| 180 |
|
| 181 |
|
| 182 |
let () = add_dep_other (Loadpath.get_worker_path loadpath) in |
| 183 |
|
| 184 |
|
| 185 |
let f = basename ^ ".v" in |
| 186 |
with_in_channel ~fname:f @@ fun chan -> |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
let buf = Lexing.from_channel ~with_positions:false chan in |
| 193 |
let open Lexer in |
| 194 |
let rec loop () = |
| 195 |
match coq_action buf with |
| 196 |
| exception Fin_fichier -> |
| 197 |
DepSet.elements !dependencies |
| 198 |
| exception Syntax_error _ -> |
| 199 |
|
| 200 |
raise (SyntaxErrorInFile f) |
| 201 |
| tok -> match tok with |
| 202 |
| Require (from, strl) -> |
| 203 |
let from, strl = coq_to_stdlib from strl in |
| 204 |
let decl str = |
| 205 |
if should_visit_v_and_mark from str then begin |
| 206 |
let files = safe_assoc loadpath from f str in |
| 207 |
let files = match from, files with |
| 208 |
| Some _, _ | None, Some _ -> files |
| 209 |
| None, None -> safe_assoc loadpath (Some ["Stdlib"]) f str in |
| 210 |
match files with |
| 211 |
| Some files -> |
| 212 |
List.iter (fun file_str -> |
| 213 |
let file_str = canonize ~separator_hack vAccu file_str in |
| 214 |
add_dep (Dep_info.Dep.Require file_str)) files |
| 215 |
| None -> |
| 216 |
if not (Loadpath.is_in_coqlib loadpath ?from str) then |
| 217 |
warning_module_notfound (Library, from, f, str) |
| 218 |
end |
| 219 |
in |
| 220 |
List.iter decl strl; |
| 221 |
loop () |
| 222 |
| Declare sl -> |
| 223 |
|
| 224 |
let sl = List.map (declare_ml_to_file f) sl in |
| 225 |
let decl (meta_file, str) = |
| 226 |
List.iter add_dep_other meta_file; |
| 227 |
str |> List.iter (fun str -> |
| 228 |
let plugin_file = Filename.chop_extension str in |
| 229 |
if not (CString.Set.mem plugin_file !visited_ml) then begin |
| 230 |
visited_ml := CString.Set.add plugin_file !visited_ml; |
| 231 |
add_dep (Dep_info.Dep.Ml plugin_file) |
| 232 |
end) |
| 233 |
in |
| 234 |
List.iter decl sl; |
| 235 |
loop () |
| 236 |
| Load file -> |
| 237 |
let canon = |
| 238 |
match file with |
| 239 |
| Logical str -> |
| 240 |
if should_visit_v_and_mark None [str] then safe_assoc loadpath None f [str] |
| 241 |
else None |
| 242 |
| Physical str -> |
| 243 |
if String.equal (Filename.basename str) str then |
| 244 |
if should_visit_v_and_mark None [str] then safe_assoc loadpath None f [str] |
| 245 |
else None |
| 246 |
else |
| 247 |
Some [canonize ~separator_hack vAccu str] |
| 248 |
in |
| 249 |
(match canon with |
| 250 |
| None -> () |
| 251 |
| Some l -> |
| 252 |
let decl canon = |
| 253 |
add_dep_other (Format.sprintf "%s.v" canon); |
| 254 |
let deps = find_dependencies st canon in |
| 255 |
List.iter add_dep deps |
| 256 |
in |
| 257 |
List.iter decl l); |
| 258 |
loop () |
| 259 |
| External(from,str) -> |
| 260 |
begin match safe_assoc loadpath ~what:External (Some from) f [str] with |
| 261 |
| Some (file :: _) -> add_dep (Dep_info.Dep.Other (canonize ~separator_hack vAccu file)) |
| 262 |
| Some [] -> assert false |
| 263 |
| None -> |
| 264 |
if not (Loadpath.is_other_in_coqlib loadpath ~from [str]) then |
| 265 |
warning_module_notfound (External, Some from, f, [str]) |
| 266 |
end; |
| 267 |
loop () |
| 268 |
in |
| 269 |
loop () |
| 270 |
|
| 271 |
|
| 272 |
let get_parse_error f = |
| 273 |
with_in_channel ~fname:f @@ fun chan -> |
| 274 |
let buf = Lexing.from_channel chan in |
| 275 |
let rec loop () = match Lexer.coq_action buf with |
| 276 |
| _tok -> loop () |
| 277 |
| exception Lexer.Syntax_error (i, j) -> (i, j) |
| 278 |
| exception Lexer.Fin_fichier -> |
| 279 |
|
| 280 |
(0, 0) |
| 281 |
in |
| 282 |
loop () |
| 283 |
|
| 284 |
let find_dependencies st basename = |
| 285 |
try find_dependencies st basename |
| 286 |
with SyntaxErrorInFile f -> |
| 287 |
let (i, j) = get_parse_error f in |
| 288 |
Error.cannot_parse f (i, j) |
| 289 |
|
| 290 |
let compute_deps st = |
| 291 |
let mk_dep (name, _orig_path) = Dep_info.make ~name ~deps:(find_dependencies st name) in |
| 292 |
st.vAccu.acc |> CList.rev_map mk_dep |
| 293 |
|
| 294 |
let rec treat_file ~separator_hack vAccu old_dirname old_name = |
| 295 |
let name = Filename.basename old_name |
| 296 |
and new_dirname = Filename.dirname old_name in |
| 297 |
let dirname = |
| 298 |
match (old_dirname,new_dirname) with |
| 299 |
| (d, ".") -> d |
| 300 |
|
| 301 |
|
| 302 |
| (None,d) -> Some d |
| 303 |
| (Some d1,d2) -> Some (filename_concat ~separator_hack d1 d2) |
| 304 |
in |
| 305 |
let complete_name = file_name ~separator_hack name dirname in |
| 306 |
let stat_res = |
| 307 |
try Unix.stat complete_name |
| 308 |
with Unix.Unix_error(error, _, _) -> |
| 309 |
Error.cannot_open complete_name (Unix.error_message error) |
| 310 |
in |
| 311 |
match stat_res.Unix.st_kind with |
| 312 |
| Unix.S_DIR -> |
| 313 |
(if name.[0] = '.' then vAccu else |
| 314 |
let newdirname = |
| 315 |
match dirname with |
| 316 |
| None -> name |
| 317 |
| Some d -> filename_concat ~separator_hack d name |
| 318 |
in |
| 319 |
Array.fold_left (fun vAccu x -> treat_file ~separator_hack vAccu (Some newdirname) x) vAccu |
| 320 |
(Sys.readdir complete_name)) |
| 321 |
| Unix.S_REG -> |
| 322 |
(match Loadpath.get_extension name [".v"] with |
| 323 |
| base,".v" -> |
| 324 |
let name = file_name ~separator_hack base dirname in |
| 325 |
let filename_concat = filename_concat ~separator_hack in |
| 326 |
let absname = Loadpath.absolute_file_name ~filename_concat base dirname in |
| 327 |
add_vAccu (name, absname) vAccu |
| 328 |
| _ -> vAccu) |
| 329 |
| _ -> vAccu |
| 330 |
|
| 331 |
let treat_file_command_line ({State.vAccu; separator_hack} as st) old_name = |
| 332 |
let vAccu = treat_file ~separator_hack vAccu None old_name in |
| 333 |
{ st with State.vAccu } |
| 334 |
|
| 335 |
|
| 336 |
let sort {State.vAccu; separator_hack; loadpath} = |
| 337 |
let seen = Hashtbl.create 97 in |
| 338 |
let rec loop file = |
| 339 |
let file = canonize ~separator_hack vAccu file in |
| 340 |
if not (Hashtbl.mem seen file) then begin |
| 341 |
Hashtbl.add seen file (); |
| 342 |
let cin = open_in (file ^ ".v") in |
| 343 |
let lb = Lexing.from_channel cin in |
| 344 |
try |
| 345 |
while true do |
| 346 |
match Lexer.coq_action lb with |
| 347 |
| Lexer.Require (from, sl) -> |
| 348 |
List.iter |
| 349 |
(fun s -> |
| 350 |
match safe_assoc loadpath from ~warn_clashes:false file s with |
| 351 |
| None -> () |
| 352 |
| Some l -> List.iter loop l) |
| 353 |
sl |
| 354 |
| _ -> () |
| 355 |
done |
| 356 |
with Lexer.Fin_fichier -> |
| 357 |
close_in cin; |
| 358 |
Format.printf "%s.v " file |
| 359 |
end |
| 360 |
in |
| 361 |
List.iter (fun (name, _) -> loop name) vAccu.acc |
| 362 |
|
| 363 |
let add_include st (rc, r, ln) = |
| 364 |
if rc then |
| 365 |
Loadpath.add_r_include st r ln |
| 366 |
else |
| 367 |
Loadpath.add_q_include st r ln |
| 368 |
|
| 369 |
let findlib_init dirs = |
| 370 |
let env_ocamlpath = |
| 371 |
try [Sys.getenv "OCAMLPATH"] |
| 372 |
with Not_found -> [] |
| 373 |
in |
| 374 |
let env_ocamlpath = dirs @ env_ocamlpath in |
| 375 |
let ocamlpathsep = if Sys.unix then ":" else ";" in |
| 376 |
let env_ocamlpath = String.concat ocamlpathsep env_ocamlpath in |
| 377 |
Findlib.init ~env_ocamlpath () |
| 378 |
|
| 379 |
let init ~make_separator_hack args = |
| 380 |
if not Coq_config.has_natdynlink then Makefile.set_dyndep "no"; |
| 381 |
let loadpath = Loadpath.State.make ~worker:args.Args.worker ~boot:args.Args.boot in |
| 382 |
Makefile.set_write_vos args.Args.vos; |
| 383 |
Makefile.set_noglob args.Args.noglob; |
| 384 |
|
| 385 |
let ml_path = args.Args.ml_path in |
| 386 |
let rocqenv = Boot.Env.maybe_init ~boot:args.boot ~coqlib:args.coqlib |
| 387 |
~warn_ignored_coqlib:CWarnings.warn_ignored_coqlib |
| 388 |
in |
| 389 |
let ml_path = match rocqenv with |
| 390 |
| Boot -> ml_path |
| 391 |
| Env env -> |
| 392 |
ml_path @ Boot.Env.Path.[to_string @@ relative (Boot.Env.runtimelib env) ".."] |
| 393 |
in |
| 394 |
findlib_init ml_path; |
| 395 |
List.iter (add_include loadpath) args.Args.vo_path; |
| 396 |
Makefile.set_dyndep args.Args.dyndep; |
| 397 |
rocqenv, { State.vAccu = empty_vAccu; loadpath; separator_hack = make_separator_hack } |