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