From 4d61185642e8446fa8c98a3f29788a8c92bd0d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 18 Apr 2023 14:37:50 +0200 Subject: [PATCH 1/3] Webcil in WasmSDK for build --- ...rosoft.NET.Sdk.WebAssembly.Browser.targets | 11 ++- .../ConvertDllsToWebCil.cs | 83 +++++++++++++++++++ ...soft.NET.Sdk.WebAssembly.Pack.Tasks.csproj | 6 ++ 3 files changed, 99 insertions(+), 1 deletion(-) 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..c43ff1ef0975f 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 @@ -205,8 +206,16 @@ Copyright (c) .NET Foundation. All rights reserved. + + <_WasmBuildWebCilPath>$(IntermediateOutputPath)\webcil + + + + + + ; + +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; } + + 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."); + + 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 @@ + + + + + From 8024e60fec814f752a3ac5c10dd85d81b82955b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 18 Apr 2023 15:03:13 +0200 Subject: [PATCH 2/3] Fix file writes. Wip on publish --- .../Microsoft.NET.Sdk.WebAssembly.Browser.targets | 15 +++++++++++++-- .../ComputeWasmPublishAssets.cs | 7 ++++++- .../ConvertDllsToWebCil.cs | 7 +++++++ 3 files changed, 26 insertions(+), 3 deletions(-) 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 c43ff1ef0975f..6a79ea96d39dd 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 @@ -207,11 +207,12 @@ Copyright (c) .NET Foundation. All rights reserved. - <_WasmBuildWebCilPath>$(IntermediateOutputPath)\webcil + <_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 index 1a3484a8fc231..fc8136525bd68 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs @@ -31,6 +31,11 @@ public class ConvertDllsToWebCil : Task [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(); @@ -63,6 +68,8 @@ public override bool Execute() 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"); From 8887ca201e16b83872a68bada64e0b8fff6f55db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Wed, 19 Apr 2023 09:33:57 +0200 Subject: [PATCH 3/3] Make publish work --- .../build/Microsoft.NET.Sdk.WebAssembly.Browser.targets | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 6a79ea96d39dd..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 @@ -169,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) @@ -210,7 +212,7 @@ Copyright (c) .NET Foundation. All rights reserved. <_WasmBuildWebCilPath>$(IntermediateOutputPath)webcil - + @@ -372,7 +374,7 @@ Copyright (c) .NET Foundation. All rights reserved. ExistingAssets="@(_WasmPublishPrefilteredAssets)" DotNetJsVersion="$(_DotNetJsVersion)" FingerprintDotNetJs="$(WasmFingerprintDotnetJs)" - IsWebCilEnabled="$(WasmEnableWebcil)" + IsWebCilEnabled="$(_WasmEnableWebcil)" > @@ -382,7 +384,7 @@ Copyright (c) .NET Foundation. All rights reserved. <_WasmPublishWebCilPath>$(IntermediateOutputPath)webcil\publish - +