Skip to content

Commit 046e73e

Browse files
committed
Add remove-function-prefix option; Add test
1 parent df0a3d8 commit 046e73e

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/options.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Debug;
12
use bindgen::{
23
builder, AliasVariation, Builder, CodegenConfig, EnumVariation,
34
MacroTypeVariation, RustTarget, DEFAULT_ANON_FIELDS_PREFIX,
@@ -8,6 +9,34 @@ use std::fs::File;
89
use std::io::{self, stderr, Error, ErrorKind, Write};
910
use std::path::PathBuf;
1011
use std::str::FromStr;
12+
use bindgen::callbacks::ParseCallbacks;
13+
14+
#[derive(Debug)]
15+
pub struct LinkNameOverrideParseCallback {
16+
pub remove_function_prefix: Option<String>,
17+
}
18+
19+
impl LinkNameOverrideParseCallback {
20+
pub fn new(prefix: &str) -> Self {
21+
LinkNameOverrideParseCallback {
22+
remove_function_prefix: Some(prefix.to_string()),
23+
}
24+
}
25+
}
26+
27+
impl ParseCallbacks for LinkNameOverrideParseCallback {
28+
fn link_name_override(
29+
&self,
30+
function_name: &str,
31+
) -> Option<String> {
32+
if let Some(prefix) = &self.remove_function_prefix {
33+
if function_name.starts_with(prefix) {
34+
return Some(function_name[prefix.len()..].to_string());
35+
}
36+
}
37+
None
38+
}
39+
}
1140

1241
/// Construct a new [`Builder`](./struct.Builder.html) from command line flags.
1342
pub fn builder_from_flags<I>(
@@ -545,6 +574,11 @@ where
545574
Arg::new("vtable-generation")
546575
.long("vtable-generation")
547576
.help("Enables generation of vtable functions."),
577+
Arg::new("remove-function-prefix")
578+
.long("remove-function-prefix")
579+
.multiple_occurrences(true)
580+
.takes_value(true)
581+
.help("Remove prefix when generating Rust function name.")
548582
]) // .args()
549583
.get_matches_from(args);
550584

@@ -1015,6 +1049,11 @@ where
10151049
builder = builder.vtable_generation(true);
10161050
}
10171051

1052+
if let Some(prefix) = matches.value_of("remove-function-prefix") {
1053+
let lnopc = LinkNameOverrideParseCallback::new(prefix);
1054+
builder = builder.parse_callbacks(Box::new(lnopc));
1055+
}
1056+
10181057
let verbose = matches.is_present("verbose");
10191058

10201059
Ok((builder, output, verbose))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
extern "C" {
9+
#[link_name = "\u{1}my_custom_prefix_function_name"]
10+
pub fn function_name(x: ::std::os::raw::c_int);
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: --remove-function-prefix my_custom_prefix_
2+
// bindgen-parse-callbacks: remove-function-prefix-my_custom_prefix_
3+
4+
void my_custom_prefix_function_name(const int x);
5+

tests/parse_callbacks/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bindgen::callbacks::*;
2+
use crate::options::LinkNameOverrideParseCallback;
23

34
#[derive(Debug)]
45
struct EnumVariantRename;
@@ -36,7 +37,15 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
3637
"enum-variant-rename" => Box::new(EnumVariantRename),
3738
"blocklisted-type-implements-trait" => {
3839
Box::new(BlocklistedTypeImplementsTrait)
40+
},
41+
call_back => {
42+
if call_back.starts_with("remove-function-prefix-") {
43+
let prefix = call_back.split("remove-function-prefix-").last().to_owned();
44+
let lnopc = LinkNameOverrideParseCallback::new(prefix.unwrap());
45+
Box::new(lnopc)
46+
} else {
47+
panic!("Couldn't find name ParseCallbacks: {}", cb)
48+
}
3949
}
40-
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
4150
}
4251
}

0 commit comments

Comments
 (0)