Skip to content
Prev Previous commit
Next Next commit
address review feedback
  • Loading branch information
juenglin committed Sep 30, 2026
commit 4002c6ddf2fed401b3a17d39f269895a453d68ee
25 changes: 17 additions & 8 deletions cuda_bindings/build_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import glob
import hashlib
import os
import shlex
import shutil
import sys
import sysconfig
Expand Down Expand Up @@ -128,20 +129,21 @@ def _resolve_toolchain_name():


def _with_compiler(command, compiler):
"""Replace the leading compiler on a sysconfig linker command; keep flags.
"""Replace the leading compiler on a linker command; keep flags.

Conda ``LDCXXSHARED`` looks like ``g++ -pthread -B .../python_compiler_compat
-shared ...``. Only the executable changes so those flags stay on the
link line. CC/CXX are not rewritten this way: they may already be a
link line. The command is tokenized with shlex so quoted arguments
survive. CC/CXX are not rewritten this way: they may already be a
launcher plus compiler (``sccache cc``).
"""
if not command or not str(command).strip():
if not command or not command.strip():
return compiler
parts = command.split()
parts = shlex.split(command)
i = 0
while i < len(parts) and not parts[i].startswith("-"):
i += 1
return " ".join([compiler, *parts[i:]])
return shlex.join([compiler, *parts[i:]])


def _with_sccache(current, compiler):
Expand All @@ -165,13 +167,20 @@ def _apply_toolchain_env(cc, cxx, explicit):
CC="sccache cc" in CI) keeps working. An explicit CUDA_PYTHON_TOOLCHAIN
override (incl. =gnu) sets CC/CXX to the toolchain compiler; an existing
sccache prefix is kept (CC="sccache cc" + llvm -> CC="sccache clang").
Sysconfig extras on LDCXXSHARED (rpath, -pthread, -B, ...) are kept;
LDSHARED is left unset so distutils rewrites it from CC.
Extras on LDCXXSHARED (rpath, -pthread, -B, ...) are kept, taken from the
environment if set there and from sysconfig otherwise; only the compiler
is swapped. LDSHARED is left unset so distutils rewrites it from CC.
"""
if explicit and cc is not None:
os.environ["CC"] = _with_sccache(os.environ.get("CC", ""), cc)
os.environ["CXX"] = _with_sccache(os.environ.get("CXX", ""), cxx)
ldcxxshared = sysconfig.get_config_var("LDCXXSHARED") or sysconfig.get_config_var("LDSHARED")
# An LDCXXSHARED the user already exported takes precedence over
# sysconfig's, as CC/CXX do; either way only the compiler is swapped.
ldcxxshared = (
os.environ.get("LDCXXSHARED")
or sysconfig.get_config_var("LDCXXSHARED")
or sysconfig.get_config_var("LDSHARED")
)
os.environ["LDCXXSHARED"] = _with_compiler(ldcxxshared, cxx) if ldcxxshared else f"{cxx} -shared"


Expand Down
125 changes: 113 additions & 12 deletions cuda_bindings/tests/test_build_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import importlib.util
import os
import shlex
import sys
import sysconfig
from pathlib import Path

# build_hooks.py imports Cython and setuptools at the top level; both are
Expand All @@ -25,6 +27,8 @@
import Cython # noqa: F401
import pytest
import setuptools # noqa: F401
from setuptools._distutils.ccompiler import new_compiler
from setuptools._distutils.sysconfig import customize_compiler


def _load_build_hooks():
Expand Down Expand Up @@ -53,6 +57,11 @@ def _isolate_toolchain_env():
os.environ.update(original)


def _fake_sysconfig(monkeypatch, **values):
"""Pin sysconfig.get_config_var so linker-command assertions are exact."""
monkeypatch.setattr(build_hooks.sysconfig, "get_config_var", lambda name: values.get(name))


class TestResolveToolchain:
"""_resolve_toolchain: pick compiler/linker/flags from CUDA_PYTHON_TOOLCHAIN.

Expand Down Expand Up @@ -107,6 +116,7 @@ def test_llvm_sets_env_and_flags(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("llvm only valid on Linux")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
_fake_sysconfig(monkeypatch, LDCXXSHARED="g++ -shared -Wl,-O1")
monkeypatch.delenv("CC", raising=False)
monkeypatch.delenv("CXX", raising=False)
monkeypatch.delenv("LDSHARED", raising=False)
Expand All @@ -115,7 +125,7 @@ def test_llvm_sets_env_and_flags(self, monkeypatch):
assert (cc, cxx) == ("clang", "clang++")
assert os.environ["CC"] == "clang"
assert os.environ["CXX"] == "clang++"
assert os.environ["LDCXXSHARED"].startswith("clang++")
assert os.environ["LDCXXSHARED"] == "clang++ -shared -Wl,-O1"
assert "LDSHARED" not in os.environ
assert "-fuse-ld=lld" in largs
# clang rejects the gcc-only flags that gnu uses; they must be absent.
Expand All @@ -127,6 +137,7 @@ def test_gnu_sets_env_and_flags(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("gnu only valid on Linux")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "gnu")
_fake_sysconfig(monkeypatch, LDCXXSHARED="x86_64-linux-gnu-g++ -shared -Wl,-O1")
monkeypatch.delenv("CC", raising=False)
monkeypatch.delenv("CXX", raising=False)
monkeypatch.delenv("LDSHARED", raising=False)
Expand All @@ -135,33 +146,35 @@ def test_gnu_sets_env_and_flags(self, monkeypatch):
assert (cc, cxx) == ("gcc", "g++")
assert os.environ["CC"] == "gcc"
assert os.environ["CXX"] == "g++"
assert os.environ["LDCXXSHARED"].startswith("g++")
assert os.environ["LDCXXSHARED"] == "g++ -shared -Wl,-O1"
assert "LDSHARED" not in os.environ
# gcc-only flags are present (this is the point of P2: explicit gnu must use gcc, not generic cc)
assert "-fpermissive" in cargs
assert "-fno-var-tracking-assignments" in cargs

@pytest.mark.agent_authored(model="glm-5.2")
def test_gnu_keeps_gcc_only_flags(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("gnu only valid on Linux")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "gnu")
_name, _cc, _cxx, cargs, _largs = build_hooks._resolve_toolchain()
assert "-fpermissive" in cargs
assert "-fno-var-tracking-assignments" in cargs

@pytest.mark.agent_authored(model="grok-4.6")
def test_llvm_keeps_sccache_prefix(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("llvm only valid on Linux")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
_fake_sysconfig(monkeypatch, LDCXXSHARED="g++ -shared -Wl,-O1")
monkeypatch.setenv("CC", "sccache cc")
monkeypatch.setenv("CXX", "sccache c++")
_name, _cc, _cxx, _cargs, _largs = build_hooks._resolve_toolchain()
assert os.environ["CC"] == "sccache clang"
assert os.environ["CXX"] == "sccache clang++"
assert "LDSHARED" not in os.environ
assert os.environ["LDCXXSHARED"].startswith("clang++")
# The launcher prefixes CC/CXX only; the shared linker command is the bare compiler.
assert os.environ["LDCXXSHARED"] == "clang++ -shared -Wl,-O1"

@pytest.mark.agent_authored(model="glm-5.2")
def test_gnu_keeps_gcc_only_flags(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("gnu only valid on Linux")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "gnu")
_name, _cc, _cxx, cargs, _largs = build_hooks._resolve_toolchain()
assert "-fpermissive" in cargs
assert "-fno-var-tracking-assignments" in cargs

@pytest.mark.agent_authored(model="grok-4.6")
def test_llvm_keeps_host_sccache_path(self, monkeypatch):
Expand Down Expand Up @@ -234,6 +247,40 @@ def test_explicit_llvm_keeps_conda_sysconfig_flags(self, monkeypatch):
assert os.environ["LDCXXSHARED"] == f"clang++ {extras}"
assert "LDSHARED" not in os.environ

@pytest.mark.agent_authored(model="claude-sonnet-5.5")
def test_explicit_toolchain_prefers_env_ldcxxshared(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("gnu/llvm only valid on Linux")
_fake_sysconfig(monkeypatch, LDCXXSHARED="g++ -shared -Wl,-O1")
monkeypatch.setenv("LDCXXSHARED", "g++ -shared -Wl,-rpath,/user/lib")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
build_hooks._resolve_toolchain()
assert os.environ["LDCXXSHARED"] == "clang++ -shared -Wl,-rpath,/user/lib"

@pytest.mark.agent_authored(model="claude-sonnet-5.5")
def test_explicit_toolchain_falls_back_to_ldshared_then_shared_flag(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("gnu/llvm only valid on Linux")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
_fake_sysconfig(monkeypatch, LDSHARED="gcc -shared -Wl,-z,relro")
build_hooks._resolve_toolchain()
assert os.environ["LDCXXSHARED"] == "clang++ -shared -Wl,-z,relro"
_fake_sysconfig(monkeypatch)
os.environ.pop("LDCXXSHARED")
build_hooks._resolve_toolchain()
assert os.environ["LDCXXSHARED"] == "clang++ -shared"

@pytest.mark.agent_authored(model="claude-sonnet-5.5")
def test_explicit_toolchain_is_idempotent(self, monkeypatch):
if sys.platform == "win32":
pytest.skip("gnu/llvm only valid on Linux")
_fake_sysconfig(monkeypatch, LDCXXSHARED="g++ -pthread -shared -Wl,-O1")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
build_hooks._resolve_toolchain()
first = os.environ["LDCXXSHARED"]
build_hooks._resolve_toolchain()
assert os.environ["LDCXXSHARED"] == first == "clang++ -pthread -shared -Wl,-O1"


class TestWithSccache:
"""_with_sccache: keep a leading sccache token, swap the compiler."""
Expand Down Expand Up @@ -282,6 +329,60 @@ def test_compiler_only_command_returns_compiler(self):
assert build_hooks._with_compiler("", "clang++") == "clang++"
assert build_hooks._with_compiler(None, "clang++") == "clang++"

@pytest.mark.agent_authored(model="claude-sonnet-5.5")
def test_keeps_quoted_arguments_intact(self):
result = build_hooks._with_compiler("g++ -Wl,-rpath='/a b' -shared", "clang++")
assert shlex.split(result) == ["clang++", "-Wl,-rpath=/a b", "-shared"]

@pytest.mark.agent_authored(model="claude-sonnet-5.5")
def test_drops_launcher_before_compiler(self):
assert build_hooks._with_compiler("ccache g++ -shared", "clang++") == "clang++ -shared"


class TestDistutilsLinkerIntegration:
"""The env set by _resolve_toolchain, as setuptools' distutils consumes it.

The tests above check os.environ; this checks the linker commands distutils
derives from it, which is what actually reaches the C++ link step.
"""

@staticmethod
def _customized_compiler():
compiler = new_compiler()
customize_compiler(compiler)
if not hasattr(compiler, "linker_so_cxx"):
pytest.skip("this setuptools' distutils has no linker_so_cxx")
return compiler

@pytest.mark.agent_authored(model="claude-sonnet-5.5")
@pytest.mark.parametrize("toolchain, compiler_cxx", [("gnu", "g++"), ("llvm", "clang++")])
def test_linker_so_cxx_swaps_compiler_and_keeps_sysconfig_flags(self, monkeypatch, toolchain, compiler_cxx):
if sys.platform != "linux":
pytest.skip("gnu/llvm only valid on Linux")
sysconfig_ld = sysconfig.get_config_var("LDCXXSHARED")
if not sysconfig_ld:
pytest.skip("this Python has no LDCXXSHARED")
expected_flags = [tok for tok in shlex.split(sysconfig_ld) if tok.startswith("-")]
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", toolchain)
build_hooks._resolve_toolchain()
linker = self._customized_compiler().linker_so_cxx
assert linker[0] == compiler_cxx
# Flags that follow the compiler in sysconfig are still there, in order.
assert linker[1 : 1 + len(expected_flags)] == expected_flags

@pytest.mark.agent_authored(model="claude-sonnet-5.5")
def test_sccache_prefixes_compile_but_not_the_shared_linker_command(self, monkeypatch):
if sys.platform != "linux":
pytest.skip("gnu/llvm only valid on Linux")
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
monkeypatch.setenv("CC", "sccache cc")
monkeypatch.setenv("CXX", "sccache c++")
build_hooks._resolve_toolchain()
compiler = self._customized_compiler()
assert compiler.compiler_cxx[:2] == ["sccache", "clang++"]
assert compiler.linker_so_cxx[0] == "clang++"
assert compiler.linker_so[:2] == ["sccache", "clang"]


class TestCheckToolchainAvailable:
"""_check_toolchain_available: fast, helpful failure when a tool is missing."""
Expand Down
25 changes: 17 additions & 8 deletions cuda_core/build_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import hashlib
import os
import re
import shlex
import shutil
import sys
import sysconfig
Expand Down Expand Up @@ -132,20 +133,21 @@ def _resolve_toolchain_name():


def _with_compiler(command, compiler):
"""Replace the leading compiler on a sysconfig linker command; keep flags.
"""Replace the leading compiler on a linker command; keep flags.

Conda ``LDCXXSHARED`` looks like ``g++ -pthread -B .../python_compiler_compat
-shared ...``. Only the executable changes so those flags stay on the
link line. CC/CXX are not rewritten this way: they may already be a
link line. The command is tokenized with shlex so quoted arguments
survive. CC/CXX are not rewritten this way: they may already be a
launcher plus compiler (``sccache cc``).
"""
if not command or not str(command).strip():
if not command or not command.strip():
return compiler
parts = command.split()
parts = shlex.split(command)
i = 0
while i < len(parts) and not parts[i].startswith("-"):
i += 1
return " ".join([compiler, *parts[i:]])
return shlex.join([compiler, *parts[i:]])


def _with_sccache(current, compiler):
Expand All @@ -169,13 +171,20 @@ def _apply_toolchain_env(cc, cxx, explicit):
CC="sccache cc" in CI) keeps working. An explicit CUDA_PYTHON_TOOLCHAIN
override (incl. =gnu) sets CC/CXX to the toolchain compiler; an existing
sccache prefix is kept (CC="sccache cc" + llvm -> CC="sccache clang").
Sysconfig extras on LDCXXSHARED (rpath, -pthread, -B, ...) are kept;
LDSHARED is left unset so distutils rewrites it from CC.
Extras on LDCXXSHARED (rpath, -pthread, -B, ...) are kept, taken from the
environment if set there and from sysconfig otherwise; only the compiler
is swapped. LDSHARED is left unset so distutils rewrites it from CC.
"""
if explicit and cc is not None:
os.environ["CC"] = _with_sccache(os.environ.get("CC", ""), cc)
os.environ["CXX"] = _with_sccache(os.environ.get("CXX", ""), cxx)
ldcxxshared = sysconfig.get_config_var("LDCXXSHARED") or sysconfig.get_config_var("LDSHARED")
# An LDCXXSHARED the user already exported takes precedence over
# sysconfig's, as CC/CXX do; either way only the compiler is swapped.
ldcxxshared = (
os.environ.get("LDCXXSHARED")
or sysconfig.get_config_var("LDCXXSHARED")
or sysconfig.get_config_var("LDSHARED")
)
os.environ["LDCXXSHARED"] = _with_compiler(ldcxxshared, cxx) if ldcxxshared else f"{cxx} -shared"


Expand Down
Loading
Loading