Skip to content

Commit 961ecbf

Browse files
authored
Merge pull request #73 from Cysharp/feature/csharp_file_headerfooter
Add csharp_file_header/footer
2 parents 8bd6285 + 41ce654 commit 961ecbf

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ csbindgen::Builder::default()
299299
"FfiConfiguration" => "Configuration".into(),
300300
_ => x,
301301
})
302+
.csharp_file_header("#if !UNITY_WEBGL") // optional, default: ""
303+
.csharp_file_footer("#endif") // optional, default: ""
302304
.generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs") // required
303305
.unwrap();
304306
```

csbindgen-tests/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ fn main() -> Result<(), Box<dyn Error>> {
143143
.csharp_namespace("Physx")
144144
.csharp_class_name("LibPhysx")
145145
.csharp_dll_name("libphys")
146+
.csharp_file_header("#if !FALSE")
147+
.csharp_file_footer("#endif")
146148
.generate_csharp_file("../dotnet-sandbox/libphysx_csbindgen.cs")?;
147149

148150
Ok(())

csbindgen/src/builder.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub struct BindgenOptions {
3434
pub csharp_imported_namespaces: Vec<String>,
3535
pub csharp_generate_const_filter: fn(const_name: &str) -> bool,
3636
pub csharp_type_rename: fn(type_name: String) -> String,
37+
pub csharp_file_header: String,
38+
pub csharp_file_footer: String,
3739
pub always_included_types: Vec<String>,
3840
}
3941

@@ -61,6 +63,8 @@ impl Default for Builder {
6163
csharp_imported_namespaces: vec![],
6264
csharp_generate_const_filter: |_| false,
6365
csharp_type_rename: identity,
66+
csharp_file_header: "".to_string(),
67+
csharp_file_footer: "".to_string(),
6468
always_included_types: vec![],
6569
},
6670
}
@@ -225,6 +229,18 @@ impl Builder {
225229
self
226230
}
227231

232+
/// configure the additional header for the generated C# code.
233+
pub fn csharp_file_header<T: Into<String>>(mut self, csharp_file_header: T) -> Builder {
234+
self.options.csharp_file_header = csharp_file_header.into();
235+
self
236+
}
237+
238+
/// configure the additional footer for the generated C# code.
239+
pub fn csharp_file_footer<T: Into<String>>(mut self, csharp_file_footer: T) -> Builder {
240+
self.options.csharp_file_footer = csharp_file_footer.into();
241+
self
242+
}
243+
228244
pub fn generate_csharp_file<P: AsRef<Path>>(
229245
&self,
230246
csharp_output_path: P,

csbindgen/src/emitter.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,25 @@ pub fn emit_csharp(
344344
}
345345
}
346346

347+
let file_header = if options.csharp_file_header.len() > 0 {
348+
options.csharp_file_header.to_string() + "\n"
349+
} else {
350+
"".to_string()
351+
} + "// <auto-generated>
352+
// This code is generated by csbindgen.
353+
// DON'T CHANGE THIS DIRECTLY.
354+
// </auto-generated>
355+
#pragma warning disable CS8500
356+
#pragma warning disable CS8981";
357+
let file_footer = &options.csharp_file_footer;
358+
347359
let mut imported_namespaces = String::new();
348360
for name in &options.csharp_imported_namespaces {
349361
imported_namespaces.push_str_ln(format!("using {name};").as_str());
350362
}
351363

352364
let result = format!(
353-
"// <auto-generated>
354-
// This code is generated by csbindgen.
355-
// DON'T CHANGE THIS DIRECTLY.
356-
// </auto-generated>
357-
#pragma warning disable CS8500
358-
#pragma warning disable CS8981
365+
"{file_header}
359366
using System;
360367
using System.Runtime.InteropServices;
361368
{imported_namespaces}
@@ -374,8 +381,7 @@ namespace {namespace}
374381
{structs_string}
375382
{enum_string}
376383
}}
377-
"
378-
);
384+
{file_footer}");
379385

380386
result
381387
}

dotnet-sandbox/libphysx_csbindgen.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if !FALSE
12
// <auto-generated>
23
// This code is generated by csbindgen.
34
// DON'T CHANGE THIS DIRECTLY.
@@ -11271,4 +11272,4 @@ internal enum PxPvdInstrumentationFlags : byte
1127111272

1127211273

1127311274
}
11274-
11275+
#endif

0 commit comments

Comments
 (0)