Skip to content

Commit d3bbc0c

Browse files
[illink] refactor code sharing between ILLink and MSBuild tasks (#9688)
We already have a few trimmer steps that run under both ILLink and the `<LinkAssembliesNoShrink/>` MSBuild task: * `AddKeepAlivesStep` * `FixAbstractMethodsStep` * `FixLegacyResourceDesignerStep` We want to move various parts of the build to become new trimmer steps like above. Previously, we also had to share these steps with the Xamarin.Android linker pipeline, but that's no longer the case. We can now cleanup how these trimmer steps are shared between ILLink and MSBuild. The benefits are: * The `<LinkAssembliesNoShrink/>` task no longer has a list of subclassed trimmer steps. * Much less `#if ILLINK` calls in shared trimmer steps. A new `MSBuildLinkContext` class will be used for running trimmer steps in an MSBuild task context. It handles MSBuild logging, etc. I also removed various Mono linker source code that is unused.
1 parent 627e239 commit d3bbc0c

24 files changed

+175
-779
lines changed

src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ProjectReference Include="..\Xamarin.Android.Build.Tasks\Xamarin.Android.Build.Tasks.csproj" ReferenceOutputAssembly="False" />
1313

1414
<!--Include shared linker sources-->
15+
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\External\Linker\BaseMarkHandler.cs" Link="External\BaseMarkHandler.cs" />
1516
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\AddKeepAlivesStep.cs" Link="MonoDroid.Tuner\AddKeepAlivesStep.cs" />
1617
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\AndroidLinkConfiguration.cs" Link="MonoDroid.Tuner\AndroidLinkConfiguration.cs" />
1718
<Compile Include="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\Extensions.cs" Link="MonoDroid.Tuner\Extensions.cs" />

src/Xamarin.Android.Build.Tasks/Linker/External/Linker.Steps/BaseStep.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Tracer Tracer {
4646
get { return _context.Tracer; }
4747
}
4848

49-
public MarkingHelpers MarkingHelpers => _context.MarkingHelpers;
49+
public void Initialize (LinkContext context) => _context = context;
5050

5151
public void Process (LinkContext context)
5252
{
@@ -79,5 +79,19 @@ protected virtual void EndProcess ()
7979
protected virtual void ProcessAssembly (AssemblyDefinition assembly)
8080
{
8181
}
82+
83+
public virtual void LogMessage (string message)
84+
{
85+
Context.LogMessage (message);
86+
}
87+
88+
public virtual void LogError (int code, string message)
89+
{
90+
#if ILLINK
91+
Context.LogMessage (MessageContainer.CreateCustomErrorMessage (message, code, origin: new MessageOrigin ()));
92+
#else // !ILLINK
93+
Context.LogError ($"XA{code}", message);
94+
#endif // !ILLINK
95+
}
8296
}
8397
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Mono.Linker.Steps
5+
{
6+
7+
/// <summary>
8+
/// This API supports the product infrastructure and is not intended to be used directly from your code.
9+
/// Extensibility point for custom logic that run during MarkStep, for marked members.
10+
/// </summary>
11+
public interface IMarkHandler
12+
{
13+
/// <summary>
14+
/// Initialize is called at the beginning of MarkStep. This should be
15+
/// used to perform global setup, and register callbacks through the
16+
/// MarkContext.Register* methods) to be called when pieces of IL are marked.
17+
/// </summary>
18+
void Initialize (LinkContext context, MarkContext markContext);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using Mono.Cecil;
6+
7+
namespace Mono.Linker.Steps
8+
{
9+
/// <summary>
10+
/// Context which can be used to register actions to call during MarkStep
11+
/// when various members are marked.
12+
/// </summary>
13+
public abstract class MarkContext
14+
{
15+
/// <summary>
16+
/// Register a callback that will be invoked once for each marked assembly.
17+
/// </summary>
18+
public abstract void RegisterMarkAssemblyAction (Action<AssemblyDefinition> action);
19+
20+
/// <summary>
21+
/// Register a callback that will be invoked once for each marked type.
22+
/// </summary>
23+
public abstract void RegisterMarkTypeAction (Action<TypeDefinition> action);
24+
25+
/// <summary>
26+
/// Register a callback that will be invoked once for each marked method.
27+
/// </summary>
28+
public abstract void RegisterMarkMethodAction (Action<MethodDefinition> action);
29+
}
30+
31+
// Used within MSBuild and tests
32+
class EmptyMarkContext : MarkContext
33+
{
34+
public override void RegisterMarkAssemblyAction (Action<AssemblyDefinition> action) { }
35+
36+
public override void RegisterMarkTypeAction (Action<TypeDefinition> action) { }
37+
38+
public override void RegisterMarkMethodAction (Action<MethodDefinition> action) { }
39+
}
40+
}

src/Xamarin.Android.Build.Tasks/Linker/External/Linker/Annotations.cs

-19
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public partial class AnnotationStore {
5959
protected readonly HashSet<CustomAttribute> marked_attributes = new HashSet<CustomAttribute> ();
6060
readonly HashSet<TypeDefinition> marked_types_with_cctor = new HashSet<TypeDefinition> ();
6161
protected readonly HashSet<TypeDefinition> marked_instantiated = new HashSet<TypeDefinition> ();
62-
protected readonly HashSet<MethodDefinition> indirectly_called = new HashSet<MethodDefinition>();
6362

6463

6564
public AnnotationStore (LinkContext context) => this.context = context;
@@ -167,24 +166,6 @@ public bool IsMarked (CustomAttribute attribute)
167166
return marked_attributes.Contains (attribute);
168167
}
169168

170-
public void MarkIndirectlyCalledMethod (MethodDefinition method)
171-
{
172-
if (!context.AddReflectionAnnotations)
173-
return;
174-
175-
indirectly_called.Add (method);
176-
}
177-
178-
public bool HasMarkedAnyIndirectlyCalledMethods ()
179-
{
180-
return indirectly_called.Count != 0;
181-
}
182-
183-
public bool IsIndirectlyCalled (MethodDefinition method)
184-
{
185-
return indirectly_called.Contains (method);
186-
}
187-
188169
public void MarkInstantiated (TypeDefinition type)
189170
{
190171
marked_instantiated.Add (type);

src/Microsoft.Android.Sdk.ILLink/BaseMarkHandler.cs src/Xamarin.Android.Build.Tasks/Linker/External/Linker/BaseMarkHandler.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Microsoft.Android.Sdk.ILLink;
21
using Mono.Cecil;
32
using Mono.Linker.Steps;
43

src/Xamarin.Android.Build.Tasks/Linker/External/Linker/ConsoleLogger.cs

-11
This file was deleted.

src/Xamarin.Android.Build.Tasks/Linker/External/Linker/ILogger.cs

-15
This file was deleted.

src/Xamarin.Android.Build.Tasks/Linker/External/Linker/IReflectionPatternRecorder.cs

-62
This file was deleted.

src/Xamarin.Android.Build.Tasks/Linker/External/Linker/KnownMembers.cs

-31
This file was deleted.

0 commit comments

Comments
 (0)