Skip to content

Commit

Permalink
allow (wrapped (as name)) in library
Browse files Browse the repository at this point in the history
This allows to specify the name of the top level module of a wrapped
library. Useful to be able to produce different libs with the same
module and then specify a dependency on either of these to choose the
implementation.
  • Loading branch information
andreypopp committed Aug 20, 2023
1 parent 2bb0a0f commit 4c682dc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/dune_lang/wrapped.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Dune_sexp.Decoder

type t =
| Simple of bool
| Yes_as of string
| Yes_with_transition of string

let equal = Poly.equal
Expand All @@ -15,24 +16,31 @@ let decode =
, Dune_sexp.Syntax.since Stanza.syntax (1, 2)
>>> let+ x = string in
Yes_with_transition x )
; ( "as"
, Dune_sexp.Syntax.since Stanza.syntax (3, 9)
>>> let+ x = string in
Yes_as x )
]
;;

let encode =
let open Dune_sexp.Encoder in
function
| Simple b -> bool b
| Yes_as name -> pair string string ("as", name)
| Yes_with_transition m -> pair string string ("transition", m)
;;

let to_bool = function
| Simple b -> b
| Yes_as _ -> true
| Yes_with_transition _ -> true
;;

let to_dyn =
let open Dyn in
function
| Simple s -> variant "Simple" [ bool s ]
| Yes_as name -> variant "Yes_as" [ string name ]
| Yes_with_transition s -> variant "Yes_with_transition" [ string s ]
;;
1 change: 1 addition & 0 deletions src/dune_lang/wrapped.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type t =
| Simple of bool
| Yes_as of string
| Yes_with_transition of string

val equal : t -> t -> bool
Expand Down
2 changes: 2 additions & 0 deletions src/dune_rules/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,8 @@ module Library = struct
| Some _ -> This (Some (Module_name.of_local_lib_name t.name)))
| None, This (Simple true | Yes_with_transition _) ->
This (Some (Module_name.of_local_lib_name t.name))
| None, This (Yes_as name) ->
This (Some (Module_name.of_string name))
;;

let to_lib_info
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ let gen_wrapped_compat_modules (lib : Library.t) cctx =
let transition_message =
lazy
(match Modules.wrapped modules with
| Simple _ -> assert false
| Simple _ | Yes_as _ -> assert false
| Yes_with_transition r -> r)
in
Module_name.Map_traversals.parallel_iter wrapped_compat ~f:(fun name m ->
Expand Down
20 changes: 10 additions & 10 deletions src/dune_rules/modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ module Stdlib = struct

let make ~(stdlib : Ocaml_stdlib.t) ~modules ~wrapped ~main_module_name =
let modules =
match wrapped with
| Wrapped.Simple true | Yes_with_transition _ ->
match Wrapped.to_bool wrapped with
| true ->
Module_name.Map.map modules ~f:(fun m ->
if Module.name m = main_module_name || special_compiler_module stdlib m
then m
else (
let path = [ main_module_name; Module.name m ] in
let m = Module.set_path m path in
Module.set_obj_name m (Module_name.Path.wrap path)))
| Simple false -> modules
| false -> modules
in
let unwrapped = stdlib.modules_before_stdlib in
let exit_module = stdlib.exit_module in
Expand Down Expand Up @@ -713,7 +713,7 @@ module Wrapped = struct
let wrapped_compat =
match (wrapped : Dune_lang.Wrapped.t) with
| Simple false -> assert false
| Simple true -> Module_name.Map.empty
| Simple true | Yes_as _ -> Module_name.Map.empty
| Yes_with_transition _ ->
let toplevel = Module_trie.toplevel_only modules in
Module_name.Map.remove toplevel main_module_name
Expand Down Expand Up @@ -893,18 +893,18 @@ let lib ~obj_dir ~main_module_name ~wrapped ~stdlib ~lib_name ~implements ~modul
let modules = Module_trie.to_map modules in
Stdlib (Stdlib.make ~stdlib ~modules ~wrapped ~main_module_name)
| None ->
(match wrapped, main_module_name, Module_trie.as_singleton modules with
| Simple false, _, Some m -> Singleton m
| Simple false, _, None ->
(match Dune_lang.Wrapped.to_bool wrapped, main_module_name, Module_trie.as_singleton modules with
| false, _, Some m -> Singleton m
| false, _, None ->
let mangle = Mangle.Unwrapped in
Unwrapped (Unwrapped.of_trie modules ~mangle ~obj_dir)
| (Yes_with_transition _ | Simple true), Some main_module_name, Some m ->
| true, Some main_module_name, Some m ->
if Module.name m = main_module_name && not implements
then Singleton m
else make_wrapped main_module_name
| (Yes_with_transition _ | Simple true), Some main_module_name, None ->
| true, Some main_module_name, None ->
make_wrapped main_module_name
| (Simple true | Yes_with_transition _), None, _ ->
| true, None, _ ->
Code_error.raise "Modules.lib: cannot wrap without main module name" [])
in
with_obj_map modules
Expand Down

0 comments on commit 4c682dc

Please sign in to comment.