use std::{env, path::PathBuf}; fn build_libxml2(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { let libxml2_dir = deps_dir.join("libxml2"); let install_dir = out_dir.join("libxml2_install"); // The temporary location where we will build let build_src = out_dir.join("libxml2_build"); // Track changes so cargo rebuilds if submodule changes println!("cargo:rerun-if-changed={}", libxml2_dir.display()); // Prepare the build directory // If the directory exists from a previous run, remove it to ensure a clean slate if build_src.exists() { std::fs::remove_dir_all(&build_src).unwrap(); } if install_dir.exists() { std::fs::remove_dir_all(&install_dir).unwrap(); } std::fs::create_dir_all(&install_dir).unwrap(); std::fs::create_dir_all(&build_src).unwrap(); // Copy the source code to OUT_DIR // We use fs_extra options to copy contents recursively let mut options = fs_extra::dir::CopyOptions::new(); options.overwrite = true; options.content_only = true; // Copy content of src into dest, not src folder itself fs_extra::dir::copy(&libxml2_dir, &build_src, &options).expect("Failed to copy C source code to OUT_DIR"); // Build the libxml2 library using autotools let dst = { let mut config = autotools::Config::new(&build_src); config .out_dir(&install_dir) .reconf("-ivf") .enable_static() .disable_shared() // Disable optional features that complicate static builds .with("python", Some("no")) .with("icu", Some("no")) .with("http", Some("no")) .with("ftp", Some("no")) .with("catalog", Some("no")) .with("docbook", Some("no")) .with("legacy", Some("no")) .with("threads", Some("no")) .with("zlib", Some("no")) .with("lzma", Some("no")) // Ensure position-independent code (safe for static linking) .cflag("-fPIC"); config.build() }; let lib_dir = dst.join("lib"); let include_dir = dst.join("include/libxml2"); // Tell rustc where to find the static library println!("cargo:rustc-link-search=native={}", lib_dir.display()); // Static linking of libxml2 println!("cargo:rustc-link-lib=static=xml2"); // Export include path for downstream use (bindgen / cc) println!("cargo:include={}", include_dir.display()); dst } fn build_openconnect(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { let openconnect_dir = deps_dir.join("openconnect"); let install_dir = out_dir.join("openconnect_install"); // The temporary location where we will build let build_src = out_dir.join("openconnect_build"); // Track changes so cargo rebuilds if submodule changes println!("cargo:rerun-if-changed={}", openconnect_dir.display()); // Prepare the build directory // If the directory exists from a previous run, remove it to ensure a clean slate if build_src.exists() { std::fs::remove_dir_all(&build_src).unwrap(); } if install_dir.exists() { std::fs::remove_dir_all(&install_dir).unwrap(); } std::fs::create_dir_all(&install_dir).unwrap(); std::fs::create_dir_all(&build_src).unwrap(); // Copy the source code to OUT_DIR // We use fs_extra options to copy contents recursively let mut options = fs_extra::dir::CopyOptions::new(); options.overwrite = true; options.content_only = true; // Copy content of src into dest, not src folder itself fs_extra::dir::copy(&openconnect_dir, &build_src, &options).expect("Failed to copy C source code to OUT_DIR"); // Build the OpenConnect library using autotools. Keep the upstream LGPL // library dynamically linked on macOS so it remains replaceable in the app // bundle. Other platforms retain the existing static-link behavior. let mut config = autotools::Config::new(&build_src); config .out_dir(&install_dir) .reconf("-ivf") .disable("nls", None) // disable translations to save space .disable("docs", None) .disable("dsa-tests", None) .without("libproxy", None) .without("stoken", None) .without("libpcsclite", None) .without("libpskc", None) .without("gssapi", None) .with("gnutls-tss2", Some("no")) .with("vpnc-script", Some("/etc/vpnc/vpnc-script")); // Specify vpnc-script, otherwise it will fail on macOS #[cfg(target_os = "macos")] { config.disable_static().enable_shared().without("gnutls", None); config.ldflag("-liconv"); config.env("LIBS", "-liconv"); } #[cfg(not(target_os = "macos"))] config.enable_static().disable_shared(); #[cfg(any(target_os = "freebsd", target_os = "openbsd"))] { config.ldflag("-L/usr/local/lib"); config.ldflag("-liconv"); config.env("LIBS", "-liconv"); } let dst = config.build(); let lib_dir = dst.join("lib"); let include_dir = dst.join("include"); println!("cargo:rustc-link-search=native={}", lib_dir.display()); #[cfg(target_os = "macos")] println!("cargo:rustc-link-lib=dylib=openconnect"); #[cfg(not(target_os = "macos"))] println!("cargo:rustc-link-lib=static=openconnect"); // Export include path for downstream use (bindgen / cc) println!("cargo:include={}", include_dir.display()); dst } fn main() { let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); let deps_dir = PathBuf::from(manifest_dir).join("deps"); let oc_dst = build_openconnect(&deps_dir, &out_dir); // Only statically link libxml2 if `LIBXML2_STATIC` is set if env::var("LIBXML2_STATIC").is_ok() { let _libxml2_dst = build_libxml2(&deps_dir, &out_dir); } else { // Using pkg-config to find system libxml2 pkg_config::probe_library("libxml-2.0").unwrap(); } // Using pkg-config to find system deps pkg_config::probe_library("zlib").unwrap(); #[cfg(target_os = "macos")] pkg_config::probe_library("openssl").unwrap(); #[cfg(not(target_os = "macos"))] { pkg_config::probe_library("liblz4").unwrap(); pkg_config::probe_library("gnutls").unwrap(); // Below are required by gnutls pkg_config::probe_library("p11-kit-1").unwrap(); pkg_config::probe_library("hogweed").unwrap(); pkg_config::probe_library("nettle").unwrap(); pkg_config::probe_library("gmp").unwrap(); } // Compile the vpn.c file println!("cargo:rerun-if-changed=src/ffi/vpn.c"); println!("cargo:rerun-if-changed=src/ffi/vpn.h"); #[cfg(any(target_os = "freebsd", target_os = "openbsd"))] { println!("cargo:rustc-link-search=native=/usr/local/lib"); println!("cargo:rustc-link-lib=iconv"); } // Prevent silent fallback to dynamic libraries #[cfg(target_os = "linux")] { println!("cargo:rustc-link-arg=-Wl,-Bstatic"); println!("cargo:rustc-link-arg=-Wl,-Bdynamic"); } cc::Build::new() .file("src/ffi/vpn.c") .include("src/ffi") .include(&oc_dst.join("include")) .compile("vpn"); }