@@ -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
@@ -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]