Skip to content

Commit

Permalink
[illink] consolidate & remove hardcoded assembly names (#9662)
Browse files Browse the repository at this point in the history
Our codebase has various notions of "framework assembly", many of
which are outdated and wrong!

  * `@(_SharedRuntimeAssemblies)` in
    `Xamarin.Android.Build.Tasks.targets`, which generates-

  * `src\Xamarin.Android.Build.Tasks\obj\$(Configuration)\Profile.g.cs`

  * `src\Xamarin.Android.Build.Tasks\Utilities\Profile.cs`

  * `src\Microsoft.Android.Sdk.ILLink\Profile.cs`

The new and *correct* way to identify a "framework assembly" is to
check either:

  * `%(FrameworkReferenceName)` is `Microsoft.Android` or starts with
    `Microsoft.NETCore.`

  * `%(NuGetPackageId)` starts with `Microsoft.NETCore.App.Runtime.`
    or `Microsoft.Android.Runtime.`

I moved the new logic into `MonoAndroidHelper.IsFrameworkAssembly()`
and `MonoAndroidHelper.IsFromAKnownRuntimePack()` (useful for `.jar`
and other files).

I deleted all other instances of `Profile.cs`, which simplifies how
trimmer steps are shared between the `<LinkAssembliesNoShrink/>`
MSBuild task and the trimmer (illink).  In these places, we were
likely just checking against a stale list of assemblies *anyway*, so
I simply removed all these checks.
  • Loading branch information
jonathanpeppers authored Jan 15, 2025
1 parent 3127c47 commit 7aed4e3
Show file tree
Hide file tree
Showing 27 changed files with 69 additions and 494 deletions.
7 changes: 3 additions & 4 deletions src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;

using Mono.Cecil;
using Xamarin.Android.Tasks;

namespace Microsoft.Android.Sdk.ILLink
{
Expand All @@ -25,7 +24,7 @@ public override SubStepTargets Targets {

public override bool IsActiveFor (AssemblyDefinition assembly)
{
return !Profile.IsSdkAssembly (assembly) && Annotations.GetAction (assembly) == AssemblyAction.Link;
return !MonoAndroidHelper.IsFrameworkAssembly (assembly) && Annotations.GetAction (assembly) == AssemblyAction.Link;
}

public override void ProcessType (TypeDefinition type)
Expand Down
7 changes: 2 additions & 5 deletions src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Mono.Linker.Steps;
using Java.Interop.Tools.Cecil;
using Xamarin.Android.Tasks;
using Profile = Microsoft.Android.Sdk.ILLink.Profile;

namespace MonoDroid.Tuner {

Expand All @@ -27,9 +26,7 @@ public override void Initialize (LinkContext context, MarkContext markContext)

bool IsActiveFor (AssemblyDefinition assembly)
{
if (Profile.IsSdkAssembly (assembly))
return false;
if (Profile.IsProductAssembly (assembly))
if (MonoAndroidHelper.IsFrameworkAssembly (assembly))
return false;

return assembly.MainModule.HasTypeReference ("System.Net.Http.HttpMessageHandler") ||
Expand Down Expand Up @@ -397,7 +394,7 @@ static bool IsImplementor (TypeDefinition type, IMetadataResolver cache)

static bool IsUserType (TypeDefinition type)
{
return !MonoAndroidHelper.IsFrameworkAssembly (type.Module.Assembly.Name.Name + ".dll");
return !MonoAndroidHelper.IsFrameworkAssembly (type.Module.Assembly);
}

void PreserveImplementor (TypeDefinition type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<PackageReference Include="Microsoft.NET.ILLink" Version="$(MicrosoftNETILLinkTasksPackageVersion)" />
<ProjectReference Include="..\Xamarin.Android.Build.Tasks\Xamarin.Android.Build.Tasks.csproj" ReferenceOutputAssembly="False" />

<Compile Include="..\Xamarin.Android.Build.Tasks\obj\$(Configuration)\Profile.g.cs" Link="Profile.g.cs" />

<!--Include shared linker sources-->
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\AddKeepAlivesStep.cs" Link="MonoDroid.Tuner\AddKeepAlivesStep.cs" />
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\AndroidLinkConfiguration.cs" Link="MonoDroid.Tuner\AndroidLinkConfiguration.cs" />
Expand All @@ -21,8 +19,6 @@
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\FixLegacyResourceDesignerStep.cs" Link="MonoDroid.Tuner\FixLegacyResourceDesignerStep.cs" />
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\LinkDesignerBase.cs" Link="MonoDroid.Tuner\LinkDesignerBase.cs" />
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\RemoveResourceDesignerStep.cs" Link="MonoDroid.Tuner\RemoveResourceDesignerStep.cs" />
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\MonoDroidProfile.cs" Link="MonoDroid.Tuner\MonoDroidProfile.cs" />
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\Mobile.Tuner\MobileProfile.cs" Link="Mobile.Tuner\MobileProfile.cs" />

<!--Other .NET for Android / Java.Interop files-->
<Compile Include="..\..\external\Java.Interop\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil\CustomAttributeProviderRocks.cs" Link="Java.Interop\CustomAttributeProviderRocks.cs" />
Expand Down
7 changes: 2 additions & 5 deletions src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections;
using System.Linq;

using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;

using Mono.Tuner;
using Mobile.Tuner;

using Mono.Cecil;
using Xamarin.Android.Tasks;

namespace MonoDroid.Tuner {

Expand Down
13 changes: 8 additions & 5 deletions src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Android.Sdk.ILLink;
using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;

using Mono.Cecil;
using Microsoft.Android.Sdk.ILLink;
using Xamarin.Android.Tasks;

namespace Mono.Tuner {

Expand All @@ -24,7 +23,11 @@ public override SubStepTargets Targets {

public override bool IsActiveFor (AssemblyDefinition assembly)
{
return !Profile.IsSdkAssembly (assembly);
if (MonoAndroidHelper.IsFrameworkAssembly (assembly))
return false;

return assembly.MainModule.HasTypeReference ("Java.Interop.ExportAttribute") ||
assembly.MainModule.HasTypeReference ("Java.Interop.ExportFieldAttribute");
}

public override void ProcessField (FieldDefinition field)
Expand Down
7 changes: 2 additions & 5 deletions src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections;
using System.Linq;

using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;

using Mono.Tuner;
using Mobile.Tuner;

using Mono.Cecil;
using Xamarin.Android.Tasks;

namespace MonoDroid.Tuner {

Expand Down
52 changes: 0 additions & 52 deletions src/Microsoft.Android.Sdk.ILLink/Profile.cs

This file was deleted.

20 changes: 0 additions & 20 deletions src/Microsoft.Android.Sdk.ILLink/SetupStep.cs

This file was deleted.

8 changes: 2 additions & 6 deletions src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ protected override void ProcessAssembly (AssemblyDefinition assembly)
if (action == AssemblyAction.Skip || action == AssemblyAction.Delete)
return;

var fileName = assembly.Name.Name + ".dll";
if (MonoAndroidHelper.IsFrameworkAssembly (fileName) &&
!MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (fileName) &&
!MonoAndroidHelper.FrameworkEmbeddedNativeLibraryAssemblies.Contains (fileName))
if (MonoAndroidHelper.IsFrameworkAssembly (assembly))
return;

bool assembly_modified = false;
foreach (var mod in assembly.Modules) {
foreach (var r in mod.Resources.ToArray ()) {
if (ShouldStripResource (r)) {
Context.LogMessage ($" Stripped {r.Name} from {fileName}");
Context.LogMessage ($" Stripped {r.Name} from {assembly.Name.Name}.dll");
mod.Resources.Remove (r);
assembly_modified = true;
}
Expand Down
Loading

0 comments on commit 7aed4e3

Please sign in to comment.