Browse Source

Merge pull request #104 from Thadah/Gpujana_issue59

Add support for PyPI packaging. Fixes #59
mtriplett-duality 9 months ago
parent
commit
9fb9160868
5 changed files with 138 additions and 1 deletions
  1. 7 1
      .gitignore
  2. 51 0
      build_package.sh
  3. 1 0
      docs/requirements.txt
  4. 1 0
      openfhe/__init__.py
  5. 78 0
      setup.py

+ 7 - 1
.gitignore

@@ -10,4 +10,10 @@ build/
 *.pyc
 .settings/
 docs/
-demoData/
+demoData/
+dist/
+openfhe/openfhe.so
+openfhe/*.pyi
+openfhe.egg-info/
+stubs/
+.venv/

+ 51 - 0
build_package.sh

@@ -0,0 +1,51 @@
+#!/bin/bash
+# Exit on any error
+set -e
+
+# Find the venv directory
+if [ -d ".venv" ]; then
+    VENV_DIR=".venv"
+elif [ -d "../.venv" ]; then
+    VENV_DIR="../.venv"
+else
+    echo "The virtual environment does not exist. Please run 'python -m venv .venv' to create it." >&2
+    exit 1
+fi
+
+# Activate the virtual environment
+source $VENV_DIR/bin/activate
+
+# Install pybind11-stubgen
+if ! pip show pybind11-stubgen > /dev/null; then
+    pip install pybind11-stubgen
+fi
+
+# Check if the virtual environment has the openfhe package installed
+if ! pip show openfhe > /dev/null; then
+    echo "The openfhe package is not installed in the virtual environment. Please run 'pip install -e .' to install it." >&2
+    exit 1
+fi
+
+# Generate stub files using pybind11-stubgen
+echo "Generating stub files..."
+pybind11-stubgen openfhe
+
+# Check if stub generation was successful
+if [ $? -eq 0 ]; then
+    echo "Stub files generated successfully."
+else
+    echo "Stub generation failed." >&2
+    exit 1
+fi
+
+# Move the generated stub files to the openfhe package directory
+echo "Moving the generated stub files to the openfhe package directory..."
+mv stubs/openfhe/* openfhe/
+rm -r -d stubs
+
+# Build the source distribution and wheel distribution
+echo "Building the sdist and bdist_wheel..."
+python setup.py sdist bdist_wheel
+
+# Indicate where the distributions were saved
+echo "The distributions have been built and are located in the 'dist' directory. You can install the package using 'pip install dist/<distribution_file>'."

+ 1 - 0
docs/requirements.txt

@@ -17,6 +17,7 @@ Pygments==2.11.2
 pyparsing==3.0.7
 pytz==2021.3
 requests==2.27.1
+setuptools==69.0.3
 snowballstemmer==2.2.0
 Sphinx==4.4.0
 sphinx-rtd-theme==1.0.0

+ 1 - 0
openfhe/__init__.py

@@ -0,0 +1 @@
+from .openfhe import *

+ 78 - 0
setup.py

@@ -0,0 +1,78 @@
+import os
+import subprocess
+import sys
+from setuptools import setup, Extension
+from setuptools.command.sdist import sdist as _sdist
+from setuptools.command.build_ext import build_ext as _build_ext
+from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
+import glob
+import shutil
+
+__version__ = '0.8.4'
+
+class CMakeExtension(Extension):
+    def __init__(self, name, sourcedir=''):
+        super().__init__(name, sources=[])
+        self.sourcedir = os.path.abspath(sourcedir)
+
+class CMakeBuild(_build_ext):
+
+    def run(self):
+        for ext in self.extensions:
+            self.build_cmake(ext)
+
+    def build_cmake(self, ext):
+        if os.path.exists('openfhe/openfhe.so'):
+            return
+        extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
+        print(extdir)
+        cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
+                      '-DPYTHON_EXECUTABLE=' + sys.executable]
+
+        cfg = 'Debug' if self.debug else 'Release'
+        build_args = ['--config', cfg]
+
+        build_temp = os.path.abspath(self.build_temp)
+        os.makedirs(build_temp, exist_ok=True)
+
+        num_cores = os.cpu_count() or 1
+        build_args += ['--parallel', str(num_cores)]
+
+        subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=build_temp)
+        subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=build_temp)
+
+        so_files = glob.glob(os.path.join(extdir, '*.so'))
+        if not so_files:
+            raise RuntimeError("Cannot find any built .so file in " + extdir)
+
+        src_file = so_files[0] 
+        dst_file = os.path.join('openfhe', 'openfhe.so')
+        shutil.move(src_file, dst_file)
+
+# Run build_ext before sdist
+class SDist(_sdist):
+    def run(self):
+        if os.path.exists('openfhe/openfhe.so'):
+            os.remove('openfhe/openfhe.so')
+        self.run_command('build_ext')
+        super().run()
+
+setup(
+    name='openfhe',
+    version=__version__,
+    description='Python wrapper for OpenFHE C++ library.',
+    author='OpenFHE Team',
+    author_email='contact@openfhe.org',
+    url='https://github.com/openfheorg/openfhe-python',
+    license='BSD-2-Clause',
+    packages=['openfhe'],
+    package_data={'openfhe': ['*.so', '*.pyi']},
+    ext_modules=[CMakeExtension('openfhe', sourcedir='')],
+    cmdclass={
+        'build_ext': CMakeBuild,
+        'sdist': SDist
+    },
+    include_package_data=True,
+    python_requires=">=3.6",
+    install_requires=['pybind11', 'pybind11-global', 'pybind11-stubgen']
+)