blob: daa363b0dd657d9bc3a82a2773fcda472c6992e1 [file] [log] [blame]
Patrick Williams2390b1b2022-11-03 13:47:49 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7##
8## Purpose:
9## This class is used to update the list of crates in SRC_URI
10## by reading Cargo.lock in the source tree.
11##
12## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
13##
14## To perform the update: bitbake -c update_crates recipe-name
15
16addtask do_update_crates after do_patch
17do_update_crates[depends] = "python3-native:do_populate_sysroot"
Andrew Geisslerc5535c92023-01-27 16:10:19 -060018do_update_crates[nostamp] = "1"
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050019do_update_crates[doc] = "Update the recipe by reading Cargo.lock and write in ${THISDIR}/${BPN}-crates.inc"
Patrick Williams2390b1b2022-11-03 13:47:49 -050020
21# The directory where to search for Cargo.lock files
22CARGO_LOCK_SRC_DIR ??= "${S}"
23
24do_update_crates() {
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050025 TARGET_FILE="${THISDIR}/${BPN}-crates.inc"
26
Patrick Williams2390b1b2022-11-03 13:47:49 -050027 nativepython3 - <<EOF
28
29def get_crates(f):
30 import tomllib
31 c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
32 c_list += '\nSRC_URI += " \\\'
33 crates = tomllib.load(open(f, 'rb'))
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050034
35 # Build a list with crates info that have crates.io in the source
36 crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package']))
37
38 if not crates_candidates:
39 raise ValueError("Unable to find any candidate crates that use crates.io")
40
41 # Build a list of crates name that have multiple version
42 crates_multiple_vers = []
43 tmp = []
44 for c in crates_candidates:
45 if c['name'] in tmp:
46 crates_multiple_vers.append(c['name'])
47 else:
48 tmp.append(c['name'])
49
50 # Update crates uri and their checksum, to avoid name clashing on the checksum
51 # we need to rename crates of the same name but different version
52 cksum_list = ''
53 for c in crates_candidates:
54 if c['name'] in crates_multiple_vers:
55 rename = "%s-%s" % (c['name'], c['version'])
56 c_list += '\n crate://crates.io/%s/%s;name=%s \\\' % (c['name'], c['version'], rename)
57 else:
58 rename = c['name']
Patrick Williams2390b1b2022-11-03 13:47:49 -050059 c_list += '\n crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050060 if 'checksum' in c:
61 cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum'])
62
Patrick Williams2390b1b2022-11-03 13:47:49 -050063 c_list += '\n"\n'
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050064 c_list += cksum_list
65 c_list += '\n'
Patrick Williams2390b1b2022-11-03 13:47:49 -050066 return c_list
67
68import os
69crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050070found = False
Patrick Williams2390b1b2022-11-03 13:47:49 -050071for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
72 for file in files:
73 if file == 'Cargo.lock':
74 crates += get_crates(os.path.join(root, file))
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050075 found = True
76if not found:
77 raise ValueError("Unable to find Cargo.lock in ${CARGO_LOCK_SRC_DIR}")
78open("${TARGET_FILE}", 'w').write(crates)
Patrick Williams2390b1b2022-11-03 13:47:49 -050079EOF
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050080
81 bbnote "Successfully update crates inside '${TARGET_FILE}'"
Patrick Williams2390b1b2022-11-03 13:47:49 -050082}