Skip to content

Commit 50bcfb4

Browse files
python: better way to build and load shared libs (#66)
* fix: use find_library instead of assuming library extension * Output all libraries in the root build dir * Add scripts * Install with cmake --------- Co-authored-by: M. Yusuf Sarıgöz <[email protected]>
1 parent b46feea commit 50bcfb4

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ if (MSVC)
248248
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
249249
endif()
250250

251+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
252+
251253
add_subdirectory(ggml)
252254

253255
add_library(clip
@@ -264,6 +266,9 @@ if (BUILD_SHARED_LIBS)
264266
set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
265267
set_target_properties(clip PROPERTIES POSITION_INDEPENDENT_CODE ON)
266268
target_compile_definitions(clip PRIVATE CLIP_SHARED CLIP_BUILD)
269+
set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR})
270+
install(TARGETS ggml clip
271+
LIBRARY DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/examples/python_bindings/clip_cpp)
267272
endif()
268273

269274
add_subdirectory(models)

examples/python_bindings/clip_cpp/clip.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ctypes
2+
from ctypes.util import find_library
23
import os
34
from typing import List, Dict, Any
45

@@ -8,11 +9,13 @@
89
this_dir = os.path.abspath(os.path.dirname(__file__))
910

1011
# Load the shared library
11-
path_to_dll = os.environ.get("CLIP_DLL", this_dir)
12-
os.chdir(path_to_dll)
13-
ggml_lib = ctypes.CDLL("./libggml.so")
14-
clip_lib = ctypes.CDLL("./libclip.so")
15-
os.chdir(cur_dir)
12+
ggml_lib_path, clip_lib_path = find_library("ggml"), find_library("clip")
13+
if ggml_lib_path is None or clip_lib_path is None:
14+
raise RuntimeError(f"Could not find shared libraries. Please copy to the current working directory or supply the "
15+
f"correct LD_LIBRARY_PATH/DYLD_LIBRARY_PATH.")
16+
17+
ggml_lib = ctypes.CDLL(ggml_lib_path)
18+
clip_lib = ctypes.CDLL(clip_lib_path)
1619

1720

1821
# Define the ctypes structures

scripts/build-python.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#/bin/bash
2+
3+
# Change to the project directory
4+
cd "$(dirname "$0")"/..
5+
6+
rm -rf ./build
7+
8+
mkdir build
9+
10+
cd build
11+
12+
cmake -DBUILD_SHARED_LIBS=ON -DCLIP_NATIVE=OFF ..
13+
14+
make
15+
16+
make install
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
#/bin/bash
2+
3+
# Change to the project directory
4+
cd "$(dirname "$0")"/..
5+
26
find . -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.c' -o -name '*.h' \) ! -path "./ggml/*" ! -path "./build/*" -exec clang-format -i {} +

0 commit comments

Comments
 (0)