summaryrefslogtreecommitdiff
path: root/build.rs
blob: bcc9c64a7594f317a27d080ad3a3bd6e7be0f225 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use chrono::{DateTime, Utc};
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

fn main() {
    let builder = tonic_build::configure()
        .type_attribute("netavark_proxy.Lease", "#[derive(serde::Serialize)]")
        .type_attribute("netavark_proxy.DhcpV4Lease", "#[derive(serde::Serialize)]")
        .type_attribute("netavark_proxy.DhcpV6Lease", "#[derive(serde::Serialize)]")
        .type_attribute("netavark_proxy.IPResponse", "#[derive(serde::Serialize)]")
        .type_attribute("netavark_proxy.MacAddress", "#[derive(serde::Serialize)]")
        .type_attribute("netavark_proxy.NvIpv4Addr", "#[derive(serde::Serialize)]")
        .type_attribute("netavark_proxy.Lease", "#[derive(serde::Deserialize)]")
        .type_attribute(
            "netavark_proxy.DhcpV4Lease",
            "#[derive(serde::Deserialize)]",
        )
        .type_attribute(
            "netavark_proxy.DhcpV6Lease",
            "#[derive(serde::Deserialize)]",
        )
        .type_attribute("netavark_proxy.IPResponse", "#[derive(serde::Deserialize)]")
        .type_attribute("netavark_proxy.MacAddress", "#[derive(serde::Deserialize)]")
        .type_attribute("netavark_proxy.NvIpv4Addr", "#[derive(serde::Deserialize)]")
        .type_attribute("netavark_proxy.MacAddress", "#[derive(Eq)]")
        .type_attribute("netavark_proxy.MacAddress", "#[derive(Hash)]")
        .type_attribute(
            "netavark_proxy.NetworkConfig",
            "#[derive(serde::Deserialize)]",
        )
        .type_attribute(
            "netavark_proxy.NetworkConfig",
            "#[derive(serde::Serialize)]",
        )
        .out_dir(PathBuf::from("src/proto-build"));

    builder
        .compile(&[Path::new("src/proto/proxy.proto")], &[Path::new("proto")])
        .unwrap_or_else(|e| panic!("Failed at builder: {:?}", e.to_string()));

    // Generate the default 'cargo:' instruction output
    println!("cargo:rerun-if-changed=build.rs");

    // get timestamp
    let now = match env::var("SOURCE_DATE_EPOCH") {
        Ok(val) => DateTime::from_timestamp(val.parse::<i64>().unwrap(), 0).unwrap(),
        Err(_) => Utc::now(),
    };
    println!("cargo:rustc-env=BUILD_TIMESTAMP={}", now.to_rfc3339());

    // get rust target triple from TARGET env
    println!(
        "cargo:rustc-env=BUILD_TARGET={}",
        std::env::var("TARGET").unwrap()
    );

    // get git commit
    let command = Command::new("git").args(["rev-parse", "HEAD"]).output();
    let commit = match command {
        Ok(output) => String::from_utf8(output.stdout).unwrap(),
        // if error, e.g. build from source without git repo, just show empty string
        Err(_) => "".to_string(),
    };
    println!("cargo:rustc-env=GIT_COMMIT={commit}");

    // Handle default firewall driver.
    // Allowed values "nftables" and "iptables".
    let fwdriver = match env::var("NETAVARK_DEFAULT_FW")
        .unwrap_or("iptables".to_string())
        .as_str()
    {
        "nftables" => "nftables",
        "iptables" => "iptables",
        "none" => "none",
        inv => panic!("Invalid default firewall driver {}", inv),
    };
    println!("cargo:rustc-check-cfg=cfg(default_fw, values(\"nftables\", \"iptables\", \"none\"))");
    println!("cargo:rustc-cfg=default_fw=\"{}\"", fwdriver);
    println!("cargo:rustc-env=DEFAULT_FW={fwdriver}");
}