make-sbom.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/env bash
  2. # Writes an SPDX 2.3 JSON software bill of materials of LYNX SHARE's third-party components (round 190nr, 0.1.31).
  3. #
  4. # scripts/make-sbom.sh <version label, e.g. 0.1.31-alpha> <output .spdx.json> [licenses folder]
  5. #
  6. # Sources: the component table of THIRD_PARTY.md (name, link, version, licence) and the licence texts the packages
  7. # carry in licenses/ (scripts/collect-licenses.sh; run into a temporary folder when no folder is given). The SPDX
  8. # licence expression, version and third_party/ folder of each row come from the COMPONENTS table below, keyed by the
  9. # row's first link text: a THIRD_PARTY.md row without an entry there, or a licences folder entry that no component
  10. # claims, stops the script, so the SBOM can't silently fall behind THIRD_PARTY.md. Plain bash + python3.
  11. set -euo pipefail
  12. LABEL="${1:?usage: scripts/make-sbom.sh <version label> <output .spdx.json> [licenses folder]}"
  13. OUT="${2:?usage: scripts/make-sbom.sh <version label> <output .spdx.json> [licenses folder]}"
  14. LIC="${3:-}"
  15. case "$OUT" in /*) ;; *) OUT="$PWD/$OUT" ;; esac
  16. case "$LIC" in /*|"") ;; *) LIC="$PWD/$LIC" ;; esac
  17. cd "$(dirname "$0")/.."
  18. TMP=""
  19. if [ -z "$LIC" ]; then
  20. TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT
  21. scripts/collect-licenses.sh "$TMP/licenses" > /dev/null
  22. LIC="$TMP/licenses"
  23. fi
  24. COMMIT="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
  25. python3 - "$LABEL" "$OUT" "$LIC" "$COMMIT" <<'PY'
  26. import datetime, json, os, re, sys, uuid
  27. label, out, lic_dir, commit = sys.argv[1:5]
  28. # key (first link text of the THIRD_PARTY.md row, or the row's plain text start) ->
  29. # version (None: the one in the key), SPDX licence LYNX uses it under (concluded), declared licence,
  30. # third_party/ folders whose licence files belong to it, shipped (False: reference only, not in the SBOM).
  31. NOA = "NOASSERTION"
  32. COMPONENTS = {
  33. "JUCE 8": dict(version="8.0.4", concluded="AGPL-3.0-only", declared="AGPL-3.0-only OR LicenseRef-JUCE-Commercial",
  34. folders=[]),
  35. "CLAP": dict(version="1.2.2", concluded="MIT", folders=["clap"]),
  36. "WavPack": dict(version="5.7.0", concluded="BSD-3-Clause", folders=["wavpack"]),
  37. "libogg 1.3.5 and libvorbis 1.3.7": dict(split=[("libogg", "1.3.5"), ("libvorbis", "1.3.7")],
  38. concluded="BSD-3-Clause", folders=["vorbis"]),
  39. "LAME 3.100": dict(concluded="LGPL-2.0-or-later", folders=["lame"]),
  40. "TinySoundFont 0.9": dict(concluded="MIT", folders=["tinysoundfont"]),
  41. "File-format references": dict(shipped=False),
  42. "stb_vorbis 1.22": dict(concluded="MIT", declared="MIT OR Unlicense", folders=["stb_vorbis"]),
  43. "minimp3": dict(version="ea99364f61c14656440e8d77e9c233ccf3124633", concluded="CC0-1.0", folders=["minimp3"]),
  44. "Signalsmith Stretch 1.4": dict(concluded="MIT", folders=["signalsmith"]),
  45. "Signalsmith Linear 0.6.6": dict(concluded="MIT", folders=["signalsmith"]),
  46. "pocketpy 2.2.0": dict(concluded="MIT", folders=["pocketpy"]),
  47. "Monocypher 4.0.2": dict(concluded="BSD-2-Clause", declared="BSD-2-Clause OR CC0-1.0", folders=["monocypher"]),
  48. "Faust Libraries": dict(version="1.6.2", name="Faust Libraries reverbs.lib (re.valhallaSupermassive, ported)",
  49. concluded="MIT", folders=[]),
  50. "PipeWire 1.6.2": dict(name="PipeWire headers", concluded="MIT", folders=["pipewire"]),
  51. "Ableton Link 4.1.1": dict(concluded="GPL-2.0-or-later", folders=["link"], exclude=["link-asio"]),
  52. "Asio 1.38.2": dict(concluded="BSL-1.0", folders=["link-asio"]),
  53. "VST 3 SDK": dict(version="3.7.12 (bundled with JUCE 8.0.4)", concluded="GPL-3.0-only",
  54. declared="GPL-3.0-only OR LicenseRef-Steinberg-Proprietary", folders=[], extra=["GPL-3.0.txt"]),
  55. "ARA SDK 2.1.0": dict(concluded="Apache-2.0", folders=["ara"]),
  56. "ASIO SDK 2.3.4": dict(concluded="GPL-3.0-only", declared="GPL-3.0-only OR LicenseRef-Steinberg-Proprietary",
  57. folders=["asiosdk"], extra=["GPL-3.0.txt"]),
  58. "ONNX Runtime 1.23.2": dict(concluded="MIT", folders=["onnxruntime"]),
  59. "Basic Pitch": dict(version="fa5997a", concluded="Apache-2.0", folders=["basic-pitch"]),
  60. "PitchNet": dict(version="121b330", name="PitchNet polyphonic engine", concluded="AGPL-3.0-only",
  61. folders=["pitchnet-poly"]),
  62. "Libraries bundled inside JUCE's modules": dict(split_libs=[
  63. ("HarfBuzz", "MIT", "https://github.com/harfbuzz/harfbuzz"),
  64. ("SheenBidi", "Apache-2.0", "https://github.com/Tehreer/SheenBidi"),
  65. ("FLAC", "BSD-3-Clause", "https://xiph.org/flac/"),
  66. ("Ogg/Vorbis (JUCE copy)", "BSD-3-Clause", "https://xiph.org/vorbis/"),
  67. ("libpng", "Libpng", "http://www.libpng.org/pub/png/libpng.html"),
  68. ("IJG libjpeg", "IJG", "https://ijg.org"),
  69. ("zlib", "Zlib", "https://zlib.net")]),
  70. "sse2neon": dict(version=NOA, concluded="MIT", folders=["sse2neon"]),
  71. # round 190qd (0.1.31): the tracker module reader, the JSFX host and the bundled JSFX pack
  72. "libxmp-lite 4.7.4": dict(concluded="MIT", folders=["libxmp-lite"]),
  73. "ysfx 0.0.47": dict(name="ysfx (with Cockos WDL / EEL2, the EEL2 GAS port and dr_libs)",
  74. concluded="Apache-2.0 AND Zlib AND (Unlicense OR MIT-0)", folders=["ysfx"]),
  75. "chokehold JSFX": dict(version="f6d175d8", concluded="MIT", folders=["jsfx-chokehold"]),
  76. "Open Sans": dict(concluded="OFL-1.1", folders=[], extra=["open-sans-OFL.txt"]),
  77. "Open Sans Condensed": dict(concluded="OFL-1.1", folders=[], extra=["open-sans-OFL.txt"]),
  78. }
  79. # Components that have a licence file in licenses/ but no row of their own in THIRD_PARTY.md's table.
  80. EXTRA_ROWS = {}
  81. LICENSE_REFS = {
  82. "LicenseRef-JUCE-Commercial": ("JUCE commercial licence",
  83. "JUCE 8 is also offered under a commercial licence by Raw Material Software; LYNX uses JUCE under the AGPL-3.0."),
  84. "LicenseRef-Steinberg-Proprietary": ("Steinberg proprietary SDK licence",
  85. "Steinberg offers the VST 3 and ASIO SDKs under a proprietary licence or the GPLv3; LYNX takes the GPLv3 option."),
  86. }
  87. rows = []
  88. for line in open("THIRD_PARTY.md", encoding="utf-8"):
  89. if not line.startswith("| ") or line.startswith("| Component") or line.startswith("|---"):
  90. continue
  91. cells = [c.strip() for c in line.strip().strip("|").split(" | ")]
  92. rows.append(cells)
  93. if not rows:
  94. sys.exit("make-sbom: no component table in THIRD_PARTY.md")
  95. def spdx_id(name):
  96. return "SPDXRef-" + re.sub(r"[^A-Za-z0-9.-]+", "-", name).strip("-")
  97. packages, claimed, errors = [], set(), []
  98. lic_files = sorted(f for f in os.listdir(lic_dir) if f != "README.txt")
  99. def files_for(spec):
  100. names = []
  101. for folder in spec.get("folders", []):
  102. for f in lic_files:
  103. if f.startswith(folder + "-") and not any(f.startswith(x + "-") for x in spec.get("exclude", [])):
  104. names.append(f)
  105. names += [f for f in spec.get("extra", []) if f in lic_files]
  106. claimed.update(names)
  107. return names
  108. def add(name, version, url, concluded, declared, used_for, licence_files):
  109. comment = "Used for: " + re.sub(r"\s+", " ", re.sub(r"[`*]", "", used_for)).strip()
  110. if licence_files:
  111. comment += ". Licence texts in the package's licenses folder: " + ", ".join(licence_files)
  112. packages.append({
  113. "name": name, "SPDXID": spdx_id(name), "versionInfo": version or NOA,
  114. "downloadLocation": url or NOA, "filesAnalyzed": False,
  115. "licenseConcluded": concluded, "licenseDeclared": declared or concluded,
  116. "copyrightText": NOA, "supplier": NOA, "comment": comment,
  117. })
  118. for cells in rows:
  119. first, used_for = cells[0], cells[1] if len(cells) > 1 else ""
  120. links = re.findall(r"\[([^\]]+)\]\(([^)]+)\)", first)
  121. key = links[0][0] if links else first
  122. spec = COMPONENTS.get(key) or next((v for k, v in COMPONENTS.items() if first.startswith(k)), None)
  123. if spec is None:
  124. errors.append(f"THIRD_PARTY.md row '{first[:60]}' has no entry in scripts/make-sbom.sh COMPONENTS")
  125. continue
  126. if spec.get("shipped") is False:
  127. continue
  128. if "split_libs" in spec:
  129. for name, lic, url in spec["split_libs"]:
  130. add(name, "bundled with JUCE 8.0.4", url, lic, None, used_for, [])
  131. continue
  132. # one row can name several projects ([Signalsmith Stretch 1.4](..) and [Signalsmith Linear 0.6.6](..), Link + Asio)
  133. for text, url in (links or [(key, None)]):
  134. spec2 = COMPONENTS.get(text)
  135. if spec2 is None:
  136. if text == key:
  137. spec2 = spec
  138. else:
  139. continue # a link inside the description (a source library, a dataset): not a component
  140. if "split" in spec2:
  141. files = files_for(spec2)
  142. for name, ver in spec2["split"]:
  143. add(name, ver, url, spec2["concluded"], spec2.get("declared"), used_for,
  144. [f for f in files if ("copying." + name) in f.lower()] or files)
  145. continue
  146. m = re.match(r"^(.*?)\s+(\d+(?:\.\d+)+)$", text)
  147. name = spec2.get("name") or (m.group(1) if m else text)
  148. version = spec2.get("version") or (m.group(2) if m else None)
  149. add(name, version, url, spec2["concluded"], spec2.get("declared"), used_for, files_for(spec2))
  150. for key, spec in EXTRA_ROWS.items():
  151. add(key, spec["version"], spec["url"], spec["concluded"], None, spec["used_for"], files_for(spec))
  152. for f in lic_files:
  153. if f not in claimed:
  154. errors.append(f"licences folder file {f} belongs to no SBOM component (add it to THIRD_PARTY.md / make-sbom.sh)")
  155. if errors:
  156. sys.exit("make-sbom:\n " + "\n ".join(errors))
  157. ids = [p["SPDXID"] for p in packages]
  158. if len(ids) != len(set(ids)):
  159. sys.exit("make-sbom: duplicate SPDX ids: " + ", ".join(sorted({i for i in ids if ids.count(i) > 1})))
  160. root = {
  161. "name": "LYNX SHARE", "SPDXID": "SPDXRef-LYNX-SHARE", "versionInfo": label,
  162. "downloadLocation": f"git+https://github.com/Ka1-S/LYNX-SHARE@{commit}" if commit != "unknown"
  163. else "https://github.com/Ka1-S/LYNX-SHARE",
  164. "homepage": "https://lynx-share.com", "filesAnalyzed": False,
  165. "licenseConcluded": "AGPL-3.0-only", "licenseDeclared": "AGPL-3.0-only",
  166. "copyrightText": NOA, "supplier": "Person: Ka1-S",
  167. "primaryPackagePurpose": "APPLICATION",
  168. }
  169. used_refs = sorted({r for p in packages for r in re.findall(r"LicenseRef-[A-Za-z0-9.-]+", p["licenseDeclared"])})
  170. doc = {
  171. "spdxVersion": "SPDX-2.3",
  172. "dataLicense": "CC0-1.0",
  173. "SPDXID": "SPDXRef-DOCUMENT",
  174. "name": f"LYNX-SHARE-{label}",
  175. "documentNamespace": f"https://github.com/Ka1-S/LYNX-SHARE/releases/v{label}/sbom-{uuid.uuid4()}",
  176. "creationInfo": {
  177. "created": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
  178. "creators": ["Tool: LYNX-SHARE scripts/make-sbom.sh", "Organization: LYNX SHARE"],
  179. "comment": "Third-party components of LYNX SHARE, from THIRD_PARTY.md and the packages' licenses folder.",
  180. },
  181. "documentDescribes": ["SPDXRef-LYNX-SHARE"],
  182. "packages": [root] + packages,
  183. "relationships": [{"spdxElementId": "SPDXRef-DOCUMENT", "relationshipType": "DESCRIBES",
  184. "relatedSpdxElement": "SPDXRef-LYNX-SHARE"}] +
  185. [{"spdxElementId": "SPDXRef-LYNX-SHARE", "relationshipType": "CONTAINS",
  186. "relatedSpdxElement": p["SPDXID"]} for p in packages],
  187. }
  188. if used_refs:
  189. doc["hasExtractedLicensingInfos"] = [{"licenseId": r, "name": LICENSE_REFS[r][0], "extractedText": LICENSE_REFS[r][1]}
  190. for r in used_refs]
  191. with open(out, "w", encoding="utf-8") as fh:
  192. json.dump(doc, fh, indent=2, ensure_ascii=False)
  193. fh.write("\n")
  194. print(f"make-sbom: {len(packages)} components -> {out}")
  195. PY