From ba7bfeee3fd40ea001b74323035c83b01bf22f0f Mon Sep 17 00:00:00 2001 From: CXSforHPU <19511928573@163.com> Date: Tue, 24 Nov 2026 15:18:45 +0800 Subject: [PATCH] [tools] fix WinError 206 caused by overlong command lines on Windows Windows CreateProcess rejects a command line longer than 32767 characters, so link and archive steps failed with "WinError 206: filename or extension too long" on large RT-Thread projects. Win32Spawn now spills an overlong GCC command line into an @response file, which covers every build action instead of only LINKCOM/ARCOM. Backslashes are doubled and arguments containing whitespace are quoted so the @file reproduces the original argument list. The misleading "check Toolchains PATH" hint is also gone: the real exception is printed now. --- tools/building.py | 4 ++-- tools/win32spawn.py | 52 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/tools/building.py b/tools/building.py index 7abedd4f27e6..5d550e62ef43 100644 --- a/tools/building.py +++ b/tools/building.py @@ -328,7 +328,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [ # patch for win32 spawn if env['PLATFORM'] == 'win32': - win32_spawn = Win32Spawn() + win32_spawn = Win32Spawn(rtconfig.PLATFORM == 'gcc') win32_spawn.env = env env['SPAWN'] = win32_spawn.spawn @@ -473,7 +473,7 @@ def PrepareModuleBuilding(env, root_directory, bsp_directory): # patch for win32 spawn if env['PLATFORM'] == 'win32': - win32_spawn = Win32Spawn() + win32_spawn = Win32Spawn(rtconfig.PLATFORM == 'gcc') win32_spawn.env = env env['SPAWN'] = win32_spawn.spawn diff --git a/tools/win32spawn.py b/tools/win32spawn.py index 961e08ddfeab..873105e4d8f4 100644 --- a/tools/win32spawn.py +++ b/tools/win32spawn.py @@ -24,8 +24,32 @@ import os import subprocess +import tempfile + +# CreateProcess rejects a command line longer than 32767 characters. +_WIN32_CMDLINE_LIMIT = 32000 + +def _response_file_arg(arg): + # GCC's @file reader (buildargv) treats '\' as an escape character, + # unlike the Windows command line, so backslashes must be doubled. + arg = arg.replace('\\', '\\\\') + # Arguments SCons already quoted keep their own grouping. + if '"' not in arg and (' ' in arg or '\t' in arg): + arg = '"' + arg + '"' + return arg + +def _write_response_file(args): + fd, path = tempfile.mkstemp(prefix='rtthread-', suffix='.rsp') + with os.fdopen(fd, 'w') as f: + # A space separator matches the original command line: whitespace + # inside quotes stays part of the argument, as it does on Windows. + f.write(' '.join(_response_file_arg(arg) for arg in args)) + return path class Win32Spawn: + def __init__(self, gnu_response_file=False): + self.gnu_response_file = gnu_response_file + def spawn(self, sh, escape, cmd, args, env): # deal with the cmd build-in commands which cannot be used in # subprocess.Popen @@ -41,6 +65,11 @@ def spawn(self, sh, escape, cmd, args, env): newargs = ' '.join(args[1:]) cmdline = cmd + " " + newargs + rsp = None + if self.gnu_response_file and len(cmdline) > _WIN32_CMDLINE_LIMIT: + rsp = _write_response_file(args[1:]) + cmdline = cmd + ' @"' + rsp + '"' + # Make sure the env is constructed by strings _e = dict([(k, str(v)) for k, v in env.items()]) @@ -51,15 +80,20 @@ def spawn(self, sh, escape, cmd, args, env): os.environ['PATH'] = _e['PATH'] try: - proc = subprocess.Popen(cmdline, env=_e, shell=False) - except Exception as e: - print('Error in calling command:' + cmdline.split(' ')[0]) - print('Exception: ' + os.strerror(e.errno)) - if (os.strerror(e.errno) == "No such file or directory"): - print ("\nPlease check Toolchains PATH setting.\n") + try: + proc = subprocess.Popen(cmdline, env=_e, shell=False) + except Exception as e: + print('Error in calling command:' + cmdline.split(' ')[0]) + print('Exception: ' + str(e)) + if e.errno == 2 and getattr(e, 'winerror', None) != 206: + print ("\nPlease check Toolchains PATH setting.\n") - return e.errno + return e.errno + return proc.wait() finally: os.environ['PATH'] = old_path - - return proc.wait() + if rsp is not None: + try: + os.remove(rsp) + except OSError: + pass