Skip to content

Commit 4161343

Browse files
authored
cmake: Add GGML_BACKEND_DIR option (#15074)
* cmake: Add GGML_BACKEND_DIR option This can be used by distributions to specify where to look for backends when ggml is built with GGML_BACKEND_DL=ON. * Fix phrasing
1 parent e5bebe5 commit 4161343

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

ggml/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ if (WIN32)
3939
set(CMAKE_SHARED_MODULE_PREFIX "")
4040
endif()
4141

42-
option(BUILD_SHARED_LIBS "ggml: build shared libraries" ${BUILD_SHARED_LIBS_DEFAULT})
43-
option(GGML_BACKEND_DL "ggml: build backends as dynamic libraries (requires BUILD_SHARED_LIBS)" OFF)
42+
option(BUILD_SHARED_LIBS "ggml: build shared libraries" ${BUILD_SHARED_LIBS_DEFAULT})
43+
option(GGML_BACKEND_DL "ggml: build backends as dynamic libraries (requires BUILD_SHARED_LIBS)" OFF)
44+
set(GGML_BACKEND_DIR "" CACHE PATH "ggml: directory to load dynamic backends from (requires GGML_BACKEND_DL")
4445

4546
#
4647
# option list

ggml/cmake/ggml-config.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ if(NOT TARGET ggml::ggml)
106106

107107
find_library(GGML_LIBRARY ggml
108108
REQUIRED
109-
HINTS ${GGML_LIB_DIR}
109+
HINTS ${GGML_LIB_DIR} ${GGML_BACKEND_DIR}
110110
NO_CMAKE_FIND_ROOT_PATH)
111111

112112
add_library(ggml::ggml UNKNOWN IMPORTED)

ggml/src/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ add_library(ggml
214214
ggml-backend-reg.cpp)
215215
add_library(ggml::ggml ALIAS ggml)
216216

217+
if (GGML_BACKEND_DIR)
218+
if (NOT GGML_BACKEND_DL)
219+
message(FATAL_ERROR "GGML_BACKEND_DIR requires GGML_BACKEND_DL")
220+
endif()
221+
target_compile_definitions(ggml PUBLIC GGML_BACKEND_DIR="${GGML_BACKEND_DIR}")
222+
endif()
223+
217224
target_link_libraries(ggml PUBLIC ggml-base)
218225

219226
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
@@ -227,7 +234,11 @@ function(ggml_add_backend_library backend)
227234
set_target_properties(${backend} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
228235
target_compile_definitions(${backend} PRIVATE GGML_BACKEND_DL)
229236
add_dependencies(ggml ${backend})
230-
install(TARGETS ${backend} LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR})
237+
if (GGML_BACKEND_DIR)
238+
install(TARGETS ${backend} LIBRARY DESTINATION ${GGML_BACKEND_DIR})
239+
else()
240+
install(TARGETS ${backend} LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR})
241+
endif()
231242
else()
232243
add_library(${backend} ${ARGN})
233244
target_link_libraries(ggml PUBLIC ${backend})

ggml/src/ggml-backend-reg.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
498498

499499
std::vector<fs::path> search_paths;
500500
if (user_search_path == nullptr) {
501+
#ifdef GGML_BACKEND_DIR
502+
search_paths.push_back(fs::u8path(GGML_BACKEND_DIR));
503+
#endif
501504
// default search paths: executable directory, current directory
502505
search_paths.push_back(get_executable_path());
503506
search_paths.push_back(fs::current_path());

0 commit comments

Comments
 (0)