fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
if target_os != "macos" {
return;
}
let Ok(out_dir) = std::env::var("OUT_DIR") else {
panic!("OUT_DIR environment variable not set -- this build script must be run by Cargo");
};
cc::Build::new()
.file("src/backend/webkit/host.m")
.flag("-fobjc-arc")
.flag("-fmodules")
.compile("webkit_host_obj");
cc::Build::new()
.file("src/backend/webkit/host_main.c")
.cargo_warnings(false)
.compile("webkit_host_main_obj");
let host_obj = format!("{out_dir}/libwebkit_host_obj.a");
let main_obj = format!("{out_dir}/libwebkit_host_main_obj.a");
let host_bin = format!("{out_dir}/fd_webkit_host");
let tool = cc::Build::new().get_compiler();
let Ok(status) = tool
.to_command()
.args([&host_obj, &main_obj])
.arg("-o")
.arg(&host_bin)
.args(["-framework", "Cocoa"])
.args(["-framework", "WebKit"])
.args(["-framework", "CoreFoundation"])
.status()
else {
panic!("Failed to run linker for webkit host binary");
};
assert!(status.success(), "Failed to link webkit host binary");
let out_path = std::path::Path::new(&out_dir);
if let Some(profile_dir) = out_path.parent().and_then(|p| p.parent()).and_then(|p| p.parent()) {
let dest = profile_dir.join("fd_webkit_host");
if let Err(e) = std::fs::copy(&host_bin, &dest) {
println!("cargo:warning=Could not copy fd_webkit_host to {}: {e}", dest.display());
}
}
if let Some(home) = std::env::var_os("HOME") {
let home = std::path::Path::new(&home);
let mac_cache = home.join("Library/Caches/ferridriver");
if let Ok(()) = std::fs::create_dir_all(&mac_cache) {
let dest = mac_cache.join("fd_webkit_host");
if let Err(e) = std::fs::copy(&host_bin, &dest) {
println!("cargo:warning=Could not copy fd_webkit_host to {}: {e}", dest.display());
}
}
let xdg_cache = home.join(".cache/ferridriver");
if let Ok(()) = std::fs::create_dir_all(&xdg_cache) {
let dest = xdg_cache.join("fd_webkit_host");
if let Err(e) = std::fs::copy(&host_bin, &dest) {
println!("cargo:warning=Could not copy fd_webkit_host to {}: {e}", dest.display());
}
}
}
println!("cargo:rerun-if-changed=src/backend/webkit/host.m");
println!("cargo:rerun-if-changed=src/backend/webkit/host_main.c");
}