| 
									
										
										
										
											2024-12-14 12:50:39 +09:00
										 |  |  | cmake_minimum_required(VERSION 3.25)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-20 05:30:19 +01:00
										 |  |  | if(NOT MLX_VERSION)
 | 
					
						
							| 
									
										
										
										
											2025-02-20 15:25:15 -08:00
										 |  |  |   file(STRINGS "mlx/version.h" _mlx_h_version REGEX "^#define MLX_VERSION_.*$")
 | 
					
						
							|  |  |  |   string(REGEX MATCH "#define MLX_VERSION_MAJOR ([0-9]+)" _ "${_mlx_h_version}")
 | 
					
						
							|  |  |  |   set(_major ${CMAKE_MATCH_1})
 | 
					
						
							|  |  |  |   string(REGEX MATCH "#define MLX_VERSION_MINOR ([0-9]+)" _ "${_mlx_h_version}")
 | 
					
						
							|  |  |  |   set(_minor ${CMAKE_MATCH_1})
 | 
					
						
							|  |  |  |   string(REGEX MATCH "#define MLX_VERSION_PATCH ([0-9]+)" _ "${_mlx_h_version}")
 | 
					
						
							|  |  |  |   set(_patch ${CMAKE_MATCH_1})
 | 
					
						
							|  |  |  |   set(MLX_PROJECT_VERSION "${_major}.${_minor}.${_patch}")
 | 
					
						
							| 
									
										
										
										
											2025-03-18 23:12:40 +09:00
										 |  |  |   set(MLX_VERSION ${MLX_PROJECT_VERSION})
 | 
					
						
							| 
									
										
										
										
											2025-02-20 05:30:19 +01:00
										 |  |  | else()
 | 
					
						
							|  |  |  |   string(REGEX REPLACE "^([0-9]+\.[0-9]+\.[0-9]+).*" "\\1" MLX_PROJECT_VERSION | 
					
						
							|  |  |  |                        ${MLX_VERSION})
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | project( | 
					
						
							|  |  |  |   mlx
 | 
					
						
							|  |  |  |   LANGUAGES C CXX
 | 
					
						
							|  |  |  |   VERSION ${MLX_PROJECT_VERSION})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # ----------------------------- Setup -----------------------------
 | 
					
						
							|  |  |  | set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
 | 
					
						
							|  |  |  | set(CMAKE_CXX_STANDARD 17)
 | 
					
						
							|  |  |  | set(CMAKE_CXX_STANDARD_REQUIRED ON)
 | 
					
						
							|  |  |  | set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 | 
					
						
							|  |  |  | set(CMAKE_INSTALL_MESSAGE NEVER)
 | 
					
						
							| 
									
										
										
										
											2025-10-02 18:40:32 -04:00
										 |  |  | set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # ----------------------------- Configuration -----------------------------
 | 
					
						
							|  |  |  | option(MLX_BUILD_TESTS "Build tests for mlx" ON)
 | 
					
						
							|  |  |  | option(MLX_BUILD_EXAMPLES "Build examples for mlx" ON)
 | 
					
						
							|  |  |  | option(MLX_BUILD_BENCHMARKS "Build benchmarks for mlx" OFF)
 | 
					
						
							|  |  |  | option(MLX_BUILD_PYTHON_BINDINGS "Build python bindings for mlx" OFF)
 | 
					
						
							| 
									
										
										
										
											2024-10-07 19:13:50 -07:00
										 |  |  | option(MLX_BUILD_METAL "Build metal backend" ON)
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  | option(MLX_BUILD_CPU "Build cpu backend" ON)
 | 
					
						
							| 
									
										
										
										
											2025-05-07 13:26:46 +09:00
										 |  |  | option(MLX_BUILD_CUDA "Build cuda backend" OFF)
 | 
					
						
							| 
									
										
										
										
											2024-03-28 09:40:31 -07:00
										 |  |  | option(MLX_METAL_DEBUG "Enhance metal debug workflow" OFF)
 | 
					
						
							| 
									
										
										
										
											2024-03-27 22:14:29 +09:00
										 |  |  | option(MLX_ENABLE_X64_MAC "Enable building for x64 macOS" OFF)
 | 
					
						
							| 
									
										
										
										
											2024-05-09 17:39:44 -07:00
										 |  |  | option(MLX_BUILD_GGUF "Include support for GGUF format" ON)
 | 
					
						
							|  |  |  | option(MLX_BUILD_SAFETENSORS "Include support for safetensors format" ON)
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  | option(MLX_BUILD_BLAS_FROM_SOURCE "Build OpenBLAS from source code" OFF)
 | 
					
						
							| 
									
										
										
										
											2024-05-22 12:57:13 -07:00
										 |  |  | option(MLX_METAL_JIT "Use JIT compilation for Metal kernels" OFF)
 | 
					
						
							| 
									
										
										
										
											2025-07-29 08:43:22 +09:00
										 |  |  | option(MLX_USE_CCACHE "Use CCache for compilation cache when available" ON)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | option(BUILD_SHARED_LIBS "Build mlx as a shared library" OFF)
 | 
					
						
							| 
									
										
										
										
											2025-08-05 01:36:11 +02:00
										 |  |  | option(USE_SYSTEM_FMT "Use system's provided fmt library" OFF)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-08 11:03:25 -08:00
										 |  |  | # --------------------- Processor tests -------------------------
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | message( | 
					
						
							|  |  |  |   STATUS
 | 
					
						
							|  |  |  |     "Building MLX for ${CMAKE_SYSTEM_PROCESSOR} processor on ${CMAKE_SYSTEM_NAME}"
 | 
					
						
							|  |  |  | )
 | 
					
						
							| 
									
										
										
										
											2023-12-08 11:03:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
 | 
					
						
							| 
									
										
										
										
											2024-03-27 22:14:29 +09:00
										 |  |  |   if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
 | 
					
						
							|  |  |  |     if(NOT MLX_ENABLE_X64_MAC)
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |       message( | 
					
						
							|  |  |  |         FATAL_ERROR
 | 
					
						
							|  |  |  |           "Building for x86_64 on macOS is not supported."
 | 
					
						
							|  |  |  |           " If you are on an Apple silicon system, check the build"
 | 
					
						
							|  |  |  |           " documentation for possible fixes: "
 | 
					
						
							|  |  |  |           "https://ml-explore.github.io/mlx/build/html/install.html#build-from-source"
 | 
					
						
							|  |  |  |       )
 | 
					
						
							| 
									
										
										
										
											2024-03-27 22:14:29 +09:00
										 |  |  |     else()
 | 
					
						
							| 
									
										
										
										
											2024-10-07 19:13:50 -07:00
										 |  |  |       set(MLX_BUILD_METAL OFF)
 | 
					
						
							| 
									
										
										
										
											2024-03-27 22:14:29 +09:00
										 |  |  |       message(WARNING "Building for x86_64 arch is not officially supported.")
 | 
					
						
							|  |  |  |     endif()
 | 
					
						
							| 
									
										
										
										
											2023-12-08 11:03:25 -08:00
										 |  |  |   endif()
 | 
					
						
							|  |  |  | else()
 | 
					
						
							| 
									
										
										
										
											2024-10-07 19:13:50 -07:00
										 |  |  |   set(MLX_BUILD_METAL OFF)
 | 
					
						
							| 
									
										
										
										
											2023-12-08 11:03:25 -08:00
										 |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-29 08:43:22 +09:00
										 |  |  | if(MLX_USE_CCACHE)
 | 
					
						
							|  |  |  |   find_program(CCACHE_PROGRAM ccache)
 | 
					
						
							|  |  |  |   if(CCACHE_PROGRAM)
 | 
					
						
							|  |  |  |     set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
 | 
					
						
							|  |  |  |     set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
 | 
					
						
							|  |  |  |     set(CMAKE_CUDA_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
 | 
					
						
							|  |  |  |   endif()
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | # ----------------------------- Lib -----------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | include(FetchContent)
 | 
					
						
							|  |  |  | # Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
 | 
					
						
							|  |  |  | cmake_policy(SET CMP0135 NEW)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | add_library(mlx)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-07 13:26:46 +09:00
										 |  |  | if(MLX_BUILD_CUDA)
 | 
					
						
							|  |  |  |   enable_language(CUDA)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-09 08:27:18 +09:00
										 |  |  | if(MLX_BUILD_METAL)
 | 
					
						
							|  |  |  |   find_library(METAL_LIB Metal)
 | 
					
						
							|  |  |  |   find_library(FOUNDATION_LIB Foundation)
 | 
					
						
							|  |  |  |   find_library(QUARTZ_LIB QuartzCore)
 | 
					
						
							|  |  |  |   if(METAL_LIB)
 | 
					
						
							|  |  |  |     message(STATUS "Metal found ${METAL_LIB}")
 | 
					
						
							|  |  |  |   else()
 | 
					
						
							|  |  |  |     message( | 
					
						
							|  |  |  |       FATAL_ERROR
 | 
					
						
							|  |  |  |         "Metal not found. Set MLX_BUILD_METAL=OFF to build without GPU")
 | 
					
						
							|  |  |  |   endif()
 | 
					
						
							| 
									
										
										
										
											2024-03-28 09:40:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   if(MLX_METAL_DEBUG)
 | 
					
						
							| 
									
										
										
										
											2024-03-28 09:40:31 -07:00
										 |  |  |     add_compile_definitions(MLX_METAL_DEBUG)
 | 
					
						
							|  |  |  |   endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 09:48:42 -08:00
										 |  |  |   # Throw an error if xcrun not found
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   execute_process( | 
					
						
							|  |  |  |     COMMAND zsh "-c" "/usr/bin/xcrun -sdk macosx --show-sdk-version"
 | 
					
						
							| 
									
										
										
										
											2025-09-09 08:27:18 +09:00
										 |  |  |     OUTPUT_VARIABLE MACOS_SDK_VERSION
 | 
					
						
							|  |  |  |     OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-18 19:16:49 -08:00
										 |  |  |   if(${MACOS_SDK_VERSION} LESS 14.0)
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     message( | 
					
						
							|  |  |  |       FATAL_ERROR
 | 
					
						
							|  |  |  |         "MLX requires macOS SDK >= 14.0 to be built with MLX_BUILD_METAL=ON")
 | 
					
						
							| 
									
										
										
										
											2024-06-26 07:02:11 -07:00
										 |  |  |   endif()
 | 
					
						
							| 
									
										
										
										
											2024-11-18 19:16:49 -08:00
										 |  |  |   message(STATUS "Building with macOS SDK version ${MACOS_SDK_VERSION}")
 | 
					
						
							| 
									
										
										
										
											2024-01-26 09:27:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   set(METAL_CPP_URL | 
					
						
							| 
									
										
										
										
											2024-12-09 22:40:35 -05:00
										 |  |  |       https://developer.apple.com/metal/cpp/files/metal-cpp_macOS15_iOS18.zip)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-19 08:45:05 -08:00
										 |  |  |   if(NOT CMAKE_OSX_DEPLOYMENT_TARGET STREQUAL "")
 | 
					
						
							|  |  |  |     set(XCRUN_FLAGS "-mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
 | 
					
						
							|  |  |  |   endif()
 | 
					
						
							|  |  |  |   execute_process( | 
					
						
							|  |  |  |     COMMAND
 | 
					
						
							|  |  |  |       zsh "-c"
 | 
					
						
							|  |  |  |       "echo \"__METAL_VERSION__\" | xcrun -sdk macosx metal ${XCRUN_FLAGS} -E -x metal -P - | tail -1 | tr -d '\n'"
 | 
					
						
							|  |  |  |     OUTPUT_VARIABLE MLX_METAL_VERSION COMMAND_ERROR_IS_FATAL ANY)
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   FetchContent_Declare(metal_cpp URL ${METAL_CPP_URL})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   FetchContent_MakeAvailable(metal_cpp)
 | 
					
						
							|  |  |  |   target_include_directories( | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     mlx PUBLIC $<BUILD_INTERFACE:${metal_cpp_SOURCE_DIR}>
 | 
					
						
							|  |  |  |                $<INSTALL_INTERFACE:include/metal_cpp>)
 | 
					
						
							|  |  |  |   target_link_libraries(mlx PUBLIC ${METAL_LIB} ${FOUNDATION_LIB} ${QUARTZ_LIB})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-27 08:06:46 +09:00
										 |  |  | if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
 | 
					
						
							|  |  |  |   # With newer clang/gcc versions following libs are implicitly linked, but when
 | 
					
						
							|  |  |  |   # building on old distributions they need to be explicitly listed.
 | 
					
						
							|  |  |  |   target_link_libraries(mlx PRIVATE dl pthread)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  | if(WIN32)
 | 
					
						
							|  |  |  |   if(MSVC)
 | 
					
						
							|  |  |  |     # GGUF does not build with MSVC.
 | 
					
						
							|  |  |  |     set(MLX_BUILD_GGUF OFF)
 | 
					
						
							|  |  |  |     # There is no prebuilt OpenBLAS distribution for MSVC.
 | 
					
						
							|  |  |  |     set(MLX_BUILD_BLAS_FROM_SOURCE ON)
 | 
					
						
							|  |  |  |   endif()
 | 
					
						
							|  |  |  |   # Windows implementation of dlfcn.h APIs.
 | 
					
						
							|  |  |  |   FetchContent_Declare( | 
					
						
							|  |  |  |     dlfcn-win32
 | 
					
						
							|  |  |  |     GIT_REPOSITORY https://github.com/dlfcn-win32/dlfcn-win32.git
 | 
					
						
							|  |  |  |     GIT_TAG v1.4.1
 | 
					
						
							|  |  |  |     EXCLUDE_FROM_ALL)
 | 
					
						
							| 
									
										
										
										
											2024-12-14 12:50:39 +09:00
										 |  |  |   block()
 | 
					
						
							|  |  |  |   set(BUILD_SHARED_LIBS OFF)
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  |   FetchContent_MakeAvailable(dlfcn-win32)
 | 
					
						
							| 
									
										
										
										
											2024-12-14 12:50:39 +09:00
										 |  |  |   endblock()
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  |   target_include_directories(mlx PRIVATE "${dlfcn-win32_SOURCE_DIR}/src")
 | 
					
						
							|  |  |  |   target_link_libraries(mlx PRIVATE dl)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | if(MLX_BUILD_CPU)
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |   find_library(ACCELERATE_LIBRARY Accelerate)
 | 
					
						
							| 
									
										
										
										
											2024-11-28 19:55:45 +01:00
										 |  |  |   if(ACCELERATE_LIBRARY)
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |     message(STATUS "Accelerate found ${ACCELERATE_LIBRARY}")
 | 
					
						
							|  |  |  |     set(MLX_BUILD_ACCELERATE ON)
 | 
					
						
							|  |  |  |   else()
 | 
					
						
							| 
									
										
										
										
											2025-10-01 16:43:45 -07:00
										 |  |  |     message(STATUS "Accelerate not found, using default backend.")
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |     set(MLX_BUILD_ACCELERATE OFF)
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  |   endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if(MLX_BUILD_ACCELERATE)
 | 
					
						
							|  |  |  |     target_link_libraries(mlx PUBLIC ${ACCELERATE_LIBRARY})
 | 
					
						
							| 
									
										
										
										
											2025-01-29 14:34:49 -08:00
										 |  |  |     add_compile_definitions(MLX_USE_ACCELERATE)
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  |     add_compile_definitions(ACCELERATE_NEW_LAPACK)
 | 
					
						
							|  |  |  |   elseif(MLX_BUILD_BLAS_FROM_SOURCE)
 | 
					
						
							|  |  |  |     # Download and build OpenBLAS from source code.
 | 
					
						
							|  |  |  |     FetchContent_Declare( | 
					
						
							|  |  |  |       openblas
 | 
					
						
							|  |  |  |       GIT_REPOSITORY https://github.com/OpenMathLib/OpenBLAS.git
 | 
					
						
							|  |  |  |       GIT_TAG v0.3.28
 | 
					
						
							|  |  |  |       EXCLUDE_FROM_ALL)
 | 
					
						
							|  |  |  |     set(BUILD_STATIC_LIBS ON) # link statically
 | 
					
						
							|  |  |  |     set(NOFORTRAN ON) # msvc has no fortran compiler
 | 
					
						
							|  |  |  |     FetchContent_MakeAvailable(openblas)
 | 
					
						
							|  |  |  |     target_link_libraries(mlx PRIVATE openblas)
 | 
					
						
							|  |  |  |     target_include_directories( | 
					
						
							|  |  |  |       mlx PRIVATE "${openblas_SOURCE_DIR}/lapack-netlib/LAPACKE/include"
 | 
					
						
							|  |  |  |                   "${CMAKE_BINARY_DIR}/generated" "${CMAKE_BINARY_DIR}")
 | 
					
						
							|  |  |  |   else()
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |     if(${CMAKE_HOST_APPLE})
 | 
					
						
							|  |  |  |       # The blas shipped in macOS SDK is not supported, search homebrew for
 | 
					
						
							|  |  |  |       # openblas instead.
 | 
					
						
							|  |  |  |       set(BLA_VENDOR OpenBLAS)
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |       set(LAPACK_ROOT | 
					
						
							|  |  |  |           "${LAPACK_ROOT};$ENV{LAPACK_ROOT};/usr/local/opt/openblas")
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |     endif()
 | 
					
						
							|  |  |  |     # Search and link with lapack.
 | 
					
						
							|  |  |  |     find_package(LAPACK REQUIRED)
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     if(NOT LAPACK_FOUND)
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |       message(FATAL_ERROR "Must have LAPACK installed")
 | 
					
						
							|  |  |  |     endif()
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     find_path(LAPACK_INCLUDE_DIRS lapacke.h /usr/include /usr/local/include | 
					
						
							|  |  |  |               /usr/local/opt/openblas/include)
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |     message(STATUS "Lapack lib " ${LAPACK_LIBRARIES})
 | 
					
						
							|  |  |  |     message(STATUS "Lapack include " ${LAPACK_INCLUDE_DIRS})
 | 
					
						
							|  |  |  |     target_include_directories(mlx PRIVATE ${LAPACK_INCLUDE_DIRS})
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  |     target_link_libraries(mlx PRIVATE ${LAPACK_LIBRARIES})
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     # List blas after lapack otherwise we may accidentally incldue an old
 | 
					
						
							|  |  |  |     # version of lapack.h from the include dirs of blas.
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |     find_package(BLAS REQUIRED)
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     if(NOT BLAS_FOUND)
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |       message(FATAL_ERROR "Must have BLAS installed")
 | 
					
						
							|  |  |  |     endif()
 | 
					
						
							|  |  |  |     # TODO find a cleaner way to do this
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     find_path(BLAS_INCLUDE_DIRS cblas.h /usr/include /usr/local/include | 
					
						
							|  |  |  |               $ENV{BLAS_HOME}/include)
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |     message(STATUS "Blas lib " ${BLAS_LIBRARIES})
 | 
					
						
							|  |  |  |     message(STATUS "Blas include " ${BLAS_INCLUDE_DIRS})
 | 
					
						
							|  |  |  |     target_include_directories(mlx PRIVATE ${BLAS_INCLUDE_DIRS})
 | 
					
						
							| 
									
										
										
										
											2024-12-11 09:14:44 +09:00
										 |  |  |     target_link_libraries(mlx PRIVATE ${BLAS_LIBRARIES})
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:08:11 -07:00
										 |  |  |   endif()
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | else()
 | 
					
						
							|  |  |  |   set(MLX_BUILD_ACCELERATE OFF)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-06 15:35:56 -08:00
										 |  |  | message(STATUS "Downloading json")
 | 
					
						
							|  |  |  | FetchContent_Declare( | 
					
						
							|  |  |  |   json
 | 
					
						
							|  |  |  |   URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
 | 
					
						
							|  |  |  | FetchContent_MakeAvailable(json)
 | 
					
						
							|  |  |  | target_include_directories( | 
					
						
							|  |  |  |   mlx PRIVATE $<BUILD_INTERFACE:${json_SOURCE_DIR}/single_include/nlohmann>)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/mlx)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | target_include_directories( | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   mlx PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
 | 
					
						
							|  |  |  |              $<INSTALL_INTERFACE:include>)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-23 22:14:58 +09:00
										 |  |  | # Do not add mlx_EXPORTS define for shared library.
 | 
					
						
							|  |  |  | set_target_properties(mlx PROPERTIES DEFINE_SYMBOL "")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-05 01:36:11 +02:00
										 |  |  | if(USE_SYSTEM_FMT)
 | 
					
						
							|  |  |  |   find_package(fmt REQUIRED)
 | 
					
						
							|  |  |  | else()
 | 
					
						
							|  |  |  |   FetchContent_Declare( | 
					
						
							|  |  |  |     fmt
 | 
					
						
							|  |  |  |     GIT_REPOSITORY https://github.com/fmtlib/fmt.git
 | 
					
						
							|  |  |  |     GIT_TAG 10.2.1
 | 
					
						
							|  |  |  |     EXCLUDE_FROM_ALL)
 | 
					
						
							|  |  |  |   FetchContent_MakeAvailable(fmt)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							| 
									
										
										
										
											2024-09-16 13:32:28 -07:00
										 |  |  | target_link_libraries(mlx PRIVATE $<BUILD_INTERFACE:fmt::fmt-header-only>)
 | 
					
						
							| 
									
										
										
										
											2024-05-22 12:57:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | if(MLX_BUILD_PYTHON_BINDINGS)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  |   message(STATUS "Building Python bindings.")
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   find_package( | 
					
						
							|  |  |  |     Python 3.8
 | 
					
						
							|  |  |  |     COMPONENTS Interpreter Development.Module
 | 
					
						
							|  |  |  |     REQUIRED)
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:12:25 -07:00
										 |  |  |   execute_process( | 
					
						
							|  |  |  |     COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     OUTPUT_STRIP_TRAILING_WHITESPACE
 | 
					
						
							| 
									
										
										
										
											2024-12-17 10:57:54 -08:00
										 |  |  |     OUTPUT_VARIABLE nanobind_ROOT)
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:12:25 -07:00
										 |  |  |   find_package(nanobind CONFIG REQUIRED)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  |   add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/python/src)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | if(MLX_BUILD_TESTS)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  |   include(CTest)
 | 
					
						
							|  |  |  |   add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tests)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | if(MLX_BUILD_EXAMPLES)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  |   add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/examples/cpp)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | if(MLX_BUILD_BENCHMARKS)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  |   add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/benchmarks/cpp)
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ----------------------------- Installation -----------------------------
 | 
					
						
							|  |  |  | include(GNUInstallDirs)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Install library
 | 
					
						
							|  |  |  | install( | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   TARGETS mlx
 | 
					
						
							|  |  |  |   EXPORT MLXTargets
 | 
					
						
							|  |  |  |   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
 | 
					
						
							|  |  |  |   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
 | 
					
						
							| 
									
										
										
										
											2024-12-19 12:26:04 +09:00
										 |  |  |   RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   INCLUDES
 | 
					
						
							|  |  |  |   DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Install headers
 | 
					
						
							|  |  |  | install( | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/mlx
 | 
					
						
							|  |  |  |   DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
 | 
					
						
							|  |  |  |   COMPONENT headers
 | 
					
						
							|  |  |  |   FILES_MATCHING
 | 
					
						
							| 
									
										
										
										
											2024-09-16 13:32:28 -07:00
										 |  |  |   PATTERN "*.h"
 | 
					
						
							|  |  |  |   PATTERN "backend/metal/kernels.h" EXCLUDE)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Install metal dependencies
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | if(MLX_BUILD_METAL)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Install metal cpp
 | 
					
						
							|  |  |  |   install( | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |     DIRECTORY ${metal_cpp_SOURCE_DIR}/
 | 
					
						
							|  |  |  |     DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/metal_cpp
 | 
					
						
							|  |  |  |     COMPONENT metal_cpp_source)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | endif()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Install cmake config
 | 
					
						
							|  |  |  | set(MLX_CMAKE_BUILD_CONFIG ${CMAKE_BINARY_DIR}/MLXConfig.cmake)
 | 
					
						
							|  |  |  | set(MLX_CMAKE_BUILD_VERSION_CONFIG ${CMAKE_BINARY_DIR}/MLXConfigVersion.cmake)
 | 
					
						
							|  |  |  | set(MLX_CMAKE_INSTALL_MODULE_DIR share/cmake/MLX)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | install( | 
					
						
							|  |  |  |   EXPORT MLXTargets
 | 
					
						
							|  |  |  |   FILE MLXTargets.cmake
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   DESTINATION ${MLX_CMAKE_INSTALL_MODULE_DIR})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | include(CMakePackageConfigHelpers)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | write_basic_package_version_file( | 
					
						
							|  |  |  |   ${MLX_CMAKE_BUILD_VERSION_CONFIG}
 | 
					
						
							|  |  |  |   COMPATIBILITY SameMajorVersion
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   VERSION ${MLX_VERSION})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | configure_package_config_file( | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   ${CMAKE_CURRENT_LIST_DIR}/mlx.pc.in ${MLX_CMAKE_BUILD_CONFIG}
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  |   INSTALL_DESTINATION ${MLX_CMAKE_INSTALL_MODULE_DIR}
 | 
					
						
							|  |  |  |   NO_CHECK_REQUIRED_COMPONENTS_MACRO
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  |   PATH_VARS CMAKE_INSTALL_LIBDIR CMAKE_INSTALL_INCLUDEDIR
 | 
					
						
							|  |  |  |             MLX_CMAKE_INSTALL_MODULE_DIR)
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | install(FILES ${MLX_CMAKE_BUILD_CONFIG} ${MLX_CMAKE_BUILD_VERSION_CONFIG} | 
					
						
							|  |  |  |         DESTINATION ${MLX_CMAKE_INSTALL_MODULE_DIR})
 | 
					
						
							| 
									
										
										
										
											2023-11-29 10:42:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 20:53:01 +01:00
										 |  |  | install(DIRECTORY ${CMAKE_MODULE_PATH}/ | 
					
						
							|  |  |  |         DESTINATION ${MLX_CMAKE_INSTALL_MODULE_DIR})
 |