if and entrypoint

This commit is contained in:
neuecc 2023-02-28 10:50:15 +09:00
parent d9764bc59a
commit 33d1012da4
5 changed files with 134 additions and 118 deletions

View File

@ -40,7 +40,9 @@ fn main() -> Result<(), Box<dyn Error>> {
.rust_method_type_path("lz4")
.csharp_class_name("LibLz4")
.csharp_dll_name("csbindgen_tests")
.csharp_method_prefix("csbindgen_")
.csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal")
.csharp_entry_point_prefix("csbindgen_")
.csharp_method_prefix("")
.generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs")?;
// csbindgen::Builder::new()

View File

@ -13,42 +13,18 @@ pub struct Builder {
pub struct BindgenOptions {
pub input_bindgen_file: String,
/// add original extern call type prefix to rust wrapper,
/// `return {rust_method_type_path}::foo()`
pub rust_method_type_path: String,
/// add method prefix to rust wrapper,
/// `pub extern "C" fn {rust_method_prefix}foo()`
pub rust_method_prefix: String,
/// add file header string to rust wrapper,
/// `mod lz4;`, `use super::lz4;`
pub rust_file_header: String,
/// configure C# file namespace(default is `CsBindgen`),
/// "namespace {csharp_namespace}"
pub csharp_namespace: String,
/// configure C# class name(default is `NativeMethods`),
/// `public static unsafe partial class {csharp_class_name}`
pub csharp_class_name: String,
/// configure C# load dll name,
/// `[DllImport({csharp_dll_name})]`
pub csharp_dll_name: String,
/// configure C# calling method name prefix,
/// `public static extern void {csharp_method_prefix}foo()`
pub csharp_entry_point_prefix: String,
pub csharp_method_prefix: String,
/// configure c_long to {csharp_c_long_convert} type,
/// default is `Int32`.
pub csharp_c_long_convert: String,
/// configure c_long to {csharp_c_long_convert} type,
/// default is `UInt32`.
pub csharp_c_ulong_convert: String,
pub csharp_if_symbol: String,
pub csharp_if_dll_name: String,
}
impl Builder {
@ -62,9 +38,12 @@ impl Builder {
csharp_namespace: "CsBindgen".to_string(),
csharp_class_name: "NativeMethods".to_string(),
csharp_dll_name: "".to_string(),
csharp_entry_point_prefix: "".to_string(),
csharp_method_prefix: "".to_string(),
csharp_c_long_convert: "int".to_string(),
csharp_c_ulong_convert: "uint".to_string(),
csharp_if_symbol: "".to_string(),
csharp_if_dll_name: "".to_string(),
},
}
}
@ -117,6 +96,16 @@ impl Builder {
self
}
/// configure C# DllImport EntryPoint prefix,
/// `[DllImport(, EntryPoint ="{csharp_entry_point_prefix}foo")]`
pub fn csharp_entry_point_prefix<T: Into<String>>(
mut self,
csharp_entry_point_prefix: T,
) -> Builder {
self.options.csharp_entry_point_prefix = csharp_entry_point_prefix.into();
self
}
/// configure C# calling method name prefix,
/// `public static extern void {csharp_method_prefix}foo()`
pub fn csharp_method_prefix<T: Into<String>>(mut self, csharp_method_prefix: T) -> Builder {
@ -138,6 +127,12 @@ impl Builder {
self
}
pub fn csharp_dll_name_if<T: Into<String>>(mut self, if_symbol: T, if_dll_name: T) -> Builder {
self.options.csharp_if_symbol = if_symbol.into();
self.options.csharp_if_dll_name = if_dll_name.into();
self
}
// pub fn generate_csharp_file<T: AsRef<Path>>(&self, csharp_output_path: T) -> io::Result<()> {
// let mut file = OpenOptions::new()
// .write(true)

View File

@ -86,12 +86,26 @@ pub fn emit_csharp(
// configure
let namespace = &options.csharp_namespace;
let class_name = &options.csharp_class_name;
let dll_name = &options.csharp_dll_name;
let method_prefix = &options.csharp_method_prefix;
let dll_name = match options.csharp_if_symbol.as_str() {
"" => format!(" const string __DllName = \"{}\";", options.csharp_dll_name),
_ => { format!("#if {0}
const string __DllName = \"{1}\";
#else
const string __DllName = \"{2}\";
#endif
", options.csharp_if_symbol, options.csharp_if_dll_name, options.csharp_dll_name)
}
};
let mut method_list_string = String::new();
for item in methods {
let method_name = &item.method_name;
let entry_point = match options.csharp_entry_point_prefix.as_str() {
"" => format!("{method_prefix}{method_name}"),
x => format!("{x}{method_name}"),
};
let return_type = match &item.return_type {
Some(x) => x.to_csharp_string(&options, &aliases),
None => "void".to_string(),
@ -105,7 +119,7 @@ pub fn emit_csharp(
.join(", ");
method_list_string.push_str_ln(
" [DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]",
format!(" [DllImport(__DllName, EntryPoint = \"{entry_point}\", CallingConvention = CallingConvention.Cdecl)]").as_str(),
);
method_list_string.push_str_ln(
format!(" public static extern {return_type} {method_prefix}{method_name}({parameters});").as_str(),
@ -165,7 +179,7 @@ namespace {namespace}
{{
public static unsafe partial class {class_name}
{{
const string __DllName = \"{dll_name}\";
{dll_name}
{method_list_string}
}}

View File

@ -7,7 +7,7 @@ unsafe
{
//var v = NativeMethods();
var s = LibLz4.csbindgen_LZ4_versionString();
var s = LibLz4.LZ4_versionString();
var ss = new string((sbyte*)s);
Console.WriteLine(ss);

View File

@ -9,133 +9,138 @@ namespace CsBindgen
{
public static unsafe partial class LibLz4
{
#if UNITY_IOS && !UNITY_EDITOR
const string __DllName = "__Internal";
#else
const string __DllName = "csbindgen_tests";
#endif
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_versionNumber();
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern byte* csbindgen_LZ4_versionString();
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_versionNumber", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_versionNumber();
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_default(byte* src, byte* dst, int srcSize, int dstCapacity);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_versionString", CallingConvention = CallingConvention.Cdecl)]
public static extern byte* LZ4_versionString();
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_safe(byte* src, byte* dst, int compressedSize, int dstCapacity);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_default", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_default(byte* src, byte* dst, int srcSize, int dstCapacity);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compressBound(int inputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_safe", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_safe(byte* src, byte* dst, int compressedSize, int dstCapacity);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_fast(byte* src, byte* dst, int srcSize, int dstCapacity, int acceleration);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compressBound", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compressBound(int inputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_sizeofState();
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_fast", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_fast(byte* src, byte* dst, int srcSize, int dstCapacity, int acceleration);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_fast_extState(void* state, byte* src, byte* dst, int srcSize, int dstCapacity, int acceleration);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_sizeofState", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_sizeofState();
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_destSize(byte* src, byte* dst, int* srcSizePtr, int targetDstSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_fast_extState", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_fast_extState(void* state, byte* src, byte* dst, int srcSize, int dstCapacity, int acceleration);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_safe_partial(byte* src, byte* dst, int srcSize, int targetOutputSize, int dstCapacity);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_destSize", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_destSize(byte* src, byte* dst, int* srcSizePtr, int targetDstSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern LZ4_stream_u* csbindgen_LZ4_createStream();
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_safe_partial", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_safe_partial(byte* src, byte* dst, int srcSize, int targetOutputSize, int dstCapacity);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_freeStream(LZ4_stream_u* streamPtr);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_createStream", CallingConvention = CallingConvention.Cdecl)]
public static extern LZ4_stream_u* LZ4_createStream();
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void csbindgen_LZ4_resetStream_fast(LZ4_stream_u* streamPtr);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_freeStream", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_freeStream(LZ4_stream_u* streamPtr);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_loadDict(LZ4_stream_u* streamPtr, byte* dictionary, int dictSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_resetStream_fast", CallingConvention = CallingConvention.Cdecl)]
public static extern void LZ4_resetStream_fast(LZ4_stream_u* streamPtr);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_fast_continue(LZ4_stream_u* streamPtr, byte* src, byte* dst, int srcSize, int dstCapacity, int acceleration);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_loadDict", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_loadDict(LZ4_stream_u* streamPtr, byte* dictionary, int dictSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_saveDict(LZ4_stream_u* streamPtr, byte* safeBuffer, int maxDictSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_fast_continue", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_fast_continue(LZ4_stream_u* streamPtr, byte* src, byte* dst, int srcSize, int dstCapacity, int acceleration);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern LZ4_streamDecode_u* csbindgen_LZ4_createStreamDecode();
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_saveDict", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_saveDict(LZ4_stream_u* streamPtr, byte* safeBuffer, int maxDictSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_freeStreamDecode(LZ4_streamDecode_u* LZ4_stream);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_createStreamDecode", CallingConvention = CallingConvention.Cdecl)]
public static extern LZ4_streamDecode_u* LZ4_createStreamDecode();
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_setStreamDecode(LZ4_streamDecode_u* LZ4_streamDecode, byte* dictionary, int dictSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_freeStreamDecode", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_freeStreamDecode(LZ4_streamDecode_u* LZ4_stream);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decoderRingBufferSize(int maxBlockSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_setStreamDecode", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_setStreamDecode(LZ4_streamDecode_u* LZ4_streamDecode, byte* dictionary, int dictSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_safe_continue(LZ4_streamDecode_u* LZ4_streamDecode, byte* src, byte* dst, int srcSize, int dstCapacity);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decoderRingBufferSize", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decoderRingBufferSize(int maxBlockSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_safe_usingDict(byte* src, byte* dst, int srcSize, int dstCapacity, byte* dictStart, int dictSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_safe_continue", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_safe_continue(LZ4_streamDecode_u* LZ4_streamDecode, byte* src, byte* dst, int srcSize, int dstCapacity);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_safe_partial_usingDict(byte* src, byte* dst, int compressedSize, int targetOutputSize, int maxOutputSize, byte* dictStart, int dictSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_safe_usingDict", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_safe_usingDict(byte* src, byte* dst, int srcSize, int dstCapacity, byte* dictStart, int dictSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern LZ4_stream_u* csbindgen_LZ4_initStream(void* buffer, UIntPtr size);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_safe_partial_usingDict", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_safe_partial_usingDict(byte* src, byte* dst, int compressedSize, int targetOutputSize, int maxOutputSize, byte* dictStart, int dictSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress(byte* src, byte* dest, int srcSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_initStream", CallingConvention = CallingConvention.Cdecl)]
public static extern LZ4_stream_u* LZ4_initStream(void* buffer, UIntPtr size);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_limitedOutput(byte* src, byte* dest, int srcSize, int maxOutputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress(byte* src, byte* dest, int srcSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_withState(void* state, byte* source, byte* dest, int inputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_limitedOutput", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_limitedOutput(byte* src, byte* dest, int srcSize, int maxOutputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_limitedOutput_withState(void* state, byte* source, byte* dest, int inputSize, int maxOutputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_withState", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_withState(void* state, byte* source, byte* dest, int inputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_continue(LZ4_stream_u* LZ4_streamPtr, byte* source, byte* dest, int inputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_limitedOutput_withState", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_limitedOutput_withState(void* state, byte* source, byte* dest, int inputSize, int maxOutputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_compress_limitedOutput_continue(LZ4_stream_u* LZ4_streamPtr, byte* source, byte* dest, int inputSize, int maxOutputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_continue", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_continue(LZ4_stream_u* LZ4_streamPtr, byte* source, byte* dest, int inputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_uncompress(byte* source, byte* dest, int outputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_compress_limitedOutput_continue", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_compress_limitedOutput_continue(LZ4_stream_u* LZ4_streamPtr, byte* source, byte* dest, int inputSize, int maxOutputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_uncompress_unknownOutputSize(byte* source, byte* dest, int isize_, int maxOutputSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_uncompress", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_uncompress(byte* source, byte* dest, int outputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void* csbindgen_LZ4_create(byte* inputBuffer);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_uncompress_unknownOutputSize", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_uncompress_unknownOutputSize(byte* source, byte* dest, int isize_, int maxOutputSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_sizeofStreamState();
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_create", CallingConvention = CallingConvention.Cdecl)]
public static extern void* LZ4_create(byte* inputBuffer);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_resetStreamState(void* state, byte* inputBuffer);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_sizeofStreamState", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_sizeofStreamState();
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern byte* csbindgen_LZ4_slideInputBuffer(void* state);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_resetStreamState", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_resetStreamState(void* state, byte* inputBuffer);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_safe_withPrefix64k(byte* src, byte* dst, int compressedSize, int maxDstSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_slideInputBuffer", CallingConvention = CallingConvention.Cdecl)]
public static extern byte* LZ4_slideInputBuffer(void* state);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_fast_withPrefix64k(byte* src, byte* dst, int originalSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_safe_withPrefix64k", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_safe_withPrefix64k(byte* src, byte* dst, int compressedSize, int maxDstSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_fast(byte* src, byte* dst, int originalSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_fast_withPrefix64k", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_fast_withPrefix64k(byte* src, byte* dst, int originalSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_fast_continue(LZ4_streamDecode_u* LZ4_streamDecode, byte* src, byte* dst, int originalSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_fast", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_fast(byte* src, byte* dst, int originalSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int csbindgen_LZ4_decompress_fast_usingDict(byte* src, byte* dst, int originalSize, byte* dictStart, int dictSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_fast_continue", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_fast_continue(LZ4_streamDecode_u* LZ4_streamDecode, byte* src, byte* dst, int originalSize);
[DllImport(__DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void csbindgen_LZ4_resetStream(LZ4_stream_u* streamPtr);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_decompress_fast_usingDict", CallingConvention = CallingConvention.Cdecl)]
public static extern int LZ4_decompress_fast_usingDict(byte* src, byte* dst, int originalSize, byte* dictStart, int dictSize);
[DllImport(__DllName, EntryPoint = "csbindgen_LZ4_resetStream", CallingConvention = CallingConvention.Cdecl)]
public static extern void LZ4_resetStream(LZ4_stream_u* streamPtr);
}