[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -5,7 +5,6 @@ handles the Cygwin port of the GNU C compiler to Windows. It also contains
the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
cygwin in no-cygwin mode).
"""
# problems:
#
# * if you use a msvc compiled python version (1.5.2)
@ -46,19 +45,25 @@ cygwin in no-cygwin mode).
# (ld supports -shared)
# * llvm-mingw with Clang 11 works
# (lld supports -shared)
from __future__ import annotations
import os
import sys
import copy
from subprocess import Popen, PIPE, check_output
import os
import re
import sys
from subprocess import check_output
from subprocess import PIPE
from subprocess import Popen
from distutils.unixccompiler import UnixCCompiler
from distutils.errors import CCompilerError
from distutils.errors import CompileError
from distutils.errors import DistutilsExecError
from distutils.errors import UnknownFileError
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 distutils.unixccompiler import UnixCCompiler
from distutils.version import LooseVersion
def get_msvcr():
"""Include the appropriate MSVC runtime library if Python was built
@ -66,7 +71,7 @@ def get_msvcr():
"""
msc_pos = sys.version.find('MSC v.')
if msc_pos != -1:
msc_ver = sys.version[msc_pos+6:msc_pos+10]
msc_ver = sys.version[msc_pos + 6:msc_pos + 10]
if msc_ver == '1300':
# MSVC 7.0
return ['msvcr70']
@ -83,79 +88,90 @@ def get_msvcr():
# VS2010 / MSVC 10.0
return ['msvcr100']
else:
raise ValueError("Unknown MS Compiler version %s " % msc_ver)
raise ValueError('Unknown MS Compiler version %s ' % msc_ver)
class CygwinCCompiler(UnixCCompiler):
""" Handles the Cygwin port of the GNU C compiler to Windows.
"""
compiler_type = 'cygwin'
obj_extension = ".o"
static_lib_extension = ".a"
shared_lib_extension = ".dll"
static_lib_format = "lib%s%s"
shared_lib_format = "%s%s"
exe_extension = ".exe"
obj_extension = '.o'
static_lib_extension = '.a'
shared_lib_extension = '.dll'
static_lib_format = 'lib%s%s'
shared_lib_format = '%s%s'
exe_extension = '.exe'
def __init__(self, verbose=0, dry_run=0, force=0):
UnixCCompiler.__init__(self, verbose, dry_run, force)
status, details = check_config_h()
self.debug_print("Python's GCC status: %s (details: %s)" %
(status, details))
self.debug_print(
"Python's GCC status: %s (details: %s)" %
(status, details),
)
if status is not CONFIG_H_OK:
self.warn(
"Python's pyconfig.h doesn't seem to support your compiler. "
"Reason: %s. "
"Compiling may fail because of undefined preprocessor macros."
% details)
'Reason: %s. '
'Compiling may fail because of undefined preprocessor macros.'
% details,
)
self.cc = os.environ.get('CC', 'gcc')
self.cxx = os.environ.get('CXX', 'g++')
if ('gcc' in self.cc): # Start gcc workaround
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) )
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":
if self.ld_version >= '2.10.90':
self.linker_dll = self.cc
else:
self.linker_dll = "dllwrap"
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"
if self.ld_version >= '2.13':
shared_option = '-shared'
else:
shared_option = "-mdll -static"
else: # Assume linker is up to date
shared_option = '-mdll -static'
else: # Assume linker is up to date
self.linker_dll = self.cc
shared_option = "-shared"
shared_option = '-shared'
self.set_executables(compiler='%s -mcygwin -O -Wall' % self.cc,
compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
linker_exe='%s -mcygwin' % self.cc,
linker_so=('%s -mcygwin %s' %
(self.linker_dll, shared_option)))
self.set_executables(
compiler='%s -mcygwin -O -Wall' % self.cc,
compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
linker_exe='%s -mcygwin' % self.cc,
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"):
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.dll_libraries = ['msvcrt']
self.warn(
"Consider upgrading to a newer version of gcc")
'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.
@ -166,20 +182,24 @@ class CygwinCCompiler(UnixCCompiler):
if ext == '.rc' or ext == '.res':
# gcc needs '.res' and '.rc' compiled to object files !!!
try:
self.spawn(["windres", "-i", src, "-o", obj])
self.spawn(['windres', '-i', src, '-o', obj])
except DistutilsExecError as msg:
raise CompileError(msg)
else: # for other files use the C-compiler
else: # for other files use the C-compiler
try:
self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
extra_postargs)
self.spawn(
self.compiler_so + cc_args + [src, '-o', obj] +
extra_postargs,
)
except DistutilsExecError as msg:
raise CompileError(msg)
def link(self, target_desc, objects, output_filename, output_dir=None,
libraries=None, library_dirs=None, runtime_library_dirs=None,
export_symbols=None, debug=0, extra_preargs=None,
extra_postargs=None, build_temp=None, target_lang=None):
def link(
self, target_desc, objects, output_filename, output_dir=None,
libraries=None, library_dirs=None, runtime_library_dirs=None,
export_symbols=None, debug=0, extra_preargs=None,
extra_postargs=None, build_temp=None, target_lang=None,
):
"""Link the objects."""
# use separate copies, so we can modify the lists
extra_preargs = copy.copy(extra_preargs or [])
@ -192,7 +212,7 @@ class CygwinCCompiler(UnixCCompiler):
# handle export symbols by creating a def-file
# with executables this only works with gcc/ld as linker
if ((export_symbols is not None) and
(target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
(target_desc != self.EXECUTABLE or self.linker_dll == 'gcc')):
# (The linker doesn't do anything if output is up-to-date.
# So it would probably better to check if we really need this,
# but for this we had to insert some unchanged parts of
@ -204,28 +224,32 @@ class CygwinCCompiler(UnixCCompiler):
temp_dir = os.path.dirname(objects[0])
# name of dll to give the helper files the same base name
(dll_name, dll_extension) = os.path.splitext(
os.path.basename(output_filename))
os.path.basename(output_filename),
)
# generate the filenames for these files
def_file = os.path.join(temp_dir, dll_name + ".def")
lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
def_file = os.path.join(temp_dir, dll_name + '.def')
lib_file = os.path.join(temp_dir, 'lib' + dll_name + '.a')
# Generate .def file
contents = [
"LIBRARY %s" % os.path.basename(output_filename),
"EXPORTS"]
'LIBRARY %s' % os.path.basename(output_filename),
'EXPORTS',
]
for sym in export_symbols:
contents.append(sym)
self.execute(write_file, (def_file, contents),
"writing %s" % def_file)
self.execute(
write_file, (def_file, contents),
'writing %s' % def_file,
)
# 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])
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])
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
@ -243,14 +267,16 @@ class CygwinCCompiler(UnixCCompiler):
# unstripped_file = stripped_file + XXX KiB
# ( XXX=254 for a typical python extension))
if not debug:
extra_preargs.append("-s")
extra_preargs.append('-s')
UnixCCompiler.link(self, target_desc, objects, output_filename,
output_dir, libraries, library_dirs,
runtime_library_dirs,
None, # export_symbols, we do this in our def-file
debug, extra_preargs, extra_postargs, build_temp,
target_lang)
UnixCCompiler.link(
self, target_desc, objects, output_filename,
output_dir, libraries, library_dirs,
runtime_library_dirs,
None, # export_symbols, we do this in our def-file
debug, extra_preargs, extra_postargs, build_temp,
target_lang,
)
# -- Miscellaneous methods -----------------------------------------
@ -262,21 +288,33 @@ class CygwinCCompiler(UnixCCompiler):
for src_name in source_filenames:
# use normcase to make sure '.rc' is really '.rc' and not '.RC'
base, ext = os.path.splitext(os.path.normcase(src_name))
if ext not in (self.src_extensions + ['.rc','.res']):
raise UnknownFileError("unknown file type '%s' (from '%s')" % \
(ext, src_name))
if ext not in (self.src_extensions + ['.rc', '.res']):
raise UnknownFileError(
"unknown file type '%s' (from '%s')" %
(ext, src_name),
)
if strip_dir:
base = os.path.basename (base)
base = os.path.basename(base)
if ext in ('.res', '.rc'):
# these need to be compiled to object files
obj_names.append (os.path.join(output_dir,
base + ext + self.obj_extension))
obj_names.append(
os.path.join(
output_dir,
base + ext + self.obj_extension,
),
)
else:
obj_names.append (os.path.join(output_dir,
base + self.obj_extension))
obj_names.append(
os.path.join(
output_dir,
base + self.obj_extension,
),
)
return obj_names
# the same as cygwin plus some additional parameters
class Mingw32CCompiler(CygwinCCompiler):
""" Handles the Mingw32 port of the GNU C compiler to Windows.
"""
@ -284,39 +322,44 @@ class Mingw32CCompiler(CygwinCCompiler):
def __init__(self, verbose=0, dry_run=0, force=0):
CygwinCCompiler.__init__ (self, verbose, dry_run, force)
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"
if ('gcc' in self.cc and self.ld_version < '2.13'):
shared_option = '-mdll -static'
else:
shared_option = "-shared"
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"):
if ('gcc' in self.cc and self.gcc_version <= '2.91.57'):
entry_point = '--entry _DllMain@12'
else:
entry_point = ''
if is_cygwincc(self.cc):
raise CCompilerError(
'Cygwin gcc cannot be used with --compiler=mingw32')
'Cygwin gcc cannot be used with --compiler=mingw32',
)
self.set_executables(compiler='%s -O -Wall' % self.cc,
compiler_so='%s -mdll -O -Wall' % self.cc,
compiler_cxx='%s -O -Wall' % self.cxx,
linker_exe='%s' % self.cc,
linker_so='%s %s %s'
% (self.linker_dll, shared_option,
entry_point))
self.set_executables(
compiler='%s -O -Wall' % self.cc,
compiler_so='%s -mdll -O -Wall' % self.cc,
compiler_cxx='%s -O -Wall' % self.cxx,
linker_exe='%s' % self.cc,
linker_so='%s %s %s'
% (
self.linker_dll, shared_option,
entry_point,
),
)
# 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')
# no additional libraries needed
self.dll_libraries=[]
self.dll_libraries = []
# Include the appropriate MSVC runtime library if Python was built
# with MSVC 7.0 or later.
@ -326,9 +369,11 @@ class Mingw32CCompiler(CygwinCCompiler):
# default, we should at least warn the user if he is using an unmodified
# version.
CONFIG_H_OK = "ok"
CONFIG_H_NOTOK = "not ok"
CONFIG_H_UNCERTAIN = "uncertain"
CONFIG_H_OK = 'ok'
CONFIG_H_NOTOK = 'not ok'
CONFIG_H_UNCERTAIN = 'uncertain'
def check_config_h():
"""Check if the current Python installation appears amenable to building
@ -355,11 +400,11 @@ def check_config_h():
# if sys.version contains GCC then python was compiled with GCC, and the
# pyconfig.h file should be OK
if "GCC" in sys.version:
if 'GCC' in sys.version:
return CONFIG_H_OK, "sys.version mentions 'GCC'"
# Clang would also work
if "Clang" in sys.version:
if 'Clang' in sys.version:
return CONFIG_H_OK, "sys.version mentions 'Clang'"
# let's see if __GNUC__ is mentioned in python.h
@ -367,18 +412,22 @@ def check_config_h():
try:
config_h = open(fn)
try:
if "__GNUC__" in config_h.read():
if '__GNUC__' in config_h.read():
return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
else:
return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
finally:
config_h.close()
except OSError as exc:
return (CONFIG_H_UNCERTAIN,
"couldn't read '%s': %s" % (fn, exc.strerror))
return (
CONFIG_H_UNCERTAIN,
"couldn't read '{}': {}".format(fn, exc.strerror),
)
RE_VERSION = re.compile(br'(\d+\.\d+(\.\d+)*)')
def _find_exe_version(cmd):
"""Find the version of an executable by running `cmd` in the shell.
@ -400,6 +449,7 @@ def _find_exe_version(cmd):
# 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.
@ -408,6 +458,7 @@ def get_versions():
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.'''
out_string = check_output([cc, '-dumpmachine'])