ptest-cargo.bbclass 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. inherit cargo ptest
  2. RUST_TEST_ARGS ??= ""
  3. RUST_TEST_ARGS[doc] = "Arguments to give to the test binaries (e.g. --shuffle)"
  4. # I didn't find a cleaner way to share data between compile and install tasks
  5. CARGO_TEST_BINARIES_FILES ?= "${B}/test_binaries_list"
  6. # Sadly, generated test binaries have no deterministic names (https://github.com/rust-lang/cargo/issues/1924)
  7. # This forces us to parse the cargo output in json format to find those test binaries.
  8. python do_compile_ptest_cargo() {
  9. import subprocess
  10. import json
  11. cargo = bb.utils.which(d.getVar("PATH"), d.getVar("CARGO"))
  12. cargo_build_flags = d.getVar("CARGO_BUILD_FLAGS")
  13. rust_flags = d.getVar("RUSTFLAGS")
  14. manifest_path = d.getVar("CARGO_MANIFEST_PATH")
  15. project_manifest_path = os.path.normpath(manifest_path)
  16. manifest_dir = os.path.dirname(manifest_path)
  17. env = os.environ.copy()
  18. env['RUSTFLAGS'] = rust_flags
  19. cmd = f"{cargo} build --tests --message-format json {cargo_build_flags}"
  20. bb.note(f"Building tests with cargo ({cmd})")
  21. try:
  22. proc = subprocess.Popen(cmd, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
  23. except subprocess.CalledProcessError as e:
  24. bb.fatal(f"Cannot build test with cargo: {e}")
  25. lines = []
  26. for line in proc.stdout:
  27. data = line.strip('\n')
  28. lines.append(data)
  29. bb.note(data)
  30. proc.communicate()
  31. if proc.returncode != 0:
  32. bb.fatal(f"Unable to compile test with cargo, '{cmd}' failed")
  33. # Definition of the format: https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages
  34. test_bins = []
  35. for line in lines:
  36. try:
  37. data = json.loads(line)
  38. except json.JSONDecodeError:
  39. # skip lines that are not a json
  40. pass
  41. else:
  42. try:
  43. # Filter the test packages coming from the current project:
  44. # - test binaries from the root manifest
  45. # - test binaries from sub manifest of the current project if any
  46. current_manifest_path = os.path.normpath(data['manifest_path'])
  47. common_path = os.path.commonpath([current_manifest_path, project_manifest_path])
  48. if common_path in [manifest_dir, current_manifest_path]:
  49. if (data['target']['test'] or data['target']['doctest']) and data['executable']:
  50. test_bins.append(data['executable'])
  51. except (KeyError, ValueError) as e:
  52. # skip lines that do not meet the requirements
  53. pass
  54. # All rust project will generate at least one unit test binary
  55. # It will just run a test suite with 0 tests, if the project didn't define some
  56. # So it is not expected to have an empty list here
  57. if not test_bins:
  58. bb.fatal("Unable to find any test binaries")
  59. cargo_test_binaries_file = d.getVar('CARGO_TEST_BINARIES_FILES')
  60. bb.note(f"Found {len(test_bins)} tests, write their paths into {cargo_test_binaries_file}")
  61. with open(cargo_test_binaries_file, "w") as f:
  62. for test_bin in test_bins:
  63. f.write(f"{test_bin}\n")
  64. }
  65. python do_install_ptest_cargo() {
  66. import shutil
  67. dest_dir = d.getVar("D")
  68. pn = d.getVar("PN")
  69. ptest_path = d.getVar("PTEST_PATH")
  70. cargo_test_binaries_file = d.getVar('CARGO_TEST_BINARIES_FILES')
  71. rust_test_args = d.getVar('RUST_TEST_ARGS') or ""
  72. ptest_dir = os.path.join(dest_dir, ptest_path.lstrip('/'))
  73. os.makedirs(ptest_dir, exist_ok=True)
  74. test_bins = []
  75. with open(cargo_test_binaries_file, "r") as f:
  76. for line in f.readlines():
  77. test_bins.append(line.strip('\n'))
  78. test_paths = []
  79. for test_bin in test_bins:
  80. shutil.copy2(test_bin, ptest_dir)
  81. test_paths.append(os.path.join(ptest_path, os.path.basename(test_bin)))
  82. ptest_script = os.path.join(ptest_dir, "run-ptest")
  83. if os.path.exists(ptest_script):
  84. with open(ptest_script, "a") as f:
  85. f.write(f"\necho \"\"\n")
  86. f.write(f"echo \"## starting to run rust tests ##\"\n")
  87. for test_path in test_paths:
  88. f.write(f"{test_path} {rust_test_args}\n")
  89. else:
  90. with open(ptest_script, "a") as f:
  91. f.write("#!/bin/sh\n")
  92. for test_path in test_paths:
  93. f.write(f"{test_path} {rust_test_args}\n")
  94. os.chmod(ptest_script, 0o755)
  95. # this is chown -R root:root ${D}${PTEST_PATH}
  96. for root, dirs, files in os.walk(ptest_dir):
  97. for d in dirs:
  98. shutil.chown(os.path.join(root, d), "root", "root")
  99. for f in files:
  100. shutil.chown(os.path.join(root, f), "root", "root")
  101. }
  102. do_install_ptest_cargo[dirs] = "${B}"
  103. do_install_ptest_cargo[doc] = "Create or update the run-ptest script with rust test binaries generated"
  104. do_compile_ptest_cargo[dirs] = "${B}"
  105. do_compile_ptest_cargo[doc] = "Generate rust test binaries through cargo"
  106. addtask compile_ptest_cargo after do_compile before do_compile_ptest_base
  107. addtask install_ptest_cargo after do_install_ptest_base before do_package
  108. python () {
  109. if not bb.data.inherits_class('native', d) and not bb.data.inherits_class('cross', d):
  110. d.setVarFlag('do_install_ptest_cargo', 'fakeroot', '1')
  111. d.setVarFlag('do_install_ptest_cargo', 'umask', '022')
  112. # Remove all '*ptest_cargo' tasks when ptest is not enabled
  113. if not(d.getVar('PTEST_ENABLED') == "1"):
  114. for i in ['do_compile_ptest_cargo', 'do_install_ptest_cargo']:
  115. bb.build.deltask(i, d)
  116. }