cargo-update-recipe-crates.bbclass 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. ##
  7. ## Purpose:
  8. ## This class is used to update the list of crates in SRC_URI
  9. ## by reading Cargo.lock in the source tree.
  10. ##
  11. ## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
  12. ##
  13. ## To perform the update: bitbake -c update_crates recipe-name
  14. addtask do_update_crates after do_patch
  15. do_update_crates[depends] = "python3-native:do_populate_sysroot"
  16. do_update_crates[nostamp] = "1"
  17. do_update_crates[doc] = "Update the recipe by reading Cargo.lock and write in ${THISDIR}/${BPN}-crates.inc"
  18. # The directory where to search for Cargo.lock files
  19. CARGO_LOCK_SRC_DIR ??= "${S}"
  20. do_update_crates() {
  21. TARGET_FILE="${THISDIR}/${BPN}-crates.inc"
  22. nativepython3 - <<EOF
  23. def get_crates(f):
  24. import tomllib
  25. c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
  26. c_list += '\nSRC_URI += " \\\'
  27. crates = tomllib.load(open(f, 'rb'))
  28. # Build a list with crates info that have crates.io in the source
  29. crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package']))
  30. if not crates_candidates:
  31. raise ValueError("Unable to find any candidate crates that use crates.io")
  32. # Update crates uri and their checksum, to avoid name clashing on the checksum
  33. # we need to rename crates with name and version to have a unique key
  34. cksum_list = ''
  35. for c in crates_candidates:
  36. rename = "%s-%s" % (c['name'], c['version'])
  37. c_list += '\n crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
  38. if 'checksum' in c:
  39. cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum'])
  40. c_list += '\n"\n'
  41. c_list += cksum_list
  42. c_list += '\n'
  43. return c_list
  44. import os
  45. crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
  46. found = False
  47. for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
  48. # ignore git and patches directories
  49. if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.pc')):
  50. continue
  51. if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.git')):
  52. continue
  53. for file in files:
  54. if file == 'Cargo.lock':
  55. try:
  56. cargo_lock_path = os.path.join(root, file)
  57. crates += get_crates(os.path.join(root, file))
  58. except Exception as e:
  59. raise ValueError("Cannot parse '%s'" % cargo_lock_path) from e
  60. else:
  61. found = True
  62. if not found:
  63. raise ValueError("Unable to find any Cargo.lock in ${CARGO_LOCK_SRC_DIR}")
  64. open("${TARGET_FILE}", 'w').write(crates)
  65. EOF
  66. bbnote "Successfully update crates inside '${TARGET_FILE}'"
  67. }