11#! /bin/bash
22# Writes a CycloneDX SBOM (<binary>.cdx.json) for each binary in $1, from the <binary>.toolchain file its build
33# left there, then removes the toolchain files. Nothing in the output depends on when or where this runs.
4+ # Needs the pinned Rust toolchain with its rust-src component, for the versions of the crates std is built from.
45set -euo pipefail
56
67dir=$1
8+ sysroot=$( cd native && rustc --print sysroot)
9+ rustc_version=$( cd native && rustc --version | cut -d' ' -f2)
10+ std_lock=" $sysroot /lib/rustlib/src/rust/library/Cargo.lock"
11+ [ -f " $std_lock " ] || { echo " no $std_lock , install the rust-src component" >&2 ; exit 1; }
712version=$( sed -n ' 1,/^\[/!b; s/^version = "\(.*\)"$/\1/p' native/Cargo.toml)
813zstd=$( sed -n ' s/.* v\([0-9.]*\) release.*/\1/p' native/zstd/README.md)
914zstd_sha=$( sed -n ' s/^SHA-256: //p' native/zstd/README.md)
@@ -29,13 +34,57 @@ tool() {
2934 jq -n --arg name " $1 " --arg version " $2 " ' {type: "application", name: $name, version: $version}'
3035}
3136
32- # the Rust standard library, linked statically on every platform
33- rust_std () {
34- jq -n --arg version " $( tc rustc) " ' {
35- type: "library", "bom-ref": "rust-std", name: "Rust standard library", version: $version,
36- licenses: [{expression: "MIT OR Apache-2.0"}],
37- purl: "pkg:github/rust-lang/rust@\($version)"
38- }'
37+ # crates.io crates std is built from, with their licenses
38+ declare -A crate_license=(
39+ [addr2line]=" Apache-2.0 OR MIT"
40+ [adler2]=" 0BSD OR MIT OR Apache-2.0"
41+ [gimli]=" MIT OR Apache-2.0"
42+ [hashbrown]=" MIT OR Apache-2.0"
43+ [libc]=" MIT OR Apache-2.0"
44+ [miniz_oxide]=" MIT OR Zlib OR Apache-2.0"
45+ [object]=" Apache-2.0 OR MIT"
46+ [rustc-demangle]=" MIT OR Apache-2.0"
47+ )
48+
49+ # Rust's std and the crates it brings into the binary. Crates with panic messages carry their source path,
50+ # /rust/deps/<name>-<version>/, the others are known to be there: hashbrown backs HashMap, libc and object are
51+ # used by std everywhere but on Windows, adler2 by miniz_oxide.
52+ rust_components () {
53+ local bin=$1 windows=$2 crates
54+ crates=$( LC_ALL=C grep -aoE ' /rust/deps[/\\][A-Za-z0-9_-]+-[0-9][0-9A-Za-z.+-]*[/\\]' " $bin " | sed -E ' s#^/rust/deps.##; s#.$##' | sort -u)
55+ crates=$( printf ' %s\n' " $crates " hashbrown-auto)
56+ [ " $windows " = 1 ] || crates=$( printf ' %s\n' " $crates " libc-auto object-auto)
57+ case $crates in * miniz_oxide-* ) crates=$( printf ' %s\n' " $crates " adler2-auto) ;; esac
58+ [ " $( tc rustc) " = " $rustc_version " ] || { echo " $bin : built with rustc $( tc rustc) , but $rustc_version is installed" >&2 ; exit 1; }
59+ local list=() crate name found lock_version
60+ for crate in $( printf ' %s\n' " $crates " | sed ' /^$/d' | sort -u) ; do
61+ name=${crate% -* }
62+ found=${crate##* -}
63+ lock_version=$( awk -v n=" $name " ' $0 == "name = \"" n "\"" { getline; gsub(/version = |"/, ""); print; exit }' " $std_lock " )
64+ [ -n " $lock_version " ] || { echo " $bin : $name is not one of std's dependencies" >&2 ; exit 1; }
65+ [ " $found " = auto ] || [ " $found " = " $lock_version " ] || { echo " $bin : $name $found , std uses $lock_version " >&2 ; exit 1; }
66+ [ -n " ${crate_license[$name]:- } " ] || { echo " $bin : unknown crate $name , add its license to sbom.sh" >&2 ; exit 1; }
67+ list+=(" $( jq -n --arg name " $name " --arg version " $lock_version " --arg license " ${crate_license[$name]} " ' {
68+ type: "library", "bom-ref": "crate-\($name)", name: $name, version: $version,
69+ description: "Part of the Rust standard library build",
70+ licenses: [{expression: $license}],
71+ purl: "pkg:cargo/\($name)@\($version)"
72+ }' ) " )
73+ done
74+ jq -n --arg version " $rustc_version " ' [
75+ {
76+ type: "library", "bom-ref": "rust-std", name: "Rust standard library", version: $version,
77+ licenses: [{expression: "(MIT OR Apache-2.0) AND Unicode-3.0"}],
78+ purl: "pkg:github/rust-lang/rust@\($version)",
79+ cpe: "cpe:2.3:a:rust-lang:rust:\($version):*:*:*:*:*:*:*"
80+ },
81+ {
82+ type: "library", "bom-ref": "compiler-builtins", name: "compiler-builtins", version: $version,
83+ description: "Part of the Rust toolchain, with code ported from LLVM compiler-rt",
84+ licenses: [{expression: "MIT AND Apache-2.0 WITH LLVM-exception AND (MIT OR Apache-2.0)"}],
85+ purl: "pkg:github/rust-lang/rust@\($version)#library/compiler-builtins"
86+ }
87+ ] + ($ARGS.positional | map(fromjson))' --args " ${list[@]} "
3988}
4089
4190for name in zstd-extract-linux-x86_64 zstd-extract-linux-aarch64 zstd-extract-darwin zstd-extract-windows-x86_64.exe; do
@@ -48,8 +97,7 @@ for name in zstd-extract-linux-x86_64 zstd-extract-linux-aarch64 zstd-extract-da
4897 linked=$( jq -n ' $ARGS.positional | map(fromjson)' --args \
4998 " $( apk musl musl MIT) " \
5099 " $( apk llvm-libunwind llvm-libunwind-static ' Apache-2.0 WITH LLVM-exception' ) " \
51- " $( apk fortify-headers fortify-headers 0BSD) " \
52- " $( rust_std) " )
100+ " $( apk fortify-headers fortify-headers 0BSD) " )
53101 tools=$( jq -n ' $ARGS.positional | map(fromjson)' --args \
54102 " $( tool rustc " $( tc rustc) " ) " \
55103 " $( apk gcc gcc ' GPL-3.0-or-later' | jq ' .type = "application" | del(."bom-ref")' ) " \
@@ -58,21 +106,22 @@ for name in zstd-extract-linux-x86_64 zstd-extract-linux-aarch64 zstd-extract-da
58106 ;;
59107 * -darwin)
60108 # libSystem is linked dynamically, so only zstd and Rust's std are part of the binary
61- linked=$( jq -n ' $ARGS.positional | map(fromjson) ' --args " $( rust_std ) " )
109+ linked=' [] '
62110 tools=$( jq -n ' $ARGS.positional | map(fromjson)' --args \
63111 " $( tool rustc " $( tc rustc) " ) " " $( tool ' Apple clang' " $( tc clang) " ) " " $( tool ' macOS SDK' " $( tc sdk) " ) " )
64112 ;;
65113 * -windows-* )
66114 license=' {"license": {"name": "Microsoft Visual Studio and Windows SDK license terms"}}'
67- linked=$( jq -n --arg msvc " $( tc msvc) " --arg ucrt " $( tc ucrt) " --argjson license " $license " --argjson std " $( rust_std ) " ' [
115+ linked=$( jq -n --arg msvc " $( tc msvc) " --arg ucrt " $( tc ucrt) " --argjson license " $license " ' [
68116 {type: "library", "bom-ref": "vcruntime", name: "Microsoft Visual C++ runtime (static)", version: $msvc, licenses: [$license]},
69- {type: "library", "bom-ref": "ucrt", name: "Microsoft Universal C runtime (static)", version: $ucrt, licenses: [$license]},
70- $std
117+ {type: "library", "bom-ref": "ucrt", name: "Microsoft Universal C runtime (static)", version: $ucrt, licenses: [$license]}
71118 ]' )
72119 tools=$( jq -n ' $ARGS.positional | map(fromjson)' --args \
73120 " $( tool rustc " $( tc rustc) " ) " " $( tool ' MSVC' " $( tc msvc) " ) " " $( tool ' Windows SDK' " $( tc sdk) " ) " )
74121 ;;
75122 esac
123+ case $name in * -windows-* ) windows=1 ;; * ) windows=0 ;; esac
124+ linked=$( jq -n --argjson linked " $linked " --argjson rust " $( rust_components " $dir /$name " " $windows " ) " ' $linked + $rust' )
76125
77126 jq -n --arg name " $name " --arg version " $version " --arg sha " $( sha256sum " $dir /$name " | cut -d' ' -f1) " \
78127 --arg zstd " $zstd " --arg zstd_sha " $zstd_sha " --argjson linked " $linked " --argjson tools " $tools " ' {
0 commit comments