From: Stefano Tondo <stondo@gmail.com>
To: openembedded-core@lists.openembedded.org
Cc: Joshua Watt <JPEWhacker@gmail.com>,
Alexander Kanavin <alex.kanavin@gmail.com>,
Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>,
Stefano Tondo <stondo@gmail.com>,
Stefano Tondo <stefano.tondo.ext@siemens.com>
Subject: [PATCH 2/4] spdx30: Record the lifecycle scope of crate dependencies
Date: Wed, 18 Nov 2026 10:29:25 +0100 [thread overview]
Message-ID: <20261118092927.204959-3-stondo@gmail.com> (raw)
In-Reply-To: <20261118092927.204959-1-stondo@gmail.com>
From: Stefano Tondo <stefano.tondo.ext@siemens.com>
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 a do_cargo_metadata task that writes the dependency graph for the
target with cargo metadata --filter-platform. The create_spdx task
reads this graph. It links the build to each crate download with a
scoped dependsOn relationship: runtime, build or test. A crate that is
not used on the target gets an annotation instead.
Signed-off-by: Stefano Tondo <stefano.tondo.ext@siemens.com>
---
meta/classes-recipe/cargo.bbclass | 17 +++++++
meta/lib/oe/spdx30_tasks.py | 81 +++++++++++++++++++++++++++++++
2 files changed, 98 insertions(+)
diff --git a/meta/classes-recipe/cargo.bbclass b/meta/classes-recipe/cargo.bbclass
index 586071988a..b3e82f9d5b 100644
--- a/meta/classes-recipe/cargo.bbclass
+++ b/meta/classes-recipe/cargo.bbclass
@@ -26,6 +26,23 @@ 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.
+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
+ "${CARGO}" metadata --format-version 1 --offline --locked \
+ --manifest-path "${CARGO_MANIFEST_PATH}" \
+ --filter-platform "${RUST_HOST_SYS}" > "${CARGO_METADATA_FILE}"
+}
+addtask cargo_metadata after do_configure before do_compile do_create_spdx
+
cargo_do_install () {
local have_installed=false
for tgt in "${B}/target/${CARGO_TARGET_SUBDIR}/"*; do
diff --git a/meta/lib/oe/spdx30_tasks.py b/meta/lib/oe/spdx30_tasks.py
index 58fe45ec7e..e40716a6df 100644
--- a/meta/lib/oe/spdx30_tasks.py
+++ b/meta/lib/oe/spdx30_tasks.py
@@ -476,6 +476,85 @@ def set_package_identity(d, dl, fd):
dl.software_packageUrl = identity["purl"]
+def get_cargo_scopes(d):
+ """Return {(crate, version): scope} from CARGO_METADATA_FILE.
+
+ The file is the output of cargo metadata --filter-platform for the
+ target, so crates used only on other targets are absent. 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.
+ """
+ path = d.getVar("CARGO_METADATA_FILE")
+ if not path or not os.path.exists(path):
+ return None
+
+ with open(path) as f:
+ metadata = json.load(f)
+
+ 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 = {}
+ stack = [(m, "runtime") for m in members]
+ while stack:
+ pkg_id, scope = stack.pop()
+ if pkg_id not in members:
+ 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 add_dependency_scopes(d, objset, build, source_files):
+ """Give crate downloads a lifecycle scope, see get_cargo_scopes()."""
+ scopes = get_cargo_scopes(d)
+ if scopes is None:
+ return
+
+ by_scope = {}
+ for dl in source_files:
+ if not (getattr(dl, "software_packageUrl", None) or "").startswith("pkg:cargo/"):
+ continue
+ scope = scopes.get((dl.name, dl.software_packageVersion))
+ if scope:
+ by_scope.setdefault(scope, []).append(dl)
+ else:
+ objset.new_annotation(
+ dl,
+ "%s %s is fetched but not built: not used on target %s"
+ % (dl.name, dl.software_packageVersion, d.getVar("RUST_HOST_SYS")),
+ oe.spdx30.AnnotationType.other,
+ )
+
+ 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 +1057,8 @@ def create_spdx(d):
source_files = add_download_files(d, build_objset)
build_inputs |= source_files
+ if bb.data.inherits_class("cargo", d):
+ 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]
--
2.56.1
next prev parent reply other threads:[~2026-11-18 9:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 9:29 [PATCH 0/4] spdx30: identity and purls of dependency downloads Stefano Tondo
2026-11-18 9:29 ` [PATCH 1/4] spdx30: Take name, version and PURL of downloads from the fetcher Stefano Tondo
2026-11-18 9:29 ` Stefano Tondo [this message]
2026-11-18 9:29 ` [PATCH 3/4] spdx30: Add the PURLs of the source downloads to the output packages Stefano Tondo
2026-11-18 9:29 ` [PATCH 4/4] oeqa/selftest: spdx: Test crate identity, scope and output PURLs Stefano Tondo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118092927.204959-3-stondo@gmail.com \
--to=stondo@gmail.com \
--cc=JPEWhacker@gmail.com \
--cc=alex.kanavin@gmail.com \
--cc=openembedded-core@lists.openembedded.org \
--cc=stefan.herbrechtsmeier@weidmueller.com \
--cc=stefano.tondo.ext@siemens.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.