Skip to content

Commit c91542b

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 aef78ca commit c91542b

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
from west.commands import WestCommand
99

10+
from zspdx.version import parse, SUPPORTED_SPDX_VERSIONS, SPDX_VERSION_2_3
1011
from zspdx.sbom import SBOMConfig, makeSPDX, setupCmakeQuery
1112

1213
SPDX_DESCRIPTION = """\
13-
This command creates an SPDX 2.2 tag-value bill of materials
14+
This command creates an SPDX 2.2 or 2.3 tag-value bill of materials
1415
following the completion of a Zephyr build.
1516
1617
Prior to the build, an empty file must be created at
@@ -41,6 +42,9 @@ def do_add_parser(self, parser_adder):
4142
help="namespace prefix")
4243
parser.add_argument('-s', '--spdx-dir',
4344
help="SPDX output directory")
45+
parser.add_argument('--spdx-version', choices=[str(v) for v in SUPPORTED_SPDX_VERSIONS],
46+
default=str(SPDX_VERSION_2_3),
47+
help="SPDX specification version to use (default: 2.3)")
4448
parser.add_argument('--analyze-includes', action="store_true",
4549
help="also analyze included header files")
4650
parser.add_argument('--include-sdk', action="store_true",
@@ -55,6 +59,7 @@ def do_run(self, args, unknown_args):
5559
self.dbg(" --build-dir is", args.build_dir)
5660
self.dbg(" --namespace-prefix is", args.namespace_prefix)
5761
self.dbg(" --spdx-dir is", args.spdx_dir)
62+
self.dbg(" --spdx-version is", args.spdx_version)
5863
self.dbg(" --analyze-includes is", args.analyze_includes)
5964
self.dbg(" --include-sdk is", args.include_sdk)
6065

@@ -85,6 +90,11 @@ def do_run_spdx(self, args):
8590
# create the SPDX files
8691
cfg = SBOMConfig()
8792
cfg.buildDir = args.build_dir
93+
try:
94+
version_obj = parse(args.spdx_version)
95+
except Exception:
96+
self.die(f"Invalid SPDX version: {args.spdx_version}")
97+
cfg.spdxVersion = version_obj
8898
if args.namespace_prefix:
8999
cfg.namespacePrefix = args.namespace_prefix
90100
else:

scripts/west_commands/zspdx/sbom.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
from west import log
88

9-
from zspdx.walker import WalkerConfig, Walker
109
from zspdx.scanner import ScannerConfig, scanDocument
10+
from zspdx.version import SPDX_VERSION_2_3
11+
from zspdx.walker import Walker, WalkerConfig
1112
from zspdx.writer import writeSPDX
1213

1314

@@ -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
@@ -7,6 +7,7 @@
77
from west import log
88

99
from zspdx.util import getHashes
10+
from zspdx.version import SPDX_VERSION_2_3
1011

1112
import re
1213

@@ -65,11 +66,12 @@ def generateDowloadUrl(url, revision):
6566

6667
return f'git+{url}@{revision}'
6768

68-
# Output tag-value SPDX 2.3 content for the given Package object.
69+
# Output tag-value SPDX content for the given Package object.
6970
# Arguments:
7071
# 1) f: file handle for SPDX document
7172
# 2) pkg: Package object being described
72-
def writePackageSPDX(f, pkg):
73+
# 3) spdx_version: SPDX specification version
74+
def writePackageSPDX(f, pkg, spdx_version=SPDX_VERSION_2_3):
7375
spdx_normalized_name = _normalize_spdx_name(pkg.cfg.name)
7476
spdx_normalize_spdx_id = _normalize_spdx_name(pkg.cfg.spdxID)
7577

@@ -83,7 +85,8 @@ def writePackageSPDX(f, pkg):
8385
PackageCopyrightText: {pkg.cfg.copyrightText}
8486
""")
8587

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

8992
if len(pkg.cfg.url) > 0:
@@ -140,14 +143,15 @@ def writeOtherLicenseSPDX(f, lic):
140143
LicenseComment: Corresponds to the license ID `{lic}` detected in an SPDX-License-Identifier: tag.
141144
""")
142145

143-
# Output tag-value SPDX 2.3 content for the given Document object.
146+
# Output tag-value SPDX content for the given Document object.
144147
# Arguments:
145148
# 1) f: file handle for SPDX document
146149
# 2) doc: Document object being described
147-
def writeDocumentSPDX(f, doc):
150+
# 3) spdx_version: SPDX specification version
151+
def writeDocumentSPDX(f, doc, spdx_version=SPDX_VERSION_2_3):
148152
spdx_normalized_name = _normalize_spdx_name(doc.cfg.name)
149153

150-
f.write(f"""SPDXVersion: SPDX-2.3
154+
f.write(f"""SPDXVersion: SPDX-{spdx_version}
151155
DataLicense: CC0-1.0
152156
SPDXID: SPDXRef-DOCUMENT
153157
DocumentName: {spdx_normalized_name}
@@ -173,7 +177,7 @@ def writeDocumentSPDX(f, doc):
173177

174178
# write packages
175179
for pkg in doc.pkgs.values():
176-
writePackageSPDX(f, pkg)
180+
writePackageSPDX(f, pkg, spdx_version)
177181

178182
# write other license info, if any
179183
if len(doc.customLicenseIDs) > 0:
@@ -185,12 +189,13 @@ def writeDocumentSPDX(f, doc):
185189
# Arguments:
186190
# 1) spdxPath: path to write SPDX document
187191
# 2) doc: SPDX Document object to write
188-
def writeSPDX(spdxPath, doc):
192+
# 3) spdx_version: SPDX specification version
193+
def writeSPDX(spdxPath, doc, spdx_version=SPDX_VERSION_2_3):
189194
# create and write document to disk
190195
try:
191-
log.inf(f"Writing SPDX document {doc.cfg.name} to {spdxPath}")
196+
log.inf(f"Writing SPDX {spdx_version} document {doc.cfg.name} to {spdxPath}")
192197
with open(spdxPath, "w") as f:
193-
writeDocumentSPDX(f, doc)
198+
writeDocumentSPDX(f, doc, spdx_version)
194199
except OSError as e:
195200
log.err(f"Error: Unable to write to {spdxPath}: {str(e)}")
196201
return False

0 commit comments

Comments
 (0)