bool emit MarshalAs(UnmanagedType.U1) close #8

This commit is contained in:
neuecc 2023-03-03 17:49:05 +09:00
parent 95c7e56b95
commit 2ff100f718
5 changed files with 83 additions and 99 deletions

View File

@ -2,15 +2,15 @@
// fn main() -> Result<(), Box<dyn Error>> { // fn main() -> Result<(), Box<dyn Error>> {
fn main() { fn main() {
bindgen::Builder::default() // bindgen::Builder::default()
.header("c/lz4/lz4.h") // .header("c/lz4/lz4.h")
.header("c/lz4/lz4hc.h") // .header("c/lz4/lz4hc.h")
.header("c/lz4/lz4frame.h") // .header("c/lz4/lz4frame.h")
.header("c/lz4/xxhash.h") // .header("c/lz4/xxhash.h")
.generate() // .generate()
.unwrap() // .unwrap()
.write_to_file("src/lz4.rs") // .write_to_file("src/lz4.rs")
.unwrap(); // .unwrap();
cc::Build::new() cc::Build::new()
.files([ .files([
@ -40,23 +40,23 @@ fn main() {
// .generate()? // .generate()?
// .write_to_file("src/bullet3.rs")?; // .write_to_file("src/bullet3.rs")?;
csbindgen::Builder::default() // csbindgen::Builder::default()
.input_bindgen_file("src/lz4.rs") // .input_bindgen_file("src/lz4.rs")
.method_filter(|x| { !x.starts_with("_") && !x.starts_with("XXH") } ) // .method_filter(|x| { !x.starts_with("_") && !x.starts_with("XXH") } )
.rust_method_prefix("csbindgen_") // .rust_method_prefix("csbindgen_")
.rust_file_header("use super::lz4;") // .rust_file_header("use super::lz4;")
.rust_method_type_path("lz4") // .rust_method_type_path("lz4")
.csharp_class_name("LibLz4") // .csharp_class_name("LibLz4")
.csharp_namespace("CsBindgen") // .csharp_namespace("CsBindgen")
.csharp_dll_name("csbindgen_tests") // .csharp_dll_name("csbindgen_tests")
.csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") // .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal")
.csharp_entry_point_prefix("csbindgen_") // .csharp_entry_point_prefix("csbindgen_")
.csharp_method_prefix("") // .csharp_method_prefix("")
.csharp_class_accessibility("public") // .csharp_class_accessibility("public")
.csharp_c_long_convert("int") // .csharp_c_long_convert("int")
.csharp_c_ulong_convert("uint") // .csharp_c_ulong_convert("uint")
.generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs") // .generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs")
.unwrap(); // .unwrap();
csbindgen::Builder::default() csbindgen::Builder::default()
.input_extern_file("src/lib.rs") .input_extern_file("src/lib.rs")

View File

@ -14,6 +14,17 @@ pub extern "C" fn my_add(x: i32, y: i32) -> i32 {
x + y x + y
} }
#[allow(unused)]
#[no_mangle]
pub extern "C" fn my_bool(x: bool, y: bool, z: bool, xr: *mut bool, yr: *mut bool, zr: *mut bool) -> bool {
unsafe {
*xr = x;
*yr = y;
*zr = z;
true
}
}
#[test] #[test]
fn build_test() { fn build_test() {
// let path = std::env::current_dir().unwrap(); // let path = std::env::current_dir().unwrap();

View File

@ -9,7 +9,7 @@ pub fn emit_rust_method(list: &Vec<ExternMethod>, options: &BindgenOptions) -> S
let method_type_path = options.rust_method_type_path.as_str(); let method_type_path = options.rust_method_type_path.as_str();
let method_type_path2 = match options.rust_method_type_path.as_str() { let method_type_path2 = match options.rust_method_type_path.as_str() {
"" => "".to_string(), "" => "".to_string(),
x => x.to_string() + "::" x => x.to_string() + "::",
}; };
let method_prefix = &options.rust_method_prefix; let method_prefix = &options.rust_method_prefix;
let file_header = &options.rust_file_header; let file_header = &options.rust_file_header;
@ -81,7 +81,7 @@ pub fn emit_csharp(
methods: &Vec<ExternMethod>, methods: &Vec<ExternMethod>,
aliases: &HashMap<String, RustType>, aliases: &HashMap<String, RustType>,
structs: &Vec<RustStruct>, structs: &Vec<RustStruct>,
options: &BindgenOptions options: &BindgenOptions,
) -> String { ) -> String {
// configure // configure
let namespace = &options.csharp_namespace; let namespace = &options.csharp_namespace;
@ -90,13 +90,20 @@ pub fn emit_csharp(
let accessibility = &options.csharp_class_accessibility; let accessibility = &options.csharp_class_accessibility;
let dll_name = match options.csharp_if_symbol.as_str() { let dll_name = match options.csharp_if_symbol.as_str() {
"" => format!(" const string __DllName = \"{}\";", options.csharp_dll_name), "" => format!(
_ => { format!("#if {0} " const string __DllName = \"{}\";",
options.csharp_dll_name
),
_ => {
format!(
"#if {0}
const string __DllName = \"{1}\"; const string __DllName = \"{1}\";
#else #else
const string __DllName = \"{2}\"; const string __DllName = \"{2}\";
#endif #endif
", options.csharp_if_symbol, options.csharp_if_dll_name, options.csharp_dll_name) ",
options.csharp_if_symbol, options.csharp_if_dll_name, options.csharp_dll_name
)
} }
}; };
@ -104,8 +111,8 @@ pub fn emit_csharp(
for item in methods { for item in methods {
let method_name = &item.method_name; let method_name = &item.method_name;
let entry_point = match options.csharp_entry_point_prefix.as_str() { let entry_point = match options.csharp_entry_point_prefix.as_str() {
"" => format!("{method_prefix}{method_name}"), "" => format!("{method_prefix}{method_name}"),
x => format!("{x}{method_name}"), x => format!("{x}{method_name}"),
}; };
let return_type = match &item.return_type { let return_type = match &item.return_type {
Some(x) => x.to_csharp_string(options, aliases), Some(x) => x.to_csharp_string(options, aliases),
@ -115,13 +122,23 @@ pub fn emit_csharp(
let parameters = item let parameters = item
.parameters .parameters
.iter() .iter()
.map(|p| format!("{} {}", p.rust_type.to_csharp_string(options, aliases), p.escape_name())) .map(|p| {
let mut type_name = p.rust_type.to_csharp_string(options, aliases);
if type_name == "bool" {
type_name = "[MarshalAs(UnmanagedType.U1)] bool".to_string();
}
format!("{} {}", type_name, p.escape_name())
})
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
method_list_string.push_str_ln( method_list_string.push_str_ln(
format!(" [DllImport(__DllName, EntryPoint = \"{entry_point}\", CallingConvention = CallingConvention.Cdecl)]").as_str(), format!(" [DllImport(__DllName, EntryPoint = \"{entry_point}\", CallingConvention = CallingConvention.Cdecl)]").as_str(),
); );
if return_type == "bool" {
method_list_string.push_str_ln(" [return: MarshalAs(UnmanagedType.U1)]");
}
method_list_string.push_str_ln( method_list_string.push_str_ln(
format!(" public static extern {return_type} {method_prefix}{method_name}({parameters});").as_str(), format!(" public static extern {return_type} {method_prefix}{method_name}({parameters});").as_str(),
); );

View File

@ -6,79 +6,31 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
unsafe unsafe
{ {
//var v = NativeMethods(); var a = false;
var b = false;
var c = false;
var z = LibRust.my_add(100, 200);
var z = LibRust.my_bool(true, false, true, &a, &b, &c);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(z); Console.WriteLine(z);
var s = CsBindgen.LibLz4.LZ4_versionString();
var ss = new string((sbyte*)s);
Console.WriteLine(ss);
//var bytes = new byte[] { 1, 10, 100, 100, 100, 100, 100, 100 };
//var dest = new byte[100];
//fixed (byte* p = bytes)
//fixed (byte* d = dest)
//{
// var len = NativeMethods.csbindgen_LZ4_compress_default(p, d, bytes.Length, dest.Length);
//}
// var vvv = new string((sbyte*)v);
// Console.WriteLine(vvv);
} }
public static unsafe partial class LibraryImportNativeMethods
{ {
public static unsafe partial class LibLz4 const string __DllName = "csbindgen_tests";
{
static LibLz4()
{
NativeLibrary.SetDllImportResolver(typeof(LibLz4).Assembly, DllImportResolver);
}
static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName == __DllName)
{
var path = "runtimes/";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
path += "win-";
} [LibraryImport(__DllName, EntryPoint = "my_bool")]
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) [return: MarshalAs(UnmanagedType.U1)]
{ public static partial bool my_bool([MarshalAs(UnmanagedType.U1)] bool x, [MarshalAs(UnmanagedType.U1)] bool y, [MarshalAs(UnmanagedType.U1)] bool z, bool* xr, bool* yr, bool* zr);
path += "osx-";
}
else
{
path += "linux-";
}
if (RuntimeInformation.OSArchitecture == Architecture.X86)
{
path += "x86";
}
else if (RuntimeInformation.OSArchitecture == Architecture.X64)
{
path += "x64";
}
else if (RuntimeInformation.OSArchitecture == Architecture.Arm64)
{
path += "arm64";
}
path += "/native/" + __DllName;
return NativeLibrary.Load(path, assembly, searchPath);
}
return IntPtr.Zero;
}
}
} }

View File

@ -14,6 +14,10 @@ namespace CsBindgen
[DllImport(__DllName, EntryPoint = "my_add", CallingConvention = CallingConvention.Cdecl)] [DllImport(__DllName, EntryPoint = "my_add", CallingConvention = CallingConvention.Cdecl)]
public static extern int my_add(int x, int y); public static extern int my_add(int x, int y);
[DllImport(__DllName, EntryPoint = "my_bool", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool my_bool([MarshalAs(UnmanagedType.U1)] bool x, [MarshalAs(UnmanagedType.U1)] bool y, [MarshalAs(UnmanagedType.U1)] bool z, bool* xr, bool* yr, bool* zr);
} }