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