From b2e41491ad1859f1792964a2432a419b64dc6fb2 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 4 Oct 2023 15:03:37 +0300 Subject: [PATCH] Improve ckzg native library search path + small improvements (#372) --- .github/workflows/csharp-tests.yml | 7 ++-- .gitignore | 2 - bindings/csharp/.gitignore | 2 + .../csharp/Ckzg.Bindings/Ckzg.Bindings.cs | 38 ++++++++++++------- .../csharp/Ckzg.Bindings/Ckzg.Bindings.csproj | 6 ++- bindings/csharp/Makefile | 7 +++- bindings/csharp/README.md | 5 ++- 7 files changed, 42 insertions(+), 25 deletions(-) diff --git a/.github/workflows/csharp-tests.yml b/.github/workflows/csharp-tests.yml index a7fb6ea..f660fb7 100644 --- a/.github/workflows/csharp-tests.yml +++ b/.github/workflows/csharp-tests.yml @@ -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 diff --git a/.gitignore b/.gitignore index 893a3d4..adac936 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,6 @@ analysis-report/ .idea/ *bindings/*/*.so *bindings/rust/target -*bindings/csharp/*.exe -*bindings/csharp/*.dll __pycache__ .DS_Store diff --git a/bindings/csharp/.gitignore b/bindings/csharp/.gitignore index 78ccd1d..6db6934 100644 --- a/bindings/csharp/.gitignore +++ b/bindings/csharp/.gitignore @@ -10,3 +10,5 @@ Ckzg.Bindings/runtimes/*/native/* test_ckzg* *.so *.dll +*.exe +*.dylib diff --git a/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.cs b/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.cs index 9013b37..895c309 100644 --- a/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.cs +++ b/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.cs @@ -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")] diff --git a/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.csproj b/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.csproj index a0d899c..586b715 100644 --- a/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.csproj +++ b/bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.csproj @@ -32,10 +32,12 @@ + - - + + + diff --git a/bindings/csharp/Makefile b/bindings/csharp/Makefile index accbcfc..84651a5 100644 --- a/bindings/csharp/Makefile +++ b/bindings/csharp/Makefile @@ -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 diff --git a/bindings/csharp/README.md b/bindings/csharp/README.md index b3d9c3b..fae605f 100644 --- a/bindings/csharp/README.md +++ b/bindings/csharp/README.md @@ -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