Skip to content

Add an option to add lines per module. #1308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn main() {
.enable_cxx_namespaces()
.rustified_enum(".*")
.raw_line("pub use self::root::*;")
.module_raw_line("root::testing", "pub type Bar = i32;")
.header("cpp/Test.h")
.clang_args(&["-x", "c++", "-std=c++11"])
.parse_callbacks(Box::new(MacroCallback {macros: macros.clone()}))
Expand Down
3 changes: 3 additions & 0 deletions bindgen-integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::ffi::CStr;
use std::os::raw::c_int;
use std::mem;

#[allow(unused)]
use bindings::testing::Bar; // This type is generated from module_raw_line.

#[test]
fn test_static_array() {
let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() };
Expand Down
19 changes: 15 additions & 4 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,18 @@ impl CodeGenerator for Module {
let mut found_any = false;
let inner_items = result.inner(|result| {
result.push(root_import(ctx, item));

let path = item.namespace_aware_canonical_path(ctx).join("::");
if let Some(raw_lines) = ctx.options().module_lines.get(&path) {
for raw_line in raw_lines {
found_any = true;
// FIXME(emilio): The use of `Term` is an abuse, but we abuse it
// in a bunch more places.
let line = Term::new(raw_line, Span::call_site());
result.push(quote! { #line });
}
}

codegen_self(result, &mut found_any);
});

Expand All @@ -431,16 +443,15 @@ impl CodeGenerator for Module {
}

let name = item.canonical_name(ctx);

result.push(if name == "root" {
let ident = ctx.rust_ident(name);
result.push(if item.id() == ctx.root_module() {
quote! {
#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
pub mod #ident {
#( #inner_items )*
}
}
} else {
let ident = ctx.rust_ident(name);
quote! {
pub mod #ident {
#( #inner_items )*
Expand Down
41 changes: 39 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ use parse::{ClangItemParser, ParseError};
use regex_set::RegexSet;

use std::borrow::Cow;
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use std::iter;
Expand Down Expand Up @@ -766,11 +767,40 @@ impl Builder {

/// Add a string to prepend to the generated bindings. The string is passed
/// through without any modification.
pub fn raw_line<T: Into<String>>(mut self, arg: T) -> Builder {
pub fn raw_line<T: Into<String>>(mut self, arg: T) -> Self {
self.options.raw_lines.push(arg.into());
self
}

/// Add a given line to the beginning of module `mod`.
pub fn module_raw_line<T, U>(mut self, mod_: T, line: U) -> Self
where
T: Into<String>,
U: Into<String>,
{
self.options
.module_lines
.entry(mod_.into())
.or_insert_with(Vec::new)
.push(line.into());
self
}

/// Add a given set of lines to the beginning of module `mod`.
pub fn module_raw_lines<T, I>(mut self, mod_: T, lines: I) -> Self
where
T: Into<String>,
I: IntoIterator,
I::Item: Into<String>,
{
self.options
.module_lines
.entry(mod_.into())
.or_insert_with(Vec::new)
.extend(lines.into_iter().map(Into::into));
self
}

/// Add an argument to be passed straight through to clang.
pub fn clang_arg<T: Into<String>>(mut self, arg: T) -> Builder {
self.options.clang_args.push(arg.into());
Expand Down Expand Up @@ -1300,9 +1330,15 @@ struct BindgenOptions {
/// Whether we should convert float types to f32/f64 types.
convert_floats: bool,

/// The set of raw lines to prepend to the generated Rust code.
/// The set of raw lines to prepend to the top-level module of generated
/// Rust code.
raw_lines: Vec<String>,

/// The set of raw lines to prepend to each of the modules.
///
/// This only makes sense if the `enable_cxx_namespaces` option is set.
module_lines: HashMap<String, Vec<String>>,

/// The set of arguments to pass straight through to Clang.
clang_args: Vec<String>,

Expand Down Expand Up @@ -1448,6 +1484,7 @@ impl Default for BindgenOptions {
msvc_mangling: false,
convert_floats: true,
raw_lines: vec![],
module_lines: HashMap::default(),
clang_args: vec![],
input_header: None,
input_unsaved_files: vec![],
Expand Down