Skip to content

Commit d40f92a

Browse files
committed
build(core): stamp the exact successful configuration
1 parent c5be9b6 commit d40f92a

3 files changed

Lines changed: 111 additions & 22 deletions

File tree

‎cuda_core/build_hooks.py‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,8 @@ def _check_build_config(toolchain, debug, coverage):
303303
return cuda_major, key
304304

305305

306-
def record_build_config(debug) -> None:
307-
"""Stamp the build configuration of the build that just completed.
308-
309-
setup.py calls this (passing self.debug) after build_ext succeeds, so
310-
that a build which failed partway through does not claim outputs it never
311-
produced. All other inputs (toolchain, cuda_major, coverage) are
312-
re-derived from the environment and the functools.cache.
313-
"""
314-
toolchain, *_ = _resolve_toolchain_name()
315-
cuda_major = _determine_cuda_major_version()
316-
coverage = bool(int(os.environ.get("CUDA_PYTHON_COVERAGE", "0")))
317-
key = _build_config_key(cuda_major, toolchain, debug, coverage)
306+
def record_build_config(key) -> None:
307+
"""Stamp the exact build configuration after the backend succeeds."""
318308
_BUILD_CONFIG_STAMP.parent.mkdir(parents=True, exist_ok=True)
319309
_BUILD_CONFIG_STAMP.write_text(key + "\n", encoding="utf-8")
320310

@@ -475,7 +465,7 @@ def module_names():
475465
# MSVC linker output paths past MAX_PATH in deeper Windows checkouts.
476466
_relativize_extension_sources(_extensions)
477467

478-
return
468+
return config_key
479469

480470

481471
def _add_cython_include_paths_to_pth(wheel_path: str) -> None:
@@ -560,20 +550,23 @@ def _add_cython_include_paths_to_pth(wheel_path: str) -> None:
560550
def build_editable(wheel_directory, config_settings=None, metadata_directory=None):
561551
debug_default = sys.platform != "win32" # Debug builds not supported on Windows
562552
debug = config_settings.get("debug", debug_default) if config_settings else debug_default
563-
_build_cuda_core(debug=debug)
553+
config_key = _build_cuda_core(debug=debug)
564554
wheel_name = _build_meta.build_editable(wheel_directory, config_settings, metadata_directory)
565555

566556
# Patch the .pth file to add Cython include paths
567557
wheel_path = os.path.join(wheel_directory, wheel_name)
568558
_add_cython_include_paths_to_pth(wheel_path)
559+
record_build_config(config_key)
569560

570561
return wheel_name
571562

572563

573564
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
574565
debug = config_settings.get("debug", False) if config_settings else False
575-
_build_cuda_core(debug=debug)
576-
return _build_meta.build_wheel(wheel_directory, config_settings, metadata_directory)
566+
config_key = _build_cuda_core(debug=debug)
567+
wheel_name = _build_meta.build_wheel(wheel_directory, config_settings, metadata_directory)
568+
record_build_config(config_key)
569+
return wheel_name
577570

578571

579572
def _get_cuda_bindings_require():

‎cuda_core/setup.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def build_extensions(self):
137137
self._configure_windows_tensor_bridge()
138138
with self._parallel_source_compilation():
139139
super().build_extensions()
140-
build_hooks.record_build_config(self.debug)
141140

142141

143142
class build_py(_build_py): # noqa: N801

‎cuda_core/tests/test_build_hooks.py‎

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,110 @@ def test_changed_coverage_forces_rebuild(self, stamp):
251251
build_hooks._check_build_config("gnu", False, True)
252252
assert build_hooks.force_build_ext is True
253253

254-
@pytest.mark.agent_authored(model="glm-5.2")
254+
@pytest.mark.agent_authored(model="gpt-5.6-sol")
255255
def test_record_writes_stamp(self, stamp):
256-
# record_build_config re-derives from env; pass debug=False to match.
257-
# Platform default toolchain: msvc on Windows, gnu on Linux.
256+
build_hooks.record_build_config("cu13-gnu-debug")
257+
assert stamp.read_text().strip() == "cu13-gnu-debug"
258+
259+
260+
class TestBuildHookStamping:
261+
@pytest.mark.agent_authored(model="gpt-5.6-sol")
262+
def test_wheel_records_exact_prepared_config_after_success(self, monkeypatch):
263+
events = []
264+
265+
def prepare(debug):
266+
events.append(("prepare", debug))
267+
return "cu13-gnu-debug"
268+
269+
def build(wheel_directory, config_settings, metadata_directory):
270+
events.append("build")
271+
return "cuda_core.whl"
272+
273+
monkeypatch.setattr(build_hooks, "_build_cuda_core", prepare)
274+
monkeypatch.setattr(build_hooks._build_meta, "build_wheel", build)
275+
monkeypatch.setattr(build_hooks, "record_build_config", lambda key: events.append(("record", key)))
276+
277+
wheel_name = build_hooks.build_wheel("dist", {"debug": True}, "metadata")
278+
279+
assert wheel_name == "cuda_core.whl"
280+
assert events == [("prepare", True), "build", ("record", "cu13-gnu-debug")]
281+
282+
@pytest.mark.agent_authored(model="gpt-5.6-sol")
283+
def test_editable_records_default_config_after_patch(self, monkeypatch):
284+
events = []
285+
expected_debug = sys.platform != "win32"
258286
toolchain = "msvc" if sys.platform == "win32" else "gnu"
259-
build_hooks.record_build_config(False)
260-
assert stamp.read_text().strip() == f"cu13-{toolchain}-opt"
287+
expected_key = f"cu13-{toolchain}-{'debug' if expected_debug else 'opt'}"
288+
289+
def prepare(debug):
290+
events.append(("prepare", debug))
291+
return expected_key
292+
293+
monkeypatch.setattr(build_hooks, "_build_cuda_core", prepare)
294+
monkeypatch.setattr(
295+
build_hooks._build_meta,
296+
"build_editable",
297+
lambda *_args: events.append("build") or "cuda_core.whl",
298+
)
299+
monkeypatch.setattr(
300+
build_hooks,
301+
"_add_cython_include_paths_to_pth",
302+
lambda wheel_path: events.append(("patch", wheel_path)),
303+
)
304+
monkeypatch.setattr(build_hooks, "record_build_config", lambda key: events.append(("record", key)))
305+
306+
wheel_name = build_hooks.build_editable("dist")
307+
308+
assert wheel_name == "cuda_core.whl"
309+
assert events == [
310+
("prepare", expected_debug),
311+
"build",
312+
("patch", os.path.join("dist", "cuda_core.whl")),
313+
("record", expected_key),
314+
]
315+
316+
@pytest.mark.agent_authored(model="gpt-5.6-sol")
317+
def test_failed_wheel_build_does_not_record_config(self, monkeypatch):
318+
monkeypatch.setattr(
319+
build_hooks,
320+
"_build_cuda_core",
321+
lambda debug: f"cu13-gnu-{'debug' if debug else 'opt'}",
322+
)
323+
324+
def fail(*args):
325+
raise RuntimeError("wheel build failed")
326+
327+
monkeypatch.setattr(build_hooks._build_meta, "build_wheel", fail)
328+
monkeypatch.setattr(
329+
build_hooks,
330+
"record_build_config",
331+
lambda _key: pytest.fail("failed build must not be stamped"),
332+
)
333+
334+
with pytest.raises(RuntimeError, match="wheel build failed"):
335+
build_hooks.build_wheel("dist", {"debug": True}, "metadata")
336+
337+
@pytest.mark.agent_authored(model="gpt-5.6-sol")
338+
def test_failed_editable_patch_does_not_record_config(self, monkeypatch):
339+
monkeypatch.setattr(
340+
build_hooks,
341+
"_build_cuda_core",
342+
lambda debug: f"cu13-gnu-{'debug' if debug else 'opt'}",
343+
)
344+
monkeypatch.setattr(build_hooks._build_meta, "build_editable", lambda *_args: "cuda_core.whl")
345+
346+
def fail(wheel_path):
347+
raise RuntimeError("editable patch failed")
348+
349+
monkeypatch.setattr(build_hooks, "_add_cython_include_paths_to_pth", fail)
350+
monkeypatch.setattr(
351+
build_hooks,
352+
"record_build_config",
353+
lambda _key: pytest.fail("unpatched editable build must not be stamped"),
354+
)
355+
356+
with pytest.raises(RuntimeError, match="editable patch failed"):
357+
build_hooks.build_editable("dist", {"debug": True}, "metadata")
261358

262359

263360
def _capture_cythonize_build_dir(monkeypatch, cuda_major):

0 commit comments

Comments
 (0)