Improve ckzg native library search path + small improvements (#372)

This commit is contained in:
Alexey 2023-10-04 15:03:37 +03:00 committed by GitHub
parent fbef59a3f9
commit b2e41491ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 25 deletions

View File

@ -30,17 +30,16 @@ jobs:
host: ubuntu-22.04 host: ubuntu-22.04
ext: .so ext: .so
reqs: sudo apt update && sudo apt install -y clang binutils-aarch64-linux-gnu libc6-arm64-cross libc6-dev-arm64-cross crossbuild-essential-arm64 reqs: sudo apt update && sudo apt install -y clang binutils-aarch64-linux-gnu libc6-arm64-cross libc6-dev-arm64-cross crossbuild-essential-arm64
- arch: arm64-darwin - arch: arm64-apple-macos11
location: osx-arm64 location: osx-arm64
host: macos-latest host: macos-latest
ext: .so ext: .dylib
reqs: reqs:
- arch: x86_64-darwin - arch: x86_64-darwin
location: osx-x64 location: osx-x64
host: macos-latest host: macos-latest
ext: .so ext: .dylib
reqs: reqs:
#TODO: support arch: x86_64-v2-win
- arch: - arch:
location: win-x64 location: win-x64
host: windows-latest host: windows-latest

2
.gitignore vendored
View File

@ -13,8 +13,6 @@ analysis-report/
.idea/ .idea/
*bindings/*/*.so *bindings/*/*.so
*bindings/rust/target *bindings/rust/target
*bindings/csharp/*.exe
*bindings/csharp/*.dll
__pycache__ __pycache__
.DS_Store .DS_Store

View File

@ -10,3 +10,5 @@ Ckzg.Bindings/runtimes/*/native/*
test_ckzg* test_ckzg*
*.so *.so
*.dll *.dll
*.exe
*.dylib

View File

@ -1,3 +1,4 @@
using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Loader; using System.Runtime.Loader;
@ -5,20 +6,31 @@ namespace Ckzg;
public static partial class Ckzg public static partial class Ckzg
{ {
static Ckzg() static Ckzg() => AssemblyLoadContext.Default.ResolvingUnmanagedDll += LoadNativeLibrary;
private static IntPtr LoadNativeLibrary(Assembly _, string path)
{ {
AssemblyLoadContext.Default.ResolvingUnmanagedDll += (_, path) => if (!path.Equals("ckzg", StringComparison.OrdinalIgnoreCase))
path.Contains("ckzg", StringComparison.OrdinalIgnoreCase) {
? NativeLibrary.Load($"runtimes/{( return IntPtr.Zero;
}
string platform =
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "")}-{RuntimeInformation.ProcessArchitecture switch RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "";
string arch = RuntimeInformation.ProcessArchitecture switch
{ {
Architecture.X64 => "x64", Architecture.X64 => "x64",
Architecture.Arm64 => "arm64", Architecture.Arm64 => "arm64",
_ => "" _ => "",
}}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}") };
: IntPtr.Zero; string extension =
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "so" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "dylib" :
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "";
return NativeLibrary.Load(Path.Combine(AppContext.BaseDirectory, $"runtimes/{platform}-{arch}/native/{path}.{extension}"));
} }
[DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")] [DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")]

View File

@ -32,10 +32,12 @@
<ItemGroup> <ItemGroup>
<Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/win-x64/native/ckzg.dll')" Include="runtimes/win-x64/native/ckzg.dll" Pack="true" PackagePath="runtimes/win-x64/native/ckzg.dll" /> <Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/win-x64/native/ckzg.dll')" Include="runtimes/win-x64/native/ckzg.dll" Pack="true" PackagePath="runtimes/win-x64/native/ckzg.dll" />
<Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/linux-x64/native/ckzg.so')" Include="runtimes/linux-x64/native/ckzg.so" Pack="true" PackagePath="runtimes/linux-x64/native/ckzg.so" /> <Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/linux-x64/native/ckzg.so')" Include="runtimes/linux-x64/native/ckzg.so" Pack="true" PackagePath="runtimes/linux-x64/native/ckzg.so" />
<Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/linux-arm64/native/ckzg.so')" Include="runtimes/linux-arm64/native/ckzg.so" Pack="true" PackagePath="runtimes/linux-arm64/native/ckzg.so" /> <Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/linux-arm64/native/ckzg.so')" Include="runtimes/linux-arm64/native/ckzg.so" Pack="true" PackagePath="runtimes/linux-arm64/native/ckzg.so" />
<Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/osx-x64/native/ckzg.so')" Include="runtimes/osx-x64/native/ckzg.so" Pack="true" PackagePath="runtimes/osx-x64/native/ckzg.so" />
<Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/osx-arm64/native/ckzg.so')" Include="runtimes/osx-arm64/native/ckzg.so" Pack="true" PackagePath="runtimes/osx-arm64/native/ckzg.so" /> <Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/osx-x64/native/ckzg.dylib')" Include="runtimes/osx-x64/native/ckzg.dylib" Pack="true" PackagePath="runtimes/osx-x64/native/ckzg.dylib" />
<Content CopyToOutputDirectory="PreserveNewest" Condition="Exists('runtimes/osx-arm64/native/ckzg.dylib')" Include="runtimes/osx-arm64/native/ckzg.dylib" Pack="true" PackagePath="runtimes/osx-arm64/native/ckzg.dylib" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -7,7 +7,8 @@ ifeq ($(OS),Windows_NT)
BLST_OBJ = blst.lib BLST_OBJ = blst.lib
LOCATION ?= win-x64 LOCATION ?= win-x64
CLANG_EXECUTABLE = clang CLANG_EXECUTABLE = clang
CKZG_LIBRARY_PATH = Ckzg.Bindings\runtimes\$(LOCATION)\native\ckzg.dll EXTENSION ?= ".dll"
CKZG_LIBRARY_PATH = Ckzg.Bindings\runtimes\$(LOCATION)\native\ckzg$(EXTENSION)
CFLAGS += -Wl,/def:ckzg.def CFLAGS += -Wl,/def:ckzg.def
else else
BLST_BUILDSCRIPT = ./build.sh BLST_BUILDSCRIPT = ./build.sh
@ -18,6 +19,7 @@ else
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m) UNAME_M := $(shell uname -m)
ifeq ($(UNAME_S),Linux) ifeq ($(UNAME_S),Linux)
EXTENSION ?= ".so"
ifeq ($(UNAME_M),x86_64) ifeq ($(UNAME_M),x86_64)
LOCATION ?= linux-x64 LOCATION ?= linux-x64
else else
@ -25,6 +27,7 @@ else
endif endif
endif endif
ifeq ($(UNAME_S),Darwin) ifeq ($(UNAME_S),Darwin)
EXTENSION ?= ".dylib"
ifeq ($(UNAME_M),arm64) ifeq ($(UNAME_M),arm64)
LOCATION ?= osx-arm64 LOCATION ?= osx-arm64
else else
@ -32,7 +35,7 @@ else
endif endif
endif endif
CKZG_LIBRARY_PATH = Ckzg.Bindings/runtimes/$(LOCATION)/native/ckzg.so CKZG_LIBRARY_PATH = Ckzg.Bindings/runtimes/$(LOCATION)/native/ckzg$(EXTENSION)
endif endif
FIELD_ELEMENTS_PER_BLOB ?= 4096 FIELD_ELEMENTS_PER_BLOB ?= 4096

View File

@ -4,8 +4,9 @@ This directory contains C# bindings for the C-KZG-4844 library.
## Prerequisites ## Prerequisites
These bindings require .NET 6.0 (not 7.0). This can be found here: Build requires:
* https://dotnet.microsoft.com/en-us/download/dotnet/6.0 - `clang` as a preferred build tool for the native wrapper of ckzg. On Windows, it's tested with clang from [Microsoft Visual Studio components](https://learn.microsoft.com/en-us/cpp/build/clang-support-msbuild?view=msvc-170);
- [.NET SDK](https://dotnet.microsoft.com/en-us/download) to build the bindings.
## Build & test ## Build & test