Skip to content

Commit 9828a2f

Browse files
committed
build: consolidate _resolve_toolchain and unify flag set across backends
Closes the audit in NVIDIA#1882. cuda_bindings' and cuda_core's ``_resolve_toolchain`` framework was identical (call _resolve_toolchain_name, msvc-debug error, _apply_toolchain_env, tuple return); only the ``extra_compile_args``/``extra_link_args`` choices differed, and NVIDIA#1882 traced that difference to legacy drift, not intent. Both are now the same function in _build_shared. Consolidations: - New ``resolve_toolchain(debug, compile_for_coverage)`` in ``_build_shared.py`` handles the whole workflow (name resolution, msvc-debug guard, flag assembly, env application, return tuple). - New ``_build_flags(name, debug, compile_for_coverage)`` in ``_build_shared.py`` carries the unified flag set. Both backends drop their local copies. - Both ``build_hooks.py`` import ``resolve_toolchain`` and call it with just the two switches; no per-package callable is passed. Flag choices are now the single set: - Linux ``-std=c++17`` (bindings used ``c++14``) - MSVC ``/std:c++17`` (bindings had no ``/std:`` at all) - Non-debug ``-g0 -O2`` (bindings used ``-O3``) - Debug ``-g -O0 -D _GLIBCXX_ASSERTIONS`` - llvm ``-fuse-ld=lld`` - Coverage ``-DCYTHON_TRACE_NOGIL=1 -DCYTHON_USE_SYS_MONITORING=0`` - Non-debug ``-Wl,--strip-all`` Bindings loses ``-Wno-deprecated-declarations``, ``-fpermissive``, and ``-fno-var-tracking-assignments`` -- per NVIDIA#1882, these were legacy leftover with no active justification. CI will surface any compilation regressions if the Cython-generated C++ actually needs ``-fpermissive``. Tests: the shared ``ResolveToolchainSharedMixin`` gains ``test_llvm_sets_env_and_flags`` (both backends now assert the same flags). Per-package ``TestResolveToolchain`` classes are gone; ``test_gnu_keeps_gcc_only_flags`` (bindings) is deleted because the flags it guarded are gone. Pass counts: 28 bindings (was 29), 59 core (unchanged).
1 parent fb82b62 commit 9828a2f

6 files changed

Lines changed: 77 additions & 170 deletions

File tree

‎cuda_bindings/_build_shared.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,55 @@ def _check_toolchain_available(name):
195195
)
196196

197197

198+
def _build_flags(name, debug, compile_for_coverage):
199+
"""Compile/link flags for a resolved toolchain (shared across backends).
200+
201+
The two backends used to carry slightly different flag sets — bindings had
202+
``-std=c++14``, ``-fpermissive``, ``-fno-var-tracking-assignments``, and
203+
``-Wno-deprecated-declarations``; core had ``-std=c++17`` and no MSVC
204+
``/std:``. See https://github.com/NVIDIA/cuda-python/issues/1882 for the
205+
audit that traced the difference to legacy drift, not intent.
206+
"""
207+
extra_compile_args = []
208+
extra_link_args = []
209+
210+
if name == "msvc":
211+
extra_compile_args += ["/std:c++17"]
212+
else:
213+
extra_compile_args += ["-std=c++17"]
214+
if name == "llvm":
215+
extra_link_args += ["-fuse-ld=lld"]
216+
if debug:
217+
extra_compile_args += ["-g", "-O0", "-D _GLIBCXX_ASSERTIONS"]
218+
else:
219+
extra_compile_args += ["-g0", "-O2"]
220+
extra_link_args += ["-Wl,--strip-all"]
221+
222+
if compile_for_coverage:
223+
# CYTHON_TRACE_NOGIL indicates to trace nogil functions. It is not
224+
# related to free-threading builds.
225+
extra_compile_args += ["-DCYTHON_TRACE_NOGIL=1", "-DCYTHON_USE_SYS_MONITORING=0"]
226+
227+
return extra_compile_args, extra_link_args
228+
229+
230+
def resolve_toolchain(debug=False, compile_for_coverage=False):
231+
"""Resolve the C/C++ toolchain from CUDA_PYTHON_TOOLCHAIN.
232+
233+
Returns (name, cc, cxx, extra_compile_args, extra_link_args). The default
234+
toolchain (gnu on Linux, msvc on Windows) does not touch CC/CXX/LDSHARED,
235+
so an externally-set compiler (e.g. CC="sccache cc") keeps working. An
236+
explicit CUDA_PYTHON_TOOLCHAIN (llvm on Linux) sets CC/CXX/LDSHARED to
237+
the toolchain's binaries so distutils' customize_compiler picks them up.
238+
"""
239+
name, _allowed, cc, cxx, explicit = _resolve_toolchain_name()
240+
if name == "msvc" and debug:
241+
raise RuntimeError("Debuggable builds are not supported on Windows.")
242+
extra_compile_args, extra_link_args = _build_flags(name, debug, compile_for_coverage)
243+
_apply_toolchain_env(cc, cxx, explicit)
244+
return name, cc, cxx, extra_compile_args, extra_link_args
245+
246+
198247
# -----------------------------------------------------------------------
199248
# Cython generated-source cache (opt-in via CUDA_PYTHON_CYTHON_CACHE_DIR)
200249
#

‎cuda_bindings/build_hooks.py‎

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
# below (flag sets, stamp bookkeeping, PEP 517 hooks) is package-specific.
2727
from _build_shared import (
2828
_abi_stamp_path,
29-
_apply_toolchain_env,
3029
_check_toolchain_available,
3130
_cython_cache_path,
3231
_get_cuda_path,
@@ -35,6 +34,7 @@
3534
_stable_cython_alias,
3635
check_build_key,
3736
record_build_key, # noqa: F401 (setup.py stamps the toolchain via this)
37+
resolve_toolchain,
3838
)
3939

4040
# Metadata hooks delegate directly to setuptools -- no CUDA needed.
@@ -53,49 +53,6 @@
5353
_extensions = None
5454

5555

56-
def _resolve_toolchain(debug=False, compile_for_coverage=False):
57-
"""Resolve the C/C++ toolchain from CUDA_PYTHON_TOOLCHAIN.
58-
59-
Returns (name, cc, cxx, extra_compile_args, extra_link_args). The default
60-
toolchain (gnu on Linux, msvc on Windows) reproduces the previous build
61-
behavior and does not touch CC/CXX/LDSHARED, so an externally-set compiler
62-
(e.g. CC="sccache cc") keeps working. A non-default toolchain (llvm on
63-
Linux) selects clang/clang++ and lld and sets CC/CXX/LDSHARED so distutils'
64-
customize_compiler picks them up.
65-
"""
66-
name, _allowed, cc, cxx, explicit = _resolve_toolchain_name()
67-
68-
extra_compile_args = []
69-
extra_link_args = []
70-
71-
if name == "msvc":
72-
if debug:
73-
raise RuntimeError("Debuggable builds are not supported on Windows.")
74-
else:
75-
# Common Linux compile flags.
76-
extra_compile_args += ["-std=c++14", "-Wno-deprecated-declarations"]
77-
# Compiler-specific flags.
78-
if name == "gnu":
79-
extra_compile_args += ["-fpermissive", "-fno-var-tracking-assignments"]
80-
elif name == "llvm":
81-
extra_link_args += ["-fuse-ld=lld"]
82-
# Common Linux debug/opt flags.
83-
if debug:
84-
extra_compile_args += ["-g", "-O0", "-D _GLIBCXX_ASSERTIONS"]
85-
else:
86-
extra_compile_args += ["-g0", "-O3"]
87-
extra_link_args += ["-Wl,--strip-all"]
88-
89-
if compile_for_coverage:
90-
# CYTHON_TRACE_NOGIL indicates to trace nogil functions. It is not
91-
# related to free-threading builds.
92-
extra_compile_args += ["-DCYTHON_TRACE_NOGIL=1", "-DCYTHON_USE_SYS_MONITORING=0"]
93-
94-
_apply_toolchain_env(cc, cxx, explicit)
95-
96-
return name, cc, cxx, extra_compile_args, extra_link_args
97-
98-
9956
# -----------------------------------------------------------------------
10057
# Toolchain stamp
10158
#
@@ -202,7 +159,7 @@ def _build_cuda_bindings(debug=False):
202159
# Resolve the C/C++ toolchain (CUDA_PYTHON_TOOLCHAIN). The default (gnu on
203160
# Linux, msvc on Windows) reproduces the previous build behavior and does
204161
# not touch CC/CXX, so an externally-set compiler (e.g. sccache) survives.
205-
toolchain, _cc, _cxx, extra_compile_args, extra_link_args = _resolve_toolchain(
162+
toolchain, _cc, _cxx, extra_compile_args, extra_link_args = resolve_toolchain(
206163
debug=debug, compile_for_coverage=compile_for_coverage
207164
)
208165
_check_toolchain_available(toolchain)

‎cuda_bindings/tests/test_build_hooks.py‎

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -64,44 +64,6 @@ def _isolate_toolchain_env():
6464
os.environ.update(original)
6565

6666

67-
class TestResolveToolchain:
68-
"""cuda.bindings-specific ``_resolve_toolchain`` assertions.
69-
70-
Shared behavior (default no-touch, sccache preservation, case-insensitive
71-
parsing, invalid-value error, llvm-overrides-external-CC) is covered by
72-
``TestResolveToolchainShared`` further down via the shared mixin. The
73-
tests here assert the bindings-specific flag set: gnu adds
74-
``-fpermissive`` and ``-fno-var-tracking-assignments``; core does not.
75-
"""
76-
77-
@pytest.mark.agent_authored(model="glm-5.2")
78-
def test_llvm_sets_env_and_flags(self, monkeypatch):
79-
if sys.platform == "win32":
80-
pytest.skip("llvm only valid on Linux")
81-
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
82-
monkeypatch.delenv("CC", raising=False)
83-
monkeypatch.delenv("CXX", raising=False)
84-
monkeypatch.delenv("LDSHARED", raising=False)
85-
name, cc, cxx, cargs, largs = build_hooks._resolve_toolchain()
86-
assert name == "llvm"
87-
assert (cc, cxx) == ("clang", "clang++")
88-
assert os.environ["CC"] == "clang"
89-
assert os.environ["CXX"] == "clang++"
90-
assert "-fuse-ld=lld" in largs
91-
# clang rejects the gcc-only flags that gnu uses; they must be absent.
92-
assert "-fpermissive" not in cargs
93-
assert "-fno-var-tracking-assignments" not in cargs
94-
95-
@pytest.mark.agent_authored(model="glm-5.2")
96-
def test_gnu_keeps_gcc_only_flags(self, monkeypatch):
97-
if sys.platform == "win32":
98-
pytest.skip("gnu only valid on Linux")
99-
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "gnu")
100-
_name, _cc, _cxx, cargs, _largs = build_hooks._resolve_toolchain()
101-
assert "-fpermissive" in cargs
102-
assert "-fno-var-tracking-assignments" in cargs
103-
104-
10567
@pytest.fixture
10668
def stamp(tmp_path, monkeypatch):
10769
"""Redirect the toolchain stamp to a scratch path and reset the shared force flag."""

‎cuda_core/build_hooks.py‎

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
2828
from _build_shared import (
2929
_BUILD_DIR,
3030
_abi_stamp_path,
31-
_apply_toolchain_env,
3231
_check_toolchain_available,
3332
_cython_cache_path,
3433
_get_cuda_path,
3534
_import_get_cuda_path_or_home, # noqa: F401 (re-export for tests)
36-
_resolve_toolchain_name,
3735
_stable_cython_alias,
3836
check_build_key,
3937
record_build_key,
38+
resolve_toolchain,
4039
)
4140

4241
prepare_metadata_for_build_editable = _build_meta.prepare_metadata_for_build_editable
@@ -50,48 +49,6 @@
5049
COMPILE_FOR_COVERAGE = bool(int(os.environ.get("CUDA_PYTHON_COVERAGE", "0")))
5150

5251

53-
def _resolve_toolchain(debug=False, compile_for_coverage=False):
54-
"""Resolve the C/C++ toolchain from CUDA_PYTHON_TOOLCHAIN (cuda.core flags).
55-
56-
Returns (name, cc, cxx, extra_compile_args, extra_link_args). The default
57-
toolchain (gnu on Linux, msvc on Windows) reproduces the previous build
58-
behavior and does not touch CC/CXX/LDSHARED, so an externally-set compiler
59-
(e.g. CC="sccache cc") keeps working. A non-default toolchain (llvm on
60-
Linux) selects clang/clang++ and lld and sets CC/CXX/LDSHARED so distutils'
61-
customize_compiler picks them up.
62-
"""
63-
name, _allowed, cc, cxx, explicit = _resolve_toolchain_name()
64-
65-
extra_compile_args = []
66-
extra_link_args = []
67-
68-
if name == "msvc":
69-
extra_compile_args += ["/std:c++17"]
70-
if debug:
71-
raise RuntimeError("Debuggable builds are not supported on Windows.")
72-
else:
73-
# Common Linux compile flags.
74-
extra_compile_args += ["-std=c++17"]
75-
# Compiler-specific flags.
76-
if name == "llvm":
77-
extra_link_args += ["-fuse-ld=lld"]
78-
# Common Linux debug/opt flags.
79-
if debug:
80-
extra_compile_args += ["-g", "-O0", "-D _GLIBCXX_ASSERTIONS"]
81-
else:
82-
extra_compile_args += ["-g0", "-O2"]
83-
extra_link_args += ["-Wl,--strip-all"]
84-
85-
if compile_for_coverage:
86-
# CYTHON_TRACE_NOGIL indicates to trace nogil functions. It is not
87-
# related to free-threading builds.
88-
extra_compile_args += ["-DCYTHON_TRACE_NOGIL=1", "-DCYTHON_USE_SYS_MONITORING=0"]
89-
90-
_apply_toolchain_env(cc, cxx, explicit)
91-
92-
return name, cc, cxx, extra_compile_args, extra_link_args
93-
94-
9552
@functools.cache
9653
def _determine_cuda_major_version() -> str:
9754
"""Determine the CUDA major version for building cuda.core.
@@ -262,7 +219,7 @@ def module_names():
262219
# Resolve the C/C++ toolchain (CUDA_PYTHON_TOOLCHAIN). The default (gnu on
263220
# Linux, msvc on Windows) reproduces the previous build behavior and does
264221
# not touch CC/CXX, so an externally-set compiler (e.g. sccache) survives.
265-
toolchain, _cc, _cxx, extra_compile_args, extra_link_args = _resolve_toolchain(
222+
toolchain, _cc, _cxx, extra_compile_args, extra_link_args = resolve_toolchain(
266223
debug=debug, compile_for_coverage=COMPILE_FOR_COVERAGE
267224
)
268225
_check_toolchain_available(toolchain)

‎cuda_core/tests/test_build_hooks.py‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -507,35 +507,6 @@ def test_serial_builds_and_compilers_without_the_hook_keep_the_stock_path(self,
507507
assert cmd.compiler.compile(["a.cpp"]) == "stock"
508508

509509

510-
class TestResolveToolchain:
511-
"""cuda.core-specific ``_resolve_toolchain`` assertions.
512-
513-
Shared behavior (default no-touch, sccache preservation, case-insensitive
514-
parsing, invalid-value error, llvm-overrides-external-CC) is covered by
515-
``TestResolveToolchainShared`` via the shared mixin. The tests here assert
516-
the cuda.core-specific flag set: neither gnu nor llvm adds
517-
``-fpermissive`` / ``-fno-var-tracking-assignments`` (those are bindings-only).
518-
"""
519-
520-
@pytest.mark.agent_authored(model="glm-5.2")
521-
def test_llvm_sets_env_and_flags(self, monkeypatch):
522-
if sys.platform == "win32":
523-
pytest.skip("llvm only valid on Linux")
524-
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
525-
monkeypatch.delenv("CC", raising=False)
526-
monkeypatch.delenv("CXX", raising=False)
527-
monkeypatch.delenv("LDSHARED", raising=False)
528-
name, cc, cxx, cargs, largs = build_hooks._resolve_toolchain()
529-
assert name == "llvm"
530-
assert (cc, cxx) == ("clang", "clang++")
531-
assert os.environ["CC"] == "clang"
532-
assert os.environ["CXX"] == "clang++"
533-
assert "-fuse-ld=lld" in largs
534-
# clang rejects the gcc-only flags that gnu uses; they must be absent.
535-
assert "-fpermissive" not in cargs
536-
assert "-fno-var-tracking-assignments" not in cargs
537-
538-
539510
# ---------------------------------------------------------------------------
540511
# Cython cache path helper (workaround for cython/cython#7532)
541512
#

‎cuda_python_test_helpers/cuda_python_test_helpers/build_shared.py‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
1313
Kept out of these mixins on purpose:
1414
15-
- ``test_llvm_sets_env_and_flags`` / ``test_gnu_sets_env_and_flags``: each
16-
package's ``_resolve_toolchain()`` assembles a different flag set (bindings
17-
adds ``-fpermissive``/``-fno-var-tracking-assignments`` on gnu, core does
18-
not), so the flag-set assertions live in each package's file.
1915
- ``TestBuildToolchainStamp`` / ``TestBuildConfigStamp``: bindings stamps the
2016
toolchain name, core stamps a composite ``cu{major}-{toolchain}-...`` key.
2117
Only the ``_abi_stamp_path`` mechanics are shared here.
@@ -30,15 +26,14 @@
3026

3127

3228
class ResolveToolchainSharedMixin:
33-
"""Common ``_resolve_toolchain`` assertions that don't depend on the flag set.
29+
"""Common ``resolve_toolchain`` assertions covering the shared flag policy.
3430
3531
Subclasses set ``build_hooks`` (the loaded build_hooks module). The
3632
default-toolchain happy paths (env not touched, external CC preserved,
37-
gnu flag set applied) aren't asserted here — a successful wheel build
38-
on any Linux CI worker already exercises them. Only behavior that a
39-
passing wheel build would not surface is kept: error paths and the
40-
llvm-override case (llvm has no CI, so it's not covered by wheels
41-
either).
33+
gnu compile) are exercised by every green Linux wheel build in CI, so
34+
they aren't asserted here. Only behavior that a passing wheel build
35+
would not surface is kept: error paths, the llvm-override case, and the
36+
llvm flag set (llvm has no CI, so it's not covered by wheels either).
4237
"""
4338

4439
build_hooks = None
@@ -48,14 +43,14 @@ def test_case_insensitive(self, monkeypatch):
4843
if sys.platform == "win32":
4944
pytest.skip("llvm only valid on Linux")
5045
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "LLVM")
51-
name, _cc, _cxx, _cargs, _largs = self.build_hooks._resolve_toolchain()
46+
name, _cc, _cxx, _cargs, _largs = self.build_hooks.resolve_toolchain()
5247
assert name == "llvm"
5348

5449
@pytest.mark.agent_authored(model="glm-5.2")
5550
def test_invalid_value_raises(self, monkeypatch):
5651
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "icc")
5752
with pytest.raises(RuntimeError, match="not supported"):
58-
self.build_hooks._resolve_toolchain()
53+
self.build_hooks.resolve_toolchain()
5954

6055
@pytest.mark.agent_authored(model="glm-5.2")
6156
def test_llvm_overrides_external_cc(self, monkeypatch):
@@ -65,9 +60,25 @@ def test_llvm_overrides_external_cc(self, monkeypatch):
6560
# external CC (e.g. "sccache cc") is replaced, not kept.
6661
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
6762
monkeypatch.setenv("CC", "sccache cc")
68-
_name, _cc, _cxx, _cargs, _largs = self.build_hooks._resolve_toolchain()
63+
_name, _cc, _cxx, _cargs, _largs = self.build_hooks.resolve_toolchain()
6964
assert os.environ["CC"] == "clang"
7065

66+
@pytest.mark.agent_authored(model="glm-5.2")
67+
def test_llvm_sets_env_and_flags(self, monkeypatch):
68+
if sys.platform == "win32":
69+
pytest.skip("llvm only valid on Linux")
70+
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "llvm")
71+
monkeypatch.delenv("CC", raising=False)
72+
monkeypatch.delenv("CXX", raising=False)
73+
monkeypatch.delenv("LDSHARED", raising=False)
74+
name, cc, cxx, cargs, largs = self.build_hooks.resolve_toolchain()
75+
assert name == "llvm"
76+
assert (cc, cxx) == ("clang", "clang++")
77+
assert os.environ["CC"] == "clang"
78+
assert os.environ["CXX"] == "clang++"
79+
assert "-fuse-ld=lld" in largs
80+
assert "-std=c++17" in cargs
81+
7182

7283
class CheckToolchainAvailableSharedMixin:
7384
"""Common ``_check_toolchain_available`` assertions.

0 commit comments

Comments
 (0)