Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,17 @@ jobs:
include:
- runs-on: ubuntu-latest
architecture: x64
framework: net6.0
- runs-on: macos-latest
architecture: x64
framework: net6.0
- runs-on: windows-latest
architecture: x64
framework: net6.0
- runs-on: windows-latest
architecture: x86
- runs-on: macos-latest
architecture: x64
framework: net48

runs-on: ${{ matrix.runs-on }}
needs: [dotnet]
steps:
Expand All @@ -226,7 +231,7 @@ jobs:
with:
name: nupkgs
path: dist/.organize/nupkgs
- run: ./build.cmd DotNetTest --architecture ${{ matrix.architecture }}
- run: ./build.cmd DotNetTest --architecture ${{ matrix.architecture }} --framework ${{ matrix.framework }}
- uses: actions/upload-artifact@v3
with:
name: DotNet Test Results
Expand Down
4 changes: 4 additions & 0 deletions build/Build.DotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,18 @@ void DotNetWriteVersionInfoProps()
(RootDirectory / "lib" / "dotnet" / "Version.props").WriteAllText(props);
}

[Parameter] string Framework;

public Target DotNetTest => _ => _
.DependsOn(PrepareGitHubArtifacts)
.Requires(() => Framework)
.Executes(() =>
{
DotNetTasks.DotNetRun(_ => _
.SetProcessWorkingDirectory(RootDirectory / "test" / "dotnet" / "AlphaSkia.Test")
.SetRuntime(TargetOperatingSystem.Current.DotNetRid + "-" +
(Architecture ?? Architecture.Current))
.SetFramework(Framework)
.AddProcessEnvironmentVariable("NUGET_PACKAGES", TemporaryDirectory / "packages")
);
});
Expand Down
3 changes: 2 additions & 1 deletion lib/dotnet/AlphaSkia/AlphaSkia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

<PropertyGroup>
<IsPackable>true</IsPackable>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageReadmeFile>README.md</PackageReadmeFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>10</LangVersion>
<Description>$(ProductDescription)</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
Expand Down
45 changes: 38 additions & 7 deletions lib/dotnet/AlphaSkia/AlphaSkiaPlatform.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.InteropServices;

namespace AlphaSkia;

Expand Down Expand Up @@ -40,7 +41,7 @@ private static bool TryLoadManually()
NativeMethods.AlphaSkiaNativeLibName + libExtension);
if (File.Exists(alphaSkiaPath))
{
if (NativeLibrary.TryLoad(alphaSkiaPath, out _) && CheckNativeLib())
if (TryLoadLibrary(alphaSkiaPath) && CheckNativeLib())
{
return true;
}
Expand All @@ -54,12 +55,42 @@ private static bool TryLoadManually()
return false;
}

private static bool TryLoadLibrary(string path)
{
// on .net FX or some .netstandard environments the NativeLibrary might not be available
// try to use it if available, otherwise fallback.
var nativeLibraryTryLoadMethod = typeof(DllImportAttribute).Assembly
.GetType("System.Runtime.InteropServices.NativeLibrary")
?.GetMethod("TryLoad", BindingFlags.Static | BindingFlags.Public);
if (nativeLibraryTryLoadMethod != null)
{
return (bool)nativeLibraryTryLoadMethod.Invoke(null, new object[]
{
path, IntPtr.Zero
})!;
}

var handle = LoadLibraryWindows(path);
return handle != IntPtr.Zero;
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "LoadLibraryW",
ExactSpelling = true)]
private static extern nint LoadLibraryWindows(string fileName);

private static string FallbackRuntimeIdentifier =>
#if NETSTANDARD2_0
"windows-" + RuntimeInformation.OSArchitecture.ToString().ToLowerInvariant();
#else
RuntimeInformation.RuntimeIdentifier;
#endif

private static string BuildKnownRid(out string libExtension)
{
var officialRid = RuntimeInformation.RuntimeIdentifier;
var officialRid = FallbackRuntimeIdentifier;
var separator = officialRid.IndexOf('-');
var officialOs = officialRid[..separator];
var officialArch = officialRid[(separator + 1)..];
var officialOs = officialRid.Substring(0, separator);
var officialArch = officialRid.Substring(separator + 1);

libExtension = Environment.OSVersion.Platform switch
{
Expand All @@ -68,7 +99,7 @@ private static string BuildKnownRid(out string libExtension)
PlatformID.MacOSX => ".dylib",
_ => ""
};

var os = Environment.OSVersion.Platform switch
{
PlatformID.Unix => "linux",
Expand All @@ -78,7 +109,7 @@ private static string BuildKnownRid(out string libExtension)
};
if (string.IsNullOrEmpty(os))
{
return RuntimeInformation.RuntimeIdentifier;
return FallbackRuntimeIdentifier;
}

var arch = RuntimeInformation.OSArchitecture switch
Expand Down
Loading