Skip to content

Commit 92a74d2

Browse files
authored
[browser] Enable webcil in Wasm SDK (#84977)
* Webcil in WasmSDK for build * Fix file writes. Wip on publish * Make publish work
1 parent bb48d46 commit 92a74d2

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Copyright (c) .NET Foundation. All rights reserved.
4040
<UsingTask TaskName="Microsoft.NET.Sdk.WebAssembly.GenerateWasmBootJson" AssemblyFile="$(_WebAssemblySdkTasksAssembly)" />
4141
<UsingTask TaskName="Microsoft.NET.Sdk.WebAssembly.ComputeWasmBuildAssets" AssemblyFile="$(_WebAssemblySdkTasksAssembly)" />
4242
<UsingTask TaskName="Microsoft.NET.Sdk.WebAssembly.ComputeWasmPublishAssets" AssemblyFile="$(_WebAssemblySdkTasksAssembly)" />
43+
<UsingTask TaskName="Microsoft.NET.Sdk.WebAssembly.ConvertDllsToWebCil" AssemblyFile="$(_WebAssemblySdkTasksAssembly)" />
4344

4445
<PropertyGroup>
4546
<SelfContained>true</SelfContained>
@@ -168,6 +169,8 @@ Copyright (c) .NET Foundation. All rights reserved.
168169
<_WasmInvariantGlobalization Condition="'$(_WasmInvariantGlobalization)' == ''">true</_WasmInvariantGlobalization>
169170
<_WasmCopyOutputSymbolsToOutputDirectory>$(CopyOutputSymbolsToOutputDirectory)</_WasmCopyOutputSymbolsToOutputDirectory>
170171
<_WasmCopyOutputSymbolsToOutputDirectory Condition="'$(_WasmCopyOutputSymbolsToOutputDirectory)'==''">true</_WasmCopyOutputSymbolsToOutputDirectory>
172+
<_WasmEnableWebcil>$(WasmEnableWebcil)</_WasmEnableWebcil>
173+
<_WasmEnableWebcil Condition="'$(_WasmEnableWebcil)' == ''">false</_WasmEnableWebcil>
171174
<_BlazorWebAssemblyStartupMemoryCache>$(BlazorWebAssemblyStartupMemoryCache)</_BlazorWebAssemblyStartupMemoryCache>
172175
<_BlazorWebAssemblyJiterpreter>$(BlazorWebAssemblyJiterpreter)</_BlazorWebAssemblyJiterpreter>
173176
<_BlazorWebAssemblyRuntimeOptions>$(BlazorWebAssemblyRuntimeOptions)</_BlazorWebAssemblyRuntimeOptions>
@@ -205,8 +208,17 @@ Copyright (c) .NET Foundation. All rights reserved.
205208
<Output TaskParameter="FilesToRemove" ItemName="_WasmBuildFilesToRemove" />
206209
</ComputeWasmBuildAssets>
207210

211+
<PropertyGroup>
212+
<_WasmBuildWebCilPath>$(IntermediateOutputPath)webcil</_WasmBuildWebCilPath>
213+
</PropertyGroup>
214+
215+
<ConvertDllsToWebCil Candidates="@(_BuildAssetsCandidates)" OutputPath="$(_WasmBuildWebCilPath)" IsEnabled="$(_WasmEnableWebcil)">
216+
<Output TaskParameter="WebCilCandidates" ItemName="_WebCilAssetsCandidates" />
217+
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
218+
</ConvertDllsToWebCil>
219+
208220
<DefineStaticWebAssets
209-
CandidateAssets="@(_BuildAssetsCandidates)"
221+
CandidateAssets="@(_WebCilAssetsCandidates)"
210222
SourceId="$(PackageId)"
211223
SourceType="Computed"
212224
AssetKind="Build"
@@ -362,14 +374,24 @@ Copyright (c) .NET Foundation. All rights reserved.
362374
ExistingAssets="@(_WasmPublishPrefilteredAssets)"
363375
DotNetJsVersion="$(_DotNetJsVersion)"
364376
FingerprintDotNetJs="$(WasmFingerprintDotnetJs)"
377+
IsWebCilEnabled="$(_WasmEnableWebcil)"
365378
>
366379
<Output TaskParameter="NewCandidates" ItemName="_NewWasmPublishStaticWebAssets" />
367380
<Output TaskParameter="FilesToRemove" ItemName="_PublishResolvedFilesToRemove" />
368381
</ComputeWasmPublishAssets>
369382

383+
<PropertyGroup>
384+
<_WasmPublishWebCilPath>$(IntermediateOutputPath)webcil\publish</_WasmPublishWebCilPath>
385+
</PropertyGroup>
386+
387+
<ConvertDllsToWebCil Candidates="@(_NewWasmPublishStaticWebAssets)" OutputPath="$(_WasmPublishWebCilPath)" IsEnabled="$(_WasmEnableWebcil)">
388+
<Output TaskParameter="WebCilCandidates" ItemName="_NewWebCilPublishStaticWebAssets" />
389+
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
390+
</ConvertDllsToWebCil>
391+
370392
<ItemGroup>
371393
<ResolvedFileToPublish Remove="@(_PublishResolvedFilesToRemove)" />
372-
<StaticWebAsset Include="@(_NewWasmPublishStaticWebAssets)" />
394+
<StaticWebAsset Include="@(_NewWebCilPublishStaticWebAssets)" />
373395

374396
<!-- TODO: Probably doesn't do anything as of now, original https://github.com/dotnet/aspnetcore/pull/34798 -->
375397
<PublishBlazorBootStaticWebAsset

src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ComputeWasmPublishAssets.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class ComputeWasmPublishAssets : Task
5555

5656
public bool FingerprintDotNetJs { get; set; }
5757

58+
public bool IsWebCilEnabled { get; set; }
59+
5860
[Output]
5961
public ITaskItem[] NewCandidates { get; set; }
6062

@@ -329,6 +331,9 @@ private List<ITaskItem> ComputeUpdatedAssemblies(
329331
{
330332
var asset = kvp.Value;
331333
var fileName = Path.GetFileName(asset.GetMetadata("RelativePath"));
334+
if (IsWebCilEnabled)
335+
fileName = Path.ChangeExtension(fileName, ".dll");
336+
332337
if (resolvedAssembliesToPublish.TryGetValue(fileName, out var existing))
333338
{
334339
// We found the assembly, so it'll have to be updated.
@@ -550,7 +555,7 @@ private void GroupResolvedFilesToPublish(
550555
}
551556

552557
var extension = candidate.GetMetadata("Extension");
553-
if (string.Equals(extension, ".dll", StringComparison.Ordinal))
558+
if (string.Equals(extension, ".dll", StringComparison.Ordinal) || string.Equals(extension, ".webcil", StringComparison.Ordinal))
554559
{
555560
var culture = candidate.GetMetadata("Culture");
556561
var inferredCulture = candidate.GetMetadata("DestinationSubDirectory").Replace("\\", "/").Trim('/');
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Reflection;
10+
using System.Runtime.Serialization;
11+
using System.Runtime.Serialization.Json;
12+
using System.Text;
13+
using System.Xml;
14+
using Microsoft.Build.Framework;
15+
using Microsoft.Build.Utilities;
16+
using ResourceHashesByNameDictionary = System.Collections.Generic.Dictionary<string, string>;
17+
18+
namespace Microsoft.NET.Sdk.WebAssembly;
19+
20+
public class ConvertDllsToWebCil : Task
21+
{
22+
[Required]
23+
public ITaskItem[] Candidates { get; set; }
24+
25+
[Required]
26+
public string OutputPath { get; set; }
27+
28+
[Required]
29+
public bool IsEnabled { get; set; }
30+
31+
[Output]
32+
public ITaskItem[] WebCilCandidates { get; set; }
33+
34+
protected readonly List<string> _fileWrites = new();
35+
36+
[Output]
37+
public string[]? FileWrites => _fileWrites.ToArray();
38+
39+
public override bool Execute()
40+
{
41+
var webCilCandidates = new List<ITaskItem>();
42+
43+
if (!IsEnabled)
44+
{
45+
WebCilCandidates = Candidates;
46+
return true;
47+
}
48+
49+
for (int i = 0; i < Candidates.Length; i++)
50+
{
51+
var candidate = Candidates[i];
52+
53+
var extension = candidate.GetMetadata("Extension");
54+
var filePath = candidate.ItemSpec;
55+
56+
if (!Directory.Exists(OutputPath))
57+
Directory.CreateDirectory(OutputPath);
58+
59+
if (extension == ".dll")
60+
{
61+
var tmpWebcil = Path.GetTempFileName();
62+
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: filePath, outputPath: tmpWebcil, logger: Log);
63+
webcilWriter.ConvertToWebcil();
64+
65+
var finalWebcil = Path.Combine(OutputPath, Path.GetFileNameWithoutExtension(filePath) + ".webcil");
66+
if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true))
67+
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} .");
68+
else
69+
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged.");
70+
71+
_fileWrites.Add(finalWebcil);
72+
73+
var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata());
74+
webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), ".webcil"));
75+
webcilItem.SetMetadata("AssetTraitName", "WasmResource");
76+
webcilItem.SetMetadata("AssetTraitValue", "runtime");
77+
webcilItem.SetMetadata("OriginalItemSpec", finalWebcil);
78+
79+
webCilCandidates.Add(webcilItem);
80+
}
81+
else
82+
{
83+
webCilCandidates.Add(candidate);
84+
}
85+
}
86+
87+
WebCilCandidates = webCilCandidates.ToArray();
88+
return true;
89+
}
90+
}

src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<RootNamespace>Microsoft.NET.Sdk.WebAssembly</RootNamespace>
77
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
<NoWarn>$(NoWarn);CS8632</NoWarn>
910
</PropertyGroup>
1011

1112
<ItemDefinitionGroup>
@@ -16,8 +17,13 @@
1617
</ItemDefinitionGroup>
1718

1819
<ItemGroup>
20+
<Compile Include="..\Common\Utils.cs" />
21+
<Compile Include="..\WasmAppBuilder\WebcilConverter.cs" />
22+
1923
<PackageReference Include="Microsoft.Build.Framework" Version="$(MicrosoftBuildFrameworkVersion)" ExcludeAssets="runtime" />
2024
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildUtilitiesCoreVersion)" ExcludeAssets="runtime" />
25+
26+
<ProjectReference Include="..\..\libraries\Microsoft.NET.WebAssembly.Webcil\src\Microsoft.NET.WebAssembly.Webcil.csproj" />
2127
</ItemGroup>
2228

2329
<Target Name="GetFilesToPackage" Returns="@(FilesToPackage)">

0 commit comments

Comments
 (0)