diff --git a/CMakeLists.txt b/CMakeLists.txt index 374462a..e3a7bd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,10 @@ pybind11_add_module(openfhe src/lib/binfhe/binfhecontext_wrapper.cpp src/lib/pke/serialization.cpp ) +# The next line ensures that the installed openfhe module can find its shared library dependencies +# in the 'lib/' subdirectory relative to itself without requiring LD_LIBRARY_PATH. +target_link_options(openfhe PRIVATE "-Wl,-rpath=$ORIGIN/lib" "-Wl,--disable-new-dtags") + ### Python installation # Allow the user to specify the path to Python executable (if not provided, find it) option(PYTHON_EXECUTABLE_PATH "Path to Python executable" "") @@ -103,5 +107,5 @@ else() endif() message("***** INSTALL IS AT ${Python_Install_Location}; to change, run cmake with -DCMAKE_INSTALL_PREFIX=/your/path") install(TARGETS openfhe LIBRARY DESTINATION ${Python_Install_Location}) -install(FILES ${CMAKE_SOURCE_DIR}/__init__.py DESTINATION ${Python_Install_Location}) +install(FILES ${CMAKE_SOURCE_DIR}/src/__init__.py DESTINATION ${Python_Install_Location}) diff --git a/__init__.py b/__init__.py deleted file mode 100644 index db9b485..0000000 --- a/__init__.py +++ /dev/null @@ -1,48 +0,0 @@ -import os -import ctypes - - -def load_shared_library(libname, paths): - for path in paths: - lib_path = os.path.join(path, libname) - if os.path.exists(lib_path): - return ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL) - - raise FileNotFoundError( - f"Shared library {libname} not found in {paths}" - ) - -# Search LD_LIBRARY_PATH -ld_paths = os.environ.get("LD_LIBRARY_PATH", "").split(":") - -if not any(ld_paths): - # Path to the bundled `lib/` directory inside site-packages - package_dir = os.path.abspath(os.path.dirname(__file__)) - internal_lib_dir = [os.path.join(package_dir, 'lib')] - - # Shared libraries required - shared_libs = [ - 'libgomp.so', - 'libOPENFHEcore.so.1', - 'libOPENFHEbinfhe.so.1', - 'libOPENFHEpke.so.1', - ] - - for libname in shared_libs: - load_shared_library(libname, internal_lib_dir) - - from .openfhe import * - -else: - # Shared libraries required - # skip 'libgomp.so' if LD_LIBRARY_PATH is set as we should get it from the libgomp.so location - shared_libs = [ - 'libOPENFHEcore.so.1', - 'libOPENFHEbinfhe.so.1', - 'libOPENFHEpke.so.1', - ] - - for libname in shared_libs: - load_shared_library(libname, ld_paths) - - # from .openfhe import * diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..8d1bfc6 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1 @@ +from .openfhe import *