Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
[wasm] EmccCompile: Fix incremental build, in case of only partial
.. rebuilds. Specifically, when a rebuild causes only the *linking* part
to be run again. In that case, we were correctly skipping over compiling
native files, but didn't add them to `@(FileWrites)`, which caused
msbuild's incremental clean logic to treat them as "orphaned" files,
and delete them!
  • Loading branch information
radical committed Sep 22, 2021
commit 7b75d8557bcc8eb323e3095723a399b125350bcc
6 changes: 4 additions & 2 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@
Inputs="@(_BitcodeFile);$(_EmccDefaultFlagsRsp);$(_EmccCompileBitcodeRsp)"
Outputs="@(_BitcodeFile->'%(ObjectFile)')"
Condition="'$(_WasmShouldAOT)' == 'true' and @(_BitcodeFile->Count()) > 0"
DependsOnTargets="_WasmWriteRspForCompilingBitcode">
DependsOnTargets="_WasmWriteRspForCompilingBitcode"
Returns="@(FileWrites)">

<ItemGroup>
<_BitCodeFile Dependencies="%(_BitCodeFile.Dependencies);$(_EmccDefaultFlagsRsp);$(_EmccCompileBitcodeRsp)" />
Expand Down Expand Up @@ -364,7 +365,8 @@
<Target Name="_WasmLinkDotNet"
Inputs="@(_WasmLinkDependencies);$(_EmccDefaultFlagsRsp);$(_EmccLinkRsp)"
Outputs="$(_WasmIntermediateOutputPath)dotnet.js;$(_WasmIntermediateOutputPath)dotnet.wasm"
DependsOnTargets="_WasmSelectRuntimeComponentsForLinking;_WasmCompileAssemblyBitCodeFilesForAOT;_WasmWriteRspFilesForLinking">
DependsOnTargets="_WasmSelectRuntimeComponentsForLinking;_WasmCompileAssemblyBitCodeFilesForAOT;_WasmWriteRspFilesForLinking"
Returns="@(FileWrites)" >

<Message Text="Linking with emcc. This may take a while ..." Importance="High" />
<Message Text="Running emcc with @(_EmccLinkStepArgs->'%(Identity)', ' ')" Importance="Low" />
Expand Down
15 changes: 11 additions & 4 deletions src/tasks/WasmAppBuilder/EmccCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private bool ExecuteActual()
if (!ShouldCompile(srcFile, objFile, depFiles, out string reason))
{
Log.LogMessage(MessageImportance.Low, $"Skipping {srcFile} because {reason}.");
outputItems.Add(CreateOutputItemFor(srcFile, objFile));
}
else
{
Expand All @@ -107,7 +108,8 @@ private bool ExecuteActual()
if (_numCompiled == _totalFiles)
{
// nothing to do!
return true;
OutputFiles = outputItems.ToArray();
return !Log.HasLoggedErrors;
}

if (_numCompiled > 0)
Expand Down Expand Up @@ -200,9 +202,7 @@ bool ProcessSourceFile(string srcFile, string objFile)
else
Log.LogMessage(MessageImportance.Low, $"Copied {tmpObjFile} to {objFile}");

ITaskItem newItem = new TaskItem(objFile);
newItem.SetMetadata("SourceFile", srcFile);
outputItems.Add(newItem);
outputItems.Add(CreateOutputItemFor(srcFile, objFile));

int count = Interlocked.Increment(ref _numCompiled);
Log.LogMessage(MessageImportance.High, $"[{count}/{_totalFiles}] {Path.GetFileName(srcFile)} -> {Path.GetFileName(objFile)} [took {elapsedSecs:F}s]");
Expand All @@ -219,6 +219,13 @@ bool ProcessSourceFile(string srcFile, string objFile)
File.Delete(tmpObjFile);
}
}

ITaskItem CreateOutputItemFor(string srcFile, string objFile)
{
ITaskItem newItem = new TaskItem(objFile);
newItem.SetMetadata("SourceFile", srcFile);
return newItem;
}
}

private bool ShouldCompile(string srcFile, string objFile, string[] depFiles, out string reason)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.IO;
using System.Linq;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected string Rebuild(bool nativeRelink, bool invariant, BuildArgs buildArgs,
buildArgs = newBuildArgs;

_testOutput.WriteLine($"{Environment.NewLine}Rebuilding with no changes ..{Environment.NewLine}");
Console.WriteLine($"{Environment.NewLine}Rebuilding with no changes ..{Environment.NewLine}");
(_, string output) = BuildProject(buildArgs,
id: id,
dotnetWasmFromRuntimePack: false,
Expand Down Expand Up @@ -137,6 +138,18 @@ internal void CompareStat(IDictionary<string, FileStat> oldStat, IDictionary<str
throw new XunitException($"CompareStat failed:{Environment.NewLine}{msg}");
}

internal IDictionary<string, (string fullPath, bool unchanged)> GetFilesTable(bool unchanged, params string[] baseDirs)
{
var dict = new Dictionary<string, (string fullPath, bool unchanged)>();
foreach (var baseDir in baseDirs)
{
foreach (var file in Directory.EnumerateFiles(baseDir, "*", new EnumerationOptions { RecurseSubdirectories = true }))
dict[Path.GetFileName(file)] = (file, unchanged);
}

return dict;
}

internal IDictionary<string, (string fullPath, bool unchanged)> GetFilesTable(BuildArgs buildArgs, BuildPaths paths, bool unchanged)
{
List<string> files = new()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Linq;
using Wasm.Build.Tests;
using Xunit;
Expand Down Expand Up @@ -33,5 +34,60 @@ public void NoOpRebuildForNativeBuilds(BuildArgs buildArgs, bool nativeRelink, b
CompareStat(originalStat, newStat, pathsDict.Values);
RunAndTestWasmApp(buildArgs, buildDir: _projectDir, expectedExitCode: 42, host: host, id: id);
}

[ConditionalTheory(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
[InlineData("Debug")]
[InlineData("Release")]
public void BlazorNoopRebuild(string config)
{
string id = $"blz_rebuild_{config}";
string projectFile = CreateBlazorWasmTemplateProject(id);
AddItemsPropertiesToProject(projectFile, extraProperties: "<WasmBuildNative>true</WasmBuildNative>");

string objDir = Path.Combine(_projectDir!, "obj", config, "net6.0", "wasm");

BlazorBuild(id, config, NativeFilesType.Relinked);
File.Move(Path.Combine(s_buildEnv.LogRootPath, id, $"{id}-build.binlog"),
Path.Combine(s_buildEnv.LogRootPath, id, $"{id}-build-first.binlog"));

var pathsDict = GetFilesTable(true, objDir);
pathsDict.Remove("runtime-icall-table.h");
var originalStat = StatFiles(pathsDict.Select(kvp => kvp.Value.fullPath));

// build again
BlazorBuild(id, config, NativeFilesType.Relinked);
var newStat = StatFiles(pathsDict.Select(kvp => kvp.Value.fullPath));

CompareStat(originalStat, newStat, pathsDict.Values);
}


[ConditionalTheory(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
[InlineData("Debug")]
[InlineData("Release")]
public void BlazorOnlyLinkRebuild(string config)
{
string id = $"blz_relink_{config}";
string projectFile = CreateBlazorWasmTemplateProject(id);
AddItemsPropertiesToProject(projectFile, extraProperties: "<WasmBuildNative>true</WasmBuildNative>");

string objDir = Path.Combine(_projectDir!, "obj", config, "net6.0", "wasm");

BlazorBuild(id, config, NativeFilesType.Relinked);
File.Move(Path.Combine(s_buildEnv.LogRootPath, id, $"{id}-build.binlog"),
Path.Combine(s_buildEnv.LogRootPath, id, $"{id}-build-first.binlog"));

var pathsDict = GetFilesTable(true, objDir);
pathsDict.Remove("runtime-icall-table.h");
pathsDict.UpdateTo(unchanged: false, "dotnet.wasm", "dotnet.js", "emcc-link.rsp");

var originalStat = StatFiles(pathsDict.Select(kvp => kvp.Value.fullPath));

// build again
BlazorBuild(id, config, NativeFilesType.Relinked, "-p:EmccLinkOptimizationFlag=-O1");
var newStat = StatFiles(pathsDict.Select(kvp => kvp.Value.fullPath));

CompareStat(originalStat, newStat, pathsDict.Values);
}
}
}