diff --git a/src/compiler/build_context/target_info.rs b/src/compiler/build_context/target_info.rs index de4b197a07f..6788e1c65c9 100644 --- a/src/compiler/build_context/target_info.rs +++ b/src/compiler/build_context/target_info.rs @@ -574,9 +574,10 @@ impl TargetInfo { target_kind: &TargetKind, target_triple: &str, gctx: &GlobalContext, + rustc: &Rustc, ) -> CargoResult<(Vec, Vec)> { match mode { - CompileMode::Build => self.calc_rustc_outputs(target_kind, target_triple, gctx), + CompileMode::Build => self.calc_rustc_outputs(target_kind, target_triple, gctx, rustc), CompileMode::Test => { match self.file_types(&CrateType::Bin, FileFlavor::Normal, target_triple)? { Some(fts) => Ok((fts, Vec::new())), @@ -598,6 +599,7 @@ impl TargetInfo { target_kind: &TargetKind, target_triple: &str, gctx: &GlobalContext, + rustc: &Rustc, ) -> CargoResult<(Vec, Vec)> { let mut unsupported = Vec::new(); let mut result = Vec::new(); @@ -619,7 +621,7 @@ impl TargetInfo { } } if !result.is_empty() { - if !gctx.should_embed_metadata() + if !gctx.should_embed_metadata(rustc) && crate_types .iter() .any(|ct| ct.benefits_from_no_embed_metadata()) diff --git a/src/compiler/build_runner/compilation_files.rs b/src/compiler/build_runner/compilation_files.rs index 8a6be2cdf9f..ff2850d865d 100644 --- a/src/compiler/build_runner/compilation_files.rs +++ b/src/compiler/build_runner/compilation_files.rs @@ -421,6 +421,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> { &TargetKind::Bin, bcx.target_data.short_name(&kind), bcx.gctx, + bcx.rustc(), ) .expect("target must support `bin`"); @@ -664,7 +665,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> { let info = bcx.target_data.info(unit.kind); let triple = bcx.target_data.short_name(&unit.kind); let (file_types, unsupported) = - info.rustc_outputs(unit.mode, unit.target.kind(), triple, bcx.gctx)?; + info.rustc_outputs(unit.mode, unit.target.kind(), triple, bcx.gctx, bcx.rustc())?; if file_types.is_empty() { if !unsupported.is_empty() { let unsupported_strs: Vec<_> = unsupported.iter().map(|ct| ct.as_str()).collect(); diff --git a/src/compiler/fingerprint/mod.rs b/src/compiler/fingerprint/mod.rs index 172165dca40..87af84e984c 100644 --- a/src/compiler/fingerprint/mod.rs +++ b/src/compiler/fingerprint/mod.rs @@ -1676,7 +1676,7 @@ fn calculate_normal( build_runner .bcx .gctx - .should_embed_metadata() + .should_embed_metadata(build_runner.bcx.rustc()) .not() .hash(&mut config); diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index d7fa56e1c46..ca5c900fd96 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -1320,7 +1320,11 @@ fn build_base_args( if unit.mode.is_check() { cmd.arg("--emit=dep-info,metadata"); - } else if !build_runner.bcx.gctx.should_embed_metadata() { + } else if !build_runner + .bcx + .gctx + .should_embed_metadata(build_runner.bcx.rustc()) + { // Nightly rustc supports the -Zembed-metadata=no flag, which tells it to avoid including // full metadata in rlib/dylib artifacts, to save space on disk. In this case, metadata // will only be stored in .rmeta files. @@ -1814,7 +1818,10 @@ pub fn extern_args( let mut result = Vec::new(); let deps = build_runner.unit_deps(unit); - let no_embed_metadata = !build_runner.bcx.gctx.should_embed_metadata(); + let no_embed_metadata = !build_runner + .bcx + .gctx + .should_embed_metadata(build_runner.bcx.rustc()); let public_dependency_enabled = is_public_dependency_enabled(build_runner, unit); // Closure to add one dependency to `result`. diff --git a/src/context/mod.rs b/src/context/mod.rs index b743ea47168..e0763f26da8 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1269,8 +1269,19 @@ impl GlobalContext { self.extra_verbose } - pub fn should_embed_metadata(&self) -> bool { - self.cli_unstable().embed_metadata.unwrap_or(true) + pub fn should_embed_metadata(&self, rustc: &Rustc) -> bool { + match self.cli_unstable().embed_metadata { + Some(v) => v, + None => { + let cargo_nightly = matches!( + crate::version().release_channel.as_deref(), + Some("nightly" | "dev") + ); + // Enable -Zembed-metadata=no by default if both cargo and rustc are nightly + let is_nightly = cargo_nightly && rustc.is_nightly; + !is_nightly + } + } } pub fn network_allowed(&self) -> bool { diff --git a/src/ops/cargo_clean.rs b/src/ops/cargo_clean.rs index fa3830775db..82d7d9386df 100644 --- a/src/ops/cargo_clean.rs +++ b/src/ops/cargo_clean.rs @@ -298,9 +298,14 @@ fn clean_specs( ] { for (compile_kind, layout) in &layouts { let triple = target_data.short_name(compile_kind); - let (file_types, _unsupported) = target_data - .info(*compile_kind) - .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?; + let (file_types, _unsupported) = + target_data.info(*compile_kind).rustc_outputs( + mode, + target.kind(), + triple, + clean_ctx.gctx, + &target_data.rustc, + )?; let artifact_dir = layout .artifact_dir() .expect("artifact-dir was not locked during clean"); @@ -375,9 +380,14 @@ fn clean_specs( ] { for (compile_kind, layout) in &layouts { let triple = target_data.short_name(compile_kind); - let (file_types, _unsupported) = target_data - .info(*compile_kind) - .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?; + let (file_types, _unsupported) = + target_data.info(*compile_kind).rustc_outputs( + mode, + target.kind(), + triple, + clean_ctx.gctx, + &target_data.rustc, + )?; let artifact_dir = layout .artifact_dir() .expect("artifact-dir was not locked during clean"); diff --git a/src/util/rustc.rs b/src/util/rustc.rs index 1ab62ae0134..6ed123d7cbf 100644 --- a/src/util/rustc.rs +++ b/src/util/rustc.rs @@ -12,7 +12,7 @@ use tracing::{debug, info, warn}; use crate::compiler::apply_env_config; use crate::util::interning::InternedString; -use crate::util::{CargoResult, GlobalContext, StableHasher}; +use crate::util::{CargoResult, GlobalContext, StableHasher, VersionExt}; /// Information on the `rustc` executable #[derive(Debug)] @@ -32,6 +32,8 @@ pub struct Rustc { pub host: InternedString, /// The rustc full commit hash, this comes from `verbose_version`. pub commit_hash: Option, + /// Is the rustc on a nightly or a dev channel? + pub is_nightly: bool, cache: Mutex, } @@ -88,6 +90,8 @@ impl Rustc { verbose_version ) })?; + let is_nightly = + version.is_prerelease() && matches!(version.pre.as_str(), "dev" | "nightly"); let commit_hash = extract("commit-hash: ").ok().map(|hash| { // Possible commit-hash values from rustc are SHA hex string and "unknown". See: // * https://github.com/rust-lang/rust/blob/531cb83fc/src/bootstrap/src/utils/channel.rs#L73 @@ -114,6 +118,7 @@ impl Rustc { version, host, commit_hash, + is_nightly, cache: Mutex::new(cache), }) } diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 4f6ad78d869..4901b0c00c8 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -6539,7 +6539,7 @@ fn embed_metadata_no_invalidate() { ) .build(); - p.cargo("build -Z embed-metadata=no") + p.cargo("build -Z embed-metadata=yes") .masquerade_as_nightly_cargo(&["-Z embed-metadata"]) .with_stderr_data(str![[r#" [LOCKING] 1 package to highest compatible version @@ -6552,8 +6552,6 @@ fn embed_metadata_no_invalidate() { p.cargo("build") .masquerade_as_nightly_cargo(&["-Z embed-metadata"]) .with_stderr_data(str![[r#" -[COMPILING] bar v0.5.0 ([ROOT]/foo/bar) -[COMPILING] foo v0.5.0 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]]) diff --git a/tests/testsuite/build_script_env.rs b/tests/testsuite/build_script_env.rs index 6daf9a4dc47..cc09f075f14 100644 --- a/tests/testsuite/build_script_env.rs +++ b/tests/testsuite/build_script_env.rs @@ -142,6 +142,7 @@ fn rustc_bootstrap() { // NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc // (this matters when tests are being run with a beta or stable cargo) .env("RUSTC_BOOTSTRAP", "1") + .arg("-Zembed-metadata=yes") .with_stderr_data(str![[r#" [COMPILING] has-dashes v0.0.1 ([ROOT]/foo) [WARNING] has-dashes@0.0.1: cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`. @@ -187,6 +188,7 @@ fn rustc_bootstrap() { // NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc // (this matters when tests are being run with a beta or stable cargo) .env("RUSTC_BOOTSTRAP", "1") + .arg("-Zembed-metadata=yes") .with_stderr_data(str![[r#" [COMPILING] foo v0.0.1 ([ROOT]/foo) [WARNING] foo@0.0.1: cannot set `RUSTC_BOOTSTRAP=1` from build script of `foo v0.0.1 ([ROOT]/foo)`.