mirror of
https://github.com/Sarsoo/csbindgen.git
synced 2024-12-23 06:56:27 +00:00
0.1.1
This commit is contained in:
parent
7218fe63b4
commit
8f65b0dba9
89
README.md
89
README.md
@ -154,11 +154,13 @@ csbindgen::Builder::default()
|
|||||||
.csharp_entry_point_prefix("csbindgen_")
|
.csharp_entry_point_prefix("csbindgen_")
|
||||||
.csharp_method_prefix("")
|
.csharp_method_prefix("")
|
||||||
.csharp_c_long_convert("int")
|
.csharp_c_long_convert("int")
|
||||||
.csharp_c_long_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();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
It will be embedded in the placeholder of the output file.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
use ::std::os::raw::*;
|
use ::std::os::raw::*;
|
||||||
@ -194,19 +196,88 @@ namespace {csharp_namespace}
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Builder options: Rust to C#
|
Adjust `rust_file_header` and `rust_method_type_path` to match your module configuration.
|
||||||
---
|
|
||||||
|
|
||||||
csbindgen::Builder::default()
|
`method_filter` allows you to specify which methods to exclude; if unspecified, methods prefixed with `_` are excluded by default.
|
||||||
.input_extern_file("src/lib.rs")
|
|
||||||
.csharp_class_name("LibRust")
|
|
||||||
.csharp_dll_name("csbindgen_tests")
|
|
||||||
.generate_csharp_file("../dotnet-sandbox/method_call.cs")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
|
`rust_method_prefix` and `csharp_method_prefix` or `csharp_entry_point_prefix` must be adjusted to match the method name to be called.
|
||||||
|
|
||||||
|
`csharp_dll_name_if` is optional. If specified, `#if` allows two DllName to be specified, which is useful if the name must be `__Internal` at iOS build.
|
||||||
|
|
||||||
|
If the file path to be loaded needs to be changed depending on the operating system, the following load code can be used.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public static unsafe partial class NativeMethods
|
||||||
|
{
|
||||||
|
// https://docs.microsoft.com/en-us/dotnet/standard/native-interop/cross-platform
|
||||||
|
// Library path will search
|
||||||
|
// win => __DllName, __DllName.dll
|
||||||
|
// linux, osx => __DllName.so, __DllName.dylib
|
||||||
|
// __DllName
|
||||||
|
|
||||||
|
static NativeMethods()
|
||||||
|
{
|
||||||
|
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, DllImportResolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
|
||||||
|
{
|
||||||
|
if (libraryName == __DllName)
|
||||||
|
{
|
||||||
|
var path = "runtimes/";
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
path += "win-";
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`csharp_c_long_convert` and `csharp_c_ulong_convert` configure how handles `c_long` and `c_ulong` to C# type. default is to `int` and `uint` because `LLP64` is 32bit representation but you can change it to 64bit.
|
||||||
|
|
||||||
|
## Builder options: Rust to C#
|
||||||
|
|
||||||
|
Rust to C# is similar workflow as C to C#, use the `input_extern_file` -> setup options -> `generate_csharp_file`.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
csbindgen::Builder::default()
|
||||||
|
.input_extern_file("src/lib.rs")
|
||||||
|
.csharp_class_name("LibRust")
|
||||||
|
.csharp_dll_name("csbindgen_tests")
|
||||||
|
.generate_csharp_file("../dotnet-sandbox/NativeMethods.cs")
|
||||||
|
.unwrap();
|
||||||
|
```
|
||||||
|
|
||||||
|
`generate_csharp_file` does not generate Rust file so no need to use `rust_` option.
|
||||||
|
|
||||||
License
|
License
|
||||||
---
|
---
|
||||||
|
@ -53,7 +53,7 @@ fn main() {
|
|||||||
.csharp_entry_point_prefix("csbindgen_")
|
.csharp_entry_point_prefix("csbindgen_")
|
||||||
.csharp_method_prefix("")
|
.csharp_method_prefix("")
|
||||||
.csharp_c_long_convert("int")
|
.csharp_c_long_convert("int")
|
||||||
.csharp_c_long_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();
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "csbindgen"
|
name = "csbindgen"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = [
|
authors = [
|
||||||
"Yoshifumi Kawai <ils@neue.cc>",
|
"Yoshifumi Kawai <ils@neue.cc>",
|
||||||
|
@ -142,7 +142,7 @@ impl Builder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// configure c_long to {csharp_c_long_convert} type,
|
/// configure c_ulong to {csharp_c_ulong_convert} type,
|
||||||
/// default is `uint`.
|
/// default is `uint`.
|
||||||
pub fn csharp_c_ulong_convert<T: Into<String>>(mut self, csharp_c_ulong_convert: T) -> Builder {
|
pub fn csharp_c_ulong_convert<T: Into<String>>(mut self, csharp_c_ulong_convert: T) -> Builder {
|
||||||
self.options.csharp_c_ulong_convert = csharp_c_ulong_convert.into();
|
self.options.csharp_c_ulong_convert = csharp_c_ulong_convert.into();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// See https://aka.ms/new-console-template for more information
|
// See https://aka.ms/new-console-template for more information
|
||||||
//using Csbindgen;
|
//using Csbindgen;
|
||||||
using CsBindgen;
|
using CsBindgen;
|
||||||
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
unsafe
|
unsafe
|
||||||
@ -31,3 +32,54 @@ unsafe
|
|||||||
// Console.WriteLine(vvv);
|
// Console.WriteLine(vvv);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace CsBindgen
|
||||||
|
{
|
||||||
|
public static unsafe partial class LibLz4
|
||||||
|
{
|
||||||
|
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-";
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user