Skip to content

Commit dde3ad3

Browse files
committed
Extract logic around the mappings into a function to compute the spec.
1 parent 93a4dc9 commit dde3ad3

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

distutils/_msvccompiler.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,41 @@ def _find_exe(exe, paths=None):
178178
return exe
179179

180180

181-
PLAT_TO_VCVARS = (
182-
{
183-
# Use the native MSVC host if the host platform would need expensive
184-
# emulation for x86.
185-
'win32': 'arm64_x86',
186-
'win-amd64': 'arm64_amd64',
187-
'win-arm32': 'arm64_arm',
188-
'win-arm64': 'arm64',
189-
}
190-
if get_host_platform() == "win-arm64"
191-
else {
192-
# Always cross-compile from x86 to work with the lighter-weight MSVC
193-
# installs that do not include native 64-bit tools.
194-
'win32': 'x86',
195-
'win-amd64': 'x86_amd64',
196-
'win-arm32': 'x86_arm',
197-
'win-arm64': 'x86_arm64',
198-
}
199-
)
200-
"""
201-
Maps get_platform() results to values expected by vcvarsall.bat.
202-
"""
181+
_vcvars_names = {
182+
'win32': 'x86',
183+
'win-amd64': 'amd64',
184+
'win-arm32': 'arm',
185+
'win-arm64': 'arm64',
186+
}
187+
188+
189+
def _get_vcvars_spec(host_platform, platform):
190+
"""
191+
Given a host platform and platform, determine the spec for vcvarsall.
192+
193+
Uses the native MSVC host if the host platform would need expensive
194+
emulation for x86.
195+
196+
>>> _get_vcvars_spec('win-arm64', 'win32')
197+
'arm64_x86'
198+
>>> _get_vcvars_spec('win-arm64', 'win-amd64')
199+
'arm64_amd64'
200+
201+
Always cross-compile from x86 to work with the lighter-weight MSVC
202+
installs that do not include native 64-bit tools.
203+
204+
>>> _get_vcvars_spec('win32', 'win32')
205+
'x86'
206+
>>> _get_vcvars_spec('win-arm32', 'win-arm32')
207+
'x86_arm'
208+
>>> _get_vcvars_spec('win-amd64', 'win-arm64')
209+
'x86_arm64'
210+
"""
211+
if host_platform != 'win-arm64':
212+
host_platform = 'win32'
213+
vc_hp = _vcvars_names[host_platform]
214+
vc_plat = _vcvars_names[platform]
215+
return vc_hp if vc_hp == vc_plat else f'{vc_hp}_{vc_plat}'
203216

204217

205218
class MSVCCompiler(CCompiler):
@@ -255,13 +268,12 @@ def initialize(self, plat_name=None):
255268
if plat_name is None:
256269
plat_name = get_platform()
257270
# sanity check for platforms to prevent obscure errors later.
258-
if plat_name not in PLAT_TO_VCVARS:
271+
if plat_name not in _vcvars_names:
259272
raise DistutilsPlatformError(
260-
f"--plat-name must be one of {tuple(PLAT_TO_VCVARS)}"
273+
f"--plat-name must be one of {tuple(_vcvars_names)}"
261274
)
262275

263-
# Get the vcvarsall.bat spec for the requested platform.
264-
plat_spec = PLAT_TO_VCVARS[plat_name]
276+
plat_spec = _get_vcvars_spec(get_host_platform(), get_platform())
265277

266278
vc_env = _get_vc_env(plat_spec)
267279
if not vc_env:

0 commit comments

Comments
 (0)