DragonOS/kernel/build.rs
login 2aaf7808ef
在内核中引入cbindgen,生成rust-C的FFI (#81)
* 解决codeql失败问题

* new: 为内核引入cbindgen
2022-11-11 22:21:44 +08:00

49 lines
1.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extern crate bindgen;
extern crate cbindgen;
use ::std::env;
use std::path::PathBuf;
fn main() {
// Tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search=src");
println!("cargo:rerun-if-changed=src/include/bindings/wrapper.h");
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let out_path = PathBuf::from(String::from("src/include/bindings/"));
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
{
let bindings = bindgen::Builder::default()
.clang_arg("-I./src")
// The input header we would like to generate
// bindings for.
.header("src/include/bindings/wrapper.h")
.clang_arg("--target=x86_64-none-none")
.clang_arg("-v")
// 使用core并将c语言的类型改为core::ffi而不是使用std库。
.use_core()
.ctypes_prefix("::core::ffi")
.generate_inline_functions(true)
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
cbindgen::generate(crate_dir)
.unwrap()
.write_to_file(out_path.join("bindings.h"));
}