Skip to content

Commit b995b24

Browse files
authored
Fix long MSVC linker commands on Windows (#2517)
Use a temporary response file when setuptools emits an oversized link.exe command so Windows builds with many object files can complete. Made-with: Cursor
1 parent ba59def commit b995b24

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

setup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ast
99
import glob
1010
import shutil
11+
import tempfile
1112
from pathlib import Path
1213
from typing import Literal, Optional
1314
from packaging.version import parse, Version
@@ -637,6 +638,35 @@ def __init__(self, *args, **kwargs) -> None:
637638

638639
super().__init__(*args, **kwargs)
639640

641+
def build_extensions(self) -> None:
642+
original_spawn = None
643+
if sys.platform == "win32" and self.compiler.compiler_type == "msvc":
644+
original_spawn = self.compiler.spawn
645+
646+
def spawn(cmd):
647+
if not cmd or Path(str(cmd[0])).name.lower() != "link.exe":
648+
return original_spawn(cmd)
649+
cmd = [str(arg) for arg in cmd]
650+
if len(subprocess.list2cmdline(cmd)) <= 32767:
651+
return original_spawn(cmd)
652+
# Temporary workaround adapted from https://github.com/pypa/distutils/pull/406
653+
# until setuptools/distutils ships response-file handling for long MSVC links.
654+
with tempfile.TemporaryDirectory() as tmpdir:
655+
rsp_path = Path(tmpdir) / "cmdline.txt"
656+
rsp_path.write_text(
657+
"\n".join(subprocess.list2cmdline([arg]) for arg in cmd[1:]) + "\n",
658+
encoding="ascii",
659+
)
660+
return original_spawn([cmd[0], f"@{rsp_path}"])
661+
662+
self.compiler.spawn = spawn
663+
664+
try:
665+
super().build_extensions()
666+
finally:
667+
if original_spawn is not None:
668+
self.compiler.spawn = original_spawn
669+
640670

641671
# Build install_requires based on platform
642672
if ROCM_BACKEND == "triton":

0 commit comments

Comments
 (0)