Skip to content

Commit a883a01

Browse files
[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.
1 parent 0818e53 commit a883a01

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+
<ShouldWeaveAssemblies Condition="'$(ShouldWeaveAssemblies)' == ''">true</ShouldWeaveAssemblies>
7+
<WeaveAssembliesAfterTargets Condition="'$(WeaveAssembliesAfterTargets)' == ''">CoreCompile</WeaveAssembliesAfterTargets>
8+
<WeaveAssembliesBeforeTargets Condition="'$(WeaveAssembliesBeforeTargets)' == ''">CopyFilesToOutputDirectory</WeaveAssembliesBeforeTargets>
9+
<WeaveAssemblyPath Condition="'$(WeaveAssemblyPath)' == ''">$(IntermediateOutputPath)$(TargetName)$(TargetExt)</WeaveAssemblyPath>
10+
<WeaveAssembliesStampFile Condition="'$(WeaveAssembliesStampFile)' == ''">$(IntermediateOutputPath)BenchmarkDotNetWeaver.stamp</WeaveAssembliesStampFile>
11+
</PropertyGroup>
12+
13+
<Target Name="WeaveAssemblies"
14+
AfterTargets="$(WeaveAssembliesAfterTargets)"
15+
BeforeTargets="$(WeaveAssembliesBeforeTargets)"
16+
Condition="'$(ShouldWeaveAssemblies)' == 'true'"
17+
Inputs="$(WeaveAssemblyPath)"
18+
Outputs="$(WeaveAssembliesStampFile)">
19+
20+
<WeaveAssemblyTask TargetAssembly="$(WeaveAssemblyPath)" />
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="$(WeaveAssembliesStampFile)" AlwaysCreate="true" />
24+
<ItemGroup>
25+
<FileWrites Include="$(WeaveAssembliesStampFile)" />
26+
</ItemGroup>
1327
</Target>
1428
</Project>

0 commit comments

Comments
 (0)