Open sproctor opened 7 years ago
Solver knows about pkg-config, so it should be possible to do this with the conditional flags mechanism. Give it a try?
I'm not sure what you mean. Something like the following?
if flag(lua51)
pkgconfig-depends: lua51
if flag(lua5.1)
pkgconfig-depends: lua5.1
...
That wouldn't be particularly useful. If you mean something else, it's beyond my understanding of cabal. Thank you for your help with this.
Nope, that's it. Why wouldn't it be useful? If you default each of the flags to true, the solver will toggle them off if lua51 doesn't exist, etc. (I guess you should mark the library not buildable if all of the flags fail.)
@sproctor actually that's not enough. You should ensure that the flags are mutually exclusive, as you're depending on different package names there, but you clearly want exactly one of those flags to be true, right? If you don't you're semantically unsound and you'll run into problems if a system happens to have multiple packages available. There is one proposal for adding support for multi-way flags to cabal which would make this easier though.
PS: Here's an example from the base
package on how to accomplish this currently:
-- sanity-check to ensure exactly one flag is set
if !((flag(integer-gmp) && !flag(integer-simple)) || (!flag(integer-gmp) && flag(integer-simple)))
build-depends: invalid-cabal-flag-settings<0
I tried making a simple example with 1 flag. This system has lua.pc with version 5.1.3
$pkg-config --modversion lua
5.1.3
I used this code:
flag lua501
description: Build against lua 5.1.
default: False
flag pkg-xdoty
default: True
library
...
if flag(lua501)
if flag(pkg-xdoty)
pkgconfig-depends: lua-5.1
else
pkgconfig-depends: lua >= 5.1
else
...
This is the result:
$ stack build --flag hslua:lua501
hslua-0.5.0: configure (lib)
Configuring hslua-0.5.0...
Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2: The pkg-config package 'lua-5.1' is
required but it could not be found.
If I change the default of the flag so it tries "lua >= 5.1" first, it builds without issue. What am I doing wrong here?
stack
doesn't resolve automatic flags, are you sure you set them explicitly right in the stack.yaml
?
Yes, you need a dep solver or manual flag assignment for this to work.
Some packages, specifically Lua, do not have a standard name in for pkg-config. Lua 5.1 could be one of lua51, lua5.1, lua-5.1, or lua. It would be nice to have an "or" feature in pkgconfig-depends so you could specify that as something like:
The last example in https://autotools.io/pkgconfig/pkg_check_modules.html shows how to accomplish this using autoconf.