1#
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"
18do_update_crates[nostamp] = "1"
19
20# The directory where to search for Cargo.lock files
21CARGO_LOCK_SRC_DIR ??= "${S}"
22
23do_update_crates() {
24    nativepython3 - <<EOF
25
26def get_crates(f):
27    import tomllib
28    c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
29    c_list += '\nSRC_URI += " \\\'
30    crates = tomllib.load(open(f, 'rb'))
31    for c in crates['package']:
32        if 'source' in c and 'crates.io' in c['source']:
33            c_list += '\n    crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
34    c_list += '\n"\n'
35    return c_list
36
37import os
38crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
39for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
40    for file in files:
41        if file == 'Cargo.lock':
42            crates += get_crates(os.path.join(root, file))
43open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates)
44
45EOF
46}
47