Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
make EmccInitialHeapSize dynamic based on size of the assemblies
  • Loading branch information
pavelsavara committed Jan 16, 2023
commit 4da3be8a8c76ef8a675853705fbc9c3dfdaada30
5 changes: 5 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@
<Error Condition="'$(_IsEMSDKMissing)' == 'true'"
Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for building native files." />

<AssembliesTotalSize Assemblies="@(WasmAssembliesToBundle)">
<Output TaskParameter="TotalSize" PropertyName="_WasmAssembliesTotalSize" />
</AssembliesTotalSize>

<PropertyGroup>
<_MonoAotCrossCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','browser-wasm'))</_MonoAotCrossCompilerPath>
<_EmccDefaultFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-default.rsp'))</_EmccDefaultFlagsRsp>
Expand Down Expand Up @@ -188,6 +192,7 @@
<_EmccLinkRsp>$(_WasmIntermediateOutputPath)emcc-link.rsp</_EmccLinkRsp>

<EmccInitialHeapSize Condition="'$(EmccInitialHeapSize)' == ''">$(EmccTotalMemory)</EmccInitialHeapSize>
<EmccInitialHeapSize Condition="'$(EmccInitialHeapSize)' == '' and '$(_WasmAssembliesTotalSize)' != '' and $(_WasmAssembliesTotalSize) > 16777216">$(_WasmAssembliesTotalSize)</EmccInitialHeapSize>
<EmccInitialHeapSize Condition="'$(EmccInitialHeapSize)' == ''">16777216</EmccInitialHeapSize>
</PropertyGroup>

Expand Down
7 changes: 6 additions & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<UsingTask TaskName="WasmAppBuilder" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />
<UsingTask TaskName="WasmLoadAssembliesAndReferences" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />
<UsingTask TaskName="AssembliesTotalSize" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />

<!--
Required public items/properties:
Expand Down Expand Up @@ -51,7 +52,7 @@
- $(EmccFlags) - Emcc flags used for both compiling native files, and linking
- $(EmccExtraLDFlags) - Extra emcc flags for linking
- $(EmccExtraCFlags) - Extra emcc flags for compiling native files
- $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 16777216
- $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 16777216 or size of the DLLs.
Corresponds to `INITIAL_MEMORY` arg for emcc.
(previously named EmccTotalMemory, which is still kept as an alias)

Expand Down Expand Up @@ -203,6 +204,10 @@
<WasmAssembliesToBundle Include="$(PublishDir)\**\*.dll" />
</ItemGroup>

<AssembliesTotalSize Assemblies="@(WasmAssembliesToBundle)">
<Output TaskParameter="TotalSize" PropertyName="_WasmAssembliesTotalSize" />
</AssembliesTotalSize>

<PropertyGroup Condition="'$(_WasmRuntimeConfigFilePath)' == ''">
<_WasmRuntimeConfigFilePath Condition="$([System.String]::new(%(PublishItemsOutputGroupOutputs.Identity)).EndsWith('$(AssemblyName).runtimeconfig.json'))">@(PublishItemsOutputGroupOutputs)</_WasmRuntimeConfigFilePath>
</PropertyGroup>
Expand Down
43 changes: 43 additions & 0 deletions src/tasks/WasmAppBuilder/AssembliesTotalSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

// estimate the total size of the assemblies we are going to load and round it up to 64K
public class AssembliesTotalSize : Task
{
[Required]
[NotNull]
public string[]? Assemblies { get; set; }

[Output]
public long? TotalSize { get; private set; }

public override bool Execute ()
{
long totalSize=0;
foreach (var asm in Assemblies)
{
var info = new FileInfo(asm);
if (!info.Exists)
{
Log.LogError($"Could not find assembly '{asm}'");
return false;
}
totalSize += info.Length;
}
totalSize += 0x10000;
totalSize &= 0xFFFF0000;

TotalSize = totalSize;

return true;
}
}