Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cargo-platform = { path = "crates/cargo-platform", version = "0.3.3" }
cargo-test-macro = { version = "0.4.14", path = "crates/cargo-test-macro" }
cargo-test-support = { version = "0.11.4", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.32", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.14.2", path = "crates/cargo-util-schemas" }
cargo-util-schemas = { version = "0.15.0", path = "crates/cargo-util-schemas" }
cargo-util-terminal = { version = "0.1.2", path = "crates/cargo-util-terminal" }
cargo_metadata = "0.23.1"
clap = "4.6.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util-schemas/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-util-schemas"
version = "0.14.3"
version = "0.15.0"
rust-version = "1.97" # MSRV:1
edition.workspace = true
license.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util-schemas/index.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
]
},
"lib": {
"description": "Whether or not this is a library dependency.",
"description": "Legacy artifact dependency flag retained for index schema v3 compatibility.\n\nCargo ignores this field.",
"type": "boolean",
"default": false
}
Expand Down
9 changes: 1 addition & 8 deletions crates/cargo-util-schemas/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -862,13 +862,6 @@
}
]
},
"lib": {
"description": "If set, the artifact should also be a dependency",
"type": [
"boolean",
"null"
]
},
"target": {
"description": "A platform name, like `x86_64-apple-darwin`",
"type": [
Expand Down Expand Up @@ -1483,4 +1476,4 @@
]
}
}
}
}
4 changes: 3 additions & 1 deletion crates/cargo-util-schemas/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ pub struct RegistryDependency<'a> {
pub artifact: Option<Vec<Cow<'a, str>>>,
/// The target for bindep.
pub bindep_target: Option<Cow<'a, str>>,
/// Whether or not this is a library dependency.
/// Legacy artifact dependency flag retained for index schema v3 compatibility.
///
/// Cargo ignores this field.
#[serde(default)]
pub lib: bool,
}
Expand Down
3 changes: 0 additions & 3 deletions crates/cargo-util-schemas/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,6 @@ pub struct TomlDetailedDependency<P: Clone = String> {

/// One or more of `bin`, `cdylib`, `staticlib`, `bin:<name>`.
pub artifact: Option<StringOrVec>,
/// If set, the artifact should also be a dependency
pub lib: Option<bool>,
/// A platform name, like `x86_64-apple-darwin`
pub target: Option<String>,

Expand Down Expand Up @@ -892,7 +890,6 @@ impl<P: Clone> Default for TomlDetailedDependency<P> {
package: Default::default(),
public: Default::default(),
artifact: Default::default(),
lib: Default::default(),
target: Default::default(),
_unused_keys: Default::default(),
}
Expand Down
1 change: 1 addition & 0 deletions crates/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct NewCrateDependency {
pub artifact: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bindep_target: Option<String>,
/// Legacy artifact dependency flag retained for registry API compatibility.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub lib: bool,
}
Expand Down
45 changes: 30 additions & 15 deletions doc/book/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -1024,20 +1024,23 @@ Artifact-dependencies adds the following keys to a dependency declaration in `Ca
zoo = { version = "1.0", artifact = ["bin:cat", "bin:dog"]}
```

- `lib` --- This is a Boolean value which indicates whether or not to also build the dependency's library as a normal Rust `lib` dependency.
This field can only be specified when `artifact` is specified.

The default for this field is `false` when `artifact` is specified.
If this is set to `true`, then the dependency's `[lib]` target will also be built for the platform target the declaring package is being built for.
This allows the package to use the dependency from Rust code like a normal dependency in addition to an artifact dependency.

Example:
To use both the library and an artifact from the same package, declare
two dependencies:

```toml
[dependencies]
bar = { version = "1.0", artifact = "bin", lib = true }
bar = "1.0"
bar-bin = { package = "bar", version = "1.0", artifact = "bin" }
```

The normal dependency and artifact dependency have different dependency
graphs. Features enabled for `bar` are not enabled for `bar-bin`, and
features enabled for `bar-bin` are not enabled for `bar`, unless the same
features are enabled on both dependencies.

The dependency-level `lib` key has been removed. Cargo rejects manifests
that set it; use separate normal and artifact dependencies instead.

- `target` --- The platform target to build the dependency for.
This field can only be specified when `artifact` is specified.

Expand All @@ -1053,6 +1056,15 @@ Artifact-dependencies adds the following keys to a dependency declaration in `Ca
same-target = { version = "1.0", artifact = "bin", target = "target" }
```

To build artifacts from the same package for multiple target platforms,
declare each alias separately:

```toml
[build-dependencies]
firmware-x86 = { package = "firmware", version = "1.0", artifact = "bin", target = "x86_64-unknown-none" }
firmware-arm = { package = "firmware", version = "1.0", artifact = "bin", target = "thumbv7em-none-eabihf" }
```

### artifact-dependencies: Environment variables

After building an artifact dependency, Cargo provides the following environment variables that you can use to access the artifact:
Expand Down Expand Up @@ -1127,22 +1139,25 @@ fn main() {
}
```

#### Example: use _binary_ artifact and its library in a binary
#### Example: use a binary artifact and its library

The `Cargo.toml` in the consuming package, building the `bar` binary for inclusion
as artifact while making it available as library as well…
The `Cargo.toml` in the consuming package can declare both a normal dependency
and an artifact dependency on the same package:

```toml
[dependencies]
bar = { artifact = "bin", version = "1.0", lib = true }
bar = "1.0"
bar-bin = { package = "bar", version = "1.0", artifact = "bin" }
```

…along with the executable using `main.rs`.
The normal dependency is available to Rust code under its dependency name, and
the artifact is available through the environment variable for the artifact
dependency alias:

```rust
fn main() {
bar::init();
command::run(env!("CARGO_BIN_FILE_BAR"));
command::run(env!("CARGO_BIN_FILE_BAR_BIN_bar"));
}
```

Expand Down
1 change: 1 addition & 0 deletions src/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ fn generate_roots(
/*dep_hash*/ 0,
IsArtifact::No,
None,
None,
false,
));
}
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ pub struct UnitInner {
///
/// [`FeaturesFor::ArtifactDep`]: crate::resolver::features::FeaturesFor::ArtifactDep
pub artifact_target_for_features: Option<CompileTarget>,
/// The dependency name that introduced this artifact dependency feature
/// graph.
pub artifact_dep_name_for_features: Option<InternedString>,

/// Skip compiling this unit because `--compile-time-deps` flag is set and
/// this is not a compile time dependency.
Expand Down Expand Up @@ -212,6 +215,10 @@ impl fmt::Debug for Unit {
"artifact_target_for_features",
&self.artifact_target_for_features,
)
.field(
"artifact_dep_name_for_features",
&self.artifact_dep_name_for_features,
)
.field("is_std", &self.is_std)
.field("dep_hash", &self.dep_hash)
.finish()
Expand Down Expand Up @@ -261,6 +268,7 @@ impl UnitInterner {
dep_hash: u64,
artifact: IsArtifact,
artifact_target_for_features: Option<CompileTarget>,
artifact_dep_name_for_features: Option<InternedString>,
skip_non_compile_time_dep: bool,
) -> Unit {
let target = match (is_std, target.kind()) {
Expand Down Expand Up @@ -298,6 +306,7 @@ impl UnitInterner {
dep_hash,
artifact,
artifact_target_for_features,
artifact_dep_name_for_features,
skip_non_compile_time_dep,
});
Unit { inner }
Expand Down
Loading