1*2390b1b6SPatrick Williams#
2*2390b1b6SPatrick Williams# Copyright OpenEmbedded Contributors
3*2390b1b6SPatrick Williams#
4*2390b1b6SPatrick Williams# SPDX-License-Identifier: MIT
5*2390b1b6SPatrick Williams#
6*2390b1b6SPatrick Williams
7*2390b1b6SPatrick Williams##
8*2390b1b6SPatrick Williams## Purpose:
9*2390b1b6SPatrick Williams## This class is used to update the list of crates in SRC_URI
10*2390b1b6SPatrick Williams## by reading Cargo.lock in the source tree.
11*2390b1b6SPatrick Williams##
12*2390b1b6SPatrick Williams## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
13*2390b1b6SPatrick Williams##
14*2390b1b6SPatrick Williams## To perform the update: bitbake -c update_crates recipe-name
15*2390b1b6SPatrick Williams
16*2390b1b6SPatrick Williamsaddtask do_update_crates after do_patch
17*2390b1b6SPatrick Williamsdo_update_crates[depends] = "python3-native:do_populate_sysroot"
18*2390b1b6SPatrick Williams
19*2390b1b6SPatrick Williams# The directory where to search for Cargo.lock files
20*2390b1b6SPatrick WilliamsCARGO_LOCK_SRC_DIR ??= "${S}"
21*2390b1b6SPatrick Williams
22*2390b1b6SPatrick Williamsdo_update_crates() {
23*2390b1b6SPatrick Williams    nativepython3 - <<EOF
24*2390b1b6SPatrick Williams
25*2390b1b6SPatrick Williamsdef get_crates(f):
26*2390b1b6SPatrick Williams    import tomllib
27*2390b1b6SPatrick Williams    c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
28*2390b1b6SPatrick Williams    c_list += '\nSRC_URI += " \\\'
29*2390b1b6SPatrick Williams    crates = tomllib.load(open(f, 'rb'))
30*2390b1b6SPatrick Williams    for c in crates['package']:
31*2390b1b6SPatrick Williams        if 'source' in c and 'crates.io' in c['source']:
32*2390b1b6SPatrick Williams            c_list += '\n    crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
33*2390b1b6SPatrick Williams    c_list += '\n"\n'
34*2390b1b6SPatrick Williams    return c_list
35*2390b1b6SPatrick Williams
36*2390b1b6SPatrick Williamsimport os
37*2390b1b6SPatrick Williamscrates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
38*2390b1b6SPatrick Williamsfor root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
39*2390b1b6SPatrick Williams    for file in files:
40*2390b1b6SPatrick Williams        if file == 'Cargo.lock':
41*2390b1b6SPatrick Williams            crates += get_crates(os.path.join(root, file))
42*2390b1b6SPatrick Williamsopen(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates)
43*2390b1b6SPatrick Williams
44*2390b1b6SPatrick WilliamsEOF
45*2390b1b6SPatrick Williams}
46