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
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ impl TargetInfo {
}
}
if !result.is_empty() {
if gctx.cli_unstable().no_embed_metadata
if !gctx.should_embed_metadata()
&& crate_types
.iter()
.any(|ct| ct.benefits_from_no_embed_metadata())
Expand Down
7 changes: 4 additions & 3 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ use std::fs;
use std::fs::File;
use std::hash::{self, Hash, Hasher};
use std::io::{self};
use std::ops::Not;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
Expand Down Expand Up @@ -1659,13 +1660,13 @@ fn calculate_normal(
&& build_runner.unit_deps(unit).iter().any(|dep| !dep.public)
&& super::is_public_dependency_enabled(build_runner, unit))
.hash(&mut config);
// -Zno-embed-metadata changes how all units are compiled, and it also changes how we tell
// -Zembed-metadata changes how all units are compiled, and it also changes how we tell
// rustc to link to deps using `--extern`. If it changes, we should rebuild everything.
build_runner
.bcx
.gctx
.cli_unstable()
.no_embed_metadata
.should_embed_metadata()
.not()
.hash(&mut config);

let compile_kind = unit.kind.fingerprint_hash();
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ fn build_base_args(

if unit.mode.is_check() {
cmd.arg("--emit=dep-info,metadata");
} else if build_runner.bcx.gctx.cli_unstable().no_embed_metadata {
} else if !build_runner.bcx.gctx.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 @@ -1947,7 +1947,7 @@ pub fn extern_args(
let mut result = Vec::new();
let deps = build_runner.unit_deps(unit);

let no_embed_metadata = build_runner.bcx.gctx.cli_unstable().no_embed_metadata;
let no_embed_metadata = !build_runner.bcx.gctx.should_embed_metadata();
let public_dependency_enabled = is_public_dependency_enabled(build_runner, unit);

// Closure to add one dependency to `result`.
Expand Down
28 changes: 25 additions & 3 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ enum Status {
Removed,
}

/// Config for the `-Zembed-metadata` option.
#[derive(Debug, Default, Deserialize)]
pub enum EmbedMetadata {
/// Embed metadata in .rlib files, the original rustc behavior.
Embed,
/// Do not embed metadata in .rlib files.
DoNotEmbed,
/// The `-Zembed-metadata` flag wasn't set.
#[default]
Unset,
}

/// A listing of stable and unstable new syntax in Cargo.toml.
///
/// This generates definitions and impls for [`Features`] and [`Feature`]
Expand Down Expand Up @@ -843,7 +855,8 @@ macro_rules! unstable_cli_options {
location.line()
);
let mut expected = vec![$(stringify!($element)),*];
expected[2..].sort();
// Skip permanently unstable fields
expected[3..].sort();
let expected = format!("{:#?}", expected);
let actual = format!("{:#?}", vec![$(stringify!($element)),*]);
snapbox::assert_data_eq!(actual, expected);
Expand All @@ -855,6 +868,7 @@ macro_rules! unstable_cli_options {
unstable_cli_options!(
// Permanently unstable features:
allow_features: Option<AllowFeatures> = ("Allow *only* the listed unstable features"),
embed_metadata: EmbedMetadata = ("Avoid embedding metadata in library artifacts"),
print_im_a_teapot: bool,

// All other unstable features.
Expand Down Expand Up @@ -893,7 +907,6 @@ unstable_cli_options!(
msrv_policy: bool = ("Enable rust-version aware policy within cargo"),
mtime_on_use: bool = ("Configure Cargo to update the mtime of used files"),
next_lockfile_bump: bool,
no_embed_metadata: bool = ("Avoid embedding metadata in library artifacts"),
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
panic_immediate_abort: bool = ("Enable setting `panic = \"immediate-abort\"` in profiles"),
Expand Down Expand Up @@ -1294,6 +1307,15 @@ impl CliUnstable {
}
}

fn parse_embed_metadata(key: &str, value: Option<&str>) -> CargoResult<EmbedMetadata> {
match value {
None => Ok(EmbedMetadata::Unset),
Some("yes") => Ok(EmbedMetadata::Embed),
Some("no") => Ok(EmbedMetadata::DoNotEmbed),
Some(s) => bail!("flag -Z{key} expected `no` or `yes`, found: `{s}`"),
}
}

/// Parse a comma-separated list
fn parse_list(value: Option<&str>) -> Vec<String> {
match value {
Expand Down Expand Up @@ -1345,6 +1367,7 @@ impl CliUnstable {
// Permanently unstable features
// Sorted alphabetically:
"allow-features" => self.allow_features = Some(parse_list(v).into_iter().collect()),
"embed-metadata" => self.embed_metadata = parse_embed_metadata(k, v)?,
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(k, v)?,

// Stabilized features
Expand Down Expand Up @@ -1446,7 +1469,6 @@ impl CliUnstable {
"msrv-policy" => self.msrv_policy = parse_empty(k, v)?,
// can also be set in .cargo/config or with and ENV
"mtime-on-use" => self.mtime_on_use = parse_empty(k, v)?,
"no-embed-metadata" => self.no_embed_metadata = parse_empty(k, v)?,
"no-index-update" => self.no_index_update = parse_empty(k, v)?,
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
"public-dependency" => self.public_dependency = parse_empty(k, v)?,
Expand Down
12 changes: 10 additions & 2 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ mod environment;
use environment::Env;

mod schema;
pub use schema::*;

use super::auth::RegistryConfig;
use crate::core::features::EmbedMetadata;
pub use schema::*;

/// Helper macro for creating typed access methods.
macro_rules! get_value_typed {
Expand Down Expand Up @@ -1261,6 +1261,14 @@ impl GlobalContext {
self.extra_verbose
}

pub fn should_embed_metadata(&self) -> bool {
match self.cli_unstable().embed_metadata {
EmbedMetadata::Embed => true,
EmbedMetadata::DoNotEmbed => false,
EmbedMetadata::Unset => true,
}
}

pub fn network_allowed(&self) -> bool {
!self.offline_flag().is_some()
}
Expand Down
11 changes: 6 additions & 5 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Each new feature described below should explain how to use it.
* [checksum-freshness](#checksum-freshness) --- When passed, the decision as to whether a crate needs to be rebuilt is made using file checksums instead of the file mtime.
* [panic-abort-tests](#panic-abort-tests) --- Allows running tests with the "abort" panic strategy.
* [host-config](#host-config) --- Allows setting `[target]`-like configuration settings for host build targets.
* [no-embed-metadata](#no-embed-metadata) --- Passes `-Zembed-metadata=no` to the compiler, which avoid embedding metadata into rlib and dylib artifacts, to save disk space.
* [embed-metadata](#embed-metadata) --- If set to `no`, cargo will pass `-Zembed-metadata=no` to the compiler, which avoid embedding metadata into rlib and dylib artifacts, to save disk space.
* [target-applies-to-host](#target-applies-to-host) --- Alters whether certain flags will be passed to host build targets.
* [gc](#gc) --- Global cache garbage collection.
* [open-namespaces](#open-namespaces) --- Allow multiple packages to participate in the same API namespace
Expand Down Expand Up @@ -1865,7 +1865,7 @@ The `-Z rustdoc-depinfo` flag leverages rustdoc's dep-info files to determine
whether documentations are required to re-generate. This can be combined with
`-Z checksum-freshness` to detect checksum changes rather than file mtime.

## no-embed-metadata
## embed-metadata
* Original Pull Request: [#15378](https://github.com/rust-lang/cargo/pull/15378)
* Tracking Issue: [#15495](https://github.com/rust-lang/cargo/issues/15495)

Expand All @@ -1874,14 +1874,15 @@ Since Cargo also passes `--emit=metadata` to these intermediate artifacts to ena
compilation, this means that a lot of metadata ends up being duplicated on disk, which wastes
disk space in the target directory.

This feature tells Cargo to pass the `-Zembed-metadata=no` flag to the compiler, which instructs
it not to embed metadata within rlib and dylib artifacts. In this case, the metadata will only
If you pass `-Zembed-metadata=no` to Cago, it will then pass the `-Zembed-metadata=no` flag to the compiler, which instructs it not to embed metadata within rlib and dylib artifacts. In this case, the metadata will only
be stored in `.rmeta` files.

```console
cargo +nightly -Zno-embed-metadata build
cargo +nightly -Zembed-metadata=no build
```

> Note that this flag is planned to be removed in the future, as the `no` behavior should become the default.

## `unstable-editions`

The `unstable-editions` value in the `cargo-features` list allows a `Cargo.toml` manifest to specify an edition that is not yet stable.
Expand Down
28 changes: 14 additions & 14 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6404,8 +6404,8 @@ fn renamed_uplifted_artifact_remains_unmodified_after_rebuild() {
assert!(not_the_same, "renamed uplifted artifact must be unmodified");
}

#[cargo_test(nightly, reason = "-Zno-embed-metadata is nightly only")]
fn no_embed_metadata() {
#[cargo_test(nightly, reason = "-Zembed-metadata is nightly only")]
fn embed_metadata_no() {
let p = project()
.file(
"Cargo.toml",
Expand All @@ -6432,8 +6432,8 @@ fn no_embed_metadata() {
)
.build();

p.cargo("build -Z no-embed-metadata")
.masquerade_as_nightly_cargo(&["-Z no-embed-metadata"])
p.cargo("build -Z embed-metadata=no")
.masquerade_as_nightly_cargo(&["-Z embed-metadata"])
.arg("-v")
.with_stderr_contains("[RUNNING] `[..]-Z embed-metadata=no[..]`")
.with_stderr_contains(
Expand All @@ -6444,8 +6444,8 @@ fn no_embed_metadata() {

// Make sure that cargo passes --extern=<dep>.rmeta even if <dep>
// is compiled as a dylib.
#[cargo_test(nightly, reason = "-Zno-embed-metadata is nightly only")]
fn no_embed_metadata_dylib_dep() {
#[cargo_test(nightly, reason = "-Zembed-metadata is nightly only")]
fn embed_metadata_no_dylib_dep() {
let p = project()
.file(
"Cargo.toml",
Expand Down Expand Up @@ -6482,8 +6482,8 @@ fn no_embed_metadata_dylib_dep() {
)
.build();

p.cargo("build -Z no-embed-metadata")
.masquerade_as_nightly_cargo(&["-Z no-embed-metadata"])
p.cargo("build -Z embed-metadata=no")
.masquerade_as_nightly_cargo(&["-Z embed-metadata"])
.arg("-v")
.with_stderr_contains("[RUNNING] `[..]-Z embed-metadata=no[..]`")
.with_stderr_contains(
Expand All @@ -6492,9 +6492,9 @@ fn no_embed_metadata_dylib_dep() {
.run();
}

#[cargo_test(nightly, reason = "-Zno-embed-metadata is nightly only")]
fn no_embed_metadata_invalidate() {
// Invalidate all deps when -Zno-embed-metadata is toggled
#[cargo_test(nightly, reason = "-Zembed-metadata is nightly only")]
fn embed_metadata_no_invalidate() {
// Invalidate all deps when -Zembed-metadata is toggled
let p = project()
.file(
"Cargo.toml",
Expand All @@ -6521,8 +6521,8 @@ fn no_embed_metadata_invalidate() {
)
.build();

p.cargo("build -Z no-embed-metadata")
.masquerade_as_nightly_cargo(&["-Z no-embed-metadata"])
p.cargo("build -Z embed-metadata=no")
.masquerade_as_nightly_cargo(&["-Z embed-metadata"])
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[COMPILING] bar v0.5.0 ([ROOT]/foo/bar)
Expand All @@ -6532,7 +6532,7 @@ fn no_embed_metadata_invalidate() {
"#]])
.run();
p.cargo("build")
.masquerade_as_nightly_cargo(&["-Z no-embed-metadata"])
.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)
Expand Down
Loading