Skip to content

Commit

Permalink
distutils: remove checks for ancient gcc/binutils
Browse files Browse the repository at this point in the history
The versions checked here are 20 years old. Also dllwrap
has started to emit a deprecation warning in the latest release
spamming the build logs.

Fixes python#54
  • Loading branch information
lazka committed Nov 10, 2021
1 parent ac9f30a commit a69be8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 144 deletions.
123 changes: 14 additions & 109 deletions Lib/distutils/cygwinccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@
import os
import sys
import copy
import re

from distutils.unixccompiler import UnixCCompiler
from distutils.file_util import write_file
from distutils.errors import (DistutilsExecError, CCompilerError,
CompileError, UnknownFileError)
from distutils.version import LooseVersion
from distutils.spawn import find_executable
from subprocess import Popen, PIPE, check_output
from subprocess import Popen, check_output

def get_msvcr():
"""Include the appropriate MSVC runtime library if Python was built
Expand Down Expand Up @@ -115,33 +113,8 @@ def __init__(self, verbose=0, dry_run=0, force=0):
self.cc = os.environ.get('CC', 'gcc')
self.cxx = os.environ.get('CXX', 'g++')

if ('gcc' in self.cc): # Start gcc workaround
self.gcc_version, self.ld_version, self.dllwrap_version = \
get_versions()
self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
(self.gcc_version,
self.ld_version,
self.dllwrap_version) )

# ld_version >= "2.10.90" and < "2.13" should also be able to use
# gcc -mdll instead of dllwrap
# Older dllwraps had own version numbers, newer ones use the
# same as the rest of binutils ( also ld )
# dllwrap 2.10.90 is buggy
if self.ld_version >= "2.10.90":
self.linker_dll = self.cc
else:
self.linker_dll = "dllwrap"

# ld_version >= "2.13" support -shared so use it instead of
# -mdll -static
if self.ld_version >= "2.13":
shared_option = "-shared"
else:
shared_option = "-mdll -static"
else: # Assume linker is up to date
self.linker_dll = self.cc
shared_option = "-shared"
self.linker_dll = self.cc
shared_option = "-shared"

self.set_executables(compiler='%s -mcygwin -O -Wall' % self.cc,
compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
Expand All @@ -150,17 +123,9 @@ def __init__(self, verbose=0, dry_run=0, force=0):
linker_so=('%s -mcygwin %s' %
(self.linker_dll, shared_option)))

# cygwin and mingw32 need different sets of libraries
if ('gcc' in self.cc and self.gcc_version == "2.91.57"):
# cygwin shouldn't need msvcrt, but without the dlls will crash
# (gcc version 2.91.57) -- perhaps something about initialization
self.dll_libraries=["msvcrt"]
self.warn(
"Consider upgrading to a newer version of gcc")
else:
# Include the appropriate MSVC runtime library if Python was built
# with MSVC 7.0 or later.
self.dll_libraries = get_msvcr()
# Include the appropriate MSVC runtime library if Python was built
# with MSVC 7.0 or later.
self.dll_libraries = get_msvcr()

def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
"""Compiles the source by spawning GCC and windres if needed."""
Expand Down Expand Up @@ -244,24 +209,17 @@ def link(self, target_desc, objects, output_filename, output_dir=None,

# next add options for def-file and to creating import libraries

# dllwrap uses different options than gcc/ld
if self.linker_dll == "dllwrap":
extra_preargs.extend(["--output-lib", lib_file])
# for dllwrap we have to use a special option
extra_preargs.extend(["--def", def_file])
# we use gcc/ld here and can be sure ld is >= 2.9.10
else:
# doesn't work: bfd_close build\...\libfoo.a: Invalid operation
#extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
# for gcc/ld the def-file is specified as any object files
objects.append(def_file)
# doesn't work: bfd_close build\...\libfoo.a: Invalid operation
#extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
# for gcc/ld the def-file is specified as any object files
objects.append(def_file)

#end: if ((export_symbols is not None) and
# (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):

# who wants symbols and a many times larger output file
# should explicitly switch the debug mode on
# otherwise we let dllwrap/ld strip the output file
# otherwise we let ld strip the output file
# (On my machine: 10KiB < stripped_file < ??100KiB
# unstripped_file = stripped_file + XXX KiB
# ( XXX=254 for a typical python extension))
Expand Down Expand Up @@ -314,19 +272,7 @@ def __init__(self, verbose=0, dry_run=0, force=0):

CygwinCCompiler.__init__ (self, verbose, dry_run, force)

# ld_version >= "2.13" support -shared so use it instead of
# -mdll -static
if ('gcc' in self.cc and self.ld_version < "2.13"):
shared_option = "-mdll -static"
else:
shared_option = "-shared"

# A real mingw32 doesn't need to specify a different entry point,
# but cygwin 2.91.57 in no-cygwin-mode needs it.
if ('gcc' in self.cc and self.gcc_version <= "2.91.57"):
entry_point = '--entry _DllMain@12'
else:
entry_point = ''
shared_option = "-shared"

if is_cygwincc(self.cc):
raise CCompilerError(
Expand All @@ -336,9 +282,8 @@ def __init__(self, verbose=0, dry_run=0, force=0):
compiler_so='%s -mdll -O2 -Wall' % self.cc,
compiler_cxx='%s -O2 -Wall' % self.cxx,
linker_exe='%s' % self.cc,
linker_so='%s %s %s'
% (self.linker_dll, shared_option,
entry_point))
linker_so='%s %s'
% (self.linker_dll, shared_option))
# Maybe we should also append -mthreads, but then the finished
# dlls need another dll (mingwm10.dll see Mingw32 docs)
# (-mthreads: Support thread-safe exception handling on `Mingw32')
Expand Down Expand Up @@ -405,46 +350,6 @@ def check_config_h():
return (CONFIG_H_UNCERTAIN,
"couldn't read '%s': %s" % (fn, exc.strerror))

RE_VERSION = re.compile(br'[\D\s]*(\d+\.\d+(\.\d+)*)[\D\s]*')

def _find_exe_version(cmd):
"""Find the version of an executable by running `cmd` in the shell.
If the command is not found, or the output does not match
`RE_VERSION`, returns None.
"""
executable = cmd.split()[0]
if find_executable(executable) is None:
return None
from subprocess import Popen, PIPE
out = Popen(cmd, shell=True, stdout=PIPE).stdout
try:
out_string = out.read()
finally:
out.close()
result = RE_VERSION.search(out_string)
if result is None:
return None
# LooseVersion works with strings
# so we need to decode our bytes
return LooseVersion(result.group(1).decode())

def get_versions():
""" Try to find out the versions of gcc, ld and dllwrap.
If not possible it returns None for it.
"""
gcc = os.environ.get('CC') or 'gcc'
ld = 'ld'
out = Popen(gcc+' --print-prog-name ld', shell=True, stdout=PIPE).stdout
try:
ld = test=str(out.read(),encoding='utf-8').strip()
finally:
out.close()
dllwrap = os.environ.get('DLLWRAP') or 'dllwrap'
# MinGW64 doesn't have i686-w64-mingw32-ld, so instead we ask gcc.
commands = [gcc+' -dumpversion', ld+' -v', dllwrap+' --version']
return tuple([_find_exe_version(cmd) for cmd in commands])

def is_cygwincc(cc):
'''Try to determine if the compiler that would be used is from cygwin.'''
Expand Down
36 changes: 1 addition & 35 deletions Lib/distutils/tests/test_cygwinccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from distutils import cygwinccompiler
from distutils.cygwinccompiler import (check_config_h,
CONFIG_H_OK, CONFIG_H_NOTOK,
CONFIG_H_UNCERTAIN, get_versions,
CONFIG_H_UNCERTAIN,
get_msvcr)
from distutils.tests import support

Expand Down Expand Up @@ -81,40 +81,6 @@ def test_check_config_h(self):
self.write_file(self.python_h, 'xxx __GNUC__ xxx')
self.assertEqual(check_config_h()[0], CONFIG_H_OK)

def test_get_versions(self):

# get_versions calls distutils.spawn.find_executable on
# 'gcc', 'ld' and 'dllwrap'
self.assertEqual(get_versions(), (None, None, None))

# Let's fake we have 'gcc' and it returns '3.4.5'
self._exes['gcc'] = b'gcc (GCC) 3.4.5 (mingw special)\nFSF'
res = get_versions()
self.assertEqual(str(res[0]), '3.4.5')

# and let's see what happens when the version
# doesn't match the regular expression
# (\d+\.\d+(\.\d+)*)
self._exes['gcc'] = b'very strange output'
res = get_versions()
self.assertEqual(res[0], None)

# same thing for ld
self._exes['ld'] = b'GNU ld version 2.17.50 20060824'
res = get_versions()
self.assertEqual(str(res[1]), '2.17.50')
self._exes['ld'] = b'@(#)PROGRAM:ld PROJECT:ld64-77'
res = get_versions()
self.assertEqual(res[1], None)

# and dllwrap
self._exes['dllwrap'] = b'GNU dllwrap 2.17.50 20060824\nFSF'
res = get_versions()
self.assertEqual(str(res[2]), '2.17.50')
self._exes['dllwrap'] = b'Cheese Wrap'
res = get_versions()
self.assertEqual(res[2], None)

def test_get_msvcr(self):

# none
Expand Down

0 comments on commit a69be8f

Please sign in to comment.