mirror of
https://gitee.com/openharmony/third_party_rust_bindgen
synced 2025-03-04 12:47:22 +00:00

The `syntex` crate is unmaintained. It is slow to build, and additionally it requires that we pre-process `src/codegen/mod.rs` before we build the `bindgen` crate. The `quote` crate provides similar quasi-quoting functionality, is maintained, and builds faster. It doesn't have a typed API or builders, however; it only deals with tokens. Before this commit: ``` $ cargo clean; cargo build <snip> Finished dev [unoptimized + debuginfo] target(s) in 98.75 secs ``` After this commit: ``` $ cargo clean; cargo build <snip> Finished dev [unoptimized + debuginfo] target(s) in 46.26 secs ``` Build time is cut in half! But what about run time? Before this commit: ``` Generated Stylo bindings in: Duration { secs: 3, nanos: 521105668 } ``` After this commit: ``` Generated Stylo bindings in: Duration { secs: 3, nanos: 548797242 } ``` So it appears to be about 20ms slower at generating Stylo bindings, but I suspect this is well within the noise. Finally, this also lets us remove that nasty `mem::transmute` inside `bindgen::ir::BindgenContext::gen` that was used for the old `syntex` context. Now `BindgenContext` doesn't have a lifetime parameter either. This should make it easier to revisit doing our analyses in parallel with `rayon`, since that context was one of the things that made it hard for `BindgenContext` to implement `Sync`. Fixes #925
74 lines
2.0 KiB
Rust
74 lines
2.0 KiB
Rust
mod target {
|
|
use std::env;
|
|
use std::fs::File;
|
|
use std::io::Write;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
pub fn main() {
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
let mut dst = File::create(Path::new(&out_dir).join("host-target.txt"))
|
|
.unwrap();
|
|
dst.write_all(env::var("TARGET").unwrap().as_bytes())
|
|
.unwrap();
|
|
}
|
|
}
|
|
|
|
mod testgen {
|
|
use std::char;
|
|
use std::env;
|
|
use std::ffi::OsStr;
|
|
use std::fs::{self, File};
|
|
use std::io::Write;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
pub fn main() {
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
let mut dst = File::create(Path::new(&out_dir).join("tests.rs"))
|
|
.unwrap();
|
|
|
|
let manifest_dir =
|
|
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
let headers_dir = manifest_dir.join("tests").join("headers");
|
|
|
|
let headers = match fs::read_dir(headers_dir) {
|
|
Ok(dir) => dir,
|
|
// We may not have headers directory after packaging.
|
|
Err(..) => return,
|
|
};
|
|
|
|
let entries =
|
|
headers.map(|result| result.expect("Couldn't read header file"));
|
|
|
|
println!("cargo:rerun-if-changed=tests/headers");
|
|
|
|
for entry in entries {
|
|
match entry.path().extension().and_then(OsStr::to_str) {
|
|
Some("h") | Some("hpp") => {
|
|
let func = entry
|
|
.file_name()
|
|
.to_str()
|
|
.unwrap()
|
|
.replace(|c| !char::is_alphanumeric(c), "_")
|
|
.replace("__", "_")
|
|
.to_lowercase();
|
|
writeln!(
|
|
dst,
|
|
"test_header!(header_{}, {:?});",
|
|
func,
|
|
entry.path()
|
|
).unwrap();
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
dst.flush().unwrap();
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
target::main();
|
|
testgen::main();
|
|
}
|