Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 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 | |
| 16 | addtask do_update_crates after do_patch |
| 17 | do_update_crates[depends] = "python3-native:do_populate_sysroot" |
| 18 | |
| 19 | # The directory where to search for Cargo.lock files |
| 20 | CARGO_LOCK_SRC_DIR ??= "${S}" |
| 21 | |
| 22 | do_update_crates() { |
| 23 | nativepython3 - <<EOF |
| 24 | |
| 25 | def 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 | |
| 36 | import os |
| 37 | crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n" |
| 38 | for 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)) |
| 42 | open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates) |
| 43 | |
| 44 | EOF |
| 45 | } |