2021-06-09 07:25:56 +01:00
|
|
|
use std::env;
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let target = env::var("TARGET").unwrap();
|
|
|
|
|
|
2021-06-15 10:48:12 +01:00
|
|
|
let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
2021-06-25 12:57:03 +01:00
|
|
|
let target_dir = PathBuf::from(env::var("CARGO_BUILD_TARGET_DIR").unwrap());
|
2021-06-09 07:25:56 +01:00
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
|
|
2021-06-25 12:57:03 +01:00
|
|
|
run_cbindgen(&cargo_dir, &target_dir);
|
2021-06-09 07:25:56 +01:00
|
|
|
run_bindgen(&target, &out_dir);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 12:57:03 +01:00
|
|
|
fn run_cbindgen(cargo_dir: &Path, target_dir: &Path) {
|
|
|
|
|
let out = target_dir.join("RustApi.h");
|
|
|
|
|
|
|
|
|
|
cbindgen::Builder::new()
|
|
|
|
|
.with_crate(cargo_dir)
|
|
|
|
|
.with_language(cbindgen::Language::C)
|
|
|
|
|
.generate()
|
|
|
|
|
.expect("Unable to generate bindings")
|
|
|
|
|
.write_to_file(&out);
|
|
|
|
|
|
|
|
|
|
println!("cargo:rerun-if-changed={}", out.display());
|
2021-06-15 10:48:12 +01:00
|
|
|
}
|
2021-06-09 07:25:56 +01:00
|
|
|
|
|
|
|
|
fn run_bindgen(target: &str, out_dir: &Path) {
|
2021-06-25 12:57:03 +01:00
|
|
|
let header = "../clib/include/CApi.h";
|
|
|
|
|
let out = out_dir.join("bindings.rs");
|
|
|
|
|
|
2021-06-09 07:25:56 +01:00
|
|
|
let mut builder = bindgen::Builder::default();
|
2021-06-25 12:57:03 +01:00
|
|
|
builder = builder.header(header);
|
2021-06-09 07:25:56 +01:00
|
|
|
match target {
|
|
|
|
|
"riscv32i-unknown-none-elf" => {
|
|
|
|
|
builder = builder.clang_arg("--target=riscv32");
|
2021-06-15 10:48:12 +01:00
|
|
|
builder = builder.use_core();
|
|
|
|
|
builder = builder.ctypes_prefix("crate::ffi");
|
2021-06-09 07:25:56 +01:00
|
|
|
}
|
2021-07-28 15:14:45 +01:00
|
|
|
"xtensa-esp32-espidf" => {
|
2021-06-15 10:48:12 +01:00
|
|
|
// Make sure that LLVM_CONFIG_PATH has been set to point to the
|
|
|
|
|
// Xtensa build of llvm-config.
|
2021-06-09 07:25:56 +01:00
|
|
|
builder = builder.clang_arg("--target=xtensa-esp32-elf");
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
panic!("Unexpect target archtitecture: {}", &target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let bindings = builder.generate().expect("Couldn't generate bindings!");
|
|
|
|
|
bindings
|
2021-06-25 12:57:03 +01:00
|
|
|
.write_to_file(&out)
|
2021-06-09 07:25:56 +01:00
|
|
|
.expect("Couldn't save bindings!");
|
2021-06-25 12:57:03 +01:00
|
|
|
|
|
|
|
|
println!("cargo:rerun-if-changed={}", header);
|
|
|
|
|
println!("cargo:rerun-if-changed={}", out.display());
|
2021-06-09 07:25:56 +01:00
|
|
|
}
|