|
1 | 1 | import os |
2 | 2 | import ctypes |
3 | 3 |
|
4 | | -####################################################################################################################### |
5 | | -# Version #1 |
6 | | -####################################################################################################################### |
7 | | -# def load_shared_library(lib_path): |
8 | 4 |
|
9 | | -# # Load a shared library ensuring its symbols are globally available. This is necessary as |
10 | | -# # the library provides symbols that your extension module (e.g., openfhe.so) depends on. |
11 | | -# return ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL) |
12 | | - |
13 | | -# # The absolute path of the current package directory |
14 | | -# package_dir = os.path.abspath(os.path.dirname(__file__)) |
15 | | - |
16 | | -# # The directory where the shared libraries are located |
17 | | -# libs_dir = os.path.join(package_dir, 'lib') |
18 | | - |
19 | | -# # List of all shared libraries to be loaded |
20 | | -# shared_libs = ['libOPENFHEcore.so.1', |
21 | | -# 'libOPENFHEbinfhe.so.1', |
22 | | -# 'libOPENFHEpke.so.1', |
23 | | -# 'libgomp.so'] |
24 | | - |
25 | | -# # Load each shared library |
26 | | -# for lib in shared_libs: |
27 | | -# lib_path = os.path.join(libs_dir, lib) |
28 | | -# if not os.path.exists(lib_path): |
29 | | -# raise FileNotFoundError(f"Required shared library not found: {lib_path}") |
30 | | -# load_shared_library(lib_path) |
31 | | - |
32 | | -# # Import the Python wrapper module (openfhe.so) from this package |
33 | | -# from .openfhe import * |
34 | | - |
35 | | -####################################################################################################################### |
36 | | -# Version #2 |
37 | | -####################################################################################################################### |
38 | | -# Description: |
39 | | -# 1. check all directories in LD_LIBRARY_PATH for each .so.1. |
40 | | -# 2. fall back cleanly to the bundled wheel contents if the external .so is missing. |
41 | | -# 3. ensure RTLD_GLOBAL is used so Pybind11 bindings don’t hit undefined symbol errors. |
42 | | -def load_shared_library(libname, fallback_dir): |
43 | | - # Search LD_LIBRARY_PATH |
44 | | - ld_paths = os.environ.get("LD_LIBRARY_PATH", "").split(":") |
45 | | - for path in ld_paths: |
46 | | - # skip 'libgomp.so' if LD_LIBRARY_PATH is set as we should get it from the libgomp.so location |
47 | | - if libname == 'libgomp.so': |
48 | | - return |
| 5 | +def load_shared_library(libname, paths): |
| 6 | + for path in paths: |
49 | 7 | lib_path = os.path.join(path, libname) |
50 | 8 | if os.path.exists(lib_path): |
51 | 9 | return ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL) |
52 | 10 |
|
53 | | - # Fallback to bundled wheel/lib directory |
54 | | - fallback_path = os.path.join(fallback_dir, libname) |
55 | | - if os.path.exists(fallback_path): |
56 | | - return ctypes.CDLL(fallback_path, mode=ctypes.RTLD_GLOBAL) |
57 | | - |
58 | 11 | raise FileNotFoundError( |
59 | | - f"Shared library {libname} not found in LD_LIBRARY_PATH or fallback path:\n" |
60 | | - f" - LD_LIBRARY_PATH: {ld_paths}\n" |
61 | | - f" - Fallback path: {fallback_path}" |
| 12 | + f"Shared library {libname} not found in {paths}" |
62 | 13 | ) |
63 | 14 |
|
64 | | -# Path to the bundled `lib/` directory inside site-packages |
65 | | -package_dir = os.path.abspath(os.path.dirname(__file__)) |
66 | | -internal_lib_dir = os.path.join(package_dir, 'lib') |
| 15 | +# Search LD_LIBRARY_PATH |
| 16 | +ld_paths = os.environ.get("LD_LIBRARY_PATH", "").split(":") |
| 17 | + |
| 18 | +if not any(ld_paths): |
| 19 | + # Path to the bundled `lib/` directory inside site-packages |
| 20 | + package_dir = os.path.abspath(os.path.dirname(__file__)) |
| 21 | + internal_lib_dir = [os.path.join(package_dir, 'lib')] |
| 22 | + |
| 23 | + # Shared libraries required |
| 24 | + shared_libs = [ |
| 25 | + 'libgomp.so', |
| 26 | + 'libOPENFHEcore.so.1', |
| 27 | + 'libOPENFHEbinfhe.so.1', |
| 28 | + 'libOPENFHEpke.so.1', |
| 29 | + ] |
| 30 | + |
| 31 | + for libname in shared_libs: |
| 32 | + load_shared_library(libname, internal_lib_dir) |
| 33 | + |
| 34 | + from .openfhe import * |
67 | 35 |
|
68 | | -# Shared libraries required |
69 | | -shared_libs = [ |
70 | | - 'libOPENFHEcore.so.1', |
71 | | - 'libOPENFHEpke.so.1', |
72 | | - 'libOPENFHEbinfhe.so.1', |
73 | | - 'libgomp.so', # should be excluded if LD_LIBRARY_PATH is set |
74 | | -] |
| 36 | +else: |
| 37 | + # Shared libraries required |
| 38 | + # skip 'libgomp.so' if LD_LIBRARY_PATH is set as we should get it from the libgomp.so location |
| 39 | + shared_libs = [ |
| 40 | + 'libOPENFHEcore.so.1', |
| 41 | + 'libOPENFHEbinfhe.so.1', |
| 42 | + 'libOPENFHEpke.so.1', |
| 43 | + ] |
75 | 44 |
|
76 | | -# Load them from LD_LIBRARY_PATH or internal fallback |
77 | | -for lib in shared_libs: |
78 | | - load_shared_library(lib, fallback_dir=internal_lib_dir) |
| 45 | + for libname in shared_libs: |
| 46 | + load_shared_library(libname, ld_paths) |
79 | 47 |
|
80 | | -# Import the Pybind11 extension after shared libraries are loaded |
81 | | -from .openfhe import * |
| 48 | + # from .openfhe import * |
0 commit comments