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

[IGNORE][wasi] cleanup use of EmitBundle* task #95384

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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
89 changes: 29 additions & 60 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 @@ -359,74 +359,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" />

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

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

<!-- runtimeconfig.bin -->
<!-- If it's an item name -->
<EmitBundleObjectFiles
FilesToBundle="$(_ParsedRuntimeConfigFilePath)"
ClangExecutable="$(WasiClang)"
BundleRegistrationFunctionName="mono_register_runtimeconfig_bin"
BundleFile="$(_WasmRuntimeConfigBundleFile)"
OutputDirectory="$(_WasmIntermediateOutputPath)">
<Output TaskParameter="BundledResources" ItemName="_BundledRuntimeConfigBin" />
FilesToBundle="@(%(_EmitBundleItemName.Identity))"
ClangExecutable="$(WasiClang)"
BundleRegistrationFunctionName="@(_EmitBundleItemName->'%(BundleRegistrationFunctionName)')"
BundleFile="@(_EmitBundleItemName->'%(BundleFile)')"
OutputDirectory="$(_WasmIntermediateOutputPath)">
<Output TaskParameter="BundledResources" ItemName="_EmitBundleOutputItem" />
</EmitBundleObjectFiles>

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

<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
63 changes: 39 additions & 24 deletions src/tasks/MonoTargetsTasks/EmitBundleTask/EmitBundleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,43 @@ 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
{
// msbuild does not release cores between invocations if the task is batched
be9?.ReleaseCores(allowedParallelism);
}

foreach (ITaskItem bundledResource in bundledResources)
{
Expand All @@ -188,8 +197,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 +218,22 @@ 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);
});

TaskItem itemForBundleFile = new TaskItem(Path.Combine(OutputDirectory, BundleFile));
itemForBundleFile.SetMetadata("DestinationFile", bundleFilePath);
bundledResources.Add(itemForBundleFile);
}

BundledResources = bundledResources.ToArray();

return !Log.HasLoggedErrors;
}

Expand Down
Loading