Files
core-cooldown/src-tauri/build.rs
Your Name e9021e51e5 Statically link WebView2Loader - single exe, no DLL needed
build.rs swaps the dynamic import library with the static archive
from webview2-com-sys, so the WebView2 loader code is baked into
the exe. msvc_compat.rs provides the MSVC CRT symbols (security
cookie, thread-safe init, C++ operators) that the MSVC-compiled
static library expects.
2026-02-07 02:32:54 +02:00

82 lines
2.7 KiB
Rust

fn main() {
// Ensure MinGW tools are on PATH for build scripts (windres, dlltool, etc.)
let mingw_bin = "C:/Users/lashman/mingw-w64/mingw64/bin";
if let Ok(current_path) = std::env::var("PATH") {
std::env::set_var("PATH", format!("{};{}", mingw_bin, current_path));
} else {
std::env::set_var("PATH", mingw_bin);
}
// On GNU targets, replace the WebView2Loader import library with the static
// library so the loader is baked into the exe — no DLL to ship.
#[cfg(target_env = "gnu")]
swap_webview2_to_static();
tauri_build::build()
}
/// Replace `WebView2Loader.dll.lib` (dynamic import lib) with the contents of
/// `WebView2LoaderStatic.lib` (static archive) in the webview2-com-sys build
/// output. The linker then statically links the WebView2 loader code, removing
/// the runtime dependency on WebView2Loader.dll.
#[cfg(target_env = "gnu")]
fn swap_webview2_to_static() {
use std::fs;
use std::path::PathBuf;
let out_dir = std::env::var("OUT_DIR").unwrap_or_default();
// OUT_DIR = target/.../build/core-cooldown-HASH/out
// We need: target/.../build/ (two levels up)
let build_dir = PathBuf::from(&out_dir)
.parent() // core-cooldown-HASH
.and_then(|p| p.parent()) // build/
.map(|p| p.to_path_buf());
let build_dir = match build_dir {
Some(d) => d,
None => return,
};
let target_arch = match std::env::var("CARGO_CFG_TARGET_ARCH")
.unwrap_or_default()
.as_str()
{
"x86_64" => "x64",
"x86" => "x86",
"aarch64" => "arm64",
_ => return,
};
let entries = match fs::read_dir(&build_dir) {
Ok(e) => e,
Err(_) => return,
};
for entry in entries.flatten() {
let name = entry.file_name();
let name = name.to_string_lossy();
if !name.starts_with("webview2-com-sys-") {
continue;
}
let lib_dir = entry.path().join("out").join(target_arch);
let import_lib = lib_dir.join("WebView2Loader.dll.lib");
let static_lib = lib_dir.join("WebView2LoaderStatic.lib");
if static_lib.exists() && import_lib.exists() {
if let Ok(static_bytes) = fs::read(&static_lib) {
match fs::write(&import_lib, &static_bytes) {
Ok(_) => println!(
"cargo:warning=Swapped WebView2Loader to static linking ({})",
lib_dir.display()
),
Err(e) => println!(
"cargo:warning=Failed to swap WebView2Loader lib: {}",
e
),
}
}
}
}
}