Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WASM Support #241

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
135 changes: 105 additions & 30 deletions Ooui.Wasm.Build.Tasks/BuildDistTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.Net;
using System.Text;

using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.Cecil;
Expand All @@ -16,9 +16,15 @@ namespace Ooui.Wasm.Build.Tasks
{
public class BuildDistTask : Task
{
const string SdkUrl = "https://xamjenkinsartifact.azureedge.net/test-mono-mainline-wasm/916/ubuntu-1804-amd64/sdks/wasm/mono-wasm-f25f9e5f2b5.zip";
const string SdkUrl = "https://jenkins.mono-project.com/job/test-mono-mainline-wasm/label=ubuntu-1804-amd64/lastSuccessfulBuild/Azure/processDownloadRequest/4942/ubuntu-1804-amd64/sdks/wasm/mono-wasm-eb468076a79.zip";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


readonly (string Name, string Version)[] NugetPackages = {
("WebAssembly.Bindings", "3.0.2"),
("WebAssembly.Net.Http", "3.0.2"),
("WebAssembly.Net.WebSockets", "3.0.2"),
};

const string AssemblyExtension = ".bin";
const string AssemblyExtension = ".clr";

[Required]
public string Assembly { get; set; }
Expand All @@ -33,6 +39,7 @@ public override bool Execute ()
try {
ok = true;
InstallSdk ();
InstallNugets ();
GetBcl ();
CreateDist ();
DeleteOldAssemblies ();
Expand All @@ -52,16 +59,18 @@ public override bool Execute ()
}

string sdkPath;
string bclPath;

void InstallSdk ()
{
var sdkName = Path.GetFileNameWithoutExtension (new Uri (SdkUrl).AbsolutePath.Replace ('/', Path.DirectorySeparatorChar));
Log.LogMessage ("SDK: " + sdkName);
string tmpDir = Path.GetTempPath ();
sdkPath = Path.Combine (tmpDir, sdkName);
bclPath = Path.Combine (sdkPath, "wasm-bcl", "wasm");
Log.LogMessage ("SDK Path: " + sdkPath);
if (Directory.Exists (sdkPath)
&& Directory.Exists (Path.Combine (sdkPath, "release")))
&& Directory.Exists (Path.Combine (sdkPath, "builds", "release")))
return;

var client = new WebClient ();
Expand All @@ -79,15 +88,54 @@ void InstallSdk ()
Log.LogMessage ($"Extracted {sdkName} to {sdkPath}");
}

string bclPath;
void InstallNugets ()
{
foreach (var p in NugetPackages) {
InstallNuget (p.Name);
}
}

void InstallNuget (string packageName)
{
var asmFileName = packageName + ".dll";
var destPath = Path.Combine (bclPath, asmFileName);
if (File.Exists (destPath))
return;

Console.WriteLine ("INSTALLING NUGET " + packageName);

string tmpDir = Path.GetTempPath ();

var packagesPath = Path.Combine (sdkPath, "packages");

Console.WriteLine ($"PP {packagesPath}");

var allfiles = Directory.GetFiles (packagesPath, "*.nupkg");
foreach (var f in allfiles) {
Console.WriteLine (f);
}

var zipPath = Directory.GetFiles(packagesPath, "*" + packageName + "*.nupkg").First ();
Console.WriteLine ("NUPKG!!! " + zipPath);

using var zipFile = ZipFile.OpenRead (zipPath);
var zipEntry = zipFile.Entries.First (x =>
x.FullName.EndsWith (".dll", StringComparison.InvariantCultureIgnoreCase) &&
x.FullName.StartsWith ("lib/", StringComparison.InvariantCultureIgnoreCase));
zipEntry.ExtractToFile (destPath);
Console.WriteLine ($"INStALL {destPath}");
}

Dictionary<string, string> bclAssemblies;

void GetBcl ()
{
bclPath = Path.Combine (sdkPath, "bcl");
var reals = Directory.GetFiles (bclPath, "*.dll");
Console.WriteLine ($"{reals.Length} reals found");
var facades = Directory.GetFiles (Path.Combine (bclPath, "Facades"), "*.dll");
Console.WriteLine ($"{facades.Length} facades found");
var allFiles = reals.Concat (facades);

bclAssemblies = allFiles.ToDictionary (x => Path.GetFileName (x));
}

Expand All @@ -114,8 +162,8 @@ void DeleteOldAssemblies ()

void CopyRuntime ()
{
var rtPath = Path.Combine (sdkPath, "release");
var files = new[] { "mono.wasm", "mono.js" };
var rtPath = Path.Combine (sdkPath, "builds", "release");
var files = new[] { "dotnet.wasm", "dotnet.js" };
foreach (var f in files) {
var src = Path.Combine (rtPath, f);
var dest = Path.Combine (distPath, f);
Expand All @@ -135,14 +183,15 @@ void LinkAssemblies ()
foreach (var r in references) {
var name = Path.GetFileName (r);
if (bclAssemblies.ContainsKey (name)) {
refpaths.Add (bclAssemblies[name]);
//Console.WriteLine ($"+ {name}");
refpaths.Add (bclAssemblies[name]);
Console.WriteLine ($"+ {name}");
}
else {
refpaths.Add (r);
//Console.WriteLine ($"- {r}");
Console.WriteLine ($"- {r}");
}
}
refpaths.Add (bclAssemblies["WebAssembly.Bindings.dll"]);

var asmPath = Path.GetFullPath (Assembly);

Expand Down Expand Up @@ -224,15 +273,15 @@ static bool IsMethodPreserved (MethodDefinition m)

void MarkAndPreserveAll (TypeDefinition type)
{
Annotations.MarkAndPush (type);
//Annotations.MarkAndPush (type);
Annotations.SetPreserve (type, TypePreserve.All);
if (!type.HasNestedTypes) {
Tracer.Pop ();
//Tracer.Pop ();
return;
}
foreach (TypeDefinition nested in type.NestedTypes)
MarkAndPreserveAll (nested);
Tracer.Pop ();
//Tracer.Pop ();
}
}

Expand All @@ -243,9 +292,10 @@ Pipeline GetLinkerPipeline ()
var p = new Pipeline ();
p.AppendStep (new DontLinkExeStep ());
p.AppendStep (new LoadReferencesStep ());
p.AppendStep (new PreserveUsingAttributesStep (bclNames));
//p.AppendStep (new PreserveUsingAttributesStep (bclNames));
p.AppendStep (new BlacklistStep ());
p.AppendStep (new LinkBclStep (bclNames));
p.AppendStep (new KeepWebAssemblyBindingsStep ());
p.AppendStep (new TypeMapStep ());
p.AppendStep (new MarkStepWithUnresolvedLogging (this));
p.AppendStep (new SweepStep ());
Expand Down Expand Up @@ -307,6 +357,22 @@ protected override void Process ()
}
}

class KeepWebAssemblyBindingsStep : BaseStep
{
protected override void Process ()
{
var asms = Context.GetAssemblies ();

foreach (var a in asms) {
if (a.Name.Name.StartsWith ("WebAssembly.")) {
foreach (var t in a.MainModule.Types) {
Annotations.SetPreserve (t, TypePreserve.All);
}
}
}
}
}

class MarkStepWithUnresolvedLogging : MarkStep
{
BuildDistTask task;
Expand Down Expand Up @@ -390,21 +456,26 @@ void GenerateHtml ()
</div>
<script defer type=""text/javascript"" src=""ooui.js""></script>
<script type=""text/javascript"">
var assemblies = [");
var config = {
vfs_prefix: ""managed"",
deploy_prefix: ""managed"",
enable_debugging: 0,
file_list: [");
var head = "";
foreach (var l in linkedAsmPaths.Select (x => Path.GetFileName (x))) {
foreach (var l in linkedAsmPaths.Select (x => Path.ChangeExtension (Path.GetFileName (x), ".dll"))) {
w.Write (head);
w.Write ('\"');
w.Write (l);
w.Write ('\"');
head = ",";
}
w.WriteLine ($@"];
w.WriteLine ($@"]
}};
document.addEventListener(""DOMContentLoaded"", function(event) {{
oouiWasm(""{entryPoint.DeclaringType.Module.Assembly.Name.Name}"", ""{entryPoint.DeclaringType.Namespace}"", ""{entryPoint.DeclaringType.Name}"", ""{entryPoint.Name}"", assemblies);
oouiWasm(""{entryPoint.DeclaringType.Module.Assembly.Name.Name}"", ""{entryPoint.DeclaringType.Namespace}"", ""{entryPoint.DeclaringType.Name}"", ""{entryPoint.Name}"");
}});
</script>
<script defer type=""text/javascript"" src=""mono.js""></script>
<script defer type=""text/javascript"" src=""dotnet.js""></script>
</body>
</html>");
}
Expand All @@ -424,17 +495,21 @@ public override AssemblyDefinition Resolve (AssemblyNameReference name, ReaderPa
{
AssemblyDefinition asm = null;
if (!AssemblyCache.TryGetValue (name.Name, out asm)) {
Console.WriteLine ($"RESOLVE {name}");
var path = task.refpaths.FirstOrDefault (x => {
var rname = Path.GetFileNameWithoutExtension (x);
var eq = rname.Equals (name.Name, StringComparison.InvariantCultureIgnoreCase);
return eq;
});
if (path != null) {
//Console.WriteLine ($"SUCCESS {path}");
Console.WriteLine ($"SUCCESS {path}");
asm = ModuleDefinition.ReadModule (path, parameters).Assembly;
CacheAssembly (asm);
}
return base.Resolve (name, parameters);
else {
Console.WriteLine ($"FAIL");
return base.Resolve (name, parameters);
}
}
return asm;
}
Expand Down Expand Up @@ -473,11 +548,11 @@ public override TypeDefinition Resolve (TypeReference type)
}
}

namespace Mono.Linker
{
[Flags]
public enum CodeOptimizations
{
BeforeFieldInit = 1 << 0,
}
}
//namespace Mono.Linker
//{
// [Flags]
// public enum CodeOptimizations
// {
// BeforeFieldInit = 1 << 0,
// }
//}
13 changes: 5 additions & 8 deletions Ooui.Wasm.Build.Tasks/Ooui.Wasm.Build.Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<DefineConstants>NET_CORE</DefineConstants>
<LangVersion>8.0</LangVersion>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="15.3.409" />
<PackageReference Include="Microsoft.Build.Framework" Version="15.3.409" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.3.409" />
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
</ItemGroup>

<ItemGroup>
<Compile Remove="linker\**" />
<Compile Include="linker\linker\Linker\**" />
<Compile Include="linker\linker\Linker.Steps\**" />
<Compile Include="linker\cecil\Mono.Cecil\**" />
<Compile Include="linker\cecil\Mono.Cecil.Cil\**" />
<Compile Include="linker\cecil\Mono.Cecil.Metadata\**" />
<Compile Include="linker\cecil\Mono.Cecil.PE\**" />
<Compile Include="linker\cecil\Mono.Collections.Generic\**" />
<Compile Include="linker\cecil\Mono\**" />
<Compile Include="linker\src\linker\Linker\**" />
<Compile Include="linker\src\linker\Linker.Steps\**" />
<Compile Remove="linker\linker\Linker\Driver.cs" />
<Compile Remove="linker\linker\Linker\AssemblyInfo.cs" />
<Compile Remove="linker\cecil\Mono.Cecil\AssemblyInfo.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Ooui.Wasm.Build.Tasks/linker
4 changes: 4 additions & 0 deletions Ooui.Wasm/Ooui.Wasm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
<files>
<file src="Ooui.Wasm.targets" target="build/netstandard2.0/Ooui.Wasm.targets" />
<file src="../Ooui.Wasm.Build.Tasks/bin/Release/netstandard2.0/Ooui.Wasm.Build.Tasks.dll" target="build/netstandard2.0/Ooui.Wasm.Build.Tasks.dll" />
<file src="../Ooui.Wasm.Build.Tasks/bin/Release/netstandard2.0/Mono.Cecil.dll" target="build/netstandard2.0/Mono.Cecil.dll" />
<file src="../Ooui.Wasm.Build.Tasks/bin/Release/netstandard2.0/Mono.Cecil.Rocks.dll" target="build/netstandard2.0/Mono.Cecil.Rocks.dll" />
<file src="../Ooui.Wasm.Build.Tasks/bin/Release/netstandard2.0/Mono.Cecil.Pdb.dll" target="build/netstandard2.0/Mono.Cecil.Pdb.dll" />
<file src="../Ooui.Wasm.Build.Tasks/bin/Release/netstandard2.0/Mono.Cecil.Mdb.dll" target="build/netstandard2.0/Mono.Cecil.Mdb.dll" />
</files>
</package>
Loading