Skip to content

Commit 1b7d73b

Browse files
authored
[One .NET] Add AddKeepAlivesStep (#5447)
Fixes: #5427 Context: e88cfbc Update the .NET 6 linker to include `AddKeepAlivesStep.cs`, update `AddKeepAlivesStep` so that it builds on .NET 6, add the custom step to the .NET 6 `illink` custom step, and let the `LinkerTests.AndroidAddKeepAlives()` test run on .NET 6.
1 parent 55dbc33 commit 1b7d73b

File tree

7 files changed

+24
-10
lines changed

7 files changed

+24
-10
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\LinkerOptions.cs" />
2929

3030
<!--Steps that are upstreamed, these are OK to remove-->
31-
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\AddKeepAlivesStep.cs" />
3231
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\ApplyPreserveAttribute.cs" />
3332
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\OutputStepWithTimestamps.cs" />
3433
<Compile Remove="..\Xamarin.Android.Build.Tasks\Linker\MonoDroid.Tuner\PreserveCode.cs" />

src/Microsoft.Android.Sdk.ILLink/SetupStep.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ protected override void Process ()
5252
if (Context.TryGetCustomData ("ProguardConfiguration", out proguardPath))
5353
InsertAfter (new GenerateProguardConfiguration (proguardPath), "CleanStep");
5454

55+
string addKeepAlivesStep;
56+
if (Context.TryGetCustomData ("AddKeepAlivesStep", out addKeepAlivesStep) && bool.TryParse (addKeepAlivesStep, out var bv) && bv)
57+
InsertAfter (new AddKeepAlivesStep (cache), "CleanStep");
58+
5559
InsertAfter (new StripEmbeddedLibraries (), "CleanStep");
5660
}
5761

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ bool AddKeepAlives (TypeDefinition type)
106106
changed = true;
107107

108108
if (methodKeepAlive == null)
109-
methodKeepAlive = Context.GetMethod ("mscorlib", "System.GC", "KeepAlive", new string [] { "System.Object" });
109+
methodKeepAlive = Context.GetMethod (
110+
#if NETCOREAPP
111+
"System.Private.CoreLib",
112+
#else
113+
"mscorlib",
114+
#endif
115+
"System.GC", "KeepAlive", new string [] { "System.Object" });
110116

111117
processor.InsertBefore (end, GetLoadArgumentInstruction (method.IsStatic ? i : i + 1, method.Parameters [i]));
112118
processor.InsertBefore (end, Instruction.Create (OpCodes.Call, module.ImportReference (methodKeepAlive)));

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Extensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ public static object GetSettableValue (this CustomAttributeArgument arg)
4646
return td != null ? td.FullName + "," + td.Module.Assembly.FullName : arg.Value;
4747
}
4848

49-
#if !NETCOREAPP
5049
public static AssemblyDefinition GetAssembly (this LinkContext context, string assemblyName)
5150
{
5251
AssemblyDefinition ad;
52+
#if !NETCOREAPP
5353
context.TryGetLinkedAssembly (assemblyName, out ad);
54+
#else
55+
ad = context.GetLoadedAssembly (assemblyName);
56+
#endif
5457
return ad;
5558
}
5659

@@ -60,15 +63,14 @@ public static TypeDefinition GetType (this LinkContext context, string assemblyN
6063
return ad == null ? null : GetType (ad, typeName);
6164
}
6265

63-
public static MethodDefinition GetMethod (this LinkContext context, string ns, string typeName, string name, string [] parameters)
66+
public static MethodDefinition GetMethod (this LinkContext context, string assemblyName, string typeName, string name, string [] parameters)
6467
{
65-
var type = context.GetType (ns, typeName);
68+
var type = context.GetType (assemblyName, typeName);
6669
if (type == null)
6770
return null;
6871

6972
return GetMethod (type, name, parameters);
7073
}
71-
#endif
7274

7375
public static MethodDefinition GetMethod (TypeDefinition td, string name)
7476
{

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This file contains the .NET 5-specific targets to customize ILLink
2626
<Target Name="_PrepareLinking"
2727
Condition=" '$(PublishTrimmed)' == 'true' "
2828
AfterTargets="ComputeResolvedFilesToPublishList"
29-
DependsOnTargets="GetReferenceAssemblyPaths">
29+
DependsOnTargets="GetReferenceAssemblyPaths;_CreatePropertiesCache">
3030
<ItemGroup>
3131
<!-- Mark all assemblies to be linked for AndroidLinkMode=Full -->
3232
<ResolvedFileToPublish
@@ -51,6 +51,10 @@ This file contains the .NET 5-specific targets to customize ILLink
5151
<_AdditionalTaskAssemblyDirectory>$(XamarinSdkRootDirectory)tools/dotnet-linker/</_AdditionalTaskAssemblyDirectory>
5252
<_AdditionalTaskAssembly>$(_AdditionalTaskAssemblyDirectory)dotnet-linker.dll</_AdditionalTaskAssembly>
5353
</PropertyGroup>
54+
<PropertyGroup
55+
Condition=" '$(AndroidAddKeepAlives)' != '' ">
56+
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --custom-data AddKeepAlivesStep="$(AndroidAddKeepAlives)"</_ExtraTrimmerArgs>
57+
</PropertyGroup>
5458
<PropertyGroup
5559
Condition=" '$(_ProguardProjectConfiguration)' != '' ">
5660
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --custom-data ProguardConfiguration="$(_ProguardProjectConfiguration)"</_ExtraTrimmerArgs>

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void BuildBasicApplication ([ValueSource (nameof (SupportedTargetFramewor
5656
}
5757
}
5858

59-
string GetLinkedPath (ProjectBuilder builder, bool isRelease, string filename)
59+
public static string GetLinkedPath (ProjectBuilder builder, bool isRelease, string filename)
6060
{
6161
return Builder.UseDotNet && isRelease ?
6262
builder.Output.GetIntermediaryPath (Path.Combine ("android.21-arm64", "linked", filename)) :

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ public AttributedButtonStub (Context context) : base (context)
290290
}
291291

292292
[Test]
293-
[Category ("DotNetIgnore")]
294293
public void AndroidAddKeepAlives ()
295294
{
296295
var proj = new XamarinAndroidApplicationProject {
@@ -327,7 +326,7 @@ public unsafe bool MyMethod (Android.OS.IBinder windowToken, [global::Android.Ru
327326
using (var b = CreateApkBuilder ()) {
328327
Assert.IsTrue (b.Build (proj), "Building a project should have succeded.");
329328

330-
var assemblyPath = b.Output.GetIntermediaryPath (Path.Combine ("android", "assets", "UnnamedProject.dll"));
329+
var assemblyPath = BuildTest.GetLinkedPath (b, true, "UnnamedProject.dll");
331330
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
332331
Assert.IsTrue (assembly != null);
333332

0 commit comments

Comments
 (0)