diff options
Diffstat (limited to 'tests/conftest.py')
| -rw-r--r-- | tests/conftest.py | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index c78e0209a..f614163c5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py | |||
| @@ -17,13 +17,46 @@ import pytest | |||
| 17 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 17 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 18 | sys.path.insert(0, os.path.join(ROOT, "tools")) | 18 | sys.path.insert(0, os.path.join(ROOT, "tools")) |
| 19 | 19 | ||
| 20 | import mc_command # noqa: E402 | ||
| 20 | import rdna2spirv # noqa: E402 | 21 | import rdna2spirv # noqa: E402 |
| 21 | 22 | ||
| 22 | 23 | ||
| 24 | def _repair_stdin_handle(): | ||
| 25 | """Windows, under pytest's fd capture: give the children the tests start a stdin that is open. | ||
| 26 | |||
| 27 | The capture replaces fd 0 with os.dup2(), which closes the handle fd 0 had, while the process's | ||
| 28 | STD_INPUT_HANDLE keeps that handle's number. subprocess starts from GetStdHandle(), not from fd 0, so a child | ||
| 29 | started without stdin=/input= fails with "[WinError 6] The handle is invalid" or, when the number has been | ||
| 30 | reused meanwhile, gets whatever file now has it. Which one depends on what ran before, so the failures move | ||
| 31 | from run to run. With -s or --capture=sys, and without pytest, the two handles agree and nothing is done. | ||
| 32 | """ | ||
| 33 | if sys.platform != "win32": | ||
| 34 | return | ||
| 35 | import ctypes | ||
| 36 | import msvcrt | ||
| 37 | kernel32 = ctypes.WinDLL("kernel32") | ||
| 38 | kernel32.GetStdHandle.restype = ctypes.c_void_p | ||
| 39 | kernel32.GetStdHandle.argtypes = [ctypes.c_uint32] | ||
| 40 | kernel32.SetStdHandle.argtypes = [ctypes.c_uint32, ctypes.c_void_p] | ||
| 41 | std_input = 0xFFFFFFF6 # STD_INPUT_HANDLE, (DWORD)-10 | ||
| 42 | handle = kernel32.GetStdHandle(std_input) | ||
| 43 | try: | ||
| 44 | fd0 = msvcrt.get_osfhandle(0) | ||
| 45 | except OSError: | ||
| 46 | fd0 = None | ||
| 47 | if not handle or handle == fd0: # no stdin at all (subprocess copes), or not redirected | ||
| 48 | return | ||
| 49 | # A NUL of our own, never closed: fd 0's handle would go stale again the next time the capture swaps fd 0. | ||
| 50 | kernel32.SetStdHandle(std_input, msvcrt.get_osfhandle(os.open(os.devnull, os.O_RDONLY))) | ||
| 51 | |||
| 52 | |||
| 53 | _repair_stdin_handle() # before the llvm-mc probe below, which is the first child | ||
| 54 | |||
| 55 | |||
| 23 | def _has_amdgpu(mc): | 56 | def _has_amdgpu(mc): |
| 24 | """True if this llvm-mc was built with the AMDGPU target (the official Windows release archives are not).""" | 57 | """True if this llvm-mc was built with the AMDGPU target (the official Windows release archives are not).""" |
| 25 | try: | 58 | try: |
| 26 | r = subprocess.run([mc, "--version"], capture_output=True, text=True, timeout=60) | 59 | r = subprocess.run(mc + ["--version"], capture_output=True, text=True, timeout=60) |
| 27 | except (OSError, subprocess.SubprocessError): | 60 | except (OSError, subprocess.SubprocessError): |
| 28 | return False | 61 | return False |
| 29 | return "amdgcn" in r.stdout | 62 | return "amdgcn" in r.stdout |
| @@ -33,12 +66,14 @@ def _find_llvm_mc(): | |||
| 33 | candidates = [] | 66 | candidates = [] |
| 34 | env = os.environ.get("LLVM_MC") | 67 | env = os.environ.get("LLVM_MC") |
| 35 | if env: | 68 | if env: |
| 36 | candidates.append(shutil.which(env)) # None when the path doesn't exist | 69 | candidates.append(mc_command.split_command(env)) |
| 37 | candidates.append(shutil.which("llvm-mc")) | 70 | found = shutil.which("llvm-mc") |
| 71 | if found: | ||
| 72 | candidates.append([found]) | ||
| 38 | # Debian/Ubuntu install it versioned, out of PATH. | 73 | # Debian/Ubuntu install it versioned, out of PATH. |
| 39 | candidates += sorted(glob.glob("/usr/lib/llvm-*/bin/llvm-mc"), reverse=True) | 74 | candidates += [[p] for p in sorted(glob.glob("/usr/lib/llvm-*/bin/llvm-mc"), reverse=True)] |
| 40 | # Default location of the official Windows installer, which doesn't add LLVM to PATH unless asked to. | 75 | # Default location of the official Windows installer, which doesn't add LLVM to PATH unless asked to. |
| 41 | candidates += glob.glob(r"C:\Program Files\LLVM\bin\llvm-mc.exe") | 76 | candidates += [[p] for p in glob.glob(r"C:\Program Files\LLVM\bin\llvm-mc.exe")] |
| 42 | for c in candidates: | 77 | for c in candidates: |
| 43 | if c and _has_amdgpu(c): | 78 | if c and _has_amdgpu(c): |
| 44 | return c | 79 | return c |
| @@ -126,7 +161,7 @@ class Shader: | |||
| 126 | rsrc2, (lx, ly, lz) = cs_header | 161 | rsrc2, (lx, ly, lz) = cs_header |
| 127 | (workdir / "fssm").mkdir(exist_ok=True) | 162 | (workdir / "fssm").mkdir(exist_ok=True) |
| 128 | (workdir / "fssm" / f"{name}.fssm").write_bytes(bytes(0x138) + struct.pack("<4I", rsrc2, lx, ly, lz)) | 163 | (workdir / "fssm" / f"{name}.fssm").write_bytes(bytes(0x138) + struct.pack("<4I", rsrc2, lx, ly, lz)) |
| 129 | cmd = [LLVM_MC, "-triple=amdgcn--amdpal", "-mcpu=gfx1013"] | 164 | cmd = LLVM_MC + ["-triple=amdgcn--amdpal", "-mcpu=gfx1013"] |
| 130 | for ln in asm.splitlines(): | 165 | for ln in asm.splitlines(): |
| 131 | s = ln.strip() | 166 | s = ln.strip() |
| 132 | if not s: | 167 | if not s: |