-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
148 lines (124 loc) · 4.12 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::env;
#[cfg(feature = "uws_vendored")]
use std::path::{Path};
#[cfg(feature = "uws_vendored")]
use std::process::Command;
use std::path::{PathBuf};
fn main() {
let host = env::var("HOST").unwrap();
let target = env::var("TARGET").unwrap();
let is_windows = host.contains("windows") && target.contains("windows");
if is_windows {
panic!("Windows is not currently supported");
}
#[cfg(feature = "uws_vendored")]
let is_apple = host.contains("apple") && target.contains("apple");
#[cfg(feature = "uws_vendored")]
let is_linux = host.contains("linux") && target.contains("linux");
let cur_dir = env::current_dir().unwrap();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let uws_dir = cur_dir.join("uWebSockets");
let us_dir = uws_dir.join("uSockets");
let capi_dir = uws_dir.join("capi");
bindgen::Builder::default()
.clang_arg(format!("-I{}", us_dir.join("src").display()))
.header(capi_dir.join("libuwebsockets.h").display().to_string())
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings")
.write_to_file(out_dir.join("bindings.rs"))
.expect("Couldn't write bindings!");
// Remove libuwebsockets.a built by make because to my understanding it has packed in a wrong way
#[cfg(feature = "uws_vendored")]
{
if !Command::new("make")
.current_dir(&capi_dir)
.arg("capi")
.status()
.expect("Failed to run make")
.success()
{
panic!("Can't build UWS CAPI!");
}
let _ = Command::new("rm").current_dir(&capi_dir).arg("-f").arg("libuwebsockets.a").status().expect("failed to delete lib").success();
let mut o_files = read_dir_by_condition(&us_dir, |file_name| file_name.ends_with(".o"));
let success = if is_apple {
let mut args = vec![
"-static".to_string(),
"-o".to_string(),
capi_dir.join("libuwebsockets.a").to_str().unwrap().to_string(),
capi_dir.join("libuwebsockets.o").to_str().unwrap().to_string(),
];
args.append(&mut o_files);
Command::new("libtool")
.current_dir(&capi_dir)
.args(args)
.status()
.expect("Failed to run libtool")
.success()
} else if is_linux {
let mut args = vec![
"rcs".to_string(),
"-o".to_string(),
capi_dir.join("libuwebsockets.a").to_str().unwrap().to_string(),
capi_dir.join("libuwebsockets.o").to_str().unwrap().to_string(),
];
args.append(&mut o_files);
Command::new("ar")
.current_dir(&out_dir)
.args(args)
.status()
.expect("Failed to pack libuwebsockets")
.success()
} else { false };
if !success {
panic!("Failed to pack libuwebsockets.a");
}
copy_by_condition(capi_dir, &out_dir, |filename| filename.ends_with(".a"));
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=uwebsockets");
}
}
#[cfg(feature = "uws_vendored")]
fn copy_by_condition<T>(from: impl AsRef<Path>, to: impl AsRef<Path>, condition: T)
where
T: Fn(&str) -> bool + Sized + 'static,
{
use std::fs;
let entries = from.as_ref().read_dir().unwrap();
for entry in entries {
let entry = entry.unwrap();
if entry.file_type().unwrap().is_dir() {
continue;
}
let file_name = entry.file_name();
if !condition(file_name.to_str().unwrap()) {
continue;
}
let from = entry.path();
let to = to.as_ref().join(file_name);
println!("copy from {from:#?} to: {to:#?}");
fs::copy(&from, &to).unwrap();
}
}
#[cfg(feature = "uws_vendored")]
fn read_dir_by_condition<T>(dir: &Path, condition: T) -> Vec<String>
where
T: Fn(&str) -> bool + Sized + 'static,
{
let mut res = Vec::new();
let entries = dir.read_dir().unwrap();
for entry in entries {
let entry = entry.unwrap();
if entry.file_type().unwrap().is_dir() {
continue;
}
let file_name = entry.file_name();
let file_name = file_name.to_str().unwrap();
if !condition(file_name) {
continue;
}
res.push(dir.join(file_name).to_str().unwrap().to_string());
}
res
}