mirror of
https://git.yoctoproject.org/poky
synced 2026-06-01 00:59:48 +00:00
classes/spdx-common: Move to library
Moves the bulk of the code in the spdx-common bbclass into library code (From OE-Core rev: 3f9b7c7f6b15493b6890031190ca8d1a10f2f384) Signed-off-by: Joshua Watt <JPEWhacker@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
committed by
Richard Purdie
parent
454008311b
commit
9850df1b60
@@ -37,96 +37,11 @@ SPDX_LICENSES ??= "${COREBASE}/meta/files/spdx-licenses.json"
|
||||
|
||||
SPDX_CUSTOM_ANNOTATION_VARS ??= ""
|
||||
|
||||
def extract_licenses(filename):
|
||||
import re
|
||||
|
||||
lic_regex = re.compile(rb'^\W*SPDX-License-Identifier:\s*([ \w\d.()+-]+?)(?:\s+\W*)?$', re.MULTILINE)
|
||||
|
||||
try:
|
||||
with open(filename, 'rb') as f:
|
||||
size = min(15000, os.stat(filename).st_size)
|
||||
txt = f.read(size)
|
||||
licenses = re.findall(lic_regex, txt)
|
||||
if licenses:
|
||||
ascii_licenses = [lic.decode('ascii') for lic in licenses]
|
||||
return ascii_licenses
|
||||
except Exception as e:
|
||||
bb.warn(f"Exception reading {filename}: {e}")
|
||||
return []
|
||||
|
||||
def is_work_shared_spdx(d):
|
||||
return bb.data.inherits_class('kernel', d) or ('work-shared' in d.getVar('WORKDIR'))
|
||||
|
||||
def get_json_indent(d):
|
||||
if d.getVar("SPDX_PRETTY") == "1":
|
||||
return 2
|
||||
return None
|
||||
|
||||
def load_spdx_license_data(d):
|
||||
import json
|
||||
if d.getVar("SPDX_LICENSE_DATA"):
|
||||
return
|
||||
|
||||
with open(d.getVar("SPDX_LICENSES"), "r") as f:
|
||||
data = json.load(f)
|
||||
# Transform the license array to a dictionary
|
||||
data["licenses"] = {l["licenseId"]: l for l in data["licenses"]}
|
||||
d.setVar("SPDX_LICENSE_DATA", data)
|
||||
|
||||
python() {
|
||||
load_spdx_license_data(d)
|
||||
import oe.spdx_common
|
||||
oe.spdx_common.load_spdx_license_data(d)
|
||||
}
|
||||
|
||||
def process_sources(d):
|
||||
pn = d.getVar('PN')
|
||||
assume_provided = (d.getVar("ASSUME_PROVIDED") or "").split()
|
||||
if pn in assume_provided:
|
||||
for p in d.getVar("PROVIDES").split():
|
||||
if p != pn:
|
||||
pn = p
|
||||
break
|
||||
|
||||
# glibc-locale: do_fetch, do_unpack and do_patch tasks have been deleted,
|
||||
# so avoid archiving source here.
|
||||
if pn.startswith('glibc-locale'):
|
||||
return False
|
||||
if d.getVar('PN') == "libtool-cross":
|
||||
return False
|
||||
if d.getVar('PN') == "libgcc-initial":
|
||||
return False
|
||||
if d.getVar('PN') == "shadow-sysroot":
|
||||
return False
|
||||
|
||||
# We just archive gcc-source for all the gcc related recipes
|
||||
if d.getVar('BPN') in ['gcc', 'libgcc']:
|
||||
bb.debug(1, 'spdx: There is bug in scan of %s is, do nothing' % pn)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def collect_direct_deps(d, dep_task):
|
||||
current_task = "do_" + d.getVar("BB_CURRENTTASK")
|
||||
pn = d.getVar("PN")
|
||||
|
||||
taskdepdata = d.getVar("BB_TASKDEPDATA", False)
|
||||
|
||||
for this_dep in taskdepdata.values():
|
||||
if this_dep[0] == pn and this_dep[1] == current_task:
|
||||
break
|
||||
else:
|
||||
bb.fatal(f"Unable to find this {pn}:{current_task} in taskdepdata")
|
||||
|
||||
deps = set()
|
||||
|
||||
for dep_name in this_dep.deps:
|
||||
dep_data = taskdepdata[dep_name]
|
||||
if dep_data.taskname == dep_task and dep_data.pn != pn:
|
||||
deps.add((dep_data.pn, dep_data.hashfn, dep_name in this_dep.taskhash_deps))
|
||||
|
||||
return sorted(deps)
|
||||
|
||||
collect_direct_deps[vardepsexclude] += "BB_TASKDEPDATA"
|
||||
collect_direct_deps[vardeps] += "DEPENDS"
|
||||
|
||||
python do_collect_spdx_deps() {
|
||||
# This task calculates the build time dependencies of the recipe, and is
|
||||
@@ -136,11 +51,12 @@ python do_collect_spdx_deps() {
|
||||
# do_create_spdx reads in the found dependencies when writing the actual
|
||||
# SPDX document
|
||||
import json
|
||||
import oe.spdx_common
|
||||
from pathlib import Path
|
||||
|
||||
spdx_deps_file = Path(d.getVar("SPDXDEPS"))
|
||||
|
||||
deps = collect_direct_deps(d, "do_create_spdx")
|
||||
deps = oe.spdx_common.collect_direct_deps(d, "do_create_spdx")
|
||||
|
||||
with spdx_deps_file.open("w") as f:
|
||||
json.dump(deps, f)
|
||||
@@ -151,104 +67,7 @@ do_collect_spdx_deps[depends] += "${PATCHDEPENDENCY}"
|
||||
do_collect_spdx_deps[deptask] = "do_create_spdx"
|
||||
do_collect_spdx_deps[dirs] = "${SPDXDIR}"
|
||||
|
||||
def get_spdx_deps(d):
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
spdx_deps_file = Path(d.getVar("SPDXDEPS"))
|
||||
|
||||
with spdx_deps_file.open("r") as f:
|
||||
return json.load(f)
|
||||
|
||||
def collect_package_providers(d):
|
||||
from pathlib import Path
|
||||
import oe.sbom
|
||||
import oe.spdx
|
||||
import json
|
||||
|
||||
deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
|
||||
|
||||
providers = {}
|
||||
|
||||
deps = collect_direct_deps(d, "do_create_spdx")
|
||||
deps.append((d.getVar("PN"), d.getVar("BB_HASHFILENAME"), True))
|
||||
|
||||
for dep_pn, dep_hashfn, _ in deps:
|
||||
localdata = d
|
||||
recipe_data = oe.packagedata.read_pkgdata(dep_pn, localdata)
|
||||
if not recipe_data:
|
||||
localdata = bb.data.createCopy(d)
|
||||
localdata.setVar("PKGDATA_DIR", "${PKGDATA_DIR_SDK}")
|
||||
recipe_data = oe.packagedata.read_pkgdata(dep_pn, localdata)
|
||||
|
||||
for pkg in recipe_data.get("PACKAGES", "").split():
|
||||
|
||||
pkg_data = oe.packagedata.read_subpkgdata_dict(pkg, localdata)
|
||||
rprovides = set(n for n, _ in bb.utils.explode_dep_versions2(pkg_data.get("RPROVIDES", "")).items())
|
||||
rprovides.add(pkg)
|
||||
|
||||
if "PKG" in pkg_data:
|
||||
pkg = pkg_data["PKG"]
|
||||
rprovides.add(pkg)
|
||||
|
||||
for r in rprovides:
|
||||
providers[r] = (pkg, dep_hashfn)
|
||||
|
||||
return providers
|
||||
|
||||
collect_package_providers[vardepsexclude] += "BB_TASKDEPDATA"
|
||||
|
||||
def spdx_get_src(d):
|
||||
"""
|
||||
save patched source of the recipe in SPDX_WORKDIR.
|
||||
"""
|
||||
import shutil
|
||||
spdx_workdir = d.getVar('SPDXWORK')
|
||||
spdx_sysroot_native = d.getVar('STAGING_DIR_NATIVE')
|
||||
pn = d.getVar('PN')
|
||||
|
||||
workdir = d.getVar("WORKDIR")
|
||||
|
||||
try:
|
||||
# The kernel class functions require it to be on work-shared, so we dont change WORKDIR
|
||||
if not is_work_shared_spdx(d):
|
||||
# Change the WORKDIR to make do_unpack do_patch run in another dir.
|
||||
d.setVar('WORKDIR', spdx_workdir)
|
||||
# Restore the original path to recipe's native sysroot (it's relative to WORKDIR).
|
||||
d.setVar('STAGING_DIR_NATIVE', spdx_sysroot_native)
|
||||
|
||||
# The changed 'WORKDIR' also caused 'B' changed, create dir 'B' for the
|
||||
# possibly requiring of the following tasks (such as some recipes's
|
||||
# do_patch required 'B' existed).
|
||||
bb.utils.mkdirhier(d.getVar('B'))
|
||||
|
||||
bb.build.exec_func('do_unpack', d)
|
||||
# Copy source of kernel to spdx_workdir
|
||||
if is_work_shared_spdx(d):
|
||||
share_src = d.getVar('WORKDIR')
|
||||
d.setVar('WORKDIR', spdx_workdir)
|
||||
d.setVar('STAGING_DIR_NATIVE', spdx_sysroot_native)
|
||||
src_dir = spdx_workdir + "/" + d.getVar('PN')+ "-" + d.getVar('PV') + "-" + d.getVar('PR')
|
||||
bb.utils.mkdirhier(src_dir)
|
||||
if bb.data.inherits_class('kernel',d):
|
||||
share_src = d.getVar('STAGING_KERNEL_DIR')
|
||||
cmd_copy_share = "cp -rf " + share_src + "/* " + src_dir + "/"
|
||||
cmd_copy_shared_res = os.popen(cmd_copy_share).read()
|
||||
bb.note("cmd_copy_shared_result = " + cmd_copy_shared_res)
|
||||
|
||||
git_path = src_dir + "/.git"
|
||||
if os.path.exists(git_path):
|
||||
shutils.rmtree(git_path)
|
||||
|
||||
# Make sure gcc and kernel sources are patched only once
|
||||
if not (d.getVar('SRC_URI') == "" or is_work_shared_spdx(d)):
|
||||
bb.build.exec_func('do_patch', d)
|
||||
|
||||
# Some userland has no source.
|
||||
if not os.path.exists( spdx_workdir ):
|
||||
bb.utils.mkdirhier(spdx_workdir)
|
||||
finally:
|
||||
d.setVar("WORKDIR", workdir)
|
||||
|
||||
spdx_get_src[vardepsexclude] += "STAGING_KERNEL_DIR"
|
||||
|
||||
oe.spdx_common.collect_direct_deps[vardepsexclude] += "BB_TASKDEPDATA"
|
||||
oe.spdx_common.collect_direct_deps[vardeps] += "DEPENDS"
|
||||
oe.spdx_common.collect_package_providers[vardepsexclude] += "BB_TASKDEPDATA"
|
||||
oe.spdx_common.get_patched_src[vardepsexclude] += "STAGING_KERNEL_DIR"
|
||||
|
||||
Reference in New Issue
Block a user