Skip to content

Commit

Permalink
[dotnet] Merge the 'RemoveAttributesStep' into the 'StoreAttributesStep'
Browse files Browse the repository at this point in the history
Removing (and storing) the attributes earlier make it's possible for them not
to be marked. IOW the attribute type itself (and not just the decorations) can
be removed.

Inspired by / extracted from #14270.
  • Loading branch information
rolfbjarne committed Feb 29, 2024
1 parent 6ef7d9c commit a2a8f79
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 79 deletions.
5 changes: 0 additions & 5 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,6 @@
-->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="SweepStep" Type="Xamarin.Linker.ManagedRegistrarLookupTablesStep" Condition="'$(Registrar)' == 'managed-static'" />

<!--
post-sweep custom steps
-->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" AfterStep="SweepStep" Condition="'$(_AreAnyAssembliesTrimmed)' == 'true'" Type="Xamarin.Linker.Steps.PostSweepDispatcher" />

<!--
pre-output custom steps
-->
Expand Down
13 changes: 0 additions & 13 deletions tools/dotnet-linker/Steps/PostSweepDispatcher.cs

This file was deleted.

59 changes: 0 additions & 59 deletions tools/dotnet-linker/Steps/RemoveAttributesStep.cs

This file was deleted.

34 changes: 32 additions & 2 deletions tools/dotnet-linker/Steps/StoreAttributesStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
#nullable enable

namespace Xamarin.Linker.Steps {
// The registrar needs some of the system attributes that the linker might remove, so store those elsewhere for the static registrar's use.
// The .NET linker comes with a way to remove attributes (by passing '--link-attributes
// some.xml' as a command-line argument), but this has a few problems:
//
// * We'd need to figure out which attributes to remove before running the linker,
// but the code to figure out which optimizations have been enabled (and which attributes
// should be removed) is in our custom linker code. We'd need to refactor a big chunk
// of code to move this logic out of our custom linker code.
// * We need to keep the removed attributes around, because the static registrar needs
// them. Our custom linker logic is not notified for removed attributes, which means
// we'd need to store all attributes for the attribute types we're interested in (as
// opposed to this solution, where we only store attributes that are actually removed).
// * The attributes we want removed may contain references to types we don't want
// linked away. If we ask the linker to remove those attributes, then the types may
// be linked away as well, and there's no good way around this.
//
// The end result is that a custom step is the best solution for now.

public class StoreAttributesStep : AttributeIteratorBaseStep {
protected override void ProcessAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out bool remove)
{
Expand All @@ -31,10 +47,24 @@ protected override void ProcessAttribute (ICustomAttributeProvider provider, Cus
break;
}
break;
case "Foundation":
case Namespaces.Foundation:
switch (attr_type.Name) {
case "ProtocolAttribute":
case "ProtocolMemberAttribute":
store = LinkContext.App.Optimizations.RegisterProtocols == true;
remove = store;
break;
}
break;
case Namespaces.ObjCRuntime:
switch (attr_type.Name) {
case "NativeNameAttribute":
store = true;
remove = store;
break;
case "AdoptsAttribute":
store = LinkContext.App.Optimizations.RegisterProtocols == true;
remove = store;
break;
}
break;
Expand Down

0 comments on commit a2a8f79

Please sign in to comment.