Conditional dependency still compiled?

In my program I link against a LAPACK implementation via the lapack and lapack-src crates. I want to use a different LAPACK implementation depending on the OS (Accelerate on macOS, OpenBLAS on Windows and Linux). This is what my attempt looks like in my Cargo.toml:

lapack = "0.19.0"

[target.'cfg(target_os = "macos")'.dependencies]
lapack-src = { version = "0.8.0", features = ["accelerate"]}

[target.'cfg(any(target_os="linux", target_os="windows"))'.dependencies]
lapack-src = {version = "0.8.0", features = ["openblas"]}

When I try to compile my code in a Linux-based Docker container I get the following error message:

#13 57.09 error: native frameworks are only available on macOS targets
#13 57.09
#13 57.11 error: could not compile `accelerate-src` due to previous error
#13 57.11 warning: build failed, waiting for other jobs to finish...
#13 61.36 error: build failed

So it looks like my attempt to use a different feature based on the target_os isn't doing what I expect. What is the correct way to do this?

Are you using edition="2021" or resolver="2" in Cargo.toml? Cargo (without these) used to unify features across all kinds of dependencies, and that often resulted in unintended features getting enabled.

1 Like

Setting edition="2021" fixed it. Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.