Skip to content

Commit 622cdcb

Browse files
authored
Merge pull request #26 from longnguyen2004/clang-mingw
Add clang mingw support
2 parents 8f53bf3 + 044adae commit 622cdcb

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

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
@@ -109,41 +111,46 @@ def __init__(self, verbose=0, dry_run=0, force=0):
109111
"Compiling may fail because of undefined preprocessor macros."
110112
% details)
111113

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

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

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

145152
# cygwin and mingw32 need different sets of libraries
146-
if self.gcc_version == "2.91.57":
153+
if ('gcc' in self.cc and self.gcc_version == "2.91.57"):
147154
# cygwin shouldn't need msvcrt, but without the dlls will crash
148155
# (gcc version 2.91.57) -- perhaps something about initialization
149156
self.dll_libraries=["msvcrt"]
@@ -281,26 +288,26 @@ def __init__(self, verbose=0, dry_run=0, force=0):
281288

282289
# ld_version >= "2.13" support -shared so use it instead of
283290
# -mdll -static
284-
if self.ld_version >= "2.13":
285-
shared_option = "-shared"
286-
else:
291+
if ('gcc' in self.cc and self.ld_version < "2.13"):
287292
shared_option = "-mdll -static"
293+
else:
294+
shared_option = "-shared"
288295

289296
# A real mingw32 doesn't need to specify a different entry point,
290297
# but cygwin 2.91.57 in no-cygwin-mode needs it.
291-
if self.gcc_version <= "2.91.57":
298+
if ('gcc' in self.cc and self.gcc_version <= "2.91.57"):
292299
entry_point = '--entry _DllMain@12'
293300
else:
294301
entry_point = ''
295302

296-
if is_cygwingcc():
303+
if is_cygwincc(self.cc):
297304
raise CCompilerError(
298305
'Cygwin gcc cannot be used with --compiler=mingw32')
299306

300-
self.set_executables(compiler='gcc -O -Wall',
301-
compiler_so='gcc -mdll -O -Wall',
302-
compiler_cxx='g++ -O -Wall',
303-
linker_exe='gcc',
307+
self.set_executables(compiler='%s -O -Wall' % self.cc,
308+
compiler_so='%s -mdll -O -Wall' % self.cc,
309+
compiler_cxx='%s -O -Wall' % self.cxx,
310+
linker_exe='%s' % self.cc,
304311
linker_so='%s %s %s'
305312
% (self.linker_dll, shared_option,
306313
entry_point))
@@ -351,6 +358,10 @@ def check_config_h():
351358
if "GCC" in sys.version:
352359
return CONFIG_H_OK, "sys.version mentions 'GCC'"
353360

361+
# Clang would also work
362+
if "Clang" in sys.version:
363+
return CONFIG_H_OK, "sys.version mentions 'Clang'"
364+
354365
# let's see if __GNUC__ is mentioned in python.h
355366
fn = sysconfig.get_config_h_filename()
356367
try:
@@ -397,7 +408,7 @@ def get_versions():
397408
commands = ['gcc -dumpversion', 'ld -v', 'dllwrap --version']
398409
return tuple([_find_exe_version(cmd) for cmd in commands])
399410

400-
def is_cygwingcc():
401-
'''Try to determine if the gcc that would be used is from cygwin.'''
402-
out_string = check_output(['gcc', '-dumpmachine'])
411+
def is_cygwincc(cc):
412+
'''Try to determine if the compiler that would be used is from cygwin.'''
413+
out_string = check_output([cc, '-dumpmachine'])
403414
return out_string.strip().endswith(b'cygwin')

0 commit comments

Comments
 (0)