11from setuptools import setup , Extension
22from setuptools .command .build_ext import build_ext
3- import sys
3+ import os , sys
44
55ext_modules = [
66 Extension (
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 ('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
1544class 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