Skip to content

Commit 33edacd

Browse files
committed
Auto merge of #9118 - bjorn3:profile_codegen_backend_option, r=joshtriplett
Add a profile option to select the codegen backend This makes it possible to compile only specific crates with a certain codegen backend. I have tested this by compiling Bevy with cg_llvm, but one of the examples using cg_clif. By the way I noticed that many unstable profile options are not checked when using profile overrides.
2 parents bcd7fba + e96309a commit 33edacd

File tree

6 files changed

+181
-121
lines changed

6 files changed

+181
-121
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ fn build_base_args(
796796
let bcx = cx.bcx;
797797
let Profile {
798798
ref opt_level,
799+
codegen_backend,
799800
codegen_units,
800801
debuginfo,
801802
debug_assertions,
@@ -860,6 +861,10 @@ fn build_base_args(
860861
}
861862
}
862863

864+
if let Some(backend) = codegen_backend {
865+
cmd.arg("-Z").arg(&format!("codegen-backend={}", backend));
866+
}
867+
863868
if let Some(n) = codegen_units {
864869
cmd.arg("-C").arg(&format!("codegen-units={}", n));
865870
}

src/cargo/core/features.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ features! {
402402

403403
// Allow to specify per-package targets (compile kinds)
404404
(unstable, per_package_target, "", "reference/unstable.html#per-package-target"),
405+
406+
// Allow to specify which codegen backend should be used.
407+
(unstable, codegen_backend, "", "reference/unstable.html#codegen-backend"),
405408
}
406409

407410
pub struct Feature {

src/cargo/core/profiles.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
565565
Some(StringOrBool::String(ref n)) => profile.lto = Lto::Named(InternedString::new(n)),
566566
None => {}
567567
}
568+
if toml.codegen_backend.is_some() {
569+
profile.codegen_backend = toml.codegen_backend;
570+
}
568571
if toml.codegen_units.is_some() {
569572
profile.codegen_units = toml.codegen_units;
570573
}
@@ -626,6 +629,8 @@ pub struct Profile {
626629
pub root: ProfileRoot,
627630
pub lto: Lto,
628631
// `None` means use rustc default.
632+
pub codegen_backend: Option<InternedString>,
633+
// `None` means use rustc default.
629634
pub codegen_units: Option<u32>,
630635
pub debuginfo: Option<u32>,
631636
pub split_debuginfo: Option<InternedString>,
@@ -644,6 +649,7 @@ impl Default for Profile {
644649
opt_level: InternedString::new("0"),
645650
root: ProfileRoot::Debug,
646651
lto: Lto::Bool(false),
652+
codegen_backend: None,
647653
codegen_units: None,
648654
debuginfo: None,
649655
debug_assertions: false,
@@ -670,6 +676,7 @@ compact_debug! {
670676
opt_level
671677
lto
672678
root
679+
codegen_backend
673680
codegen_units
674681
debuginfo
675682
split_debuginfo
@@ -757,6 +764,7 @@ impl Profile {
757764
(
758765
self.opt_level,
759766
self.lto,
767+
self.codegen_backend,
760768
self.codegen_units,
761769
self.debuginfo,
762770
self.split_debuginfo,

src/cargo/util/toml/mod.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ pub enum U32OrBool {
433433
pub struct TomlProfile {
434434
pub opt_level: Option<TomlOptLevel>,
435435
pub lto: Option<StringOrBool>,
436+
pub codegen_backend: Option<InternedString>,
436437
pub codegen_units: Option<u32>,
437438
pub debug: Option<U32OrBool>,
438439
pub split_debuginfo: Option<String>,
@@ -491,12 +492,12 @@ impl TomlProfile {
491492
) -> CargoResult<()> {
492493
if let Some(ref profile) = self.build_override {
493494
features.require(Feature::profile_overrides())?;
494-
profile.validate_override("build-override")?;
495+
profile.validate_override("build-override", features)?;
495496
}
496497
if let Some(ref packages) = self.package {
497498
features.require(Feature::profile_overrides())?;
498499
for profile in packages.values() {
499-
profile.validate_override("package")?;
500+
profile.validate_override("package", features)?;
500501
}
501502
}
502503

@@ -562,6 +563,18 @@ impl TomlProfile {
562563
if self.strip.is_some() {
563564
features.require(Feature::strip())?;
564565
}
566+
567+
if let Some(codegen_backend) = &self.codegen_backend {
568+
features.require(Feature::codegen_backend())?;
569+
if codegen_backend.contains(|c: char| !c.is_ascii_alphanumeric() && c != '_') {
570+
bail!(
571+
"`profile.{}.codegen-backend` setting of `{}` is not a valid backend name.",
572+
name,
573+
codegen_backend,
574+
);
575+
}
576+
}
577+
565578
Ok(())
566579
}
567580

@@ -642,7 +655,7 @@ impl TomlProfile {
642655
Ok(())
643656
}
644657

645-
fn validate_override(&self, which: &str) -> CargoResult<()> {
658+
fn validate_override(&self, which: &str, features: &Features) -> CargoResult<()> {
646659
if self.package.is_some() {
647660
bail!("package-specific profiles cannot be nested");
648661
}
@@ -658,6 +671,9 @@ impl TomlProfile {
658671
if self.rpath.is_some() {
659672
bail!("`rpath` may not be specified in a `{}` profile", which)
660673
}
674+
if self.codegen_backend.is_some() {
675+
features.require(Feature::codegen_backend())?;
676+
}
661677
Ok(())
662678
}
663679

@@ -671,6 +687,10 @@ impl TomlProfile {
671687
self.lto = Some(v.clone());
672688
}
673689

690+
if let Some(v) = profile.codegen_backend {
691+
self.codegen_backend = Some(v);
692+
}
693+
674694
if let Some(v) = profile.codegen_units {
675695
self.codegen_units = Some(v);
676696
}

src/doc/src/reference/unstable.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,3 +1406,21 @@ environment variables.
14061406
The `rust-version` field in `Cargo.toml` has been stabilized in the 1.56 release.
14071407
See the [rust-version field](manifest.html#the-rust-version-field) for more
14081408
information on using the `rust-version` field and the `--ignore-rust-version` option.
1409+
1410+
### codegen-backend
1411+
1412+
The `codegen-backend` feature makes it possible to select the codegen backend used by rustc using a
1413+
profile.
1414+
1415+
Example:
1416+
1417+
```toml
1418+
[package]
1419+
name = "foo"
1420+
1421+
[dependencies]
1422+
serde = "1.0.117"
1423+
1424+
[profile.dev.package.foo]
1425+
codegen-backend = "cranelift"
1426+
```

0 commit comments

Comments
 (0)