Link python extension with mlx statically on Windows (#1716)

* Link python extension with mlx statically on Windows

* More readable code
This commit is contained in:
Cheng 2024-12-19 12:26:04 +09:00 committed by GitHub
parent 7480059306
commit ed4ec81bca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 6 deletions

View File

@ -266,10 +266,7 @@ install(
EXPORT MLXTargets EXPORT MLXTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
DESTINATION
# On Windows, DLLs must be put in the same dir with the python bindings.
$<IF:$<BOOL:${WIN32}>,${CMAKE_INSTALL_PREFIX},${CMAKE_INSTALL_BINDIR}>
INCLUDES INCLUDES
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

View File

@ -2,6 +2,7 @@
import datetime import datetime
import os import os
import platform
import re import re
import subprocess import subprocess
import sys import sys
@ -60,7 +61,6 @@ class CMakeBuild(build_ext):
cmake_args = [ cmake_args = [
f"-DCMAKE_INSTALL_PREFIX={extdir}{os.sep}", f"-DCMAKE_INSTALL_PREFIX={extdir}{os.sep}",
f"-DCMAKE_BUILD_TYPE={cfg}", f"-DCMAKE_BUILD_TYPE={cfg}",
"-DBUILD_SHARED_LIBS=ON",
"-DMLX_BUILD_PYTHON_BINDINGS=ON", "-DMLX_BUILD_PYTHON_BINDINGS=ON",
"-DMLX_BUILD_TESTS=OFF", "-DMLX_BUILD_TESTS=OFF",
"-DMLX_BUILD_BENCHMARKS=OFF", "-DMLX_BUILD_BENCHMARKS=OFF",
@ -77,11 +77,18 @@ class CMakeBuild(build_ext):
# Pass version to C++ # Pass version to C++
cmake_args += [f"-DMLX_VERSION={self.distribution.get_version()}"] # type: ignore[attr-defined] cmake_args += [f"-DMLX_VERSION={self.distribution.get_version()}"] # type: ignore[attr-defined]
if sys.platform.startswith("darwin"): if platform.system() == "Darwin":
# Cross-compile support for macOS - respect ARCHFLAGS if set # Cross-compile support for macOS - respect ARCHFLAGS if set
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", "")) archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
if archs: if archs:
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))] cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
if platform.system() == "Windows":
# On Windows DLLs must be put in the same dir with the extension
# while cmake puts mlx.dll into the "bin" sub-dir. Link with mlx
# statically to work around it.
cmake_args += ["-DBUILD_SHARED_LIBS=OFF"]
else:
cmake_args += ["-DBUILD_SHARED_LIBS=ON"]
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
# across all generators. # across all generators.