1
1
from setuptools import setup , Extension
2
2
from setuptools .command .build_ext import build_ext
3
- import sys
3
+ import os , sys
4
4
5
5
ext_modules = [
6
6
Extension (
11
11
),
12
12
]
13
13
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
+
14
43
15
44
class BuildExt (build_ext ):
16
45
"""A custom build extension for adding compiler-specific options."""
17
46
c_opts = {
18
47
'msvc' : ['/EHsc' ],
19
- 'unix' : ['-std=c++11' ],
48
+ 'unix' : [],
20
49
}
21
50
22
51
if sys .platform == 'darwin' :
@@ -25,6 +54,8 @@ class BuildExt(build_ext):
25
54
def build_extensions (self ):
26
55
ct = self .compiler .compiler_type
27
56
opts = self .c_opts .get (ct , [])
57
+ if ct == 'unix' :
58
+ opts .append (cpp_flag (self .compiler ))
28
59
for ext in self .extensions :
29
60
ext .extra_compile_args = opts
30
61
build_ext .build_extensions (self )
0 commit comments