-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
85 lines (69 loc) · 2.67 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
use git2::Repository;
use std::{env, path::PathBuf};
use which::which;
const CMSIS_NN_URL: &str = "https://github.com/ARM-software/CMSIS-NN.git";
fn switch_branch(repo: &Repository, branch_name: &str) {
let (object, reference) = repo.revparse_ext(branch_name).unwrap();
repo.checkout_tree(&object, None).unwrap();
match reference {
Some(gref) => repo.set_head(gref.name().unwrap()),
None => repo.set_head_detached(object.id()),
}
.unwrap();
}
fn main() {
let target = env::var("TARGET").unwrap();
let arm_toolchain = env::var("ARM_TOOLCHAIN_PATH").unwrap_or_else(|_| {
let gcc_path = which("arm-none-eabi-gcc").unwrap();
gcc_path
.parent()
.unwrap()
.parent()
.unwrap()
.to_str()
.unwrap()
.to_string()
});
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let cmsis_nn_dir = out_path.join("CMSIS-NN");
let cmsis_nn_repo = get_repo(&CMSIS_NN_URL, &cmsis_nn_dir);
switch_branch(&cmsis_nn_repo, "v7.0.0");
let toolchain_file = manifest_dir.join("cmake/toolchain/arm-none-eabi-gcc.cmake");
let target_flag = match target.as_str() {
"thumbv7em-none-eabihf" => "cortex-m4",
_ => unimplemented!(),
};
let lib = cmake::Config::new(&cmsis_nn_dir)
.define("CMAKE_TOOLCHAIN_FILE", &toolchain_file)
.define("TARGET_CPU", target_flag)
.build_target("cmsis-nn")
.build()
.join("build");
println!("cargo:rustc-link-search={}", lib.display());
println!("cargo:rustc-link-lib=static=cmsis-nn");
let cmsis_nn_include_dir = cmsis_nn_dir.join("Include");
let arm_toolchain_include_dir = PathBuf::from(arm_toolchain).join("include");
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.derive_default(true)
.generate_comments(true)
.formatter(bindgen::Formatter::Rustfmt)
.clang_arg(format!("-I{}", cmsis_nn_include_dir.display()))
// .clang_arg("-nostdinc")
.clang_arg(format!("-I{}", arm_toolchain_include_dir.display()))
.use_core()
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn get_repo(url: &str, path: &PathBuf) -> Repository {
let repo = match Repository::open(&path) {
Ok(repo) => repo,
Err(e) if e.code() == git2::ErrorCode::NotFound => Repository::clone(url, &path).unwrap(),
Err(e) => panic!("Failed to open: {}", e),
};
repo
}