diff options
| author | Mathieu Arnold <mat@FreeBSD.org> | 2026-11-04 08:12:40 +0000 |
|---|---|---|
| committer | Mathieu Arnold <mat@FreeBSD.org> | 2026-11-04 08:25:31 +0000 |
| commit | 07747390ec3be28e5b72612605c5b251c399d0f6 (patch) | |
| tree | e14f0bb84ae65bdbd5af29acc907e8238c02b8a9 | |
| parent | 3cc782b634df9b2a0019192482f7a81f50df9be3 (diff) | |
ports-mgmt/sbom-generator: create SPDX SBOM files for installed packages
| -rw-r--r-- | ports-mgmt/Makefile | 1 | ||||
| -rw-r--r-- | ports-mgmt/sbom-generator/Makefile | 34 | ||||
| -rw-r--r-- | ports-mgmt/sbom-generator/files/sbom-generator.1 | 139 | ||||
| -rw-r--r-- | ports-mgmt/sbom-generator/files/sbom-generator.in | 96 | ||||
| -rw-r--r-- | ports-mgmt/sbom-generator/pkg-descr | 14 |
5 files changed, 284 insertions, 0 deletions
diff --git a/ports-mgmt/Makefile b/ports-mgmt/Makefile index 5da6b7260eb0..75239db975c0 100644 --- a/ports-mgmt/Makefile +++ b/ports-mgmt/Makefile @@ -80,6 +80,7 @@ SUBDIR += py-skog SUBDIR += rc-subr-jail SUBDIR += reprise + SUBDIR += sbom-generator SUBDIR += sccache-overlay SUBDIR += submodules2tuple SUBDIR += synth diff --git a/ports-mgmt/sbom-generator/Makefile b/ports-mgmt/sbom-generator/Makefile new file mode 100644 index 000000000000..83c1135c82ff --- /dev/null +++ b/ports-mgmt/sbom-generator/Makefile @@ -0,0 +1,34 @@ +PORTNAME= sbom-generator +PORTVERSION= 1.0 +CATEGORIES= ports-mgmt +MASTER_SITES= # none +DISTFILES= # none + +MAINTAINER= portmgr@FreeBSD.org +COMMENT= Create SPDX SBOM files for installed packages +WWW= https://www.FreeBSD.org/ + +LICENSE= BSD2CLAUSE + +RUN_DEPENDS= bomtool:devel/pkgconf \ + spdxtool:devel/pkgconf + +NO_ARCH= yes +NO_BUILD= yes + +SUB_FILES= sbom-generator +SUB_LIST= LOCALBASE=${LOCALBASE} + +PLIST_FILES= bin/sbom-generator \ + share/man/man1/sbom-generator.1.gz + +do-extract: + @${MKDIR} ${WRKSRC} + +do-install: + ${INSTALL_SCRIPT} ${WRKDIR}/sbom-generator \ + ${STAGEDIR}${PREFIX}/bin/sbom-generator + ${INSTALL_MAN} ${FILESDIR}/sbom-generator.1 \ + ${STAGEDIR}${PREFIX}/share/man/man1 + +.include <bsd.port.mk> diff --git a/ports-mgmt/sbom-generator/files/sbom-generator.1 b/ports-mgmt/sbom-generator/files/sbom-generator.1 new file mode 100644 index 000000000000..45e59ce4b5ed --- /dev/null +++ b/ports-mgmt/sbom-generator/files/sbom-generator.1 @@ -0,0 +1,139 @@ +.Dd November 4, 2026 +.Dt SBOM-GENERATOR 1 +.Os +.Sh NAME +.Nm sbom-generator +.Nd generate SPDX documents from installed package SBOM files +.Sh SYNOPSIS +.Nm +.Op Fl c +.Op Ar package ... +.Sh DESCRIPTION +The +.Nm +utility reads the SBOM files that each package installs. +It creates two SPDX documents for each SBOM file. +.Pp +Each SBOM file is a +.Xr pc 5 +file in the +.Pa share/sbom/pc +directory. +The name of the file is the name of the package or subpackage, followed by +.Dq .pc . +.Pp +.Nm +reads the +.Dq Requires +field of each file. +It adds all dependencies to the document. +It adds the dependencies of each dependency, and so on. +.Pp +If you give no +.Ar package , +.Nm +processes all SBOM files in the +.Pa share/sbom/pc +directory. +If you give one or more +.Ar package +names, it processes only those packages. +.Pp +.Nm +creates these files for each package: +.Bl -tag -width ".Pa spdx-3.0.1/package.jsonld" +.It Pa spdx-2.2/package.spdx +An SPDX 2.2 document in tag-value format. +.Nm +uses +.Xr bomtool 1 +to create it. +.It Pa spdx-3.0.1/package.jsonld +An SPDX 3.0.1 document in JSON-LD format. +.Nm +uses +.Xr spdxtool 1 +to create it. +.El +.Pp +.Nm +replaces a document only after it creates the new document without error. +.Pp +The options are as follows: +.Bl -tag -width indent +.It Fl c +Remove the documents that have no SBOM file. +This removes the documents of packages that you removed. +.El +.Sh ENVIRONMENT +.Bl -tag -width ".Ev SOURCE_DATE_EPOCH" +.It Ev LOCALBASE +The base directory of installed packages. +The port build sets the default value. +.It Ev SOURCE_DATE_EPOCH +If set, +.Xr bomtool 1 +and +.Xr spdxtool 1 +use this UNIX timestamp as the creation time of the documents. +Two runs with the same SBOM files and the same value then create the +same documents. +.El +.Pp +.Nm +sets +.Ev PKG_CONFIG_LIBDIR +to the +.Pa share/sbom/pc +directory and unsets +.Ev PKG_CONFIG_PATH +and +.Ev PKG_CONFIG_PRELOADED_FILES . +The tools then find SBOM files only. +They do not find the regular +.Xr pkgconf 1 +modules. +.Sh FILES +.Bl -tag -width ".Pa ${LOCALBASE}/share/sbom/spdx-3.0.1/" +.It Pa ${LOCALBASE}/share/sbom/pc/ +The SBOM files that the packages install. +.It Pa ${LOCALBASE}/share/sbom/spdx-2.2/ +The SPDX 2.2 documents. +.It Pa ${LOCALBASE}/share/sbom/spdx-3.0.1/ +The SPDX 3.0.1 documents. +.El +.Sh EXIT STATUS +.Ex -std +.Pp +.Nm +writes a message to standard error if it cannot create a document. +Then it continues with the next package. +This can occur when an SBOM file lists a dependency that has no SBOM file. +At the end, +.Nm +exits with a value of 1. +.Sh EXAMPLES +Create the documents for all installed packages: +.Pp +.Dl # sbom-generator +.Pp +Create the documents for the +.Dq curl +package only: +.Pp +.Dl # sbom-generator curl +.Pp +Create all documents again and remove the documents of packages that you removed: +.Pp +.Dl # sbom-generator -c +.Pp +Create documents that are the same each time: +.Pp +.Dl # SOURCE_DATE_EPOCH=0 sbom-generator +.Sh SEE ALSO +.Xr bomtool 1 , +.Xr pkgconf 1 , +.Xr spdxtool 1 , +.Xr pc 5 +.Sh AUTHORS +.An Mathieu Arnold Aq Mt mat@FreeBSD.org diff --git a/ports-mgmt/sbom-generator/files/sbom-generator.in b/ports-mgmt/sbom-generator/files/sbom-generator.in new file mode 100644 index 000000000000..f863ce272f0c --- /dev/null +++ b/ports-mgmt/sbom-generator/files/sbom-generator.in @@ -0,0 +1,96 @@ +#!/bin/sh +# +# Create SPDX 2.2 (.spdx, bomtool) and SPDX 3.0.1 (.jsonld, spdxtool) +# documents from the pc(5) SBOM files that packages install. +# +# bomtool and spdxtool read the Requires: field of each file. +# They add each dependency, and its own dependencies, to the documents. + +set -u + +LOCALBASE="${LOCALBASE:-%%LOCALBASE%%}" +SBOMDIR="${LOCALBASE}/share/sbom/pc" +SPDX22DIR="${LOCALBASE}/share/sbom/spdx-2.2" +SPDX30DIR="${LOCALBASE}/share/sbom/spdx-3.0.1" + +usage() { + echo "usage: ${0##*/} [-c] [package ...]" >&2 + exit 64 +} + +clean=0 +while getopts c opt; do + case ${opt} in + c) clean=1 ;; + *) usage ;; + esac +done +shift $((OPTIND - 1)) + +# Find modules only in the SBOM directory. +# This prevents a clash between SBOM names and regular pkg-config modules. +PKG_CONFIG_LIBDIR="${SBOMDIR}" +export PKG_CONFIG_LIBDIR +unset PKG_CONFIG_PATH PKG_CONFIG_PRELOADED_FILES + +if [ ! -d "${SBOMDIR}" ]; then + echo "${0##*/}: ${SBOMDIR}: no such directory" >&2 + exit 1 +fi + +mkdir -p "${SPDX22DIR}" "${SPDX30DIR}" + +if [ $# -eq 0 ]; then + set -- $(cd "${SBOMDIR}" && ls -1 | sed -n 's/\.pc$//p') +fi + +rc=0 + +# generate <destfile> <command> [args ...] +# Write the output of the command to a temporary file, then move it to +# <destfile> if the command succeeds. +generate() { + local dest=$1 + local tmp + + tmp=$(mktemp "${dest}.XXXXXX") || { + rc=1 + return + } + shift + if "$@" >"${tmp}" 2>/dev/null && [ -s "${tmp}" ]; then + chmod 644 "${tmp}" + mv -f "${tmp}" "${dest}" + else + rm -f "${tmp}" + echo "${0##*/}: failed to generate ${dest##*/}" >&2 + rc=1 + fi +} + +for name in "$@"; do + name=${name%.pc} + if [ ! -f "${SBOMDIR}/${name}.pc" ]; then + echo "${0##*/}: ${name}: no SBOM file in ${SBOMDIR}" >&2 + rc=1 + continue + fi + generate "${SPDX22DIR}/${name}.spdx" bomtool "${name}" + generate "${SPDX30DIR}/${name}.jsonld" \ + spdxtool --agent-name "FreeBSD Ports" "${name}" +done + +if [ ${clean} -eq 1 ]; then + for f in "${SPDX22DIR}"/*.spdx; do + [ -e "${f}" ] || continue + n=${f##*/} + [ -f "${SBOMDIR}/${n%.spdx}.pc" ] || rm -f "${f}" + done + for f in "${SPDX30DIR}"/*.jsonld; do + [ -e "${f}" ] || continue + n=${f##*/} + [ -f "${SBOMDIR}/${n%.jsonld}.pc" ] || rm -f "${f}" + done +fi + +exit ${rc} diff --git a/ports-mgmt/sbom-generator/pkg-descr b/ports-mgmt/sbom-generator/pkg-descr new file mode 100644 index 000000000000..fcacf2c778eb --- /dev/null +++ b/ports-mgmt/sbom-generator/pkg-descr @@ -0,0 +1,14 @@ +sbom-generator creates SPDX documents for installed packages. + +Each package installs an SBOM file in the share/sbom/pc directory. +The file uses the pc(5) format. +sbom-generator reads these files. +For each file, it creates two documents: + +- An SPDX 2.2 document (.spdx) in the share/sbom/spdx-2.2 directory. + It uses bomtool. +- An SPDX 3.0.1 document (.jsonld) in the share/sbom/spdx-3.0.1 directory. + It uses spdxtool. + +Each document includes the dependencies of the package. +It also includes the dependencies of each dependency. |
