forked from mozilla/mtu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
130 lines (119 loc) · 4.47 KB
/
build.rs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(clippy::unwrap_used)] // OK in build scripts.
const BINDINGS: &str = "bindings.rs";
#[cfg(feature = "gecko")]
fn clang_args() -> Vec<String> {
use mozbuild::TOPOBJDIR;
let flags_path = TOPOBJDIR.join("netwerk/socket/neqo/extra-bindgen-flags");
println!("cargo:rerun-if-changed={}", flags_path.to_str().unwrap());
let mut flags: Vec<String> = std::fs::read_to_string(flags_path)
.expect("Failed to read extra-bindgen-flags file")
.split_whitespace()
.map(std::borrow::ToOwned::to_owned)
.collect();
flags.push(String::from("-include"));
flags.push(
TOPOBJDIR
.join("dist")
.join("include")
.join("mozilla-config.h")
.to_str()
.unwrap()
.to_string(),
);
flags
}
#[cfg(not(any(feature = "gecko", target_os = "windows")))]
const fn clang_args() -> Vec<String> {
Vec::new()
}
#[cfg(not(windows))]
fn bindgen() {
#[cfg(target_os = "linux")]
let bindings = bindgen::Builder::default()
.header_contents("rtnetlink.h", "#include <linux/rtnetlink.h>")
// Only generate bindings for the following types
.allowlist_type("rtattr|rtmsg|ifinfomsg|nlmsghdr");
#[cfg(not(target_os = "linux"))]
let bindings = bindgen::Builder::default()
.header_contents(
"route.h",
"#include <sys/types.h>\n#include <sys/socket.h>\n#include <net/route.h>\n#include <net/if.h>",
)
// Only generate bindings for the following types and items
.allowlist_type("rt_msghdr|rt_metrics|if_data")
.allowlist_item("RTAX_MAX|RTM_GET|RTM_VERSION|RTA_DST|RTA_IFP");
let bindings = bindings
.clang_args(clang_args())
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Constants should be generated as &CStr instead of &[u8].
.generate_cstr(true)
// Always emit explicit padding fields.
.explicit_padding(true)
// Default trait should be derived when possible
.derive_default(true)
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/$BINDINGS file.
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join(BINDINGS);
bindings
.write_to_file(out_path.clone())
.expect("Couldn't write bindings!");
println!("cargo:rustc-env=BINDINGS={}", out_path.display());
}
#[cfg(windows)]
fn bindgen() {
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join(BINDINGS);
windows_bindgen::bindgen([
"--out",
out_path.to_str().unwrap(),
"--config",
"flatten",
"no-inner-attributes",
"minimal",
"--filter",
"Windows.Win32.Foundation.NO_ERROR",
"Windows.Win32.Networking.WinSock.AF_INET",
"Windows.Win32.Networking.WinSock.AF_INET6",
"Windows.Win32.Networking.WinSock.SOCKADDR_INET",
"Windows.Win32.NetworkManagement.IpHelper.FreeMibTable",
"Windows.Win32.NetworkManagement.IpHelper.GetBestInterfaceEx",
"Windows.Win32.NetworkManagement.IpHelper.GetIpInterfaceTable",
"Windows.Win32.NetworkManagement.IpHelper.if_indextoname",
"Windows.Win32.NetworkManagement.IpHelper.MIB_IPINTERFACE_ROW",
"Windows.Win32.NetworkManagement.Ndis.IF_MAX_STRING_SIZE",
])
.expect("Couldn't write bindings!");
println!("cargo:rustc-env=BINDINGS={}", out_path.display());
}
fn main() {
// Setup cfg aliases
cfg_aliases::cfg_aliases! {
// Platforms
apple: {
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "visionos"
)
},
bsd: {
any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "solaris"
)
}
}
bindgen();
}