Skip to content
Draft
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
16 changes: 7 additions & 9 deletions doc/book/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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.
* [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.
* [embed-metadata](#embed-metadata) --- If set to `yes`, cargo will not pass `-Cembed-metadata=no` to the compiler, so metadata is embedded into rlib and dylib artifacts.
* [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 @@ -1911,19 +1911,17 @@ whether documentations are required to re-generate. This can be combined with
* Original Pull Request: [#15378](https://github.com/rust-lang/cargo/pull/15378)
* Tracking Issue: [#15495](https://github.com/rust-lang/cargo/issues/15495)

The default behavior of Rust is to embed crate metadata into `rlib` and `dylib` artifacts.
Since Cargo also passes `--emit=metadata` to these intermediate artifacts to enable pipelined
compilation, this means that a lot of metadata ends up being duplicated on disk, which wastes
disk space in the target directory.
By default, when rustc supports it, Cargo passes the `-Cembed-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.

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.
To embed metadata anyway, pass `-Zembed-metadata=yes`:

```console
cargo +nightly -Zembed-metadata=no build
cargo +nightly -Zembed-metadata=yes build
```

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

## `unstable-editions`

Expand Down
22 changes: 11 additions & 11 deletions src/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,19 @@ impl TargetInfo {
}
}

// TODO: Remove this and also `-Zembed-metadata` flag
// once Cargo' MSRV is at the version stabilizing `-Cembed-metadata`
// (possibly 1.101)
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
}
None => rustc
.cached_output(
crate_type_process
.clone()
.args(&["-C", "embed-metadata=no"]),
extra_fingerprint,
)
.is_err(),
};

return Ok(TargetInfo {
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ fn build_base_args(
.info(unit.kind)
.should_embed_metadata()
{
// Nightly rustc supports the -Zembed-metadata=no flag, which tells it to avoid including
// The -Cembed-metadata=no flag tells rustc 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.
// When we use this flag, we should also pass --emit=metadata to all artifacts that
Expand All @@ -1365,12 +1365,12 @@ fn build_base_args(
// files on disk.
if unit.benefits_from_no_embed_metadata() {
cmd.arg("--emit=dep-info,metadata,link");
cmd.args(&["-Z", "embed-metadata=no"]);
cmd.args(&["-C", "embed-metadata=no"]);
} else {
cmd.arg("--emit=dep-info,link");
}
} else {
// If we don't use -Zembed-metadata=no, we emit .rmeta files only for rlib outputs.
// If we don't use -Cembed-metadata=no, we emit .rmeta files only for rlib outputs.
// This metadata may be used in this session for a pipelined compilation, or it may
// be used in a future Cargo session as part of a pipelined compile.
if !unit.requires_upstream_objects() {
Expand Down Expand Up @@ -1927,7 +1927,7 @@ pub fn extern_args(
if output.flavor == FileFlavor::Linkable {
pass(&output.path);
}
// If we use -Zembed-metadata=no, we also need to pass the path to the
// If we use -Cembed-metadata=no, we also need to pass the path to the
// corresponding .rmeta file to the linkable artifact, because the
// normal dependency (rlib) doesn't contain the full metadata.
else if no_embed_metadata && output.flavor == FileFlavor::Rmeta {
Expand Down
22 changes: 7 additions & 15 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6450,7 +6450,7 @@ fn renamed_uplifted_artifact_remains_unmodified_after_rebuild() {
assert!(not_the_same, "renamed uplifted artifact must be unmodified");
}

#[cargo_test(nightly, reason = "-Zembed-metadata is nightly only")]
#[cargo_test(nightly, reason = "-Cembed-metadata requires rustc 1.101")]
fn embed_metadata_no() {
let p = project()
.file(
Expand Down Expand Up @@ -6478,11 +6478,8 @@ fn embed_metadata_no() {
)
.build();

p.cargo("build")
.arg("-Zembed-metadata=no")
.masquerade_as_nightly_cargo(&["-Z embed-metadata"])
.arg("-v")
.with_stderr_contains("[RUNNING] `[..]-Z embed-metadata=no[..]`")
p.cargo("build -v")
.with_stderr_contains("[RUNNING] `[..]-C embed-metadata=no[..]`")
.with_stderr_contains(
"[RUNNING] `[..]--extern bar=[ROOT]/foo/target/debug/build/bar/[HASH]/out/libbar-[HASH].rmeta[..]`",
)
Expand All @@ -6491,7 +6488,7 @@ fn embed_metadata_no() {

// Make sure that cargo passes --extern=<dep>.rmeta even if <dep>
// is compiled as a dylib.
#[cargo_test(nightly, reason = "-Zembed-metadata is nightly only")]
#[cargo_test(nightly, reason = "-Cembed-metadata requires rustc 1.101")]
fn embed_metadata_no_dylib_dep() {
let p = project()
.file(
Expand Down Expand Up @@ -6529,18 +6526,15 @@ fn embed_metadata_no_dylib_dep() {
)
.build();

p.cargo("build")
.arg("-Zembed-metadata=no")
.masquerade_as_nightly_cargo(&["-Z embed-metadata"])
.arg("-v")
.with_stderr_contains("[RUNNING] `[..]-Z embed-metadata=no[..]`")
p.cargo("build -v")
.with_stderr_contains("[RUNNING] `[..]-C embed-metadata=no[..]`")
.with_stderr_contains(
"[RUNNING] `[..]--extern bar=[ROOT]/foo/target/debug/build/bar/[HASH]/out/libbar.rmeta[..]`",
)
.run();
}

#[cargo_test(nightly, reason = "-Zembed-metadata is nightly only")]
#[cargo_test(nightly, reason = "-Cembed-metadata requires rustc 1.101")]
fn embed_metadata_no_invalidate() {
// Invalidate all deps when -Zembed-metadata is toggled
let p = project()
Expand Down Expand Up @@ -6570,8 +6564,6 @@ fn embed_metadata_no_invalidate() {
.build();

p.cargo("build")
.arg("-Zembed-metadata=no")
.masquerade_as_nightly_cargo(&["-Z embed-metadata"])
.with_stderr_data(str![[r#"
[LOCKING] 1 package to highest compatible version
[COMPILING] bar v0.5.0 ([ROOT]/foo/bar)
Expand Down
Loading