-
Notifications
You must be signed in to change notification settings - Fork 565
[linker] Add metadata to linked assemblies indicating the RID for which they were created #8183
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9bba7fc
Attribute gets added properly...
grendello 6ecd2ac
Metadata set to the appropriate RID
grendello d5ca586
Try to fix link failures
grendello a90047a
Update src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/…
grendello bbcf701
Make the step itself conditional, too
grendello 043a70a
Update apkdesc
grendello ad73f8a
Merge branch 'main' into rid-attribute
grendello 1f137da
Merge branch 'main' into rid-attribute
grendello 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
6 changes: 6 additions & 0 deletions
6
src/Microsoft.Android.Sdk.ILLink/PreserveLists/System.Private.CoreLib.xml
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,6 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <linker> | ||
| <assembly fullname="System.Private.CoreLib"> | ||
| <type fullname="System.Reflection.AssemblyMetadataAttribute" /> | ||
| </assembly> | ||
| </linker> |
100 changes: 100 additions & 0 deletions
100
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddRidMetadataAttributeStep.cs
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,100 @@ | ||
| using System; | ||
|
|
||
| using Mono.Cecil; | ||
| using Mono.Linker; | ||
| using Mono.Linker.Steps; | ||
|
|
||
| #if ILLINK | ||
| using Microsoft.Android.Sdk.ILLink; | ||
| #endif | ||
|
|
||
| namespace MonoDroid.Tuner; | ||
|
|
||
| public class AddRidMetadataAttributeStep : BaseStep | ||
| { | ||
| protected override void ProcessAssembly (AssemblyDefinition assembly) | ||
| { | ||
| if (!Annotations.HasAction (assembly)) { | ||
| return; | ||
| } | ||
|
|
||
| var action = Annotations.GetAction (assembly); | ||
| if (action == AssemblyAction.Skip || action == AssemblyAction.Delete) { | ||
| return; | ||
| } | ||
|
|
||
| string? rid = null; | ||
| #if ILLINK | ||
| if (!Context.TryGetCustomData ("XARuntimeIdentifier", out rid)) { | ||
| throw new InvalidOperationException ("Missing XARuntimeIdentifier custom data"); | ||
| } | ||
| #endif | ||
| if (String.IsNullOrEmpty (rid)) { | ||
| throw new InvalidOperationException ("RID must have a non-empty value"); | ||
| } | ||
|
|
||
| AssemblyDefinition corlib = GetCorlib (); | ||
| MethodDefinition assemblyMetadataAttributeCtor = FindAssemblyMetadataAttributeCtor (corlib); | ||
| TypeDefinition systemString = GetSystemString (corlib); | ||
|
|
||
| var attr = new CustomAttribute (assembly.MainModule.ImportReference (assemblyMetadataAttributeCtor)); | ||
| attr.ConstructorArguments.Add (new CustomAttributeArgument (systemString, "XamarinAndroidAbi")); // key | ||
|
|
||
| // TODO: figure out how to get the RID... | ||
| attr.ConstructorArguments.Add (new CustomAttributeArgument (systemString, rid)); // value | ||
|
|
||
| assembly.CustomAttributes.Add (attr); | ||
|
|
||
| if (action == AssemblyAction.Copy) { | ||
| Annotations.SetAction (assembly, AssemblyAction.Save); | ||
| } | ||
| } | ||
|
|
||
| TypeDefinition GetSystemString (AssemblyDefinition asm) => FindType (asm, "System.String", required: true); | ||
|
|
||
| AssemblyDefinition GetCorlib () | ||
| { | ||
| const string ImportAssembly = "System.Private.CoreLib"; | ||
| AssemblyDefinition? asm = Context.Resolve (AssemblyNameReference.Parse (ImportAssembly)); | ||
| if (asm == null) { | ||
| throw new InvalidOperationException ($"Unable to import assembly '{ImportAssembly}'"); | ||
| } | ||
|
|
||
| return asm; | ||
| } | ||
|
|
||
| MethodDefinition FindAssemblyMetadataAttributeCtor (AssemblyDefinition asm) | ||
| { | ||
| const string AttributeType = "System.Reflection.AssemblyMetadataAttribute"; | ||
|
|
||
| TypeDefinition assemblyMetadataAttribute = FindType (asm, AttributeType, required: true); | ||
| foreach (MethodDefinition md in assemblyMetadataAttribute!.Methods) { | ||
| if (!md.IsConstructor) { | ||
| continue; | ||
| } | ||
|
|
||
| return md; | ||
| } | ||
|
|
||
| throw new InvalidOperationException ($"Unable to find the {AttributeType} type constructor"); | ||
| } | ||
|
|
||
| TypeDefinition? FindType (AssemblyDefinition asm, string typeName, bool required) | ||
| { | ||
| foreach (ModuleDefinition md in asm.Modules) { | ||
| foreach (TypeDefinition et in md.Types) { | ||
| if (String.Compare (typeName, et.FullName, StringComparison.Ordinal) != 0) { | ||
| continue; | ||
| } | ||
|
|
||
| return et; | ||
| } | ||
| } | ||
|
|
||
| if (required) { | ||
| throw new InvalidOperationException ($"Internal error: required type '{typeName}' in assembly {asm} not found"); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we avoid naming things with an
XAorXamarinprefix? Can this just beRuntimeIdentifier?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RuntimeIdentifieris too generic IMO.XAorXamarinAndroidmakes it less likely to clash with other people's metadataThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or
AndroidRuntimeIdentifier? I don't think they want us to introduce new things that sayXamarin.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who would know? :)