use std::env;
use std::path::PathBuf;
use std::process::Command;
fn main() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let mlx_src = manifest_dir.join("vendor").join("mlx");
if !mlx_src.join("CMakeLists.txt").exists() {
panic!(
"rlx-mlx-sys: vendor/mlx is empty — run:\n\
\n\
\tgit submodule update --init rlx-mlx-sys/vendor/mlx\n\
\n\
Expected MLX source at {}",
mlx_src.display()
);
}
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let is_macos = target_os == "macos";
if is_macos
&& Command::new("xcrun")
.args(["--find", "metal"])
.output()
.is_err()
{
eprintln!("warning: `xcrun metal` not found; MLX Metal kernels will fail to build");
}
let mlx_build = cmake::Config::new(&mlx_src)
.define("MLX_BUILD_TESTS", "OFF")
.define("MLX_BUILD_EXAMPLES", "OFF")
.define("MLX_BUILD_BENCHMARKS", "OFF")
.define("MLX_BUILD_PYTHON_BINDINGS", "OFF")
.define("MLX_BUILD_PYTHON_STUBS", "OFF")
.define("MLX_BUILD_METAL", if is_macos { "ON" } else { "OFF" })
.define("MLX_BUILD_CPU", "ON")
.define("MLX_BUILD_GGUF", "OFF")
.define("MLX_BUILD_SAFETENSORS", "OFF")
.define("BUILD_SHARED_LIBS", "OFF")
.define("CMAKE_BUILD_TYPE", "Release")
.build();
let mlx_lib_dir = mlx_build.join("lib");
let mlx_include_dir = mlx_build.join("include");
println!("cargo:rustc-link-search=native={}", mlx_lib_dir.display());
let mut shim = cc::Build::new();
shim.cpp(true)
.std("c++20")
.file("cpp/rlx_mlx_shim.cpp")
.include(&mlx_include_dir)
.include(&mlx_src)
.flag_if_supported("-fexceptions")
.flag_if_supported("-fvisibility=hidden")
.warnings(false);
shim.compile("rlx_mlx_shim");
println!("cargo:rustc-link-lib=static=mlx");
if is_macos {
for fw in &["Metal", "Foundation", "QuartzCore", "Accelerate"] {
println!("cargo:rustc-link-lib=framework={fw}");
}
println!("cargo:rustc-link-lib=c++");
} else {
println!("cargo:rustc-link-lib=stdc++");
}
println!("cargo:rerun-if-changed=cpp/rlx_mlx_shim.cpp");
println!("cargo:rerun-if-changed=cpp/rlx_mlx_shim.h");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=vendor/mlx/mlx/version.h");
}