Skip to content

Commit f1bc9fa

Browse files
committed
Use a build traversal project template instead of copying csproj template.
1 parent 4edac27 commit f1bc9fa

File tree

13 files changed

+49
-49
lines changed

13 files changed

+49
-49
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.Build.Traversal/4.1">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="$CSPROJPATH$">
5+
<Properties>TargetFramework=$TFM$</Properties>
6+
</ProjectReference>
7+
</ItemGroup>
8+
9+
</Project>

src/BenchmarkDotNet/Toolchains/ArtifactsPaths.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ArtifactsPaths
1313
[PublicAPI] public string ProgramCodePath { get; }
1414
[PublicAPI] public string AppConfigPath { get; }
1515
[PublicAPI] public string NuGetConfigPath { get; }
16-
[PublicAPI] public string BuildForReferencesProjectFilePath { get; }
16+
[PublicAPI] public string BuildTraversalProjectFilePath { get; }
1717
[PublicAPI] public string ProjectFilePath { get; }
1818
[PublicAPI] public string BuildScriptFilePath { get; }
1919
[PublicAPI] public string ExecutablePath { get; }
@@ -28,7 +28,7 @@ public ArtifactsPaths(
2828
string programCodePath,
2929
string appConfigPath,
3030
string nuGetConfigPath,
31-
string buildForReferencesProjectFilePath,
31+
string buildTraversalProjectFilePath,
3232
string projectFilePath,
3333
string buildScriptFilePath,
3434
string executablePath,
@@ -42,7 +42,7 @@ public ArtifactsPaths(
4242
ProgramCodePath = programCodePath;
4343
AppConfigPath = appConfigPath;
4444
NuGetConfigPath = nuGetConfigPath;
45-
BuildForReferencesProjectFilePath = buildForReferencesProjectFilePath;
45+
BuildTraversalProjectFilePath = buildTraversalProjectFilePath;
4646
ProjectFilePath = projectFilePath;
4747
BuildScriptFilePath = buildScriptFilePath;
4848
ExecutablePath = executablePath;

src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected override string GetProjectFilePath(string buildArtifactsDirectoryPath)
6262
=> Path.Combine(buildArtifactsDirectoryPath, "BenchmarkDotNet.Autogenerated.csproj");
6363

6464
protected override string GetProjectFilePathForReferences(string buildArtifactsDirectoryPath)
65-
=> Path.Combine(buildArtifactsDirectoryPath, "BenchmarkDotNet.Autogenerated.ForReferences.csproj");
65+
=> Path.Combine(buildArtifactsDirectoryPath, "BuildTraversal.buildproj");
6666

6767
protected override string GetBinariesDirectoryPath(string buildArtifactsDirectoryPath, string configuration)
6868
=> Path.Combine(buildArtifactsDirectoryPath, "bin", configuration, TargetFrameworkMoniker);
@@ -71,8 +71,8 @@ protected override string GetIntermediateDirectoryPath(string buildArtifactsDire
7171
=> Path.Combine(buildArtifactsDirectoryPath, "obj", configuration, TargetFrameworkMoniker);
7272

7373
[SuppressMessage("ReSharper", "StringLiteralTypo")] // R# complains about $variables$
74-
private string LoadCsProj(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, string projectFile, string customProperties, string sdkName)
75-
=> new StringBuilder(ResourceHelper.LoadTemplate("CsProj.txt"))
74+
private string LoadCsProj(string template, BuildPartition buildPartition, ArtifactsPaths artifactsPaths, string projectFile, string customProperties, string sdkName)
75+
=> new StringBuilder(ResourceHelper.LoadTemplate(template))
7676
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
7777
.Replace("$CODEFILENAME$", Path.GetFileName(artifactsPaths.ProgramCodePath))
7878
.Replace("$CSPROJPATH$", projectFile)
@@ -88,35 +88,35 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
8888
{
8989
var projectFile = GetProjectFilePath(buildPartition.RepresentativeBenchmarkCase.Descriptor.Type, logger);
9090

91+
GenerateBuildTraversalProject(artifactsPaths, projectFile.FullName);
92+
9193
var xmlDoc = new XmlDocument();
9294
xmlDoc.Load(projectFile.FullName);
9395
var (customProperties, sdkName) = GetSettingsThatNeedToBeCopied(xmlDoc, projectFile);
9496

95-
GenerateBuildForReferencesProject(buildPartition, artifactsPaths, projectFile.FullName, customProperties, sdkName);
96-
97-
var content = LoadCsProj(buildPartition, artifactsPaths, projectFile.FullName, customProperties, sdkName);
97+
var content = new StringBuilder(ResourceHelper.LoadTemplate("CsProj.txt"))
98+
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
99+
.Replace("$CODEFILENAME$", Path.GetFileName(artifactsPaths.ProgramCodePath))
100+
.Replace("$CSPROJPATH$", projectFile.FullName)
101+
.Replace("$TFM$", TargetFrameworkMoniker)
102+
.Replace("$PROGRAMNAME$", artifactsPaths.ProgramName)
103+
.Replace("$RUNTIMESETTINGS$", GetRuntimeSettings(buildPartition.RepresentativeBenchmarkCase.Job.Environment.Gc, buildPartition.Resolver))
104+
.Replace("$COPIEDSETTINGS$", customProperties)
105+
.Replace("$CONFIGURATIONNAME$", buildPartition.BuildConfiguration)
106+
.Replace("$SDKNAME$", sdkName)
107+
.ToString();
98108

99109
File.WriteAllText(artifactsPaths.ProjectFilePath, content);
100110
}
101111

102-
protected void GenerateBuildForReferencesProject(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, string projectFile, string customProperties, string sdkName)
112+
protected void GenerateBuildTraversalProject(ArtifactsPaths artifactsPaths, string projectFilePath)
103113
{
104-
var content = LoadCsProj(buildPartition, artifactsPaths, projectFile, customProperties, sdkName);
105-
106-
// We don't include the generated .notcs file when building the reference dlls, only in the final build.
107-
var xmlDoc = new XmlDocument();
108-
xmlDoc.Load(new StringReader(content));
109-
XmlElement projectElement = xmlDoc.DocumentElement;
110-
projectElement.RemoveChild(projectElement.SelectSingleNode("ItemGroup/Compile").ParentNode);
111-
112-
var startupObjectElement = projectElement.SelectSingleNode("PropertyGroup/StartupObject");
113-
startupObjectElement.ParentNode.RemoveChild(startupObjectElement);
114-
115-
// We need to change the output type to library since we're only compiling for dlls.
116-
var outputTypeElement = projectElement.SelectSingleNode("PropertyGroup/OutputType");
117-
outputTypeElement.InnerText = "Library";
114+
var content = new StringBuilder(ResourceHelper.LoadTemplate("BuildTraversalProj.txt"))
115+
.Replace("$CSPROJPATH$", projectFilePath)
116+
.Replace("$TFM$", TargetFrameworkMoniker)
117+
.ToString();
118118

119-
xmlDoc.Save(artifactsPaths.BuildForReferencesProjectFilePath);
119+
File.WriteAllText(artifactsPaths.BuildTraversalProjectFilePath, content);
120120
}
121121

122122
/// <summary>

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliBuilder.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public DotNetCliBuilder(string targetFrameworkMoniker, string? customDotNetCliPa
2828
public BuildResult Build(GenerateResult generateResult, BuildPartition buildPartition, ILogger logger)
2929
{
3030
var cliCommand = new DotNetCliCommand(
31-
generateResult.ArtifactsPaths.BuildForReferencesProjectFilePath,
31+
generateResult.ArtifactsPaths.BuildTraversalProjectFilePath,
3232
CustomDotNetCliPath,
3333
string.Empty,
3434
generateResult,
@@ -75,11 +75,6 @@ internal static void GatherReferences(ArtifactsPaths artifactsPaths)
7575
foreach (var assemblyFile in Directory.GetFiles(artifactsPaths.BinariesDirectoryPath, "*.dll"))
7676
{
7777
var assemblyName = Path.GetFileNameWithoutExtension(assemblyFile);
78-
// The dummy csproj was used to build the original project, but it also outputs a dll for itself which we need to ignore because it's not valid.
79-
if (assemblyName == artifactsPaths.ProgramName)
80-
{
81-
continue;
82-
}
8378
var referenceElement = xmlDoc.CreateElement("Reference");
8479
itemGroup.AppendChild(referenceElement);
8580
referenceElement.SetAttribute("Include", assemblyName);

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ protected override void CopyAllRequiredFiles(ArtifactsPaths artifactsPaths)
101101
protected override void GenerateBuildScript(BuildPartition buildPartition, ArtifactsPaths artifactsPaths)
102102
{
103103
var content = new StringBuilder(300)
104-
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition, artifactsPaths.BuildForReferencesProjectFilePath)}")
105-
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetBuildCommand(artifactsPaths, buildPartition, artifactsPaths.BuildForReferencesProjectFilePath)}")
104+
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition, artifactsPaths.BuildTraversalProjectFilePath)}")
105+
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetBuildCommand(artifactsPaths, buildPartition, artifactsPaths.BuildTraversalProjectFilePath)}")
106106
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition, artifactsPaths.ProjectFilePath)}")
107107
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetBuildCommand(artifactsPaths, buildPartition, artifactsPaths.ProjectFilePath)}")
108108
.ToString();

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliPublisher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public DotNetCliPublisher(
2727
public BuildResult Build(GenerateResult generateResult, BuildPartition buildPartition, ILogger logger)
2828
{
2929
var cliCommand = new DotNetCliCommand(
30-
generateResult.ArtifactsPaths.BuildForReferencesProjectFilePath,
30+
generateResult.ArtifactsPaths.BuildTraversalProjectFilePath,
3131
CustomDotNetCliPath,
3232
ExtraArguments,
3333
generateResult,

src/BenchmarkDotNet/Toolchains/GeneratorBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private ArtifactsPaths GetArtifactsPaths(BuildPartition buildPartition, string r
150150
appConfigPath: $"{executablePath}.config",
151151
nuGetConfigPath: Path.Combine(buildArtifactsDirectoryPath, "NuGet.config"),
152152
projectFilePath: GetProjectFilePath(buildArtifactsDirectoryPath),
153-
buildForReferencesProjectFilePath: GetProjectFilePathForReferences(buildArtifactsDirectoryPath),
153+
buildTraversalProjectFilePath: GetProjectFilePathForReferences(buildArtifactsDirectoryPath),
154154
buildScriptFilePath: Path.Combine(buildArtifactsDirectoryPath, $"{programName}{OsDetector.ScriptFileExtension}"),
155155
executablePath: executablePath,
156156
programName: programName,

src/BenchmarkDotNet/Toolchains/InProcess/Emit/InProcessEmitArtifactsPath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public InProcessEmitArtifactsPath(
1717
baseArtifacts.AppConfigPath,
1818
baseArtifacts.NuGetConfigPath,
1919
baseArtifacts.ProjectFilePath,
20-
baseArtifacts.BuildForReferencesProjectFilePath,
20+
baseArtifacts.BuildTraversalProjectFilePath,
2121
baseArtifacts.BuildScriptFilePath,
2222
baseArtifacts.ExecutablePath,
2323
baseArtifacts.ProgramName,

src/BenchmarkDotNet/Toolchains/InProcess/Emit/InProcessEmitGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private ArtifactsPaths GetArtifactsPaths(BuildPartition buildPartition, string r
5252
appConfigPath: null,
5353
nuGetConfigPath: null,
5454
projectFilePath: null,
55-
buildForReferencesProjectFilePath: null,
55+
buildTraversalProjectFilePath: null,
5656
buildScriptFilePath: null,
5757
executablePath: executablePath,
5858
programName: programName,

src/BenchmarkDotNet/Toolchains/Mono/MonoPublisher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public MonoPublisher(string customDotNetCliPath)
2727
public BuildResult Build(GenerateResult generateResult, BuildPartition buildPartition, ILogger logger)
2828
{
2929
var cliCommand = new DotNetCliCommand(
30-
generateResult.ArtifactsPaths.BuildForReferencesProjectFilePath,
30+
generateResult.ArtifactsPaths.BuildTraversalProjectFilePath,
3131
CustomDotNetCliPath,
3232
string.Empty,
3333
generateResult,

src/BenchmarkDotNet/Toolchains/MonoAotLLVM/MonoAotLLVMGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
3030
BenchmarkCase benchmark = buildPartition.RepresentativeBenchmarkCase;
3131
var projectFile = GetProjectFilePath(benchmark.Descriptor.Type, logger);
3232

33+
GenerateBuildTraversalProject(artifactsPaths, projectFile.FullName);
34+
3335
string useLLVM = AotCompilerMode == MonoAotCompilerMode.llvm ? "true" : "false";
3436

3537
var xmlDoc = new XmlDocument();
3638
xmlDoc.Load(projectFile.FullName);
3739
var (customProperties, sdkName) = GetSettingsThatNeedToBeCopied(xmlDoc, projectFile);
3840

39-
GenerateBuildForReferencesProject(buildPartition, artifactsPaths, projectFile.FullName, customProperties, sdkName);
40-
4141
string content = new StringBuilder(ResourceHelper.LoadTemplate("MonoAOTLLVMCsProj.txt"))
4242
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
4343
.Replace("$CODEFILENAME$", Path.GetFileName(artifactsPaths.ProgramCodePath))

src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ protected void GenerateProjectFile(BuildPartition buildPartition, ArtifactsPaths
4343
BenchmarkCase benchmark = buildPartition.RepresentativeBenchmarkCase;
4444
var projectFile = GetProjectFilePath(benchmark.Descriptor.Type, logger);
4545

46+
GenerateBuildTraversalProject(artifactsPaths, projectFile.FullName);
47+
4648
WasmRuntime runtime = (WasmRuntime) buildPartition.Runtime;
4749

4850
var xmlDoc = new XmlDocument();
4951
xmlDoc.Load(projectFile.FullName);
5052
var (customProperties, sdkName) = GetSettingsThatNeedToBeCopied(xmlDoc, projectFile);
5153

52-
GenerateBuildForReferencesProject(buildPartition, artifactsPaths, projectFile.FullName, customProperties, sdkName);
53-
5454
string content = new StringBuilder(ResourceHelper.LoadTemplate("WasmCsProj.txt"))
5555
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
5656
.Replace("$CODEFILENAME$", Path.GetFileName(artifactsPaths.ProgramCodePath))

src/BenchmarkDotNet/Toolchains/NativeAot/Generator.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ protected override void GenerateBuildScript(BuildPartition buildPartition, Artif
7474
string extraArguments = NativeAotToolchain.GetExtraArguments(runtimeIdentifier);
7575

7676
var content = new StringBuilder(300)
77-
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition, artifactsPaths.BuildForReferencesProjectFilePath, extraArguments)}")
78-
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetPublishCommand(artifactsPaths, buildPartition, artifactsPaths.BuildForReferencesProjectFilePath, extraArguments)}")
77+
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition, artifactsPaths.BuildTraversalProjectFilePath, extraArguments)}")
78+
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetPublishCommand(artifactsPaths, buildPartition, artifactsPaths.BuildTraversalProjectFilePath, extraArguments)}")
7979
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition, artifactsPaths.ProjectFilePath, extraArguments)}")
8080
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetPublishCommand(artifactsPaths, buildPartition, artifactsPaths.ProjectFilePath, extraArguments)}")
8181
.ToString();
@@ -116,11 +116,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
116116
{
117117
var projectFile = GetProjectFilePath(buildPartition.RepresentativeBenchmarkCase.Descriptor.Type, logger);
118118

119-
var xmlDoc = new XmlDocument();
120-
xmlDoc.Load(projectFile.FullName);
121-
var (customProperties, sdkName) = GetSettingsThatNeedToBeCopied(xmlDoc, projectFile);
122-
123-
GenerateBuildForReferencesProject(buildPartition, artifactsPaths, projectFile.FullName, customProperties, sdkName);
119+
GenerateBuildTraversalProject(artifactsPaths, projectFile.FullName);
124120

125121
File.WriteAllText(artifactsPaths.ProjectFilePath, GenerateProjectForNuGetBuild(buildPartition, artifactsPaths, logger));
126122
GenerateReflectionFile(artifactsPaths);

0 commit comments

Comments
 (0)