Is it possible to disable a dependency's feature when building with RLS? - rust

I have the following dependency configuration in Cargo.toml.
[dependencies.ffmpeg-sys]
version = "3.4.1"
default-features = false
features = ["avcodec", "avformat", "swresample", "static", "build"]
Adding the build feature hugely increases compile time and I only need to do it when building in release mode.
Is there a way to disable features of a dependency based on the build type (debug, release, RLS)?

Related

Can I activate a dependency's feature only for debug profile?

I have just started looking into the Bevy game engine for Rust. It has a feature called dynamic, which enables dynamic linking, to speed up compilation time during development. We are, however, advised to disable this when building for release.
Is there a way to tell Cargo to enable the dynamic feature for a debug build, but disable it for a release build? Or do I have to personally remember to change bevy = { version = "0.5.0", features = ["dynamic"] } to bevy = "0.5.0" in Cargo.toml before running cargo build --release?
Along the lines of Rodrigo's comment, can confirm the following seems to work well:
[dependencies]
bevy = { version = "0.5.0" }
[features]
default = ["fast-compile"]
fast-compile = ["bevy/dynamic"]
Then for development, simply: cargo build
And for release: cargo build --release --no-default-features

How to activate an optional dependency?

Cargo.toml:
[features]
parallel = ["rayon"]
[dependencies.rayon]
version = "1.5"
optional = true
lib.rs:
#[cfg(feature = "parallel")]
pub mod par;
Rust Analyzer:
code is inactive due to #[cfg] directives: feature = "parallel" is disabled
How to enable the optional dependency?
You can set the Rust Analyzer configuration option rust-analyzer.cargo.features to an array containing a list of features that you want RA to consider active.
You can also set rust-analyzer.cargo.allFeatures to true, in order to enable all features in the project.
Methods for setting these vary according to the IDE you are using - for example, if using VS Code, you can set it via the "Extension Settings" for Rust Analyzer.
This assumes you are asking how to activate the features within Rust Analyzer - to activate them when building or running from Cargo, just use the --features option.
See: Rust Analyzer Manual
All non-default features need to be specified when running. So run with:
# Option 1
cargo run --features parallel
# Option 2
cargo run --all-features
Check cargo run --help for more help.

How to compile some dependencies with release

I'd like to build my rust app with "dev" profile, but some of the dependencies with "release" profile (because otherwise they are really slow). How can I selectively specify profiles for my crate dependencies?
Cargo is able to override profile for a specific package.
From the reference :
Profile settings can be overridden for specific packages and
build-time crates. To override the settings for a specific package,
use the package table to change the settings for the named package:
# The `foo` package will use the -Copt-level=3 flag.
[profile.dev.package.foo]
opt-level = 3
While compiling with dev profile, this will override optimize level for foo package.
If you want to optimize few dependency with a default value from dev profile and more from a release profile:
#override target package to build with dev default(opt-level)
[profile.dev.package.bar]
opt-level = 0
#override all other dependencies to build with release default(opt-level)
[profile.dev.package."*"]
opt-level = 3
If you want to optimize all of your dependencies except your
application(also workspace members)
[profile.dev.package."*"]
opt-level = 3
See also :
Default Profiles with their default settings
Reference of Profile Settings

How do I fix mismatching dependencies in my Cargo file to work around native library collisions?

I'm setting up a Rust server with Rocket and I'm trying to use it with a JWT library. They use different versions of the *ring* crate and I get an error during cargo build:
error: multiple packages link to native library `ring-asm`, but a native library can be linked only once
package `ring v0.12.1`
... which is depended on by `jsonwebtoken v4.0.1`
... which is depended on by `auther v0.1.0 (file:///home/drpytho/x/downloadble/auther)`
links to native library `ring-asm`
package `ring v0.11.0`
... which is depended on by `cookie v0.9.2`
... which is depended on by `rocket v0.3.6`
... which is depended on by `rocket_codegen v0.3.6`
... which is depended on by `auther v0.1.0 (file:///home/drpytho/x/downloadble/auther)`
also links to native library `ring-asm`
My Cargo.toml
[package]
name = "auther"
version = "0.1.0"
authors = ["Name <Email#mail.se>"]
[dependencies]
rocket = "0.3.6"
rocket_codegen = "0.3.6"
jsonwebtoken = "4"
serde_derive = "1"
serde = "1"
I read that you are supposed to fix the mismatching dependencies in your Cargo file, but I can't figure out how to do it.
You have to fix this by not transitively depending on different versions of crates that link to a native library.
There's no newer version of rocket available that depends on version 0.10 of cookie, which depends on ring 0.12, so you'll need to downgrade jsonwebtoken to 2.0.3.
You can work this out by checking the crates.io pages for the crates in question (like with jsonwebtoken), going back through older versions, and looking to see what dependencies it needs.

Does Cargo support custom profiles?

I often want to compile in release mode with debug = true so that I can read the generated assembly a bit easier. I am currently doing this:
[profile.release]
debug = true
but I don't want any debug symbols in my final release build. I'd like to do something like:
[profile.custom]
debug = true
opt-level = 3
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'unwind'
And then run
cargo build --custom
I've read the documentation to no avail.
As of Rust v1.57.0, the custom profiles feature is now stable.
Add a profile section, specify a base profile to inherit from, and tweak as you see fit:
[profile.production]
inherits = "release"
lto = true
Specify the profile to use via the --profile <name> flag to cargo.
Does Cargo support custom profiles?
No, stable releases of Cargo do not support this. It is available as an unstable nightly feature.
If you are using a nightly version of Cargo, you can create custom profiles in your Cargo.toml:
cargo-features = ["named-profiles"]
[profile.release-lto]
inherits = "release"
lto = true
And then use them:
cargo +nightly build --profile release-lto -Z unstable-options

Resources