summaryrefslogtreecommitdiffstats
path: root/meta/classes-recipe/cargo-update-recipe-crates.bbclass
blob: 697460d215d5d337c72f952abe87404dafa923b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

##
## Purpose:
## This class is used to update the list of crates in SRC_URI
## by reading Cargo.lock in the source tree.
##
## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
##
## To perform the update: bitbake -c update_crates recipe-name

addtask do_update_crates after do_patch
do_update_crates[depends] = "python3-native:do_populate_sysroot"
do_update_crates[nostamp] = "1"

# The directory where to search for Cargo.lock files
CARGO_LOCK_SRC_DIR ??= "${S}"

do_update_crates() {
    nativepython3 - <<EOF

def get_crates(f):
    import tomllib
    c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
    c_list += '\nSRC_URI += " \\\'
    crates = tomllib.load(open(f, 'rb'))
    for c in crates['package']:
        if 'source' in c and 'crates.io' in c['source']:
            c_list += '\n    crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
    c_list += '\n"\n'
    return c_list

import os
crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
    for file in files:
        if file == 'Cargo.lock':
            crates += get_crates(os.path.join(root, file))
open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates)

EOF
}