diff options
-rw-r--r-- | meta/classes-recipe/cargo-update-recipe-crates.bbclass | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/meta/classes-recipe/cargo-update-recipe-crates.bbclass b/meta/classes-recipe/cargo-update-recipe-crates.bbclass index 697460d215..daa363b0dd 100644 --- a/meta/classes-recipe/cargo-update-recipe-crates.bbclass +++ b/meta/classes-recipe/cargo-update-recipe-crates.bbclass | |||
@@ -16,11 +16,14 @@ | |||
16 | addtask do_update_crates after do_patch | 16 | addtask do_update_crates after do_patch |
17 | do_update_crates[depends] = "python3-native:do_populate_sysroot" | 17 | do_update_crates[depends] = "python3-native:do_populate_sysroot" |
18 | do_update_crates[nostamp] = "1" | 18 | do_update_crates[nostamp] = "1" |
19 | do_update_crates[doc] = "Update the recipe by reading Cargo.lock and write in ${THISDIR}/${BPN}-crates.inc" | ||
19 | 20 | ||
20 | # The directory where to search for Cargo.lock files | 21 | # The directory where to search for Cargo.lock files |
21 | CARGO_LOCK_SRC_DIR ??= "${S}" | 22 | CARGO_LOCK_SRC_DIR ??= "${S}" |
22 | 23 | ||
23 | do_update_crates() { | 24 | do_update_crates() { |
25 | TARGET_FILE="${THISDIR}/${BPN}-crates.inc" | ||
26 | |||
24 | nativepython3 - <<EOF | 27 | nativepython3 - <<EOF |
25 | 28 | ||
26 | def get_crates(f): | 29 | def get_crates(f): |
@@ -28,19 +31,52 @@ def get_crates(f): | |||
28 | c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}') | 31 | c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}') |
29 | c_list += '\nSRC_URI += " \\\' | 32 | c_list += '\nSRC_URI += " \\\' |
30 | crates = tomllib.load(open(f, 'rb')) | 33 | crates = tomllib.load(open(f, 'rb')) |
31 | for c in crates['package']: | 34 | |
32 | if 'source' in c and 'crates.io' in c['source']: | 35 | # Build a list with crates info that have crates.io in the source |
36 | crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package'])) | ||
37 | |||
38 | if not crates_candidates: | ||
39 | raise ValueError("Unable to find any candidate crates that use crates.io") | ||
40 | |||
41 | # Build a list of crates name that have multiple version | ||
42 | crates_multiple_vers = [] | ||
43 | tmp = [] | ||
44 | for c in crates_candidates: | ||
45 | if c['name'] in tmp: | ||
46 | crates_multiple_vers.append(c['name']) | ||
47 | else: | ||
48 | tmp.append(c['name']) | ||
49 | |||
50 | # Update crates uri and their checksum, to avoid name clashing on the checksum | ||
51 | # we need to rename crates of the same name but different version | ||
52 | cksum_list = '' | ||
53 | for c in crates_candidates: | ||
54 | if c['name'] in crates_multiple_vers: | ||
55 | rename = "%s-%s" % (c['name'], c['version']) | ||
56 | c_list += '\n crate://crates.io/%s/%s;name=%s \\\' % (c['name'], c['version'], rename) | ||
57 | else: | ||
58 | rename = c['name'] | ||
33 | c_list += '\n crate://crates.io/%s/%s \\\' % (c['name'], c['version']) | 59 | c_list += '\n crate://crates.io/%s/%s \\\' % (c['name'], c['version']) |
60 | if 'checksum' in c: | ||
61 | cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum']) | ||
62 | |||
34 | c_list += '\n"\n' | 63 | c_list += '\n"\n' |
64 | c_list += cksum_list | ||
65 | c_list += '\n' | ||
35 | return c_list | 66 | return c_list |
36 | 67 | ||
37 | import os | 68 | import os |
38 | crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n" | 69 | crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n" |
70 | found = False | ||
39 | for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'): | 71 | for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'): |
40 | for file in files: | 72 | for file in files: |
41 | if file == 'Cargo.lock': | 73 | if file == 'Cargo.lock': |
42 | crates += get_crates(os.path.join(root, file)) | 74 | crates += get_crates(os.path.join(root, file)) |
43 | open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates) | 75 | found = True |
44 | 76 | if not found: | |
77 | raise ValueError("Unable to find Cargo.lock in ${CARGO_LOCK_SRC_DIR}") | ||
78 | open("${TARGET_FILE}", 'w').write(crates) | ||
45 | EOF | 79 | EOF |
80 | |||
81 | bbnote "Successfully update crates inside '${TARGET_FILE}'" | ||
46 | } | 82 | } |