Skip to content

Commit

Permalink
allow to use a private name when declaring external variants
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Pluvinage <lucas.pluvinage@gmail.com>
  • Loading branch information
TheLortex committed May 31, 2019
1 parent b7d5f68 commit 1ad1d27
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 34 deletions.
54 changes: 38 additions & 16 deletions src/lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1335,12 +1335,20 @@ module DB = struct
Errors.fail_opt loc
"Library %a isn't a virtual library."
Lib_name.pp vlib
| Some (Redirect (None, best_name)) ->
Errors.fail_opt loc
"To declare variants you must use %a's public name,@ which is %a."
Lib_name.pp vlib Lib_name.pp best_name
| _ -> ())

let error_two_impl_for_variant name (variant, (loc1, impl1), (loc2, impl2)) =
Errors.fail_opt None
"Error: Two implementations of %a have the same variant %a:\n\
- %a (%a)\n\
- %a (%a)\n"
Lib_name.pp name
Variant.pp variant
Lib_name.pp impl1
Loc.pp_file_colon_line loc1
Lib_name.pp impl2
Loc.pp_file_colon_line loc2

let create_from_library_stanzas ?parent ~lib_config lib_stanzas
variants_stanzas =
(* Lookup the local scope to find implementations*)
Expand All @@ -1365,25 +1373,39 @@ module DB = struct
|> Lib_name.Map.map ~f:Variant.Map.of_list
|> Lib_name.Map.mapi ~f:(fun name -> function
| Ok x -> x
| Error (variant, (loc1, impl1), (loc2, impl2)) ->
Errors.fail_opt None
"Error: Two implementations of %a have the same variant %a:\n\
- %a (%a)\n\
- %a (%a)\n"
Lib_name.pp name
Variant.pp variant
Lib_name.pp impl1
Loc.pp_file_colon_line loc1
Lib_name.pp impl2
Loc.pp_file_colon_line loc2)
| Error x -> error_two_impl_for_variant name x
)
in
let map =
List.concat_map lib_stanzas ~f:(fun (dir, (conf : Dune_file.Library.t)) ->
let variants =
let variants_public =
Dune_file.Library.best_name conf
|> Lib_name.Map.find variant_map
|> Option.value ~default:Variant.Map.empty
in
let variants_private =
conf.public
|> Option.map
~f:(fun p ->
let name = Dune_file.Public_lib.name p in
if (Lib_name.equal name (Lib_name.of_local conf.name)) then
Variant.Map.empty
else
(Lib_name.of_local conf.name)
|> Lib_name.Map.find variant_map
|> Option.value ~default:Variant.Map.empty
)
|> Option.value ~default:Variant.Map.empty
in
let variants = Variant.Map.merge variants_private variants_public
~f:(fun variant a b -> match a, b with
| Some a, Some b ->
error_two_impl_for_variant
(Dune_file.Library.best_name conf)
(variant, a, b)
| Some a, _ -> Some a
| _, b -> b)
in
let info = Lib_info.of_library_stanza ~dir ~lib_config variants conf in
match conf.public with
| None ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
(virtual_library libfoo)
(variant somevariant)
(implementation impl1))

(external_variant
(virtual_library another-name)
(variant somevariant)
(implementation impl2))
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ implementation with the same variant.
Error: Library libfoo isn't a virtual library.
[1]

$ dune build --root not-a-vlib-redirect
Entering directory 'not-a-vlib-redirect'
File "dune", line 5, characters 0-91:
5 | (external_variant
6 | (virtual_library libfoo)
7 | (variant somevariant)
8 | (implementation impl1))
Error: To declare variants you must use libfoo's public name,
which is another-name.
$ dune build --root public-private
Entering directory 'public-private'
Error: Two implementations of another-name have the same variant "somevariant":
- impl1 (dune:5)
- impl2 (dune:10)
[1]

$ dune build --root vlib-dont-exist
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(executable
(name exe)
(libraries vlibfoo)
(libraries vlibfoo-ext)
(variants somevariant))
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
(library
(name vlibfoo)
(public_name vlibfoo)
(public_name vlibfoo-ext)
(virtual_modules vlibfoo)
)

(external_variant
(virtual_library vlibfoo)
(variant somevariant)
(implementation impl))
(implementation impl))


(external_variant
(virtual_library vlibfoo-ext)
(variant somevariant-2)
(implementation impl-2))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(name impl)
(public_name impl)
(implements vlibfoo))
(implements vlibfoo-ext))
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ declared in the virtual library definition.

$ dune build exe/exe.exe

$ dune build -p vlibfoo
$ dune build -p vlibfoo-ext

$ cat _build/install/default/lib/vlibfoo/dune-package
$ cat _build/install/default/lib/vlibfoo-ext/dune-package
(lang dune 1.10)
(name vlibfoo)
(name vlibfoo-ext)
(library
(name vlibfoo)
(name vlibfoo-ext)
(kind normal)
(virtual)
(foreign_archives (native vlibfoo$ext_lib))
(known_implementations (somevariant impl))
(known_implementations (somevariant impl) (somevariant-2 impl-2))
(main_module_name Vlibfoo)
(modes byte native)
(modules
Expand Down

0 comments on commit 1ad1d27

Please sign in to comment.