-
Notifications
You must be signed in to change notification settings - Fork 561
[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking in MarkIProtocolHandler. #24931
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
Merged
rolfbjarne
merged 8 commits into
main
from
dev/rolf/use-dynamic-dependency-attributes-markiprotocolhandler
Mar 23, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1a9742f
[dotnet-linker] Use [DynamicDependency] attributes instead of manual …
rolfbjarne 53725f4
Auto-format source code
f7766bf
Opt out & review
rolfbjarne 2399bc6
improvements
rolfbjarne 4e79705
[tools] Enable xml doc generation for dotnet-linker.
rolfbjarne 6d19d4f
Merge remote-tracking branch 'origin/main' into dev/rolf/use-dynamic-…
rolfbjarne e5e4199
simplify
rolfbjarne 278c8dc
Auto-format source code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| using System; | ||
|
|
||
| using Mono.Cecil; | ||
| using Mono.Linker; | ||
| using Mono.Linker.Steps; | ||
|
|
||
| using Xamarin.Linker.Steps; | ||
| using Xamarin.Tuner; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.Linker { | ||
|
|
||
| // If we're using the dynamic registrar, we need to mark interfaces that represent protocols | ||
| // even if it doesn't look like the interfaces are used, since we need them at runtime. | ||
| // | ||
| // Sadly, there doesn't seem to be a way to preserve only some interfaces, we have to preserve them all. | ||
| // We add this to the current type's static cctor: | ||
| // | ||
| // [DynamicDependency (DynamicallyAccessedMemberTypes.Interfaces, typeof (TheClass))] | ||
| // static TheClass () | ||
| // { | ||
| // } | ||
| // | ||
| // This step is a replacement for the MarkIProtocolHandler step (which can't be moved out of the linker). | ||
| // | ||
| // One difference with the MarkIProtocolHandler step is that this step will preserve all interfaces for | ||
| // a type, while MarkIProtocolHandler will only preserve protocol interfaces. Taking into account that | ||
| // preserving protocol interfaces is only needed when using the dynamic registrar, and that's not our | ||
| // most optimized build configuration (nor the default release configuration on any platform), this should | ||
| // hopefully not be a big issue). | ||
| // | ||
| public class PreserveProtocolsStep : AssemblyModifierStep { | ||
| protected override string Name { get; } = "Preserve Block Code"; | ||
| protected override int ErrorCode { get; } = 2240; | ||
|
|
||
| protected override bool IsActiveFor (AssemblyDefinition assembly) | ||
| { | ||
| if (DerivedLinkContext.App.Registrar != Bundler.RegistrarMode.Dynamic) | ||
| return false; | ||
|
|
||
| if (Annotations.GetAction (assembly) != AssemblyAction.Link) | ||
| return false; | ||
|
|
||
| if (!assembly.MainModule.HasTypes) | ||
| return false; | ||
|
|
||
| // Unless an assembly is or references our platform assembly, then it won't have anything we need to register | ||
| if (!Configuration.Profile.IsOrReferencesProductAssembly (assembly)) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| protected override bool ProcessType (TypeDefinition type) | ||
| { | ||
| var modified = false; | ||
|
|
||
| if (!type.HasInterfaces) | ||
| return modified; | ||
|
|
||
| if (!type.IsNSObject (DerivedLinkContext)) | ||
| return modified; | ||
|
|
||
| var hasProtocols = false; | ||
| foreach (var iface in type.Interfaces) { | ||
| var resolvedInterfaceType = iface.InterfaceType.Resolve (); | ||
| hasProtocols = resolvedInterfaceType.HasCustomAttribute (DerivedLinkContext, Namespaces.Foundation, "ProtocolAttribute"); | ||
| if (hasProtocols) | ||
| break; | ||
| } | ||
|
|
||
| if (!hasProtocols) | ||
| return modified; | ||
|
|
||
| var attrib = abr.CreateDynamicDependencyAttribute (DynamicallyAccessedMemberTypes.Interfaces, type); | ||
| modified |= abr.AddAttributeToStaticConstructor (type, attrib); | ||
| return modified; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using System; | ||
| using System.Linq; | ||
|
|
||
| using Mono.Cecil; | ||
|
|
||
| using Mono.Linker; | ||
| using Mono.Linker.Steps; | ||
| using Mono.Tuner; | ||
|
|
||
| using Xamarin.Bundler; | ||
| using Xamarin.Linker; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.Linker.Steps; | ||
|
|
||
| public abstract class AssemblyModifierStep : ConfigurationAwareStep { | ||
| private protected AppBundleRewriter abr => Configuration.AppBundleRewriter; | ||
|
|
||
| protected override void TryProcessAssembly (AssemblyDefinition assembly) | ||
| { | ||
| var modified = false; | ||
|
|
||
| abr.SetCurrentAssembly (assembly); | ||
| foreach (var type in assembly.MainModule.Types) | ||
| modified |= ProcessTypeImpl (type); | ||
|
|
||
| if (modified) | ||
| abr.SaveCurrentAssembly (); | ||
| abr.ClearCurrentAssembly (); | ||
| } | ||
|
|
||
| protected virtual bool ProcessType (TypeDefinition type) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| bool ProcessTypeImpl (TypeDefinition type) | ||
| { | ||
| var modified = ProcessType (type); | ||
| if (type.HasNestedTypes) { | ||
| foreach (var nested in type.NestedTypes) | ||
| modified |= ProcessTypeImpl (nested); | ||
| } | ||
| return modified; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.