-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.rs
240 lines (219 loc) · 9.04 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#![feature(nll)]
#[macro_use]
extern crate const_format;
extern crate bindgen;
use heck;
use regex;
use std::env;
use std::fs;
use std::path::PathBuf;
const MUMBLE_NAME_ROOT: &'static str = "mumble";
const MUMBLE_WRAPPER_NAME: &'static str = concatcp!(MUMBLE_NAME_ROOT, "-wrapper.h");
const MUMBLE_BINDINGS_NAME: &'static str = concatcp!(MUMBLE_NAME_ROOT, ".rs");
const MUMBLE_WRAPPER_SRC: &'static str = "src/";
const MUMBLE_WRAPPER: &'static str = concatcp!(MUMBLE_WRAPPER_SRC, MUMBLE_WRAPPER_NAME);
const MUMBLE_BINDINGS: &'static str = MUMBLE_BINDINGS_NAME;
#[derive(Debug)]
struct CustomCallbacks {
inner: bindgen::CargoCallbacks,
}
impl CustomCallbacks {
pub fn new() -> Self {
CustomCallbacks {
inner: bindgen::CargoCallbacks,
}
}
fn item_name_handler(&self, original_item_name: &str) -> Option<String> {
if original_item_name == "root" {
return Some("m".into());
}
let is_mumble_function_prefix = regex::RegexBuilder::new(r"^mumble_[a-z].+$")
.build()
.unwrap();
match original_item_name {
"Version" => return None,
"MumbleStringWrapper" => return None,
"mumble_plugin_id_t" => return Some("PluginId".into()),
x if x.starts_with("MumbleAPI_") => return Some("MumbleAPI".into()),
x if x.starts_with("Mumble_") && x.chars().filter(|x| *x == '_').count() == 1 => {
return Some(x["Mumble_".len()..].into())
}
x if x.starts_with("mumble_")
&& !x.ends_with("_t")
&& is_mumble_function_prefix.is_match(&x) =>
{
return Some(x.into())
}
_ => {}
}
let strip_mumble_prefix = regex::RegexBuilder::new(r"^[Mm]umble_(.+)$")
.build()
.unwrap();
let name: String = strip_mumble_prefix
.replace(original_item_name, r"$1")
.into();
// methods should become snake case; types should become pascal case
let name: String = if !name.ends_with("_t") {
heck::SnekCase::to_snek_case(name.as_str()).into()
} else {
let name = name.replace("id_", "Id_");
let unsnake = regex::RegexBuilder::new(r"(?:^|_)([a-z])").build().unwrap();
let name = if name.starts_with(|c: char| c.is_alphabetic() && c.is_lowercase()) {
unsnake
.replace_all(&name, |cap: ®ex::Captures| {
cap.get(1).map(|m| m.as_str().to_uppercase()).unwrap()
})
.into()
} else {
name
};
name
};
Some(name)
}
}
impl bindgen::callbacks::ParseCallbacks for CustomCallbacks {
fn item_name(&self, original_item_name: &str) -> Option<String> {
let new_name = self.item_name_handler(original_item_name);
println!(
"GEN NAME: {} = {}",
original_item_name,
match &new_name {
Some(x) => x.as_str(),
None => original_item_name,
}
);
new_name
}
fn include_file(&self, filename: &str) {
self.inner.include_file(filename)
}
}
fn main() {
// println!("cargo:rerun-if-changed={}", MUMBLE_WRAPPER);
env_logger::builder()
.format(|buf, record| {
use std::io::Write;
writeln!(buf, "{}: {:#?}", record.level(), record.args())
})
.init();
// let out_file = if cfg!(feature = "idebuild") {
let out_dir = env::current_dir().unwrap();
let out_file = out_dir.join(MUMBLE_WRAPPER_SRC).join(MUMBLE_BINDINGS);
// } else {
// let out_dir = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
// let out_file = out_dir.join(MUMBLE_BINDINGS);
// out_file
// };
let should_build = !out_file.exists() || true; //TODO: Remove || true
if should_build {
let mumble_sources_symlink = env::current_dir().unwrap().join("mumble_sources");
let mumble_home = if mumble_sources_symlink.exists() {
mumble_sources_symlink
.canonicalize()
.unwrap()
.to_str()
.unwrap()
.to_string()
} else {
let home_path = env::var("MUMBLE_HOME")
.expect("Must have a MUMBLE_HOME environment variable set if no mumble_sources symlink is present")
.trim_end_matches("/")
.to_string();
if !PathBuf::from(&home_path).exists() {
panic!(
"No mumble_sources directory in repo root, and MUMBLE_HOME env var referred to non-existent location"
)
}
home_path
};
// TODO: This should be done with procedural macros instead
let regex_replace_with_nonnull = regex::RegexBuilder::new(
r"\bpub ([^\s:]+): (?:(?:::std::)?option::)?Option<\s*(.+?,)\s*>,\n",
)
.dot_matches_new_line(true)
.build()
.unwrap();
let replace_fn_ptrs_nonnull = |s: &str| -> String {
regex_replace_with_nonnull
.replace_all(s, "pub ${1}: $2\n")
.to_string()
};
let bindings = bindgen::Builder::default()
.clang_args(&["-x", "c++", "-std=c++20"])
.clang_arg(format!("-I{}/plugins", mumble_home))
// .layout_tests(false)
.rust_target(bindgen::RustTarget::Nightly)
.enable_cxx_namespaces()
.default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: false,
})
.default_alias_style(bindgen::AliasVariation::NewTypeDeref)
.type_alias("mumble_error_t")
.derive_eq(true)
.size_t_is_usize(true)
// <allow-list>
.whitelist_var("MUMBLE_PLUGIN_API_VERSION")
.whitelist_type("MumbleAPI_v.*")
// Mandatory functions
.whitelist_function("mumble_init")
.whitelist_function("mumble_shutdown")
.whitelist_function("mumble_getName")
.whitelist_function("mumble_getAPIVersion")
.whitelist_function("mumble_registerAPIFunctions")
.whitelist_function("mumble_releaseResource")
// General functions
.whitelist_function("mumble_setMumbleInfo")
.whitelist_function("mumble_getVersion")
.whitelist_function("mumble_getAuthor")
.whitelist_function("mumble_getDescription")
.whitelist_function("mumble_registerPluginID")
.whitelist_function("mumble_getFeatures")
.whitelist_function("mumble_deactivateFeatures")
// Positional audio
.whitelist_function("mumble_initPositionalData")
.whitelist_function("mumble_fetchPositionalData")
.whitelist_function("mumble_shutdownPositionalData")
// EventHandlers / Callback Functions
.whitelist_function("mumble_onServerConnected")
.whitelist_function("mumble_onServerDisconnected")
.whitelist_function("mumble_onServerSynchronized")
.whitelist_function("mumble_onChannelEntered")
.whitelist_function("mumble_onChannelExited")
.whitelist_function("mumble_onUserTalkingStateChanged")
.whitelist_function("mumble_onAudioInput")
.whitelist_function("mumble_onAudioSourceFetched")
.whitelist_function("mumble_onAudioOutputAboutToPlay")
.whitelist_function("mumble_onReceiveData")
.whitelist_function("mumble_onUserAdded")
.whitelist_function("mumble_onUserRemoved")
.whitelist_function("mumble_onChannelAdded")
.whitelist_function("mumble_onChannelRemoved")
.whitelist_function("mumble_onChannelRenamed")
.whitelist_function("mumble_onKeyEvent")
// Plugin updates
.whitelist_function("mumble_hasUpdate")
.whitelist_function("mumble_getUpdateDownloadURL")
// </allow-list>
.detect_include_paths(true)
// PluginComponents has references to std::string which apparently explode these days
// We replace the ifdefs to get it to parse
.header_contents(
"PluginComponents_v_1_0_x.h",
&std::fs::read_to_string(
PathBuf::from(&mumble_home)
.join("plugins")
.join("PluginComponents_v_1_0_x.h"),
)
.expect("PluginComponents file must exist")
.replace("#ifdef __cplusplus", "#ifdef __never"),
)
.header(MUMBLE_WRAPPER)
.parse_callbacks(Box::new(CustomCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
fs::write(&out_file, replace_fn_ptrs_nonnull(&bindings.to_string()))
.expect("Couldn't write bindings!");
println!("Wrote bindings to {:?}", &out_file);
}
}