Skip to content

[illink] Add AddKeepAlivesStep #5447

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

Merged
merged 3 commits into from
Jan 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<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\AddKeepAlivesStep.cs" />
<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" />
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.Android.Sdk.ILLink/SetupStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ protected override void Process ()
if (Context.TryGetCustomData ("ProguardConfiguration", out proguardPath))
InsertAfter (new GenerateProguardConfiguration (proguardPath), "CleanStep");

string addKeepAlivesStep;
if (Context.TryGetCustomData ("AddKeepAlivesStep", out addKeepAlivesStep) && bool.TryParse (addKeepAlivesStep, out var bv) && bv)
InsertAfter (new AddKeepAlivesStep (cache), "CleanStep");

InsertAfter (new StripEmbeddedLibraries (), "CleanStep");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ bool AddKeepAlives (TypeDefinition type)
changed = true;

if (methodKeepAlive == null)
methodKeepAlive = Context.GetMethod ("mscorlib", "System.GC", "KeepAlive", new string [] { "System.Object" });
methodKeepAlive = Context.GetMethod (
#if NETCOREAPP
"System.Private.CoreLib",
#else
"mscorlib",
#endif
"System.GC", "KeepAlive", new string [] { "System.Object" });

processor.InsertBefore (end, GetLoadArgumentInstruction (method.IsStatic ? i : i + 1, method.Parameters [i]));
processor.InsertBefore (end, Instruction.Create (OpCodes.Call, module.ImportReference (methodKeepAlive)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ public static object GetSettableValue (this CustomAttributeArgument arg)
return td != null ? td.FullName + "," + td.Module.Assembly.FullName : arg.Value;
}

#if !NETCOREAPP
public static AssemblyDefinition GetAssembly (this LinkContext context, string assemblyName)
{
AssemblyDefinition ad;
#if !NETCOREAPP
context.TryGetLinkedAssembly (assemblyName, out ad);
#else
ad = context.GetLoadedAssembly (assemblyName);
#endif
return ad;
}

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

public static MethodDefinition GetMethod (this LinkContext context, string ns, string typeName, string name, string [] parameters)
public static MethodDefinition GetMethod (this LinkContext context, string assemblyName, string typeName, string name, string [] parameters)
{
var type = context.GetType (ns, typeName);
var type = context.GetType (assemblyName, typeName);
if (type == null)
return null;

return GetMethod (type, name, parameters);
}
#endif

public static MethodDefinition GetMethod (TypeDefinition td, string name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This file contains the .NET 5-specific targets to customize ILLink
<Target Name="_PrepareLinking"
Condition=" '$(PublishTrimmed)' == 'true' "
AfterTargets="ComputeResolvedFilesToPublishList"
DependsOnTargets="GetReferenceAssemblyPaths">
DependsOnTargets="GetReferenceAssemblyPaths;_CreatePropertiesCache">
<ItemGroup>
<!-- Mark all assemblies to be linked for AndroidLinkMode=Full -->
<ResolvedFileToPublish
Expand All @@ -51,6 +51,10 @@ This file contains the .NET 5-specific targets to customize ILLink
<_AdditionalTaskAssemblyDirectory>$(XamarinSdkRootDirectory)tools/dotnet-linker/</_AdditionalTaskAssemblyDirectory>
<_AdditionalTaskAssembly>$(_AdditionalTaskAssemblyDirectory)dotnet-linker.dll</_AdditionalTaskAssembly>
</PropertyGroup>
<PropertyGroup
Condition=" '$(AndroidAddKeepAlives)' != '' ">
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --custom-data AddKeepAlivesStep="$(AndroidAddKeepAlives)"</_ExtraTrimmerArgs>
</PropertyGroup>
<PropertyGroup
Condition=" '$(_ProguardProjectConfiguration)' != '' ">
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --custom-data ProguardConfiguration="$(_ProguardProjectConfiguration)"</_ExtraTrimmerArgs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void BuildBasicApplication ([ValueSource (nameof (SupportedTargetFramewor
}
}

string GetLinkedPath (ProjectBuilder builder, bool isRelease, string filename)
public static string GetLinkedPath (ProjectBuilder builder, bool isRelease, string filename)
{
return Builder.UseDotNet && isRelease ?
builder.Output.GetIntermediaryPath (Path.Combine ("android.21-arm64", "linked", filename)) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ public AttributedButtonStub (Context context) : base (context)
}

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

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

Expand Down