Skip to content

Commit

Permalink
Add option for all module raw lines
Browse files Browse the repository at this point in the history
Provide an option to add a line to every mod generated by bindgen.

Part of google/autocxx#124, though
this is only in fact necessary if we make the changes given in
google/autocxx#1456.
  • Loading branch information
adetaylor committed Feb 26, 2025
1 parent 20aa65a commit 88a2125
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions bindgen-tests/tests/expectations/tests/namespace.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/namespace.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --enable-cxx-namespaces --module-raw-line root::whatever 'pub type whatever_other_thing_t = whatever_int_t;'
// bindgen-flags: --enable-cxx-namespaces --module-raw-line root::whatever 'pub type whatever_other_thing_t = whatever_int_t;' --every-module-raw-line 'struct PerModStruct;'

void top_level();

Expand Down
9 changes: 9 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ impl CodeGenerator for Module {
.namespace_aware_canonical_path(ctx)
.join("::")
.into_boxed_str();

if let Some(raw_lines) = ctx.options().module_lines.get(&path) {
for raw_line in raw_lines {
found_any = true;
Expand All @@ -617,6 +618,14 @@ impl CodeGenerator for Module {
);
}
}
// For lines we add to all modules, don't modify `found_any`
// since we don't want to generate this module unless there's
// other stuff present.
result.extend(ctx.options().every_module_raw_lines.iter().map(
|raw_line| {
proc_macro2::TokenStream::from_str(raw_line).unwrap()
},
));

codegen_self(result, &mut found_any);
});
Expand Down
5 changes: 5 additions & 0 deletions bindgen/options/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ struct BindgenCommand {
/// Add a raw line of Rust code at the beginning of output.
#[arg(long)]
raw_line: Vec<String>,
/// Add a raw line of Rust code at the beginning of each module.
#[arg(long)]
every_module_raw_line: Vec<String>,
/// Add a RAW_LINE of Rust code to a given module with name MODULE_NAME.
#[arg(long, number_of_values = 2, value_names = ["MODULE_NAME", "RAW_LINE"])]
module_raw_line: Vec<String>,
Expand Down Expand Up @@ -601,6 +604,7 @@ where
opaque_type,
output,
raw_line,
every_module_raw_line,
module_raw_line,
rust_target,
rust_edition,
Expand Down Expand Up @@ -907,6 +911,7 @@ where
block_extern_crate,
opaque_type,
raw_line,
every_module_raw_line,
use_core => |b, _| b.use_core(),
distrust_clang_mangling => |b, _| b.trust_clang_mangling(false),
conservative_inline_namespaces => |b, _| b.conservative_inline_namespaces(),
Expand Down
18 changes: 18 additions & 0 deletions bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,24 @@ options! {
}
},
},
/// The set of raw lines to be prepended to every module of the generated Rust code.
every_module_raw_lines: Vec<Box<str>> {
methods: {
/// Add a line of Rust code at the beginning of each module (i.e.
/// C++ namespace) in the generated code. The string is
/// passed through without any modification.
pub fn every_module_raw_line<T: Into<String>>(mut self, arg: T) -> Self {
self.options.every_module_raw_lines.push(arg.into().into_boxed_str());
self
}
},
as_args: |every_module_raw_lines, args| {
for line in every_module_raw_lines {
args.push("--every-module-raw-line".to_owned());
args.push(line.clone().into());
}
},
},
/// The set of raw lines to prepend to different modules.
module_lines: HashMap<Box<str>, Vec<Box<str>>> {
methods: {
Expand Down

0 comments on commit 88a2125

Please sign in to comment.