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" |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 18 | do_update_crates[nostamp] = "1" |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 19 | |
| 20 | # The directory where to search for Cargo.lock files |
| 21 | CARGO_LOCK_SRC_DIR ??= "${S}" |
| 22 | |
| 23 | do_update_crates() { |
| 24 | nativepython3 - <<EOF |
| 25 | |
| 26 | def get_crates(f): |
| 27 | import tomllib |
| 28 | c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}') |
| 29 | c_list += '\nSRC_URI += " \\\' |
| 30 | crates = tomllib.load(open(f, 'rb')) |
| 31 | for c in crates['package']: |
| 32 | if 'source' in c and 'crates.io' in c['source']: |
| 33 | c_list += '\n crate://crates.io/%s/%s \\\' % (c['name'], c['version']) |
| 34 | c_list += '\n"\n' |
| 35 | return c_list |
| 36 | |
| 37 | import os |
| 38 | crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n" |
| 39 | for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'): |
| 40 | for file in files: |
| 41 | if file == 'Cargo.lock': |
| 42 | crates += get_crates(os.path.join(root, file)) |
| 43 | open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates) |
| 44 | |
| 45 | EOF |
| 46 | } |