Handy/src-tauri/build.rs
2025-12-12 22:14:17 +07:00

133 lines
4.4 KiB
Rust

fn main() {
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
build_apple_intelligence_bridge();
tauri_build::build()
}
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
fn build_apple_intelligence_bridge() {
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
const REAL_SWIFT_FILE: &str = "swift/apple_intelligence.swift";
const STUB_SWIFT_FILE: &str = "swift/apple_intelligence_stub.swift";
const BRIDGE_HEADER: &str = "swift/apple_intelligence_bridge.h";
println!("cargo:rerun-if-changed={REAL_SWIFT_FILE}");
println!("cargo:rerun-if-changed={STUB_SWIFT_FILE}");
println!("cargo:rerun-if-changed={BRIDGE_HEADER}");
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
let object_path = out_dir.join("apple_intelligence.o");
let static_lib_path = out_dir.join("libapple_intelligence.a");
let sdk_path = String::from_utf8(
Command::new("xcrun")
.args(["--sdk", "macosx", "--show-sdk-path"])
.output()
.expect("Failed to locate macOS SDK")
.stdout,
)
.expect("SDK path is not valid UTF-8")
.trim()
.to_string();
// Check if the SDK supports FoundationModels (required for Apple Intelligence)
let framework_path =
Path::new(&sdk_path).join("System/Library/Frameworks/FoundationModels.framework");
let has_foundation_models = framework_path.exists();
let source_file = if has_foundation_models {
println!("cargo:warning=Building with Apple Intelligence support.");
REAL_SWIFT_FILE
} else {
println!("cargo:warning=Apple Intelligence SDK not found. Building with stubs.");
STUB_SWIFT_FILE
};
if !Path::new(source_file).exists() {
panic!("Source file {} is missing!", source_file);
}
let swiftc_path = String::from_utf8(
Command::new("xcrun")
.args(["--find", "swiftc"])
.output()
.expect("Failed to locate swiftc")
.stdout,
)
.expect("swiftc path is not valid UTF-8")
.trim()
.to_string();
let toolchain_swift_lib = Path::new(&swiftc_path)
.parent()
.and_then(|p| p.parent())
.map(|root| root.join("lib/swift/macosx"))
.expect("Unable to determine Swift toolchain lib directory");
let sdk_swift_lib = Path::new(&sdk_path).join("usr/lib/swift");
// Use macOS 11.0 as deployment target for compatibility
// The @available(macOS 26.0, *) checks in Swift handle runtime availability
// Weak linking for FoundationModels is handled via cargo:rustc-link-arg below
let status = Command::new("xcrun")
.args([
"swiftc",
"-target",
"arm64-apple-macosx11.0",
"-sdk",
&sdk_path,
"-O",
"-import-objc-header",
BRIDGE_HEADER,
"-c",
source_file,
"-o",
object_path
.to_str()
.expect("Failed to convert object path to string"),
])
.status()
.expect("Failed to invoke swiftc for Apple Intelligence bridge");
if !status.success() {
panic!("swiftc failed to compile {source_file}");
}
let status = Command::new("libtool")
.args([
"-static",
"-o",
static_lib_path
.to_str()
.expect("Failed to convert static lib path to string"),
object_path
.to_str()
.expect("Failed to convert object path to string"),
])
.status()
.expect("Failed to create static library for Apple Intelligence bridge");
if !status.success() {
panic!("libtool failed for Apple Intelligence bridge");
}
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static=apple_intelligence");
println!(
"cargo:rustc-link-search=native={}",
toolchain_swift_lib.display()
);
println!("cargo:rustc-link-search=native={}", sdk_swift_lib.display());
println!("cargo:rustc-link-lib=framework=Foundation");
if has_foundation_models {
// Use weak linking so the app can launch on systems without FoundationModels
println!("cargo:rustc-link-arg=-weak_framework");
println!("cargo:rustc-link-arg=FoundationModels");
}
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/swift");
}