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

Handle more Attribute Types when Filtering Assemblies #9373

Merged
merged 4 commits into from
Oct 8, 2024
Merged
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
4 changes: 4 additions & 0 deletions samples/HelloWorld/HelloLibrary/LibraryActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
using Android.Views;
using Android.Widget;

[assembly: HelloLibrary.GenericTest<string>]
namespace HelloLibrary
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class GenericTestAttribute<T> : Attribute { }

[Activity(Label = "Library Activity", Name="mono.samples.hello.LibraryActivity")]
public class LibraryActivity : Activity
{
Expand Down
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ void DoAddAssembliesFromArchCollection (AndroidTargetArch arch, Dictionary<strin
// Thus, we no longer just store them in the apk but we call the `GetCompressionMethod` method to find out whether
// or not we're supposed to compress .so files.
foreach (ITaskItem assembly in assemblies.Values) {
if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {
if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec, Log)) {
Log.LogCodedWarning ("XA0107", assembly.ItemSpec, 0, Properties.Resources.XA0107, assembly.ItemSpec);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ bool IsAndroidAssembly (AssemblyDefinition assembly, MetadataReader reader)
{
foreach (var handle in assembly.GetCustomAttributes ()) {
var attribute = reader.GetCustomAttribute (handle);
var name = reader.GetCustomAttributeFullName (attribute);
var name = reader.GetCustomAttributeFullName (attribute, Log);
switch (name) {
case "System.Runtime.Versioning.TargetFrameworkAttribute":
string targetFrameworkIdentifier = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1461,5 +1461,48 @@ public void BuildWithJavaToolOptions ()
Environment.SetEnvironmentVariable ("JAVA_TOOL_OPTIONS", oldEnvVar);
}
}

[Test]
public void LibraryWithGenericAttribute ()
{
var path = Path.Combine ("temp", TestContext.CurrentContext.Test.Name);
var lib = new XamarinAndroidLibraryProject {
ProjectName = "Library1",
IsRelease = true,
Sources = {
new BuildItem.Source ("Class1.cs") {
TextContent = () => """
namespace Library1;
public class Class1 { }
"""
},
new BuildItem.Source ("GenericTestAttribute.cs") {
TextContent = () => """
using System;
[assembly: GenericTestAttribute<Guid>]
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class GenericTestAttribute<T> : Attribute { }
""",
},
},
};
var proj = new XamarinAndroidApplicationProject {
ProjectName = "App1",
IsRelease = true,
Sources = {
new BuildItem.Source ("Class2.cs") {
TextContent= () => """
namespace App1;
class Class2 : Library1.Class1 { }
""",
},
},
};
proj.AddReference (lib);
using var libb = CreateDllBuilder (Path.Combine (path, "Library1"));
Assert.IsTrue (libb.Build (lib), "Library1 Build should have succeeded.");
using var b = CreateApkBuilder (Path.Combine (path, "App1"));
Assert.IsTrue (b.Build (proj), "App1 Build should have succeeded.");
}
}
}
26 changes: 23 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Utilities/MetadataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,37 @@
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;

namespace Xamarin.Android.Tasks
{
public static class MetadataExtensions
{
public static string GetCustomAttributeFullName (this MetadataReader reader, CustomAttribute attribute)
public static string GetCustomAttributeFullName (this MetadataReader reader, CustomAttribute attribute, TaskLoggingHelper log)
{
if (attribute.Constructor.Kind == HandleKind.MemberReference) {
var ctor = reader.GetMemberReference ((MemberReferenceHandle)attribute.Constructor);
var type = reader.GetTypeReference ((TypeReferenceHandle)ctor.Parent);
return reader.GetString (type.Namespace) + "." + reader.GetString (type.Name);
try {
if (ctor.Parent.Kind == HandleKind.TypeReference) {
var type = reader.GetTypeReference ((TypeReferenceHandle)ctor.Parent);
return reader.GetString (type.Namespace) + "." + reader.GetString (type.Name);
} else if (ctor.Parent.Kind == HandleKind.TypeSpecification) {
var type = reader.GetTypeSpecification ((TypeSpecificationHandle)ctor.Parent);
BlobReader blobReader = reader.GetBlobReader (type.Signature);
SignatureTypeCode typeCode = blobReader.ReadSignatureTypeCode ();
EntityHandle typeHandle = blobReader.ReadTypeHandle ();
TypeReference typeRef = reader.GetTypeReference ((TypeReferenceHandle)typeHandle);
return reader.GetString (typeRef.Namespace) + "." + reader.GetString (typeRef.Name);
} else {
log.LogDebugMessage ($"Unsupported EntityHandle.Kind: {ctor.Parent.Kind}");
return null;
}
dellis1972 marked this conversation as resolved.
Show resolved Hide resolved
}
catch (InvalidCastException ex) {
log.LogDebugMessage ($"Unsupported EntityHandle.Kind `{ctor.Parent.Kind}`: {ex}");
return null;
dellis1972 marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (attribute.Constructor.Kind == HandleKind.MethodDefinition) {
var ctor = reader.GetMethodDefinition ((MethodDefinitionHandle)attribute.Constructor);
var type = reader.GetTypeDefinition (ctor.GetDeclaringType ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,15 @@ public static bool HasMonoAndroidReference (MetadataReader reader)
return false;
}

public static bool IsReferenceAssembly (string assembly)
public static bool IsReferenceAssembly (string assembly, TaskLoggingHelper log)
{
using (var stream = File.OpenRead (assembly))
using (var pe = new PEReader (stream)) {
var reader = pe.GetMetadataReader ();
var assemblyDefinition = reader.GetAssemblyDefinition ();
foreach (var handle in assemblyDefinition.GetCustomAttributes ()) {
var attribute = reader.GetCustomAttribute (handle);
var attributeName = reader.GetCustomAttributeFullName (attribute);
var attributeName = reader.GetCustomAttributeFullName (attribute, log);
if (attributeName == "System.Runtime.CompilerServices.ReferenceAssemblyAttribute")
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ string GetResourceDesignerClass (MetadataReader reader)
var assembly = reader.GetAssemblyDefinition ();
foreach (var handle in assembly.GetCustomAttributes ()) {
var attribute = reader.GetCustomAttribute (handle);
var fullName = reader.GetCustomAttributeFullName (attribute);
var fullName = reader.GetCustomAttributeFullName (attribute, Log);
if (fullName == "Android.Runtime.ResourceDesignerAttribute") {
var values = attribute.GetCustomAttributeArguments ();
foreach (var arg in values.NamedArguments) {
Expand Down
Loading