-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_example12_cpp_native_kernel_bridge.py
More file actions
186 lines (148 loc) · 6.43 KB
/
check_example12_cpp_native_kernel_bridge.py
File metadata and controls
186 lines (148 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
"""Build and test the Example 12 C++ bridge with the LLVM-produced bool kernel."""
from __future__ import annotations
import json
import os
import shutil
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
BUILD_DIR = ROOT / "b" / "cppn12"
@dataclass(frozen=True)
class ConfigureCandidate:
name: str
args: list[str]
def require_tool(name: str) -> None:
if shutil.which(name) is None:
raise RuntimeError(f"required tool not found on PATH: {name}")
def run(command: list[str]) -> subprocess.CompletedProcess[str]:
print("$ " + " ".join(command))
result = subprocess.run(command, cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
if result.stdout:
print(result.stdout.rstrip())
if result.stderr:
print(result.stderr.rstrip(), file=sys.stderr)
if result.returncode != 0:
raise RuntimeError(f"command failed with exit {result.returncode}: {' '.join(command)}")
return result
def run_result(command: list[str]) -> subprocess.CompletedProcess[str]:
print("$ " + " ".join(command))
result = subprocess.run(command, cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
if result.stdout:
print(result.stdout.rstrip())
if result.stderr:
print(result.stderr.rstrip(), file=sys.stderr)
return result
def remove_build_dir(build_dir: Path) -> None:
if build_dir.exists():
shutil.rmtree(build_dir)
def prepare_build_dir() -> Path:
if not BUILD_DIR.exists():
return BUILD_DIR
try:
remove_build_dir(BUILD_DIR)
return BUILD_DIR
except OSError:
return ROOT / "b" / f"cppn12_{os.getpid()}"
def configure_candidates() -> list[ConfigureCandidate]:
candidates: list[ConfigureCandidate] = []
if sys.platform == "win32":
if shutil.which("mingw32-make") is not None:
candidates.append(ConfigureCandidate("MinGW Makefiles", ["-G", "MinGW Makefiles"]))
if shutil.which("ninja") is not None:
candidates.append(ConfigureCandidate("Ninja", ["-G", "Ninja"]))
candidates.append(ConfigureCandidate("default", []))
else:
candidates.append(ConfigureCandidate("default", []))
return candidates
def cmake_configure_base_command(build_dir: Path) -> list[str]:
return [
"cmake",
"-S",
"Implementations/Reference/Runtime/cpp",
"-B",
str(build_dir.relative_to(ROOT)),
"-DFROG_RUNTIME_CPP_ENABLE_LLVM_KERNEL_BRIDGE=ON",
]
def configure_build_dir(build_dir: Path) -> Path:
errors: list[str] = []
for candidate in configure_candidates():
candidate_build_dir = build_dir
try:
remove_build_dir(candidate_build_dir)
except OSError:
candidate_build_dir = ROOT / "b" / f"cppn12_{os.getpid()}_{candidate.name.replace(' ', '_')}"
result = run_result(cmake_configure_base_command(candidate_build_dir) + candidate.args)
if result.returncode == 0:
print(f"CMake configure selected: {candidate.name}")
return candidate_build_dir
errors.append(f"{candidate.name}: exit {result.returncode}")
raise RuntimeError(f"no usable CMake generator for native Button switch-when-released bridge check ({'; '.join(errors)})")
def executable_candidates(build_dir: Path, name: str) -> list[Path]:
suffix = ".exe" if sys.platform == "win32" else ""
return [
build_dir / f"{name}{suffix}",
build_dir / "Debug" / f"{name}{suffix}",
build_dir / "Release" / f"{name}{suffix}",
build_dir / "RelWithDebInfo" / f"{name}{suffix}",
]
def executable_path(build_dir: Path, name: str) -> Path:
for candidate in executable_candidates(build_dir, name):
if candidate.exists():
return candidate
joined = ", ".join(str(path.relative_to(ROOT)) for path in executable_candidates(build_dir, name))
raise RuntimeError(f"unable to locate built executable {name}; checked {joined}")
def check_native_runtime_headless(build_dir: Path) -> None:
executable = executable_path(build_dir, "frog_reference_runtime_cpp_llvm_kernel")
press_result = run([str(executable.relative_to(ROOT)), "run", "true", "--example", "12"])
press_artifact = json.loads(press_result.stdout)
assert press_artifact["status"] == "ok"
assert press_artifact["execution_summary"]["trigger_pressed"] is True
assert press_artifact["outputs"]["public"]["switched"] is False
assert press_artifact["outputs"]["ui"]["trigger_button"] is False
assert press_artifact["outputs"]["ui"]["switched_indicator"] is False
assert press_artifact["ui_runtime"]["panel"]["layout"]["width"] == 420
release_result = run([str(executable.relative_to(ROOT)), "run", "false", "--example", "12"])
release_artifact = json.loads(release_result.stdout)
assert release_artifact["status"] == "ok"
assert release_artifact["execution_summary"]["trigger_pressed"] is False
assert release_artifact["outputs"]["public"]["switched"] is True
assert release_artifact["outputs"]["ui"]["trigger_button"] is True
assert release_artifact["outputs"]["ui"]["switched_indicator"] is True
def main() -> int:
try:
require_tool("cmake")
require_tool("clang")
build_dir = configure_build_dir(prepare_build_dir())
run([
"cmake",
"--build",
str(build_dir.relative_to(ROOT)),
"--target",
"frog_reference_runtime_cpp_llvm_button_switch_release_kernel_tests",
])
run([
"ctest",
"--test-dir",
str(build_dir.relative_to(ROOT)),
"-R",
"frog_reference_runtime_cpp_llvm_button_switch_release_kernel_tests",
"--output-on-failure",
])
run([
"cmake",
"--build",
str(build_dir.relative_to(ROOT)),
"--target",
"frog_reference_runtime_cpp_llvm_kernel",
])
check_native_runtime_headless(build_dir)
print("Example 12 C++ native Button switch-when-released kernel bridge check: ok")
return 0
except (RuntimeError, AssertionError, json.JSONDecodeError) as exc:
print(f"Example 12 C++ native Button switch-when-released kernel bridge check: FAILED: {exc}", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())