Skip to content

Commit

Permalink
fix(build.rs): use CARGO_CFG_TARGET_OS and target_os android (#81)
Browse files Browse the repository at this point in the history
In a `build.rs` file `cfg(target_os = "linux")` refers to the target OS of the
`build.rs` binary. The environment variable `CARGO_CFG_TARGET_OS` refers to the
target OS of the final binary.

When cross-compiling, the target OS of the `build.rs` binary and the target OS
of the final binary are not the same. The `mtu`'s `build.rs` should use
`CARGO_CFG_TARGET_OS`.

In addition, `cfg(target_os = "linux")` does not include `android`. This
commit `cfg`s `linux` and `android` everywhere.
  • Loading branch information
mxinden authored Jan 29, 2025
1 parent a5b8746 commit d25c0cf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ homepage = "https://github.com/mozilla/mtu/"
repository = "https://github.com/mozilla/mtu/"
authors = ["The Mozilla Necko Team <[email protected]>"]
readme = "README.md"
version = "0.2.4"
version = "0.2.5"
edition = "2021"
license = "MIT OR Apache-2.0"
# Don't increase beyond what Firefox is currently using:
Expand Down Expand Up @@ -37,8 +37,6 @@ windows = { version = ">=0.58,<0.60", features = [
[build-dependencies]
cfg_aliases = { version = "0.2", default-features = false }
mozbuild = { version = "0.1", default-features = false, optional = true }

[target.'cfg(not(windows))'.build-dependencies]
# Don't increase beyond what Firefox is currently using: https://searchfox.org/mozilla-central/source/Cargo.lock
bindgen = { version = "0.69", default-features = false, features = ["runtime"] }

Expand Down
33 changes: 20 additions & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#![allow(clippy::unwrap_used)] // OK in build scripts.

#[cfg(not(windows))]
use std::env;

const BINDINGS: &str = "bindings.rs";

#[cfg(feature = "gecko")]
Expand Down Expand Up @@ -35,27 +36,34 @@ fn clang_args() -> Vec<String> {
flags
}

#[cfg(not(any(feature = "gecko", target_os = "windows")))]
#[cfg(not(feature = "gecko"))]
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()
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");

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");
.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
Expand All @@ -73,7 +81,7 @@ fn bindgen() {
.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);
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!");
Expand Down Expand Up @@ -102,6 +110,5 @@ fn main() {
}
}

#[cfg(not(windows))]
bindgen();
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! asserted_const_with_type {
#[cfg(any(apple, bsd))]
mod bsd;

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux;

#[cfg(target_os = "windows")]
Expand All @@ -71,7 +71,7 @@ mod routesocket;

#[cfg(any(apple, bsd))]
use bsd::interface_and_mtu_impl;
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
use linux::interface_and_mtu_impl;
#[cfg(target_os = "windows")]
use windows::interface_and_mtu_impl;
Expand Down Expand Up @@ -133,7 +133,7 @@ mod test {

#[cfg(any(apple, target_os = "freebsd",))]
const LOOPBACK: &[NameMtu] = &[NameMtu(Some("lo0"), 16_384), NameMtu(Some("lo0"), 16_384)];
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
const LOOPBACK: &[NameMtu] = &[NameMtu(Some("lo"), 65_536), NameMtu(Some("lo"), 65_536)];
#[cfg(target_os = "windows")]
const LOOPBACK: &[NameMtu] = &[
Expand Down
8 changes: 4 additions & 4 deletions src/routesocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use libc::{fsync, read, socket, write, SOCK_RAW};

use crate::unlikely_err;

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
type AtomicRouteSocketSeq = std::sync::atomic::AtomicU32;
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
type RouteSocketSeq = u32;

#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "android")))]
type AtomicRouteSocketSeq = std::sync::atomic::AtomicI32;
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "android")))]
type RouteSocketSeq = i32;

static SEQ: AtomicRouteSocketSeq = AtomicRouteSocketSeq::new(0);
Expand Down

0 comments on commit d25c0cf

Please sign in to comment.