Skip to content

Commit

Permalink
(foreign_library): support (enabled_if) (#9914)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolás Ojeda Bär <[email protected]>
  • Loading branch information
nojb authored Feb 3, 2024
1 parent 445a688 commit ce21136
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
2 changes: 2 additions & 0 deletions doc/changes/9914.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- The `(foreign_library)` stanza now supports the `(enabled_if)` field. (#9914,
@nojb)
6 changes: 4 additions & 2 deletions doc/reference/foreign.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ described in :ref:`foreign-sandboxing`, or ask Dune to build it via the
(foreign_library
(archive_name arch1)
(language c)
(enabled_if true)
(names src4 src5)
(include_dir headers))
Expand All @@ -147,10 +148,11 @@ object files into an archive ``arch1``, whose full name is typically
``libarch1.a`` for static linking and ``dllarch1.so`` for dynamic
linking.

The ``foreign_library`` stanza supports all :ref:`foreign-stubs` fields plus
the ``archive_name`` field, which specifies the archive's name. You can refer
The ``foreign_library`` stanza supports all :ref:`foreign-stubs` fields.
The ``archive_name`` field specifies the archive's name. You can refer
to the same archive name from multiple OCaml libraries and executables, so a
foreign archive is a bit like a foreign library, hence the name of the stanza.
The ``enabled_if`` field has the same meaning as in the :ref:`library` stanza.

Foreign archives are particularly useful when embedding a library written in
a foreign language and/or built with another build system. See
Expand Down
6 changes: 4 additions & 2 deletions src/dune_rules/foreign.ml
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,17 @@ module Library = struct
{ archive_name : Archive.Name.t
; archive_name_loc : Loc.t
; stubs : Stubs.t
; enabled_if : Blang.t
}

let decode =
let open Dune_lang.Decoder in
fields
(let+ archive_name_loc, archive_name =
located (field "archive_name" Archive.Name.decode)
and+ stubs = Stubs.decode_stubs ~for_library:true in
{ archive_name; archive_name_loc; stubs })
and+ stubs = Stubs.decode_stubs ~for_library:true
and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:(Some (3, 14)) () in
{ archive_name; archive_name_loc; stubs; enabled_if })
;;

include Stanza.Make (struct
Expand Down
1 change: 1 addition & 0 deletions src/dune_rules/foreign.mli
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ module Library : sig
{ archive_name : Archive.Name.t
; archive_name_loc : Loc.t
; stubs : Stubs.t
; enabled_if : Blang.t
}

val decode : t Dune_lang.Decoder.t
Expand Down
6 changes: 4 additions & 2 deletions src/dune_rules/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ end = struct
rules)
enabled_if
| Foreign.Library.T lib ->
let+ () = Lib_rules.foreign_rules lib ~sctx ~dir ~dir_contents ~expander in
empty_none
Expander.eval_blang expander lib.enabled_if
>>= if_available (fun () ->
let+ () = Lib_rules.foreign_rules lib ~sctx ~dir ~dir_contents ~expander in
empty_none)
| Executables.T exes ->
Expander.eval_blang expander exes.enabled_if
>>= if_available (fun () ->
Expand Down
47 changes: 47 additions & 0 deletions test/blackbox-tests/test-cases/foreign-library.t
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,50 @@ Testsuite for the (foreign_library ...) stanza.
Leaving directory 'stubs_in_libs'
$ stubs_in_libs/_build/default/main.exe
12

--------------------------------------------------------------------------------
* Test (enabled_if)

$ mkdir -p enabled_if
$ cat >enabled_if/dune-project <<EOF
> (lang dune 3.13)
> EOF
$ cat >enabled_if/lib.c <<EOF
> int foo(void) { return 0; }
> EOF
$ cat >enabled_if/dune <<EOF
> (foreign_library
> (archive_name lib)
> (language c)
> (enabled_if (<> %{env:ENABLE=} "0"))
> (names lib))
> EOF
$ dune build --root enabled_if
Entering directory 'enabled_if'
File "dune", line 4, characters 1-37:
4 | (enabled_if (<> %{env:ENABLE=} "0"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: 'enabled_if' is only available since version 3.14 of the dune
language. Please update your dune-project file to have (lang dune 3.14).
Leaving directory 'enabled_if'
[1]
$ cat >enabled_if/dune-project <<EOF
> (lang dune 3.14)
> EOF
$ cat >>enabled_if/dune <<EOF
> (library
> (name lib)
> (foreign_archives lib))
> EOF
$ ENABLE=1 dune build --root enabled_if
Entering directory 'enabled_if'
Leaving directory 'enabled_if'
$ ENABLE=0 dune build --root enabled_if
Entering directory 'enabled_if'
File "dune", line 6, characters 0-45:
6 | (library
7 | (name lib)
8 | (foreign_archives lib))
Error: No rule found for liblib.a
Leaving directory 'enabled_if'
[1]

0 comments on commit ce21136

Please sign in to comment.