···77 - [lib_to_pkg]: reverse index for fast [package_of] lookups, since
88 callers (e.g. monopam.lint dead-lib check) only have a library name. *)
991010+type origin = Local | Opam
1111+1012type t = {
1113 pkg_libs : (string, Dune.Lib_index.t) Hashtbl.t;
1414+ pkg_origin : (string, origin) Hashtbl.t;
1215 lib_to_pkg : (string, string) Hashtbl.t;
1316}
1417···5760 Hashtbl.replace t.pkg_libs pkg i;
5861 i
59626060-let scan_pkg_dir t pkg pkg_dir =
6363+let scan_pkg_dir ~origin t pkg pkg_dir =
6464+ let registered = ref false in
6565+ let register () =
6666+ if not !registered then begin
6767+ Hashtbl.replace t.pkg_origin pkg origin;
6868+ registered := true
6969+ end
7070+ in
6171 match load_eio_path Eio.Path.(pkg_dir / "dune-package") with
6272 | Some content ->
7373+ register ();
6374 let i = pkg_index t pkg in
6475 ignore (Dune.Lib_index.add_dune_package i content);
6576 List.iter
···6980 match cmi_modules_in_dir pkg_dir with
7081 | [] -> ()
7182 | modules ->
8383+ register ();
7284 let i = pkg_index t pkg in
7385 ignore (Dune.Lib_index.add_cmi_modules i ~pkg ~modules);
7486 Hashtbl.replace t.lib_to_pkg pkg pkg)
75877688let build ~fs ~monorepo =
7777- let t = { pkg_libs = Hashtbl.create 256; lib_to_pkg = Hashtbl.create 512 } in
7878- let scan root =
8989+ let t =
9090+ {
9191+ pkg_libs = Hashtbl.create 256;
9292+ pkg_origin = Hashtbl.create 256;
9393+ lib_to_pkg = Hashtbl.create 512;
9494+ }
9595+ in
9696+ let scan ~origin root =
7997 let entries = try Eio.Path.read_dir root with Eio.Io _ -> [] in
8080- List.iter (fun pkg -> scan_pkg_dir t pkg Eio.Path.(root / pkg)) entries
9898+ List.iter
9999+ (fun pkg -> scan_pkg_dir ~origin t pkg Eio.Path.(root / pkg))
100100+ entries
81101 in
82102 let opam_lib =
83103 Eio.Path.(fs / Fpath.to_string Fpath.(monorepo / "_opam" / "lib"))
···88108 / Fpath.to_string
89109 Fpath.(monorepo / "_build" / "install" / "default" / "lib"))
90110 in
9191- scan opam_lib;
9292- scan build_lib;
111111+ scan ~origin:Opam opam_lib;
112112+ scan ~origin:Local build_lib;
93113 t
94114115115+let origin t pkg = Hashtbl.find_opt t.pkg_origin pkg
116116+95117let packages t =
96118 Hashtbl.fold (fun k _ acc -> k :: acc) t.pkg_libs []
97119 |> List.sort String.compare
···111133 match Hashtbl.find_opt t.pkg_libs pkg with
112134 | None -> []
113135 | Some i -> Dune.Lib_index.modules i lib)
136136+137137+let is_virtual_implementation t lib =
138138+ match package_of t lib with
139139+ | None -> false
140140+ | Some pkg -> (
141141+ match Hashtbl.find_opt t.pkg_libs pkg with
142142+ | None -> false
143143+ | Some i -> Dune.Lib_index.is_virtual_implementation i lib)
+23-1
lib/index/monopam_info_index.mli
···1818 recover the [pkg.subpkg] tree, but that needs the [Meta] module to not
1919 collide with [compiler-libs.Meta] in mdx-built executables. *)
20202121+type origin =
2222+ | Local
2323+ | Opam
2424+ (** Where a package was discovered:
2525+ - [Local]: built locally and installed under
2626+ [<monorepo>/_build/install/default/lib/]. Takes precedence over an
2727+ opam-installed copy of the same package.
2828+ - [Opam]: only present in [<monorepo>/_opam/lib/], i.e. an external
2929+ opam dependency. *)
3030+2131type t
2232(** Snapshot of the libraries available in a monorepo's opam switch. *)
2333···2535(** [build ~fs ~monorepo] scans both [<monorepo>/_opam/lib/] and
2636 [<monorepo>/_build/install/default/lib/] and aggregates everything into a
2737 single index. The build-install root is scanned second so locally built
2828- packages override opam-installed ones. *)
3838+ packages override opam-installed ones, and their {!origin} comes out as
3939+ [Local]. *)
29403041(** {1 Querying by opam package} *)
31423243val packages : t -> string list
3344(** [packages t] is the sorted list of opam package names known to [t]. *)
4545+4646+val origin : t -> string -> origin option
4747+(** [origin t pkg] is [Some Local] / [Some Opam] for known packages, or [None]
4848+ if [pkg] is not in the index. *)
34493550val libraries : t -> string -> string list
3651(** [libraries t pkg] is the sorted list of dune library names provided by opam
···4661(** [modules t lib] is the list of top-level module names exposed by dune
4762 library [lib], or [[]] if [lib] is unknown. Module names are capitalised, in
4863 the form [Dune.Package.Library.t.modules] returns. *)
6464+6565+val is_virtual_implementation : t -> string -> bool
6666+(** [is_virtual_implementation t lib] is [true] iff [lib] has an
6767+ [(implements X)] entry in its [dune-package]. Such libraries are link-time
6868+ live even when none of their modules appear in source. Always [false] for
6969+ libraries discovered via META, since findlib has no notion of virtual
7070+ libraries. *)