-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wasm] Add support for using custom native libraries (#55797)
- Loading branch information
Showing
14 changed files
with
234 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/tests/BuildWasmApps/Wasm.Build.Tests/NativeLibraryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
#nullable enable | ||
|
||
namespace Wasm.Build.Tests | ||
{ | ||
public class NativeLibraryTests : BuildTestBase | ||
{ | ||
public NativeLibraryTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) | ||
: base(output, buildContext) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[BuildAndRun(aot: false)] | ||
[BuildAndRun(aot: true)] | ||
public void ProjectWithNativeReference(BuildArgs buildArgs, RunHost host, string id) | ||
{ | ||
string projectName = $"AppUsingNativeLib-a"; | ||
buildArgs = buildArgs with { ProjectName = projectName }; | ||
buildArgs = ExpandBuildArgs(buildArgs, extraItems: "<NativeFileReference Include=\"native-lib.o\" />"); | ||
|
||
if (!_buildContext.TryGetBuildFor(buildArgs, out BuildProduct? _)) | ||
{ | ||
InitPaths(id); | ||
if (Directory.Exists(_projectDir)) | ||
Directory.Delete(_projectDir, recursive: true); | ||
|
||
Utils.DirectoryCopy(Path.Combine(BuildEnvironment.TestAssetsPath, "AppUsingNativeLib"), _projectDir); | ||
File.Copy(Path.Combine(BuildEnvironment.TestAssetsPath, "native-libs", "native-lib.o"), Path.Combine(_projectDir, "native-lib.o")); | ||
} | ||
|
||
BuildProject(buildArgs, | ||
dotnetWasmFromRuntimePack: false, | ||
id: id); | ||
|
||
string output = RunAndTestWasmApp(buildArgs, buildDir: _projectDir, expectedExitCode: 0, | ||
test: output => {}, | ||
host: host, id: id); | ||
|
||
Assert.Contains("print_line: 100", output); | ||
Assert.Contains("from pinvoke: 142", output); | ||
} | ||
|
||
[Theory] | ||
[BuildAndRun(aot: false)] | ||
[BuildAndRun(aot: true)] | ||
public void ProjectUsingSkiaSharp(BuildArgs buildArgs, RunHost host, string id) | ||
{ | ||
string projectName = $"AppUsingSkiaSharp"; | ||
buildArgs = buildArgs with { ProjectName = projectName }; | ||
buildArgs = ExpandBuildArgs(buildArgs, | ||
extraItems: @$" | ||
<PackageReference Include=""SkiaSharp"" Version=""2.80.3"" /> | ||
<PackageReference Include=""SkiaSharp.NativeAssets.WebAssembly"" Version=""2.80.3"" /> | ||
<NativeFileReference Include=""$(SkiaSharpStaticLibraryPath)\2.0.9\*.a"" /> | ||
<WasmFilesToIncludeInFileSystem Include=""{Path.Combine(BuildEnvironment.TestAssetsPath, "mono.png")}"" /> | ||
"); | ||
|
||
string programText = @" | ||
using System; | ||
using SkiaSharp; | ||
public class Test | ||
{ | ||
public static int Main() | ||
{ | ||
using SKFileStream skfs = new SKFileStream(""mono.png""); | ||
using SKImage img = SKImage.FromEncodedData(skfs); | ||
Console.WriteLine ($""Size: {skfs.Length} Height: {img.Height}, Width: {img.Width}""); | ||
return 0; | ||
} | ||
}"; | ||
|
||
BuildProject(buildArgs, | ||
initProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText), | ||
dotnetWasmFromRuntimePack: false, | ||
id: id); | ||
|
||
string output = RunAndTestWasmApp(buildArgs, buildDir: _projectDir, expectedExitCode: 0, | ||
test: output => {}, | ||
host: host, id: id, | ||
args: "mono.png"); | ||
|
||
Assert.Contains("Size: 26462 Height: 599, Width: 499", output); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.