summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <me@emiliocobos.me>2016-04-03 01:03:06 +0200
committerEmilio Cobos Álvarez <me@emiliocobos.me>2016-04-03 01:03:06 +0200
commit7ee7bae7887899642f9c07f9c02ee841e9f06556 (patch)
treee329e319b979c3dfac010203d728d423cb322b07
parente476eb500f7af3290d20175c34e9682d0c136fd0 (diff)
gen: Allow specifying raw lines before the content starts
It's useful to add `use` or `include!` lines.
-rw-r--r--src/bin/bindgen.rs7
-rw-r--r--src/lib.rs21
2 files changed, 26 insertions, 2 deletions
diff --git a/src/bin/bindgen.rs b/src/bin/bindgen.rs
index 67ddc8a8..42c0cb40 100644
--- a/src/bin/bindgen.rs
+++ b/src/bin/bindgen.rs
@@ -139,6 +139,13 @@ fn parse_args(args: &[String]) -> ParseResult {
options.rename_types = false;
ix += 1;
}
+ "-raw-line" => {
+ if ix + 1 >= args_len {
+ return ParseResult::ParseErr("Missing raw-line argument".to_string());
+ }
+ options.raw_lines.push(args[ix + 1].clone());
+ ix += 2;
+ }
_ => {
options.clang_args.push(args[ix].clone());
ix += 1;
diff --git a/src/lib.rs b/src/lib.rs
index f8a392c3..c5737035 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -61,6 +61,11 @@ impl<'a> Builder<'a> {
self
}
+ pub fn raw_line<T: Into<String>>(&mut self, arg: T) -> &mut Self {
+ self.options.raw_lines.push(arg.into());
+ self
+ }
+
pub fn clang_arg<T: Into<String>>(&mut self, arg: T) -> &mut Self {
self.options.clang_args.push(arg.into());
self
@@ -142,6 +147,7 @@ pub struct BindgenOptions {
pub rename_types: bool,
pub derive_debug: bool,
pub override_enum_ty: String,
+ pub raw_lines: Vec<String>,
pub clang_args: Vec<String>,
}
@@ -162,6 +168,7 @@ impl Default for BindgenOptions {
derive_debug: true,
enable_cxx_namespaces: false,
override_enum_ty: "".to_string(),
+ raw_lines: vec![],
clang_args: Vec::new()
}
}
@@ -181,7 +188,8 @@ pub trait Logger {
#[derive(Debug, Clone)]
pub struct Bindings {
- module: ast::Mod
+ module: ast::Mod,
+ raw_lines: Vec<String>,
}
impl Bindings {
@@ -203,7 +211,8 @@ impl Bindings {
};
Ok(Bindings {
- module: module
+ module: module,
+ raw_lines: options.raw_lines.clone(),
})
}
@@ -229,6 +238,14 @@ impl Bindings {
#[cfg_attr(feature = "clippy", allow(needless_lifetimes))]
pub fn write<'a>(&self, mut writer: Box<Write + 'a>) -> io::Result<()> {
try!(writer.write("/* automatically generated by rust-bindgen */\n\n".as_bytes()));
+
+ for line in self.raw_lines.iter() {
+ try!(writer.write(line.as_bytes()));
+ }
+ if !self.raw_lines.is_empty() {
+ try!(writer.write("\n\n".as_bytes()));
+ }
+
let mut ps = pprust::rust_printer(writer);
try!(ps.print_mod(&self.module, &[]));
try!(ps.print_remaining_comments());