Skip to content

Commit 1d8ad03

Browse files
authored
[wasm] Fix regression in compiling .bc -> .o files (#56063)
* [wasm] Add back --emit-llvm that got removed mistakenly, in an earlier commit .. found thanks to Jerome Laban. * [wasm] Set EmccCompile's messages to MessageImportance.Low by default. .. and to MessageImportance.Normal if `$(EmccVerbose)==true`. * [wasm] Quote filenames passed to emcc compile command line * Add more blazorwasm tests - for debug/release, aot/relinking * Bump sdk for workload testing to 6.0.100-rc.1.21370.2 * [wasm] Fix regression in compiling bitcode -> .o The `-emit-llvm` arg has been incorrectly added, and removed from the args used for compiling .bc->.o . This commit fixes it, and adds a crude test for it, so we don't regress again. * Fix build
1 parent a2f2a62 commit 1d8ad03

File tree

8 files changed

+70
-16
lines changed

8 files changed

+70
-16
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
<SQLitePCLRawbundle_greenVersion>2.0.4</SQLitePCLRawbundle_greenVersion>
162162
<MoqVersion>4.12.0</MoqVersion>
163163
<FsCheckVersion>2.14.3</FsCheckVersion>
164-
<SdkVersionForWorkloadTesting>6.0.100-preview.7.21362.5</SdkVersionForWorkloadTesting>
164+
<SdkVersionForWorkloadTesting>6.0.100-rc.1.21370.2</SdkVersionForWorkloadTesting>
165165
<!-- Docs -->
166166
<MicrosoftPrivateIntellisenseVersion>5.0.0-preview-20201009.2</MicrosoftPrivateIntellisenseVersion>
167167
<!-- ILLink -->

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@
158158
<EmccLinkOptimizationFlag Condition="'$(EmccLinkOptimizationFlag)' == ''" >$(EmccCompileOptimizationFlag)</EmccLinkOptimizationFlag>
159159

160160
<_EmccCompileRsp>$(_WasmIntermediateOutputPath)emcc-compile.rsp</_EmccCompileRsp>
161+
<_EmccCompileOutputMessageImportance Condition="'$(EmccVerbose)' == 'true'">Normal</_EmccCompileOutputMessageImportance>
162+
<_EmccCompileOutputMessageImportance Condition="'$(EmccVerbose)' != 'true'">Low</_EmccCompileOutputMessageImportance>
161163
</PropertyGroup>
162164

163165
<ItemGroup>
@@ -181,6 +183,7 @@
181183
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
182184
<_EmccCFlags Include="-DCORE_BINDINGS" />
183185
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
186+
<_EmccCFlags Include="-emit-llvm" />
184187

185188
<_EmccCFlags Include="&quot;-I%(_EmccIncludePaths.Identity)&quot;" />
186189
<_EmccCFlags Include="-g" Condition="'$(WasmNativeDebugSymbols)' == 'true'" />
@@ -240,7 +243,11 @@
240243
<Exec Command="$(_EmBuilder) build MINIMAL" EnvironmentVariables="@(EmscriptenEnvVars)" StandardOutputImportance="Low" StandardErrorImportance="Low" />
241244

242245
<Message Text="Compiling native assets with emcc. This may take a while ..." Importance="High" />
243-
<EmccCompile SourceFiles="@(_WasmSourceFileToCompile)" Arguments='"@$(_EmccDefaultFlagsRsp)" "@$(_EmccCompileRsp)"' EnvironmentVariables="@(EmscriptenEnvVars)" />
246+
<EmccCompile
247+
SourceFiles="@(_WasmSourceFileToCompile)"
248+
Arguments='"@$(_EmccDefaultFlagsRsp)" "@$(_EmccCompileRsp)"'
249+
EnvironmentVariables="@(EmscriptenEnvVars)"
250+
OutputMessageImportance="$(_EmccCompileOutputMessageImportance)" />
244251

245252
<ItemGroup>
246253
<WasmNativeAsset Include="%(_WasmSourceFileToCompile.ObjectFile)" />
@@ -269,8 +276,9 @@
269276
<EmccCompile
270277
Condition="@(_BitCodeFile->Count()) > 0"
271278
SourceFiles="@(_BitCodeFile)"
272-
Arguments="&quot;@$(_EmccDefaultFlagsRsp)&quot; &quot;@$(_EmccCompileRsp)&quot;"
273-
EnvironmentVariables="@(EmscriptenEnvVars)" />
279+
Arguments="&quot;@$(_EmccDefaultFlagsRsp)&quot; @(_EmccLDFlags, ' ')"
280+
EnvironmentVariables="@(EmscriptenEnvVars)"
281+
OutputMessageImportance="$(_EmccCompileOutputMessageImportance)" />
274282

275283
<ItemGroup>
276284
<!-- order seems to matter -->

src/tasks/WasmAppBuilder/EmccCompile.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class EmccCompile : Microsoft.Build.Utilities.Task
3333
public bool DisableParallelCompile { get; set; }
3434
public string Arguments { get; set; } = string.Empty;
3535
public string? WorkingDirectory { get; set; }
36+
public string OutputMessageImportance{ get; set; } = "Low";
3637

3738
[Output]
3839
public ITaskItem[]? OutputFiles { get; private set; }
@@ -54,6 +55,12 @@ public override bool Execute()
5455
return false;
5556
}
5657

58+
if (!Enum.TryParse(OutputMessageImportance, ignoreCase: true, out MessageImportance messageImportance))
59+
{
60+
Log.LogError($"Invalid value for OutputMessageImportance={OutputMessageImportance}. Valid values: {string.Join(", ", Enum.GetNames(typeof(MessageImportance)))}");
61+
return false;
62+
}
63+
5764
IDictionary<string, string> envVarsDict = GetEnvironmentVariablesDict();
5865
ConcurrentBag<ITaskItem> outputItems = new();
5966
try
@@ -112,7 +119,7 @@ bool ProcessSourceFile(ITaskItem srcItem)
112119

113120
try
114121
{
115-
string command = $"emcc {Arguments} -c -o {objFile} {srcFile}";
122+
string command = $"emcc {Arguments} -c -o \"{objFile}\" \"{srcFile}\"";
116123

117124
// Log the command in a compact format which can be copy pasted
118125
StringBuilder envStr = new StringBuilder(string.Empty);
@@ -125,7 +132,7 @@ bool ProcessSourceFile(ITaskItem srcItem)
125132
envVarsDict,
126133
workingDir: Environment.CurrentDirectory,
127134
logStdErrAsMessage: true,
128-
debugMessageImportance: MessageImportance.High,
135+
debugMessageImportance: messageImportance,
129136
label: Path.GetFileName(srcFile));
130137

131138
if (exitCode != 0)

src/tests/BuildWasmApps/Wasm.Build.Tests/BlazorWasmTests.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ public BlazorWasmTests(ITestOutputHelper output, SharedBuildPerTestClassFixture
1616
{
1717
}
1818

19-
[ConditionalFact(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
20-
public void PublishTemplateProject()
19+
// TODO: invariant case?
20+
21+
[ConditionalTheory(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
22+
[InlineData("Debug", false)]
23+
[InlineData("Debug", true)] // just aot
24+
[InlineData("Release", false)] // should re-link
25+
[InlineData("Release", true)]
26+
public void PublishTemplateProject(string config, bool aot)
2127
{
22-
string id = "blazorwasm";
28+
string id = $"blazorwasm_{config}_aot_{aot}";
2329
InitPaths(id);
2430
if (Directory.Exists(_projectDir))
2531
Directory.Delete(_projectDir, recursive: true);
@@ -37,14 +43,16 @@ public void PublishTemplateProject()
3743
.ExecuteWithCapturedOutput("new blazorwasm")
3844
.EnsureSuccessful();
3945

40-
string publishLogPath = Path.Combine(logPath, $"{id}.publish.binlog");
46+
string publishLogPath = Path.Combine(logPath, $"{id}.binlog");
4147
new DotNetCommand(s_buildEnv)
4248
.WithWorkingDirectory(_projectDir)
43-
.ExecuteWithCapturedOutput("publish", $"-bl:{publishLogPath}", "-p:RunAOTCompilation=true")
49+
.ExecuteWithCapturedOutput("publish", $"-bl:{publishLogPath}", aot ? "-p:RunAOTCompilation=true" : "", $"-p:Configuration={config}")
4450
.EnsureSuccessful();
4551

4652
//TODO: validate the build somehow?
4753
// compare dotnet.wasm?
54+
// relinking - dotnet.wasm should be smaller
55+
//
4856
// playwright?
4957
}
5058
}

src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ protected static BuildArgs ExpandBuildArgs(BuildArgs buildArgs, string extraProp
332332
}
333333

334334
if (useCache)
335-
{
336335
_buildContext.CacheBuild(buildArgs, new BuildProduct(_projectDir, logFilePath, true));
337-
Console.WriteLine($"caching build for {buildArgs}");
338-
}
339336

340337
return (_projectDir, result.buildOutput);
341338
}

src/tests/BuildWasmApps/Wasm.Build.Tests/NativeBuildTests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.IO;
65
using Xunit;
76
using Xunit.Abstractions;
7+
using Xunit.Sdk;
88

99
#nullable enable
1010

@@ -43,5 +43,37 @@ private void NativeBuild(string projectNamePrefix, string projectContents, Build
4343
test: output => {},
4444
host: host, id: id);
4545
}
46+
47+
[Theory]
48+
[BuildAndRun(host: RunHost.None, aot: true)]
49+
public void IntermediateBitcodeToObjectFilesAreNotLLVMIR(BuildArgs buildArgs, string id)
50+
{
51+
string printFileTypeTarget = @"
52+
<Target Name=""PrintIntermediateFileType"" AfterTargets=""WasmBuildApp"">
53+
<Exec Command=""wasm-dis $(_WasmIntermediateOutputPath)System.Private.CoreLib.dll.o -o $(_WasmIntermediateOutputPath)wasm-dis-out.txt""
54+
ConsoleToMSBuild=""true""
55+
EnvironmentVariables=""@(EmscriptenEnvVars)""
56+
IgnoreExitCode=""true"">
57+
58+
<Output TaskParameter=""ExitCode"" PropertyName=""ExitCode"" />
59+
</Exec>
60+
61+
<Message Text=""wasm-dis exit code: $(ExitCode)"" Importance=""High"" />
62+
</Target>
63+
";
64+
string projectName = $"bc_to_o_{buildArgs.Config}";
65+
66+
buildArgs = buildArgs with { ProjectName = projectName };
67+
buildArgs = ExpandBuildArgs(buildArgs, insertAtEnd: printFileTypeTarget);
68+
69+
(_, string output) = BuildProject(buildArgs,
70+
initProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), s_mainReturns42),
71+
dotnetWasmFromRuntimePack: false,
72+
id: id);
73+
74+
if (!output.Contains("wasm-dis exit code: 0"))
75+
throw new XunitException($"Expected to successfully run wasm-dis on System.Private.CoreLib.dll.o ."
76+
+ " It might fail if it was incorrectly compiled to a bitcode file, instead of wasm.");
77+
}
4678
}
4779
}

src/tests/BuildWasmApps/Wasm.Build.Tests/SharedBuildPerTestClassFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private void RemoveDirectory(string path)
4444
{
4545
try
4646
{
47-
Directory.Delete(path, recursive: true);
47+
Directory.Delete(path, recursive: true);
4848
}
4949
catch (Exception ex)
5050
{

src/tests/BuildWasmApps/Wasm.Build.Tests/ToolCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private async Task<CommandResult> ExecuteAsyncInternal(string executable, string
8888
return;
8989

9090
output.Add($"[{_label}] {e.Data}");
91+
Console.WriteLine($"[{_label}] {e.Data}");
9192
ErrorDataReceived?.Invoke(s, e);
9293
};
9394

@@ -97,6 +98,7 @@ private async Task<CommandResult> ExecuteAsyncInternal(string executable, string
9798
return;
9899

99100
output.Add($"[{_label}] {e.Data}");
101+
Console.WriteLine($"[{_label}] {e.Data}");
100102
OutputDataReceived?.Invoke(s, e);
101103
};
102104

0 commit comments

Comments
 (0)