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 @@ +
+ + reloaded Logo + + +

Reloaded Deploy Rust C Library to .NET Wrapper Action

+ +

+ + License + +

+
+ +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); + } +} + +/// +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 + internal static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) + { + if (libraryName == __DllName) + { + var dllName = __DllName; + var path = "runtimes/"; + var extension = ""; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + path += "win-"; + extension = ".dll"; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + path += "osx-"; + extension = ".dylib"; + dllName = "lib" + dllName; + } + else + { + path += "linux-"; + extension = ".so"; + dllName = "lib" + dllName; + } + + if (RuntimeInformation.ProcessArchitecture == Architecture.X86) + { + path += "x86"; + } + else if (RuntimeInformation.ProcessArchitecture == Architecture.X64) + { + path += "x64"; + } + else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm) + { + path += "arm"; + } + else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64) + { + path += "arm64"; + } + + path += "/native/" + dllName + extension; + try + { + return NativeLibrary.Load(Path.Combine(AppContext.BaseDirectory, path), assembly, searchPath); + } + catch (DllNotFoundException) + { + return NativeLibrary.Load(Path.Combine(AppContext.BaseDirectory, dllName + extension)); + } + } + + return IntPtr.Zero; + } +} +``` + +For more information, read the [full documentation for csbindgen][csbindgen]. + +2. Use the [Example Above](#an-example) to set up a workflow. + +## Typical Workflow + +Once set up, usage is very easy. In fact, it's automatic. + +1. You add 1 or more C exports to your Rust library. +2. When you next build, `csbindgen` will automatically update `NativeMethods.g.cs`. + +You're done. Everything else is automated. + +## Inputs + +| Input | Required | Default | Description | +| --------------------- | -------- | ----------------- | ------------------------------------------------------------------- | +| `targets` | Yes | | Comma-separated list of Rust target triples to include in the build | +| `artifact-prefix` | No | `C-Library-` | Prefix for the artifact names to download | +| `csharp-project-path` | No | `bindings/csharp` | Path to the C# project directory | +| `install-dotnet` | No | `false` | If true, installs the .NET SDK with specified version | +| `dotnet-version` | No | `8.0.x` | Version of the .NET SDK to install | +| `nuget-artifact-name` | No | `NuGet-Package` | Name for the NuGet artifact | + +## Supported Targets + +This action automatically injects the following target platforms: + +- Windows (x86_64, i686, aarch64) +- Linux (x86_64, i686, aarch64, armv7) +- macOS (x86_64, aarch64) +- Android (x86_64, aarch64, armv7, i686) +- iOS (aarch64, x86_64) + +Basically everything upstream .NET CoreCLR supports, and some hypothetical platforms. + +## Output + +The action generates a NuGet package containing the .NET library and the native binaries for the +specified targets. The NuGet package is uploaded as an artifact with the name specified by the +`nuget-artifact-name` input. + +## Contributing + +Contributions are welcome! If you encounter any issues or have suggestions for improvements, please +open an issue or submit a pull request in this repository. + +## License + +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. + +[rust-lightweight-library]: https://github.com/Reloaded-Project/devops-rust-lightweight-binary +[csbindgen]: https://github.com/Cysharp/csbindgen +[prs-rs]: https://github.com/Sewer56/prs-rs \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..729b4f0 --- /dev/null +++ b/action.yml @@ -0,0 +1,144 @@ +name: 'Build and Package .NET Library with Native Binaries' +description: 'Builds and packages a .NET library, incorporating pre-downloaded native binaries for specified targets.' +branding: + icon: 'package' + color: 'red' + +inputs: + targets: + description: 'Comma-separated list of Rust target triples to include in the build' + required: true + artifact-prefix: + description: 'Prefix for the artifact names to download' + required: false + default: 'C-Library-' + csharp-project-path: + description: 'Path to the C# project directory' + required: false + default: 'bindings/csharp' + install-dotnet: + description: 'If true, installs the .NET SDK with specified version' + required: false + default: 'false' + dotnet-version: + description: 'Version of the .NET SDK to install' + required: false + default: '8.0.x' + nuget-artifact-name: + description: 'Name for the NuGet artifact' + required: false + default: 'NuGet-Package' + +runs: + using: 'composite' + steps: + - name: Install .NET Core SDK + if: ${{ inputs.install-dotnet == 'true' }} + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ inputs.dotnet-version }} + + - name: Download Artifacts with Prefix + uses: actions/download-artifact@v4 + with: + pattern: ${{ inputs.artifact-prefix }}* + path: downloaded_artifacts/ + + - name: Organize Artifacts + shell: bash + run: | + tree "downloaded_artifacts/" + IFS=',' read -ra TARGETS <<< "${{ inputs.targets }}" + # Note: Some targets here will likely never be supported by .NET, but they + # are included for the sake of completeness. + for TARGET in "${TARGETS[@]}"; do + case "$TARGET" in + x86_64-unknown-linux-gnu) + RUNTIME_ID="linux-x64" + FILE_EXTENSION=".so" + ;; + i686-unknown-linux-gnu) + RUNTIME_ID="linux-x86" + FILE_EXTENSION=".so" + ;; + aarch64-unknown-linux-gnu) + RUNTIME_ID="linux-arm64" + FILE_EXTENSION=".so" + ;; + armv7-unknown-linux-gnueabihf) + RUNTIME_ID="linux-arm" + FILE_EXTENSION=".so" + ;; + x86_64-pc-windows-msvc) + RUNTIME_ID="win-x64" + FILE_EXTENSION=".dll" + ;; + i686-pc-windows-msvc) + RUNTIME_ID="win-x86" + FILE_EXTENSION=".dll" + ;; + aarch64-pc-windows-msvc) + RUNTIME_ID="win-arm64" + FILE_EXTENSION=".dll" + ;; + x86_64-apple-darwin) + RUNTIME_ID="osx-x64" + FILE_EXTENSION=".dylib" + ;; + aarch64-apple-darwin) + RUNTIME_ID="osx-arm64" + FILE_EXTENSION=".dylib" + ;; + x86_64-linux-android) + RUNTIME_ID="android-x64" + FILE_EXTENSION=".so" + ;; + aarch64-linux-android) + RUNTIME_ID="android-arm64" + FILE_EXTENSION=".so" + ;; + armv7-linux-androideabi) + RUNTIME_ID="android-arm" + FILE_EXTENSION=".so" + ;; + i686-linux-android) + RUNTIME_ID="android-x86" + FILE_EXTENSION=".so" + ;; + aarch64-apple-ios) + RUNTIME_ID="ios-arm64" + FILE_EXTENSION=".dylib" + ;; + x86_64-apple-ios) + RUNTIME_ID="ios-x64" + FILE_EXTENSION=".dylib" + ;; + esac + + # Create the target directory + mkdir -p "${{ inputs.csharp-project-path }}/runtimes/$RUNTIME_ID/native" + + # Move only the specified file types into the correct runtime directory + find downloaded_artifacts/ -type f -path "*/*$TARGET*/*$FILE_EXTENSION" -exec mv {} "${{ inputs.csharp-project-path }}/runtimes/$RUNTIME_ID/native/" \; + done + tree "${{ inputs.csharp-project-path }}/runtimes" + + - name: Build .NET Project + shell: bash + run: | + cd "${{ inputs.csharp-project-path }}" + dotnet build -c Release + + - name: Pack .NET Project + shell: bash + run: | + cd "${{ inputs.csharp-project-path }}" + dotnet pack -c Release --output nupkg/ + + - name: Upload NuGet Package + uses: actions/upload-artifact@v4 + with: + name: ${{ inputs.nuget-artifact-name }} + path: | + ${{ inputs.csharp-project-path }}/nupkg/*.nupkg + ${{ inputs.csharp-project-path }}/nupkg/*.snupkg \ No newline at end of file diff --git a/assets/reloaded-logo.png b/assets/reloaded-logo.png new file mode 100644 index 0000000..090fb3a Binary files /dev/null and b/assets/reloaded-logo.png differ