setup.py 2.5 KB

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