| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![allow(clippy::unwrap_used)] // OK in build scripts. |
| 8 |
|
| 9 |
use std::env; |
| 10 |
|
| 11 |
const BINDINGS: &str = "bindings.rs"; |
| 12 |
|
| 13 |
#[cfg(feature = "gecko")] |
| 14 |
fn clang_args() -> Vec<String> { |
| 15 |
use mozbuild::TOPOBJDIR; |
| 16 |
|
| 17 |
let flags_path = TOPOBJDIR.join("netwerk/socket/neqo/extra-bindgen-flags"); |
| 18 |
println!("cargo:rerun-if-changed={}", flags_path.to_str().unwrap()); |
| 19 |
|
| 20 |
let mut flags: Vec<String> = std::fs::read_to_string(flags_path) |
| 21 |
.expect("Failed to read extra-bindgen-flags file") |
| 22 |
.split_whitespace() |
| 23 |
.map(std::borrow::ToOwned::to_owned) |
| 24 |
.collect(); |
| 25 |
|
| 26 |
flags.push(String::from("-include")); |
| 27 |
flags.push( |
| 28 |
TOPOBJDIR |
| 29 |
.join("dist") |
| 30 |
.join("include") |
| 31 |
.join("mozilla-config.h") |
| 32 |
.to_str() |
| 33 |
.unwrap() |
| 34 |
.to_string(), |
| 35 |
); |
| 36 |
flags |
| 37 |
} |
| 38 |
|
| 39 |
#[cfg(not(feature = "gecko"))] |
| 40 |
const fn clang_args() -> Vec<String> { |
| 41 |
Vec::new() |
| 42 |
} |
| 43 |
|
| 44 |
fn bindgen() { |
| 45 |
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set"); |
| 46 |
|
| 47 |
// Platforms currently not supported. |
| 48 |
// |
| 49 |
// See <https://github.com/mozilla/mtu/issues/82>. |
| 50 |
if matches!(target_os.as_str(), "ios" | "tvos" | "visionos") { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if target_os == "windows" { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
let bindings = if matches!(target_os.as_str(), "linux" | "android") { |
| 59 |
bindgen::Builder::default() |
| 60 |
.header_contents("rtnetlink.h", "#include <linux/rtnetlink.h>") |
| 61 |
// Only generate bindings for the following types |
| 62 |
.allowlist_type("rtattr|rtmsg|ifinfomsg|nlmsghdr") |
| 63 |
} else { |
| 64 |
bindgen::Builder::default() |
| 65 |
.header_contents( |
| 66 |
"route.h", |
| 67 |
"#include <sys/types.h>\n#include <sys/socket.h>\n#include <net/route.h>\n#include <net/if.h>", |
| 68 |
) |
| 69 |
// Only generate bindings for the following types and items |
| 70 |
.allowlist_type("rt_msghdr|rt_metrics|if_data") |
| 71 |
.allowlist_item("RTAX_MAX|RTM_GET|RTM_VERSION|RTA_DST|RTA_IFP") |
| 72 |
}; |
| 73 |
|
| 74 |
let bindings = bindings |
| 75 |
.clang_args(clang_args()) |
| 76 |
// Tell cargo to invalidate the built crate whenever any of the |
| 77 |
// included header files changed. |
| 78 |
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
| 79 |
// Constants should be generated as &CStr instead of &[u8]. |
| 80 |
.generate_cstr(true) |
| 81 |
// Always emit explicit padding fields. |
| 82 |
.explicit_padding(true) |
| 83 |
// Default trait should be derived when possible |
| 84 |
.derive_default(true) |
| 85 |
// Finish the builder and generate the bindings. |
| 86 |
.generate() |
| 87 |
// Unwrap the Result and panic on failure. |
| 88 |
.expect("Unable to generate bindings"); |
| 89 |
|
| 90 |
// Write the bindings to the $OUT_DIR/$BINDINGS file. |
| 91 |
let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap()).join(BINDINGS); |
| 92 |
bindings |
| 93 |
.write_to_file(out_path.clone()) |
| 94 |
.expect("Couldn't write bindings!"); |
| 95 |
println!("cargo:rustc-env=BINDINGS={}", out_path.display()); |
| 96 |
} |
| 97 |
|
| 98 |
fn main() { |
| 99 |
// Setup cfg aliases |
| 100 |
cfg_aliases::cfg_aliases! { |
| 101 |
bsd: { |
| 102 |
any( |
| 103 |
target_os = "freebsd", |
| 104 |
target_os = "openbsd", |
| 105 |
target_os = "netbsd", |
| 106 |
target_os = "solaris" |
| 107 |
) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
bindgen(); |
| 112 |
} |