setup.py 2.6 KB

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