Skip to content

Commit

Permalink
refactor: move library path handling to single to one place (#7728)
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg authored May 14, 2023
1 parent 9d2c36a commit 3c5a9ed
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
28 changes: 15 additions & 13 deletions src/dune_rules/expander.ml
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,23 @@ let expand_read_macro ~dir ~source s ~read ~pack =
let file_of_lib { Artifacts.Public_libs.context; public_libs } ~loc ~lib ~file =
let open Resolve.Memo.O in
let+ lib = Lib.DB.resolve public_libs (loc, lib) in
if Lib.is_local lib then
let package, rest = Lib_name.split (Lib.name lib) in
let lib_install_dir =
let lib_install_dir =
let dir =
let info = Lib.info lib in
match Lib.is_local lib with
| false -> Lib_info.src_dir info
| true ->
let name = Lib.name lib in
let subdir =
Lib_info.Status.relative_to_package (Lib_info.status info) name
|> Option.value_exn
in
let pkg_root =
let package = Lib_name.package_name name in
Local_install_path.lib_dir ~context:context.name ~package
in
match rest with
| [] -> lib_install_dir
| _ -> Path.Build.relative lib_install_dir (String.concat rest ~sep:"/")
in
Path.build (Path.Build.relative lib_install_dir file)
else
let info = Lib.info lib in
let src_dir = Lib_info.src_dir info in
Path.relative src_dir file
Path.build (Path.Build.append_local pkg_root subdir)
in
Path.relative dir file

let expand_lib_variable t source ~arg:s ~lib_exec ~lib_private =
let loc = Dune_lang.Template.Pform.loc source in
Expand Down
16 changes: 5 additions & 11 deletions src/dune_rules/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -527,18 +527,12 @@ end = struct
in
let lib_root lib =
let subdir =
let name = Lib.name lib in
let _, subdir = Lib_name.split name in
match
let info = Lib.info lib in
Lib_info.status info
with
| Private (_, Some _) ->
Lib_name.Local.mangled_path_under_package (Lib_name.to_local_exn name)
@ subdir
| _ -> subdir
Lib_info.Status.relative_to_package
(Lib_info.status @@ Lib.info lib)
(Lib.name lib)
|> Option.value_exn
in
Path.Build.L.relative pkg_root subdir
Path.Build.append_local pkg_root subdir
in
let* entries =
Memo.parallel_map lib_entries ~f:(fun (stanza : Scope.DB.Lib_entry.t) ->
Expand Down
12 changes: 12 additions & 0 deletions src/dune_rules/lib_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ module Status = struct
let project = function
| Installed_private | Installed -> None
| Private (project, _) | Public (project, _) -> Some project

let relative_to_package t name =
match t with
| Private (_, None) -> None
| _ ->
(let _, subdir = Lib_name.split name in
match t with
| Private (_, Some _) ->
Lib_name.Local.mangled_path_under_package (Lib_name.to_local_exn name)
@ subdir
| _ -> subdir)
|> String.concat ~sep:"/" |> Path.Local.of_string |> Option.some
end

module Source = struct
Expand Down
4 changes: 4 additions & 0 deletions src/dune_rules/lib_info.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module Status : sig

(** For local libraries, return the project they are part of *)
val project : t -> Dune_project.t option

(** [relative_to_package t name] return the path of [name] relative to the
package determined by [t]. If there's no package, return [None] *)
val relative_to_package : t -> Lib_name.t -> Path.Local.t option
end

(** For values like modules that need to be evaluated to be fetched *)
Expand Down

0 comments on commit 3c5a9ed

Please sign in to comment.