Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

mlx: fix mac assumptions on linux/windows (#17898)

Daniel Hiltgen4e13421378ca
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 55c725b2a..0607f0995 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -45,6 +45,10 @@ if(APPLE)
set(CMAKE_BUILD_RPATH "@loader_path")
set(CMAKE_INSTALL_RPATH "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
+elseif(UNIX)
+ set(CMAKE_BUILD_RPATH "$ORIGIN")
+ set(CMAKE_INSTALL_RPATH "$ORIGIN")
+ set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
endif()
set(OLLAMA_BUILD_DIR ${CMAKE_BINARY_DIR}/lib/ollama)
diff --git a/cmake/mlx/CMakeLists.txt b/cmake/mlx/CMakeLists.txt
index 30fd941fa..00e7bfd4c 100644
--- a/cmake/mlx/CMakeLists.txt
+++ b/cmake/mlx/CMakeLists.txt
@@ -23,6 +23,10 @@ if(APPLE)
set(CMAKE_BUILD_RPATH "@loader_path")
set(CMAKE_INSTALL_RPATH "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
+elseif(UNIX)
+ set(CMAKE_BUILD_RPATH "$ORIGIN")
+ set(CMAKE_INSTALL_RPATH "$ORIGIN")
+ set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
endif()
if(NOT DEFINED OLLAMA_SOURCE_DIR OR "${OLLAMA_SOURCE_DIR}" STREQUAL "")
diff --git a/x/mlxrunner/mlx/CMakeLists.txt b/x/mlxrunner/mlx/CMakeLists.txt
index 9dfd259d7..5e52498bd 100644
--- a/x/mlxrunner/mlx/CMakeLists.txt
+++ b/x/mlxrunner/mlx/CMakeLists.txt
@@ -11,7 +11,11 @@ set(MLX_BUILD_SAFETENSORS ON CACHE BOOL "" FORCE)
set(MLX_C_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
-set(CMAKE_INSTALL_RPATH "@loader_path")
+if(APPLE)
+ set(CMAKE_INSTALL_RPATH "@loader_path")
+else()
+ set(CMAKE_INSTALL_RPATH "$ORIGIN")
+endif()
include(FetchContent)
diff --git a/x/mlxrunner/mlx/dynamic.c b/x/mlxrunner/mlx/dynamic.c
index 3420139a9..758904b31 100644
--- a/x/mlxrunner/mlx/dynamic.c
+++ b/x/mlxrunner/mlx/dynamic.c
@@ -4,7 +4,25 @@
#ifdef _WIN32
#include <windows.h>
-#define DLOPEN(path) LoadLibraryA(path)
+#include <string.h>
+
+static void* mlx_dlopen(const char* path) {
+ // Windows doesn't search the DLL's own directory for dependencies.
+ char dir[MAX_PATH] = {0};
+ strncpy(dir, path, MAX_PATH - 1);
+ char* last_sep = NULL;
+ for (char* p = dir; *p; p++) {
+ if (*p == '\\' || *p == '/') last_sep = p;
+ }
+ if (last_sep) *last_sep = '\0';
+ else dir[0] = '\0';
+
+ if (dir[0]) SetDllDirectoryA(dir);
+ void* h = (void*) LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
+ SetDllDirectoryA(NULL);
+ return h;
+}
+
#define DLCLOSE(handle) FreeLibrary((HMODULE)(handle))
#else
#ifdef __APPLE__
@@ -12,12 +30,14 @@
#include <libgen.h>
#endif
#include <dlfcn.h>
-#define DLOPEN(path) dlopen(path, RTLD_LAZY | RTLD_GLOBAL)
+static void* mlx_dlopen(const char* path) {
+ return dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
+}
#define DLCLOSE(handle) dlclose(handle)
#endif
static int mlx_dynamic_open(mlx_dynamic_handle* handle, const char* path) {
- handle->ctx = (void*) DLOPEN(path);
+ handle->ctx = mlx_dlopen(path);
if (handle->ctx == NULL) {
return 1;
}