Skip to content

Commit 7040a30

Browse files
authored
Merge pull request #114 from nuskey8/main
feat: support multiple `csharp_dll_name_if()`
2 parents 9bf3ba4 + e8a3ddb commit 7040a30

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

csbindgen/src/builder.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ pub struct BindgenOptions {
2727
pub csharp_class_accessibility: String,
2828
pub csharp_entry_point_prefix: String,
2929
pub csharp_method_prefix: String,
30-
pub csharp_if_symbol: String,
31-
pub csharp_if_dll_name: String,
30+
pub csharp_if_dll_imports: Vec<(String, String)>,
3231
pub csharp_use_function_pointer: bool,
3332
pub csharp_use_nint_types: bool,
3433
pub csharp_imported_namespaces: Vec<String>,
@@ -56,8 +55,7 @@ impl Default for Builder {
5655
csharp_entry_point_prefix: "".to_string(),
5756
csharp_method_prefix: "".to_string(),
5857
csharp_class_accessibility: "internal".to_string(),
59-
csharp_if_symbol: "".to_string(),
60-
csharp_if_dll_name: "".to_string(),
58+
csharp_if_dll_imports: vec![],
6159
csharp_use_function_pointer: true,
6260
csharp_use_nint_types: true,
6361
csharp_imported_namespaces: vec![],
@@ -192,8 +190,7 @@ impl Builder {
192190
/// configure add C# dll name if directive,
193191
/// #if {if_symbol} __DllName = {if_dll_name}
194192
pub fn csharp_dll_name_if<T: Into<String>>(mut self, if_symbol: T, if_dll_name: T) -> Builder {
195-
self.options.csharp_if_symbol = if_symbol.into();
196-
self.options.csharp_if_dll_name = if_dll_name.into();
193+
self.options.csharp_if_dll_imports.push((if_symbol.into(), if_dll_name.into()));
197194
self
198195
}
199196

csbindgen/src/emitter.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,42 @@ pub fn emit_csharp(
9090
let method_prefix = &options.csharp_method_prefix;
9191
let accessibility = &options.csharp_class_accessibility;
9292

93-
let mut dll_name = match options.csharp_if_symbol.as_str() {
94-
"" => format!(
93+
let dll_name = if options.csharp_disable_emit_dll_name {
94+
"".to_string()
95+
} else if options.csharp_if_dll_imports.is_empty() {
96+
format!(
9597
" const string __DllName = \"{}\";",
9698
options.csharp_dll_name
97-
),
98-
_ => {
99+
)
100+
} else {
101+
let mut s = String::new();
102+
for (i, pair) in options.csharp_if_dll_imports.iter().enumerate() {
103+
let (if_symbol, if_dll_name) = pair;
104+
if i == 0 {
105+
s.push_str(
106+
format!(
107+
"#if {if_symbol}\n const string __DllName = \"{if_dll_name}\";\n"
108+
)
109+
.as_str(),
110+
);
111+
} else {
112+
s.push_str(
113+
format!(
114+
"#elif {if_symbol}\n const string __DllName = \"{if_dll_name}\";\n"
115+
)
116+
.as_str(),
117+
);
118+
}
119+
}
120+
s.push_str(
99121
format!(
100-
"#if {0}
101-
const string __DllName = \"{1}\";
102-
#else
103-
const string __DllName = \"{2}\";
104-
#endif
105-
",
106-
options.csharp_if_symbol, options.csharp_if_dll_name, options.csharp_dll_name
122+
"#else\n const string __DllName = \"{}\";\n#endif\n ",
123+
options.csharp_dll_name
107124
)
108-
}
125+
.as_str(),
126+
);
127+
s
109128
};
110-
if options.csharp_disable_emit_dll_name {
111-
dll_name = "".to_string();
112-
}
113129

114130
let mut method_list_string = String::new();
115131
let mut delegate_list = Vec::new();

0 commit comments

Comments
 (0)