From patchwork Wed Nov 25 18:36:10 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Tondo X-Patchwork-Id: 105710 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by mx.groups.io with SMTP id smtpd.msgproc02-g2.48617.1795631779464494774 for ; Wed, 25 Nov 2026 10:36:19 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=VBJZ2+Sl; spf=pass (domain: gmail.com, ip: 209.85.221.42, mailfrom: stondo@gmail.com) Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-4a357b1c649so3590647f8f.3 for ; Wed, 25 Nov 2026 10:36:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795631778; x=1796236578; darn=lists.openembedded.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6HldrghvJlpN9mTMY86AyoE3qFpe/gYwg6+yoM3Mv+Y=; b=VBJZ2+Sl4GxOmSquoSY+sUyFbhnS7e3BHygEbl6vFcNHTM/uwRxTCT/HAjeApEY578 oNAQgdLUNEj4s7VFXAVrrmQmrSdt/zJLdGgMdS1XX7KXoKfovCoVgcVnExgaE4WyYUYs ONcUYzIHd9U8ASD3TkG1ImrwTzeXa+q1QTd8U6dAzjY+ZLPD/nZq85IeZeqna6yr3WyT MGSsZQB/rBUe3rJ4r91uebfZSViOQCUFwWGHWt1JWSerif2FVWlwv3CYczoS3VQTyIXE DDSxmNlP7TMptgWX2G9iprMAkm1xjvQexBWeRbAVk0VtUPfYPx8rVDc2KDiBOYrNzPNN E4AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795631778; x=1796236578; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=6HldrghvJlpN9mTMY86AyoE3qFpe/gYwg6+yoM3Mv+Y=; b=xKAiHV2jPC3FH17+tws+pZJZQpzVna4FOMEPsFml0cTk/ygpmjfy1BBswpPbcVdERW bNUs3G+7x6BlB4VcQvg2qDS2hI85/QgzIHfemODvxyFqZW4ghc2bv4E8iPxBLHPyydAm iD0s/lhWM4FiibfnHuaJ7KhXO2son+SA5X+dB2DDZ/r1p85eHnWei3Fb0PYPWnxjgcr4 f9W82Hw4NyYmY+DE+hUnT1n7jw31wlRLFmx/+zmgb6v9wsiOwkF3sflzdt17TvJvDJjp egubog6m//gh7BDZ++je++dB77kDQIMAOg3jRmYYPfOQ0xsedjjWW8F1ZXsA3b0s2AJ7 lOLA== X-Gm-Message-State: APuU4Z9FDrb5jU+VfmUainLAWy2LPhZy+8z+TFI9b33YpiTiWEmcMWG2 NwjjQdyFjV5GYxD3NzLjx54qBwJTEfzLYzSCNjOBfvbOWtY/EWoDNSHvUs9+Fks6 X-Gm-Gg: AZFdTZcebQxL7qw4Z4S9cdiQoboGhCfsNz/DA0KaDWOrRIYYvfoypwVfHlD+BZmdm4a uR0qVyhqXnvUbBvBb5mPBh3kdmiDrWSaeObxurWCaahfQ8y59f0M+gOUrDmijJ2oKyf2Rvtk93k jJxz8os6Q9bfM8ytm5Yv1vOi4GyF236gMaT5UBjJgXcfRm+oNvwy20RV0JETgehcC8azDy+2ZiS 6n7zfEfwJUVVBgRf4bspnJHh/+Pyl0JTFxsbg7qdw2QVTpmjBEakclO+CC0JAWisV5/jKnY78EK u2Ed9VExCBul7jJIAXxRvg+w6buOuMWNDxMANbX02KDATdG7oswTJb4PSirUcSKN2jPTXrhdtPq E/5SkE0Q6wNhe7DQjfm+oBUrHmNwyWJLicLgKhlsSu+mU8k13Gw0np8/on6UgUgeUvgHR/mR3ZS 3P2PLgdAtkSPRTjZLJK17ZYLtp1RX4fSh2C1dgQ4scE5bKoSpTqfQAMRO3BC6WKAM3cz3FYpBzS VVdQgp3cTPtsXcACXDpkRx+qSh81LSC04d/qTLU06VYRlrZZVRKfsKBJlZhnBbAoy0IY9B+W9Ap XZuAuSPmWXPx X-Received: by 2002:adf:e013:0:10b0:4a6:c3f:9d22 with SMTP id ffacd0b85a97d-4abb32a41fdmr25566331f8f.39.1795631777542; Wed, 25 Nov 2026 10:36:17 -0800 (PST) Return-Path: Received: from fedora ([165.225.94.251]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2c8csm36941016f8f.2.2026.11.25.10.36.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 10:36:16 -0800 (PST) From: Stefano Tondo To: openembedded-core@lists.openembedded.org Cc: Joshua Watt , Alexander Kanavin , Stefan Herbrechtsmeier , Paul Barker , Mathieu Dubois-Briand , Peter Marko , stefano.tondo.ext@siemens.com Subject: [PATCH v4 1/4] spdx30: Take name, version and PURL of downloads from the fetcher Date: Wed, 25 Nov 2026 19:36:10 +0100 Message-ID: <20261125183613.1335718-2-stondo@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125183613.1335718-1-stondo@gmail.com> References: <20261125183613.1335718-1-stondo@gmail.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Wed, 25 Nov 2026 18:36:24 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/openembedded-core/message/251237 From: Stefano Tondo The fetchers of package registries (crate, gomod, npm) know the name and the version of each package they download. Today the SPDX of such a download only has the file name, so an SBOM scanner cannot match the dependency. Ask the fetcher with package_identity() and set the name, the version and the PURL of the download. This needs package_identity() in bitbake. Signed-off-by: Stefano Tondo Reviewed-by: Joshua Watt --- meta/lib/oe/spdx30_tasks.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/meta/lib/oe/spdx30_tasks.py b/meta/lib/oe/spdx30_tasks.py index 6eaa74ce3d..eded315819 100644 --- a/meta/lib/oe/spdx30_tasks.py +++ b/meta/lib/oe/spdx30_tasks.py @@ -463,6 +463,19 @@ def enrich_source_package(d, dl, fd, file_name, primary_purpose): ) +def set_package_identity(d, dl, fd): + """Set name, version and PURL of a download that names a package. + + Fetchers of package registries (crate, gomod, npm) know which package + a URL downloads, see FetchMethod.package_identity() in bitbake. + """ + identity = fd.method.package_identity(fd, d) + if identity: + dl.name = identity.name + dl.software_packageVersion = identity.version + dl.software_packageUrl = identity.purl + + def add_download_files(d, objset): inputs = set() release_date = get_release_date(d) @@ -528,6 +541,7 @@ def add_download_files(d, objset): ) enrich_source_package(d, dl, fd, file_name, primary_purpose) + set_package_identity(d, dl, fd) if release_date is not None: dl.releaseTime = release_date From patchwork Wed Nov 25 18:36:11 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Tondo X-Patchwork-Id: 105711 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by mx.groups.io with SMTP id smtpd.msgproc02-g2.48618.1795631780726357208 for ; Wed, 25 Nov 2026 10:36:21 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=AslriHFn; spf=pass (domain: gmail.com, ip: 209.85.221.46, mailfrom: stondo@gmail.com) Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-4aa5833c263so5898162f8f.2 for ; Wed, 25 Nov 2026 10:36:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795631779; x=1796236579; darn=lists.openembedded.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=iL3jDwl9cMryG6lLJ4d0ZLn7apqKT+XE1mTDuw40x0o=; b=AslriHFnp5CaEHZG1H2qZ1I+riHVQD/E+kn4MtkTv/BEMICUH3sG5Z4gQsSIdRLs8P uqW4WzwN+ZMiddfUHFeYf6npGOUG7Ph5cM6Oa+F3IarEVAcPXu9NL/PVVZuJYQ2b/qzV +xO+ldBOBAVRxrlDZYUUY2wON4liYickAxFcGKhBy/Flzvc4P/iezeI7eZV5gJyz2smF gTnclkQYoRPcmRXH2r99o1tBaJ2C4CjG/8sY7XruYT1Yh9KNoGMZsimWZ6BQAVXVyfD8 bDH0f6nEz7eDA0Hih2PIYotNaF8DPbOUI6edTSz3jvD3UtaIie4aQtroThz9ziyBHftp 7l3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795631779; x=1796236579; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=iL3jDwl9cMryG6lLJ4d0ZLn7apqKT+XE1mTDuw40x0o=; b=DjLObABM1F4kDR6HpLwaBFZOIgP4JpYpWBMSXqTjiJMno4IZ72zkCFnx44LJR6/dUr IN98s0ELDtUN9BGUOpy/yr/93ergWuTB2PRhIQkkD2TPi0dMyKvdNfbw2lZKGC2Fdk5h oN7A/vOg8a+qCwEEF3LXdZ1OOjdIslvZWSm+lOdVYomq7ZPj2tKkx3agngKPsM4JCjCb gYaJijQPKePeC+WiWaXFHE2T1Cvbk+txQKCHExQNLGljtstEMKzwL8V8eNTUqrTY9i+S GLa9aQrbJg7TVNS8Z5TMQ9sWmk4Yzu84R0qwp1Ri/lL0WpNXZJErymIaVfZeVy9DbhTQ QW2A== X-Gm-Message-State: APuU4Z9IWVo2U1zaa6+PXQLw4h1iVzEwH7MDZy6wVVac8BAirrxv7hwn CPZ/aLZPw5Xn3R3UQllDuYWLGrqfw5Ye1zyA5/+5SX1TPbNqDvHht6HKduv0M06F X-Gm-Gg: AZFdTZcnyFHH/h8MShr2PIRdnsULkTjN7nwf9x78Lx6lYS6uG88IJzyzlyZl8EURbzs JKjRIFh3fioko3toMnXBWvTBrn0vioyvAhPWSgxCvtEPph/eaXl1FcblZ9i6IgypsOvyTUMGjaI cRBhxjMX1EXLgMGpUTR/F8Jlv5kGC5Oxhr4IYSoES0se0CYZkwGxHorp8sYN31hM0Tgw9b8FtHm 0PXoXDRR1LwG3tYgZDVwbZyKHWgTs2JfEBcBGrLWhFvnLEWJoEn+/HgM3VOEnS2Td9cC2F6aYLH qdDq3db3v+N3qtqIr5u7js34wiBQV4jX0JusH1msKgh/52yBr6Ju7QFRpw1QczkldTg/w6EbYAY bzVhFKGKcokMCgYtk3Ec4K08VMZVW4ng8a/aJtxEPWJ9kx6ViYDd9EGl1UcEFkXpS9bjdqoShvb pCT5eTxx9DWifDm+Gb7O/jjsTy9U2co1KqswzsmVL3IJJXX4Iqq6UYQINb+nxgRVq919647hQlY 4v82bX/69hDB/RN7zbrEAdqjVv6mU1cvraev7tIvFeinkSv8HAuOeVgmgcrMCPCh1cBl7HxKrKQ nahoWMzN30njEC+ucATiVOtC++z7yXsN X-Received: by 2002:a05:600c:6749:b0:4a2:1867:10e8 with SMTP id 5b1f17b1804b1-4bb8881a294mr372491295e9.25.1795631778705; Wed, 25 Nov 2026 10:36:18 -0800 (PST) Return-Path: Received: from fedora ([165.225.94.251]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2c8csm36941016f8f.2.2026.11.25.10.36.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 10:36:18 -0800 (PST) From: Stefano Tondo To: openembedded-core@lists.openembedded.org Cc: Joshua Watt , Alexander Kanavin , Stefan Herbrechtsmeier , Paul Barker , Mathieu Dubois-Briand , Peter Marko , stefano.tondo.ext@siemens.com Subject: [PATCH v4 2/4] spdx30: Record the lifecycle scope of dependencies Date: Wed, 25 Nov 2026 19:36:11 +0100 Message-ID: <20261125183613.1335718-3-stondo@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125183613.1335718-1-stondo@gmail.com> References: <20261125183613.1335718-1-stondo@gmail.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Wed, 25 Nov 2026 18:36:24 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/openembedded-core/message/251238 From: Stefano Tondo A Cargo.lock file lists the crates for all targets, and some crates only run at build time (build scripts, proc-macros and the crates they use). The fetcher downloads all of them, but only some end up in the binary. Add the variable SPDX_DEPENDENCY_SCOPE_FUNCS. A class adds a function to it that gives the downloads of a recipe a lifecycle scope. The create_spdx task links the build to each download with a scoped dependsOn relationship: runtime, build or test. A download that is fetched but not used on the target gets an annotation instead. The SPDX code does not read the files of a package manager. For Cargo, add a do_cargo_metadata task that writes the dependency graph for the target with cargo metadata --filter-platform, and the scope function oe.rust.cargo_dependency_scopes() that reads it. The graph has all features, because a recipe can enable features in several places (CARGO_BUILD_FLAGS, PACKAGECONFIG, pyproject.toml). So a crate counts as not used on the target only when no feature uses it. If cargo cannot give the graph, the task does not fail, and the crate downloads get no scope. The walk expands each workspace member only once. Members can depend on each other in a cycle, and a member can be a dev dependency of itself, as uv-preview is in python3-uv-build. Add unit tests for the scopes, with member cycles. Signed-off-by: Stefano Tondo --- meta/classes-recipe/cargo.bbclass | 27 ++++++ meta/classes/create-spdx-3.0.bbclass | 12 +++ meta/lib/oe/rust.py | 74 ++++++++++++++ meta/lib/oe/spdx30_tasks.py | 48 ++++++++++ .../oeqa/selftest/cases/oelib/cargo_scopes.py | 96 +++++++++++++++++++ 5 files changed, 257 insertions(+) create mode 100644 meta/lib/oeqa/selftest/cases/oelib/cargo_scopes.py diff --git a/meta/classes-recipe/cargo.bbclass b/meta/classes-recipe/cargo.bbclass index 586071988a..4bb685599a 100644 --- a/meta/classes-recipe/cargo.bbclass +++ b/meta/classes-recipe/cargo.bbclass @@ -26,6 +26,33 @@ cargo_do_compile () { "${CARGO}" build ${CARGO_BUILD_FLAGS} ${PACKAGECONFIG_CONFARGS} "$@" } +# The crate dependency graph for the target, with the crates that are not +# used on it removed. create-spdx uses it to give each crate download a +# lifecycle scope. The graph has all features, as the features that a +# recipe enables are not known here: a crate is left out only when no +# feature uses it on the target. +CARGO_METADATA_FILE = "${WORKDIR}/cargo-metadata.json" + +do_cargo_metadata () { + rm -f "${CARGO_METADATA_FILE}" + # Some recipes inherit cargo without being a cargo project + if [ ! -f "${CARGO_MANIFEST_PATH}" ]; then + return + fi + # Without the graph, create-spdx keeps the PURLs of all crates. So do + # not fail when cargo cannot give it, for example when the recipe + # configures the crate sources in its own way. + if ! "${CARGO}" metadata --format-version 1 --offline --locked \ + --manifest-path "${CARGO_MANIFEST_PATH}" \ + --filter-platform "${RUST_HOST_SYS}" --all-features \ + > "${CARGO_METADATA_FILE}"; then + bbnote "cargo metadata failed: the crate downloads get no lifecycle scope" + rm -f "${CARGO_METADATA_FILE}" + fi +} +addtask cargo_metadata after do_configure before do_compile do_create_spdx +SPDX_DEPENDENCY_SCOPE_FUNCS += "oe.rust.cargo_dependency_scopes" + cargo_do_install () { local have_installed=false for tgt in "${B}/target/${CARGO_TARGET_SUBDIR}/"*; do diff --git a/meta/classes/create-spdx-3.0.bbclass b/meta/classes/create-spdx-3.0.bbclass index 3c37a50a69..3a69305127 100644 --- a/meta/classes/create-spdx-3.0.bbclass +++ b/meta/classes/create-spdx-3.0.bbclass @@ -21,6 +21,15 @@ SPDX_INCLUDE_BITBAKE_PARENT_BUILD[doc] = "Report the parent invocation of bitbak for each Build object. This allows you to know who invoked bitbake to perform \ a build, but will result in non-reproducible SPDX output." +SPDX_DEPENDENCY_SCOPE_FUNCS ?= "" +SPDX_DEPENDENCY_SCOPE_FUNCS[doc] = "Space-separated list of functions that give \ + the downloads of a recipe a lifecycle scope. Each function is called as \ + func(d, identities). identities is a list of bb.fetch.PackageIdentity, one \ + for each PURL of the downloads. The function returns a dict that maps the \ + PURL of each download that it knows to the name of a LifecycleScopeType, for \ + example runtime, build or test, or to None for a download that is fetched \ + but not used on the target." + SPDX_PACKAGE_ADDITIONAL_PURPOSE ?= "" SPDX_PACKAGE_ADDITIONAL_PURPOSE[doc] = "The list of additional purposes to assign to \ the generated packages for a recipe. The primary purpose is always `install`. \ @@ -283,6 +292,9 @@ do_create_spdx[depends] += " \ ${@create_spdx_source_deps(d)} \ " do_create_spdx[vardeps] += "${SPDX3_VAR_DEPS}" +# The scope functions are called by name, so bitbake does not find them in +# the code +do_create_spdx[vardeps] += "${SPDX_DEPENDENCY_SCOPE_FUNCS}" python do_create_spdx_setscene () { sstate_setscene(d) diff --git a/meta/lib/oe/rust.py b/meta/lib/oe/rust.py index 1dc9cf150d..a0845c0135 100644 --- a/meta/lib/oe/rust.py +++ b/meta/lib/oe/rust.py @@ -4,8 +4,82 @@ # SPDX-License-Identifier: MIT # +import json +import os + # Handle mismatches between `uname -m`-style output and Rust's arch names def arch_to_rust_arch(arch): if arch == "ppc64le": return "powerpc64le" return arch + + +def get_cargo_scopes(metadata): + """Return {(crate, version): scope} for the output of cargo metadata. + + Walks the dependency graph from the workspace members: normal + dependencies are runtime, build dependencies and proc-macro crates (and + everything they pull in) are build, dev dependencies of the members are + test. + """ + packages = {p["id"]: p for p in metadata["packages"]} + nodes = {n["id"]: n for n in metadata["resolve"]["nodes"]} + members = set(metadata["workspace_members"]) + rank = {"test": 1, "build": 2, "runtime": 3} + + def is_proc_macro(pkg_id): + return any("proc-macro" in t["kind"] for t in packages[pkg_id]["targets"]) + + scopes = {} + expanded_members = set() + stack = [(m, "runtime") for m in members] + while stack: + pkg_id, scope = stack.pop() + if pkg_id in members: + # A member is a root with the runtime scope. Expand it only + # once: members can depend on each other in a cycle, and a + # member can even be a dev dependency of itself. + if pkg_id in expanded_members: + continue + expanded_members.add(pkg_id) + scope = "runtime" + else: + if scope == "runtime" and is_proc_macro(pkg_id): + scope = "build" + if rank.get(scopes.get(pkg_id), 0) >= rank[scope]: + continue + scopes[pkg_id] = scope + for dep in nodes[pkg_id]["deps"]: + for kind in (k["kind"] for k in dep["dep_kinds"]): + if kind == "dev": + if pkg_id in members: + stack.append((dep["pkg"], "test")) + elif kind == "build": + stack.append((dep["pkg"], "test" if scope == "test" else "build")) + else: + stack.append((dep["pkg"], scope)) + + return { + (packages[i]["name"], packages[i]["version"]): s for i, s in scopes.items() + } + + +def cargo_dependency_scopes(d, identities): + """Return the lifecycle scope of the crate downloads. + + See SPDX_DEPENDENCY_SCOPE_FUNCS. CARGO_METADATA_FILE is the output of + cargo metadata --filter-platform for the target, so a crate that is + only used on other targets gets None. + """ + path = d.getVar("CARGO_METADATA_FILE") + if not path or not os.path.exists(path): + return {} + + with open(path) as f: + scopes = get_cargo_scopes(json.load(f)) + + return { + i.purl: scopes.get((i.name, i.version)) + for i in identities + if i.purl.startswith("pkg:cargo/") + } diff --git a/meta/lib/oe/spdx30_tasks.py b/meta/lib/oe/spdx30_tasks.py index eded315819..588be68e58 100644 --- a/meta/lib/oe/spdx30_tasks.py +++ b/meta/lib/oe/spdx30_tasks.py @@ -476,6 +476,53 @@ def set_package_identity(d, dl, fd): dl.software_packageUrl = identity.purl +def add_dependency_scopes(d, objset, build, source_files): + """Give the downloads a lifecycle scope, see SPDX_DEPENDENCY_SCOPE_FUNCS. + + The build gets a scoped dependsOn relationship to each download with a + scope. A download that is fetched but not used on the target gets an + annotation instead. + """ + downloads = {} + for dl in source_files: + purl = getattr(dl, "software_packageUrl", None) + if purl: + downloads.setdefault(purl, []).append(dl) + identities = [ + bb.fetch.PackageIdentity(dls[0].name, dls[0].software_packageVersion, purl) + for purl, dls in downloads.items() + ] + + scopes = {} + for func in (d.getVar("SPDX_DEPENDENCY_SCOPE_FUNCS") or "").split(): + scopes.update( + bb.utils.better_eval(func + "(d, identities)", {"d": d, "identities": identities}) + ) + + by_scope = {} + for purl, scope in scopes.items(): + if scope is not None and scope not in oe.spdx30.LifecycleScopeType.NAMED_INDIVIDUALS: + bb.fatal("SPDX_DEPENDENCY_SCOPE_FUNCS: %s is not a lifecycle scope" % scope) + for dl in downloads.get(purl, []): + if scope is None: + objset.new_annotation( + dl, + "%s %s is fetched but not built: not used on the target" + % (dl.name, dl.software_packageVersion), + oe.spdx30.AnnotationType.other, + ) + else: + by_scope.setdefault(scope, []).append(dl) + + for scope, dls in sorted(by_scope.items()): + objset.new_scoped_relationship( + [build], + oe.spdx30.RelationshipType.dependsOn, + getattr(oe.spdx30.LifecycleScopeType, scope), + sorted(oe.sbom30.get_element_link_id(dl) for dl in dls), + ) + + def add_download_files(d, objset): inputs = set() release_date = get_release_date(d) @@ -978,6 +1025,7 @@ def create_spdx(d): source_files = add_download_files(d, build_objset) build_inputs |= source_files + add_dependency_scopes(d, build_objset, build, source_files) recipe_spdx_license = add_license_expression( d, build_objset, d.getVar("LICENSE"), license_data, [recipe_objset] diff --git a/meta/lib/oeqa/selftest/cases/oelib/cargo_scopes.py b/meta/lib/oeqa/selftest/cases/oelib/cargo_scopes.py new file mode 100644 index 0000000000..f16718abb9 --- /dev/null +++ b/meta/lib/oeqa/selftest/cases/oelib/cargo_scopes.py @@ -0,0 +1,96 @@ +# +# Copyright OpenEmbedded Contributors +# +# SPDX-License-Identifier: MIT +# + +import json +import os +import tempfile +from unittest.case import TestCase + +import bb.data +import bb.fetch +import oe.rust + + +def package(name, proc_macro=False): + kind = ["proc-macro"] if proc_macro else ["lib"] + return {"id": name, "name": name, "version": "1.0.0", "targets": [{"kind": kind}]} + + +def dep(name, *kinds): + return {"pkg": name, "dep_kinds": [{"kind": kind} for kind in kinds or (None,)]} + + +class TestCargoScopes(TestCase): + def get_scopes(self, members, packages, deps): + metadata = { + "packages": packages, + "workspace_members": members, + "resolve": {"nodes": [{"id": p["id"], "deps": deps.get(p["id"], [])} for p in packages]}, + } + scopes = oe.rust.get_cargo_scopes(metadata) + return {name: scope for (name, version), scope in scopes.items()} + + def test_scopes(self): + packages = [package(n) for n in ("app", "runtime-dep", "build-dep", "build-dep-dep", + "dev-dep", "shared")] + packages.append(package("derive", proc_macro=True)) + deps = { + "app": [dep("runtime-dep"), dep("build-dep", "build"), dep("dev-dep", "dev"), + dep("derive"), dep("shared", "dev")], + "build-dep": [dep("build-dep-dep")], + "runtime-dep": [dep("shared")], + } + self.assertEqual(self.get_scopes(["app"], packages, deps), { + "runtime-dep": "runtime", + "shared": "runtime", + "build-dep": "build", + "build-dep-dep": "build", + "derive": "build", + "dev-dep": "test", + }) + + def test_member_cycles(self): + # Workspace members that depend on each other in a cycle, and a member + # that is a dev dependency of itself, as in uv. Each member is a root + # and is expanded once. + packages = [package(n) for n in ("member-a", "member-b", "member-c", "crate-a", "crate-c")] + deps = { + "member-a": [dep("member-b"), dep("member-a", "dev"), dep("crate-a")], + "member-b": [dep("member-c"), dep("member-a", "dev")], + "member-c": [dep("member-a"), dep("member-c", None, "dev"), dep("crate-c", "dev")], + } + self.assertEqual(self.get_scopes(["member-a", "member-b", "member-c"], packages, deps), { + "crate-a": "runtime", + "crate-c": "test", + }) + + def test_dependency_scopes(self): + # Only crate downloads get a scope. A crate that is not in the graph + # for the target gets None. + packages = [package(n) for n in ("app", "used")] + metadata = { + "packages": packages, + "workspace_members": ["app"], + "resolve": {"nodes": [{"id": "app", "deps": [dep("used")]}, {"id": "used", "deps": []}]}, + } + identities = [ + bb.fetch.PackageIdentity("used", "1.0.0", "pkg:cargo/used@1.0.0"), + bb.fetch.PackageIdentity("other-os", "1.0.0", + "pkg:cargo/other-os@1.0.0?repository_url=https%3A%2F%2Fcrates.example.com"), + bb.fetch.PackageIdentity("used", "1.0.0", "pkg:npm/used@1.0.0"), + ] + with tempfile.TemporaryDirectory() as tmpdir: + path = os.path.join(tmpdir, "cargo-metadata.json") + with open(path, "w") as f: + json.dump(metadata, f) + d = bb.data.init() + d.setVar("CARGO_METADATA_FILE", path) + self.assertEqual(oe.rust.cargo_dependency_scopes(d, identities), { + "pkg:cargo/used@1.0.0": "runtime", + "pkg:cargo/other-os@1.0.0?repository_url=https%3A%2F%2Fcrates.example.com": None, + }) + d.setVar("CARGO_METADATA_FILE", os.path.join(tmpdir, "missing.json")) + self.assertEqual(oe.rust.cargo_dependency_scopes(d, identities), {}) From patchwork Wed Nov 25 18:36:12 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Tondo X-Patchwork-Id: 105712 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by mx.groups.io with SMTP id smtpd.msgproc02-g2.48619.1795631781800988460 for ; Wed, 25 Nov 2026 10:36:22 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=BfHqjR9H; spf=pass (domain: gmail.com, ip: 209.85.128.45, mailfrom: stondo@gmail.com) Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4bb64ef5a00so49476665e9.3 for ; Wed, 25 Nov 2026 10:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795631780; x=1796236580; darn=lists.openembedded.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/FleaBFvWPlvaB4AMIla7Lw9JilLoGWrC6AZTqDKtaQ=; b=BfHqjR9H/qRRGJV99CX7GCg/252wR0HMontkZM0+E5p7+cJNSmuAe5T+wEkDmB+7PT g+IMjC1/WtgmB/W9PjyD985ydtVy9VmhYwBTBXoxkMg0wV+dXjvYVuS1sYDlwI4c3r3b AuToBkt2G+vZn7I545JmdyKNyH+HWbOPMDbojV7ytf19k/eCcR9x3gnYgTNudiOtEopC jtQE20nQLjkNt15M3lEGX4HqTvlWzLp+jgUwmsPf8BWoUUiDaBquWexv26R+btOv0uzo AZSR3mBuP+Ik8lE4P+uksViJZLsv5C3NMve+ToNQUl1jMDFsRcrWInhxSSNrpBfwM4LI OCXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795631780; x=1796236580; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/FleaBFvWPlvaB4AMIla7Lw9JilLoGWrC6AZTqDKtaQ=; b=2KUu6ShlOWU/RWJnF4q3vtOaoIAIDzU5GlrWYDzFPGlr0VGfgYmFkWm3iS+SCng9j7 ksbKyRY6SUrh6UsjT5WBB1V5VMZ5qZcQqiLJckHE5tBRQ3x32Vc63IddkigPErK8MkLO mkAMGy8egFq8nex3Tlf7kkA9IeFku071k0vlF82V2nzDuzEqYaJ3QaCMrjS5v4es9CyC lw8xmwRw6g+ndhreQOi+eOMtbYWKdlwYW+qDntHTC3xDsPFiLV7KmVQVe2tGQ+noMDGF nwzcxw6W/9XIeArRjky+ocJrWNZ0Q4Cm1Tsk756/1BqEwWdnfJW1/KkF/1rJK8ZeRMOI ZMFw== X-Gm-Message-State: APuU4Z8lH1MCdemKBVeH+wTKB1Y7ZeFg6ZBj1wSAy7y5TMyo63zbaXig pXWQ8GvhbKG0bR/auXuNSc0RYNKe0m4CxxJVj9V1e8i2az3OtY+snmNtCa0op5S3 X-Gm-Gg: AZFdTZc0GjAH2pEaQVFgmta6ontxqAZAVBUtAC12/tom7xC9VOxDRGgkZm72EoHGXgI pE6jKlLiFYQuFg53qkxIWVbgNOTl6ZjEY/nSAi84OKQ8MzhEOx60eME210Ui3xuxQ301L66094+ VB2D2S423FXfzbT4sQFJ/egUlJFfmsXaFbsXdPgC+HlLUl+7Pwi3os6XL6SsEfdtu8vfuuywO2f icJV+/f02Lh0I+4u2nhu8jPA5iOnHc1hpoUteWdoWO6SgmnjtmWaxHpePQRlfaUKV7GOC02+tE4 Qis9r3QVYTp9KikuHNjJOcSiMg+kEtUUi5xynE+gBpxDJ6373zG693/WsRKzpMGh+UqrDug/lwX Gl753O00BLZhBOaVEhT/R+cHPHM4FH/fxqcNKdQeszpeDwhsibBuqC1c9FwwhbMBHmvBatvpLFL 6LmPX8FCPJBA/k92JRJOE4A9MQtX9AQhGPVzWZryuz1ce06O0vMYUnlJTpd/1H6xrND3slepOh5 UoPkn55k6yJg0/jh0OdaNTFZes/2KFp6IMoGbTV4su8EoT0wNtm3jPLAJBTXXLK64XeMiISz8m4 zGC9n0Jo3Tk= X-Received: by 2002:a05:600c:154f:b0:4bb:6842:66ef with SMTP id 5b1f17b1804b1-4bb8893a963mr364279565e9.30.1795631779770; Wed, 25 Nov 2026 10:36:19 -0800 (PST) Return-Path: Received: from fedora ([165.225.94.251]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2c8csm36941016f8f.2.2026.11.25.10.36.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 10:36:19 -0800 (PST) From: Stefano Tondo To: openembedded-core@lists.openembedded.org Cc: Joshua Watt , Alexander Kanavin , Stefan Herbrechtsmeier , Paul Barker , Mathieu Dubois-Briand , Peter Marko , stefano.tondo.ext@siemens.com Subject: [PATCH v4 3/4] spdx30: Add the PURLs of the source downloads to the output packages Date: Wed, 25 Nov 2026 19:36:12 +0100 Message-ID: <20261125183613.1335718-4-stondo@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125183613.1335718-1-stondo@gmail.com> References: <20261125183613.1335718-1-stondo@gmail.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Wed, 25 Nov 2026 18:36:24 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/openembedded-core/message/251239 From: Stefano Tondo Statically linked code, for example a Rust crate, is part of the binary, but the SPDX of the output package only names the recipe. A vulnerability scanner that checks the packages of an image does not find the dependency. Add the PURLs of the source downloads to the external identifiers of each output package. Leave out the downloads that a function in SPDX_DEPENDENCY_SCOPE_FUNCS shows are not built into the target, for example crates that only run at build time. The software_packageUrl of the package stays the first PURL in SPDX_PACKAGE_URLS. Signed-off-by: Stefano Tondo --- meta/lib/oe/spdx30_tasks.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/meta/lib/oe/spdx30_tasks.py b/meta/lib/oe/spdx30_tasks.py index 588be68e58..d613b4dad1 100644 --- a/meta/lib/oe/spdx30_tasks.py +++ b/meta/lib/oe/spdx30_tasks.py @@ -482,6 +482,9 @@ def add_dependency_scopes(d, objset, build, source_files): The build gets a scoped dependsOn relationship to each download with a scope. A download that is fetched but not used on the target gets an annotation instead. + + Returns the downloads that are not built into the target: downloads + that are not used on it, and downloads with a scope other than runtime. """ downloads = {} for dl in source_files: @@ -500,10 +503,13 @@ def add_dependency_scopes(d, objset, build, source_files): ) by_scope = {} + not_in_target = set() for purl, scope in scopes.items(): if scope is not None and scope not in oe.spdx30.LifecycleScopeType.NAMED_INDIVIDUALS: bb.fatal("SPDX_DEPENDENCY_SCOPE_FUNCS: %s is not a lifecycle scope" % scope) for dl in downloads.get(purl, []): + if scope != "runtime": + not_in_target.add(dl) if scope is None: objset.new_annotation( dl, @@ -522,6 +528,8 @@ def add_dependency_scopes(d, objset, build, source_files): sorted(oe.sbom30.get_element_link_id(dl) for dl in dls), ) + return not_in_target + def add_download_files(d, objset): inputs = set() @@ -1025,7 +1033,18 @@ def create_spdx(d): source_files = add_download_files(d, build_objset) build_inputs |= source_files - add_dependency_scopes(d, build_objset, build, source_files) + + # The output packages inherit the PURLs of the downloads they are built + # from, as the code of statically linked dependencies is in them. Only + # the downloads known not to be built into the target are left out. + not_in_target = add_dependency_scopes(d, build_objset, build, source_files) + dependency_purls = sorted( + { + dl.software_packageUrl + for dl in source_files + if getattr(dl, "software_packageUrl", None) and dl not in not_in_target + } + ) recipe_spdx_license = add_license_expression( d, build_objset, d.getVar("LICENSE"), license_data, [recipe_objset] @@ -1138,7 +1157,7 @@ def create_spdx(d): d.getVar("SPDX_PACKAGE_URLS:%s" % package) or d.getVar("SPDX_PACKAGE_URLS") or "" - ).split() + ).split() + dependency_purls set_purls(spdx_package, purls) From patchwork Wed Nov 25 18:36:13 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Tondo X-Patchwork-Id: 105713 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by mx.groups.io with SMTP id smtpd.msgproc02-g2.48622.1795631783530302786 for ; Wed, 25 Nov 2026 10:36:23 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=N6Tevl2l; spf=pass (domain: gmail.com, ip: 209.85.221.47, mailfrom: stondo@gmail.com) Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-490c3ae81d3so3855395f8f.2 for ; Wed, 25 Nov 2026 10:36:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795631782; x=1796236582; darn=lists.openembedded.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=W/q7W2M3bmR1CipvfJKpZ3xCzB6Tq8+NWRg0UZIqGcE=; b=N6Tevl2lL9q0WL3paiEDnJz128gV+rCUo6pSgoVWq2zrO3noWX8rGvoP5v7/ZQrB9B vpDT6LmtVbgNr/te6jyCQLWC+Sjs0cl39ExqSYQADedn6tm+0u2rQU+9siwN4K6Ma1T2 oweaeAYOUiQe4qzVfEmfcFbwuJUBlt9Vg3GyAvC0IhbCIVbrfJJfDPpb54slNVKUjzso +kw1BPs72pLE6hfxRpoIFnaFiZcIVAMJLiLiYRxWzIVXlOkWB7185GA/XIdf9z4CF6Vs Fwl96dT/WFNb3Vzkvr46TDaaACahGschRcgK+YqmMOyF3sRif1YuoA2hqvadVDQ820s/ Ve6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795631782; x=1796236582; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=W/q7W2M3bmR1CipvfJKpZ3xCzB6Tq8+NWRg0UZIqGcE=; b=rr5Woyc7XIUXMonOq2YD6+dxjZ8VbH7BexrVPDf8YOd8SnbClK7chG0TmKdTqMs5uS 2vEC1kLTOO97f/De9z+vPx+4KJlKV4nJng5NlNZS6ydIdOjFUChkO/7QsCr+Wt+NbTz5 jZzQRa2Q59mJ2ygEyd6z2a3p2rMl/t8yylmuNBJxo8o2mVTkg+SdmHoVlz9gSCDT+35p wZ6WtnwpibERpSE/MDwUib8/aAsDxHMQh7Rrn+NAtV55g3o54Gq7N+yteVFutqYecOFE nVbAIusihuuiVmzDt7p8Pl7fThodo7g5pjrL6F9d2qAC/8qzMB5bDJE6iuHskuyR7RwA Cp4g== X-Gm-Message-State: APuU4Z8l2msjIRI6BpGaBZHXjigk5ZzIzbxb76HcZzbOVkJPraoDzlJx Hn8FuqSFILAv6RDMfmCrtmW/assDm06Vhk28l2TbEy2HPRFjkMaKajx+f/VPz8dr X-Gm-Gg: AZFdTZe86AeAuUNETjXNrHESjcQ1/18TAtJ5V18ekW2Ll5rdNuzC8r0l2I+j1ni2aWe TQvimDjR1THVtWb3x4nv8dgPWWjIRMIQhhuw+a42oeEWFa80hyJqH/D6D87+VC00ft3ZDQPuZHk x6TJPmBK/pmVxDVpPW1HtMBiabmxuYIbgDfL9TF1+7aK3tQyCs7bBG8cuxVvdfQZBjfbB/BIYsn jEhKTzlNJpHsK1pNnnA96VwDsjBP10QqOO8yfkGmBhn2zBCQPXtVknC6wqlPZvXJRwL66zVwMew DuJHvqXSuweBSBCxXneMVBQ64kXx6uGUphvf0XaIE7hACWIhfSsOQ+r5sp3Mw5bNL42OOAWOvCJ c7o6pflg3z+Ji7cGb/CHHa2k9cfdf5ITkMNuoNM0FDQ2mTZGXAkAigxO3qZNriEV+7nmqeiUONi EoMAXNA3UnSKMMBkh+Eatnnn/oSPaLe9ZQzTnaMv7KZLBQMbi0Hv/z+PN3e85e/BLDcByokYHOX 8DXZtR9DCZYoLQbFweQDEMWWWRVyEI69m/9Fz8tpKQQzafy/mYon6FlKFLha6866jlluW55tdg1 0mWa2qFl4yDqOBHFqlQC5oYY++pn9gjO X-Received: by 2002:a05:6000:47d4:b0:493:8c32:94 with SMTP id ffacd0b85a97d-4abb400ae31mr24170552f8f.38.1795631781623; Wed, 25 Nov 2026 10:36:21 -0800 (PST) Return-Path: Received: from fedora ([165.225.94.251]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2c8csm36941016f8f.2.2026.11.25.10.36.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 10:36:20 -0800 (PST) From: Stefano Tondo To: openembedded-core@lists.openembedded.org Cc: Joshua Watt , Alexander Kanavin , Stefan Herbrechtsmeier , Paul Barker , Mathieu Dubois-Briand , Peter Marko , stefano.tondo.ext@siemens.com Subject: [PATCH v4 4/4] oeqa/selftest: spdx: Test crate identity, scope and output PURLs Date: Wed, 25 Nov 2026 19:36:13 +0100 Message-ID: <20261125183613.1335718-5-stondo@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125183613.1335718-1-stondo@gmail.com> References: <20261125183613.1335718-1-stondo@gmail.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Wed, 25 Nov 2026 18:36:24 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/openembedded-core/message/251240 From: Stefano Tondo python3-bcrypt fetches 42 crates. The new tests check three things: - Each crate download has its name, version and PURL. - Each crate download has a lifecycle scope, or an annotation that it is not built. - The output package gets the PURLs of the runtime crates only. A fourth test checks rpm-sequoia, which builds with the feature crypto-openssl instead of its default features: the download of openssl-sys must have the runtime scope. The tests name only crates that do not change on recipe upgrades. blowfish is the cipher of bcrypt. Only the pyo3 proc-macros use syn, at build time. openssl-sys is the OpenSSL binding of the crypto-openssl backend. The other checks do not use crate names. Signed-off-by: Stefano Tondo --- meta/lib/oeqa/selftest/cases/spdx.py | 151 +++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/meta/lib/oeqa/selftest/cases/spdx.py b/meta/lib/oeqa/selftest/cases/spdx.py index ce9a8af4f6..4768081ec5 100644 --- a/meta/lib/oeqa/selftest/cases/spdx.py +++ b/meta/lib/oeqa/selftest/cases/spdx.py @@ -9,6 +9,7 @@ import hashlib from datetime import datetime, timezone from oeqa.selftest.case import OESelftestTestCase from oeqa.utils.commands import bitbake, get_bb_var, get_bb_vars +import oe.purl import oe.spdx30 @@ -543,3 +544,153 @@ class SPDX30Check(SPDX3CheckBase, OESelftestTestCase): # Document should be fully linked self.check_objset_missing_ids(objset) + + def test_crate_package_identity(self): + """Test that crate downloads carry name, version and a cargo PURL.""" + objset = self.check_recipe_spdx( + "python3-bcrypt", + "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/builds/build-python3-bcrypt.spdx.json", + ) + + prefix = "https://static.crates.io/crates/" + crates = 0 + for pkg in objset.foreach_type(oe.spdx30.software_Package): + location = pkg.software_downloadLocation or "" + if not location.startswith(prefix): + continue + name, version, _ = location[len(prefix):].split("/") + self.assertEqual(pkg.name, name) + self.assertEqual(pkg.software_packageVersion, version) + self.assertEqual( + pkg.software_packageUrl, + f"pkg:cargo/{oe.purl.quote(name)}@{oe.purl.quote(version)}", + ) + crates += 1 + + self.assertGreater(crates, 0, "no crate downloads found") + + def test_crate_dependency_scope(self): + """Test that crate downloads get a lifecycle scope for the target. + + Every crate download is either the target of one scoped dependsOn + relationship or annotated as not built. python3-bcrypt fetches crates + for other platforms (WebAssembly, UEFI), which are never built for a + Linux target. blowfish is the cipher of bcrypt, and syn only runs at + build time in proc-macros, so these names do not change on upgrades. + """ + objset = self.check_recipe_spdx( + "python3-bcrypt", + "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/builds/build-python3-bcrypt.spdx.json", + ) + + crates = { + pkg._id: pkg.name + for pkg in objset.foreach_type(oe.spdx30.software_Package) + if (pkg.software_packageUrl or "").startswith("pkg:cargo/") + and (pkg.software_downloadLocation or "").startswith("https://static.crates.io/") + } + + scope_of = {} + for rel in objset.foreach_type(oe.spdx30.LifecycleScopedRelationship): + if rel.relationshipType != oe.spdx30.RelationshipType.dependsOn: + continue + for to in rel.to: + to_id = to if isinstance(to, str) else to._id + if to_id in crates: + self.assertNotIn(crates[to_id], scope_of) + scope_of[crates[to_id]] = rel.scope + + not_built = set() + for annotation in objset.foreach_type(oe.spdx30.Annotation): + subject = annotation.subject + subject_id = subject if isinstance(subject, str) else subject._id + if subject_id in crates and "not built" in annotation.statement: + not_built.add(crates[subject_id]) + + self.assertEqual(set(scope_of) | not_built, set(crates.values())) + self.assertFalse(set(scope_of) & not_built) + self.assertTrue(not_built, "no crate for another platform found") + self.assertEqual(scope_of["syn"], oe.spdx30.LifecycleScopeType.build) + self.assertEqual(scope_of["blowfish"], oe.spdx30.LifecycleScopeType.runtime) + + def test_crate_scope_with_recipe_features(self): + """Test that a crate of a feature that the recipe enables is runtime. + + rpm-sequoia builds with --no-default-features --features + crypto-openssl, so its library uses openssl-sys. The default + features of rpm-sequoia select another backend. The crate graph has + all features, so openssl-sys gets the runtime scope. + """ + objset = self.check_recipe_spdx( + "rpm-sequoia", + "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/builds/build-rpm-sequoia.spdx.json", + ) + + openssl_sys = { + pkg._id + for pkg in objset.foreach_type(oe.spdx30.software_Package) + if (pkg.software_packageUrl or "").startswith("pkg:cargo/openssl-sys@") + } + self.assertTrue(openssl_sys, "no download of openssl-sys found") + + runtime = set() + for rel in objset.foreach_type(oe.spdx30.LifecycleScopedRelationship): + if ( + rel.relationshipType == oe.spdx30.RelationshipType.dependsOn + and rel.scope == oe.spdx30.LifecycleScopeType.runtime + ): + runtime |= {to if isinstance(to, str) else to._id for to in rel.to} + self.assertTrue(openssl_sys <= runtime, "openssl-sys does not have the runtime scope") + + def test_crate_purls_in_output_package(self): + """Test that the output package inherits the PURLs of its crates. + + The package gets the PURLs of the crates with the runtime scope in + the build SPDX, and no other crate PURLs. syn only runs at build + time in proc-macros, so it is not in the binary. + """ + objset = self.check_recipe_spdx( + "python3-bcrypt", + "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/packages/package-python3-bcrypt.spdx.json", + ) + + package = next( + pkg + for pkg in objset.foreach_type(oe.spdx30.software_Package) + if pkg.name == "python3-bcrypt" + ) + purls = { + e.identifier + for e in package.externalIdentifier + if e.externalIdentifierType == oe.spdx30.ExternalIdentifierType.packageUrl + } + crates = {p[len("pkg:cargo/"):].split("@")[0] for p in purls if p.startswith("pkg:cargo/")} + + # The package's own PURL stays first + own_purls = get_bb_var("SPDX_PACKAGE_URLS", "python3-bcrypt").split() + self.assertEqual(package.software_packageUrl, own_purls[0]) + self.assertTrue(set(own_purls) <= purls) + self.assertIn("blowfish", crates) + self.assertNotIn("syn", crates) + + spdx_vars = get_bb_vars(["DEPLOY_DIR_SPDX", "SSTATE_PKGARCH"], "python3-bcrypt") + build_objset = self.check_spdx_file( + "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/builds/build-python3-bcrypt.spdx.json".format(**spdx_vars) + ) + downloads = { + pkg._id: pkg.software_packageUrl + for pkg in build_objset.foreach_type(oe.spdx30.software_Package) + if (pkg.software_packageUrl or "").startswith("pkg:cargo/") + } + runtime = set() + for rel in build_objset.foreach_type(oe.spdx30.LifecycleScopedRelationship): + if rel.scope != oe.spdx30.LifecycleScopeType.runtime: + continue + for to in rel.to: + to_id = to if isinstance(to, str) else to._id + if to_id in downloads: + runtime.add(downloads[to_id]) + + self.assertTrue(runtime, "no crate with the runtime scope found") + cargo_purls = {p for p in purls if p.startswith("pkg:cargo/")} + self.assertEqual(cargo_purls - set(own_purls), runtime)