Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasi] Simplify, and clean up target for bundled resource generation #95426

Merged
merged 8 commits into from
Nov 30, 2023
4 changes: 4 additions & 0 deletions src/mono/wasi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ app-builder:
build-tasks:
$(DOTNET) build $(TOP)/src/tasks/tasks.proj /p:Configuration=$(CONFIG) $(MSBUILD_ARGS)

build-packages:
rm -f $(TOP)/artifacts/packages/$(CONFIG)/Shipping/*.nupkg
WASI_SDK_PATH=$(WASI_SDK_PATH) $(TOP)/build.sh mono.packages+mono.manifests+packs.product -os wasi -c $(CONFIG) --binaryLog /p:ContinueOnError=false /p:StopOnFirstFailure=true $(MSBUILD_ARGS)

clean:
$(RM) -rf $(BUILDS_OBJ_DIR)

Expand Down
90 changes: 29 additions & 61 deletions src/mono/wasi/build/WasiApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
_GetNativeFilesForLinking;
_GenerateManagedToNative;
_WasmCompileNativeFiles;
_GenerateAssemblyObjectFiles;
_GenerateObjectFilesForSingleFileBundle;
_WasiLinkDotNet;
</_WasiBuildNativeCoreDependsOn>

Expand Down Expand Up @@ -326,7 +326,6 @@
<Target Name="_WasmSelectRuntimeComponentsForLinking" Condition="'$(UsingWasiRuntimeWorkload)' == 'true'" DependsOnTargets="_MonoSelectRuntimeComponents" />

<Target Name="_GetNativeFilesForLinking" DependsOnTargets="_WasmSelectRuntimeComponentsForLinking" Returns="@(_WasmNativeFileForLinking)">
<Message Text="MicrosoftNetCoreAppRuntimePackRidNativeDir: $(MicrosoftNetCoreAppRuntimePackRidNativeDir)" Importance="High" />
<PropertyGroup>
<!-- FIXME: eh case -->
<_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-wasm.a</_WasmEHLib>
Expand Down Expand Up @@ -359,74 +358,43 @@
</ItemGroup>
</Target>

<Target Name="_GenerateAssemblyObjectFiles" Returns="@(_WasiObjectFilesForBundle)"
Condition="'$(WasmSingleFileBundle)' == 'true'"
>
<!-- Get the file hashes of everything in @(_WasmBundleFiles), then pass it all to EmitWasmBundleObjectFiles. This
will emit corresponding .o files for anything we don't already have on disk. -->
<PropertyGroup>
<_WasmAssembliesBundleObjectFile>wasi_bundled_assemblies.o</_WasmAssembliesBundleObjectFile>
<_WasmIcuBundleObjectFile>wasi_bundled_icu.o</_WasmIcuBundleObjectFile>
<_WasmRuntimeConfigBundleFile>wasi_runtime_config_bin.o</_WasmRuntimeConfigBundleFile>
</PropertyGroup>
<!-- TODO make this incremental compilation -->
<EmitBundleObjectFiles
FilesToBundle="@(_WasmAssembliesInternal)"
ClangExecutable="$(WasiClang)"
BundleRegistrationFunctionName="mono_register_assemblies_bundle"
BundleFile="$(_WasmAssembliesBundleObjectFile)"
OutputDirectory="$(_WasmIntermediateOutputPath)">
<Output TaskParameter="BundledResources" ItemName="_WasmBundledAssemblies" />
</EmitBundleObjectFiles>

<ItemGroup>
<_WasiObjectFilesForBundle Include="$(_WasmIntermediateOutputPath)$(_WasmAssembliesBundleObjectFile)" />
<_WasiObjectFilesForBundle Include="%(_WasmBundledAssemblies.DestinationFile)" />
</ItemGroup>

<!-- Clean up the bundle-objects dir - remove anything we no longer need -->
<Target Name="_GenerateObjectFilesForSingleFileBundle" Returns="@(_WasiObjectFilesForBundle)" Condition="'$(WasmSingleFileBundle)' == 'true'">
<ItemGroup>
<WasmBundleFileToDelete Include="$(_WasmIntermediateOutputPath)bundled_*.o" />
<WasmBundleFileToDelete Remove="$(_WasmIntermediateOutputPath)$(_WasmAssembliesBundleObjectFile)" />
<WasmBundleFileToDelete Remove="%(_WasmBundledAssemblies.DestinationFile)" />
</ItemGroup>

<EmitBundleObjectFiles
Condition="'$(InvariantGlobalization)' != 'true'"
FilesToBundle="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)icudt.dat"
ClangExecutable="$(WasiClang)"
BundleRegistrationFunctionName="mono_register_icu_bundle"
BundleFile="$(_WasmIcuBundleObjectFile)"
OutputDirectory="$(_WasmIntermediateOutputPath)">
<Output TaskParameter="BundledResources" ItemName="BundledWasmIcu" />
</EmitBundleObjectFiles>

<ItemGroup Condition="'$(InvariantGlobalization)' != 'true'">
<_WasiObjectFilesForBundle Include="$(_WasmIntermediateOutputPath)$(_WasmIcuBundleObjectFile)" />
<_WasiObjectFilesForBundle Include="%(BundledWasmIcu.DestinationFile)" />

<WasmBundleFileToDelete Remove="$(_WasmIntermediateOutputPath)$(_WasmIcuBundleObjectFile)" />
<WasmBundleFileToDelete Remove="%(BundledWasmIcu.DestinationFile)" />
<_EmitBundleItemName
Include="@(_WasmAssembliesInternal)"
BundleRegistrationFunctionName="mono_register_assemblies_bundle"
BundleFile="wasi_bundled_assemblies.o" />

<_EmitBundleItemName
Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)icudt.dat"
BundleRegistrationFunctionName="mono_register_icu_bundle"
BundleFile="wasi_bundled_icu.o" />

<_EmitBundleItemName
Include="$(_ParsedRuntimeConfigFilePath)"
BundleRegistrationFunctionName="mono_register_runtimeconfig_bin"
BundleFile="wasi_bundled_runtimeconfig_bin.o" />
</ItemGroup>

<!-- runtimeconfig.bin -->
<!-- batched on .BundleRegistrationFunctionName+.BundleFile -->
<EmitBundleObjectFiles
FilesToBundle="$(_ParsedRuntimeConfigFilePath)"
ClangExecutable="$(WasiClang)"
BundleRegistrationFunctionName="mono_register_runtimeconfig_bin"
BundleFile="$(_WasmRuntimeConfigBundleFile)"
OutputDirectory="$(_WasmIntermediateOutputPath)">
<Output TaskParameter="BundledResources" ItemName="_BundledRuntimeConfigBin" />
FilesToBundle="@(_EmitBundleItemName)"
ClangExecutable="$(WasiClang)"
BundleRegistrationFunctionName="%(_EmitBundleItemName.BundleRegistrationFunctionName)"
BundleFile="%(_EmitBundleItemName.BundleFile)"
OutputDirectory="$(_WasmIntermediateOutputPath)">
<Output TaskParameter="BundledResources" ItemName="_EmitBundleOutputItem" />
<Output TaskParameter="BundleRegistrationFile" ItemName="_EmitBundleRegistrationFile" />
</EmitBundleObjectFiles>

<ItemGroup>
<_WasiObjectFilesForBundle Include="$(_WasmIntermediateOutputPath)$(_WasmRuntimeConfigBundleFile)" />
<_WasiObjectFilesForBundle Include="%(_BundledRuntimeConfigBin.DestinationFile)" />
<_EmitBundleOutputFile Remove="@(_EmitBundleOutputFile)" />
<_EmitBundleOutputFile Include="%(_EmitBundleOutputItem.DestinationFile)" />
<_EmitBundleOutputFile Include="@(_EmitBundleRegistrationFile)" />

<WasmBundleFileToDelete Remove="$(_WasmIntermediateOutputPath)$(_WasmRuntimeConfigBundleFile)" />
<WasmBundleFileToDelete Remove="%(_BundledRuntimeConfigBin.DestinationFile)" />
<_WasiObjectFilesForBundle Include="@(_EmitBundleOutputFile)" />
<FileWrites Include="@(_EmitBundleOutputFile)" />
</ItemGroup>
<Delete Files="@(WasmBundleFileToDelete)" />
</Target>

<Target Name="_WasmWriteRspFilesForLinking" DependsOnTargets="_CheckWasiClangIsExpectedVersion;_WasmCalculateInitialHeapSize">
Expand Down
64 changes: 40 additions & 24 deletions src/tasks/MonoTargetsTasks/EmitBundleTask/EmitBundleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public abstract class EmitBundleBase : Microsoft.Build.Utilities.Task, ICancelab
[Output]
public ITaskItem[] BundledResources { get; set; } = default!;

// Set only if @BundleFile was set
[Output]
public string? BundleRegistrationFile { get; set; }

public override bool Execute()
{
if (!Directory.Exists(OutputDirectory))
Expand Down Expand Up @@ -150,34 +154,42 @@ public override bool Execute()

// Generate source file(s) containing each resource's byte data and size
int allowedParallelism = Math.Max(Math.Min(bundledResources.Count, Environment.ProcessorCount), 1);
if (BuildEngine is IBuildEngine9 be9)
IBuildEngine9? be9 = BuildEngine as IBuildEngine9;
if (be9 is not null)
allowedParallelism = be9.RequestCores(allowedParallelism);

Parallel.For(0, remainingDestinationFilesToBundle.Length, new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism, CancellationToken = BuildTaskCancelled.Token }, (i, state) =>
try
{
var group = remainingDestinationFilesToBundle[i];
Parallel.For(0, remainingDestinationFilesToBundle.Length, new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism, CancellationToken = BuildTaskCancelled.Token }, (i, state) =>
{
var group = remainingDestinationFilesToBundle[i];

var contentSourceFile = group.First();
var contentSourceFile = group.First();

var inputFile = contentSourceFile.ItemSpec;
var destinationFile = contentSourceFile.GetMetadata("DestinationFile");
var registeredName = contentSourceFile.GetMetadata(RegisteredName);
var inputFile = contentSourceFile.ItemSpec;
var destinationFile = contentSourceFile.GetMetadata("DestinationFile");
var registeredName = contentSourceFile.GetMetadata(RegisteredName);

var count = Interlocked.Increment(ref verboseCount);
Log.LogMessage(MessageImportance.Low, "{0}/{1} Bundling {2} ...", count, remainingDestinationFilesToBundle.Length, registeredName);
var count = Interlocked.Increment(ref verboseCount);
Log.LogMessage(MessageImportance.Low, "{0}/{1} Bundling {2} ...", count, remainingDestinationFilesToBundle.Length, registeredName);

Log.LogMessage(MessageImportance.Low, "Bundling {0} into {1}", inputFile, destinationFile);
var symbolName = _resourceDataSymbolDictionary[registeredName];
if (!EmitBundleFile(destinationFile, (codeStream) =>
{
using var inputStream = File.OpenRead(inputFile);
using var outputUtf8Writer = new StreamWriter(codeStream, Utf8NoBom);
BundleFileToCSource(symbolName, inputStream, outputUtf8Writer);
}))
{
state.Stop();
}
});
Log.LogMessage(MessageImportance.Low, "Bundling {0} into {1}", inputFile, destinationFile);
var symbolName = _resourceDataSymbolDictionary[registeredName];
if (!EmitBundleFile(destinationFile, (codeStream) =>
{
using var inputStream = File.OpenRead(inputFile);
using var outputUtf8Writer = new StreamWriter(codeStream, Utf8NoBom);
BundleFileToCSource(symbolName, inputStream, outputUtf8Writer);
}))
{
state.Stop();
}
});
}
finally
{
be9?.ReleaseCores(allowedParallelism);
}

foreach (ITaskItem bundledResource in bundledResources)
{
Expand All @@ -188,8 +200,6 @@ public override bool Execute()
bundledResource.SetMetadata("DataLenSymbolValue", symbolDataLen[resourceDataSymbol].ToString());
}

BundledResources = bundledResources.ToArray();

if (!string.IsNullOrEmpty(BundleFile))
{
string resourceSymbols = GatherUniqueExportedResourceDataSymbols(bundledResources);
Expand All @@ -211,14 +221,20 @@ public override bool Execute()

Log.LogMessage(MessageImportance.Low, $"Bundling {files.Count} files for {BundleRegistrationFunctionName}");

string bundleFilePath = Path.Combine(OutputDirectory, BundleFile);

// Generate source file to preallocate resources and register bundled resources
EmitBundleFile(Path.Combine(OutputDirectory, BundleFile), (outputStream) =>
EmitBundleFile(bundleFilePath, (outputStream) =>
{
using var outputUtf8Writer = new StreamWriter(outputStream, Utf8NoBom);
GenerateBundledResourcePreallocationAndRegistration(resourceSymbols, BundleRegistrationFunctionName, files, outputUtf8Writer);
});

BundleRegistrationFile = bundleFilePath;
}

BundledResources = bundledResources.ToArray();

return !Log.HasLoggedErrors;
}

Expand Down
Loading