`(enabled_if (installed package))` in Dune?

Hi!

Is there a booleans syntax to check for installed packages? To have an enabled_if test that mirrors a select stanza in a library.

Thanks!

2 Likes

Bumping this topic as I need the same feature. Dune supports the lib-available: variable expansion, but the package I depend on does not install a libary. It is one of the conf-xxx packages which are essentially empty. Is there a way to detect that such a package is installed?

1 Like

Depending on what the conf-* package is, you might be able to use the bin-available variable to check for it in dune. I’ve used it for some tests, even with binaries that don’t have a corresponding conf-* package.

I don’t think that what you ask is directly possible.
However, there are 2 more standard ways to achieve this:

  • Make the thing that depends on conf-x its own package. Example: ctypes (pure-ish) vs ctypes-foreign (depends on conf-libffi). conf-libffi is listed in ctypes-foreign.opam but dune is not concerned with this at all - the build rules for ctypes-foreign can just assume that libffi is installed.
  • Make the thing that depends on conf-x in a subpackage, ie give it a (public_name something.x) and make it (optional). You’ll have a single opam package where conf-x can be listed in depopts.

Generally, the first is considered best practice, but the second is a simpler option. It is more difficult to consume for your users since they don’t have an easy way to express a dependency to something.x in their opam files; they have to depend on something and conf-x.

A variant of the second pattern can be encoded using “flag packages”, where you define an “empty” package, and decide on whether to build the optional stuff based on the presence of this empty package, but I don’t think it’s a good idea to create new packages like that. (examples: coq-native, old versions of ctypes-foreign).

In my case I have an empty “flag package” and a dune rule that needs to be enabled_if this package is installed. To be more specific, this is the non-working dune rule. Is there any other way to achieve this?

No, dune doesn’t have a notion of package like that.

I guess the simplest solution then is to make the “flag package” install a dummy library.

@emillon I would also like to access package variables set in opam in a dune file, something like %{package:conf-ios:arch}. If dune is using opam as library, would that be doable? Is a dune plugin the right place to implement such a feature?

Dune generally does not have a notion of opam packages when building, as that would also require it to have a notion of opam-repository which it doesn’t.

I think plenty of good ways have been presented here (the simplest way would be bin-available to depend on something that’s installed by conf-maccatalyst), the nicest way would be a separate public_name/OPAM package with the parts that require maccatalyst.

1 Like

I’ll share my minimal solution. The only thing needed is for the virtual package to install a META file. Then the lib-available: variable expansion in dune works. No need to install a dummy binary or library.