-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
Problem
I just tried to use the new per-package-target feature implemented in #9030 (tracking issue) together with -Zbuild-std, but it results in an error: -Zbuild-std requires --target.
Steps
- Add a
default-targetorforced-targetkey inCargo.toml. - Try to compile by running
cargo build -Zbuild-std=core.
Possible Solution(s)
I think the issue is that the target check already happens here:
cargo/src/cargo/ops/cargo_compile.rs
Lines 394 to 399 in db741ac
| if build_config.requested_kinds[0].is_host() { | |
| // TODO: This should eventually be fixed. Unfortunately it is not | |
| // easy to get the host triple in BuildConfig. Consider changing | |
| // requested_target to an enum, or some other approach. | |
| anyhow::bail!("-Zbuild-std requires --target"); | |
| } |
But the default-target/forced-target fields are only considered later in generate_targets:
cargo/src/cargo/ops/cargo_compile.rs
Lines 925 to 943 in db741ac
| // If `--target` has not been specified, then the unit | |
| // graph is built almost like if `--target $HOST` was | |
| // specified. See `rebuild_unit_graph_shared` for more on | |
| // why this is done. However, if the package has its own | |
| // `package.target` key, then this gets used instead of | |
| // `$HOST` | |
| let explicit_kinds = if let Some(k) = pkg.manifest().forced_kind() { | |
| vec![k] | |
| } else { | |
| requested_kinds | |
| .iter() | |
| .map(|kind| match kind { | |
| CompileKind::Host => { | |
| pkg.manifest().default_kind().unwrap_or(explicit_host_kind) | |
| } | |
| CompileKind::Target(t) => CompileKind::Target(*t), | |
| }) | |
| .collect() | |
| }; |
The generate_targets function is invoked here:
cargo/src/cargo/ops/cargo_compile.rs
Lines 470 to 488 in db741ac
| // Passing `build_config.requested_kinds` instead of | |
| // `explicit_host_kinds` here so that `generate_targets` can do | |
| // its own special handling of `CompileKind::Host`. It will | |
| // internally replace the host kind by the `explicit_host_kind` | |
| // before setting as a unit. | |
| let mut units = generate_targets( | |
| ws, | |
| &to_builds, | |
| filter, | |
| &build_config.requested_kinds, | |
| explicit_host_kind, | |
| build_config.mode, | |
| &resolve, | |
| &workspace_resolve, | |
| &resolved_features, | |
| &pkg_set, | |
| &profiles, | |
| interner, | |
| )?; |
Notes
Output of cargo version:
cargo 1.53.0-nightly (4369396ce 2021-04-27)
release: 1.53.0
commit-hash: 4369396ce7d270972955d876eaa4954bea56bcd9
commit-date: 2021-04-27
cc @Ekleog