Skip to content

Commit a4c0d39

Browse files
authored
fix: emit warnings as warnings when learning rust target info (#15036)
### What does this PR try to resolve? This is a horrible hack, which lets the rustc invocation for learning target info always emit warnings as warnings. But at least it unblocks people who pass `-Awarnings` via RUSTFLAGS. A long-term solution is a better interface between build systems and the compiler. See the discussion on Zulip: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Improving.20communication.20interface.20between.20cargo.2Frustc Fixes #8010 ### How should we test and review this PR? Ensure `CFG_DISABLE_CROSS_TESTS` is not set, and run `cargo t --test testsuite always_emit_warnings_as_warnings_when_learning_target_info` This also additionally adds `wasm32-unknown-unknown` target to Cargo's CI. ### Additional information
2 parents c3a2b1a + 5e3558c commit a4c0d39

File tree

8 files changed

+81
-23
lines changed

8 files changed

+81
-23
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ jobs:
161161
- run: rustup update --no-self-update stable
162162
- run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
163163
- run: rustup target add ${{ matrix.other }}
164+
- run: rustup target add wasm32-unknown-unknown
164165
- run: rustup target add aarch64-unknown-none # need this for build-std mock tests
165166
if: startsWith(matrix.rust, 'nightly')
166167
- run: rustup component add rustc-dev llvm-tools-preview rust-docs
@@ -225,6 +226,7 @@ jobs:
225226
- uses: actions/checkout@v4
226227
- run: rustup update --no-self-update stable && rustup default stable
227228
- run: rustup target add i686-unknown-linux-gnu
229+
- run: rustup target add wasm32-unknown-unknown
228230
- run: sudo apt update -y && sudo apt install gcc-multilib libsecret-1-0 libsecret-1-dev -y
229231
- run: rustup component add rustfmt || echo "rustfmt not available"
230232
- run: cargo test -p cargo

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ cargo-credential-macos-keychain = { version = "0.4.7", path = "credential/cargo-
3333
cargo-credential-wincred = { version = "0.4.7", path = "credential/cargo-credential-wincred" }
3434
cargo-platform = { path = "crates/cargo-platform", version = "0.2.0" }
3535
cargo-test-macro = { version = "0.4.0", path = "crates/cargo-test-macro" }
36-
cargo-test-support = { version = "0.7.0", path = "crates/cargo-test-support" }
36+
cargo-test-support = { version = "0.7.1", path = "crates/cargo-test-support" }
3737
cargo-util = { version = "0.2.14", path = "crates/cargo-util" }
3838
cargo-util-schemas = { version = "0.7.3", path = "crates/cargo-util-schemas" }
3939
cargo_metadata = "0.19.0"

crates/cargo-test-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-test-support"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
edition.workspace = true
55
rust-version = "1.83" # MSRV:1
66
license.workspace = true

crates/cargo-test-support/src/cross_compile.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,33 @@ pub fn can_run_on_host() -> bool {
267267
return true;
268268
}
269269
}
270+
271+
/// Check if the given target has been installed.
272+
///
273+
/// Generally [`disabled`] should be used to check if cross-compilation is allowed.
274+
/// And [`alternate`] to get the cross target.
275+
///
276+
/// You should only use this as a last resort to skip tests,
277+
/// because it doesn't report skipped tests as ignored.
278+
pub fn requires_target_installed(target: &str) -> bool {
279+
let has_target = std::process::Command::new("rustup")
280+
.args(["target", "list", "--installed"])
281+
.output()
282+
.ok()
283+
.map(|output| {
284+
String::from_utf8(output.stdout)
285+
.map(|stdout| stdout.contains(target))
286+
.unwrap_or_default()
287+
})
288+
.unwrap_or_default();
289+
if !has_target {
290+
let msg =
291+
format!("to run this test, run `rustup target add {target} --toolchain <toolchain>`",);
292+
if cargo_util::is_ci() {
293+
panic!("{msg}");
294+
} else {
295+
eprintln!("{msg}");
296+
}
297+
}
298+
has_target
299+
}

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ impl TargetInfo {
206206
process.arg("--print=crate-name"); // `___` as a delimiter.
207207
process.arg("--print=cfg");
208208

209+
// parse_crate_type() relies on "unsupported/unknown crate type" error message,
210+
// so make warnings always emitted as warnings.
211+
process.arg("-Wwarnings");
212+
209213
let (output, error) = rustc
210214
.cached_output(&process, extra_fingerprint)
211215
.with_context(|| {

tests/testsuite/cross_compile.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,3 +1260,38 @@ fn doctest_xcompile_linker() {
12601260
"#]])
12611261
.run();
12621262
}
1263+
1264+
#[cargo_test]
1265+
fn always_emit_warnings_as_warnings_when_learning_target_info() {
1266+
if cross_compile::disabled() {
1267+
return;
1268+
}
1269+
1270+
let target = "wasm32-unknown-unknown";
1271+
if !cross_compile::requires_target_installed(target) {
1272+
return;
1273+
}
1274+
1275+
let p = project()
1276+
.file(
1277+
"Cargo.toml",
1278+
r#"
1279+
[package]
1280+
name = "foo"
1281+
edition = "2015"
1282+
"#,
1283+
)
1284+
.file("src/lib.rs", "")
1285+
.build();
1286+
1287+
p.cargo("build -v --target")
1288+
.env("RUSTFLAGS", "-Awarnings")
1289+
.arg(target)
1290+
.with_stderr_data(str![[r#"
1291+
[COMPILING] foo v0.0.0 ([ROOT]/foo)
1292+
[RUNNING] `rustc --crate-name foo [..]-Awarnings[..]`
1293+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1294+
1295+
"#]])
1296+
.run();
1297+
}

tests/testsuite/standard_lib.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use std::path::{Path, PathBuf};
88

9+
use cargo_test_support::cross_compile;
910
use cargo_test_support::prelude::*;
1011
use cargo_test_support::registry::{Dependency, Package};
1112
use cargo_test_support::ProjectBuilder;
@@ -391,24 +392,8 @@ fn check_core() {
391392

392393
#[cargo_test(build_std_mock)]
393394
fn build_std_with_no_arg_for_core_only_target() {
394-
let has_rustup_aarch64_unknown_none = std::process::Command::new("rustup")
395-
.args(["target", "list", "--installed"])
396-
.output()
397-
.ok()
398-
.map(|output| {
399-
String::from_utf8(output.stdout)
400-
.map(|stdout| stdout.contains("aarch64-unknown-none"))
401-
.unwrap_or_default()
402-
})
403-
.unwrap_or_default();
404-
if !has_rustup_aarch64_unknown_none {
405-
let msg =
406-
"to run this test, run `rustup target add aarch64-unknown-none --toolchain nightly`";
407-
if cargo_util::is_ci() {
408-
panic!("{msg}");
409-
} else {
410-
eprintln!("{msg}");
411-
}
395+
let target = "aarch64-unknown-none";
396+
if !cross_compile::requires_target_installed(target) {
412397
return;
413398
}
414399

@@ -427,7 +412,8 @@ fn build_std_with_no_arg_for_core_only_target() {
427412
.build();
428413

429414
p.cargo("build -v")
430-
.arg("--target=aarch64-unknown-none")
415+
.arg("--target")
416+
.arg(target)
431417
.build_std(&setup)
432418
.with_stderr_data(
433419
str![[r#"
@@ -457,7 +443,8 @@ fn build_std_with_no_arg_for_core_only_target() {
457443
// Note that we don't download std dependencies for the second call
458444
// because `-Zbuild-std` downloads them all also when building for core only.
459445
p.cargo("build -v")
460-
.arg("--target=aarch64-unknown-none")
446+
.arg("--target")
447+
.arg(target)
461448
.target_host()
462449
.build_std(&setup)
463450
.with_stderr_data(

0 commit comments

Comments
 (0)