Skip to content

Commit

Permalink
Updates following PR review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonludlam committed Nov 11, 2024
1 parent 9f12fc6 commit 7dd3fb2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/driver/odoc_driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,14 @@ let run mode
let extra_paths = Voodoo.extra_paths odoc_dir in
(all, extra_paths, actions)
| Dune { path } ->
(Dune_style.of_dune_build path, Util.StringMap.(empty, empty), All)
(Dune_style.of_dune_build path, Voodoo.empty_extra_paths, All)
| Opam { packages; packages_dir } ->
let libs = if packages = [] then Ocamlfind.all () else packages in
let libs =
List.map Ocamlfind.sub_libraries libs
|> List.fold_left Util.StringSet.union Util.StringSet.empty
in
(Packages.of_libs ~packages_dir libs, Util.StringMap.(empty, empty), All)
(Packages.of_libs ~packages_dir libs, Voodoo.empty_extra_paths, All)
in

let virtual_check =
Expand Down
6 changes: 4 additions & 2 deletions src/driver/odoc_units_of.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
open Odoc_unit

let packages ~dirs ~extra_paths:(extra_pkg_paths, extra_libs_paths)
(pkgs : Packages.t list) : t list =
let packages ~dirs ~extra_paths (pkgs : Packages.t list) : t list =
let { odoc_dir; odocl_dir; index_dir; mld_dir = _ } = dirs in
(* [module_of_hash] Maps a hash to the corresponding [Package.t], library name and
[Packages.modulety]. [lib_dirs] maps a library name to the odoc dir containing its
odoc files. *)
let extra_libs_paths = extra_paths.Voodoo.libs in
let extra_pkg_paths = extra_paths.Voodoo.pkgs in

let module_of_hash, lib_dirs =
let open Packages in
let h = Util.StringMap.empty in
Expand Down
5 changes: 1 addition & 4 deletions src/driver/odoc_units_of.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
open Odoc_unit

val packages :
dirs:dirs ->
extra_paths:Fpath.t Util.StringMap.t * Fpath.t Util.StringMap.t ->
Packages.t list ->
t list
dirs:dirs -> extra_paths:Voodoo.extra_paths -> Packages.t list -> t list
77 changes: 49 additions & 28 deletions src/driver/voodoo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type pkg = {

let prep_path = ref "prep"

(* We mark the paths that contain compiled units for both packages and libraries
by dropping in a marker file. The contents of the file is unimportant, as we
can determine which package or library we're looking at simply by its path. *)
let lib_marker = ".odoc_lib_marker"
let pkg_marker = ".odoc_pkg_marker"

Expand Down Expand Up @@ -249,7 +252,7 @@ let of_voodoo pkg_name ~blessed =
in
match contents with
| Error _ -> Util.StringMap.empty
| Ok c ->
| Ok c -> (
let sorted = List.sort (fun p1 p2 -> Fpath.compare p1 p2) c in
let last, packages =
List.fold_left
Expand All @@ -276,40 +279,58 @@ let of_voodoo pkg_name ~blessed =
(None, []) sorted
in
let packages = List.filter_map (fun x -> x) (last :: packages) in
let packages = List.map process_package packages in
let pkg = List.hd packages in
Logs.debug (fun m -> m "Package: %a\n%!" Packages.pp pkg);
Util.StringMap.singleton pkg_name (List.hd packages)
match packages with
| [ package ] ->
let package = process_package package in
Logs.debug (fun m -> m "Package: %a\n%!" Packages.pp package);
Util.StringMap.singleton pkg_name package
| [] ->
Logs.err (fun m -> m "No package found for %s" pkg_name);
Util.StringMap.empty
| _ ->
Logs.err (fun m -> m "Multiple packages found for %s" pkg_name);
Util.StringMap.empty)

type extra_paths = {
pkgs : Fpath.t Util.StringMap.t;
libs : Fpath.t Util.StringMap.t;
}

let empty_extra_paths =
{ pkgs = Util.StringMap.empty; libs = Util.StringMap.empty }

let extra_paths compile_dir =
let contents =
Bos.OS.Dir.fold_contents ~dotfiles:true
(fun p acc -> p :: acc)
[] compile_dir
in
match contents with
| Error _ -> (Util.StringMap.empty, Util.StringMap.empty)
| Ok c ->
List.fold_left
(fun (pkgs, libs) abs_path ->
let path = Fpath.rem_prefix compile_dir abs_path |> Option.get in
match Fpath.segs path with
| [ "p"; _pkg; _version; libname; l ] when l = lib_marker ->
Logs.debug (fun m -> m "Found lib marker: %a" Fpath.pp path);
(pkgs, Util.StringMap.add libname (Fpath.parent path) libs)
| [ "p"; pkg; _version; l ] when l = pkg_marker ->
Logs.debug (fun m -> m "Found pkg marker: %a" Fpath.pp path);
(Util.StringMap.add pkg (Fpath.parent path) pkgs, libs)
| [ "u"; _universe; _pkg; _version; libname; l ] when l = lib_marker
->
Logs.debug (fun m -> m "Found lib marker: %a" Fpath.pp path);
(pkgs, Util.StringMap.add libname (Fpath.parent path) libs)
| [ "u"; _universe; pkg; _version; l ] when l = pkg_marker ->
Logs.debug (fun m -> m "Found pkg marker: %a" Fpath.pp path);
(Util.StringMap.add pkg (Fpath.parent path) pkgs, libs)
| _ -> (pkgs, libs))
(Util.StringMap.empty, Util.StringMap.empty)
c
let pkgs, libs =
match contents with
| Error _ -> (Util.StringMap.empty, Util.StringMap.empty)
| Ok c ->
List.fold_left
(fun (pkgs, libs) abs_path ->
let path = Fpath.rem_prefix compile_dir abs_path |> Option.get in
match Fpath.segs path with
| [ "p"; _pkg; _version; libname; l ] when l = lib_marker ->
Logs.debug (fun m -> m "Found lib marker: %a" Fpath.pp path);
(pkgs, Util.StringMap.add libname (Fpath.parent path) libs)
| [ "p"; pkg; _version; l ] when l = pkg_marker ->
Logs.debug (fun m -> m "Found pkg marker: %a" Fpath.pp path);
(Util.StringMap.add pkg (Fpath.parent path) pkgs, libs)
| [ "u"; _universe; _pkg; _version; libname; l ] when l = lib_marker
->
Logs.debug (fun m -> m "Found lib marker: %a" Fpath.pp path);
(pkgs, Util.StringMap.add libname (Fpath.parent path) libs)
| [ "u"; _universe; pkg; _version; l ] when l = pkg_marker ->
Logs.debug (fun m -> m "Found pkg marker: %a" Fpath.pp path);
(Util.StringMap.add pkg (Fpath.parent path) pkgs, libs)
| _ -> (pkgs, libs))
(Util.StringMap.empty, Util.StringMap.empty)
c
in
{ pkgs; libs }

let write_lib_markers odoc_dir pkgs =
let write file str =
Expand Down
17 changes: 16 additions & 1 deletion src/driver/voodoo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ val find_universe_and_version :

val of_voodoo : string -> blessed:bool -> Packages.set

val extra_paths : Fpath.t -> Fpath.t Util.StringMap.t * Fpath.t Util.StringMap.t
type extra_paths = {
pkgs : Fpath.t Util.StringMap.t;
libs : Fpath.t Util.StringMap.t;
}

val empty_extra_paths : extra_paths
(** When [odoc_driver] is not running in voodoo mode, this value can be passed to
{!Odoc_units_of.packages} *)

val extra_paths : Fpath.t -> extra_paths
(** [extra_paths odoc_dir] returns the paths to packages and libraries that have previously
been compiled by odoc_driver running in voodoo mode. In order to find these,
the previous invocation of odoc_driver will need to have written marker files by
calling {!write_lib_markers} *)

val write_lib_markers : Fpath.t -> Packages.t Util.StringMap.t -> unit
(** [write_lib_markers odoc_dir pkgs] writes marker files to show the locations of the
compilation units associated with packages and libraries in [pkgs]. *)

0 comments on commit 7dd3fb2

Please sign in to comment.