-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
108 lines (95 loc) · 4 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sys
import tempfile
from collections import defaultdict
from distutils.ccompiler import CCompiler
from distutils.errors import CompileError
from glob import glob
from pathlib import Path
import pybind11
from setuptools import (Extension,
find_packages,
setup)
from setuptools.command.build_ext import build_ext
import seidel
def has_flag(compiler: CCompiler, name: str) -> bool:
"""Detects whether a flag name is supported on the specified compiler."""
with tempfile.NamedTemporaryFile('w',
suffix='.cpp') as file:
file.write('int main (int argc, char **argv) { return 0; }')
try:
compiler.compile([file.name],
extra_postargs=[name])
except CompileError:
return False
return True
def cpp_flag(compiler: CCompiler) -> str:
"""
Returns the -std=c++[11/14/17] compiler flag.
The newer version is preferred when available.
"""
flags = ['-std=c++14', '-std=c++11']
if sys.platform != 'darwin':
flags = ['-std=c++17'] + flags
for flag in flags:
if has_flag(compiler, flag):
return flag
raise RuntimeError('Unsupported compiler: '
'at least C++11 support is needed!')
class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
compile_args = defaultdict(list,
{'msvc': ['/EHsc'],
'unix': []})
link_args = defaultdict(list,
{'msvc': [],
'unix': []})
if sys.platform == 'darwin':
darwin_args = ['-stdlib=libc++', '-mmacosx-version-min=10.7']
compile_args['unix'] += darwin_args
link_args['unix'] += darwin_args
def build_extensions(self) -> None:
compiler_type = self.compiler.compiler_type
compile_args = self.compile_args[compiler_type]
link_args = self.link_args[compiler_type]
if compiler_type == 'unix':
compile_args.append(cpp_flag(self.compiler))
if has_flag(self.compiler, '-fvisibility=hidden'):
compile_args.append('-fvisibility=hidden')
define_macros = [('VERSION_INFO',
('\\"{}\\"' if compiler_type == 'msvc' else '"{}"')
.format(self.distribution.get_version()))]
for extension in self.extensions:
extension.extra_compile_args = compile_args
extension.extra_link_args = link_args
extension.define_macros = define_macros
super().build_extensions()
project_base_url = 'https://github.com/lycantropos/seidel/'
setup(name=seidel.__name__,
packages=find_packages(exclude=('tests', 'tests.*')),
version=seidel.__version__,
description=seidel.__doc__,
long_description=Path('README.md').read_text(encoding='utf-8'),
long_description_content_type='text/markdown',
author='Azat Ibrakov',
author_email='azatibrakov@gmail.com',
classifiers=[
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: Implementation :: CPython',
],
license='MIT License',
url=project_base_url,
download_url=project_base_url + 'archive/master.zip',
python_requires='>=3.5',
install_requires=Path('requirements.txt').read_text(),
cmdclass={'build_ext': BuildExt},
ext_modules=[Extension('_' + seidel.__name__,
glob('src/*.cpp'),
include_dirs=[pybind11.get_include(),
pybind11.get_include(True)],
language='c++',
undef_macros=['NDEBUG'])],
zip_safe=False)