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
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
- arch: arm64-darwin
- arch: arm64-apple-macos11
location: osx-arm64
host: macos-latest
ext: .so
ext: .dylib
reqs:
- arch: x86_64-darwin
location: osx-x64
host: macos-latest
ext: .so
ext: .dylib
reqs:
#TODO: support arch: x86_64-v2-win
- arch:
location: win-x64
host: windows-latest

2
.gitignore vendored
View File

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

View File

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

View File

@ -1,3 +1,4 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
@ -5,20 +6,31 @@ namespace 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) =>
path.Contains("ckzg", StringComparison.OrdinalIgnoreCase)
? NativeLibrary.Load($"runtimes/{(
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "")}-{RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "x64",
Architecture.Arm64 => "arm64",
_ => ""
}}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}")
: IntPtr.Zero;
if (!path.Equals("ckzg", StringComparison.OrdinalIgnoreCase))
{
return IntPtr.Zero;
}
string platform =
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "";
string arch = RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "x64",
Architecture.Arm64 => "arm64",
_ => "",
};
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")]

View File

@ -32,10 +32,12 @@
<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/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/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>

View File

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

View File

@ -4,8 +4,9 @@ This directory contains C# bindings for the C-KZG-4844 library.
## Prerequisites
These bindings require .NET 6.0 (not 7.0). This can be found here:
* https://dotnet.microsoft.com/en-us/download/dotnet/6.0
Build requires:
- `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