Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Merge the 'RemoveAttributesStep' into the 'StoreAttributesStep' #20224

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,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