-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.rs
112 lines (96 loc) · 3.53 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
// 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.
use std::env;
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(feature = "gecko"))]
const fn clang_args() -> Vec<String> {
Vec::new()
}
fn bindgen() {
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
// Platforms currently not supported.
//
// See <https://github.com/mozilla/mtu/issues/82>.
if matches!(target_os.as_str(), "ios" | "tvos" | "visionos") {
return;
}
if target_os == "windows" {
return;
}
let bindings = if matches!(target_os.as_str(), "linux" | "android") {
bindgen::Builder::default()
.header_contents("rtnetlink.h", "#include <linux/rtnetlink.h>")
// Only generate bindings for the following types
.allowlist_type("rtattr|rtmsg|ifinfomsg|nlmsghdr")
} else {
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(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());
}
fn main() {
// Setup cfg aliases
cfg_aliases::cfg_aliases! {
bsd: {
any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "solaris"
)
}
}
bindgen();
}