Skip to content

Commit 0933e30

Browse files
authored
[release/8.0][wasm] Fix regressed file sizes for blazor (#92627)
* [wasm] Add getters for __indirect_function_table, and wasmMemory .. so that can work even if they get renamed during minimization. * [wasm] build: Revert to older behavior for WasmNativeStrip The earlier change was done in 678fd6a, which changed to pass `-g` to the link step also. But that resulted in increased native file sizes. Changed sizes for the `minimum blazor template - publish` scenario: ``` | Last rc1 run | With the change ----------------------------------------------------------|----------------- |------------------ SOD - Minimum Blazor Template - Publish |8590723.000 bytes |7889806.000 bytes Total Uncompressed _framework |4304274.000 bytes |4202273.000 bytes pub/wwwroot/_framework/dotnet.js |35722.000 bytes |35838.000 bytes pub/wwwroot/_framework/dotnet.native.8.0.0-VERSION.js |239307.000 bytes |134566.000 bytes pub/wwwroot/_framework/dotnet.native.wasm |1174394.000 bytes |1148841.000 bytes pub/wwwroot/_framework/dotnet.runtime.8.0.0-VERSION.js |221356.000 bytes |221712.000 bytes ``` * [wasm] cleanup corresponding tests * [wasm] Remove WasmNativeStrip from wasm.proj as it will have no effect
1 parent 849fc57 commit 0933e30

File tree

7 files changed

+91
-60
lines changed

7 files changed

+91
-60
lines changed

src/mono/wasm/Wasm.Build.Tests/WasmNativeDefaultsTests.cs

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Text.RegularExpressions;
67
using Xunit;
78
using Xunit.Abstractions;
89

@@ -12,6 +13,7 @@ namespace Wasm.Build.Tests
1213
{
1314
public class WasmNativeDefaultsTests : TestMainJsTestBase
1415
{
16+
private static Regex s_regex = new("\\*\\* WasmBuildNative:.*");
1517
public WasmNativeDefaultsTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext)
1618
: base(output, buildContext)
1719
{
@@ -39,19 +41,20 @@ public static TheoryData<string, string, bool, bool, bool> SettingDifferentFromV
3941
// Config=Release always causes relinking when publishing
4042
bool publishValue = forPublish && config == "Release" ? true : false;
4143
// Setting the default value from the runtime pack shouldn't trigger relinking
42-
data.Add(config, $"<{defaultPair.propertyName}>{defaultPair.defaultValueInRuntimePack}</{defaultPair.propertyName}>",
44+
data.Add(config, $"<{defaultPair.propertyName}>{defaultPair.defaultValueInRuntimePack.ToString().ToLower()}</{defaultPair.propertyName}>",
4345
/*aot*/ false, /*build*/ false, /*publish*/ publishValue);
4446
// Leaving the property unset, so checking the default
4547
data.Add(config, "", /*aot*/ false, /*build*/ false, /*publish*/ publishValue);
4648

4749
// Setting the !default value should trigger relinking
48-
data.Add(config, $"<{defaultPair.propertyName}>{!defaultPair.defaultValueInRuntimePack}</{defaultPair.propertyName}>",
50+
data.Add(config, $"<{defaultPair.propertyName}>{(!defaultPair.defaultValueInRuntimePack).ToString().ToLower()}</{defaultPair.propertyName}>",
4951
/*aot*/ false, /*build*/ true, /*publish*/ true);
5052
}
5153
}
5254

5355
return data;
5456
}
57+
5558
public static TheoryData<string, string, bool, bool, bool> DefaultsTestData(bool forPublish)
5659
{
5760
TheoryData<string, string, bool, bool, bool> data = new()
@@ -93,45 +96,34 @@ public static TheoryData<string, string, bool, bool, bool> DefaultsTestData(bool
9396
return data;
9497
}
9598

99+
#pragma warning disable xUnit1026 // For unused *buildValue*, and *publishValue* parameters
96100
[Theory]
97101
[MemberData(nameof(DefaultsTestData), parameters: false)]
98102
[MemberData(nameof(SettingDifferentFromValuesInRuntimePack), parameters: false)]
99103
public void DefaultsWithBuild(string config, string extraProperties, bool aot, bool expectWasmBuildNativeForBuild, bool expectWasmBuildNativeForPublish)
100104
{
101-
string output = CheckWasmNativeDefaultValue("native_defaults_build", config, extraProperties, aot, dotnetWasmFromRuntimePack: !expectWasmBuildNativeForPublish, publish: false);
102-
103-
bool expectedWasmNativeStripValue = true;
104-
if (/*isBuild && */ expectWasmBuildNativeForBuild && config == "Debug")
105-
expectedWasmNativeStripValue = false;
105+
(string output, string? line) = CheckWasmNativeDefaultValue("native_defaults_build", config, extraProperties, aot, dotnetWasmFromRuntimePack: !expectWasmBuildNativeForBuild, publish: false);
106106

107-
// bool expectedWasmNativeStripValue = !(wasmBuildNativeForBuild && config == "Debug");
108-
// for build
109-
Assert.Contains($"** WasmBuildNative: '{expectWasmBuildNativeForBuild.ToString().ToLower()}', WasmNativeStrip: '{expectedWasmNativeStripValue.ToString().ToLower()}', WasmBuildingForNestedPublish: ''", output);
110-
Assert.Contains("Stopping the build", output);
107+
InferAndCheckPropertyValues(line, isPublish: false, wasmBuildNative: expectWasmBuildNativeForBuild, config: config);
111108
}
112109

113-
#pragma warning disable xUnit1026 // For unused *buildValue* parameter
114110
[Theory]
115111
[MemberData(nameof(DefaultsTestData), parameters: true)]
116112
[MemberData(nameof(SettingDifferentFromValuesInRuntimePack), parameters: true)]
117113
public void DefaultsWithPublish(string config, string extraProperties, bool aot, bool expectWasmBuildNativeForBuild, bool expectWasmBuildNativeForPublish)
118114
{
119-
string output = CheckWasmNativeDefaultValue("native_defaults_publish", config, extraProperties, aot, dotnetWasmFromRuntimePack: !expectWasmBuildNativeForPublish, publish: true);
115+
(string output, string? line) = CheckWasmNativeDefaultValue("native_defaults_publish", config, extraProperties, aot, dotnetWasmFromRuntimePack: !expectWasmBuildNativeForPublish, publish: true);
120116

121-
// for build
122-
// Assert.DoesNotContain($"** WasmBuildNative: '{buildValue.ToString().ToLower()}', WasmNativeStrip: 'true', WasmBuildingForNestedPublish: ''", output);
123-
// for publish
124-
Assert.Contains($"** WasmBuildNative: '{expectWasmBuildNativeForPublish.ToString().ToLower()}', WasmNativeStrip: 'true', WasmBuildingForNestedPublish: 'true'", output);
125-
Assert.Contains("Stopping the build", output);
117+
InferAndCheckPropertyValues(line, isPublish: true, wasmBuildNative: expectWasmBuildNativeForPublish, config: config);
126118
}
127119
#pragma warning restore xunit1026
128120

129121
public static TheoryData<string, string, bool, bool> SetWasmNativeStripExplicitlyTestData(bool publish) => new()
130122
{
131-
{"Debug", "<WasmNativeStrip>true</WasmNativeStrip>", false, true },
132-
{"Release", "<WasmNativeStrip>true</WasmNativeStrip>", publish, true },
133-
{"Debug", "<WasmNativeStrip>false</WasmNativeStrip>", true, false },
134-
{"Release", "<WasmNativeStrip>false</WasmNativeStrip>", true, false }
123+
{"Debug", "<WasmNativeStrip>true</WasmNativeStrip>", /*wasmBuildNative*/ false, /*wasmNativeStrip*/ true },
124+
{"Release", "<WasmNativeStrip>true</WasmNativeStrip>", /*wasmBuildNative*/ publish, /*wasmNativeStrip*/ true },
125+
{"Debug", "<WasmNativeStrip>false</WasmNativeStrip>", /*wasmBuildNative*/ true, /*wasmNativeStrip*/ false },
126+
{"Release", "<WasmNativeStrip>false</WasmNativeStrip>", /*wasmBuildNative*/ true, /*wasmNativeStrip*/ false }
135127
};
136128

137129
public static TheoryData<string, string, bool, bool> SetWasmNativeStripExplicitlyWithWasmBuildNativeTestData() => new()
@@ -147,48 +139,52 @@ public void DefaultsWithPublish(string config, string extraProperties, bool aot,
147139
[MemberData(nameof(SetWasmNativeStripExplicitlyWithWasmBuildNativeTestData))]
148140
public void WasmNativeStripDefaultWithBuild(string config, string extraProperties, bool expectedWasmBuildNativeValue, bool expectedWasmNativeStripValue)
149141
{
150-
string output = CheckWasmNativeDefaultValue("native_strip_defaults", config, extraProperties, aot: false, dotnetWasmFromRuntimePack: !expectedWasmBuildNativeValue, publish: false);
142+
(string output, string? line) = CheckWasmNativeDefaultValue("native_strip_defaults", config, extraProperties, aot: false, dotnetWasmFromRuntimePack: !expectedWasmBuildNativeValue, publish: false);
151143

152-
Assert.Contains($"** WasmBuildNative: '{expectedWasmBuildNativeValue.ToString().ToLower()}', WasmNativeStrip: '{expectedWasmNativeStripValue.ToString().ToLower()}', WasmBuildingForNestedPublish: ''", output);
153-
Assert.Contains("Stopping the build", output);
144+
CheckPropertyValues(line,
145+
wasmBuildNative: expectedWasmBuildNativeValue,
146+
wasmNativeStrip: expectedWasmNativeStripValue,
147+
wasmNativeDebugSymbols: true,
148+
wasmBuildingForNestedPublish: null);
154149
}
155150

156151
[Theory]
157152
[MemberData(nameof(SetWasmNativeStripExplicitlyTestData), parameters: /*publish*/ true)]
158153
[MemberData(nameof(SetWasmNativeStripExplicitlyWithWasmBuildNativeTestData))]
159154
public void WasmNativeStripDefaultWithPublish(string config, string extraProperties, bool expectedWasmBuildNativeValue, bool expectedWasmNativeStripValue)
160155
{
161-
string output = CheckWasmNativeDefaultValue("native_strip_defaults", config, extraProperties, aot: false, dotnetWasmFromRuntimePack: !expectedWasmBuildNativeValue, publish: true);
156+
(string output, string? line) = CheckWasmNativeDefaultValue("native_strip_defaults", config, extraProperties, aot: false, dotnetWasmFromRuntimePack: !expectedWasmBuildNativeValue, publish: true);
162157

163-
Assert.Contains($"** WasmBuildNative: '{expectedWasmBuildNativeValue.ToString().ToLower()}', WasmNativeStrip: '{expectedWasmNativeStripValue.ToString().ToLower()}', WasmBuildingForNestedPublish: 'true'", output);
164-
Assert.Contains("Stopping the build", output);
158+
CheckPropertyValues(line,
159+
wasmBuildNative: expectedWasmBuildNativeValue,
160+
wasmNativeStrip: expectedWasmNativeStripValue,
161+
wasmNativeDebugSymbols: true,
162+
wasmBuildingForNestedPublish: true);
165163
}
166164

167165
[Theory]
168166
/* always relink */
169-
[InlineData("Debug", "", /*build*/ true, /*publish*/ true)]
170-
[InlineData("Release", "", /*build*/ true, /*publish*/ true)]
171-
[InlineData("Release", "<PublishTrimmed>false</PublishTrimmed>", /*build*/ true, /*publish*/ true)]
172-
public void WithNativeReference(string config, string extraProperties, bool buildValue, bool publishValue)
167+
[InlineData("Debug", "", /*publish*/ false)]
168+
[InlineData("Debug", "", /*publish*/ true)]
169+
[InlineData("Release", "", /*publish*/ false)]
170+
[InlineData("Release", "", /*publish*/ true)]
171+
[InlineData("Release", "<PublishTrimmed>false</PublishTrimmed>", /*publish*/ true)]
172+
public void WithNativeReference(string config, string extraProperties, bool publish)
173173
{
174174
string nativeLibPath = Path.Combine(BuildEnvironment.TestAssetsPath, "native-libs", "native-lib.o");
175175
string nativeRefItem = @$"<NativeFileReference Include=""{nativeLibPath}"" />";
176-
string output = CheckWasmNativeDefaultValue("native_defaults_publish",
176+
(string output, string? line) = CheckWasmNativeDefaultValue("native_defaults_publish",
177177
config,
178178
extraProperties,
179179
aot: false,
180-
dotnetWasmFromRuntimePack: !publishValue,
181-
publish: true,
180+
dotnetWasmFromRuntimePack: !publish,
181+
publish: publish,
182182
extraItems: nativeRefItem);
183183

184-
// for build - FIXME:
185-
Assert.DoesNotContain($"** WasmBuildNative: '{buildValue.ToString().ToLower()}', WasmBuildingForNestedPublish: ''", output);
186-
// for publish
187-
Assert.Contains($"** WasmBuildNative: '{publishValue.ToString().ToLower()}', WasmNativeStrip: 'true', WasmBuildingForNestedPublish: 'true'", output);
188-
Assert.Contains("Stopping the build", output);
184+
InferAndCheckPropertyValues(line, isPublish: publish, wasmBuildNative: true, config: config);
189185
}
190186

191-
private string CheckWasmNativeDefaultValue(string projectName,
187+
private (string, string?) CheckWasmNativeDefaultValue(string projectName,
192188
string config,
193189
string extraProperties,
194190
bool aot,
@@ -201,7 +197,7 @@ private string CheckWasmNativeDefaultValue(string projectName,
201197

202198
string printValueTarget = @"
203199
<Target Name=""PrintWasmBuildNative"" AfterTargets=""_BeforeWasmBuildApp"">
204-
<Message Text=""** WasmBuildNative: '$(WasmBuildNative)', WasmNativeStrip: '$(WasmNativeStrip)', WasmBuildingForNestedPublish: '$(WasmBuildingForNestedPublish)'"" Importance=""High"" />
200+
<Message Text=""** WasmBuildNative: '$(WasmBuildNative)', WasmNativeStrip: '$(WasmNativeStrip)', WasmNativeDebugSymbols: '$(WasmNativeDebugSymbols)', WasmBuildingForNestedPublish: '$(WasmBuildingForNestedPublish)'"" Importance=""High"" />
205201
" + (publish
206202
? @"<Error Text=""Stopping the build"" Condition=""$(WasmBuildingForNestedPublish) == 'true'"" />"
207203
: @"<Error Text=""Stopping the build"" />")
@@ -223,7 +219,32 @@ private string CheckWasmNativeDefaultValue(string projectName,
223219
BuildOnlyAfterPublish: false,
224220
Publish: publish));
225221

226-
return output;
222+
Assert.Contains("Stopping the build", output);
223+
224+
Match m = s_regex.Match(output);
225+
Assert.Equal(1, m.Groups.Count);
226+
return (output, m.Success ? m.Groups[0]?.ToString() : null);
227+
}
228+
229+
private void InferAndCheckPropertyValues(string? line, bool isPublish, bool wasmBuildNative, string config)
230+
{
231+
bool expectedWasmNativeStripValue;
232+
if (!isPublish && wasmBuildNative && config == "Debug")
233+
expectedWasmNativeStripValue = false;
234+
else
235+
expectedWasmNativeStripValue = true;
236+
237+
CheckPropertyValues(line, wasmBuildNative, expectedWasmNativeStripValue, /*wasmNativeDebugSymbols*/true, isPublish);
238+
}
239+
240+
private void CheckPropertyValues(string? line, bool wasmBuildNative, bool wasmNativeStrip, bool wasmNativeDebugSymbols, bool? wasmBuildingForNestedPublish)
241+
{
242+
Assert.NotNull(line);
243+
Assert.Contains($"** WasmBuildNative: '{wasmBuildNative.ToString().ToLower()}', " +
244+
$"WasmNativeStrip: '{wasmNativeStrip.ToString().ToLower()}', " +
245+
$"WasmNativeDebugSymbols: '{wasmNativeDebugSymbols.ToString().ToLower()}', " +
246+
$"WasmBuildingForNestedPublish: '{(wasmBuildingForNestedPublish.HasValue && wasmBuildingForNestedPublish == true ? "true" : "")}'",
247+
line);
227248
}
228249
}
229250
}

src/mono/wasm/build/WasmApp.Native.targets

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
$(_BeforeWasmBuildAppDependsOn);
2323
_SetupEmscripten;
2424
_SetWasmBuildNativeDefaults;
25-
_SetWasmNativeStripDefault;
2625
_ReadEmccProps
2726
</_BeforeWasmBuildAppDependsOn>
2827

@@ -119,6 +118,7 @@
119118
<_BoolPropertiesThatTriggerRelinking Include="InvariantTimezone" DefaultValueInRuntimePack="false" />
120119
<_BoolPropertiesThatTriggerRelinking Include="InvariantGlobalization" DefaultValueInRuntimePack="false" />
121120
<_BoolPropertiesThatTriggerRelinking Include="WasmNativeStrip" DefaultValueInRuntimePack="true" />
121+
<!--<_BoolPropertiesThatTriggerRelinking Include="WasmNativeDebugSymbols" DefaultValueInRuntimePack="true" />-->
122122
</ItemGroup>
123123

124124
<PropertyGroup>
@@ -133,7 +133,6 @@
133133
<WasmBuildNative Condition="'$(RunAOTCompilation)' == 'true' and '$(RunAOTCompilationAfterBuild)' == 'true'">true</WasmBuildNative>
134134

135135
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and @(NativeFileReference->Count()) > 0" >true</WasmBuildNative>
136-
<WasmBuildNative Condition="'$(WasmBuildNative)' == ''">false</WasmBuildNative>
137136
</PropertyGroup>
138137

139138
<!-- When Publishing -->
@@ -147,10 +146,23 @@
147146

148147
<!-- default to relinking in Release config -->
149148
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(Configuration)' == 'Release'">true</WasmBuildNative>
149+
</PropertyGroup>
150150

151+
<PropertyGroup>
151152
<WasmBuildNative Condition="'$(WasmBuildNative)' == ''">false</WasmBuildNative>
152153
</PropertyGroup>
153154

155+
<!-- Default with nothing set: Build+relink+config=debug -->
156+
<PropertyGroup Condition="'$(WasmNativeDebugSymbols)' == '' and '$(WasmNativeStrip)' == '' and '$(WasmBuildingForNestedPublish)' != 'true' and '$(WasmBuildNative)' == 'true' and '$(Configuration)' == 'Debug'">
157+
<WasmNativeDebugSymbols>true</WasmNativeDebugSymbols>
158+
<WasmNativeStrip>false</WasmNativeStrip>
159+
</PropertyGroup>
160+
161+
<PropertyGroup>
162+
<WasmNativeDebugSymbols Condition="'$(WasmNativeDebugSymbols)' == ''">true</WasmNativeDebugSymbols>
163+
<WasmNativeStrip Condition="'$(WasmNativeStrip)' == ''">true</WasmNativeStrip>
164+
</PropertyGroup>
165+
154166
<!-- If we want to default to true, and sdk is missing, then just warn, and set it to false -->
155167
<Warning Condition="'$(WasmBuildNative)' == 'true' and '$(_IsEMSDKMissing)' == 'true'"
156168
Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for building native files." />
@@ -160,14 +172,6 @@
160172
</PropertyGroup>
161173
</Target>
162174

163-
<Target Name="_SetWasmNativeStripDefault" Condition="'$(WasmNativeStrip)' == ''">
164-
<PropertyGroup>
165-
<!-- Build+relink+config=debug -->
166-
<WasmNativeStrip Condition="'$(WasmBuildingForNestedPublish)' != 'true' and '$(WasmBuildNative)' == 'true' and '$(Configuration)' == 'Debug'">false</WasmNativeStrip>
167-
<WasmNativeStrip Condition="'$(WasmNativeStrip)' == ''">true</WasmNativeStrip>
168-
</PropertyGroup>
169-
</Target>
170-
171175
<Target Name="_WasmBuildNativeCore" DependsOnTargets="$(_WasmBuildNativeCoreDependsOn)" Condition="'$(WasmBuildNative)' == 'true'" />
172176

173177
<Target Name="_PrepareForWasmBuildNative">
@@ -178,7 +182,6 @@
178182
<_MonoAotCrossCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','browser-wasm'))</_MonoAotCrossCompilerPath>
179183
<_EmccDefaultFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-default.rsp'))</_EmccDefaultFlagsRsp>
180184
<_EmccDefaultLinkFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-link.rsp'))</_EmccDefaultLinkFlagsRsp>
181-
<WasmNativeDebugSymbols Condition="'$(WasmNativeDebugSymbols)' == ''">true</WasmNativeDebugSymbols>
182185
<WasmLinkIcalls Condition="'$(WasmLinkIcalls)' == ''">$(WasmBuildNative)</WasmLinkIcalls>
183186

184187
<_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h</_WasmICallTablePath>
@@ -221,7 +224,7 @@
221224

222225
<_EmccCommonFlags Include="$(_DefaultEmccFlags)" />
223226
<_EmccCommonFlags Include="$(EmccFlags)" />
224-
<_EmccCommonFlags Include="-g" Condition="'$(WasmNativeDebugSymbols)' == 'true'" />
227+
<_EmccCommonFlags Include="-g" Condition="'$(WasmNativeStrip)' == 'false'" />
225228
<_EmccCommonFlags Include="-v" Condition="'$(EmccVerbose)' != 'false'" />
226229
<_EmccCommonFlags Include="-s DISABLE_EXCEPTION_CATCHING=0" Condition="'$(WasmEnableExceptionHandling)' == 'false'" />
227230
<_EmccCommonFlags Include="-fwasm-exceptions" Condition="'$(WasmEnableExceptionHandling)' == 'true'" />
@@ -249,6 +252,7 @@
249252
<_EmccCFlags Include="-emit-llvm" />
250253

251254
<_EmccCFlags Include="&quot;-I%(_EmccIncludePaths.Identity)&quot;" />
255+
<_EmccCFlags Include="-g" Condition="'$(WasmNativeDebugSymbols)' == 'true'" />
252256

253257
<!-- Adding optimization flag at the top, so it gets precedence -->
254258
<_EmccLDFlags Include="$(EmccLinkOptimizationFlag)" />
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
if (_nativeModuleLoaded) throw new Error("Native module already loaded");
22
_nativeModuleLoaded = true;
3-
createDotnetRuntime = Module = createDotnetRuntime(Module);
3+
createDotnetRuntime = Module = createDotnetRuntime(Module);
4+
Module["getWasmIndirectFunctionTable"] = function () { return wasmTable; }
5+
Module["getMemory"] = function () { return wasmMemory; }

0 commit comments

Comments
 (0)