/
Generator.cs
221 lines (199 loc) · 11.4 KB
/
Generator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CsProj;
using BenchmarkDotNet.Toolchains.DotNetCli;
namespace BenchmarkDotNet.Toolchains.CoreRt
{
/// <summary>
/// generates new csproj file for self-contained .NET Core RT app
/// based on https://github.com/dotnet/corert/blob/7f902d4d8b1c3280e60f5e06c71951a60da173fb/Documentation/how-to-build-and-run-ilcompiler-in-console-shell-prompt.md#compiling-source-to-native-code-using-the-ilcompiler-you-built
/// and https://github.com/dotnet/corert/tree/7f902d4d8b1c3280e60f5e06c71951a60da173fb/samples/HelloWorld#add-corert-to-your-project
/// </summary>
public class Generator : CsProjGenerator
{
internal const string CoreRtNuGetFeed = "coreRtNuGetFeed";
internal Generator(string coreRtVersion, bool useCppCodeGenerator,
string runtimeFrameworkVersion, string targetFrameworkMoniker, string cliPath,
string runtimeIdentifier, IReadOnlyDictionary<string, string> feeds, bool useNuGetClearTag,
bool useTempFolderForRestore, string packagesRestorePath,
bool rootAllApplicationAssemblies, bool ilcGenerateCompleteTypeMetadata, bool ilcGenerateStackTraceData)
: base(targetFrameworkMoniker, cliPath, GetPackagesDirectoryPath(useTempFolderForRestore, packagesRestorePath), runtimeFrameworkVersion)
{
this.coreRtVersion = coreRtVersion;
this.useCppCodeGenerator = useCppCodeGenerator;
this.targetFrameworkMoniker = targetFrameworkMoniker;
this.runtimeIdentifier = runtimeIdentifier;
this.feeds = feeds;
this.useNuGetClearTag = useNuGetClearTag;
this.useTempFolderForRestore = useTempFolderForRestore;
this.rootAllApplicationAssemblies = rootAllApplicationAssemblies;
this.ilcGenerateCompleteTypeMetadata = ilcGenerateCompleteTypeMetadata;
this.ilcGenerateStackTraceData = ilcGenerateStackTraceData;
}
private readonly string coreRtVersion;
private readonly bool useCppCodeGenerator;
[SuppressMessage("ReSharper", "NotAccessedField.Local")]
private readonly string targetFrameworkMoniker;
private readonly string runtimeIdentifier;
private readonly IReadOnlyDictionary<string, string> feeds;
private readonly bool useNuGetClearTag;
private readonly bool useTempFolderForRestore;
private readonly bool rootAllApplicationAssemblies;
private readonly bool ilcGenerateCompleteTypeMetadata;
private readonly bool ilcGenerateStackTraceData;
private bool IsNuGetCoreRt => feeds.ContainsKey(CoreRtNuGetFeed) && !string.IsNullOrWhiteSpace(coreRtVersion);
protected override string GetExecutableExtension() => RuntimeInformation.ExecutableExtension;
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programName)
=> useTempFolderForRestore
? Path.Combine(Path.GetTempPath(), programName) // store everything in temp to avoid collisions with IDE
: base.GetBuildArtifactsDirectoryPath(buildPartition, programName);
protected override string GetBinariesDirectoryPath(string buildArtifactsDirectoryPath, string configuration)
=> Path.Combine(buildArtifactsDirectoryPath, "bin", configuration, TargetFrameworkMoniker, runtimeIdentifier, "publish");
protected override void GenerateBuildScript(BuildPartition buildPartition, ArtifactsPaths artifactsPaths)
{
string extraArguments = useCppCodeGenerator ? $"-r {runtimeIdentifier} /p:NativeCodeGen=cpp" : $"-r {runtimeIdentifier}";
var content = new StringBuilder(300)
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition)} {extraArguments}")
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetBuildCommand(artifactsPaths, buildPartition)} {extraArguments}")
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetPublishCommand(artifactsPaths, buildPartition)} {extraArguments}")
.ToString();
File.WriteAllText(artifactsPaths.BuildScriptFilePath, content);
}
// we always want to have a new directory for NuGet packages restore
// to avoid this https://github.com/dotnet/coreclr/blob/master/Documentation/workflow/UsingDotNetCli.md#update-coreclr-using-runtime-nuget-package
// some of the packages are going to contain source code, so they can not be in the subfolder of current solution
// otherwise they would be compiled too (new .csproj include all .cs files from subfolders by default
private static string GetPackagesDirectoryPath(bool useTempFolderForRestore, string packagesRestorePath)
=> packagesRestorePath ?? (useTempFolderForRestore ? Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) : null);
protected override string[] GetArtifactsToCleanup(ArtifactsPaths artifactsPaths)
=> useTempFolderForRestore
? base.GetArtifactsToCleanup(artifactsPaths).Concat(new[] { artifactsPaths.PackagesDirectoryName }).ToArray()
: base.GetArtifactsToCleanup(artifactsPaths);
protected override void GenerateNuGetConfig(ArtifactsPaths artifactsPaths)
{
if (!feeds.Any())
return;
string content =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
{(useNuGetClearTag ? "<clear/>" : string.Empty)}
{string.Join(Environment.NewLine + " ", feeds.Select(feed => $"<add key=\"{feed.Key}\" value=\"{feed.Value}\" />"))}
</packageSources>
</configuration>";
File.WriteAllText(artifactsPaths.NuGetConfigPath, content);
}
protected override void GenerateProject(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger)
{
File.WriteAllText(artifactsPaths.ProjectFilePath,
IsNuGetCoreRt
? GenerateProjectForNuGetBuild(buildPartition, artifactsPaths, logger)
: GenerateProjectForLocalBuild(buildPartition, artifactsPaths, logger));
GenerateReflectionFile(artifactsPaths);
}
private string GenerateProjectForNuGetBuild(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger) => $@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<ImportDirectoryBuildProps>false</ImportDirectoryBuildProps>
<ImportDirectoryBuildTargets>false</ImportDirectoryBuildTargets>
<OutputType>Exe</OutputType>
<TargetFramework>{TargetFrameworkMoniker}</TargetFramework>
<RuntimeIdentifier>{runtimeIdentifier}</RuntimeIdentifier>
<RuntimeFrameworkVersion>{RuntimeFrameworkVersion}</RuntimeFrameworkVersion>
<AssemblyName>{artifactsPaths.ProgramName}</AssemblyName>
<AssemblyTitle>{artifactsPaths.ProgramName}</AssemblyTitle>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<UseSharedCompilation>false</UseSharedCompilation>
<Deterministic>true</Deterministic>
{GetTrimmingSettings()}
<IlcGenerateCompleteTypeMetadata>{ilcGenerateCompleteTypeMetadata}</IlcGenerateCompleteTypeMetadata>
<IlcGenerateStackTraceData>{ilcGenerateStackTraceData}</IlcGenerateStackTraceData>
<EnsureNETCoreAppRuntime>false</EnsureNETCoreAppRuntime> <!-- workaround for 'This runtime may not be supported by.NET Core.' error -->
</PropertyGroup>
{GetRuntimeSettings(buildPartition.RepresentativeBenchmarkCase.Job.Environment.Gc, buildPartition.Resolver)}
<ItemGroup>
<Compile Include=""{Path.GetFileName(artifactsPaths.ProgramCodePath)}"" Exclude=""bin\**;obj\**;**\*.xproj;packages\**"" />
</ItemGroup>
<ItemGroup>
<PackageReference Include=""Microsoft.DotNet.ILCompiler"" Version=""{coreRtVersion}"" />
<ProjectReference Include=""{GetProjectFilePath(buildPartition.RepresentativeBenchmarkCase.Descriptor.Type, logger).FullName}"" />
</ItemGroup>
<ItemGroup>
<RdXmlFile Include=""rd.xml"" />
</ItemGroup>
</Project>";
private string GenerateProjectForLocalBuild(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger) => $@"
<Project>
<Import Project=""$(MSBuildSDKsPath)\Microsoft.NET.Sdk\Sdk\Sdk.props"" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>{TargetFrameworkMoniker}</TargetFramework>
<RuntimeIdentifier>{runtimeIdentifier}</RuntimeIdentifier>
<RuntimeFrameworkVersion>{RuntimeFrameworkVersion}</RuntimeFrameworkVersion>
<AssemblyName>{artifactsPaths.ProgramName}</AssemblyName>
<AssemblyTitle>{artifactsPaths.ProgramName}</AssemblyTitle>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<UseSharedCompilation>false</UseSharedCompilation>
<Deterministic>true</Deterministic>
{GetTrimmingSettings()}
<IlcGenerateCompleteTypeMetadata>{ilcGenerateCompleteTypeMetadata}</IlcGenerateCompleteTypeMetadata>
<IlcGenerateStackTraceData>{ilcGenerateStackTraceData}</IlcGenerateStackTraceData>
</PropertyGroup>
<Import Project=""$(MSBuildSDKsPath)\Microsoft.NET.Sdk\Sdk\Sdk.targets"" />
<Import Project=""$(IlcPath)\build\Microsoft.NETCore.Native.targets"" />
{GetRuntimeSettings(buildPartition.RepresentativeBenchmarkCase.Job.Environment.Gc, buildPartition.Resolver)}
<ItemGroup>
<Compile Include=""{Path.GetFileName(artifactsPaths.ProgramCodePath)}"" Exclude=""bin\**;obj\**;**\*.xproj;packages\**"" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include=""{GetProjectFilePath(buildPartition.RepresentativeBenchmarkCase.Descriptor.Type, logger).FullName}"" />
</ItemGroup>
<ItemGroup>
<RdXmlFile Include=""rd.xml"" />
</ItemGroup>
</Project>";
private string GetTrimmingSettings()
=> rootAllApplicationAssemblies
? "<PublishTrimmed>false</PublishTrimmed>"
: "<TrimMode>link</TrimMode>";
/// <summary>
/// mandatory to make it possible to call GC.GetAllocatedBytesForCurrentThread() using reflection (not part of .NET Standard)
/// </summary>
private void GenerateReflectionFile(ArtifactsPaths artifactsPaths)
{
const string content = @"
<Directives>
<Application>
<Assembly Name=""System.Runtime"">
<Type Name=""System.GC"" Dynamic=""Required All"" />
</Assembly>
<Assembly Name=""System.Threading.ThreadPool"">
<Type Name=""System.Threading.ThreadPool"" Dynamic=""Required All"" />
</Assembly>
<Assembly Name=""System.Threading"">
<Type Name=""System.Threading.Monitor"" Dynamic=""Required All"" />
</Assembly>
</Application>
</Directives>
";
string directoryName = Path.GetDirectoryName(artifactsPaths.ProjectFilePath);
if (directoryName != null)
File.WriteAllText(Path.Combine(directoryName, "rd.xml"), content);
else
throw new InvalidOperationException($"Can't get directory of projectFilePath ('{artifactsPaths.ProjectFilePath}')");
}
}
}