wv-sys 0.2.1

Webview raw ffi bindings
Documentation
use cmake::Config;
use std::env;
use std::path::PathBuf;

fn main() {
    let target = env::var("TARGET").unwrap();
    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
    let exe_pth = out_dir.clone();

    println!("cargo:rerun-if-changed=webview");
    println!("cargo:rerun-if-changed=libs");

    let mut dst = Config::new("webview");

    if target.contains("windows") {
        let edge_weview_native = "libs".to_string();
        let include = edge_weview_native.clone();
        dst.define(
            "CMAKE_C_STANDARD_INCLUDE_DIRECTORIES=",
            format!("{}/include", include),
        );

        for &lib in &[
            "user32", "oleaut32", "ole32", "version", "shell32", "advapi32", "shlwapi",
        ] {
            println!("cargo:rustc-link-lib={}", lib);
        }

        let wv_arch = if target.contains("x86_64") {
            "x64"
        } else if target.contains("i686") {
            "x86"
        } else {
            "arm64"
        };

        let mut wv_path = manifest_dir;

        wv_path.push(edge_weview_native);

        wv_path.push(wv_arch);
        let webview2_dir = wv_path.as_path().to_str().unwrap();
        println!("cargo:rustc-link-search={}", webview2_dir);
        println!(
            "cargo:rustc-link-search={}",
            out_dir.join("../../..").display()
        );
        if target.contains("msvc") {
            println!("cargo:rustc-link-lib=WebView2LoaderStatic");
        } else {
            if !target.contains("aarch64") {
                println!("cargo:rustc-link-lib=WebView2Loader");
                for entry in std::fs::read_dir(wv_path).expect("Can't read DLL dir") {
                    let entry_path = entry.expect("Invalid fs entry").path();
                    let file_name_result = entry_path.file_name();
                    let mut exe_pth = exe_pth.clone();
                    if let Some(file_name) = file_name_result {
                        let file_name = file_name.to_str().unwrap();
                        if file_name.ends_with(".dll") {
                            exe_pth.push("../../..");
                            let mut for_examples_exe_pth = exe_pth.clone();
                            for_examples_exe_pth.push("examples");
                            exe_pth.push(file_name);
                            std::fs::copy(&entry_path, exe_pth.as_path())
                                .expect("Can't copy from DLL dir /target/..");

                            // Copy .dll to examples folder too, in order to run examples when cross compiling from linux.
                            for_examples_exe_pth.push(file_name);
                            std::fs::copy(&entry_path, for_examples_exe_pth.as_path())
                                .expect("Can't copy from DLL dir to /target/../examples");
                        }
                    }
                }
            } else {
                panic!("{:?} not supported yet", target)
            }
        }
    } else if target.contains("apple") {
        println!("cargo:rustc-link-lib=framework=Cocoa");
        println!("cargo:rustc-link-lib=framework=WebKit");
    } else if target.contains("linux") || target.contains("bsd") {
        dst.define("WEBVIEW_WEBKITGTK_API", "4.1");
        let lib = pkg_config::Config::new()
            .atleast_version("2.8")
            .probe("webkit2gtk-4.1")
            .unwrap();
        for path in lib.libs {
            println!("cargo:rustc-link-lib={}", path);
        }
    } else {
        panic!("Unsupported platform");
    }

    let dst = dst
        .profile("Release")
        .define("WEBVIEW_BUILD_DOCS", "OFF")
        .define("WEBVIEW_BUILD_EXAMPLES", "OFF")
        .define("WEBVIEW_BUILD_DOCS", "OFF")
        .define("WEBVIEW_BUILD_STATIC_LIBRARY", "ON")
        .define("WEBVIEW_BUILD_SHARED_LIBRARY", "OFF")
        .define("WEBVIEW_BUILD_AMALGAMATION", "OFF")
        .define("WEBVIEW_IS_CI", "OFF")
        .define("WEBVIEW_BUILD_DOCS", "OFF")
        .define("WEBVIEW_BUILD_TESTS", "OFF")
        .define("WEBVIEW_BUILD", "ON")
        .define("WEBVIEW_INSTALL", "ON")
        .build();
    println!(
        "cargo:rustc-link-search=native={}",
        dst.join("lib").display()
    );
    if target.contains("msvc") {
        println!("cargo:rustc-link-lib=static=webview_static");
    } else {
        println!("cargo:rustc-link-lib=static=webview");
    }
    if target.contains("gnu") || target.contains("musl") {
        println!("cargo:rustc-link-lib=stdc++");
    } else if target.contains("apple") {
        println!("cargo:rustc-link-lib=c++");
    }
}