Skip to content

Commit 6a3d0d0

Browse files
authored
Merge pull request #18 from ohadravid/zigbuild
Improve the build script for `cargo zigbuild` support
2 parents 43295af + 4962736 commit 6a3d0d0

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "netstat2"
3-
version = "0.10.0"
3+
version = "0.10.1"
44
authors = ["Ohad Ravid <ohad.rv@gmail.com>", "ivxvm <ivxvm@protonmail.com>"]
55
edition = "2021"
66
license = "MIT OR Apache-2.0"

build.rs

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,59 @@
11
use std::env;
22
use std::path::Path;
33

4-
#[cfg(any(target_os = "macos", target_os = "ios"))]
4+
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "linux", target_os = "android"))]
55
fn main() {
6-
let bindings = bindgen::builder()
7-
.header_contents("libproc_rs.h", "#include <libproc.h>")
8-
.layout_tests(false)
9-
.clang_args(&["-x", "c++", "-I", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/"])
10-
.generate()
11-
.expect("Failed to build libproc bindings");
12-
13-
let output_path = Path::new(&env::var("OUT_DIR")
14-
.expect("OUT_DIR env var was not defined"))
15-
.join("libproc_bindings.rs");
16-
17-
bindings
18-
.write_to_file(output_path)
19-
.expect("Failed to write libproc bindings");
20-
}
21-
22-
#[cfg(any(target_os = "linux", target_os = "android"))]
23-
fn main() {
24-
let bindings = bindgen::builder()
25-
.header_contents("linux_bindings.h", r#"
26-
#include <linux/sock_diag.h>
27-
#include <linux/inet_diag.h>
28-
#include <linux/rtnetlink.h>
29-
#include <linux/netlink.h>
30-
#include <linux/tcp.h>
31-
"#)
32-
.layout_tests(false)
33-
.generate()
34-
.expect("Failed to build linux bindings");
35-
36-
let output_path = Path::new(&env::var("OUT_DIR")
37-
.expect("OUT_DIR env var was not defined"))
38-
.join("linux_bindings.rs");
39-
40-
bindings
41-
.write_to_file(output_path)
42-
.expect("Failed to write linux bindings");
6+
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
7+
8+
match os.as_str() {
9+
"macos" | "ios" => {
10+
let bindings = bindgen::builder()
11+
.header_contents("libproc_rs.h", "#include <libproc.h>")
12+
.layout_tests(false)
13+
.clang_args(&[
14+
"-x",
15+
"c++",
16+
"-I",
17+
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/",
18+
])
19+
.generate()
20+
.expect("Failed to build libproc bindings");
21+
22+
let output_path =
23+
Path::new(&env::var("OUT_DIR").expect("OUT_DIR env var was not defined"))
24+
.join("libproc_bindings.rs");
25+
26+
bindings
27+
.write_to_file(output_path)
28+
.expect("Failed to write libproc bindings");
29+
}
30+
"linux" | "android" => {
31+
let bindings = bindgen::builder()
32+
.header_contents(
33+
"linux_bindings.h",
34+
r#"
35+
#include <linux/sock_diag.h>
36+
#include <linux/inet_diag.h>
37+
#include <linux/rtnetlink.h>
38+
#include <linux/netlink.h>
39+
#include <linux/tcp.h>
40+
"#,
41+
)
42+
.layout_tests(false)
43+
.generate()
44+
.expect("Failed to build linux bindings");
45+
46+
let output_path =
47+
Path::new(&env::var("OUT_DIR").expect("OUT_DIR env var was not defined"))
48+
.join("linux_bindings.rs");
49+
50+
bindings
51+
.write_to_file(output_path)
52+
.expect("Failed to write linux bindings");
53+
}
54+
_ => {}
55+
}
4356
}
4457

45-
#[cfg(target_os = "windows")]
46-
fn main() {}
47-
48-
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux", target_os = "android", target_os = "windows")))]
49-
fn main() {}
58+
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux", target_os = "android")))]
59+
fn main() {}

0 commit comments

Comments
 (0)