Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/dune_rules/stanzas/buildable.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type t =
; melange_modules : Ordered_set_lang.Unexpanded.t option
; empty_module_interface_if_absent : bool
; libraries : Lib_dep.t list
; melange_libraries : Lib_dep.t list option
; foreign_archives : (Loc.t * Foreign.Archive.t) list
; extra_objects : Foreign.Objects.t
; foreign_stubs : Foreign.Stubs.t list
Expand Down Expand Up @@ -125,6 +126,11 @@ let decode (for_ : for_) =
>>> enter (maybe string))))
and+ libraries =
field "libraries" (Lib_dep.L.decode ~allow_re_export:in_library) ~default:[]
and+ melange_libraries =
field_o
"melange.libraries"
(Dune_lang.Syntax.since Stanza.syntax (3, 24)
>>> Lib_dep.L.decode ~allow_re_export:in_library)
and+ flags = decode_ocaml_flags
and+ js_of_ocaml =
field
Expand Down Expand Up @@ -210,6 +216,7 @@ let decode (for_ : for_) =
; foreign_archives
; extra_objects
; libraries
; melange_libraries
; flags
; js_of_ocaml = { js = js_of_ocaml; wasm = wasm_of_ocaml }
; allow_overlapping_dependencies
Expand Down
1 change: 1 addition & 0 deletions src/dune_rules/stanzas/buildable.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type t =
; melange_modules : Ordered_set_lang.Unexpanded.t option
; empty_module_interface_if_absent : bool
; libraries : Lib_dep.t list
; melange_libraries : Lib_dep.t list option
; foreign_archives : (Loc.t * Foreign.Archive.t) list
; extra_objects : Foreign.Objects.t
; foreign_stubs : Foreign.Stubs.t list
Expand Down
30 changes: 26 additions & 4 deletions src/dune_rules/stanzas/library.ml
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,25 @@ let to_lib_id ~src_dir t =
Lib_id.Local.make ~loc ~src_dir (Lib_name.of_local t.name)
;;

let library_deps ~loc ~field ~modes ~libs ~melange_libs =
let ocaml, melange =
let { Lib_mode.Map.ocaml = { byte; native }; melange } = modes in
let ocaml_libraries = if byte || native then libs else [] in
let melange_libraries =
match melange, melange_libs with
| true, Some melange_libraries -> melange_libraries
| true, None -> libs
| false, None -> []
| false, Some _ ->
User_error.raise
~loc
[ Pp.textf "Cannot specify `%s' without `melange' mode" field ]
in
ocaml_libraries, melange_libraries
in
{ Compilation_mode.By_mode.ocaml; melange }
;;

let to_lib_info
conf
~expander
Expand Down Expand Up @@ -576,14 +595,17 @@ let to_lib_info
| Public (_, pkg) -> Package.version pkg
| Installed_private | Installed | Private _ -> None
in
let loc = conf.buildable.loc in
let requires =
{ Compilation_mode.By_mode.ocaml = conf.buildable.libraries
; melange = conf.buildable.libraries
}
library_deps
~loc
~field:"melange.libraries"
~modes
~libs:conf.buildable.libraries
~melange_libs:conf.buildable.melange_libraries
in
let parameters = conf.parameters in
let allow_unused_libraries = conf.buildable.allow_unused_libraries in
let loc = conf.buildable.loc in
let kind = conf.kind in
let src_dir = dir in
let orig_src_dir = None in
Expand Down
1 change: 1 addition & 0 deletions src/dune_rules/stanzas/parameter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ let decode =
; modules
; empty_module_interface_if_absent = false
; libraries
; melange_libraries = None
; melange_modules = None
; foreign_archives = []
; extra_objects = Foreign.Objects.empty
Expand Down
111 changes: 111 additions & 0 deletions test/blackbox-tests/test-cases/melange/conditional-libraries.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
`melange.libraries` applies only to Melange compilation.

The field is available starting in Dune 3.24.

$ mkdir old
$ cat > old/dune-project <<EOF
> (lang dune 3.23)
> (using melange 0.1)
> EOF
$ cat > old/dune <<EOF
> (library
> (name old)
> (modes melange)
> (melange.libraries dep))
> EOF
$ dune build --root old
Entering directory 'old'
File "dune", line 4, characters 1-24:
4 | (melange.libraries dep))
^^^^^^^^^^^^^^^^^^^^^^^
Error: 'melange.libraries' is only available since version 3.24 of the dune
language. Please update your dune-project file to have (lang dune 3.24).
Leaving directory 'old'
[1]
$ rm -rf old

$ cat > dune-project <<EOF
> (lang dune 3.24)
> (using melange 0.1)
> EOF

$ mkdir lib_for_melange lib_for_native app
$ cat > lib_for_melange/dune <<EOF
> (library
> (modes melange)
> (name lib_for_melange))
> EOF
$ cat > lib_for_melange/foo.ml <<EOF
> let x = "lib for melange"
> EOF

$ cat > lib_for_native/dune <<EOF
> (library
> (modes :standard)
> (name lib_for_native))
> EOF
$ cat > lib_for_native/foo.ml <<EOF
> let x = "lib for native"
> EOF

$ cat > app/dune <<EOF
> (library
> (modes melange :standard)
> (name app)
> (modules app common_intf)
> (libraries lib_for_native)
> (melange.libraries lib_for_melange))
>
> (melange.emit
> (target out)
> (modules main)
> (emit_stdlib false)
> (libraries app))
>
> (executable
> (name main)
> (modules main)
> (libraries app))
> EOF

$ cat > app/common_intf.mli <<EOF
> val print : string -> unit
> EOF
$ cat > app/common_intf.ml <<EOF
> let message = Lib_for_native.Foo.x
> let print prefix = Format.eprintf "%s%s@." prefix message
> EOF
$ cat > app/common_intf.melange.ml <<EOF
> let message = Lib_for_melange.Foo.x
> let print prefix = Js.log2 prefix message
> EOF
$ cat > app/app.ml <<EOF
> let say_hello () = Common_intf.print "message: "
> EOF
$ cat > app/main.ml <<EOF
> let () = App.say_hello ()
> EOF

$ dune build @app/melange app/main.exe
$ node _build/default/app/out/app/main.js
message: lib for melange
$ _build/default/app/main.exe
message: lib for native

The field is rejected without Melange mode.

$ mkdir no-melange
$ cat > no-melange/dune <<EOF
> (library
> (modes :standard)
> (name no_melange)
> (melange.libraries lib_for_melange))
> EOF
$ dune build no-melange
File "no-melange/dune", lines 1-4, characters 0-84:
1 | (library
2 | (modes :standard)
3 | (name no_melange)
4 | (melange.libraries lib_for_melange))
Error: Cannot specify `melange.libraries' without `melange' mode
[1]
Loading