Skip to content

Commit

Permalink
Add support for building extensions using MinGW compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
naveen521kk committed Jan 9, 2024
1 parent fb5c570 commit 2ad8784
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .file_util import move_file
from .dir_util import mkpath
from ._modified import newer_group
from .util import split_quoted, execute
from .util import split_quoted, execute, is_mingw
from ._log import log


Expand Down Expand Up @@ -1076,6 +1076,10 @@ def get_default_compiler(osname=None, platform=None):
osname = os.name
if platform is None:
platform = sys.platform
# Mingw is a special case where sys.platform is 'win32' but we
# want to use the 'mingw32' compiler, so check it first
if is_mingw():
return 'mingw32'
for pattern, compiler in _default_compilers:
if (
re.match(pattern, platform) is not None
Expand Down
8 changes: 4 additions & 4 deletions distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ..sysconfig import get_config_h_filename
from .._modified import newer_group
from ..extension import Extension
from ..util import get_platform
from ..util import get_platform, is_mingw
from distutils._log import log
from . import py37compat

Expand Down Expand Up @@ -189,7 +189,7 @@ def finalize_options(self): # noqa: C901
# for extensions under windows use different directories
# for Release and Debug builds.
# also Python's library directory must be appended to library_dirs
if os.name == 'nt':
if os.name == 'nt' and not is_mingw():
# the 'libs' directory is for binary installs - we assume that
# must be the *native* platform. But we don't really support
# cross-compiling via a binary install anyway, so we let it go.
Expand Down Expand Up @@ -742,7 +742,7 @@ def get_libraries(self, ext): # noqa: C901
# pyconfig.h that MSVC groks. The other Windows compilers all seem
# to need it mentioned explicitly, though, so that's what we do.
# Append '_d' to the python import library on debug builds.
if sys.platform == "win32":
if sys.platform == "win32" and not is_mingw():
from .._msvccompiler import MSVCCompiler

if not isinstance(self.compiler, MSVCCompiler):
Expand Down Expand Up @@ -772,7 +772,7 @@ def get_libraries(self, ext): # noqa: C901
# A native build on an Android device or on Cygwin
if hasattr(sys, 'getandroidapilevel'):
link_libpython = True
elif sys.platform == 'cygwin':
elif sys.platform == 'cygwin' or is_mingw():
link_libpython = True
elif '_PYTHON_HOST_PLATFORM' in os.environ:
# We are cross-compiling for one of the relevant platforms
Expand Down
4 changes: 2 additions & 2 deletions distutils/cygwinccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_msvcr():
try:
msc_ver = int(match.group(1))
except AttributeError:
return
return []
try:
return _msvcr_lookup[msc_ver]
except KeyError:
Expand Down Expand Up @@ -277,7 +277,7 @@ def __init__(self, verbose=0, dry_run=0, force=0):

self.set_executables(
compiler='%s -O -Wall' % self.cc,
compiler_so='%s -mdll -O -Wall' % self.cc,
compiler_so='%s -shared -O -Wall' % self.cc,
compiler_cxx='%s -O -Wall' % self.cxx,
linker_exe='%s' % self.cc,
linker_so='{} {}'.format(self.linker_dll, shared_option),
Expand Down
9 changes: 6 additions & 3 deletions distutils/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .errors import DistutilsPlatformError
from . import py39compat
from ._functools import pass_none
from .util import is_mingw

IS_PYPY = '__pypy__' in sys.builtin_module_names

Expand Down Expand Up @@ -120,8 +121,10 @@ def get_python_inc(plat_specific=0, prefix=None):
"""
default_prefix = BASE_EXEC_PREFIX if plat_specific else BASE_PREFIX
resolved_prefix = prefix if prefix is not None else default_prefix
# MinGW imitates posix like layout, but os.name != posix
os_name = "posix" if is_mingw() else os.name
try:
getter = globals()[f'_get_python_inc_{os.name}']
getter = globals()[f'_get_python_inc_{os_name}']
except KeyError:
raise DistutilsPlatformError(
"I don't know where Python installs its C header files "
Expand Down Expand Up @@ -244,7 +247,7 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
else:
prefix = plat_specific and EXEC_PREFIX or PREFIX

if os.name == "posix":
if os.name == "posix" or is_mingw():
if plat_specific or standard_lib:
# Platform-specific modules (any module from a non-pure-Python
# module distribution) or standard Python library modules.
Expand Down Expand Up @@ -273,7 +276,7 @@ def customize_compiler(compiler): # noqa: C901
Mainly needed on Unix, so we can plug in the information that
varies across Unices and is stored in Python's Makefile.
"""
if compiler.compiler_type == "unix":
if compiler.compiler_type in ["unix", "cygwin", "mingw32"]:
if sys.platform == "darwin":
# Perform first-time customization of compiler-related
# config vars on OS X now that we know we need a compiler.
Expand Down
9 changes: 9 additions & 0 deletions distutils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,12 @@ def rfc822_escape(header):
suffix = indent if ends_in_newline else ""

return indent.join(lines) + suffix


def is_mingw():
"""Returns True if the current platform is mingw.
Python compiled with Mingw-w64 has sys.platform == 'win32' and
get_platform() starts with 'mingw'.
"""
return sys.platform == 'win32' and get_platform().startswith('mingw')

0 comments on commit 2ad8784

Please sign in to comment.