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

The default packaging was broken due to mac assumptions
leaking into windows
This commit is contained in:
Daniel Hiltgen
2026-08-20 09:50:10 -07:00
committed by GitHub
parent b7871fc0d1
commit 4e13421378
4 changed files with 36 additions and 4 deletions
+4
View File
@@ -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)
+4
View File
@@ -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 "")
+5 -1
View File
@@ -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)
+23 -3
View File
@@ -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;
}