Skip to content

Commit

Permalink
Remove custom types marking for types which XA does not own for netcore
Browse files Browse the repository at this point in the history
This is or will be handled by BCL libraries and XA tooling should try
not to interfere with more fine tuned handling done by BCL.
  • Loading branch information
marek-safar committed Dec 2, 2020
1 parent 4cd3913 commit d6b9365
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
36 changes: 10 additions & 26 deletions src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttributeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
namespace Microsoft.Android.Sdk.ILLink
{

public abstract class ApplyPreserveAttributeBase : BaseSubStep {

// set 'removeAttribute' to true if you want the preserved attribute to be removed from the final assembly
protected abstract bool IsPreservedAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out bool removeAttribute);
public class ApplyPreserveAttribute : BaseSubStep {

public override SubStepTargets Targets {
get {
Expand Down Expand Up @@ -66,7 +63,7 @@ public override void ProcessEvent (EventDefinition @event)

void MarkMethodIfPreserved (MethodDefinition method)
{
foreach (var attribute in GetPreserveAttributes (method))
foreach (var attribute in GetPreserveAttributes (method))
MarkMethod (method, attribute);
}

Expand Down Expand Up @@ -126,6 +123,9 @@ void PreserveUnconditional (IMetadataTokenProvider provider)

void TryApplyPreserveAttribute (TypeDefinition type)
{
if (!type.HasCustomAttributes)
return;

foreach (var attribute in GetPreserveAttributes (type)) {
Annotations.Mark (type);

Expand All @@ -138,28 +138,12 @@ void TryApplyPreserveAttribute (TypeDefinition type)
}
}

List<CustomAttribute> GetPreserveAttributes (ICustomAttributeProvider provider)
IEnumerable<CustomAttribute> GetPreserveAttributes (ICustomAttributeProvider provider)
{
List<CustomAttribute> attrs = new List<CustomAttribute> ();

if (!provider.HasCustomAttributes)
return attrs;

var attributes = provider.CustomAttributes;

for (int i = attributes.Count - 1; i >= 0; i--) {
var attribute = attributes [i];

bool remote_attribute;
if (!IsPreservedAttribute (provider, attribute, out remote_attribute))
continue;

attrs.Add (attribute);
if (remote_attribute)
attributes.RemoveAt (i);
}

return attrs;
yield return provider.CustomAttributes.Where (a => {
var type = a.Constructor.DeclaringType;
return type.Namespace == "Android.Runtime" && type.Name == "PreserveAttribute";
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\LinkerOptions.cs" />

<!--Steps that are upstreamed, these are OK to remove-->
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\ApplyPreserveAttribute.cs" />
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\OutputStepWithTimestamps.cs" />
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\PreserveCode.cs" />
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\PreserveDynamicTypes.cs" />
Expand All @@ -36,6 +37,7 @@
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\PreserveRuntimeSerialization.cs" />
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\PreserveTlsProvider.cs" />
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\PreserveTypeConverters.cs" />
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\RemoveAttributes.cs" />

<!--TODO: Subclasses MarkStep-->
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\MonoDroidMarkStep.cs" />
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.Android.Sdk.ILLink/SetupStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ protected override void Process ()
subSteps2.Add (new PreserveApplications ());
subSteps2.Add (new PreserveRegistrations (cache));
subSteps2.Add (new PreserveJavaInterfaces ());
subSteps2.Add (new RemoveAttributes ());

InsertAfter (new FixAbstractMethodsStep (cache), "RemoveUnreachableBlocksStep");
InsertAfter (subSteps2, "RemoveUnreachableBlocksStep");
Expand Down
10 changes: 10 additions & 0 deletions src/Mono.Android/ILLink/ILLink.LinkAttributes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<linker>
<assembly fullname="*">
<type fullname="Android.Runtime.PreserveAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
<type fullname="Android.Runtime.IntDefinitionAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
</assembly>
</linker>
2 changes: 2 additions & 0 deletions src/Mono.Android/Mono.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<ProjectReference Include="..\..\external\Java.Interop\src\Java.Interop\Java.Interop.csproj" />

<EmbeddedResource Include="ILLink/ILLink.LinkAttributes.xml" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d6b9365

Please sign in to comment.