Skip to content

Commit 6a05ed9

Browse files
committed
Append SBOM file suffix instead of replacing
Instead of replacing the file extension, the `.cargo-sbom.json` suffix is appended to the output file. This is to keep existing file extensions in place. * refactor logic to set `sbom` property from build config * expand build script related test to check JSON output
1 parent 3baf221 commit 6a05ed9

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,16 @@ impl BuildConfig {
105105
}
106106

107107
// If sbom flag is set, it requires the unstable feature
108-
let mut sbom = match gctx.get_env_os("CARGO_BUILD_SBOM") {
109-
Some(sbom) => sbom == "true",
110-
None => cfg.sbom == Some(true),
108+
let sbom = match (cfg.sbom, gctx.cli_unstable().sbom) {
109+
(Some(sbom), true) => sbom,
110+
(Some(_), false) => {
111+
gctx.shell()
112+
.warn("ignoring 'sbom' config, pass `-Zsbom` to enable it")?;
113+
false
114+
}
115+
(None, _) => false,
111116
};
112117

113-
if sbom && !gctx.cli_unstable().sbom {
114-
gctx.shell()
115-
.warn("ignoring 'sbom' config, pass `-Zsbom` to enable it")?;
116-
sbom = false;
117-
}
118-
119118
Ok(BuildConfig {
120119
requested_kinds,
121120
jobs,

src/cargo/core/compiler/build_runner/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,21 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
427427
///
428428
/// Only call this function when `sbom` is active.
429429
pub fn sbom_output_files(&self, unit: &Unit) -> CargoResult<Vec<PathBuf>> {
430+
const SBOM_FILE_EXTENSION: &str = ".cargo-sbom.json";
431+
432+
fn append_sbom_suffix(link: &PathBuf, suffix: &str) -> PathBuf {
433+
let mut link_buf = link.clone().into_os_string();
434+
link_buf.push(suffix);
435+
PathBuf::from(link_buf)
436+
}
437+
430438
assert!(self.bcx.build_config.sbom);
431439
let files = self
432440
.outputs(unit)?
433441
.iter()
434442
.filter(|o| matches!(o.flavor, FileFlavor::Normal | FileFlavor::Linkable))
435443
.filter_map(|output_file| output_file.hardlink.as_ref())
436-
.map(|link_dst| link_dst.with_extension("cargo-sbom.json"))
444+
.map(|link| append_sbom_suffix(link, SBOM_FILE_EXTENSION))
437445
.collect::<Vec<_>>();
438446
Ok(files)
439447
}

tests/testsuite/sbom.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,77 @@ fn build_sbom_with_simple_build_script() {
181181

182182
let path = p.bin("foo").with_extension("cargo-sbom.json");
183183
assert!(path.is_file());
184+
185+
assert_json_output(
186+
path,
187+
r#"
188+
{
189+
"format_version": 1,
190+
"package_id": "path+file:///[..]/foo#0.0.1",
191+
"name": "foo",
192+
"version": "0.0.1",
193+
"source": "[ROOT]/foo",
194+
"target": {
195+
"kind": [
196+
"bin"
197+
],
198+
"crate_type": "bin",
199+
"name": "foo",
200+
"edition": "2015"
201+
},
202+
"profile": {
203+
"name": "dev",
204+
"opt_level": "0",
205+
"lto": "false",
206+
"codegen_backend": null,
207+
"codegen_units": null,
208+
"debuginfo": 2,
209+
"split_debuginfo": "{...}",
210+
"debug_assertions": true,
211+
"overflow_checks": true,
212+
"rpath": false,
213+
"incremental": false,
214+
"panic": "unwind",
215+
"strip": {
216+
"deferred": "None"
217+
}
218+
},
219+
"packages": [
220+
{
221+
"build_type": "build",
222+
"dependencies": [
223+
{
224+
"features": [],
225+
"name": "foo",
226+
"package_id": "foo 0.0.1 (path+file:///[..]/foo)",
227+
"version": "0.0.1"
228+
}
229+
],
230+
"extern_crate_name": "build_script_build",
231+
"features": [],
232+
"package": "foo",
233+
"package_id": "foo 0.0.1 (path+file:///[..]/foo)",
234+
"version": "0.0.1"
235+
},
236+
{
237+
"package_id": "foo 0.0.1 (path+file:///[..]/foo)",
238+
"package": "foo",
239+
"version": "0.0.1",
240+
"features": [],
241+
"build_type": "normal",
242+
"extern_crate_name": "build_script_build",
243+
"dependencies": []
244+
}
245+
],
246+
"features": [],
247+
"rustc": {
248+
"version": "[..]",
249+
"wrapper": null,
250+
"commit_hash": "[..]",
251+
"host": "[..]"
252+
}
253+
}"#,
254+
);
184255
}
185256

186257
#[cargo_test]

0 commit comments

Comments
 (0)