|
| 1 | +# based on |
| 2 | +# https://github.com/pybind/python_example/blob/master/setup.py |
| 3 | + |
| 4 | +from setuptools import setup, Extension |
| 5 | +from setuptools.command.build_ext import build_ext |
| 6 | +import sys |
| 7 | +import setuptools |
| 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 | + import subprocess |
| 19 | + try: |
| 20 | + import pybind11 |
| 21 | + except ImportError: |
| 22 | + if subprocess.call([sys.executable, '-m', 'pip', 'install', 'pybind11']): |
| 23 | + raise RuntimeError('pybind11 install failed.') |
| 24 | + self.user = user |
| 25 | + |
| 26 | + def __str__(self): |
| 27 | + import pybind11 |
| 28 | + return pybind11.get_include(self.user) |
| 29 | + |
| 30 | + |
| 31 | +ext_modules = [ |
| 32 | + Extension( |
| 33 | + 'blspy', |
| 34 | + [ |
| 35 | + 'src/chaincode.cpp', |
| 36 | + 'src/extendedpublickey.cpp', |
| 37 | + 'src/extendedprivatekey.cpp', |
| 38 | + 'src/chaincode.cpp', |
| 39 | + 'src/signature.cpp', |
| 40 | + 'src/publickey.cpp', |
| 41 | + 'src/privatekey.cpp', |
| 42 | + 'src/bls.cpp', |
| 43 | + 'src/aggregationinfo.cpp', |
| 44 | + 'src/threshold.cpp', |
| 45 | + ], |
| 46 | + include_dirs=[ |
| 47 | + # Path to pybind11 headers |
| 48 | + get_pybind_include(), |
| 49 | + get_pybind_include(user=True), |
| 50 | + 'mpir_gc_x64', |
| 51 | + 'relic_x64/include', |
| 52 | + 'contrib/relic/include', |
| 53 | + 'contrib/catch', |
| 54 | + ], |
| 55 | + library_dirs=['mpir_gc_x64','relic_x64/lib'], |
| 56 | + libraries=['mpir'], |
| 57 | + language='c++' |
| 58 | + ), |
| 59 | +] |
| 60 | + |
| 61 | + |
| 62 | +# As of Python 3.6, CCompiler has a `has_flag` method. |
| 63 | +# cf http://bugs.python.org/issue26689 |
| 64 | +def has_flag(compiler, flagname): |
| 65 | + """Return a boolean indicating whether a flag name is supported on |
| 66 | + the specified compiler. |
| 67 | + """ |
| 68 | + import tempfile |
| 69 | + with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f: |
| 70 | + f.write('int main (int argc, char **argv) { return 0; }') |
| 71 | + try: |
| 72 | + compiler.compile([f.name], extra_postargs=[flagname]) |
| 73 | + except setuptools.distutils.errors.CompileError: |
| 74 | + return False |
| 75 | + return True |
| 76 | + |
| 77 | + |
| 78 | +def cpp_flag(compiler): |
| 79 | + """Return the -std=c++[11/14/17] compiler flag. |
| 80 | +
|
| 81 | + The newer version is prefered over c++11 (when it is available). |
| 82 | + """ |
| 83 | + flags = ['-std=c++17', '-std=c++14', '-std=c++11'] |
| 84 | + |
| 85 | + for flag in flags: |
| 86 | + if has_flag(compiler, flag): return flag |
| 87 | + |
| 88 | + raise RuntimeError('Unsupported compiler -- at least C++11 support ' |
| 89 | + 'is needed!') |
| 90 | + |
| 91 | + |
| 92 | +class BuildExt(build_ext): |
| 93 | + """A custom build extension for adding compiler-specific options.""" |
| 94 | + c_opts = { |
| 95 | + 'msvc': ['/EHsc','/std:c++17'], |
| 96 | + 'unix': [], |
| 97 | + } |
| 98 | + l_opts = { |
| 99 | + 'msvc': [], |
| 100 | + 'unix': [], |
| 101 | + } |
| 102 | + |
| 103 | + if sys.platform == 'darwin': |
| 104 | + darwin_opts = ['-stdlib=libc++', '-mmacosx-version-min=10.14'] |
| 105 | + c_opts['unix'] += darwin_opts |
| 106 | + l_opts['unix'] += darwin_opts |
| 107 | + |
| 108 | + def build_extensions(self): |
| 109 | + ct = self.compiler.compiler_type |
| 110 | + opts = self.c_opts.get(ct, []) |
| 111 | + link_opts = self.l_opts.get(ct, []) |
| 112 | + if ct == 'unix': |
| 113 | + opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version()) |
| 114 | + opts.append(cpp_flag(self.compiler)) |
| 115 | + if has_flag(self.compiler, '-fvisibility=hidden'): |
| 116 | + opts.append('-fvisibility=hidden') |
| 117 | + elif ct == 'msvc': |
| 118 | + opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()) |
| 119 | + for ext in self.extensions: |
| 120 | + ext.extra_compile_args = opts |
| 121 | + ext.extra_link_args = link_opts |
| 122 | + build_ext.build_extensions(self) |
| 123 | + |
| 124 | +setup( |
| 125 | + name='blspy', |
| 126 | + author='Mariano Sorgente', |
| 127 | + |
| 128 | + description='BLS signatures in c++ (python bindings)', |
| 129 | + long_description='BLS signatures with aggregation. Uses fast c++ implementation. See https://github.com/Chia-Network/bls-signatures for more details', |
| 130 | + license='Apache License', |
| 131 | + python_requires='>=3.7', |
| 132 | + setup_requires=['pybind11>=2.4'], |
| 133 | + install_requires=['pybind11>=2.4'], |
| 134 | + build_requires=["pybind11>=2.4"], |
| 135 | + ext_modules=ext_modules, |
| 136 | + cmdclass={'build_ext': BuildExt}, |
| 137 | + zip_safe=False, |
| 138 | +) |
0 commit comments