From 92a74d26ed37342f6770cfaa9562e51d39f3dac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Sat, 22 Apr 2023 01:55:13 +0200 Subject: [PATCH] [browser] Enable webcil in Wasm SDK (#84977) * Webcil in WasmSDK for build * Fix file writes. Wip on publish * Make publish work --- ...rosoft.NET.Sdk.WebAssembly.Browser.targets | 26 +++++- .../ComputeWasmPublishAssets.cs | 7 +- .../ConvertDllsToWebCil.cs | 90 +++++++++++++++++++ ...soft.NET.Sdk.WebAssembly.Pack.Tasks.csproj | 6 ++ 4 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs diff --git a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets index c610f34c1b689..3d48cf2d7cf0f 100644 --- a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets +++ b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets @@ -40,6 +40,7 @@ Copyright (c) .NET Foundation. All rights reserved. + true @@ -168,6 +169,8 @@ Copyright (c) .NET Foundation. All rights reserved. <_WasmInvariantGlobalization Condition="'$(_WasmInvariantGlobalization)' == ''">true <_WasmCopyOutputSymbolsToOutputDirectory>$(CopyOutputSymbolsToOutputDirectory) <_WasmCopyOutputSymbolsToOutputDirectory Condition="'$(_WasmCopyOutputSymbolsToOutputDirectory)'==''">true + <_WasmEnableWebcil>$(WasmEnableWebcil) + <_WasmEnableWebcil Condition="'$(_WasmEnableWebcil)' == ''">false <_BlazorWebAssemblyStartupMemoryCache>$(BlazorWebAssemblyStartupMemoryCache) <_BlazorWebAssemblyJiterpreter>$(BlazorWebAssemblyJiterpreter) <_BlazorWebAssemblyRuntimeOptions>$(BlazorWebAssemblyRuntimeOptions) @@ -205,8 +208,17 @@ Copyright (c) .NET Foundation. All rights reserved. + + <_WasmBuildWebCilPath>$(IntermediateOutputPath)webcil + + + + + + + + + <_WasmPublishWebCilPath>$(IntermediateOutputPath)webcil\publish + + + + + + + - + ComputeUpdatedAssemblies( { var asset = kvp.Value; var fileName = Path.GetFileName(asset.GetMetadata("RelativePath")); + if (IsWebCilEnabled) + fileName = Path.ChangeExtension(fileName, ".dll"); + if (resolvedAssembliesToPublish.TryGetValue(fileName, out var existing)) { // We found the assembly, so it'll have to be updated. @@ -550,7 +555,7 @@ private void GroupResolvedFilesToPublish( } var extension = candidate.GetMetadata("Extension"); - if (string.Equals(extension, ".dll", StringComparison.Ordinal)) + if (string.Equals(extension, ".dll", StringComparison.Ordinal) || string.Equals(extension, ".webcil", StringComparison.Ordinal)) { var culture = candidate.GetMetadata("Culture"); var inferredCulture = candidate.GetMetadata("DestinationSubDirectory").Replace("\\", "/").Trim('/'); diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs new file mode 100644 index 0000000000000..fc8136525bd68 --- /dev/null +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs @@ -0,0 +1,90 @@ +// 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.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Json; +using System.Text; +using System.Xml; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using ResourceHashesByNameDictionary = System.Collections.Generic.Dictionary; + +namespace Microsoft.NET.Sdk.WebAssembly; + +public class ConvertDllsToWebCil : Task +{ + [Required] + public ITaskItem[] Candidates { get; set; } + + [Required] + public string OutputPath { get; set; } + + [Required] + public bool IsEnabled { get; set; } + + [Output] + public ITaskItem[] WebCilCandidates { get; set; } + + protected readonly List _fileWrites = new(); + + [Output] + public string[]? FileWrites => _fileWrites.ToArray(); + + public override bool Execute() + { + var webCilCandidates = new List(); + + if (!IsEnabled) + { + WebCilCandidates = Candidates; + return true; + } + + for (int i = 0; i < Candidates.Length; i++) + { + var candidate = Candidates[i]; + + var extension = candidate.GetMetadata("Extension"); + var filePath = candidate.ItemSpec; + + if (!Directory.Exists(OutputPath)) + Directory.CreateDirectory(OutputPath); + + if (extension == ".dll") + { + var tmpWebcil = Path.GetTempFileName(); + var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: filePath, outputPath: tmpWebcil, logger: Log); + webcilWriter.ConvertToWebcil(); + + var finalWebcil = Path.Combine(OutputPath, Path.GetFileNameWithoutExtension(filePath) + ".webcil"); + if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) + Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} ."); + else + Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged."); + + _fileWrites.Add(finalWebcil); + + var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata()); + webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), ".webcil")); + webcilItem.SetMetadata("AssetTraitName", "WasmResource"); + webcilItem.SetMetadata("AssetTraitValue", "runtime"); + webcilItem.SetMetadata("OriginalItemSpec", finalWebcil); + + webCilCandidates.Add(webcilItem); + } + else + { + webCilCandidates.Add(candidate); + } + } + + WebCilCandidates = webCilCandidates.ToArray(); + return true; + } +} diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj index e56ee68e46a5e..747e216bfb46e 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj @@ -6,6 +6,7 @@ Microsoft.NET.Sdk.WebAssembly true true + $(NoWarn);CS8632 @@ -16,8 +17,13 @@ + + + + +