Skip to content

Commit 36480ce

Browse files
committed
Plumb rustc -Zhint-mostly-unused flag through as a profile option
The rustc `-Zhint-mostly-unused` flag tells rustc that most of a crate will go unused. This is useful for speeding up compilation of large dependencies from which you only use a few items. Plumb that option through as a profile option, to allow specifying it for specific dependencies: ```toml [profile.dev.package.huge-mostly-unused-dependency] hint-mostly-unused = true ``` To enable this feature, pass `-Zprofile-hint-mostly-unused`. However, since this option is a hint, using it without passing `-Zprofile-hint-mostly-unused` will only warn and ignore the profile option. Versions of Cargo prior to the introduction of this feature will give an "unused manifest key" warning, but will otherwise function without erroring. This allows using the hint in a crate's `Cargo.toml` without mandating the use of a newer Cargo to build it. Add a test verifying that the profile option gets ignored with a warning without passing `-Zprofile-hint-mostly-unused`, and another test verifying that it gets handled when passing `-Zprofile-hint-mostly-unused`.
1 parent 8789c2b commit 36480ce

File tree

9 files changed

+182
-46
lines changed

9 files changed

+182
-46
lines changed

crates/cargo-util-schemas/manifest.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,14 @@
13851385
}
13861386
],
13871387
"default": null
1388+
},
1389+
"hint-mostly-unused": {
1390+
"description": "Unstable feature `hint-mostly-unused`",
1391+
"type": [
1392+
"boolean",
1393+
"null"
1394+
],
1395+
"default": null
13881396
}
13891397
}
13901398
},

crates/cargo-util-schemas/src/manifest/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ pub struct TomlProfile {
907907
pub build_override: Option<Box<TomlProfile>>,
908908
/// Unstable feature `-Ztrim-paths`.
909909
pub trim_paths: Option<TomlTrimPaths>,
910+
/// Unstable feature `hint-mostly-unused`
911+
pub hint_mostly_unused: Option<bool>,
910912
}
911913

912914
impl TomlProfile {
@@ -998,6 +1000,10 @@ impl TomlProfile {
9981000
if let Some(v) = &profile.trim_paths {
9991001
self.trim_paths = Some(v.clone())
10001002
}
1003+
1004+
if let Some(v) = profile.hint_mostly_unused {
1005+
self.hint_mostly_unused = Some(v);
1006+
}
10011007
}
10021008
}
10031009

src/cargo/core/compiler/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ fn build_base_args(
11351135
strip,
11361136
rustflags: profile_rustflags,
11371137
trim_paths,
1138+
hint_mostly_unused,
11381139
..
11391140
} = unit.profile.clone();
11401141
let test = unit.mode.is_any_test();
@@ -1325,6 +1326,16 @@ fn build_base_args(
13251326
opt(cmd, "-C", "incremental=", Some(dir));
13261327
}
13271328

1329+
if hint_mostly_unused {
1330+
if bcx.gctx.cli_unstable().profile_hint_mostly_unused {
1331+
cmd.arg("-Zhint-mostly-unused");
1332+
} else {
1333+
bcx.gctx
1334+
.shell()
1335+
.warn("ignoring 'hint-mostly-unused' profile option, pass `-Zprofile-hint-mostly-unused` to enable it")?;
1336+
}
1337+
}
1338+
13281339
let strip = strip.into_inner();
13291340
if strip != StripInner::None {
13301341
cmd.arg("-C").arg(format!("strip={}", strip));

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ unstable_cli_options!(
845845
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
846846
package_workspace: bool = ("Handle intra-workspace dependencies when packaging"),
847847
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
848+
profile_hint_mostly_unused: bool = ("Enable the `hint-mostly-unused` setting in profiles to mark a crate as mostly unused."),
848849
profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"),
849850
public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"),
850851
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
@@ -1366,6 +1367,7 @@ impl CliUnstable {
13661367
"package-workspace" => self.package_workspace = parse_empty(k, v)?,
13671368
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
13681369
"public-dependency" => self.public_dependency = parse_empty(k, v)?,
1370+
"profile-hint-mostly-unused" => self.profile_hint_mostly_unused = parse_empty(k, v)?,
13691371
"profile-rustflags" => self.profile_rustflags = parse_empty(k, v)?,
13701372
"trim-paths" => self.trim_paths = parse_empty(k, v)?,
13711373
"publish-timeout" => self.publish_timeout = parse_empty(k, v)?,

src/cargo/core/profiles.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
577577
if let Some(trim_paths) = &toml.trim_paths {
578578
profile.trim_paths = Some(trim_paths.clone());
579579
}
580+
if let Some(hint_mostly_unused) = toml.hint_mostly_unused {
581+
profile.hint_mostly_unused = hint_mostly_unused;
582+
}
580583
profile.strip = match toml.strip {
581584
Some(StringOrBool::Bool(true)) => Strip::Resolved(StripInner::Named("symbols".into())),
582585
Some(StringOrBool::Bool(false)) => Strip::Resolved(StripInner::None),
@@ -626,6 +629,8 @@ pub struct Profile {
626629
// remove when `-Ztrim-paths` is stablized
627630
#[serde(skip_serializing_if = "Option::is_none")]
628631
pub trim_paths: Option<TomlTrimPaths>,
632+
#[serde(skip_serializing_if = "std::ops::Not::not")]
633+
pub hint_mostly_unused: bool,
629634
}
630635

631636
impl Default for Profile {
@@ -647,6 +652,7 @@ impl Default for Profile {
647652
strip: Strip::Deferred(StripInner::None),
648653
rustflags: vec![],
649654
trim_paths: None,
655+
hint_mostly_unused: false,
650656
}
651657
}
652658
}
@@ -676,6 +682,7 @@ compact_debug! {
676682
strip
677683
rustflags
678684
trim_paths
685+
hint_mostly_unused
679686
)]
680687
}
681688
}

src/doc/src/reference/unstable.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,25 @@ profile-rustflags = true
924924
rustflags = [ "-C", "..." ]
925925
```
926926

927+
## Profile `hint-mostly-unused` option
928+
* Tracking Issue: [#15644](https://github.com/rust-lang/cargo/issues/15644)
929+
930+
This feature provides a new option in the `[profile]` section to enable the
931+
rustc `hint-mostly-unused` option. This is primarily useful to enable for
932+
specific dependencies:
933+
934+
```toml
935+
[profile.dev.package.huge-mostly-unused-dependency]
936+
hint-mostly-unused = true
937+
```
938+
939+
To enable this feature, pass `-Zprofile-hint-mostly-unused`. However, since
940+
this option is a hint, using it without passing `-Zprofile-hint-mostly-unused`
941+
will only warn and ignore the profile option. Versions of Cargo prior to the
942+
introduction of this feature will give an "unused manifest key" warning, but
943+
will otherwise function without erroring. This allows using the hint in a
944+
crate's `Cargo.toml` without mandating the use of a newer Cargo to build it.
945+
927946
## rustdoc-map
928947
* Tracking Issue: [#8296](https://github.com/rust-lang/cargo/issues/8296)
929948

tests/testsuite/cargo/z_help/stdout.term.svg

Lines changed: 48 additions & 46 deletions
Loading

tests/testsuite/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,7 @@ fn all_profile_options() {
16661666
build_override: None,
16671667
rustflags: None,
16681668
trim_paths: None,
1669+
hint_mostly_unused: None,
16691670
};
16701671
let mut overrides = BTreeMap::new();
16711672
let key = cargo_toml::ProfilePackageSpec::Spec(PackageIdSpec::parse("foo").unwrap());

0 commit comments

Comments
 (0)