Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keewenaw
GitHub Repository: keewenaw/ethereum-wallet-cracker
Path: blob/main/test/lib/python3.9/site-packages/setuptools/_distutils/cygwinccompiler.py
4799 views
1
"""distutils.cygwinccompiler
2
3
Provides the CygwinCCompiler class, a subclass of UnixCCompiler that
4
handles the Cygwin port of the GNU C compiler to Windows. It also contains
5
the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
6
cygwin in no-cygwin mode).
7
"""
8
9
# problems:
10
#
11
# * if you use a msvc compiled python version (1.5.2)
12
# 1. you have to insert a __GNUC__ section in its config.h
13
# 2. you have to generate an import library for its dll
14
# - create a def-file for python??.dll
15
# - create an import library using
16
# dlltool --dllname python15.dll --def python15.def \
17
# --output-lib libpython15.a
18
#
19
# see also http://starship.python.net/crew/kernr/mingw32/Notes.html
20
#
21
# * We put export_symbols in a def-file, and don't use
22
# --export-all-symbols because it doesn't worked reliable in some
23
# tested configurations. And because other windows compilers also
24
# need their symbols specified this no serious problem.
25
#
26
# tested configurations:
27
#
28
# * cygwin gcc 2.91.57/ld 2.9.4/dllwrap 0.2.4 works
29
# (after patching python's config.h and for C++ some other include files)
30
# see also http://starship.python.net/crew/kernr/mingw32/Notes.html
31
# * mingw32 gcc 2.95.2/ld 2.9.4/dllwrap 0.2.4 works
32
# (ld doesn't support -shared, so we use dllwrap)
33
# * cygwin gcc 2.95.2/ld 2.10.90/dllwrap 2.10.90 works now
34
# - its dllwrap doesn't work, there is a bug in binutils 2.10.90
35
# see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html
36
# - using gcc -mdll instead dllwrap doesn't work without -static because
37
# it tries to link against dlls instead their import libraries. (If
38
# it finds the dll first.)
39
# By specifying -static we force ld to link against the import libraries,
40
# this is windows standard and there are normally not the necessary symbols
41
# in the dlls.
42
# *** only the version of June 2000 shows these problems
43
# * cygwin gcc 3.2/ld 2.13.90 works
44
# (ld supports -shared)
45
# * mingw gcc 3.2/ld 2.13 works
46
# (ld supports -shared)
47
# * llvm-mingw with Clang 11 works
48
# (lld supports -shared)
49
50
import os
51
import sys
52
import copy
53
import shlex
54
import warnings
55
from subprocess import check_output
56
57
from distutils.unixccompiler import UnixCCompiler
58
from distutils.file_util import write_file
59
from distutils.errors import (DistutilsExecError, CCompilerError,
60
CompileError, UnknownFileError)
61
from distutils.version import LooseVersion, suppress_known_deprecation
62
63
def get_msvcr():
64
"""Include the appropriate MSVC runtime library if Python was built
65
with MSVC 7.0 or later.
66
"""
67
msc_pos = sys.version.find('MSC v.')
68
if msc_pos != -1:
69
msc_ver = sys.version[msc_pos+6:msc_pos+10]
70
if msc_ver == '1300':
71
# MSVC 7.0
72
return ['msvcr70']
73
elif msc_ver == '1310':
74
# MSVC 7.1
75
return ['msvcr71']
76
elif msc_ver == '1400':
77
# VS2005 / MSVC 8.0
78
return ['msvcr80']
79
elif msc_ver == '1500':
80
# VS2008 / MSVC 9.0
81
return ['msvcr90']
82
elif msc_ver == '1600':
83
# VS2010 / MSVC 10.0
84
return ['msvcr100']
85
elif msc_ver == '1700':
86
# VS2012 / MSVC 11.0
87
return ['msvcr110']
88
elif msc_ver == '1800':
89
# VS2013 / MSVC 12.0
90
return ['msvcr120']
91
elif 1900 <= int(msc_ver) < 2000:
92
# VS2015 / MSVC 14.0
93
return ['ucrt', 'vcruntime140']
94
else:
95
raise ValueError("Unknown MS Compiler version %s " % msc_ver)
96
97
98
class CygwinCCompiler(UnixCCompiler):
99
""" Handles the Cygwin port of the GNU C compiler to Windows.
100
"""
101
compiler_type = 'cygwin'
102
obj_extension = ".o"
103
static_lib_extension = ".a"
104
shared_lib_extension = ".dll"
105
static_lib_format = "lib%s%s"
106
shared_lib_format = "%s%s"
107
exe_extension = ".exe"
108
109
def __init__(self, verbose=0, dry_run=0, force=0):
110
111
super().__init__(verbose, dry_run, force)
112
113
status, details = check_config_h()
114
self.debug_print("Python's GCC status: %s (details: %s)" %
115
(status, details))
116
if status is not CONFIG_H_OK:
117
self.warn(
118
"Python's pyconfig.h doesn't seem to support your compiler. "
119
"Reason: %s. "
120
"Compiling may fail because of undefined preprocessor macros."
121
% details)
122
123
self.cc = os.environ.get('CC', 'gcc')
124
self.cxx = os.environ.get('CXX', 'g++')
125
126
self.linker_dll = self.cc
127
shared_option = "-shared"
128
129
self.set_executables(compiler='%s -mcygwin -O -Wall' % self.cc,
130
compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
131
compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
132
linker_exe='%s -mcygwin' % self.cc,
133
linker_so=('%s -mcygwin %s' %
134
(self.linker_dll, shared_option)))
135
136
# Include the appropriate MSVC runtime library if Python was built
137
# with MSVC 7.0 or later.
138
self.dll_libraries = get_msvcr()
139
140
@property
141
def gcc_version(self):
142
# Older numpy dependend on this existing to check for ancient
143
# gcc versions. This doesn't make much sense with clang etc so
144
# just hardcode to something recent.
145
# https://github.com/numpy/numpy/pull/20333
146
warnings.warn(
147
"gcc_version attribute of CygwinCCompiler is deprecated. "
148
"Instead of returning actual gcc version a fixed value 11.2.0 is returned.",
149
DeprecationWarning,
150
stacklevel=2,
151
)
152
with suppress_known_deprecation():
153
return LooseVersion("11.2.0")
154
155
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
156
"""Compiles the source by spawning GCC and windres if needed."""
157
if ext == '.rc' or ext == '.res':
158
# gcc needs '.res' and '.rc' compiled to object files !!!
159
try:
160
self.spawn(["windres", "-i", src, "-o", obj])
161
except DistutilsExecError as msg:
162
raise CompileError(msg)
163
else: # for other files use the C-compiler
164
try:
165
self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
166
extra_postargs)
167
except DistutilsExecError as msg:
168
raise CompileError(msg)
169
170
def link(self, target_desc, objects, output_filename, output_dir=None,
171
libraries=None, library_dirs=None, runtime_library_dirs=None,
172
export_symbols=None, debug=0, extra_preargs=None,
173
extra_postargs=None, build_temp=None, target_lang=None):
174
"""Link the objects."""
175
# use separate copies, so we can modify the lists
176
extra_preargs = copy.copy(extra_preargs or [])
177
libraries = copy.copy(libraries or [])
178
objects = copy.copy(objects or [])
179
180
# Additional libraries
181
libraries.extend(self.dll_libraries)
182
183
# handle export symbols by creating a def-file
184
# with executables this only works with gcc/ld as linker
185
if ((export_symbols is not None) and
186
(target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
187
# (The linker doesn't do anything if output is up-to-date.
188
# So it would probably better to check if we really need this,
189
# but for this we had to insert some unchanged parts of
190
# UnixCCompiler, and this is not what we want.)
191
192
# we want to put some files in the same directory as the
193
# object files are, build_temp doesn't help much
194
# where are the object files
195
temp_dir = os.path.dirname(objects[0])
196
# name of dll to give the helper files the same base name
197
(dll_name, dll_extension) = os.path.splitext(
198
os.path.basename(output_filename))
199
200
# generate the filenames for these files
201
def_file = os.path.join(temp_dir, dll_name + ".def")
202
lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
203
204
# Generate .def file
205
contents = [
206
"LIBRARY %s" % os.path.basename(output_filename),
207
"EXPORTS"]
208
for sym in export_symbols:
209
contents.append(sym)
210
self.execute(write_file, (def_file, contents),
211
"writing %s" % def_file)
212
213
# next add options for def-file and to creating import libraries
214
215
# doesn't work: bfd_close build\...\libfoo.a: Invalid operation
216
#extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
217
# for gcc/ld the def-file is specified as any object files
218
objects.append(def_file)
219
220
#end: if ((export_symbols is not None) and
221
# (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
222
223
# who wants symbols and a many times larger output file
224
# should explicitly switch the debug mode on
225
# otherwise we let ld strip the output file
226
# (On my machine: 10KiB < stripped_file < ??100KiB
227
# unstripped_file = stripped_file + XXX KiB
228
# ( XXX=254 for a typical python extension))
229
if not debug:
230
extra_preargs.append("-s")
231
232
UnixCCompiler.link(self, target_desc, objects, output_filename,
233
output_dir, libraries, library_dirs,
234
runtime_library_dirs,
235
None, # export_symbols, we do this in our def-file
236
debug, extra_preargs, extra_postargs, build_temp,
237
target_lang)
238
239
# -- Miscellaneous methods -----------------------------------------
240
241
def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
242
"""Adds supports for rc and res files."""
243
if output_dir is None:
244
output_dir = ''
245
obj_names = []
246
for src_name in source_filenames:
247
# use normcase to make sure '.rc' is really '.rc' and not '.RC'
248
base, ext = os.path.splitext(os.path.normcase(src_name))
249
if ext not in (self.src_extensions + ['.rc','.res']):
250
raise UnknownFileError("unknown file type '%s' (from '%s')" % \
251
(ext, src_name))
252
if strip_dir:
253
base = os.path.basename (base)
254
if ext in ('.res', '.rc'):
255
# these need to be compiled to object files
256
obj_names.append (os.path.join(output_dir,
257
base + ext + self.obj_extension))
258
else:
259
obj_names.append (os.path.join(output_dir,
260
base + self.obj_extension))
261
return obj_names
262
263
# the same as cygwin plus some additional parameters
264
class Mingw32CCompiler(CygwinCCompiler):
265
""" Handles the Mingw32 port of the GNU C compiler to Windows.
266
"""
267
compiler_type = 'mingw32'
268
269
def __init__(self, verbose=0, dry_run=0, force=0):
270
271
super().__init__ (verbose, dry_run, force)
272
273
shared_option = "-shared"
274
275
if is_cygwincc(self.cc):
276
raise CCompilerError(
277
'Cygwin gcc cannot be used with --compiler=mingw32')
278
279
self.set_executables(compiler='%s -O -Wall' % self.cc,
280
compiler_so='%s -mdll -O -Wall' % self.cc,
281
compiler_cxx='%s -O -Wall' % self.cxx,
282
linker_exe='%s' % self.cc,
283
linker_so='%s %s'
284
% (self.linker_dll, shared_option))
285
286
# Maybe we should also append -mthreads, but then the finished
287
# dlls need another dll (mingwm10.dll see Mingw32 docs)
288
# (-mthreads: Support thread-safe exception handling on `Mingw32')
289
290
# no additional libraries needed
291
self.dll_libraries=[]
292
293
# Include the appropriate MSVC runtime library if Python was built
294
# with MSVC 7.0 or later.
295
self.dll_libraries = get_msvcr()
296
297
# Because these compilers aren't configured in Python's pyconfig.h file by
298
# default, we should at least warn the user if he is using an unmodified
299
# version.
300
301
CONFIG_H_OK = "ok"
302
CONFIG_H_NOTOK = "not ok"
303
CONFIG_H_UNCERTAIN = "uncertain"
304
305
def check_config_h():
306
"""Check if the current Python installation appears amenable to building
307
extensions with GCC.
308
309
Returns a tuple (status, details), where 'status' is one of the following
310
constants:
311
312
- CONFIG_H_OK: all is well, go ahead and compile
313
- CONFIG_H_NOTOK: doesn't look good
314
- CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h
315
316
'details' is a human-readable string explaining the situation.
317
318
Note there are two ways to conclude "OK": either 'sys.version' contains
319
the string "GCC" (implying that this Python was built with GCC), or the
320
installed "pyconfig.h" contains the string "__GNUC__".
321
"""
322
323
# XXX since this function also checks sys.version, it's not strictly a
324
# "pyconfig.h" check -- should probably be renamed...
325
326
from distutils import sysconfig
327
328
# if sys.version contains GCC then python was compiled with GCC, and the
329
# pyconfig.h file should be OK
330
if "GCC" in sys.version:
331
return CONFIG_H_OK, "sys.version mentions 'GCC'"
332
333
# Clang would also work
334
if "Clang" in sys.version:
335
return CONFIG_H_OK, "sys.version mentions 'Clang'"
336
337
# let's see if __GNUC__ is mentioned in python.h
338
fn = sysconfig.get_config_h_filename()
339
try:
340
config_h = open(fn)
341
try:
342
if "__GNUC__" in config_h.read():
343
return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
344
else:
345
return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
346
finally:
347
config_h.close()
348
except OSError as exc:
349
return (CONFIG_H_UNCERTAIN,
350
"couldn't read '%s': %s" % (fn, exc.strerror))
351
352
def is_cygwincc(cc):
353
'''Try to determine if the compiler that would be used is from cygwin.'''
354
out_string = check_output(shlex.split(cc) + ['-dumpmachine'])
355
return out_string.strip().endswith(b'cygwin')
356
357
358
get_versions = None
359
"""
360
A stand-in for the previous get_versions() function to prevent failures
361
when monkeypatched. See pypa/setuptools#2969.
362
"""
363
364