Skip to content

Commit 7ab57a9

Browse files
committed
feat: native library support and testing
- GitHub Actions tag triggers, NuGet publish job - NativesHelper, NativeLibraryVersionAttribute - vcpkg integration, static linking - zstd support - native library tests, import checks - Native AOT test app - NetCordNativesDir assembly metadata
1 parent 67716e5 commit 7ab57a9

13 files changed

Lines changed: 562 additions & 194 deletions

.github/workflows/build-natives.yml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ on:
55
push:
66
paths:
77
- 'NetCord.Natives/**'
8+
- '.github/workflows/build-natives.yml'
9+
tags:
10+
- "[0-9]+.[0-9]+.[0-9]+"
11+
- "[0-9]+.[0-9]+.[0-9]+-*"
812

913
jobs:
1014
build-natives:
@@ -57,11 +61,6 @@ jobs:
5761
vcpkg-${{ runner.os }}-${{ matrix.rid }}-
5862
vcpkg-${{ runner.os }}-
5963
60-
- name: Setup .NET
61-
uses: actions/setup-dotnet@v4
62-
with:
63-
dotnet-version: 8.0.x
64-
6564
- name: Resolve vcpkg from runner image
6665
shell: pwsh
6766
run: |
@@ -116,21 +115,6 @@ jobs:
116115
path: artifacts
117116
merge-multiple: false
118117

119-
- name: Setup .NET
120-
uses: actions/setup-dotnet@v4
121-
with:
122-
dotnet-version: 8.0.x
123-
124-
- name: Resolve native license root
125-
shell: pwsh
126-
run: |
127-
$licenseRoot = Get-ChildItem artifacts -Directory | Sort-Object Name | Select-Object -First 1
128-
if (-not $licenseRoot) {
129-
throw "No native artifact directories were downloaded."
130-
}
131-
132-
"NativeLicenseRoot=$($licenseRoot.FullName)" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
133-
134118
- name: Pack combined nupkg
135119
shell: pwsh
136120
run: |
@@ -141,3 +125,20 @@ jobs:
141125
with:
142126
name: NetCord.Natives.Package
143127
path: artifacts/package/*.nupkg
128+
129+
publish:
130+
needs: pack-natives
131+
if: startsWith(github.ref, 'refs/tags/')
132+
runs-on: ubuntu-latest
133+
steps:
134+
- name: Download nupkg
135+
uses: actions/download-artifact@v4
136+
with:
137+
name: NetCord.Natives.Package
138+
path: artifacts/package
139+
140+
- name: Publish Package
141+
env:
142+
KEY: ${{ secrets.NUGET_API_KEY }}
143+
run: |
144+
dotnet nuget push artifacts/package/*.nupkg -k $KEY -s https://api.nuget.org/v3/index.json --skip-duplicate

NetCord.Natives/NativesHelper.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
5+
namespace NetCord.Natives;
6+
7+
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
8+
public class NativeLibraryVersionAttribute(string name, string version) : Attribute
9+
{
10+
public string Name { get; } = name;
11+
public string Version { get; } = version;
12+
}
13+
14+
public static class NativesHelper
15+
{
16+
public static IEnumerable<NativeLibraryVersionAttribute> GetNativeLibraryVersions()
17+
{
18+
return typeof(NativesHelper).Assembly.GetCustomAttributes<NativeLibraryVersionAttribute>();
19+
}
20+
}

NetCord.Natives/NetCord.Natives.csproj

Lines changed: 218 additions & 171 deletions
Large diffs are not rendered by default.

NetCord.Natives/NetCord.Natives.local.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
<Target Name="ProcessNetCordNativeAot_Local" BeforeTargets="ProcessNetCordNativeAot">
66

7-
<MSBuild Projects="$(MSBuildThisFileDirectory)\NetCord.Natives.csproj" Targets="GetStaticBuildDir">
7+
<MSBuild Projects="$(MSBuildThisFileDirectory)\NetCord.Natives.csproj"
8+
Targets="GetStaticBuildDir">
89
<Output TaskParameter="TargetOutputs" ItemName="_NetCordStaticDir" />
910
</MSBuild>
1011

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22

33
<PropertyGroup>
4-
<NetCordNativesStaticDir Condition="'$(NetCordNativesStaticDir)' == ''">$(MSBuildThisFileDirectory)..\runtimes-static\$(RuntimeIdentifier)</NetCordNativesStaticDir>
4+
<NetCordNativesStaticDir Condition="'$(NetCordNativesStaticDir)' == ''">$(MSBuildThisFileDirectory)..\staticlibs\$(RuntimeIdentifier)</NetCordNativesStaticDir>
55
</PropertyGroup>
66

77
</Project>

NetCord.Natives/NetCord.Natives.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Condition="'%(DirectPInvoke.Identity)' == 'libsodium'" />
1010
<_NetCordStaticLink Include="opus"
1111
Condition="'%(DirectPInvoke.Identity)' == 'opus'" />
12+
<_NetCordStaticLink Include="zstd"
13+
Condition="'%(DirectPInvoke.Identity)' == 'zstd'" />
1214

1315
<NativeLibToLink Include="$(NetCordNativesStaticDir)\%(_NetCordStaticLink.Identity).*" />
1416

NetCord.Natives/vcpkg.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"dependencies": [
33
"libdave",
44
"opus",
5-
"libsodium"
5+
"libsodium",
6+
"zstd"
67
],
78
"vcpkg-configuration": {
89
"overlay-ports": [

NetCord.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<File Path="Tests/Directory.Build.targets" />
8585
<Project Path="Tests/ColorTest/ColorTest.csproj" />
8686
<Project Path="Tests/MentionTest/MentionTest.csproj" />
87+
<Project Path="Tests/NetCord.Natives.Tests/NetCord.Natives.Tests.csproj" />
8788
<Project Path="Tests/NetCord.Rest.Tests/NetCord.Rest.Tests.csproj" />
8889
<Project Path="Tests/NetCord.Test.Hosting.AspNetCore/NetCord.Test.Hosting.AspNetCore.csproj" />
8990
<Project Path="Tests/NetCord.Test.Hosting/NetCord.Test.Hosting.csproj" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
9+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
<PublishAot>true</PublishAot>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<!-- <ProjectReference Include="..\..\NetCord.Natives\NetCord.Natives.csproj" /> -->
15+
<ProjectReference Include="$(NetCordNativesDir)\NetCord.Natives.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<DirectPInvoke Include="$(NetCordDirectPInvoke)" />
20+
</ItemGroup>
21+
<!-- <Import Project="..\NetCord\NetCord.Natives\NetCord.Natives.local.targets"/> -->
22+
<Import Project="$(NetCordNativesDir)\NetCord.Natives.local.targets" />
23+
24+
</Project>

0 commit comments

Comments
 (0)