xask: add bindings::write

pull/1/head
Alessandro Decina 4 years ago
parent ee205627b7
commit b5cb1f479f

@ -1,4 +1,4 @@
use std::{fs::File, io::Write, path::PathBuf, process::Command}; use std::path::PathBuf;
use anyhow::anyhow; use anyhow::anyhow;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
@ -15,7 +15,7 @@ use syn::{
}; };
use crate::codegen::{ use crate::codegen::{
bindings::bindgen, bindings::{self, bindgen},
getters::{generate_getters_for_items, Getter}, getters::{generate_getters_for_items, Getter},
}; };
@ -52,32 +52,23 @@ pub fn codegen(opts: CodegenOptions) -> Result<(), anyhow::Error> {
}; };
tx.visit_file_mut(&mut tree); tx.visit_file_mut(&mut tree);
let bindings = tree.to_token_stream().to_string(); bindings::write(
let filename = generated.join("bindings.rs"); &tree.to_token_stream().to_string(),
{ "",
let mut file = File::create(&filename)?; &generated.join("bindings.rs"),
write!(file, "{}", bindings)?; )?;
}
Command::new("rustfmt").arg(filename).status()?; bindings::write(
&tx.helpers.join(""),
let filename = generated.join("helpers.rs"); "use crate::bpf::generated::bindings::*;",
{ &generated.join("helpers.rs"),
let mut file = File::create(&filename)?; )?;
write!(file, "use crate::bpf::generated::bindings::*;")?;
for helper in &tx.helpers { bindings::write(
file.write(helper.as_bytes())?; &generate_getters_for_items(&tree.items, gen_probe_read_getter).to_string(),
} "use crate::bpf::generated::bindings::*;",
} &generated.join("getters.rs"),
Command::new("rustfmt").arg(filename).status()?; )?;
let getters = generate_getters_for_items(&tree.items, gen_probe_read_getter);
let filename = generated.join("getters.rs");
{
let mut file = File::create(&filename)?;
write!(file, "use crate::bpf::generated::bindings::*;")?;
write!(file, "{}", getters)?;
}
Command::new("rustfmt").arg(filename).status()?;
Ok(()) Ok(())
} }

@ -1,4 +1,9 @@
use std::process::Command; use std::{
fs::File,
io::{self, Write},
path::Path,
process::Command,
};
pub fn bindgen(types: &[&str], vars: &[&str]) -> Command { pub fn bindgen(types: &[&str], vars: &[&str]) -> Command {
let mut cmd = Command::new("bindgen"); let mut cmd = Command::new("bindgen");
@ -20,3 +25,12 @@ pub fn bindgen(types: &[&str], vars: &[&str]) -> Command {
cmd cmd
} }
pub fn write(bindings: &str, header: &str, filename: &Path) -> io::Result<()> {
let mut file = File::create(&filename)?;
file.write(header.as_bytes())?;
file.write(bindings.as_bytes())?;
Command::new("rustfmt").arg(filename).status()?;
Ok(())
}

Loading…
Cancel
Save