|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""build_hooks.py: the CUDA header check that runs before cythonize. |
| 5 | +
|
| 6 | +A cuda-bindings source tree is generated from one CUDA header set and compiles |
| 7 | +only against a toolkit of that major.minor; against another minor the C++ |
| 8 | +compile fails with redefinition errors that do not name the cause. The check |
| 9 | +reads both versions and fails early with a message that does. No GPU needed. |
| 10 | +
|
| 11 | +build_hooks.py is a PEP 517 backend, not an installed module, so it is loaded |
| 12 | +from source. It imports setuptools at the top; the ``test`` extra provides it. |
| 13 | +""" |
| 14 | + |
| 15 | +import importlib.util |
| 16 | +import os |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import pytest |
| 20 | +import setuptools # noqa: F401 |
| 21 | + |
| 22 | +# Don't call .resolve(): a symlinked checkout would make parents[1] point elsewhere. |
| 23 | +BUILD_HOOKS = Path(__file__).parents[1] / "build_hooks.py" |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="module") |
| 27 | +def build_hooks(): |
| 28 | + if not BUILD_HOOKS.is_file(): |
| 29 | + pytest.skip(f"{BUILD_HOOKS} is not in this tree; these tests need the source checkout") |
| 30 | + spec = importlib.util.spec_from_file_location("cuda_bindings_build_hooks", BUILD_HOOKS) |
| 31 | + module = importlib.util.module_from_spec(spec) |
| 32 | + spec.loader.exec_module(module) |
| 33 | + return module |
| 34 | + |
| 35 | + |
| 36 | +def _write_cuda_h(tmp_path, cuda_version): |
| 37 | + include = tmp_path / "include" |
| 38 | + include.mkdir(exist_ok=True) |
| 39 | + (include / "cuda.h").write_text(f"#define CUDA_VERSION {cuda_version}\n", encoding="utf-8") |
| 40 | + return str(tmp_path) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.agent_authored(model="claude-fable-5-1") |
| 44 | +def test_generated_header_version_is_read_from_cydriver_pxd(build_hooks): |
| 45 | + generated = build_hooks._generated_cuda_version() |
| 46 | + assert generated // 1000 in (12, 13) |
| 47 | + assert build_hooks._major_minor(13040) == "13.4" |
| 48 | + assert build_hooks._major_minor(12090) == "12.9" |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.agent_authored(model="claude-fable-5-1") |
| 52 | +def test_a_header_of_the_generated_major_minor_passes(build_hooks, tmp_path): |
| 53 | + generated = build_hooks._generated_cuda_version() |
| 54 | + build_hooks._check_cuda_headers(_write_cuda_h(tmp_path, generated)) |
| 55 | + # Only major.minor matters; the last digit (13041) is a toolkit patch. |
| 56 | + build_hooks._check_cuda_headers(_write_cuda_h(tmp_path, generated + 1)) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.agent_authored(model="claude-fable-5-1") |
| 60 | +@pytest.mark.parametrize("delta", [-10, 10, -1000, 1000]) |
| 61 | +def test_another_header_fails_and_names_both_versions(build_hooks, tmp_path, delta): |
| 62 | + generated = build_hooks._generated_cuda_version() |
| 63 | + cuda_path = _write_cuda_h(tmp_path, generated + delta) |
| 64 | + with pytest.raises(RuntimeError) as excinfo: |
| 65 | + build_hooks._check_cuda_headers(cuda_path) |
| 66 | + message = str(excinfo.value) |
| 67 | + needed, found = build_hooks._major_minor(generated), build_hooks._major_minor(generated + delta) |
| 68 | + assert message.startswith(f"This cuda-bindings source tree needs CUDA {needed} headers, but ") |
| 69 | + assert os.path.realpath(os.path.join(cuda_path, "include", "cuda.h")) in message # the resolved path |
| 70 | + assert f" is CUDA {found}. This is a build-time requirement only" in message |
| 71 | + assert build_hooks._INSTALL_URL in message |
| 72 | + assert message.endswith( |
| 73 | + f"Point CUDA_PATH or CUDA_HOME at a CUDA {needed} toolkit, or build from cuda-bindings {found}.x sources." |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.agent_authored(model="claude-fable-5-1") |
| 78 | +def test_an_unreadable_cuda_h_is_a_clear_error(build_hooks, tmp_path): |
| 79 | + with pytest.raises(RuntimeError, match=r"Cannot read CUDA_VERSION from .*cuda\.h"): |
| 80 | + build_hooks._check_cuda_headers(str(tmp_path)) # no include/cuda.h |
| 81 | + (tmp_path / "include").mkdir() |
| 82 | + (tmp_path / "include" / "cuda.h").write_text("/* no CUDA_VERSION macro */\n", encoding="utf-8") |
| 83 | + with pytest.raises(RuntimeError, match=r"Cannot read CUDA_VERSION from .*cuda\.h"): |
| 84 | + build_hooks._check_cuda_headers(str(tmp_path)) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.agent_authored(model="claude-fable-5-1") |
| 88 | +def test_the_build_checks_the_header_before_it_touches_the_tree(build_hooks, tmp_path, monkeypatch): |
| 89 | + pytest.importorskip("Cython") # _build_cuda_bindings imports it first |
| 90 | + cuda_path = _write_cuda_h(tmp_path, build_hooks._generated_cuda_version() + 10) |
| 91 | + monkeypatch.setattr(build_hooks, "_get_cuda_path", lambda: cuda_path) |
| 92 | + |
| 93 | + def not_reached(): |
| 94 | + raise AssertionError("the header check must run before the source tree is modified") |
| 95 | + |
| 96 | + monkeypatch.setattr(build_hooks, "_rename_architecture_specific_files", not_reached) |
| 97 | + with pytest.raises(RuntimeError, match="source tree needs CUDA .* headers"): |
| 98 | + build_hooks._build_cuda_bindings() |
0 commit comments