-
Notifications
You must be signed in to change notification settings - Fork 535
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
[XABT] Create separate assembly preparation tasks. #9637
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
221 changes: 0 additions & 221 deletions
221
src/Xamarin.Android.Build.Tasks/Tasks/CollectAssemblyFilesForArchive.cs
This file was deleted.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
src/Xamarin.Android.Build.Tasks/Tasks/CompressAssemblies.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Android.Build.Tasks; | ||
using Microsoft.Build.Framework; | ||
using Xamarin.Android.Tools; | ||
|
||
namespace Xamarin.Android.Tasks; | ||
|
||
/// <summary> | ||
/// Compresses assemblies using LZ4 compression before placing them in the APK. | ||
/// Note this is independent of whether they are stored compressed with ZIP in the APK. | ||
/// Our runtime bits will LZ4 decompress them at assembly load time. | ||
/// </summary> | ||
public class CompressAssemblies : AndroidTask | ||
{ | ||
public override string TaskPrefix => "CAS"; | ||
|
||
[Required] | ||
public string ApkOutputPath { get; set; } = ""; | ||
|
||
public bool EmbedAssemblies { get; set; } | ||
|
||
[Required] | ||
public bool EnableCompression { get; set; } | ||
|
||
public bool IncludeDebugSymbols { get; set; } | ||
|
||
[Required] | ||
public string ProjectFullPath { get; set; } = ""; | ||
|
||
[Required] | ||
public ITaskItem [] ResolvedFrameworkAssemblies { get; set; } = []; | ||
|
||
[Required] | ||
public ITaskItem [] ResolvedUserAssemblies { get; set; } = []; | ||
|
||
[Required] | ||
public string [] SupportedAbis { get; set; } = []; | ||
|
||
[Output] | ||
public ITaskItem [] ResolvedFrameworkAssembliesOutput { get; set; } = []; | ||
|
||
[Output] | ||
public ITaskItem [] ResolvedUserAssembliesOutput { get; set; } = []; | ||
|
||
public override bool RunTask () | ||
{ | ||
if (IncludeDebugSymbols || !EnableCompression || !EmbedAssemblies) { | ||
ResolvedFrameworkAssembliesOutput = ResolvedFrameworkAssemblies; | ||
ResolvedUserAssembliesOutput = ResolvedUserAssemblies; | ||
return true; | ||
} | ||
|
||
var compressed_assemblies_info = GetCompressedAssemblyInfo (); | ||
|
||
// Get all the user and framework assemblies we may need to compresss | ||
var assemblies = ResolvedFrameworkAssemblies.Concat (ResolvedUserAssemblies).Where (asm => !(ShouldSkipAssembly (asm))).ToArray (); | ||
var per_arch_assemblies = MonoAndroidHelper.GetPerArchAssemblies (assemblies, SupportedAbis, true); | ||
var compressed_output_dir = Path.GetFullPath (Path.Combine (Path.GetDirectoryName (ApkOutputPath), "..", "lz4")); | ||
|
||
foreach (var kvp in per_arch_assemblies) { | ||
Log.LogDebugMessage ($"Compressing assemblies for architecture '{kvp.Key}'"); | ||
|
||
foreach (var asm in kvp.Value.Values) { | ||
MonoAndroidHelper.LogIfReferenceAssembly (asm, Log); | ||
|
||
var compressed_assembly = AssemblyCompression.Compress (Log, asm, compressed_assemblies_info, compressed_output_dir); | ||
|
||
if (compressed_assembly.HasValue ()) { | ||
Log.LogDebugMessage ($"Compressed '{asm.ItemSpec}' to '{compressed_assembly}'."); | ||
asm.SetMetadata ("CompressedAssembly", compressed_assembly); | ||
} | ||
} | ||
} | ||
|
||
ResolvedFrameworkAssembliesOutput = ResolvedFrameworkAssemblies; | ||
ResolvedUserAssembliesOutput = ResolvedUserAssemblies; | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>> GetCompressedAssemblyInfo () | ||
{ | ||
var key = CompressedAssemblyInfo.GetKey (ProjectFullPath); | ||
Log.LogDebugMessage ($"Retrieving assembly compression info with key '{key}'"); | ||
|
||
var compressedAssembliesInfo = BuildEngine4.UnregisterTaskObjectAssemblyLocal<IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>> (key, RegisteredTaskObjectLifetime.Build); | ||
|
||
if (compressedAssembliesInfo is null) | ||
throw new InvalidOperationException ($"Assembly compression info not found for key '{key}'. Compression will not be performed."); | ||
|
||
return compressedAssembliesInfo; | ||
} | ||
|
||
bool ShouldSkipAssembly (ITaskItem asm) | ||
{ | ||
var should_skip = asm.GetMetadataOrDefault ("AndroidSkipAddToPackage", false); | ||
|
||
if (should_skip) | ||
Log.LogDebugMessage ($"Skipping {asm.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' "); | ||
|
||
return should_skip; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 old code was like this, but...
What saves this
RegisterTaskObject
value? If we can avoid it, I wouldn't recommend usingRegisterTaskObject
here as a project could have:The two would overwrite each other with this value for the
key
. TheCompressedAssemblyInfo
type will be incompatible between the two assemblies under .NET framework because their versions do not match.Can we calculate the value within this task and not use
RegisterTaskObject
?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.
It's generated here:
android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateCompressedAssembliesNativeSourceFiles.cs
Line 88 in fec3b79
The problem is that we have to generate the LLVM IR assembly sources way, way before we package the assemblies and the info gathered while generating the native assembly sources is crucial to the correct construction of the assembly store. We allocate buffers of specific size for each of the assemblies, so that decompressed data for each assembly gets a buffer of the correct size. This saves memory. The way it works is that each assembly has an index, allocated while generating the native assembler sources, and that index is used at run time to find the correct buffer.
Maybe the assemblies could be compressed just before generation step for the native assembler sources, but I don't know if it isn't too early.
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.
From what I could tell, the only thing that gets used from this is the
"DescriptorIndex"
:android/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyCompression.cs
Line 136 in f9f421b
It feels like this could be a piece of metadata added to the
@(Item)
instead, but I certainly haven't decoded the full picture.Any change here should likely be done in its own PR.