12390b1b6SPatrick Williams#
22390b1b6SPatrick Williams# Copyright OpenEmbedded Contributors
32390b1b6SPatrick Williams#
42390b1b6SPatrick Williams# SPDX-License-Identifier: MIT
52390b1b6SPatrick Williams#
62390b1b6SPatrick Williams
72390b1b6SPatrick Williams##
82390b1b6SPatrick Williams## Purpose:
92390b1b6SPatrick Williams## This class is used to update the list of crates in SRC_URI
102390b1b6SPatrick Williams## by reading Cargo.lock in the source tree.
112390b1b6SPatrick Williams##
122390b1b6SPatrick Williams## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
132390b1b6SPatrick Williams##
142390b1b6SPatrick Williams## To perform the update: bitbake -c update_crates recipe-name
152390b1b6SPatrick Williams
162390b1b6SPatrick Williamsaddtask do_update_crates after do_patch
172390b1b6SPatrick Williamsdo_update_crates[depends] = "python3-native:do_populate_sysroot"
18*c5535c91SAndrew Geisslerdo_update_crates[nostamp] = "1"
192390b1b6SPatrick Williams
202390b1b6SPatrick Williams# The directory where to search for Cargo.lock files
212390b1b6SPatrick WilliamsCARGO_LOCK_SRC_DIR ??= "${S}"
222390b1b6SPatrick Williams
232390b1b6SPatrick Williamsdo_update_crates() {
242390b1b6SPatrick Williams    nativepython3 - <<EOF
252390b1b6SPatrick Williams
262390b1b6SPatrick Williamsdef get_crates(f):
272390b1b6SPatrick Williams    import tomllib
282390b1b6SPatrick Williams    c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
292390b1b6SPatrick Williams    c_list += '\nSRC_URI += " \\\'
302390b1b6SPatrick Williams    crates = tomllib.load(open(f, 'rb'))
312390b1b6SPatrick Williams    for c in crates['package']:
322390b1b6SPatrick Williams        if 'source' in c and 'crates.io' in c['source']:
332390b1b6SPatrick Williams            c_list += '\n    crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
342390b1b6SPatrick Williams    c_list += '\n"\n'
352390b1b6SPatrick Williams    return c_list
362390b1b6SPatrick Williams
372390b1b6SPatrick Williamsimport os
382390b1b6SPatrick Williamscrates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
392390b1b6SPatrick Williamsfor root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
402390b1b6SPatrick Williams    for file in files:
412390b1b6SPatrick Williams        if file == 'Cargo.lock':
422390b1b6SPatrick Williams            crates += get_crates(os.path.join(root, file))
432390b1b6SPatrick Williamsopen(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates)
442390b1b6SPatrick Williams
452390b1b6SPatrick WilliamsEOF
462390b1b6SPatrick Williams}
47