8000 Use -std=c++14 flag when available on clang and gcc · isuruf/python_example@7912e31 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7912e31

Browse files
committed
Use -std=c++14 flag when available on clang and gcc
1 parent 36b8dea commit 7912e31

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

setup.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, Extension
22
from setuptools.command.build_ext import build_ext
3-
import sys
3+
import os, sys
44

55
ext_modules = [
66
Extension(
@@ -11,12 +11,41 @@
1111
),
1212
]
1313

14+
def has_flag(compiler, flagname):
15+
"""Return a boolean indicating whether a flag name is supported on
16+
the specified compiler.
17+
"""
18+
import tempfile
19+
fd, fname = tempfile.mkstemp('.cpp', 'main', text=True)
20+
f = os.fdopen(fd, 'w')
21+
try:
22+
f.write(< 10000 span class=pl-s>'int main (int argc, char **argv) { return 0; }')
23+
finally:
24+
f.close()
25+
try:
26+
compiler.compile([fname], extra_postargs=[flagname])
27+
except setuptools.distutils.errors.CompileError:
28+
return False
29+
return True
30+
31+
def cpp_flag(compiler):
32+
"""Return the -std=c++[11/14] compiler flag.
33+
34+
The c++14 is prefered over c++11 (when it is available).
35+
"""
36+
if has_flag(compiler, '-std=c++14'):
37+
return '-std=c++14'
38+
elif has_flag(compiler, '-std=c++11'):
39+
return '-std=c++11'
40+
else:
41+
raise RuntimeError('Unsupported compiler -- at least C++11 support is needed!')
42+
1443

1544
class BuildExt(build_ext):
1645
"""A custom build extension for adding compiler-specific options."""
1746
c_opts = {
1847
'msvc': ['/EHsc'],
19-
'unix': ['-std=c++11'],
48+
'unix': [],
2049
}
2150

2251
if sys.platform == 'darwin':
@@ -25,6 +54,8 @@ class BuildExt(build_ext):
2554
def build_extensions(self):
2655
ct = self.compiler.compiler_type
2756
opts = self.c_opts.get(ct, [])
57+
if ct == 'unix':
58+
opts.append(cpp_flag(self.compiler))
2859
for ext in self.extensions:
2960
ext.extra_compile_args = opts
3061
build_ext.build_extensions(self)

0 commit comments

Comments
 (0)
0