Skip to content

Commit e32105b

Browse files
longnguyen2004lazka
authored andcommitted
distutils: get compilers from env vars
Upstreamed at pypa/distutils#26 This would allow us to bypass all gcc workarounds when we're using clang
1 parent 4f1c118 commit e32105b

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

Lib/distutils/cygwinccompiler.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
# (ld supports -shared)
4545
# * mingw gcc 3.2/ld 2.13 works
4646
# (ld supports -shared)
47+
# * llvm-mingw with Clang 11 works
48+
# (lld supports -shared)
4749

4850
import os
4951
import sys
@@ -110,41 +112,46 @@ def __init__(self, verbose=0, dry_run=0, force=0):
110112
"Compiling may fail because of undefined preprocessor macros."
111113
% details)
112114

113-
self.gcc_version, self.ld_version, self.dllwrap_version = \
114-
get_versions()
115-
self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
116-
(self.gcc_version,
117-
self.ld_version,
118-
self.dllwrap_version) )
119-
120-
# ld_version >= "2.10.90" and < "2.13" should also be able to use
121-
# gcc -mdll instead of dllwrap
122-
# Older dllwraps had own version numbers, newer ones use the
123-
# same as the rest of binutils ( also ld )
124-
# dllwrap 2.10.90 is buggy
125-
if self.ld_version >= "2.10.90":
126-
self.linker_dll = "gcc"
127-
else:
128-
self.linker_dll = "dllwrap"
115+
self.cc = os.environ.get('CC', 'gcc')
116+
self.cxx = os.environ.get('CXX', 'g++')
117+
118+
if ('gcc' in self.cc): # Start gcc workaround
119+
self.gcc_version, self.ld_version, self.dllwrap_version = \
120+
get_versions()
121+
self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
122+
(self.gcc_version,
123+
self.ld_version,
124+
self.dllwrap_version) )
125+
126+
# ld_version >= "2.10.90" and < "2.13" should also be able to use
127+
# gcc -mdll instead of dllwrap
128+
# Older dllwraps had own version numbers, newer ones use the
129+
# same as the rest of binutils ( also ld )
130+
# dllwrap 2.10.90 is buggy
131+
if self.ld_version >= "2.10.90":
132+
self.linker_dll = self.cc
133+
else:
134+
self.linker_dll = "dllwrap"
129135

130-
# ld_version >= "2.13" support -shared so use it instead of
131-
# -mdll -static
132-
if self.ld_version >= "2.13":
136+
# ld_version >= "2.13" support -shared so use it instead of
137+
# -mdll -static
138+
if self.ld_version >= "2.13":
139+
shared_option = "-shared"
140+
else:
141+
shared_option = "-mdll -static"
142+
else: # Assume linker is up to date
143+
self.linker_dll = self.cc
133144
shared_option = "-shared"
134-
else:
135-
shared_option = "-mdll -static"
136145

137-
# Hard-code GCC because that's what this is all about.
138-
# XXX optimization, warnings etc. should be customizable.
139-
self.set_executables(compiler='gcc -mcygwin -O -Wall',
140-
compiler_so='gcc -mcygwin -mdll -O -Wall',
141-
compiler_cxx='g++ -mcygwin -O -Wall',
142-
linker_exe='gcc -mcygwin',
146+
self.set_executables(compiler='%s -mcygwin -O -Wall' % self.cc,
147+
compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
148+
compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
149+
linker_exe='%s -mcygwin' % self.cc,
143150
linker_so=('%s -mcygwin %s' %
144151
(self.linker_dll, shared_option)))
145152

146153
# cygwin and mingw32 need different sets of libraries
147-
if self.gcc_version == "2.91.57":
154+
if ('gcc' in self.cc and self.gcc_version == "2.91.57"):
148155
# cygwin shouldn't need msvcrt, but without the dlls will crash
149156
# (gcc version 2.91.57) -- perhaps something about initialization
150157
self.dll_libraries=["msvcrt"]
@@ -287,26 +294,26 @@ def __init__(self, verbose=0, dry_run=0, force=0):
287294

288295
# ld_version >= "2.13" support -shared so use it instead of
289296
# -mdll -static
290-
if self.ld_version >= "2.13":
291-
shared_option = "-shared"
292-
else:
297+
if ('gcc' in self.cc and self.ld_version < "2.13"):
293298
shared_option = "-mdll -static"
299+
else:
300+
shared_option = "-shared"
294301

295302
# A real mingw32 doesn't need to specify a different entry point,
296303
# but cygwin 2.91.57 in no-cygwin-mode needs it.
297-
if self.gcc_version <= "2.91.57":
304+
if ('gcc' in self.cc and self.gcc_version <= "2.91.57"):
298305
entry_point = '--entry _DllMain@12'
299306
else:
300307
entry_point = ''
301308

302-
if is_cygwingcc():
309+
if is_cygwincc(self.cc):
303310
raise CCompilerError(
304311
'Cygwin gcc cannot be used with --compiler=mingw32')
305312

306-
self.set_executables(compiler='gcc -O2 -Wall',
307-
compiler_so='gcc -mdll -O2 -Wall',
308-
compiler_cxx='g++ -O2 -Wall',
309-
linker_exe='gcc',
313+
self.set_executables(compiler='%s -O2 -Wall' % self.cc,
314+
compiler_so='%s -mdll -O2 -Wall' % self.cc,
315+
compiler_cxx='%s -O2 -Wall' % self.cxx,
316+
linker_exe='%s' % self.cc,
310317
linker_so='%s %s %s'
311318
% (self.linker_dll, shared_option,
312319
entry_point))
@@ -357,6 +364,10 @@ def check_config_h():
357364
if "GCC" in sys.version:
358365
return CONFIG_H_OK, "sys.version mentions 'GCC'"
359366

367+
# Clang would also work
368+
if "Clang" in sys.version:
369+
return CONFIG_H_OK, "sys.version mentions 'Clang'"
370+
360371
# let's see if __GNUC__ is mentioned in python.h
361372
fn = sysconfig.get_config_h_filename()
362373
try:
@@ -413,7 +424,7 @@ def get_versions():
413424
commands = [gcc+' -dumpversion', ld+' -v', dllwrap+' --version']
414425
return tuple([_find_exe_version(cmd) for cmd in commands])
415426

416-
def is_cygwingcc():
417-
'''Try to determine if the gcc that would be used is from cygwin.'''
418-
out_string = check_output(['gcc', '-dumpmachine'])
427+
def is_cygwincc(cc):
428+
'''Try to determine if the compiler that would be used is from cygwin.'''
429+
out_string = check_output([cc, '-dumpmachine'])
419430
return out_string.strip().endswith(b'cygwin')

0 commit comments

Comments
 (0)