diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml new file mode 100644 index 0000000..db7152d --- /dev/null +++ b/.github/workflows/test-action.yml @@ -0,0 +1,105 @@ +name: Test Build and Package .NET Library with Native Binaries + +on: + schedule: + - cron: '0 0 * * *' # Run daily at midnight + workflow_dispatch: + push: + branches: [ main ] + paths: + - 'action.yml' + - '.github/workflows/test-action.yml' + tags: + - 'v1' + - 'v1-test' + pull_request: + branches: [ main ] + paths: + - 'action.yml' + - '.github/workflows/test-action.yml' + +jobs: + test-action: + strategy: + matrix: + include: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + - os: ubuntu-latest + target: i686-unknown-linux-gnu + - os: ubuntu-latest + target: aarch64-unknown-linux-gnu + - os: ubuntu-latest + target: armv7-unknown-linux-gnueabihf + - os: windows-latest + target: x86_64-pc-windows-msvc + - os: windows-latest + target: i686-pc-windows-msvc + - os: windows-latest + target: aarch64-pc-windows-msvc + - os: macos-latest + target: x86_64-apple-darwin + - os: macos-latest + target: aarch64-apple-darwin + - os: ubuntu-latest + target: x86_64-linux-android + - os: ubuntu-latest + target: aarch64-linux-android + - os: ubuntu-latest + target: armv7-linux-androideabi + - os: ubuntu-latest + target: i686-linux-android + - os: macos-latest + target: aarch64-apple-ios + - os: macos-latest + target: x86_64-apple-ios + runs-on: ${{ matrix.os }} + steps: + - name: Checkout Test Repository + uses: actions/checkout@v4 + with: + repository: Sewer56/prs-rs + ref: d08599ed5473616f57d57a0966939e1a5dbda9b4 + path: test-repo + + - name: Build C Library + uses: Reloaded-Project/devops-rust-lightweight-binary@v1 + with: + crate-name: prs-rs + target: ${{ matrix.target }} + features: c-exports + build-library: true + + - name: Build and Package .NET Library + uses: Reloaded-Project/devops-rust-c-library-to-dotnet@v1 + with: + targets: ${{ matrix.target }} + csharp-project-path: test-repo/bindings/csharp + install-dotnet: true + + - name: Upload NuGet Package + uses: actions/upload-artifact@v4 + with: + name: NuGet-Package-${{ matrix.target }} + path: | + test-repo/bindings/csharp/nupkg/*.nupkg + test-repo/bindings/csharp/nupkg/*.snupkg + + verify-nuget-packages: + needs: test-action + runs-on: ubuntu-latest + steps: + - name: Download NuGet Packages + uses: actions/download-artifact@v4 + with: + name: NuGet-Package-* + path: nuget-packages + + - name: Assert NuGet Packages Exist + shell: bash + run: | + if [ -z "$(ls -A nuget-packages)" ]; then + echo "No NuGet packages found in the 'nuget-packages' directory" + exit 1 + fi + echo "NuGet packages exist in the 'nuget-packages' directory" \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a91fae5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2023 Sewer56 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..7643118 --- /dev/null +++ b/README.MD @@ -0,0 +1,290 @@ +
+ +The `devops-rust-c-library-to-dotnet` GitHub Action builds and packages a .NET library that +acts as a wrapper for a Rust library with C exports. + +It works by taking the artifact outputs of [devops-rust-lightweight-binary@v1][rust-lightweight-library] +action (usually named `C-Library-*`), and extracting the native (`.dll`, `.so`, `.dylib`) binaries +from them. + +These are then placed in appropriate `runtime` folders in the .NET library project, which is then +built and packed into a NuGet package. + +## An Example + +Suppose you run the following step in your GitHub workflow: + +```yaml +test-library-build: + strategy: + matrix: + include: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + use-pgo: true + use-cross: false + - os: ubuntu-latest + target: i686-unknown-linux-gnu + use-pgo: true + use-cross: false + - os: ubuntu-latest + target: aarch64-unknown-linux-gnu + use-pgo: true # x64 host to aarch64 simulated guest via cross + use-cross: true + - os: ubuntu-latest + target: armv7-unknown-linux-gnueabihf + use-pgo: true # x64 host to armv7 simulated guest via cross + use-cross: true + - os: windows-latest + target: x86_64-pc-windows-msvc + use-pgo: true + use-cross: false + - os: windows-latest + target: i686-pc-windows-msvc + use-pgo: true + use-cross: false + - os: windows-latest + target: aarch64-pc-windows-msvc + use-pgo: false # no virtualization support (proprietary OS) + use-cross: false + - os: macos-13 # x86 + target: x86_64-apple-darwin + use-pgo: true + use-cross: false + - os: macos-14 # M1 + target: aarch64-apple-darwin + use-pgo: true + use-cross: false + runs-on: ${{ matrix.os }} + steps: + - name: Checkout Test Repository + uses: actions/checkout@v4 + with: + repository: Sewer56/prs-rs + ref: d08599ed5473616f57d57a0966939e1a5dbda9b4 + - name: Test Library Build + uses: Reloaded-Project/devops-rust-lightweight-binary@v1-test + with: + crate-name: prs-rs + target: ${{ matrix.target }} + use-pgo: ${{ matrix.use-pgo }} + use-cross: ${{ matrix.use-cross }} + features: "c-exports" + build-library: true + upload-artifacts: false +``` + +This will produce the following artifacts: + +- `C-Library-aarch64-apple-darwin-c-exports.zip` +- `C-Library-aarch64-pc-windows-msvc-c-exports.zip` +- `C-Library-aarch64-unknown-linux-gnu-c-exports.zip` +- `C-Library-armv7-unknown-linux-gnueabihf-c-exports.zip` +- `C-Library-i686-unknown-linux-gnu-c-exports.zip` +- `C-Library-i686-pc-windows-msvc-c-exports.zip` +- `C-Library-x86_64-apple-darwin-c-exports.zip` +- `C-Library-x86_64-pc-windows-msvc-c-exports.zip` +- `C-Library-x86_64-unknown-linux-gnu-c-exports.zip` + + +Running this action with the following configuration: + +```yaml +- name: Build and Package .NET Library + uses: Reloaded-Project/devops-rust-c-library-to-dotnet@v1 + with: + targets: x86_64-unknown-linux-gnu + csharp-project-path: bindings/csharp + install-dotnet: true +``` + +Will perform the following. + +1. Install .NET, if needed. +2. Download artifacts and insert them into appropriate subdirectories of `bindings/csharp` + +``` +bindings/csharp/ +├── MyLibrary.csproj +├── runtimes/ +│ ├── linux-x64/ +│ │ └── native/ +│ │ └── libmylibrary.so +│ ├── win-x64/ +│ │ └── native/ +│ │ └── mylibrary.dll +│ └── osx-x64/ +│ └── native/ +│ └── libmylibrary.dylib +└── // Other C# files and directories +``` + +3. Build & Pack the Project into a NuGet package. +4. Upload the NuGet package as an artifact, with name specified in `nuget-artifact-name`. + +## Setting up a new Repository + +This action was made to be used in conjunction with the excellent [Cysharp/csbindgen][csbindgen] +binding generator by Yoshifumi Kawai (neuecc). + +Typical usage is as follows, [prs-rs][prs-rs] will be used as an example. + +1. Set up `csbindgen` to generate C# bindings for your Rust library. + +```rust +// build.rs in Rust project +fn main() { + csbindgen::Builder::default() + .input_extern_file("src/exports.rs") + .csharp_dll_name("prs_rs") + .csharp_class_accessibility("public") + .csharp_namespace("prs_rs.Net.Sys") + .generate_csharp_file("bindings/csharp/NativeMethods.g.cs") + .unwrap(); +} +``` + +```csharp +// Ensure DLL is loaded when requested. +class Init +{ + [ModuleInitializer] + internal static void RegisterImportResolver() + { + // .NET Core 3.X and above only!! + NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, NativeMethods.DllImportResolver); + } +} + +///