|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +from setuptools import setup, Extension |
| 5 | +from setuptools.command.sdist import sdist as _sdist |
| 6 | +from setuptools.command.build_ext import build_ext as _build_ext |
| 7 | +from wheel.bdist_wheel import bdist_wheel as _bdist_wheel |
| 8 | +import glob |
| 9 | +import shutil |
| 10 | + |
| 11 | +__version__ = '0.8.4' |
| 12 | + |
| 13 | +class CMakeExtension(Extension): |
| 14 | + def __init__(self, name, sourcedir=''): |
| 15 | + super().__init__(name, sources=[]) |
| 16 | + self.sourcedir = os.path.abspath(sourcedir) |
| 17 | + |
| 18 | +class CMakeBuild(_build_ext): |
| 19 | + |
| 20 | + def run(self): |
| 21 | + for ext in self.extensions: |
| 22 | + self.build_cmake(ext) |
| 23 | + |
| 24 | + def build_cmake(self, ext): |
| 25 | + if os.path.exists('openfhe/openfhe.so'): |
| 26 | + return |
| 27 | + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) |
| 28 | + print(extdir) |
| 29 | + cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, |
| 30 | + '-DPYTHON_EXECUTABLE=' + sys.executable] |
| 31 | + |
| 32 | + cfg = 'Debug' if self.debug else 'Release' |
| 33 | + build_args = ['--config', cfg] |
| 34 | + |
| 35 | + build_temp = os.path.abspath(self.build_temp) |
| 36 | + os.makedirs(build_temp, exist_ok=True) |
| 37 | + |
| 38 | + num_cores = os.cpu_count() or 1 |
| 39 | + build_args += ['--parallel', str(num_cores)] |
| 40 | + |
| 41 | + subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=build_temp) |
| 42 | + subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=build_temp) |
| 43 | + |
| 44 | + so_files = glob.glob(os.path.join(extdir, '*.so')) |
| 45 | + if not so_files: |
| 46 | + raise RuntimeError("Cannot find any built .so file in " + extdir) |
| 47 | + |
| 48 | + src_file = so_files[0] |
| 49 | + dst_file = os.path.join('openfhe', 'openfhe.so') |
| 50 | + shutil.move(src_file, dst_file) |
| 51 | + |
| 52 | +# Run build_ext before sdist |
| 53 | +class SDist(_sdist): |
| 54 | + def run(self): |
| 55 | + if os.path.exists('openfhe/openfhe.so'): |
| 56 | + os.remove('openfhe/openfhe.so') |
| 57 | + self.run_command('build_ext') |
| 58 | + super().run() |
| 59 | + |
| 60 | +setup( |
| 61 | + name='openfhe', |
| 62 | + version=__version__, |
| 63 | + description='Python wrapper for OpenFHE C++ library.', |
| 64 | + author='OpenFHE Team', |
| 65 | + |
| 66 | + url='https://github.com/openfheorg/openfhe-python', |
| 67 | + license='BSD-2-Clause', |
| 68 | + packages=['openfhe'], |
| 69 | + package_data={'openfhe': ['*.so', '*.pyi']}, |
| 70 | + ext_modules=[CMakeExtension('openfhe', sourcedir='')], |
| 71 | + cmdclass={ |
| 72 | + 'build_ext': CMakeBuild, |
| 73 | + 'sdist': SDist |
| 74 | + }, |
| 75 | + include_package_data=True, |
| 76 | + python_requires=">=3.6", |
| 77 | + install_requires=['pybind11', 'pybind11-global', 'pybind11-stubgen'] |
| 78 | +) |
0 commit comments