Skip to content

Commit fa59841

Browse files
authored
[wasm] Improve the autogenerated project (#1936)
* Remove `runtimeSrcDir` command line parameter used by wasm * [wasm] Update the autogenerated project - Use a single template project file - Automatically import any `$projectName.Wasm.props`, and `$projectName.Wasm.targets` files, if found. - This allows the actual project to control settings for the generated project, eg, for modifying the wasm build to add some files to the wasm vfs.
1 parent ca51035 commit fa59841

File tree

6 files changed

+27
-121
lines changed

6 files changed

+27
-121
lines changed

src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs

-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ public class CommandLineOptions
187187
[Option("AOTCompilerMode", Required = false, Default = MonoAotCompilerMode.mini, HelpText = "Mono AOT compiler mode, either 'mini' or 'llvm'")]
188188
public MonoAotCompilerMode AOTCompilerMode { get; set; }
189189

190-
[Option("runtimeSrcDir", Required = false, HelpText = "Path to a local copy of dotnet/runtime. . Used by the WASM toolchain when AOTCompilerMode is 'wasm'.")]
191-
public DirectoryInfo RuntimeSrcDir { get; set; }
192-
193190
internal bool UserProvidedFilters => Filters.Any() || AttributeNames.Any() || AllCategories.Any() || AnyCategories.Any();
194191

195192
[Usage(ApplicationAlias = "")]

src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs

-22
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,6 @@ private static Parser CreateParser(ILogger logger)
9797
settings.MaximumDisplayWidth = Math.Max(MinimumDisplayWidth, GetMaximumDisplayWidth());
9898
});
9999

100-
private static bool MonikerIsWasm(RuntimeMoniker moniker)
101-
102-
{
103-
switch (moniker)
104-
{
105-
case RuntimeMoniker.Wasm:
106-
case RuntimeMoniker.WasmNet50:
107-
case RuntimeMoniker.WasmNet60:
108-
case RuntimeMoniker.WasmNet70:
109-
return true;
110-
default:
111-
return false;
112-
}
113-
}
114-
115100
private static bool Validate(CommandLineOptions options, ILogger logger)
116101
{
117102
if (!AvailableJobs.ContainsKey(options.BaseJob))
@@ -131,12 +116,6 @@ private static bool Validate(CommandLineOptions options, ILogger logger)
131116
{
132117
logger.WriteLineError($"The provided {nameof(options.AOTCompilerPath)} \"{ options.AOTCompilerPath }\" does NOT exist. It MUST be provided.");
133118
}
134-
else if (MonikerIsWasm(runtimeMoniker) && (options.RuntimeSrcDir == null || options.RuntimeSrcDir.IsNotNullButDoesNotExist()))
135-
136-
{
137-
logger.WriteLineError($"The provided {nameof(options.RuntimeSrcDir)} \"{options.RuntimeSrcDir}\" does NOT exist. It MUST be provided for wasm-aot.");
138-
return false;
139-
}
140119
}
141120

142121
foreach (string exporter in options.Exporters)
@@ -447,7 +426,6 @@ private static Job MakeWasmJob(Job baseJob, CommandLineOptions options, string m
447426
javaScriptEngine: options.WasmJavascriptEngine?.FullName ?? "v8",
448427
javaScriptEngineArguments: options.WasmJavaScriptEngineArguments,
449428
aot: wasmAot,
450-
runtimeSrcDir: options.RuntimeSrcDir,
451429
moniker: moniker);
452430

453431
var toolChain = WasmToolChain.From(new NetCoreAppSettings(

src/BenchmarkDotNet/Environments/Runtimes/WasmRuntime.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public class WasmRuntime : Runtime, IEquatable<WasmRuntime>
1616

1717
public bool Aot { get; }
1818

19-
public DirectoryInfo RuntimeSrcDir { get; }
20-
2119
/// <summary>
2220
/// creates new instance of WasmRuntime
2321
/// </summary>
@@ -26,25 +24,23 @@ public class WasmRuntime : Runtime, IEquatable<WasmRuntime>
2624
/// <param name="msBuildMoniker">moniker, default: "net5.0"</param>
2725
/// <param name="displayName">default: "Wasm"</param>
2826
/// <param name="aot">Specifies whether AOT or Interpreter (default) project should be generated.</param>
29-
/// <param name="runtimeSrcDir">The path to runtime source directory.</param>
30-
public WasmRuntime(string msBuildMoniker = "net5.0", string displayName = "Wasm", string javaScriptEngine = "v8", string javaScriptEngineArguments = "--expose_wasm", bool aot = false, DirectoryInfo runtimeSrcDir = null, RuntimeMoniker moniker = RuntimeMoniker.Wasm) : base(moniker, msBuildMoniker, displayName)
27+
public WasmRuntime(string msBuildMoniker = "net5.0", string displayName = "Wasm", string javaScriptEngine = "v8", string javaScriptEngineArguments = "--expose_wasm", bool aot = false, RuntimeMoniker moniker = RuntimeMoniker.Wasm) : base(moniker, msBuildMoniker, displayName)
3128
{
3229
if (!string.IsNullOrEmpty(javaScriptEngine) && javaScriptEngine != "v8" && !File.Exists(javaScriptEngine))
3330
throw new FileNotFoundException($"Provided {nameof(javaScriptEngine)} file: \"{javaScriptEngine}\" doest NOT exist");
3431

3532
JavaScriptEngine = javaScriptEngine;
3633
JavaScriptEngineArguments = javaScriptEngineArguments;
3734
Aot = aot;
38-
RuntimeSrcDir = runtimeSrcDir;
3935
}
4036

4137
public override bool Equals(object obj)
4238
=> obj is WasmRuntime other && Equals(other);
4339

4440
public bool Equals(WasmRuntime other)
45-
=> other != null && base.Equals(other) && other.JavaScriptEngine == JavaScriptEngine && other.JavaScriptEngineArguments == JavaScriptEngineArguments && other.Aot == Aot && other.RuntimeSrcDir == RuntimeSrcDir;
41+
=> other != null && base.Equals(other) && other.JavaScriptEngine == JavaScriptEngine && other.JavaScriptEngineArguments == JavaScriptEngineArguments && other.Aot == Aot;
4642

4743
public override int GetHashCode()
48-
=> base.GetHashCode() ^ (JavaScriptEngine?.GetHashCode() ?? 0) ^ (JavaScriptEngineArguments?.GetHashCode() ?? 0 ^ Aot.GetHashCode() ^ (RuntimeSrcDir?.GetHashCode() ?? 0));
44+
=> base.GetHashCode() ^ (JavaScriptEngine?.GetHashCode() ?? 0) ^ (JavaScriptEngineArguments?.GetHashCode() ?? 0 ^ Aot.GetHashCode());
4945
}
5046
}

src/BenchmarkDotNet/Templates/WasmAotCsProj.txt

-45
This file was deleted.
+20-11
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
11
<Project Sdk="$SDKNAME$" DefaultTargets="publish">
2+
<PropertyGroup>
3+
<OriginalCSProjPath>$CSPROJPATH$</OriginalCSProjPath>
4+
<WasmPropsPath>$([System.IO.Path]::ChangeExtension('$(OriginalCSProjPath)', '.Wasm.props'))</WasmPropsPath>
5+
<WasmTargetsPath>$([System.IO.Path]::ChangeExtension('$(OriginalCSProjPath)', '.Wasm.targets'))</WasmTargetsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(WasmPropsPath)" Condition="Exists($(WasmPropsPath))" />
9+
210
<PropertyGroup>
311
<OutputType>Exe</OutputType>
412
<OutputPath>bin</OutputPath>
5-
<RuntimeSrcDir>$RUNTIMESRCDIR$</RuntimeSrcDir>
613
<RuntimeConfig>Release</RuntimeConfig>
714
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
815
<TargetFramework>$TFM$</TargetFramework>
916
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1017
<AppDir>$(MSBuildThisFileDirectory)\bin\$TFM$\browser-wasm\publish</AppDir>
1118
<AssemblyName>$PROGRAMNAME$</AssemblyName>
1219
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
13-
<PublishTrimmed>false</PublishTrimmed>
14-
<RunAOTCompilation>false</RunAOTCompilation>
15-
<WasmMainJSPath>$(RuntimeSrcDir)\src\mono\wasm\$MAINJS$</WasmMainJSPath>
20+
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
21+
<RunAOTCompilation>$RUN_AOT$</RunAOTCompilation>
22+
<PublishTrimmed>$(RunAOTCompilation)</PublishTrimmed>
1623
<WasmGenerateRunV8Script>true</WasmGenerateRunV8Script>
1724
<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>
18-
<WasmNativeWorkload>false</WasmNativeWorkload>
19-
<UsingBrowserRuntimeWorkload>false</UsingBrowserRuntimeWorkload>
25+
<EnableDefaultWasmAssembliesToBundle>false</EnableDefaultWasmAssembliesToBundle>
2026
$COPIEDSETTINGS$
2127
</PropertyGroup>
2228

23-
<Import Project="$(RuntimeSrcDir)/src/mono/wasm/build/WasmApp.LocalBuild.props" />
24-
<Import Project="$(RuntimeSrcDir)/src/mono/wasm/build/WasmApp.LocalBuild.targets" />
25-
2629
<ItemGroup>
2730
<Compile Include="$CODEFILENAME$" Exclude="bin\**;obj\**;**\*.xproj;packages\**" />
31+
<!-- move this too? -->
32+
<TrimmerRootDescriptor Include="WasmLinkerDescription.xml" Condition="'$(RunAOTCompilation)' == 'true'" />
2833
</ItemGroup>
2934

3035
<ItemGroup>
31-
<ProjectReference Include="$CSPROJPATH$" />
36+
<ProjectReference Include="$(OriginalCSProjPath)" />
3237
</ItemGroup>
3338

3439
<PropertyGroup>
3540
<WasmBuildAppAfterThisTarget>PrepareForWasmBuild</WasmBuildAppAfterThisTarget>
3641
</PropertyGroup>
42+
3743
<Target Name="PrepareForWasmBuild" AfterTargets="Publish">
3844
<ItemGroup>
39-
<WasmAssembliesToBundle Include="$(TargetDir)publish\*.dll" />
45+
<WasmAssembliesToBundle Include="$(TargetDir)publish\*.dll" Condition="'$(RunAOTCompilation)' != 'true'" />
46+
<WasmAssembliesToBundle Include="$(TargetDir)publish\*.dll" Exclude="$(TargetDir)publish\KernelTraceControl.dll" Condition="'$(RunAOTCompilation)' == 'true'" />
4047
</ItemGroup>
4148
</Target>
49+
50+
<Import Project="$(WasmTargetsPath)" Condition="Exists($(WasmTargetsPath))" />
4251
</Project>

src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs

+4-33
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,17 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
2727
{
2828
if (((WasmRuntime)buildPartition.Runtime).Aot)
2929
{
30-
GenerateProjectAot(buildPartition, artifactsPaths, logger);
30+
GenerateProjectFile(buildPartition, artifactsPaths, aot: true, logger);
3131

3232
var linkDescriptionFileName = "WasmLinkerDescription.xml";
3333
File.WriteAllText(Path.Combine(Path.GetDirectoryName(artifactsPaths.ProjectFilePath), linkDescriptionFileName), ResourceHelper.LoadTemplate(linkDescriptionFileName));
3434
} else
3535
{
36-
GenerateProjectInterpreter(buildPartition, artifactsPaths, logger);
36+
GenerateProjectFile(buildPartition, artifactsPaths, aot: false, logger);
3737
}
3838
}
3939

40-
protected void GenerateProjectAot(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger)
41-
{
42-
BenchmarkCase benchmark = buildPartition.RepresentativeBenchmarkCase;
43-
var projectFile = GetProjectFilePath(benchmark.Descriptor.Type, logger);
44-
45-
WasmRuntime runtime = (WasmRuntime)buildPartition.Runtime;
46-
47-
using (var file = new StreamReader(File.OpenRead(projectFile.FullName)))
48-
{
49-
var (customProperties, sdkName) = GetSettingsThatNeedsToBeCopied(file, projectFile);
50-
51-
string content = new StringBuilder(ResourceHelper.LoadTemplate("WasmAotCsProj.txt"))
52-
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
53-
.Replace("$CODEFILENAME$", Path.GetFileName(artifactsPaths.ProgramCodePath))
54-
.Replace("$CSPROJPATH$", projectFile.FullName)
55-
.Replace("$TFM$", TargetFrameworkMoniker)
56-
.Replace("$PROGRAMNAME$", artifactsPaths.ProgramName)
57-
.Replace("$RUNTIMESRCDIR$", runtime.RuntimeSrcDir.ToString())
58-
.Replace("$COPIEDSETTINGS$", customProperties)
59-
.Replace("$CONFIGURATIONNAME$", buildPartition.BuildConfiguration)
60-
.Replace("$SDKNAME$", sdkName)
61-
.Replace("$MAINJS$", MainJS)
62-
.ToString();
63-
64-
File.WriteAllText(artifactsPaths.ProjectFilePath, content);
65-
}
66-
}
67-
68-
protected void GenerateProjectInterpreter(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger)
40+
protected void GenerateProjectFile(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, bool aot, ILogger logger)
6941
{
7042
BenchmarkCase benchmark = buildPartition.RepresentativeBenchmarkCase;
7143
var projectFile = GetProjectFilePath(benchmark.Descriptor.Type, logger);
@@ -79,14 +51,13 @@ protected void GenerateProjectInterpreter(BuildPartition buildPartition, Artifac
7951
string content = new StringBuilder(ResourceHelper.LoadTemplate("WasmCsProj.txt"))
8052
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
8153
.Replace("$CODEFILENAME$", Path.GetFileName(artifactsPaths.ProgramCodePath))
54+
.Replace("$RUN_AOT$", aot.ToString().ToLower())
8255
.Replace("$CSPROJPATH$", projectFile.FullName)
8356
.Replace("$TFM$", TargetFrameworkMoniker)
8457
.Replace("$PROGRAMNAME$", artifactsPaths.ProgramName)
85-
.Replace("$RUNTIMESRCDIR$", runtime.RuntimeSrcDir.ToString())
8658
.Replace("$COPIEDSETTINGS$", customProperties)
8759
.Replace("$CONFIGURATIONNAME$", buildPartition.BuildConfiguration)
8860
.Replace("$SDKNAME$", sdkName)
89-
.Replace("$RUNTIMEPACK$", CustomRuntimePack ?? "")
9061
.Replace("$MAINJS$", MainJS)
9162
.Replace("$TARGET$", CustomRuntimePack != null ? "PublishWithCustomRuntimePack" : "Publish")
9263
.ToString();

0 commit comments

Comments
 (0)