Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions src/BenchmarkDotNet/Templates/BenchmarkDotNet.Build.props.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project>
<!-- Define properties for benchmark project. -->
<PropertyGroup Condition="'$(MSBuildProjectFile)' == '$BENCHMARK_PROJ$'">

</PropertyGroup>

<!-- Define properties for DllGatherer.csproj. -->
<PropertyGroup Condition="'$(MSBuildProjectFile)' == '$GATHERER_PROJ$'">

</PropertyGroup>

<!-- Define properties for BenchmarkDotNet.Autogenerated.csproj. -->
<PropertyGroup Condition="'$(MSBuildProjectFile)' == '$AUTOGENERATED_PROJ$'">

</PropertyGroup>
</Project>
16 changes: 16 additions & 0 deletions src/BenchmarkDotNet/Templates/BenchmarkDotNet.Build.targets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project>
<!-- Define properties for benchmark project. -->
<PropertyGroup Condition="'$(MSBuildProjectFile)' == '$BENCHMARK_PROJ$'">

</PropertyGroup>

<!-- Define properties for DllGatherer.csproj. -->
<PropertyGroup Condition="'$(MSBuildProjectFile)' == '$GATHERER_PROJ$'">

</PropertyGroup>

<!-- Define properties for BenchmarkDotNet.Autogenerated.csproj. -->
<PropertyGroup Condition="'$(MSBuildProjectFile)' == '$AUTOGENERATED_PROJ$'">

</PropertyGroup>
</Project>
2 changes: 2 additions & 0 deletions src/BenchmarkDotNet/Templates/CsProj.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project Sdk="$SDKNAME$">
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.props" />

<PropertyGroup>
<AssemblyTitle>$PROGRAMNAME$</AssemblyTitle>
Expand Down Expand Up @@ -44,4 +45,5 @@

$RUNTIMESETTINGS$

<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.targets" />
</Project>
6 changes: 4 additions & 2 deletions src/BenchmarkDotNet/Templates/MonoAOTLLVMCsProj.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk" DefaultTarget="Publish">
<Project Sdk="Microsoft.NET.Sdk" DefaultTarget="Publish">
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.props" />

<PropertyGroup>
<OriginalCSProjPath>$CSPROJPATH$</OriginalCSProjPath>
<MonoPropsPath>$([System.IO.Path]::ChangeExtension('$(OriginalCSProjPath)', '.Mono.props'))</MonoPropsPath>
Expand Down Expand Up @@ -71,7 +73,6 @@
</AotInputAssemblies>
</ItemGroup>


<MonoAOTCompiler
CompilerBinaryPath="$COMPILERBINARYPATH$"
Mode="Normal"
Expand All @@ -91,4 +92,5 @@
</Target>

<Import Project="$(MonoTargetsPath)" Condition="Exists($(MonoTargetsPath))" />
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.targets" />
</Project>
3 changes: 3 additions & 0 deletions src/BenchmarkDotNet/Templates/R2RCsProj.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk" DefaultTarget="Publish">
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.props" />

<!-- Properties same as standard CsProj.txt template -->
<PropertyGroup>
Expand Down Expand Up @@ -69,4 +70,6 @@
</Crossgen2Pack>
</ItemGroup>
</Target>

<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.targets" />
</Project>
5 changes: 4 additions & 1 deletion src/BenchmarkDotNet/Templates/WasmCsProj.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Project Sdk="$SDKNAME$" DefaultTargets="publish">
<Project Sdk="$SDKNAME$" DefaultTargets="publish">
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.props" />

<PropertyGroup>
<OriginalCSProjPath>$CSPROJPATH$</OriginalCSProjPath>
<WasmPropsPath>$([System.IO.Path]::ChangeExtension('$(OriginalCSProjPath)', '.Wasm.props'))</WasmPropsPath>
Expand Down Expand Up @@ -59,4 +61,5 @@ $CORECLR_OVERRIDES$
</Target>

<Import Project="$(WasmTargetsPath)" Condition="Exists($(WasmTargetsPath))" />
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.targets" />
</Project>
27 changes: 25 additions & 2 deletions src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace BenchmarkDotNet.Toolchains.CsProj
public class CsProjGenerator : DotNetCliGenerator, IEquatable<CsProjGenerator>
{
private const string DefaultSdkName = "Microsoft.NET.Sdk";
private const string DllGathererProjectName = "DllGatherer.csproj";
private const string AutogeneratedProjectName = "BenchmarkDotNet.Autogenerated.csproj";

private static readonly ImmutableArray<string> SettingsWeWantToCopy = new[]
{
Expand Down Expand Up @@ -58,11 +60,32 @@ protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPar
}

protected override string GetProjectFilePath(string buildArtifactsDirectoryPath)
=> Path.Combine(buildArtifactsDirectoryPath, "BenchmarkDotNet.Autogenerated.csproj");
=> Path.Combine(buildArtifactsDirectoryPath, AutogeneratedProjectName);

protected override string GetBinariesDirectoryPath(string buildArtifactsDirectoryPath, string configuration)
=> Path.Combine(buildArtifactsDirectoryPath, "bin", configuration, TargetFrameworkMoniker);

protected override async ValueTask GenerateCustomBuildHooksAsync(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger, CancellationToken cancellationToken)
{
var benchmark = buildPartition.RepresentativeBenchmarkCase;
var benchmarkProjectFile = GetProjectFilePath(benchmark.Descriptor.Type, logger);

var propsPath = Path.Combine(artifactsPaths.BuildArtifactsDirectoryPath, "BenchmarkDotNet.Build.props");
var props = GetTemplateContent("BenchmarkDotNet.Build.props.txt");
await File.WriteAllTextAsync(propsPath, props.ToString(), cancellationToken).ConfigureAwait(false);

var targetsPath = Path.Combine(artifactsPaths.BuildArtifactsDirectoryPath, "BenchmarkDotNet.Build.targets");
var targets = GetTemplateContent("BenchmarkDotNet.Build.targets.txt");
await File.WriteAllTextAsync(targetsPath, targets.ToString(), cancellationToken).ConfigureAwait(false);

string GetTemplateContent(string templateName)
=> new StringBuilder(ResourceHelper.LoadTemplate(templateName))
.Replace("$BENCHMARK_PROJ$", benchmarkProjectFile.Name)
.Replace("$GATHERER_PROJ$", DllGathererProjectName)
.Replace("$AUTOGENERATED_PROJ$", AutogeneratedProjectName)
.ToString();
}

protected override ValueTask GenerateBuildScriptAsync(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, CancellationToken cancellationToken)
{
string projectFilePath = GetProjectFilePath(buildPartition.RepresentativeBenchmarkCase.Descriptor.Type, NullLogger.Instance).FullName;
Expand Down Expand Up @@ -115,7 +138,7 @@ private async ValueTask<string> GenerateBuildProject(BuildPartition buildPartiti
}

private static string GetDllGathererPath(string filePath)
=> Path.Combine(Path.GetDirectoryName(filePath)!, $"DllGatherer{Path.GetExtension(filePath)}");
=> Path.Combine(Path.GetDirectoryName(filePath)!, $"{Path.GetFileNameWithoutExtension(DllGathererProjectName)}{Path.GetExtension(filePath)}");

protected async ValueTask GatherReferencesAsync(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger, CancellationToken cancellationToken)
{
Expand Down
6 changes: 6 additions & 0 deletions src/BenchmarkDotNet/Toolchains/GeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public async ValueTask<GenerateResult> GenerateProjectAsync(BuildPartition build
// There is no async file copy API, so we just do it synchronously. We are likely on a ThreadPool thread here anyway if this generator is ran in parallel.
CopyAllRequiredFiles(artifactsPaths);

await GenerateCustomBuildHooksAsync(buildPartition, artifactsPaths, logger, cancellationToken).ConfigureAwait(false);
await GenerateCodeAsync(buildPartition, artifactsPaths, cancellationToken).ConfigureAwait(false);
await GenerateAppConfigAsync(buildPartition, artifactsPaths, cancellationToken).ConfigureAwait(false);
await GenerateNuGetConfigAsync(artifactsPaths, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -101,6 +102,11 @@ [PublicAPI] protected virtual void CopyAllRequiredFiles(ArtifactsPaths artifacts
/// </summary>
[PublicAPI] protected virtual ValueTask GenerateProjectAsync(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger, CancellationToken cancellationToken) => new();

/// <summary>
/// generates .props and .targets files to customize build.
/// </summary>
[PublicAPI] protected virtual ValueTask GenerateCustomBuildHooksAsync(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger, CancellationToken cancellationToken) => new();

/// <summary>
/// generates a script can be used when debugging compilation issues
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/BenchmarkDotNet/Toolchains/NativeAot/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected override async ValueTask GenerateProjectAsync(BuildPartition buildPart

private string GenerateProjectForNuGetBuild(string projectFilePath, BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger) => $"""
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>{TargetFrameworkMoniker}</TargetFrameworks>
Expand Down Expand Up @@ -179,6 +180,7 @@ private string GenerateProjectForNuGetBuild(string projectFilePath, BuildPartiti
<PropertyGroup>
<LangVersion Condition="'$(LangVersion)' == '' Or ($([System.Char]::IsDigit('$(LangVersion)', 0)) And '$(LangVersion)' &lt; '8.0')">latest</LangVersion>
</PropertyGroup>
<Import Project="$(MSBuildThisFileDirectory)BenchmarkDotNet.Build.targets" />
</Project>
""";

Expand Down