Skip to content

Commit be504b0

Browse files
committed
west: spdx: allow to generate for different SPDX versions
When support for SPDX 2.3 was added, it effectively dropped support for SPDX 2.2, which in retrospect was a bad idea since SPDX 2.2 is the version that is the current ISO/IEC standard. This commit adds a `--spdx-version` option to the `west spdx` command so that users can generate SPDX 2.2 documents if they want. Default is 2.3 given that's effectively what shipped for a few releases now, including latest LTS. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 22b0894 commit be504b0

File tree

5 files changed

+68
-18
lines changed

5 files changed

+68
-18
lines changed

doc/develop/west/zephyr-cmds.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ See :zephyr_file:`share/zephyr-package/cmake` for details.
7575
Software bill of materials: ``west spdx``
7676
*****************************************
7777

78-
This command generates SPDX 2.3 tag-value documents, creating relationships
78+
This command generates SPDX 2.2 or 2.3 tag-value documents, creating relationships
7979
from source files to the corresponding generated build files.
8080
``SPDX-License-Identifier`` comments in source files are scanned and filled
8181
into the SPDX documents.
@@ -105,6 +105,12 @@ To use this command:
105105
106106
west spdx -d BUILD_DIR
107107
108+
By default, this generates SPDX 2.3 documents. To generate SPDX 2.2 documents instead:
109+
110+
.. code-block:: bash
111+
112+
west spdx -d BUILD_DIR --spdx-version 2.2
113+
108114
.. note::
109115

110116
When building with :ref:`sysbuild`, make sure you target the actual application
@@ -144,6 +150,10 @@ source files that are compiled to generate the built library files.
144150
- ``-s SPDX_DIR``: specifies an alternate directory where the SPDX documents
145151
should be written instead of :file:`BUILD_DIR/spdx/`.
146152

153+
- ``--spdx-version {2.2,2.3}``: specifies which SPDX specification version to use.
154+
Defaults to ``2.3``. SPDX 2.3 includes additional fields like ``PrimaryPackagePurpose``
155+
that are not available in SPDX 2.2.
156+
147157
- ``--analyze-includes``: in addition to recording the compiled source code
148158
files (e.g. ``.c``, ``.S``) in the bills-of-materials, also attempt to
149159
determine the specific header files that are included for each ``.c`` file.

scripts/west_commands/spdx.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import uuid
77

88
from west.commands import WestCommand
9-
109
from zspdx.sbom import SBOMConfig, makeSPDX, setupCmakeQuery
10+
from zspdx.version import SPDX_VERSION_2_3, SUPPORTED_SPDX_VERSIONS, parse
1111

1212
SPDX_DESCRIPTION = """\
13-
This command creates an SPDX 2.2 tag-value bill of materials
13+
This command creates an SPDX 2.2 or 2.3 tag-value bill of materials
1414
following the completion of a Zephyr build.
1515
1616
Prior to the build, an empty file must be created at
@@ -41,6 +41,9 @@ def do_add_parser(self, parser_adder):
4141
help="namespace prefix")
4242
parser.add_argument('-s', '--spdx-dir',
4343
help="SPDX output directory")
44+
parser.add_argument('--spdx-version', choices=[str(v) for v in SUPPORTED_SPDX_VERSIONS],
45+
default=str(SPDX_VERSION_2_3),
46+
help="SPDX specification version to use (default: 2.3)")
4447
parser.add_argument('--analyze-includes', action="store_true",
4548
help="also analyze included header files")
4649
parser.add_argument('--include-sdk', action="store_true",
@@ -55,6 +58,7 @@ def do_run(self, args, unknown_args):
5558
self.dbg(" --build-dir is", args.build_dir)
5659
self.dbg(" --namespace-prefix is", args.namespace_prefix)
5760
self.dbg(" --spdx-dir is", args.spdx_dir)
61+
self.dbg(" --spdx-version is", args.spdx_version)
5862
self.dbg(" --analyze-includes is", args.analyze_includes)
5963
self.dbg(" --include-sdk is", args.include_sdk)
6064

@@ -85,6 +89,11 @@ def do_run_spdx(self, args):
8589
# create the SPDX files
8690
cfg = SBOMConfig()
8791
cfg.buildDir = args.build_dir
92+
try:
93+
version_obj = parse(args.spdx_version)
94+
except Exception:
95+
self.die(f"Invalid SPDX version: {args.spdx_version}")
96+
cfg.spdxVersion = version_obj
8897
if args.namespace_prefix:
8998
cfg.namespacePrefix = args.namespace_prefix
9099
else:

scripts/west_commands/zspdx/sbom.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from west import log
88

99
from zspdx.scanner import ScannerConfig, scanDocument
10+
from zspdx.version import SPDX_VERSION_2_3
1011
from zspdx.walker import Walker, WalkerConfig
1112
from zspdx.writer import writeSPDX
1213

@@ -26,6 +27,9 @@ def __init__(self):
2627
# location of SPDX document output directory
2728
self.spdxDir = ""
2829

30+
# SPDX specification version to use
31+
self.spdxVersion = SPDX_VERSION_2_3
32+
2933
# should also analyze for included header files?
3034
self.analyzeIncludes = False
3135

@@ -101,31 +105,33 @@ def makeSPDX(cfg):
101105

102106
# write SDK document, if we made one
103107
if cfg.includeSDK:
104-
retval = writeSPDX(os.path.join(cfg.spdxDir, "sdk.spdx"), w.docSDK)
108+
retval = writeSPDX(os.path.join(cfg.spdxDir, "sdk.spdx"), w.docSDK, cfg.spdxVersion)
105109
if not retval:
106110
log.err("SPDX writer failed for SDK document; bailing")
107111
return False
108112

109113
# write app document
110-
retval = writeSPDX(os.path.join(cfg.spdxDir, "app.spdx"), w.docApp)
114+
retval = writeSPDX(os.path.join(cfg.spdxDir, "app.spdx"), w.docApp, cfg.spdxVersion)
111115
if not retval:
112116
log.err("SPDX writer failed for app document; bailing")
113117
return False
114118

115119
# write zephyr document
116-
retval = writeSPDX(os.path.join(cfg.spdxDir, "zephyr.spdx"), w.docZephyr)
120+
retval = writeSPDX(os.path.join(cfg.spdxDir, "zephyr.spdx"), w.docZephyr, cfg.spdxVersion)
117121
if not retval:
118122
log.err("SPDX writer failed for zephyr document; bailing")
119123
return False
120124

121125
# write build document
122-
retval = writeSPDX(os.path.join(cfg.spdxDir, "build.spdx"), w.docBuild)
126+
retval = writeSPDX(os.path.join(cfg.spdxDir, "build.spdx"), w.docBuild, cfg.spdxVersion)
123127
if not retval:
124128
log.err("SPDX writer failed for build document; bailing")
125129
return False
126130

127131
# write modules document
128-
retval = writeSPDX(os.path.join(cfg.spdxDir, "modules-deps.spdx"), w.docModulesExtRefs)
132+
retval = writeSPDX(
133+
os.path.join(cfg.spdxDir, "modules-deps.spdx"), w.docModulesExtRefs, cfg.spdxVersion
134+
)
129135
if not retval:
130136
log.err("SPDX writer failed for modules-deps document; bailing")
131137
return False
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) 2025 The Linux Foundation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from packaging.version import Version
6+
7+
SPDX_VERSION_2_2 = Version("2.2")
8+
SPDX_VERSION_2_3 = Version("2.3")
9+
10+
SUPPORTED_SPDX_VERSIONS = [
11+
SPDX_VERSION_2_2,
12+
SPDX_VERSION_2_3,
13+
]
14+
15+
16+
def parse(version_str):
17+
v = Version(version_str)
18+
if v not in SUPPORTED_SPDX_VERSIONS:
19+
raise ValueError(f"Unsupported SPDX version: {version_str}")
20+
return v

scripts/west_commands/zspdx/writer.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from west import log
99

1010
from zspdx.util import getHashes
11+
from zspdx.version import SPDX_VERSION_2_3
1112

1213
CPE23TYPE_REGEX = (
1314
r'^cpe:2\.3:[aho\*\-](:(((\?*|\*?)([a-zA-Z0-9\-\._]|(\\[\\\*\?!"#$$%&\'\(\)\+,\/:;<=>@\[\]\^'
@@ -67,11 +68,12 @@ def generateDowloadUrl(url, revision):
6768

6869
return f'git+{url}@{revision}'
6970

70-
# Output tag-value SPDX 2.3 content for the given Package object.
71+
# Output tag-value SPDX content for the given Package object.
7172
# Arguments:
7273
# 1) f: file handle for SPDX document
7374
# 2) pkg: Package object being described
74-
def writePackageSPDX(f, pkg):
75+
# 3) spdx_version: SPDX specification version
76+
def writePackageSPDX(f, pkg, spdx_version=SPDX_VERSION_2_3):
7577
spdx_normalized_name = _normalize_spdx_name(pkg.cfg.name)
7678
spdx_normalize_spdx_id = _normalize_spdx_name(pkg.cfg.spdxID)
7779

@@ -85,7 +87,8 @@ def writePackageSPDX(f, pkg):
8587
PackageCopyrightText: {pkg.cfg.copyrightText}
8688
""")
8789

88-
if pkg.cfg.primaryPurpose != "":
90+
# PrimaryPackagePurpose is only available in SPDX 2.3 and later
91+
if spdx_version >= SPDX_VERSION_2_3 and pkg.cfg.primaryPurpose != "":
8992
f.write(f"PrimaryPackagePurpose: {pkg.cfg.primaryPurpose}\n")
9093

9194
if len(pkg.cfg.url) > 0:
@@ -142,14 +145,15 @@ def writeOtherLicenseSPDX(f, lic):
142145
LicenseComment: Corresponds to the license ID `{lic}` detected in an SPDX-License-Identifier: tag.
143146
""")
144147

145-
# Output tag-value SPDX 2.3 content for the given Document object.
148+
# Output tag-value SPDX content for the given Document object.
146149
# Arguments:
147150
# 1) f: file handle for SPDX document
148151
# 2) doc: Document object being described
149-
def writeDocumentSPDX(f, doc):
152+
# 3) spdx_version: SPDX specification version
153+
def writeDocumentSPDX(f, doc, spdx_version=SPDX_VERSION_2_3):
150154
spdx_normalized_name = _normalize_spdx_name(doc.cfg.name)
151155

152-
f.write(f"""SPDXVersion: SPDX-2.3
156+
f.write(f"""SPDXVersion: SPDX-{spdx_version}
153157
DataLicense: CC0-1.0
154158
SPDXID: SPDXRef-DOCUMENT
155159
DocumentName: {spdx_normalized_name}
@@ -178,7 +182,7 @@ def writeDocumentSPDX(f, doc):
178182

179183
# write packages
180184
for pkg in doc.pkgs.values():
181-
writePackageSPDX(f, pkg)
185+
writePackageSPDX(f, pkg, spdx_version)
182186

183187
# write other license info, if any
184188
if len(doc.customLicenseIDs) > 0:
@@ -190,12 +194,13 @@ def writeDocumentSPDX(f, doc):
190194
# Arguments:
191195
# 1) spdxPath: path to write SPDX document
192196
# 2) doc: SPDX Document object to write
193-
def writeSPDX(spdxPath, doc):
197+
# 3) spdx_version: SPDX specification version
198+
def writeSPDX(spdxPath, doc, spdx_version=SPDX_VERSION_2_3):
194199
# create and write document to disk
195200
try:
196-
log.inf(f"Writing SPDX document {doc.cfg.name} to {spdxPath}")
201+
log.inf(f"Writing SPDX {spdx_version} document {doc.cfg.name} to {spdxPath}")
197202
with open(spdxPath, "w") as f:
198-
writeDocumentSPDX(f, doc)
203+
writeDocumentSPDX(f, doc, spdx_version)
199204
except OSError as e:
200205
log.err(f"Error: Unable to write to {spdxPath}: {str(e)}")
201206
return False

0 commit comments

Comments
 (0)