Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
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
Copy link
Member

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 XA or Xamarin prefix? Can this just be RuntimeIdentifier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeIdentifier is too generic IMO. XA or XamarinAndroid makes it less likely to clash with other people's metadata

Copy link
Member

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 say Xamarin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who would know? :)


// 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This file contains the .NET 5-specific targets to customize ILLink
https://github.com/dotnet/sdk/blob/a5393731b5b7b225692fff121f747fbbc9e8b140/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ILLink.targets#L147
-->
<_TrimmerCustomData Include="XATargetFrameworkDirectories" Value="$(_XATargetFrameworkDirectories)" />
<_TrimmerCustomData Include="XARuntimeIdentifier" Value="$(RuntimeIdentifier)" Condition=" '$(_AndroidAddRuntimeIdentifierToAssemblies)' == 'true' " />
<_TrimmerCustomData
Condition=" '$(_ProguardProjectConfiguration)' != '' "
Include="ProguardConfiguration"
Expand Down Expand Up @@ -94,6 +95,12 @@ This file contains the .NET 5-specific targets to customize ILLink
BeforeStep="MarkStep"
Type="MonoDroid.Tuner.FixLegacyResourceDesignerStep"
/>
<_TrimmerCustomSteps
Condition=" '$(_AndroidAddRuntimeIdentifierToAssemblies)' == 'true' "
Include="$(_AndroidLinkerCustomStepAssembly)"
AfterStep="CleanStep"
Type="MonoDroid.Tuner.AddRidMetadataAttributeStep"
/>
<_PreserveLists Include="$(MSBuildThisFileDirectory)..\PreserveLists\*.xml" />
<TrimmerRootDescriptor
Condition=" '@(ResolvedFileToPublish->Count())' != '0' and '%(Filename)' != '' "
Expand Down