Browse Source

Set rpath, so the installed openfhe module can find its shared library dependencies (#239)

* Added RPATH to CMakeLists.txt

* Cleaned __init__.py

* Moved __init__.py to src/ and amended CMakeLists.txt

---------

Co-authored-by: Dmitriy Suponitskiy <dsuponitskiy@dualitytech.com>
dsuponitskiy 7 months ago
parent
commit
6d121a93a5
3 changed files with 6 additions and 49 deletions
  1. 5 1
      CMakeLists.txt
  2. 0 48
      __init__.py
  3. 1 0
      src/__init__.py

+ 5 - 1
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})
 

+ 0 - 48
__init__.py

@@ -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 *

+ 1 - 0
src/__init__.py

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