Skip to content
Merged
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
31 changes: 27 additions & 4 deletions src/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub struct TargetInfo {
pub rustflags: Rc<[String]>,
/// Extra flags to pass to `rustdoc`, see [`extra_args`].
pub rustdocflags: Rc<[String]>,
/// Should metadata be embedded in .rlib files?
/// Corresponds to `-Zembed-metadata`.
pub should_embed_metadata: bool,
}

/// Kind of each file generated by a Unit, part of `FileType`.
Expand Down Expand Up @@ -344,6 +347,21 @@ impl TargetInfo {
}
}

let should_embed_metadata = match gctx.cli_unstable().embed_metadata {
Some(v) => v,
None => {
let cargo_nightly = matches!(
crate::version().release_channel.as_deref(),
Some("nightly" | "dev")
);
let rustc_nightly = matches!(rustc.version.pre.as_str(), "dev" | "nightly");

// Enable -Zembed-metadata=no by default if both cargo and rustc are nightly
let is_nightly = cargo_nightly && rustc_nightly;
!is_nightly
}
};

return Ok(TargetInfo {
crate_type_process,
crate_types: RefCell::new(map),
Expand All @@ -361,6 +379,7 @@ impl TargetInfo {
cfg,
supports_std,
support_split_debuginfo,
should_embed_metadata,
});
}
}
Expand Down Expand Up @@ -573,10 +592,9 @@ impl TargetInfo {
mode: CompileMode,
target_kind: &TargetKind,
target_triple: &str,
gctx: &GlobalContext,
) -> CargoResult<(Vec<FileType>, Vec<CrateType>)> {
match mode {
CompileMode::Build => self.calc_rustc_outputs(target_kind, target_triple, gctx),
CompileMode::Build => self.calc_rustc_outputs(target_kind, target_triple),
CompileMode::Test => {
match self.file_types(&CrateType::Bin, FileFlavor::Normal, target_triple)? {
Some(fts) => Ok((fts, Vec::new())),
Expand All @@ -597,7 +615,6 @@ impl TargetInfo {
&self,
target_kind: &TargetKind,
target_triple: &str,
gctx: &GlobalContext,
) -> CargoResult<(Vec<FileType>, Vec<CrateType>)> {
let mut unsupported = Vec::new();
let mut result = Vec::new();
Expand All @@ -619,7 +636,7 @@ impl TargetInfo {
}
}
if !result.is_empty() {
if !gctx.should_embed_metadata()
if !self.should_embed_metadata()
&& crate_types
.iter()
.any(|ct| ct.benefits_from_no_embed_metadata())
Expand Down Expand Up @@ -650,6 +667,12 @@ impl TargetInfo {
pub fn maybe_support_std(&self) -> bool {
matches!(self.supports_std, Some(true) | None)
}

/// Should crate metadata be embedded into .rlib files?
/// If not, they will only be stored in .rmeta files.
pub fn should_embed_metadata(&self) -> bool {
self.should_embed_metadata
}
}

/// Takes rustc output (using specialized command line args), and calculates the file prefix and
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/build_runner/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
CompileMode::Build,
&TargetKind::Bin,
bcx.target_data.short_name(&kind),
bcx.gctx,
)
.expect("target must support `bin`");

Expand Down Expand Up @@ -664,7 +663,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)?;
if file_types.is_empty() {
if !unsupported.is_empty() {
let unsupported_strs: Vec<_> = unsupported.iter().map(|ct| ct.as_str()).collect();
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,8 @@ fn calculate_normal(
// rustc to link to deps using `--extern`. If it changes, we should rebuild everything.
build_runner
.bcx
.gctx
.target_data
.info(unit.kind)
.should_embed_metadata()
.not()
.hash(&mut config);
Expand Down
13 changes: 11 additions & 2 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,12 @@ 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
.target_data
.info(unit.kind)
.should_embed_metadata()
{
// 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.
Expand Down Expand Up @@ -1814,7 +1819,11 @@ 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
.target_data
.info(unit.kind)
.should_embed_metadata();
let public_dependency_enabled = is_public_dependency_enabled(build_runner, unit);

// Closure to add one dependency to `result`.
Expand Down
4 changes: 0 additions & 4 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,10 +1269,6 @@ impl GlobalContext {
self.extra_verbose
}

pub fn should_embed_metadata(&self) -> bool {
self.cli_unstable().embed_metadata.unwrap_or(true)
}

pub fn network_allowed(&self) -> bool {
!self.offline_flag().is_some()
}
Expand Down
4 changes: 2 additions & 2 deletions src/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ fn clean_specs(
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)?;
.rustc_outputs(mode, target.kind(), triple)?;
let artifact_dir = layout
.artifact_dir()
.expect("artifact-dir was not locked during clean");
Expand Down Expand Up @@ -377,7 +377,7 @@ fn clean_specs(
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)?;
.rustc_outputs(mode, target.kind(), triple)?;
let artifact_dir = layout
.artifact_dir()
.expect("artifact-dir was not locked during clean");
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6549,7 +6549,7 @@ fn embed_metadata_no_invalidate() {

"#]])
.run();
p.cargo("build")
p.cargo("build -Z embed-metadata=yes")
.masquerade_as_nightly_cargo(&["-Z embed-metadata"])
.with_stderr_data(str![[r#"
[COMPILING] bar v0.5.0 ([ROOT]/foo/bar)
Expand Down