-
Notifications
You must be signed in to change notification settings - Fork 544
[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
Conversation
c1585c9
to
f0627ef
Compare
…>` tasks to use an actual pipeline.
f0627ef
to
a89dfa4
Compare
public bool ProcessAssembly (AssemblyDefinition assembly, StepContext context) | ||
{ | ||
// Only run this step on user Android assemblies | ||
if (context.IsFrameworkAssembly || !context.IsAndroidAssembly) |
There was a problem hiding this comment.
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()
:
android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs
Lines 21 to 22 in 9ad492a
public static bool IsFrameworkAssembly (string assembly) => | |
KnownAssemblyNames.Contains (Path.GetFileNameWithoutExtension (assembly)); |
android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs
Lines 381 to 398 in 9ad492a
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
- Framework: "dotnet" ships it, ex:
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 inRelease
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;
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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…
There was a problem hiding this comment.
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 ofKnownAssemblyNames
, inMonoAndroidHelper.Linker.cs
?
The unsatisfying answer is I simply moved the existing logic.😉
The ones that "don't match" are
Mono.Android.Export
andMono.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.
There was a problem hiding this comment.
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! 🤷♂️
There was a problem hiding this 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.
public interface IAssemblyModifierPipelineStep | ||
{ | ||
bool ProcessAssembly (AssemblyDefinition assembly, StepContext context); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
#if !ILLINK | ||
, IAssemblyModifierPipelineStep | ||
#endif // !ILLINK |
There was a problem hiding this comment.
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
?
#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 |
There was a problem hiding this comment.
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
?
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:FindJavaObjectsStep
step in the<AssemblyModifierPipeline>
pipeline.<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:
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.