|
| 1 | +open Import |
| 2 | +open Memo.O |
| 3 | + |
| 4 | +(* Encoded representation of a set of library names + scope *) |
| 5 | +module Key : sig |
| 6 | + type encoded = Digest.t |
| 7 | + |
| 8 | + module Decoded : sig |
| 9 | + type t = private |
| 10 | + { pps : Lib_name.t list |
| 11 | + ; project_root : Path.Source.t option |
| 12 | + } |
| 13 | + |
| 14 | + val of_libs : Lib.t list -> t |
| 15 | + end |
| 16 | + |
| 17 | + val encode : Decoded.t -> encoded |
| 18 | +end = struct |
| 19 | + type encoded = Digest.t |
| 20 | + |
| 21 | + module Decoded = struct |
| 22 | + type t = |
| 23 | + { pps : Lib_name.t list |
| 24 | + ; project_root : Path.Source.t option |
| 25 | + } |
| 26 | + |
| 27 | + let equal x y = |
| 28 | + List.equal Lib_name.equal x.pps y.pps |
| 29 | + && Option.equal Path.Source.equal x.project_root y.project_root |
| 30 | + ;; |
| 31 | + |
| 32 | + let to_string { pps; project_root } = |
| 33 | + let s = String.enumerate_and (List.map pps ~f:Lib_name.to_string) in |
| 34 | + match project_root with |
| 35 | + | None -> s |
| 36 | + | Some dir -> |
| 37 | + sprintf "%s (in project: %s)" s (Path.Source.to_string_maybe_quoted dir) |
| 38 | + ;; |
| 39 | + |
| 40 | + let of_libs libs = |
| 41 | + let pps = |
| 42 | + (let compare a b = Lib_name.compare (Lib.name a) (Lib.name b) in |
| 43 | + List.sort libs ~compare) |
| 44 | + |> List.map ~f:Lib.name |
| 45 | + in |
| 46 | + let project = |
| 47 | + List.fold_left libs ~init:None ~f:(fun acc lib -> |
| 48 | + let scope_for_key = |
| 49 | + let info = Lib.info lib in |
| 50 | + let status = Lib_info.status info in |
| 51 | + match status with |
| 52 | + | Private (scope_name, _) -> Some scope_name |
| 53 | + | Installed_private | Public _ | Installed -> None |
| 54 | + in |
| 55 | + Option.merge acc scope_for_key ~f:(fun a b -> |
| 56 | + assert (Dune_project.equal a b); |
| 57 | + a)) |
| 58 | + in |
| 59 | + { pps; project_root = Option.map project ~f:Dune_project.root } |
| 60 | + ;; |
| 61 | + end |
| 62 | + |
| 63 | + let reverse_table : (Digest.t, Decoded.t) Table.t = Table.create (module Digest) 128 |
| 64 | + |
| 65 | + let encode ({ Decoded.pps; project_root } as x) = |
| 66 | + let y = Digest.generic (pps, project_root) in |
| 67 | + match Table.find reverse_table y with |
| 68 | + | None -> |
| 69 | + Table.set reverse_table y x; |
| 70 | + y |
| 71 | + | Some x' -> |
| 72 | + if Decoded.equal x x' |
| 73 | + then y |
| 74 | + else |
| 75 | + User_error.raise |
| 76 | + [ Pp.textf "Hash collision between set of ppx drivers:" |
| 77 | + ; Pp.textf "- cache : %s" (Decoded.to_string x') |
| 78 | + ; Pp.textf "- fetch : %s" (Decoded.to_string x) |
| 79 | + ] |
| 80 | + ;; |
| 81 | +end |
| 82 | + |
| 83 | +let ppx_exe_path (ctx : Build_context.t) ~key = |
| 84 | + Path.Build.relative ctx.build_dir (".ppx/" ^ key ^ "/ppx.exe") |
| 85 | +;; |
| 86 | + |
| 87 | +let ppx_driver_exe (ctx : Context.t) libs = |
| 88 | + let key = Digest.to_string (Key.Decoded.of_libs libs |> Key.encode) in |
| 89 | + Context.host ctx >>| Context.build_context >>| ppx_exe_path ~key |
| 90 | +;; |
| 91 | + |
| 92 | +let get_ppx_exe ctx ~scope pps = |
| 93 | + let open Resolve.Memo.O in |
| 94 | + let* libs = Lib.DB.resolve_pps (Scope.libs scope) pps in |
| 95 | + ppx_driver_exe ctx libs |> Resolve.Memo.lift_memo |
| 96 | +;; |
0 commit comments