-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
231 lines (206 loc) · 7.37 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
use std::env;
use std::fs::copy;
use std::path::{Path, PathBuf};
use std::process::Command;
use bindgen::callbacks::DeriveInfo;
#[derive(Debug)]
struct MyCallback;
impl core::panic::UnwindSafe for MyCallback {}
impl bindgen::callbacks::ParseCallbacks for MyCallback {
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
match info.name {
"PatchParameterId"
| "PatchButtonId"
| "OpenWareMidiSysexCommand"
| "OpenWareMidiControl"
| "MidiRPN"
| "OwlProtocol"
| "UsbMidi"
| "MidiControlChange"
| "MidiStatus" => vec!["num_derive::FromPrimitive".to_string()],
_ => vec![],
}
}
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap())
.canonicalize()
.unwrap();
if !PathBuf::from("OwlProgram/.git").exists() {
let _ = Command::new("git")
.args(["submodule", "update", "--init", "OwlProgram"])
.status();
}
let owl_base_path = PathBuf::from("OwlProgram")
.canonicalize()
.expect("Could not locate OwlProgram");
let lib_source = owl_base_path.join("LibSource");
let cpp_source = owl_base_path.join("Source");
let cmsis_include = owl_base_path.join("Libraries/CMSIS/Include");
let cmsis_include_dsp = owl_base_path.join("Libraries/CMSIS/DSP/Include");
generate_bindings(&cpp_source, &out_path, &lib_source);
copy_linker_scripts(&cpp_source, &out_path);
compile_c_lib(
&lib_source,
&cpp_source,
&cmsis_include,
&cmsis_include_dsp,
&out_path,
);
}
fn copy_linker_scripts(cpp_source: &Path, out_path: &Path) {
println!("cargo:rustc-link-search={}", out_path.to_str().unwrap());
for file in ["owl1.ld", "owl2.ld", "owl3.ld"] {
copy(cpp_source.join(file), out_path.join(file)).expect("failed to copy linker file");
}
}
fn generate_bindings(cpp_source: &Path, out_path: &Path, lib_source: &Path) {
let bindings = bindgen::Builder::default()
.header(cpp_source.join("ProgramVector.h").to_str().unwrap())
.use_core()
.prepend_enum_name(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.allowlist_type("ProgramVector")
.allowlist_var("[^_].*")
.blocklist_var("programVector")
.clang_args(["-x", "c++"])
.rustified_enum("ProgramVectorAudioStatus")
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("program_vector.rs"))
.expect("Couldn't write bindings!");
let bindings = bindgen::Builder::default()
.header("stddef.h")
.header("stdint.h")
.header(lib_source.join("MidiMessage.h").to_str().unwrap())
.use_core()
.allowlist_type("MidiRPN")
.allowlist_type("OwlProtocol")
.allowlist_type("UsbMidi")
.allowlist_type("MidiControlChange")
.allowlist_type("MidiStatus")
.rustified_enum("MidiRPN")
.rustified_enum("OwlProtocol")
.rustified_enum("UsbMidi")
.rustified_enum("MidiControlChange")
.rustified_enum("MidiStatus")
.blocklist_var("_.*")
.prepend_enum_name(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.parse_callbacks(Box::new(MyCallback))
.clang_args(["-x", "c++", "-fshort-enums"])
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("midi_message.rs"))
.expect("Couldn't write bindings!");
let bindings = bindgen::Builder::default()
.header(cpp_source.join("ServiceCall.h").to_str().unwrap())
.use_core()
.prepend_enum_name(false)
.generate_cstr(true)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.blocklist_function("serviceCall")
.clang_args(["-x", "c++"])
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("service_call.rs"))
.expect("Couldn't write bindings!");
let bindings = bindgen::Builder::default()
.header(lib_source.join("OpenWareMidiControl.h").to_str().unwrap())
.use_core()
.prepend_enum_name(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.parse_callbacks(Box::new(MyCallback))
.generate_cstr(true)
.clang_args(["-x", "c++", "-fshort-enums"])
.rustified_enum("PatchParameterId")
.rustified_enum("PatchButtonId")
.rustified_enum("OpenWareMidiSysexCommand")
.rustified_enum("OpenWareMidiControl")
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("openware_midi_control.rs"))
.expect("Couldn't write bindings!");
// let bindings = bindgen::Builder::default()
// .header(lib_source.join("basicmaths.h").to_str().unwrap())
// .allowlist_function("fast_.*")
// .use_core()
// .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// .clang_args([&format!("-I{}", cpp_source.to_str().unwrap())])
// .generate()
// .expect("Unable to generate bindings");
// bindings
// .write_to_file(out_path.join("fastmaths.rs"))
// .expect("Couldn't write bindings!");
}
fn compile_c_lib(
lib_source: &Path,
cpp_source: &Path,
cmsis_include: &Path,
cmsis_include_dsp: &Path,
out_path: &Path,
) {
let cc_args = if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "arm" {
vec![
"-O3",
"-ffast-math",
"-DNDEBUG",
"-DARM_CORTEX",
"-DEXTERNAL_SRAM",
"-nostdlib",
"-fno-builtin",
"-ffreestanding",
"-fpic",
"-fpie",
"-fdata-sections",
"-ffunction-sections",
"-mno-unaligned-access",
"-fno-omit-frame-pointer",
"-fno-strict-aliasing",
"-std=gnu11",
"-D__PROGRAM_START=1",
"-fsingle-precision-constant",
"-mthumb",
]
} else {
vec![
"-g",
"-O0",
"-Wall",
"-Wcpp",
"-Wunused-function",
"-DDEBUG",
"-DUSE_FULL_ASSERT",
"-D__PROGRAM_START=1",
]
};
copy("c_src/tables.c", out_path.join("tables.c")).expect("failed to copy tables.c");
in_dir(out_path, || {
let mut c_builder = cc::Build::new();
c_builder.include(cpp_source);
c_builder.include(lib_source);
c_builder.include(cmsis_include);
c_builder.include(cmsis_include_dsp);
c_builder.file(lib_source.join("basicmaths.c"));
c_builder.file(lib_source.join("fastpow.c"));
c_builder.file(lib_source.join("fastlog.c"));
c_builder.file("tables.c");
for flag in cc_args.into_iter() {
c_builder.flag(flag);
}
c_builder.compile("fastmaths");
});
println!("cargo:rustc-link-search={}", out_path.to_str().unwrap());
println!("cargo:rustc-link-lib=fastmaths");
}
fn in_dir(dir: &Path, f: impl FnOnce()) {
let old_dir = std::env::current_dir().unwrap();
std::env::set_current_dir(dir).unwrap();
f();
std::env::set_current_dir(old_dir).unwrap();
}