From 4fe863c88d73f1f64dc65f0b355398e5256a5129 Mon Sep 17 00:00:00 2001 From: Yingjia Cai Date: Wed, 15 Jul 2026 18:33:39 +0800 Subject: [PATCH] Fix installed static MLX package on Windows --- CMakeLists.txt | 20 +++++++++++++++++--- mlx.pc.in | 19 +++++++++++++++++++ mlx/backend/cuda/CMakeLists.txt | 4 +++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f8ee65ad4b..394125a89c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,7 +261,15 @@ if(WIN32) FetchContent_MakeAvailable(dlfcn-win32) endblock() target_include_directories(mlx PRIVATE "${dlfcn-win32_SOURCE_DIR}/src") - target_link_libraries(mlx PRIVATE dl) + if(BUILD_SHARED_LIBS) + target_link_libraries(mlx PRIVATE dl) + else() + # A static MLX package must not expose the FetchContent-only dlfcn-win32::dl + # target to installed consumers. Compile the small Windows implementation + # directly into mlx.lib and propagate its system library. + target_sources(mlx PRIVATE "${dlfcn-win32_SOURCE_DIR}/src/dlfcn.c") + target_link_libraries(mlx PRIVATE Psapi.lib) + endif() endif() if(MLX_BUILD_CPU) @@ -296,8 +304,12 @@ if(MLX_BUILD_CPU) URL "https://github.com/OpenMathLib/OpenBLAS/releases/download/v0.3.33/${OPENBLAS_ZIP}" ) FetchContent_MakeAvailable(openblas) - target_link_libraries( - mlx PRIVATE "${openblas_SOURCE_DIR}/lib/${OPENBLAS_LIB}.lib") + set(OPENBLAS_IMPORT_LIBRARY + "${openblas_SOURCE_DIR}/lib/${OPENBLAS_LIB}.lib") + add_library(MLX::OpenBLAS UNKNOWN IMPORTED GLOBAL) + set_target_properties(MLX::OpenBLAS PROPERTIES IMPORTED_LOCATION + "${OPENBLAS_IMPORT_LIBRARY}") + target_link_libraries(mlx PRIVATE MLX::OpenBLAS) target_include_directories(mlx PRIVATE "${openblas_SOURCE_DIR}/${OPENBLAS_INC}") # Make sure the DLL file is placed in the same dir with executables. @@ -417,6 +429,8 @@ if(WIN32) if(MLX_BUILD_CPU) # Install OpenBLAS. install(FILES ${OPENBLAS_DLL_FILE} TYPE BIN) + install(FILES ${OPENBLAS_IMPORT_LIBRARY} + DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() endif() diff --git a/mlx.pc.in b/mlx.pc.in index c4e2515d71..0193667e93 100644 --- a/mlx.pc.in +++ b/mlx.pc.in @@ -11,6 +11,25 @@ @PACKAGE_INIT@ +include(CMakeFindDependencyMacro) + +if(WIN32 AND @MLX_BUILD_CPU@ AND NOT TARGET MLX::OpenBLAS) + add_library(MLX::OpenBLAS UNKNOWN IMPORTED) + set_target_properties(MLX::OpenBLAS PROPERTIES + IMPORTED_LOCATION "@PACKAGE_CMAKE_INSTALL_LIBDIR@/@OPENBLAS_LIB@.lib" + ) +endif() + +if(@MLX_BUILD_CUDA@) + find_dependency(CUDAToolkit REQUIRED) + + set(_MLX_SAVED_CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}") + list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + find_dependency(CUDNN REQUIRED) + set(CMAKE_MODULE_PATH "${_MLX_SAVED_CMAKE_MODULE_PATH}") + unset(_MLX_SAVED_CMAKE_MODULE_PATH) +endif() + include(@PACKAGE_MLX_CMAKE_INSTALL_MODULE_DIR@/MLXTargets.cmake) include(@PACKAGE_MLX_CMAKE_INSTALL_MODULE_DIR@/extension.cmake) diff --git a/mlx/backend/cuda/CMakeLists.txt b/mlx/backend/cuda/CMakeLists.txt index 065220e24e..20e30960c4 100644 --- a/mlx/backend/cuda/CMakeLists.txt +++ b/mlx/backend/cuda/CMakeLists.txt @@ -277,7 +277,9 @@ set(CUDNN_FRONTEND_BUILD_SAMPLES OFF) set(CUDNN_FRONTEND_BUILD_TESTS OFF) set(CUDNN_FRONTEND_BUILD_PYTHON_BINDINGS OFF) FetchContent_MakeAvailable(cudnn) -target_link_libraries(mlx PRIVATE cudnn_frontend) +# cudnn_frontend is header-only and is only needed while compiling MLX. Do not +# leak its FetchContent target into the installed static link interface. +target_link_libraries(mlx PRIVATE $) # Link with the actual cuDNN libraries. target_link_libraries(mlx PRIVATE CUDNN::cudnn_all)