Move CheckForObsoletePreserveAttribute into PostTrimmingPipeline#11009
Move CheckForObsoletePreserveAttribute into PostTrimmingPipeline#11009
Conversation
Previously, CheckForObsoletePreserveAttribute ran as a separate MSBuild task on every incremental build (AfterTargets="_PrepareAssemblies"), taking ~120ms to open and read every user assembly with PEReader. Now the check runs as a step in PostTrimmingPipeline, where assemblies are already loaded by Mono.Cecil. This eliminates the overhead of separately opening each assembly file, and the check only runs when trimming is enabled. Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/android/sessions/34cf49e8-922e-4ad8-b7ea-c598fcf71b63
| } | ||
|
|
||
| var steps = new List<IAssemblyModifierPipelineStep> (); | ||
| steps.Add (new CheckForObsoletePreserveAttributeStep (Log)); |
There was a problem hiding this comment.
@copilot there is a build error:
(CoreCompile target) ->
/Users/builder/azdo/_work/2/s/android/src/Xamarin.Android.Build.Tasks/Tasks/PostTrimmingPipeline.cs(50,18): error CS0246: The type or namespace name 'CheckForObsoletePreserveAttributeStep' could not be found (are you missing a using directive or an assembly reference?) [/Users/builder/azdo/_work/2/s/android/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj]
177 Warning(s)
1 Error(s)
There was a problem hiding this comment.
Fixed in 3add14d. Two issues: (1) the Linker\** directory is excluded from compilation in the csproj, so the new file needed an explicit <Compile Include> entry, and (2) LogCodedWarning is an extension method from Microsoft.Android.Build.Tasks namespace which needed a using directive.
…d missing using The Linker\** directory is excluded from compilation by default, so files must be explicitly listed. Also add the missing Microsoft.Android.Build.Tasks using directive for the LogCodedWarning extension method. Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/android/sessions/0d36b884-8972-4ef3-b842-8834afd6a7af
There was a problem hiding this comment.
Pull request overview
This PR aims to remove the incremental-build cost of scanning assemblies for Android.Runtime.PreserveAttribute references by moving the check into the existing post-trimming pipeline, reusing already-loaded Mono.Cecil assemblies.
Changes:
- Added a new
CheckForObsoletePreserveAttributeStepimplementingIAssemblyModifierPipelineStepto emitIL6001without extra I/O. - Registered the new step in
PostTrimmingPipelineso it runs alongside other post-trim assembly modifications. - Removed the standalone
CheckForObsoletePreserveAttributeMSBuild task and its_CheckForObsoletePreserveAttributetarget wiring.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj | Includes the new linker step source file in the explicit Linker compile list. |
| src/Xamarin.Android.Build.Tasks/Tasks/PostTrimmingPipeline.cs | Adds the new warning step into the post-trimming step list. |
| src/Xamarin.Android.Build.Tasks/Tasks/CheckForObsoletePreserveAttribute.cs | Removes the standalone PEReader-based task implementation. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets | Removes the UsingTask and target that ran the warning task after _PrepareAssemblies. |
| src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/CheckForObsoletePreserveAttributeStep.cs | New Cecil-based pipeline step that detects PreserveAttribute type references and logs IL6001. |
|
|
||
| <UsingTask TaskName="Xamarin.Android.Tasks.CheckForObsoletePreserveAttribute" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" /> | ||
| <UsingTask TaskName="Xamarin.Android.Tasks.ProcessAssemblies" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" /> | ||
| <UsingTask TaskName="Xamarin.Android.Tasks.ProcessNativeLibraries" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" /> | ||
| <UsingTask TaskName="Xamarin.Android.Tasks.ProcessRuntimePackLibraryDirectories" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" /> |
There was a problem hiding this comment.
Removing the standalone _CheckForObsoletePreserveAttribute target drops the IL6001 warning from the normal _PrepareAssemblies path, and the replacement implementation currently only runs via PostTrimmingPipeline (which is conditioned on $(PublishTrimmed)). This likely changes behavior for standard Release linked builds (AndroidLinkMode/TrimMode) that don’t set PublishTrimmed—LinkerTests.WarnWithReferenceToPreserveAttribute currently covers this and will probably fail. Please ensure the warning is still emitted for the configurations where ILLink/linking runs today, or update the targets/conditions accordingly.
| } | ||
|
|
||
| var steps = new List<IAssemblyModifierPipelineStep> (); | ||
| steps.Add (new CheckForObsoletePreserveAttributeStep (Log)); |
There was a problem hiding this comment.
Adding the warning check as a PostTrimmingPipeline step means it will only run when the PostTrimmingPipeline MSBuild target runs (currently gated on $(PublishTrimmed) in Microsoft.Android.Sdk.TypeMap.LlvmIr.targets). The existing build/test scenario LinkerTests.WarnWithReferenceToPreserveAttribute expects the IL6001 warning in a normal Release linked build without setting PublishTrimmed, so this change will likely stop emitting the warning (and break that test) for non-PublishTrimmed builds. Consider moving this check into the ILLink pipeline used for AndroidLinkMode/TrimMode release linking, or otherwise ensuring the step runs whenever linking/trimming is performed, not only for PublishTrimmed publishes.
| steps.Add (new CheckForObsoletePreserveAttributeStep (Log)); |
CheckForObsoletePreserveAttributeruns as a standalone MSBuild task on every incremental build, costing ~120ms to open and scan every user assembly withPEReader. This is unnecessary work sincePostTrimmingPipelinealready loads all assemblies via Mono.Cecil.Changes
CheckForObsoletePreserveAttributeStep— implementsIAssemblyModifierPipelineStep, checks forAndroid.Runtime.PreserveAttributetype references using the already-loadedAssemblyDefinition(zero additional I/O)PostTrimmingPipeline— runs beforeStripEmbeddedLibrariesStep, emits the sameIL6001warningCheckForObsoletePreserveAttribute.cstask, itsUsingTaskdeclaration, and the_CheckForObsoletePreserveAttributetarget fromMicrosoft.Android.Sdk.AssemblyResolution.targets