Skip to content

Commit

Permalink
mingw: make get_msvcr() a noop
Browse files Browse the repository at this point in the history
This was added back in the day to make mingw use the same CRT as CPython
(https://bugs.python.org/issue870382), but at least with newer mingw-w64 and
ucrt switching the CRT at "runtime" isn't supported anymore. To build a
compatible extension you have to use a ucrt mingw-w64 build, so things match
up and link against the same CRT.

CPython 3.5+ uses ucrt (see https://wiki.python.org/moin/WindowsCompilers), so
anything besides that is no longer relevant, which only leaves vcruntime140.

Since it's not clear what linking against vcruntime140 solves, and there have
been reports of it needing to be patched out:

* #4101
* pypa/distutils#204 (comment)

let's just make it return nothing. Keep get_msvcr() around for now to avoid breaking
code which patched it.

Fixes #204
  • Loading branch information
lazka committed Jul 21, 2024
1 parent 6a39d04 commit 943fc82
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 80 deletions.
40 changes: 2 additions & 38 deletions distutils/cygwinccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import copy
import os
import pathlib
import re
import shlex
import sys
import warnings
from subprocess import check_output

from ._collections import RangeMap
from .errors import (
CCompilerError,
CompileError,
Expand All @@ -26,42 +24,10 @@
from .unixccompiler import UnixCCompiler
from .version import LooseVersion, suppress_known_deprecation

_msvcr_lookup = RangeMap.left(
{
# MSVC 7.0
1300: ['msvcr70'],
# MSVC 7.1
1310: ['msvcr71'],
# VS2005 / MSVC 8.0
1400: ['msvcr80'],
# VS2008 / MSVC 9.0
1500: ['msvcr90'],
# VS2010 / MSVC 10.0
1600: ['msvcr100'],
# VS2012 / MSVC 11.0
1700: ['msvcr110'],
# VS2013 / MSVC 12.0
1800: ['msvcr120'],
# VS2015 / MSVC 14.0
1900: ['vcruntime140'],
2000: RangeMap.undefined_value,
},
)


def get_msvcr():
"""Include the appropriate MSVC runtime library if Python was built
with MSVC 7.0 or later.
"""
match = re.search(r'MSC v\.(\d{4})', sys.version)
try:
msc_ver = int(match.group(1))
except AttributeError:
return []
try:
return _msvcr_lookup[msc_ver]
except KeyError:
raise ValueError(f"Unknown MS Compiler version {msc_ver} ")
"""No longer needed, but kept for backward compatibility."""
return []


_runtime_library_dirs_msg = (
Expand Down Expand Up @@ -109,8 +75,6 @@ def __init__(self, verbose=False, dry_run=False, force=False):
linker_so=(f'{self.linker_dll} -mcygwin {shared_option}'),
)

# Include the appropriate MSVC runtime library if Python was built
# with MSVC 7.0 or later.
self.dll_libraries = get_msvcr()

@property
Expand Down
42 changes: 0 additions & 42 deletions distutils/tests/test_cygwinccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,50 +71,8 @@ def test_check_config_h(self):
assert check_config_h()[0] == CONFIG_H_OK

def test_get_msvcr(self):
# []
sys.version = (
'2.6.1 (r261:67515, Dec 6 2008, 16:42:21) '
'\n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]'
)
assert get_msvcr() == []

# MSVC 7.0
sys.version = (
'2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1300 32 bits (Intel)]'
)
assert get_msvcr() == ['msvcr70']

# MSVC 7.1
sys.version = (
'2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bits (Intel)]'
)
assert get_msvcr() == ['msvcr71']

# VS2005 / MSVC 8.0
sys.version = (
'2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1400 32 bits (Intel)]'
)
assert get_msvcr() == ['msvcr80']

# VS2008 / MSVC 9.0
sys.version = (
'2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1500 32 bits (Intel)]'
)
assert get_msvcr() == ['msvcr90']

sys.version = (
'3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 18:46:30) '
'[MSC v.1929 32 bit (Intel)]'
)
assert get_msvcr() == ['vcruntime140']

# unknown
sys.version = (
'2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.2000 32 bits (Intel)]'
)
with pytest.raises(ValueError):
get_msvcr()

@pytest.mark.skipif('sys.platform != "cygwin"')
def test_dll_libraries_not_none(self):
from distutils.cygwinccompiler import CygwinCCompiler
Expand Down

0 comments on commit 943fc82

Please sign in to comment.