I want to conditionally change the crate-type
field in my Cargo.toml
based on an enabled feature. I could not find any way to do this. Is it possible to do this either directly in Cargo.toml
or through something in build.rs
? or an environment variable?
Example of what I am looking for: compile only rlib if b is enabled.
[package]
name = "mylib"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[features]
a = []
b = []
['cfg(feature = "a")'.lib]
crate-type = ["staticlib"]
['cfg(feature = "b")'.lib]
crate-type = ["rlib"]
In case you are wondering why I even need this:
I am writing a library that will be used by both rust applications & over FFI in C/C++ applications, and the apis, as well as some of the types are adjusted based on the enabled feature for ease of use etc.
If I enable all crate types ["rlib", "staticlib"]
, the produced .a
is MASSIVE (12MB+).
If I only build staticlib
, its only 700KB.
If I only build rlib
, its only 1.5MB.
Is there maybe some other underlying reason? A cargo bug maybe?
As per @kmdreko:
Build with --crate-type=
as found here
Eg:
cargo rustc --features b --crate-type=rlib