Hello everyone!
I have the following setup:
-
My own binary crate
Bin
. -
This
Bin
crate depends on two crates: my ownLib
library crate and third-partyFoo
crate -
Lib
crate, in turn, depends on third-partyBar
crate. -
Foo
andBar
are very simple crates that wrap around native librarieslibfoo
andlibbar
. -
Both of them are able to wrap either a system-provided library with the help of
system-libfoo
andsystem-libbar
features, or to compile-in their own versions withbuiltin-libfoo
andbuiltin-libbar
features. Of course those features are mutually exclusive.
Now to the tricky part. I want my program to rely on system-provided libraries on Unix, but on Windows I want to embed libs into my program. I already did my homework and found that unfortunately target-specific features aren't supported by the Cargo, and the possible workaround is target-scecific dependencies with it's own set of features each.
The only problem is, it works kinda 50/50.
I have the following in the Cargo.toml
for Bin
crate:
[dependencies]
lib = { path = "../lib" }
[target.'cfg(windows)'.dependencies]
foo = { version = "0.1", default-features = false, features = ["builtin-libfoo"] }
[target.'cfg(unix)'.dependencies]
foo = { version = "0.1", default-features = false, features = ["system-libfoo"] }
And similarly, for the Lib
crate:
[target.'cfg(windows)'.dependencies]
bar = { version = "0.2", default-features = false, features = ["builtin-libbar"] }
[target.'cfg(unix)'.dependencies]
bar = { version = "0.2", default-features = false, features = ["system-libbar"] }
Now, if I try to independently build Lib
crate for both targets everything works as expected.
If I try to build Bin
crate excluding Lib
crate dependency, everything also works as expected for both targets.
But if I enable Lib
dependency in Bin
create and try to build it, build fails - Bar
crate complains that I've enabled two incompatible features.
What did I miss? Why everything's working when I build Lib
crate standalone but not as a dependency? And how to workaround this?