Skip to content
14 changes: 13 additions & 1 deletion distutils/compilers/C/msvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import contextlib
import os
from pathlib import Path
import subprocess
import tempfile
import unittest.mock as mock
import warnings
from collections.abc import Iterable
Expand Down Expand Up @@ -551,7 +553,17 @@ def link(
self.mkpath(output_dir)
try:
log.debug('Executing "%s" %s', self.linker, ' '.join(ld_args))
self.spawn([self.linker] + ld_args)
# The maximum length of the commandline is 32,767
# we must pass in the arguments through a file if it is longer
# https://learn.microsoft.com/en-us/cpp/build/reference/linking?view=msvc-170#linker-command-files
# https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#parameters
Comment thread
jaraco marked this conversation as resolved.
Outdated
if len(subprocess.list2cmdline(ld_args)) > 32767:
Comment thread
astrelsky marked this conversation as resolved.
Outdated
with tempfile.TemporaryDirectory() as tmpdir:
Comment thread
astrelsky marked this conversation as resolved.
Outdated
cmdline = Path(tmpdir) / 'cmdline.txt'
cmdline.write_text('\n'.join(f'"{item}"' for item in ld_args))
Comment thread
astrelsky marked this conversation as resolved.
Outdated
self.spawn([self.linker, '@'+str(cmdline)])
Comment thread
astrelsky marked this conversation as resolved.
Outdated
else:
self.spawn([self.linker] + ld_args)
except DistutilsExecError as msg:
raise LinkError(msg)
else:
Expand Down