This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
generate-bindings.rs
134 lines (116 loc) · 3.45 KB
/
generate-bindings.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
use std::error::Error;
use std::fs;
use std::path::Path;
use std::process::{Command, Stdio};
macro_rules! error {
($cause:expr, $fmt:literal $(, $arg:expr)*) => {
GenError::with_cause($cause, format!($fmt $(, $arg)*))
};
($fmt:literal $(, $arg:expr)*) => {
GenError::new(format!($fmt $(, $arg)*))
};
}
pub fn main() {
match generate_bindings() {
Ok(_) => println!("done"),
Err(err) => {
eprintln!("error: {}", err);
let mut source = err.source();
while let Some(cause) = source {
eprintln!("\tcaused by: {}", cause);
source = cause.source();
}
std::process::exit(1);
}
};
}
fn generate_bindings() -> Result<(), GenError> {
let os = std::env::consts::OS;
let arch = std::env::consts::ARCH;
let subdir = format!("{}-{}", os, arch);
println!("generating bindings for `{}`...", subdir);
let output_dir = Path::new("miniaudio-sys/bindings").join(subdir);
fs::create_dir_all(&output_dir)
.map_err(|e| error!(e, "failed to create directory `{}`", output_dir.display()))?;
println!("generating `bindings.rs`...");
let status = base_command()
.arg("-o")
.arg(output_dir.join("bindings.rs"))
.arg("miniaudio-sys/bindings.h")
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.map_err(|e| error!(e, "failed to run bindgen command for bindings.rs"))?
.status;
if !status.success() {
return Err(error!("bindgen exited with error status {}", status));
}
println!("generating `bindings-with-vorbis.rs`...");
let status = base_command()
.arg("-o")
.arg(output_dir.join("bindings-with-vorbis.rs"))
.arg("miniaudio-sys/bindings-with-vorbis.h")
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.map_err(|e| error!(e, "failed to run bindgen command for bindings.rs"))?
.status;
if !status.success() {
return Err(error!("bindgen exited with error status {}", status));
}
Ok(())
}
fn base_command() -> Command {
let mut cmd = Command::new("bindgen");
cmd.args(&[
"--verbose",
"--use-core",
"--size_t-is-usize",
"--impl-debug",
"--no-prepend-enum-name",
]);
cmd.args(&["--ctypes-prefix", "libc"]);
cmd.args(&["--rust-target", "1.36"]);
cmd.args(&["--whitelist-type", "ma_.*"]);
cmd.args(&["--whitelist-function", "ma_.*"]);
cmd.args(&["--whitelist-var", "(ma|MA)_.*"]);
cmd
}
#[derive(Debug)]
struct GenError {
message: String,
cause: Option<Box<dyn Error>>,
}
impl GenError {
fn new<S>(message: S) -> GenError
where
S: Into<String>,
{
GenError {
message: message.into(),
cause: None,
}
}
fn with_cause<S, E>(cause: E, message: S) -> GenError
where
S: Into<String>,
E: Error + 'static,
{
GenError {
message: message.into(),
cause: Some(Box::new(cause)),
}
}
}
impl std::fmt::Display for GenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl Error for GenError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.cause.as_ref().map(|e| e.as_ref())
}
}