use std::{
env,
path::{Path, PathBuf},
process::Command,
};
fn xcrun(args: &[&str]) -> PathBuf {
let out = Command::new("xcrun")
.args(args)
.output()
.expect("xcrun is required to build Gent Desktop");
assert!(out.status.success(), "xcrun {args:?} failed");
PathBuf::from(String::from_utf8(out.stdout).expect("xcrun path").trim())
}
pub fn build() {
let arch = match env::var("CARGO_CFG_TARGET_ARCH").as_deref() {
Ok("aarch64") => "arm64",
Ok("x86_64") => "x86_64",
_ => panic!("unsupported macOS architecture"),
};
let min = env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| "11.0".into());
println!("cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET");
println!("cargo:rerun-if-changed=native");
let out = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let sdk = xcrun(&["--sdk", "macosx", "--show-sdk-path"]);
let swiftc = xcrun(&["--sdk", "macosx", "--find", "swiftc"]);
let ok = Command::new(&swiftc)
.args([
"-swift-version",
"5",
"-parse-as-library",
"-O",
"-emit-library",
"-static",
"-module-name",
"GentNative",
"-sdk",
])
.arg(&sdk)
.args([
"-target",
&format!("{arch}-apple-macos{min}"),
"native/Delivery.swift",
"native/Launcher.swift",
"native/Bridge.swift",
"-o",
])
.arg(out.join("libGentNative.a"))
.status()
.expect("could not run swiftc");
assert!(
ok.success(),
"could not compile the native notification and launch bridge"
);
let toolchain = swiftc
.parent()
.and_then(Path::parent)
.unwrap()
.join("lib/swift/macosx");
for dir in [out, toolchain, sdk.join("usr/lib/swift")] {
println!("cargo:rustc-link-search=native={}", dir.display());
}
println!("cargo:rustc-link-lib=static=GentNative");
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/swift");
}