forked from vnt-dev/lwip-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
130 lines (118 loc) · 4.56 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
use std::{
env,
path::{Path, PathBuf},
process::Command,
};
fn sdk_include_path_for(sdk: &str) -> String {
// sdk path find by `xcrun --sdk {iphoneos|macosx} --show-sdk-path`
let output = Command::new("xcrun")
.arg("--sdk")
.arg(sdk)
.arg("--show-sdk-path")
.output()
.expect("failed to execute xcrun");
let inc_path = Path::new(String::from_utf8_lossy(&output.stdout).trim()).join("usr/include");
inc_path.to_str().expect("invalid include path").to_string()
}
fn sdk_include_path() -> Option<String> {
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
match os.as_str() {
"ios" => {
if arch == "x86_64" {
Some(sdk_include_path_for("iphonesimulator"))
} else {
Some(sdk_include_path_for("iphoneos"))
}
}
"macos" => Some(sdk_include_path_for("macosx")),
_ => None,
}
}
fn compile_lwip() {
println!("cargo:rerun-if-changed=lwip/old-src/core");
let mut build = cc::Build::new();
build
.file("lwip/old-src/core/init.c")
.file("lwip/old-src/core/def.c")
// .file("lwip/old-src/core/dns.c")
.file("lwip/old-src/core/inet_chksum.c")
.file("lwip/old-src/core/ip.c")
.file("lwip/old-src/core/mem.c")
.file("lwip/old-src/core/memp.c")
.file("lwip/old-src/core/netif.c")
.file("lwip/old-src/core/pbuf.c")
.file("lwip/old-src/core/raw.c")
// .file("lwip/old-src/core/stats.c")
// .file("lwip/old-src/core/sys.c")
.file("lwip/old-src/core/tcp.c")
.file("lwip/old-src/core/tcp_in.c")
.file("lwip/old-src/core/tcp_out.c")
.file("lwip/old-src/core/timeouts.c")
.file("lwip/old-src/core/udp.c")
// .file("lwip/old-src/core/ipv4/autoip.c")
// .file("lwip/old-src/core/ipv4/dhcp.c")
// .file("lwip/old-src/core/ipv4/etharp.c")
.file("lwip/old-src/core/ipv4/icmp.c")
// .file("lwip/old-src/core/ipv4/igmp.c")
.file("lwip/old-src/core/ipv4/ip4_frag.c")
.file("lwip/old-src/core/ipv4/ip4.c")
.file("lwip/old-src/core/ipv4/ip4_addr.c")
// .file("lwip/old-src/core/ipv6/dhcp6.c")
// .file("lwip/old-src/core/ipv6/ethip6.c")
.file("lwip/old-src/core/ipv6/icmp6.c")
// .file("lwip/old-src/core/ipv6/inet6.c")
.file("lwip/old-src/core/ipv6/ip6.c")
.file("lwip/old-src/core/ipv6/ip6_addr.c")
.file("lwip/old-src/core/ipv6/ip6_frag.c")
// .file("lwip/old-src/core/ipv6/mld6.c")
.file("lwip/old-src/core/ipv6/nd6.c")
.file("lwip/old-src/custom/sys_arch.c")
.file("lwip/src/api/err.c")
.include("lwip/old-src/custom")
.include("lwip/old-src/include")
.warnings(false)
.flag_if_supported("-Wno-everything");
if let Some(sdk_include_path) = sdk_include_path() {
build.include(sdk_include_path);
}
build.debug(true);
build.compile("liblwip.a");
}
fn generate_lwip_bindings() {
println!("cargo:rustc-link-lib=lwip");
// println!("cargo:rerun-if-changed=lwip/old-src/custom/wrapper.h");
println!("cargo:include=lwip/old-src/include");
let sdk_include_path = sdk_include_path();
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let mut builder = bindgen::Builder::default()
.header("lwip/old-src/custom/wrapper.h")
.clang_arg("-I./lwip/old-src/include")
.clang_arg("-I./lwip/old-src/custom")
.clang_arg("-Wno-everything")
.layout_tests(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));
if arch == "aarch64" && os == "ios" {
// https://github.com/rust-lang/rust-bindgen/issues/1211
builder = builder.clang_arg("--target=arm64-apple-ios");
}
if let Some(sdk_include_path) = sdk_include_path {
builder = builder.clang_arg(format!("-I{}", sdk_include_path));
}
if os == "windows" {
builder = builder.size_t_is_usize(false);
}
let bindings = builder.generate().expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn main() {
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
println!("cargo:warning=host os {}", os);
compile_lwip();
generate_lwip_bindings();
println!("cargo:rerun-if-changed=build.rs");
}