Skip to content

Commit

Permalink
feat: support libraries with the same name in multiple contexts
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Chávarri <javier.chavarri@gmail.com>
Signed-off-by: Antonio Nuno Monteiro <anmonteiro@gmail.com>
  • Loading branch information
jchavarri authored and anmonteiro committed Mar 26, 2024
1 parent f1d8223 commit 02b7186
Show file tree
Hide file tree
Showing 35 changed files with 935 additions and 305 deletions.
3 changes: 2 additions & 1 deletion bin/describe/describe_workspace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,10 @@ module Crawl = struct
| true ->
(* XXX why do we have a second object directory? *)
let* modules_, obj_dir_ =
let sentinel = Lib.sentinel lib in
Dir_contents.get sctx ~dir:(Path.as_in_build_dir_exn src_dir)
>>= Dir_contents.ocaml
>>| Ml_sources.modules_and_obj_dir ~for_:(Library name)
>>| Ml_sources.modules_and_obj_dir ~for_:(Library sentinel)
in
let* pp_map =
let+ version =
Expand Down
18 changes: 12 additions & 6 deletions src/dune_rules/dir_contents.ml
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,19 @@ end
include Load

let modules_of_local_lib sctx lib =
let info = Lib.Local.info lib in
let* t =
let dir = Lib_info.src_dir info in
get sctx ~dir
let+ sources =
let* t =
let info = Lib.Local.info lib in
let dir = Lib_info.src_dir info in
get sctx ~dir
in
ocaml t
in
let sentinel =
let lib = Lib.Local.to_lib lib in
Lib.sentinel lib
in
let name = Lib_info.name info in
ocaml t >>| Ml_sources.modules ~for_:(Library name)
Ml_sources.modules sources ~for_:(Library sentinel)
;;

let modules_of_lib sctx lib =
Expand Down
23 changes: 17 additions & 6 deletions src/dune_rules/dune_package.ml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ module Lib = struct
let entry_modules = Modules.entry_modules modules |> List.map ~f:Module.name in
let info : Path.t Lib_info.t =
let src_dir = Obj_dir.dir obj_dir in
let sentinel =
Lib_info.Sentinel.external_ ~loc ~src_dir ~enabled_if:Blang.true_ name
in
let enabled = Memo.return Lib_info.Enabled_status.Normal in
let status =
match Lib_name.analyze name with
Expand All @@ -252,6 +255,7 @@ module Lib = struct
~path_kind:External
~loc
~name
~sentinel
~kind
~status
~src_dir
Expand Down Expand Up @@ -358,12 +362,12 @@ end
module Entry = struct
type t =
| Library of Lib.t
| Deprecated_library_name of Deprecated_library_name.t
| Deprecated_library_name of Path.t * Deprecated_library_name.t
| Hidden_library of Lib.t

let name = function
| Library lib | Hidden_library lib -> Lib_info.name (Lib.info lib)
| Deprecated_library_name d -> d.old_public_name
| Deprecated_library_name (_, d) -> d.old_public_name
;;

let version = function
Expand All @@ -373,7 +377,14 @@ module Entry = struct

let loc = function
| Library lib | Hidden_library lib -> Lib_info.loc (Lib.info lib)
| Deprecated_library_name d -> d.loc
| Deprecated_library_name (_, d) -> d.loc
;;

let sentinel = function
| Library lib | Hidden_library lib ->
let info = Lib.info lib in
Lib_info.sentinel info
| Deprecated_library_name _ -> assert false
;;

let cstrs ~lang ~dir =
Expand All @@ -383,15 +394,15 @@ module Entry = struct
Library lib )
; ( "deprecated_library_name"
, let+ x = Deprecated_library_name.decode in
Deprecated_library_name x )
Deprecated_library_name (dir, x) )
]
;;

let to_dyn x =
let open Dyn in
match x with
| Library lib -> variant "Library" [ Lib.to_dyn lib ]
| Deprecated_library_name lib ->
| Deprecated_library_name (_, lib) ->
variant "Deprecated_library_name" [ Deprecated_library_name.to_dyn lib ]
| Hidden_library lib -> variant "Hidden_library" [ Lib.to_dyn lib ]
;;
Expand Down Expand Up @@ -516,7 +527,7 @@ let encode ~dune_version { entries; name; version; dir; sections; sites; files }
match e with
| Entry.Library lib ->
list (Dune_lang.atom "library" :: Lib.encode lib ~package_root:dir ~stublibs)
| Deprecated_library_name d ->
| Deprecated_library_name (_, d) ->
list (Dune_lang.atom "deprecated_library_name" :: Deprecated_library_name.encode d)
| Hidden_library lib ->
Code_error.raise
Expand Down
3 changes: 2 additions & 1 deletion src/dune_rules/dune_package.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end
module Entry : sig
type t =
| Library of Lib.t
| Deprecated_library_name of Deprecated_library_name.t
| Deprecated_library_name of Path.t * Deprecated_library_name.t
| Hidden_library of Lib.t
(** Only for external libraries that:
Expand All @@ -53,6 +53,7 @@ module Entry : sig
Dune itself never produces hidden libraries. *)

val name : t -> Lib_name.t
val sentinel : t -> Lib_info.Sentinel.t
val version : t -> Package_version.t option
val loc : t -> Loc.t
val to_dyn : t Dyn.builder
Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/expander.ml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ let expand_lib_variable t source ~lib ~file ~lib_exec ~lib_private =
then Resolve.Memo.map p ~f:(fun _ -> assert false)
else
let open Resolve.Memo.O in
Lib.DB.available (Scope.libs scope) lib
Lib.DB.available_by_name (Scope.libs scope) lib
|> Resolve.Memo.lift_memo
>>= function
| false ->
Expand Down Expand Up @@ -653,7 +653,7 @@ let expand_pform_macro
(let lib = Lib_name.parse_string_exn (Dune_lang.Template.Pform.loc source, s) in
let open Memo.O in
let* scope = t.scope in
let+ available = Lib.DB.available (Scope.libs scope) lib in
let+ available = Lib.DB.available_by_name (Scope.libs scope) lib in
available |> string_of_bool |> string))
| Bin_available ->
Need_full_expander
Expand Down
17 changes: 12 additions & 5 deletions src/dune_rules/findlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ end
let builtin_for_dune : Dune_package.t =
let entry =
Dune_package.Entry.Deprecated_library_name
{ loc = Loc.of_pos __POS__
; old_public_name = Lib_name.of_string "dune.configurator"
; new_public_name = Lib_name.of_string "dune-configurator"
}
( Path.external_ Path.External.initial_cwd
, { loc = Loc.of_pos __POS__
; old_public_name = Lib_name.of_string "dune.configurator"
; new_public_name = Lib_name.of_string "dune-configurator"
} )
in
{ name = Opam_package.Name.of_string "dune"
; entries = Lib_name.Map.singleton (Dune_package.Entry.name entry) entry
Expand Down Expand Up @@ -206,10 +207,16 @@ let to_dune_library (t : Findlib.Package.t) ~dir_contents ~ext_lib ~external_loc
| Error e -> Error e))))
in
let modules = Lib_info.Source.External None in
let name = t.name in
let sentinel =
let enabled_if = Blang.true_ in
Lib_info.Sentinel.external_ ~loc ~src_dir ~enabled_if name
in
Lib_info.create
~loc
~path_kind:External
~name:t.name
~name
~sentinel
~kind
~status
~src_dir
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/gen_meta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let gen ~(package : Package.t) ~add_directory_entry entries =
let+ pkgs =
Memo.parallel_map entries ~f:(fun (e : Scope.DB.Lib_entry.t) ->
match e with
| Library lib ->
| Library (_, lib) ->
let info = Lib.Local.info lib in
let pub_name =
let name = Lib_info.name info in
Expand Down
5 changes: 4 additions & 1 deletion src/dune_rules/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ end = struct
let+ () = Toplevel.Stanza.setup ~sctx ~dir ~toplevel in
empty_none
| Library.T lib ->
let* enabled_if = Lib.DB.available (Scope.libs scope) (Library.best_name lib) in
let* enabled_if =
let sentinel = Library.to_sentinel ~src_dir lib in
Lib.DB.available (Scope.libs scope) sentinel
in
if_available_buildable
~loc:lib.buildable.loc
(fun () -> Lib_rules.rules lib ~sctx ~dir ~scope ~dir_contents ~expander)
Expand Down
39 changes: 26 additions & 13 deletions src/dune_rules/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ end = struct
>>| List.singleton
;;

let lib_files ~dir_contents ~dir ~lib_config lib =
let lib_files ~dir_contents ~dir ~lib_config ~sentinel lib =
let+ modules =
let+ ml_sources = Dir_contents.ocaml dir_contents in
Some (Ml_sources.modules ml_sources ~for_:(Library (Lib_info.name lib)))
Some (Ml_sources.modules ml_sources ~for_:(Library sentinel))
and+ foreign_archives =
match Lib_info.virtual_ lib with
| None -> Memo.return (Mode.Map.Multi.to_flat_list @@ Lib_info.foreign_archives lib)
Expand Down Expand Up @@ -179,9 +179,13 @@ end = struct
~lib_config
in
let lib_name = Library.best_name lib in
let sentinel =
let src_dir = Path.drop_optional_build_context_src_exn (Path.build dir) in
Library.to_sentinel ~src_dir lib
in
let* installable_modules =
let+ modules =
Dir_contents.ocaml dir_contents >>| Ml_sources.modules ~for_:(Library lib_name)
Dir_contents.ocaml dir_contents >>| Ml_sources.modules ~for_:(Library sentinel)
and+ impl = Virtual_rules.impl sctx ~lib ~scope in
Vimpl.impl_modules impl modules |> Modules.split_by_lib
in
Expand Down Expand Up @@ -305,7 +309,7 @@ end = struct
if Module.kind m = Virtual then [] else common m |> set_dir m)
in
modules_vlib @ modules_impl
and+ lib_files = lib_files ~dir ~dir_contents ~lib_config info
and+ lib_files = lib_files ~dir ~dir_contents ~lib_config ~sentinel info
and+ execs = lib_ppxs ctx ~scope ~lib
and+ dll_files =
dll_files ~modes:ocaml ~dynlink:lib.dynlink ~ctx info
Expand All @@ -330,15 +334,20 @@ end = struct
]
;;

let keep_if expander ~scope stanza =
let keep_if ~expander ~scope ~dir stanza =
let+ keep =
match Stanza.repr stanza with
| Library.T lib ->
let* enabled_if = Expander.eval_blang expander lib.enabled_if in
if enabled_if
then
if lib.optional
then Lib.DB.available (Scope.libs scope) (Library.best_name lib)
then (
let sentinel =
let src_dir = Path.drop_optional_build_context_src_exn (Path.build dir) in
Library.to_sentinel ~src_dir lib
in
Lib.DB.available (Scope.libs scope) sentinel)
else Memo.return true
else Memo.return false
| Documentation.T _ -> Memo.return true
Expand Down Expand Up @@ -448,7 +457,7 @@ end = struct
;;

let stanza_to_entries ~package_db ~sctx ~dir ~scope ~expander stanza =
(let+ stanza = keep_if expander stanza ~scope in
(let+ stanza = keep_if ~expander ~scope ~dir stanza in
let open Option.O in
let* stanza = stanza in
let+ package = Stanzas.stanza_package stanza in
Expand Down Expand Up @@ -620,8 +629,8 @@ end = struct
(Some
( old_public_name
, Dune_package.Entry.Deprecated_library_name
{ loc; old_public_name; new_public_name } ))
| Library lib ->
(Path.build pkg_root, { loc; old_public_name; new_public_name }) ))
| Library (sentinel, lib) ->
let info = Lib.Local.info lib in
let dir = Lib_info.src_dir info in
let* dir_contents = Dir_contents.get sctx ~dir in
Expand Down Expand Up @@ -653,7 +662,8 @@ end = struct
|> Foreign.Sources.object_files ~dir ~ext_obj
|> List.map ~f:Path.build
and* modules =
Dir_contents.ocaml dir_contents >>| Ml_sources.modules ~for_:(Library name)
Dir_contents.ocaml dir_contents
>>| Ml_sources.modules ~for_:(Library sentinel)
and* melange_runtime_deps = file_deps (Lib_info.melange_runtime_deps info)
and* public_headers = file_deps (Lib_info.public_headers info) in
let+ dune_lib =
Expand Down Expand Up @@ -730,6 +740,9 @@ end = struct
acc
>>>
let dune_pkg =
let dir =
Path.build (Install.Context.lib_dir ~context:ctx.name ~package:name)
in
let entries =
match Package.Name.Map.find deprecated_dune_packages name with
| None -> Lib_name.Map.empty
Expand All @@ -751,13 +764,13 @@ end = struct
acc
old_public_name
(Dune_package.Entry.Deprecated_library_name
{ loc; old_public_name; new_public_name }))
(dir, { loc; old_public_name; new_public_name })))
in
let sections = sections ctx.name [] pkg in
{ Dune_package.version = Package.version pkg
; name
; entries
; dir = Path.build (Install.Context.lib_dir ~context:ctx.name ~package:name)
; dir
; sections
; sites = Package.sites pkg
; files = []
Expand Down Expand Up @@ -798,7 +811,7 @@ end = struct
let* () = Action_builder.return () in
match
List.find_map entries ~f:(function
| Library lib ->
| Library (_, lib) ->
let info = Lib.Local.info lib in
Option.some_if (Option.is_some (Lib_info.virtual_ info)) lib
| Deprecated_library_name _ -> None)
Expand Down
Loading

0 comments on commit 02b7186

Please sign in to comment.