ggml-backend: Separate dynamic lib install and search paths, add relative search path

This commit is contained in:
Perry Naseck 2026-01-13 14:43:03 -05:00
parent 0e76501e1d
commit f45ba0d0ef
No known key found for this signature in database
GPG Key ID: 14977DC34B9F5678
3 changed files with 41 additions and 4 deletions

View File

@ -79,9 +79,12 @@ if (WIN32)
set(CMAKE_SHARED_MODULE_PREFIX "")
endif()
option(BUILD_SHARED_LIBS "ggml: build shared libraries" ${BUILD_SHARED_LIBS_DEFAULT})
option(GGML_BACKEND_DL "ggml: build backends as dynamic libraries (requires BUILD_SHARED_LIBS)" OFF)
set(GGML_BACKEND_DIR "" CACHE PATH "ggml: directory to load dynamic backends from (requires GGML_BACKEND_DL")
option(BUILD_SHARED_LIBS "ggml: build shared libraries" ${BUILD_SHARED_LIBS_DEFAULT})
option(GGML_BACKEND_DL "ggml: build backends as dynamic libraries (requires BUILD_SHARED_LIBS)" OFF)
set(GGML_BACKEND_DIR "" CACHE PATH "ggml: DEPRECATED: directory to load dynamic backends from (requires GGML_BACKEND_DL")
set(GGML_BACKEND_INSTALL_DIR "" CACHE PATH "ggml: directory to install dynamic backends into (requires GGML_BACKEND_DL")
set(GGML_BACKEND_ABSOLUTE_SEARCH_DIR "" CACHE PATH "ggml: directory to load dynamic backends from (requires GGML_BACKEND_DL")
set(GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR "" CACHE PATH "ggml: directory relative to the executable to load dynamic backends from (requires GGML_BACKEND_DL")
#
# option list

View File

@ -231,11 +231,32 @@ set_target_properties(ggml PROPERTIES
)
if (GGML_BACKEND_DIR)
message(WARNING "GGML_BACKEND_DIR is deprecated: use GGML_BACKEND_INSTALL_DIR along with GGML_BACKEND_ABSOLUTE_SEARCH_DIR or GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR instead")
if (NOT GGML_BACKEND_DL)
message(FATAL_ERROR "GGML_BACKEND_DIR requires GGML_BACKEND_DL")
endif()
if (GGML_BACKEND_INSTALL_DIR OR GGML_BACKEND_ABSOLUTE_SEARCH_DIR OR GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR)
message(FATAL_ERROR "Cannot use GGML_BACKEND_DIR with GGML_BACKEND_INSTALL_DIR, GGML_BACKEND_ABSOLUTE_SEARCH_DIR, or GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR")
endif()
target_compile_definitions(ggml PUBLIC GGML_BACKEND_DIR="${GGML_BACKEND_DIR}")
endif()
if (GGML_BACKEND_INSTALL_DIR)
if (NOT GGML_BACKEND_DL)
message(FATAL_ERROR "GGML_BACKEND_INSTALL_DIR requires GGML_BACKEND_DL")
endif()
endif()
if (GGML_BACKEND_ABSOLUTE_SEARCH_DIR)
if (NOT GGML_BACKEND_DL)
message(FATAL_ERROR "GGML_BACKEND_ABSOLUTE_SEARCH_DIR requires GGML_BACKEND_DL")
endif()
target_compile_definitions(ggml PUBLIC GGML_BACKEND_ABSOLUTE_SEARCH_DIR="${GGML_BACKEND_ABSOLUTE_SEARCH_DIR}")
endif()
if (GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR)
if (NOT GGML_BACKEND_DL)
message(FATAL_ERROR "GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR requires GGML_BACKEND_DL")
endif()
target_compile_definitions(ggml PUBLIC GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR="${GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR}")
endif()
target_link_libraries(ggml PUBLIC ggml-base)
@ -251,7 +272,11 @@ function(ggml_add_backend_library backend)
target_compile_definitions(${backend} PRIVATE GGML_BACKEND_DL)
add_dependencies(ggml ${backend})
if (GGML_BACKEND_DIR)
# NOTE: GGML_BACKEND_DIR is deprecated, use GGML_BACKEND_INSTALL_DIR along with
# GGML_BACKEND_ABSOLUTE_SEARCH_DIR or GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR instead.
install(TARGETS ${backend} LIBRARY DESTINATION ${GGML_BACKEND_DIR})
elseif (GGML_BACKEND_INSTALL_DIR)
install(TARGETS ${backend} LIBRARY DESTINATION ${GGML_BACKEND_INSTALL_DIR})
else()
install(TARGETS ${backend} LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

View File

@ -527,11 +527,20 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
std::vector<fs::path> search_paths;
if (user_search_path == nullptr) {
const fs::path executable_path = get_executable_path();
#ifdef GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR
search_paths.push_back(executable_path / fs::u8path(GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR));
#endif
#ifdef GGML_BACKEND_DIR
// NOTE: GGML_BACKEND_DIR is deprecated, use GGML_BACKEND_ABSOLUTE_SEARCH_DIR or
// GGML_BACKEND_RELATIVE_TO_EXE_SEARCH_DIR along with GGML_BACKEND_INSTALL_DIR instead.
search_paths.push_back(fs::u8path(GGML_BACKEND_DIR));
#endif
#ifdef GGML_BACKEND_ABSOLUTE_SEARCH_DIR
search_paths.push_back(fs::u8path(GGML_BACKEND_ABSOLUTE_SEARCH_DIR));
#endif
// default search paths: executable directory, current directory
search_paths.push_back(get_executable_path());
search_paths.push_back(executable_path);
search_paths.push_back(fs::current_path());
} else {
search_paths.push_back(fs::u8path(user_search_path));