Skip to content

Commit 96d1d3a

Browse files
committed
Guard sbom logic behind unstable feature
1 parent 27003f3 commit 96d1d3a

File tree

6 files changed

+39
-18
lines changed

6 files changed

+39
-18
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,16 @@ impl BuildConfig {
104104
anyhow::bail!("-Zbuild-std requires --target");
105105
}
106106

107+
// If sbom flag is set, it requires the unstable feature
107108
let sbom = match gctx.get_env_os("CARGO_BUILD_SBOM") {
108109
Some(sbom) => sbom == "true",
109110
None => cfg.sbom == Some(true),
110111
};
111112

113+
if sbom && !gctx.cli_unstable().sbom {
114+
anyhow::bail!("Cargo build config 'sbom' is unstable; pass `-Zsbom` to enable it");
115+
}
116+
112117
Ok(BuildConfig {
113118
requested_kinds,
114119
jobs,

src/cargo/core/compiler/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,9 @@ where
674674
/// completion of other units will be added later in runtime, such as flags
675675
/// from build scripts.
676676
fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
677+
let gctx = build_runner.bcx.gctx;
677678
let is_primary = build_runner.is_primary_package(unit);
678679
let is_workspace = build_runner.bcx.ws.is_member(&unit.pkg);
679-
let sbom = build_runner.bcx.build_config.sbom;
680680

681681
let mut base = build_runner
682682
.compilation
@@ -687,14 +687,14 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
687687
build_deps_args(&mut base, build_runner, unit)?;
688688
add_cap_lints(build_runner.bcx, unit, &mut base);
689689
base.args(build_runner.bcx.rustflags_args(unit));
690-
if build_runner.bcx.gctx.cli_unstable().binary_dep_depinfo {
690+
if gctx.cli_unstable().binary_dep_depinfo {
691691
base.arg("-Z").arg("binary-dep-depinfo");
692692
}
693693

694694
if is_primary {
695695
base.env("CARGO_PRIMARY_PACKAGE", "1");
696696

697-
if sbom {
697+
if gctx.cli_unstable().sbom && build_runner.bcx.build_config.sbom {
698698
let file_list = build_runner
699699
.sbom_output_files(unit)?
700700
.iter()

src/cargo/core/features.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ unstable_cli_options!(
773773
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
774774
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
775775
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
776+
sbom: bool = ("Enable the `sbom` option in build config in .cargo/config.toml file"),
776777
script: bool = ("Enable support for single-file, `.rs` packages"),
777778
separate_nightlies: bool,
778779
skip_rustdoc_fingerprint: bool,
@@ -1155,9 +1156,10 @@ impl CliUnstable {
11551156
"publish-timeout" => self.publish_timeout = parse_empty(k, v)?,
11561157
"rustdoc-map" => self.rustdoc_map = parse_empty(k, v)?,
11571158
"rustdoc-scrape-examples" => self.rustdoc_scrape_examples = parse_empty(k, v)?,
1159+
"sbom" => self.sbom = parse_empty(k, v)?,
1160+
"script" => self.script = parse_empty(k, v)?,
11581161
"separate-nightlies" => self.separate_nightlies = parse_empty(k, v)?,
11591162
"skip-rustdoc-fingerprint" => self.skip_rustdoc_fingerprint = parse_empty(k, v)?,
1160-
"script" => self.script = parse_empty(k, v)?,
11611163
"target-applies-to-host" => self.target_applies_to_host = parse_empty(k, v)?,
11621164
"unstable-options" => self.unstable_options = parse_empty(k, v)?,
11631165
_ => bail!("\

src/cargo/util/context/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,7 @@ pub struct CargoBuildConfig {
25812581
pub rustc: Option<ConfigRelativePath>,
25822582
pub rustdoc: Option<ConfigRelativePath>,
25832583
pub out_dir: Option<ConfigRelativePath>,
2584+
/// Unstable feature `-Zsbom`.
25842585
pub sbom: Option<bool>,
25852586
}
25862587

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

Lines changed: 12 additions & 10 deletions
Loading

tests/testsuite/sbom.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ fn build_sbom_using_cargo_config() {
3434
.file("src/main.rs", r#"fn main() {}"#)
3535
.build();
3636

37-
p.cargo("build").run();
37+
p.cargo("build -Zsbom")
38+
.masquerade_as_nightly_cargo(&["sbom"])
39+
.run();
3840

3941
let file = p.bin("foo").with_extension("cargo-sbom.json");
4042
assert!(file.is_file());
@@ -47,7 +49,10 @@ fn build_sbom_using_env_var() {
4749
.file("src/foo.rs", r#"fn main() {}"#)
4850
.build();
4951

50-
p.cargo("build").env("CARGO_BUILD_SBOM", "true").run();
52+
p.cargo("build -Zsbom")
53+
.env("CARGO_BUILD_SBOM", "true")
54+
.masquerade_as_nightly_cargo(&["sbom"])
55+
.run();
5156

5257
let file = p.bin("foo").with_extension("cargo-sbom.json");
5358
assert!(file.is_file());
@@ -72,7 +77,10 @@ fn build_sbom_project_bin_and_lib() {
7277
.file("src/lib.rs", r#"pub fn give_five() -> i32 { 5 }"#)
7378
.build();
7479

75-
p.cargo("build").stream().run();
80+
p.cargo("build -Zsbom")
81+
.stream()
82+
.masquerade_as_nightly_cargo(&["sbom"])
83+
.run();
7684

7785
assert!(p.bin("foo").with_extension("cargo-sbom.json").is_file());
7886
assert_eq!(
@@ -102,7 +110,10 @@ fn build_sbom_with_simple_build_script() {
102110
)
103111
.build();
104112

105-
p.cargo("build").stream().run();
113+
p.cargo("build -Zsbom")
114+
.stream()
115+
.masquerade_as_nightly_cargo(&["sbom"])
116+
.run();
106117

107118
let path = p.bin("foo").with_extension("cargo-sbom.json");
108119
assert!(path.is_file());

0 commit comments

Comments
 (0)