Skip to content

[XABT] Refactor <LinkAssembliesNoShrink>/<AssemblyModifierPipeline> tasks to use an actual pipeline. #10024

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 2 commits into from
Apr 16, 2025

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Apr 10, 2025

In order to continue moving forward with moving tasks from the <GenerateJavaStubs> task to our "additional linker steps", we need a more flexible assembly scanning/modification pipeline.

Specifically, we will need to run "RewriteMarshalMethods" in the <AssemblyModifierPipeline> task so that it happens for both linked and non-linked builds. However, we want to run it:

  • After the FindJavaObjectsStep step in the <AssemblyModifierPipeline> pipeline.
  • In the same loop as the other assembly modification steps in the <LinkAssembliesNoShrink> task (which is before <AssemblyModifierPipeline> steps run) so that we do not need to write the assemblies twice.

Although we could achieve these goals with enough hacks, instead refactor the steps into a proper, flexible "pipeline".

This pipeline provides the features we need:

  • Steps from both tasks can be run in any order
  • Steps from both tasks can modify assemblies, and only 1 write call will happen

Additionally, encapsulate the "should this step run on this assembly" logic to each step, so that the pipeline doesn't need to know about each step's requirements.

@jpobst jpobst force-pushed the dev/jpobst/new-pipeline branch from c1585c9 to f0627ef Compare April 10, 2025 20:49
@jpobst jpobst force-pushed the dev/jpobst/new-pipeline branch from f0627ef to a89dfa4 Compare April 10, 2025 22:29
@jpobst jpobst marked this pull request as ready for review April 11, 2025 00:31
public bool ProcessAssembly (AssemblyDefinition assembly, StepContext context)
{
// Only run this step on user Android assemblies
if (context.IsFrameworkAssembly || !context.IsAndroidAssembly)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line confuses me: context.isFrameworkAssembly appears to be MonoAndroidHelper.IsFrameworkAssembly():

public static bool IsFrameworkAssembly (string assembly) =>
KnownAssemblyNames.Contains (Path.GetFileNameWithoutExtension (assembly));

public static bool IsFrameworkAssembly (ITaskItem assembly)
{
// Known assembly names: Mono.Android, Java.Interop, etc.
if (IsFrameworkAssembly (assembly.ItemSpec))
return true;
// Known %(FrameworkReferenceName)
var frameworkReferenceName = assembly.GetMetadata ("FrameworkReferenceName") ?? "";
if (frameworkReferenceName == "Microsoft.Android") {
return true; // Microsoft.Android assemblies
}
if (frameworkReferenceName.StartsWith ("Microsoft.NETCore.", StringComparison.OrdinalIgnoreCase)) {
return true; // BCL assemblies
}
// Known %(NuGetPackageId) runtime pack names
return IsFromAKnownRuntimePack (assembly);
}

which looks like it should be true for Mono.Android.dll, and IsAndroidAssembly is set from MonoAndroidHelper.IsAndroidAssembly(), which would also be true for Mono.Andorid.dll.

So I read this as true || !true? And just get confused.

I think our naming needs an improvement. Relatedly: what is "Framework" supposed to mean in "IsFrameworkAssembly"? As per MonoAndroidHelper.Linker.cs, it means "assembly name is one of Mono.Android, Mono.Android.Export, or Java.Interop", but in general "framework" could also mean the entire .NET BCL. I think "Framework" may be causing me more confusion than enlightenment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add a StepContext.ShouldScanAssemblyForJCWs property, and let AssemblyModifierPipeline.cs sort it out?

Copy link
Contributor Author

@jpobst jpobst Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our naming needs an improvement. Relatedly: what is "Framework" supposed to mean in "IsFrameworkAssembly"? As per MonoAndroidHelper.Linker.cs, it means "assembly name is one of Mono.Android, Mono.Android.Export, or Java.Interop", but in general "framework" could also mean the entire .NET BCL. I think "Framework" may be causing me more confusion than enlightenment.

Agreed that this is confusing (this is what led to the temporary "WasScanned" tracking just so I could verify I translated the logic correctly). Here, there are two dimensions that influence whether this step runs:

  • IsFrameworkAssembly/IsUserAssembly
    • Framework: "dotnet" ships it, ex: System.Collections.dll, Mono.Android.dll
    • User: an assembly the user added, ex: MyApp.dll, Xamarin.AndroidX.Core.dll
  • IsAndroidAssembly/!IsAndroidAssembly
    • In this context, this is essentially: can it contain JLO objects or not

So I read this as true || !true? And just get confused.

Correct, the key is the word "user" in the comment. The AddKeepAlives step is only run on user assemblies that can contain JLO objects. We assume that everything we ship "in box" these days has keep alives built in, so we don't need to check things like Mono.Android.

Maybe we could add a StepContext.ShouldScanAssemblyForJCWs property, and let AssemblyModifierPipeline.cs sort it out?

The issue is that different steps have different criteria for whether they need to run.

  • JavaCallableWrappers: only scans user assemblies for JLOs in Debug mode, scans user and framework assemblies in Release mode
  • Typemaps: scans both user and framework assemblies for JLOs in both Debug/Release modes

Given this, I am happy with where the logic is, but maybe we can address naming so that it's less confusing. If we have better naming than Framework/User or Android we can certainly go with that. Another way to make it clearer might be to add some compound properties in StepContext like:

public class StepContext {
    public bool IsAndroidFrameworkAssembly => IsAndroidAssembly && IsFrameworkAssembly;
    public bool IsAndroidUserAssembly => IsAndroidAssembly && IsUserAssembly;
}

Then our check becomes the much clearer:

// Only run this step on user Android assemblies
if (!context.IsAndroidUserAssembly)
    return false;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decision was to use StepContext.IsAndroidUserAssembly for better clarity here. PR updated.

{
// Names of assemblies which don't have Mono.Android.dll references, or are framework assemblies, but which must
// be scanned for Java types.
static readonly HashSet<string> SpecialAssemblies = new HashSet<string> (StringComparer.OrdinalIgnoreCase) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many "copies" of this list do we have?!

See also MonoAndroidHelper.Linker.cs which has MonoAndroidHelper.KnownAssemblyNames, which includes:

  • Mono.Android
  • Mono.Android.Export
  • Java.Interop

The ones that "don't match" are Mono.Android.Export and Mono.Android.Runtime, which is probably more an oversight than intentional…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have/use SpecialAssembles instead of KnownAssemblyNames, in MonoAndroidHelper.Linker.cs?

The unsatisfying answer is I simply moved the existing logic.😉

The ones that "don't match" are Mono.Android.Export and Mono.Android.Runtime, which is probably more an oversight than intentional…

Maybe? We would likely need to audit all users of both lists to see what their intentions are.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unified the lists and CI isn't complaining, so I guess that's good enough! 🤷‍♂️

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had the one comment if we could get rid of some of the #if ILLINK. But seems OK to me as-is.

Comment on lines +52 to +55
public interface IAssemblyModifierPipelineStep
{
bool ProcessAssembly (AssemblyDefinition assembly, StepContext context);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this go in its own file, so then src\Microsoft.Android.Sdk.ILLink\Microsoft.Android.Sdk.ILLink.csproj could use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The biggest issue is that it would also require StepContext which would then require things like ITaskItem which I don't think exist in ILLink.

We would likely need to add more if #ILLINK to add dummy implementations of the missing types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if ITaskItem is required, then we shouldn't do it.

Comment on lines +14 to +16
#if !ILLINK
, IAssemblyModifierPipelineStep
#endif // !ILLINK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If src\Microsoft.Android.Sdk.ILLink\Microsoft.Android.Sdk.ILLink.csproj had IAssemblyModifierPipelineStep.cs we could drop the #if?

Comment on lines +31 to +40
#if !ILLINK
public bool ProcessAssembly (AssemblyDefinition assembly, StepContext context)
{
// Only run this step on user Android assemblies
if (!context.IsAndroidUserAssembly)
return false;

return AddKeepAlives (assembly);
}
#endif // !ILLINK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, maybe we could lose the #if?

@jpobst jpobst merged commit b54ec05 into main Apr 16, 2025
57 of 59 checks passed
@jpobst jpobst deleted the dev/jpobst/new-pipeline branch April 16, 2025 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants