diff mbox series

[v5,2/4] classes/cargo-update-recipe-crates: support v1 Cargo.lock format

Message ID 20261201-mfe-multi-cargo-lock-files-v5-2-6c4eae24ec27@pengutronix.de
State New
Headers show
Series Add support for multi Cargo.lock files | expand

Commit Message

Marco Felsch Dec. 1, 2026, 4:21 p.m. UTC
Cargo.lock files in the old v1 format don't store a checksum per
[[package]] but collect them in the [metadata] table as
"checksum <name> <version> (<source>)" = "<sha256>". do_update_crates
only read the per-package field, so it emitted crate:// entries without
SRC_URI[...sha256sum] for such lockfiles. Fall back to the [metadata]
checksums and skip "<none>" entries used for e.g. git sources.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 .../cargo-update-recipe-crates.bbclass             | 32 ++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/cargo-update-recipe-crates.bbclass b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
index d4318bc9f5749503cb7995927e2e54b9771a6b75..9f92f0a522979fde8f0b88abdefb9a03c0c651ee 100644
--- a/meta/classes-recipe/cargo-update-recipe-crates.bbclass
+++ b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
@@ -9,6 +9,10 @@ 
 ## This class is used to update the list of crates in SRC_URI
 ## by reading Cargo.lock in the source tree.
 ##
+## Both the current Cargo.lock format (checksum stored per [[package]])
+## and the old v1 format (checksums stored in the [metadata] table as
+## "checksum <name> <version> (<source>)" checksum-val) are supported.
+##
 ## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
 ##
 ## To perform the update: bitbake -c update_crates recipe-name
@@ -25,6 +29,25 @@  CARGO_LOCK_SRC_DIR ??= "${S}"
 python do_update_crates() {
     cargo_lock_src_dir = d.getVar("CARGO_LOCK_SRC_DIR")
 
+    def get_legacy_checksums(crates, filename):
+        # Old (v1) Cargo.lock files do not carry a 'checksum' field per
+        # [[package]]; instead they are collected in the [metadata] table:
+        #   "checksum <name> <version> (<source>)" = "<sha256>"
+        # Packages without a checksum (e.g. git sources) use "<none>".
+        checksums = {}
+        for key, value in crates.get('metadata', {}).items():
+            if not key.startswith('checksum '):
+                continue
+            parts = key.split(' ', 3)
+            if len(parts) != 4 or not (parts[3].startswith('(') and parts[3].endswith(')')):
+                bb.warn("Unable to parse metadata entry '%s' in %s" % (key, filename))
+                continue
+            name, version, source = parts[1], parts[2], parts[3][1:-1]
+            if value == '<none>':
+                continue
+            checksums[(name, version, source)] = value
+        return checksums
+
     def get_crates(filename):
         try:
             import tomllib
@@ -36,6 +59,8 @@  python do_update_crates() {
         with open(filename, 'rb') as fp:
             crates = tomllib.load(fp)
 
+        legacy_checksums = get_legacy_checksums(crates, filename)
+
         # Build a list with crates info that have crates.io in the source
         crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package']))
 
@@ -48,9 +73,12 @@  python do_update_crates() {
         cksum_list = ''
         for c in crates_candidates:
             rename = "%s-%s" % (c['name'], c['version'])
+            checksum = c.get('checksum')
+            if checksum is None:
+                checksum = legacy_checksums.get((c['name'], c['version'], c['source']))
             c_list += '\n    crate://crates.io/%s/%s \\' % (c['name'], c['version'])
-            if 'checksum' in c:
-                cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum'])
+            if checksum is not None:
+                cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, checksum)
 
         c_list += '\n"\n'
         c_list += cksum_list