Description
Problem
When depending on a crate which declares it supports multiple crate types in its [lib]
section, cargo will always build all of the specified crate types when building the dependency. This means that a crate which specifies something like the following in its Cargo.toml
will always build a cdylib and staticlib when built as a dependency, even if we're only going to be using the normal lib dependency.
[lib]
crate-type = ["cdylib", "staticlib", "lib"]
This leads to wasted effort building unnecessary artifacts for dependencies, and can also lead to issues when the configuration used to build the lib would cause building the cdylib or staticlib to fail to link for whatever reason (e.g. due to linker flags).
Proposed Solution
It would be nice if there was a way to specify which crate types you are depending on from your dependencies when declaring a dependency in Cargo.toml, similar to how you can specify required feature flags.
I imagine this acting in a similar way to features, where when a dependency is declared without a crate-type
specification, it implicitly requires the crate types from the dependency's Cargo.toml
, and a dependency with crate-type
only requires the specified crate types.
This might look something like the following:
[dependencies.mycrate]
version = "1.0.0"
crate-type = ["lib"]
Notes
There may be some overlap between this and other work for artifact dependencies (#9096), although I am not familiar enough with the work in that area to know if it already covers this use-case.