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", None) .without("openssl", None) .with("gnutls-tss2", Some("no")) .with("vpnc-script", Some("/etc/vpnc/vpnc-script")); // Specify vpnc-script, otherwise it will fail on macOS if target_os() == "macos" { config.disable_static().enable_shared(); config.ldflag("-liconv"); config.env("LIBS", "-liconv"); } if target_os() != "macos" { config.enable_static().disable_shared(); } if matches!(target_os().as_str(), "freebsd" | "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"); { let configuration = std::fs::read_to_string(dst.join("build/config.h")) .expect("Failed to read the OpenConnect TLS backend configuration"); assert!( configuration.lines().any(|line| line == "#define OPENCONNECT_GNUTLS 1") && !configuration .lines() .any(|line| line == "#define OPENCONNECT_OPENSSL 1"), "OpenConnect must use GnuTLS" ); } if target_os() == "macos" { // Do not embed Cargo's temporary build directory in consumers of this library. let library = std::fs::canonicalize(lib_dir.join("libopenconnect.dylib")).unwrap(); let library_name = library.file_name().unwrap().to_str().unwrap(); let status = std::process::Command::new("install_name_tool") .arg("-id") .arg(format!("@rpath/{library_name}")) .arg(&library) .status() .expect("Failed to update the OpenConnect dylib install name"); assert!(status.success(), "Failed to update the OpenConnect dylib install name"); let status = std::process::Command::new("codesign") .args(["--force", "--sign", "-"]) .arg(&library) .status() .expect("Failed to sign the OpenConnect dylib"); assert!(status.success(), "Failed to sign the OpenConnect dylib"); println!("cargo:lib_dir={}", lib_dir.display()); } println!("cargo:rustc-link-search=native={}", lib_dir.display()); if target_os() == "macos" { println!("cargo:rustc-link-lib=dylib=openconnect"); } if 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 target = target_os(); assert!( matches!(target.as_str(), "macos" | "linux" | "freebsd" | "openbsd"), "Unsupported OpenConnect target: {target}" ); 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); println!( "cargo:rustc-env=OPENCONNECT_PROTOCOL_BUILD={}", oc_dst.join("build").display() ); println!( "cargo:rustc-env=OPENCONNECT_PROTOCOL_SOURCE={}", out_dir.join("openconnect_build").display() ); // 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(); if target_os() == "macos" { pkg_config::probe_library("gnutls").unwrap(); } if 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"); if matches!(target_os().as_str(), "freebsd" | "openbsd") { println!("cargo:rustc-link-search=native=/usr/local/lib"); println!("cargo:rustc-link-lib=iconv"); } // Prevent silent fallback to dynamic libraries if 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"); } fn target_os() -> String { env::var("CARGO_CFG_TARGET_OS").expect("Cargo target OS") }