-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.rs
133 lines (117 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
use std::collections::HashSet;
use std::env;
use std::path::PathBuf;
#[derive(Debug)]
struct IgnoreMacros(HashSet<String>);
impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if self.0.contains(name) {
bindgen::callbacks::MacroParsingBehavior::Ignore
} else {
bindgen::callbacks::MacroParsingBehavior::Default
}
}
}
struct Lib {
pub link_name: String,
pub header_path: String,
pub out_name: String,
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let libraries = [
Lib {
link_name: "X11".to_string(),
header_path: "/usr/include/X11/Xlib.h".to_string(),
out_name: "Xlib.rs".to_string(),
},
Lib {
link_name: "GL".to_string(),
header_path: "/usr/include/GL/gl.h".to_string(),
out_name: "gl.rs".to_string(),
},
Lib {
link_name: "GLX".to_string(),
header_path: "/usr/include/GL/glx.h".to_string(),
out_name: "glx.rs".to_string(),
},
Lib {
link_name: "m".to_string(),
header_path: "/usr/include/math.h".to_string(),
out_name: "math.rs".to_string(),
},
//Lib {
//link_name: "pcm".to_string(),
//header_path: "/usr/include/alsa/asoundlib.h".to_string(),
//out_name: "alsa.rs".to_string(),
//},
];
for lib in &libraries {
let mut bindings = bindgen::Builder::default()
.rust_target(bindgen::RustTarget::Nightly)
.trust_clang_mangling(false)
.derive_copy(false)
.derive_debug(false)
.use_core()
.raw_line(format!(r#"#[link(name="{}")] extern{{}}"#, lib.link_name))
.ctypes_prefix("libc")
.header(lib.header_path.clone());
if lib.out_name == "math.rs" {
let ignored_macros = IgnoreMacros(
vec![
"FP_INFINITE".into(),
"FP_NAN".into(),
"FP_NORMAL".into(),
"FP_SUBNORMAL".into(),
"FP_ZERO".into(),
"IPPORT_RESERVED".into(),
]
.into_iter()
.collect(),
);
bindings = bindings
.parse_callbacks(Box::new(ignored_macros))
.rustfmt_bindings(true);
}
let bindings = bindings.generate().expect(&format!(
"Couldn't generate bindings for {}",
lib.header_path
));
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join(&lib.out_name))
.expect("Couldn't write bindings!");
}
generate_bindings();
}
// MIT License: https://github.com/diwic/alsa-sys/blob/master/build.rs
fn generate_bindings() {
let mut codegen_config = bindgen::CodegenConfig::empty();
codegen_config.insert(bindgen::CodegenConfig::FUNCTIONS);
codegen_config.insert(bindgen::CodegenConfig::TYPES);
let builder = bindgen::Builder::default()
.rust_target(bindgen::RustTarget::Nightly)
.derive_copy(false)
.derive_debug(false)
.use_core()
.raw_line(r#"#[link(name="asound")] extern {}"#)
.ctypes_prefix("libc")
.header("/usr/include/alsa/asoundlib.h")
//.header("alsa-wrapper.h")
.size_t_is_usize(true)
//.whitelist_recursively(false)
.prepend_enum_name(false)
.layout_tests(false)
//.whitelist_function("snd_.*")
//.whitelist_type("_?snd_.*")
//.whitelist_type(".*va_list.*")
//.with_codegen_config(codegen_config)
//.clang_args(clang_include_args)
//.parse_callbacks(Box::new(bindgen::CargoCallbacks));
;
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("alsa.rs"))
.expect("Couldn't write bindings");
}