Skip to content

Commit 7cad308

Browse files
[msbuild] refactor BenchmarkDotNet.Weaver.targets to support mobile platforms (dotnet#2931)
* [msbuild] refactor `BenchmarkDotNet.Weaver.targets` to support mobile platforms Context: dotnet#2929 Context: https://github.com/dotnet/android/blob/4aa9af89102af2e745a8507992187d3c5993d638/Documentation/guides/MSBuildBestPractices.md In initially testing BenchmarkDotNet with .NET MAUI, I found that the weaver was not being executed because the `Publish` target is not called during the build process for Android and iOS projects. I think the target could actually run much sooner during builds and achieve better results. To address this, I refactored the `BenchmarkDotNet.Weaver.targets` file to run after `CoreCompile` on the `@(IntermediateAssembly)` in the `obj` directory. This is similar to what other targets do, such as the XamlC compiler: https://github.com/dotnet/maui/blob/8224becbb3a8a6bb1caaca4bbe70c56e88875506/src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets#L213-L255 Other general MSBuild improvements: * Defined an MSBuild property for everything that seems useful. This allows consuming projects to configure the behavior (or workarounds!) as needed. * Made the MSBuild target incremental by using inputs and outputs. If the `.dll` file is an input, we can write a `.stamp` file as an output to indicate that the weaver has already been run for that assembly. If the `.dll` file changes, the weaver will run again. I tested this change with a .NET MAUI on 4 platforms and also the existing `BenchmarkDotNet.Samples` console app. * Prefix `BenchmarkDotNet`
1 parent 3db7350 commit 7cad308

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/BenchmarkDotNet.Weaver/buildTransitive/netstandard2.0/BenchmarkDotNet.Weaver.targets

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<UsingTask TaskName="BenchmarkDotNet.Weaver.WeaveAssemblyTask" AssemblyFile="$(MSBuildThisFileDirectory)..\..\tasks\netstandard2.0\BenchmarkDotNet.Weaver.dll" />
44

5-
<Target Name="MarkShouldRunWeaveAssemblyTask" AfterTargets="Build">
6-
<PropertyGroup>
7-
<ShouldRunWeaveAssemblyTask>true</ShouldRunWeaveAssemblyTask>
8-
</PropertyGroup>
9-
</Target>
5+
<PropertyGroup>
6+
<BenchmarkDotNetShouldWeaveAssemblies Condition="'$(BenchmarkDotNetShouldWeaveAssemblies)' == ''">true</BenchmarkDotNetShouldWeaveAssemblies>
7+
<BenchmarkDotNetWeaveAssembliesAfterTargets Condition="'$(BenchmarkDotNetWeaveAssembliesAfterTargets)' == ''">CoreCompile</BenchmarkDotNetWeaveAssembliesAfterTargets>
8+
<BenchmarkDotNetWeaveAssembliesBeforeTargets Condition="'$(BenchmarkDotNetWeaveAssembliesBeforeTargets)' == ''">CopyFilesToOutputDirectory</BenchmarkDotNetWeaveAssembliesBeforeTargets>
9+
<BenchmarkDotNetWeaveAssemblyPath Condition="'$(BenchmarkDotNetWeaveAssemblyPath)' == ''">$(IntermediateOutputPath)$(TargetName)$(TargetExt)</BenchmarkDotNetWeaveAssemblyPath>
10+
<BenchmarkDotNetWeaveAssembliesStampFile Condition="'$(BenchmarkDotNetWeaveAssembliesStampFile)' == ''">$(IntermediateOutputPath)BenchmarkDotNetWeaver.stamp</BenchmarkDotNetWeaveAssembliesStampFile>
11+
</PropertyGroup>
12+
13+
<Target Name="BenchmarkDotNetWeaveAssemblies"
14+
AfterTargets="$(BenchmarkDotNetWeaveAssembliesAfterTargets)"
15+
BeforeTargets="$(BenchmarkDotNetWeaveAssembliesBeforeTargets)"
16+
Condition="'$(BenchmarkDotNetShouldWeaveAssemblies)' == 'true'"
17+
Inputs="$(BenchmarkDotNetWeaveAssemblyPath)"
18+
Outputs="$(BenchmarkDotNetWeaveAssembliesStampFile)">
19+
20+
<WeaveAssemblyTask TargetAssembly="$(BenchmarkDotNetWeaveAssemblyPath)" />
1021

11-
<Target Name="WeaveAssemblies" AfterTargets="MarkShouldRunWeaveAssemblyTask" BeforeTargets="Publish" >
12-
<WeaveAssemblyTask TargetAssembly="$(TargetDir)$(TargetFileName)" Condition="'$(ShouldRunWeaveAssemblyTask)' == 'true'" />
22+
<!-- Create stamp file for incrementality -->
23+
<Touch Files="$(BenchmarkDotNetWeaveAssembliesStampFile)" AlwaysCreate="true" />
24+
<ItemGroup>
25+
<FileWrites Include="$(BenchmarkDotNetWeaveAssembliesStampFile)" />
26+
</ItemGroup>
1327
</Target>
1428
</Project>

0 commit comments

Comments
 (0)