make-sbom.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "Open Sans": dict(concluded="OFL-1.1", folders=[], extra=["open-sans-OFL.txt"]),
  72. "Open Sans Condensed": dict(concluded="OFL-1.1", folders=[], extra=["open-sans-OFL.txt"]),
  73. }
  74. # Components that have a licence file in licenses/ but no row of their own in THIRD_PARTY.md's table.
  75. EXTRA_ROWS = {}
  76. LICENSE_REFS = {
  77. "LicenseRef-JUCE-Commercial": ("JUCE commercial licence",
  78. "JUCE 8 is also offered under a commercial licence by Raw Material Software; LYNX uses JUCE under the AGPL-3.0."),
  79. "LicenseRef-Steinberg-Proprietary": ("Steinberg proprietary SDK licence",
  80. "Steinberg offers the VST 3 and ASIO SDKs under a proprietary licence or the GPLv3; LYNX takes the GPLv3 option."),
  81. }
  82. rows = []
  83. for line in open("THIRD_PARTY.md", encoding="utf-8"):
  84. if not line.startswith("| ") or line.startswith("| Component") or line.startswith("|---"):
  85. continue
  86. cells = [c.strip() for c in line.strip().strip("|").split(" | ")]
  87. rows.append(cells)
  88. if not rows:
  89. sys.exit("make-sbom: no component table in THIRD_PARTY.md")
  90. def spdx_id(name):
  91. return "SPDXRef-" + re.sub(r"[^A-Za-z0-9.-]+", "-", name).strip("-")
  92. packages, claimed, errors = [], set(), []
  93. lic_files = sorted(f for f in os.listdir(lic_dir) if f != "README.txt")
  94. def files_for(spec):
  95. names = []
  96. for folder in spec.get("folders", []):
  97. for f in lic_files:
  98. if f.startswith(folder + "-") and not any(f.startswith(x + "-") for x in spec.get("exclude", [])):
  99. names.append(f)
  100. names += [f for f in spec.get("extra", []) if f in lic_files]
  101. claimed.update(names)
  102. return names
  103. def add(name, version, url, concluded, declared, used_for, licence_files):
  104. comment = "Used for: " + re.sub(r"\s+", " ", re.sub(r"[`*]", "", used_for)).strip()
  105. if licence_files:
  106. comment += ". Licence texts in the package's licenses folder: " + ", ".join(licence_files)
  107. packages.append({
  108. "name": name, "SPDXID": spdx_id(name), "versionInfo": version or NOA,
  109. "downloadLocation": url or NOA, "filesAnalyzed": False,
  110. "licenseConcluded": concluded, "licenseDeclared": declared or concluded,
  111. "copyrightText": NOA, "supplier": NOA, "comment": comment,
  112. })
  113. for cells in rows:
  114. first, used_for = cells[0], cells[1] if len(cells) > 1 else ""
  115. links = re.findall(r"\[([^\]]+)\]\(([^)]+)\)", first)
  116. key = links[0][0] if links else first
  117. spec = COMPONENTS.get(key) or next((v for k, v in COMPONENTS.items() if first.startswith(k)), None)
  118. if spec is None:
  119. errors.append(f"THIRD_PARTY.md row '{first[:60]}' has no entry in scripts/make-sbom.sh COMPONENTS")
  120. continue
  121. if spec.get("shipped") is False:
  122. continue
  123. if "split_libs" in spec:
  124. for name, lic, url in spec["split_libs"]:
  125. add(name, "bundled with JUCE 8.0.4", url, lic, None, used_for, [])
  126. continue
  127. # one row can name several projects ([Signalsmith Stretch 1.4](..) and [Signalsmith Linear 0.6.6](..), Link + Asio)
  128. for text, url in (links or [(key, None)]):
  129. spec2 = COMPONENTS.get(text)
  130. if spec2 is None:
  131. if text == key:
  132. spec2 = spec
  133. else:
  134. continue # a link inside the description (a source library, a dataset): not a component
  135. if "split" in spec2:
  136. files = files_for(spec2)
  137. for name, ver in spec2["split"]:
  138. add(name, ver, url, spec2["concluded"], spec2.get("declared"), used_for,
  139. [f for f in files if ("copying." + name) in f.lower()] or files)
  140. continue
  141. m = re.match(r"^(.*?)\s+(\d+(?:\.\d+)+)$", text)
  142. name = spec2.get("name") or (m.group(1) if m else text)
  143. version = spec2.get("version") or (m.group(2) if m else None)
  144. add(name, version, url, spec2["concluded"], spec2.get("declared"), used_for, files_for(spec2))
  145. for key, spec in EXTRA_ROWS.items():
  146. add(key, spec["version"], spec["url"], spec["concluded"], None, spec["used_for"], files_for(spec))
  147. for f in lic_files:
  148. if f not in claimed:
  149. errors.append(f"licences folder file {f} belongs to no SBOM component (add it to THIRD_PARTY.md / make-sbom.sh)")
  150. if errors:
  151. sys.exit("make-sbom:\n " + "\n ".join(errors))
  152. ids = [p["SPDXID"] for p in packages]
  153. if len(ids) != len(set(ids)):
  154. sys.exit("make-sbom: duplicate SPDX ids: " + ", ".join(sorted({i for i in ids if ids.count(i) > 1})))
  155. root = {
  156. "name": "LYNX SHARE", "SPDXID": "SPDXRef-LYNX-SHARE", "versionInfo": label,
  157. "downloadLocation": f"git+https://github.com/Ka1-S/LYNX-SHARE@{commit}" if commit != "unknown"
  158. else "https://github.com/Ka1-S/LYNX-SHARE",
  159. "homepage": "https://lynx-share.com", "filesAnalyzed": False,
  160. "licenseConcluded": "AGPL-3.0-only", "licenseDeclared": "AGPL-3.0-only",
  161. "copyrightText": NOA, "supplier": "Person: Ka1-S",
  162. "primaryPackagePurpose": "APPLICATION",
  163. }
  164. used_refs = sorted({r for p in packages for r in re.findall(r"LicenseRef-[A-Za-z0-9.-]+", p["licenseDeclared"])})
  165. doc = {
  166. "spdxVersion": "SPDX-2.3",
  167. "dataLicense": "CC0-1.0",
  168. "SPDXID": "SPDXRef-DOCUMENT",
  169. "name": f"LYNX-SHARE-{label}",
  170. "documentNamespace": f"https://github.com/Ka1-S/LYNX-SHARE/releases/v{label}/sbom-{uuid.uuid4()}",
  171. "creationInfo": {
  172. "created": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
  173. "creators": ["Tool: LYNX-SHARE scripts/make-sbom.sh", "Organization: LYNX SHARE"],
  174. "comment": "Third-party components of LYNX SHARE, from THIRD_PARTY.md and the packages' licenses folder.",
  175. },
  176. "documentDescribes": ["SPDXRef-LYNX-SHARE"],
  177. "packages": [root] + packages,
  178. "relationships": [{"spdxElementId": "SPDXRef-DOCUMENT", "relationshipType": "DESCRIBES",
  179. "relatedSpdxElement": "SPDXRef-LYNX-SHARE"}] +
  180. [{"spdxElementId": "SPDXRef-LYNX-SHARE", "relationshipType": "CONTAINS",
  181. "relatedSpdxElement": p["SPDXID"]} for p in packages],
  182. }
  183. if used_refs:
  184. doc["hasExtractedLicensingInfos"] = [{"licenseId": r, "name": LICENSE_REFS[r][0], "extractedText": LICENSE_REFS[r][1]}
  185. for r in used_refs]
  186. with open(out, "w", encoding="utf-8") as fh:
  187. json.dump(doc, fh, indent=2, ensure_ascii=False)
  188. fh.write("\n")
  189. print(f"make-sbom: {len(packages)} components -> {out}")
  190. PY