summaryrefslogtreecommitdiffhomepage
path: root/talpid-wireguard/build.rs
blob: 91a4a4e5d33873d35501bf5f64f828b09fc9f9aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::{env, path::PathBuf};

fn main() {
    let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");

    declare_libs_dir("../dist-assets/binaries");
    declare_libs_dir("../build/lib");

    let link_type = match target_os.as_str() {
        "android" => "",
        "linux" | "macos" => "=static",
        // We would like to avoid panicking on windows even if we can not link correctly
        // because we would like to be able to run check and clippy.
        // This does not allow for correct linking or buijding.
        #[cfg(not(windows))]
        "windows" => "",
        #[cfg(windows)]
        "windows" => "dylib",
        _ => panic!("Unsupported platform: {target_os}"),
    };

    println!("cargo:rustc-link-lib{link_type}=wg");

    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "android"))]
    println!("cargo:rustc-cfg=wireguard_go");
}

fn declare_libs_dir(base: &str) {
    let target_triplet = env::var("TARGET").expect("TARGET is not set");
    let lib_dir = manifest_dir().join(base).join(target_triplet);
    println!("cargo:rerun-if-changed={}", lib_dir.display());
    println!("cargo:rustc-link-search={}", lib_dir.display());
}

fn manifest_dir() -> PathBuf {
    env::var("CARGO_MANIFEST_DIR")
        .map(PathBuf::from)
        .expect("CARGO_MANIFEST_DIR env var not set")
}