|
| 1 | +from setuptools import setup, Extension |
| 2 | +from setuptools.command.build_ext import build_ext |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import setuptools |
| 6 | + |
| 7 | +__version__ = '0.0.1' |
| 8 | + |
| 9 | + |
| 10 | +class get_pybind_include(object): |
| 11 | + """Helper class to determine the pybind11 include path |
| 12 | +
|
| 13 | + The purpose of this class is to postpone importing pybind11 |
| 14 | + until it is actually installed, so that the ``get_include()`` |
| 15 | + method can be invoked. """ |
| 16 | + |
| 17 | + def __init__(self, user=False): |
| 18 | + self.user = user |
| 19 | + |
| 20 | + def __str__(self): |
| 21 | + import pybind11 |
| 22 | + return pybind11.get_include(self.user) |
| 23 | + |
| 24 | + |
| 25 | +ext_modules = [ |
| 26 | + Extension( |
| 27 | + 'xtensor_python_benchmark', |
| 28 | + ['main.cpp'], |
| 29 | + include_dirs=[ |
| 30 | + # Path to pybind11 headers |
| 31 | + get_pybind_include(), |
| 32 | + get_pybind_include(user=True), |
| 33 | + os.path.join(sys.prefix, 'include'), |
| 34 | + os.path.join(sys.prefix, 'Library', 'include') |
| 35 | + ], |
| 36 | + language='c++' |
| 37 | + ), |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +def has_flag(compiler, flagname): |
| 42 | + """Return a boolean indicating whether a flag name is supported on |
| 43 | + the specified compiler. |
| 44 | + """ |
| 45 | + import tempfile |
| 46 | + with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f: |
| 47 | + f.write('int main (int argc, char **argv) { return 0; }') |
| 48 | + try: |
| 49 | + compiler.compile([f.name], extra_postargs=[flagname]) |
| 50 | + except setuptools.distutils.errors.CompileError: |
| 51 | + return False |
| 52 | + return True |
| 53 | + |
| 54 | + |
| 55 | +def cpp_flag(compiler): |
| 56 | + """Return the -std=c++14 compiler flag and errors when the flag is |
| 57 | + no available. |
| 58 | + """ |
| 59 | + if has_flag(compiler, '-std=c++14'): |
| 60 | + return '-std=c++14' |
| 61 | + else: |
| 62 | + raise RuntimeError('C++14 support is required by xtensor!') |
| 63 | + |
| 64 | + |
| 65 | +class BuildExt(build_ext): |
| 66 | + """A custom build extension for adding compiler-specific options.""" |
| 67 | + c_opts = { |
| 68 | + 'msvc': ['/EHsc'], |
| 69 | + 'unix': [], |
| 70 | + } |
| 71 | + |
| 72 | + if sys.platform == 'darwin': |
| 73 | + c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7'] |
| 74 | + |
| 75 | + def build_extensions(self): |
| 76 | + ct = self.compiler.compiler_type |
| 77 | + opts = self.c_opts.get(ct, []) |
| 78 | + if ct == 'unix': |
| 79 | + opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version()) |
| 80 | + opts.append(cpp_flag(self.compiler)) |
| 81 | + if has_flag(self.compiler, '-fvisibility=hidden'): |
| 82 | + opts.append('-fvisibility=hidden') |
| 83 | + elif ct == 'msvc': |
| 84 | + opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()) |
| 85 | + for ext in self.extensions: |
| 86 | + ext.extra_compile_args = opts |
| 87 | + build_ext.build_extensions(self) |
| 88 | + |
| 89 | +setup( |
| 90 | + name='xtensor_python_benchmark', |
| 91 | + version=__version__, |
| 92 | + author='Sylvain Corlay', |
| 93 | + author_email='sylvain.corlay@gmail.com', |
| 94 | + url='https://github.com/pybind/python_example', |
| 95 | + description='An example project using xtensor-python', |
| 96 | + long_description='', |
| 97 | + ext_modules=ext_modules, |
| 98 | + install_requires=['pybind11==2.0.1'], |
| 99 | + cmdclass={'build_ext': BuildExt}, |
| 100 | + zip_safe=False, |
| 101 | +) |
0 commit comments