Skip to content
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] Break BuildApk into individual tasks for each content type. #9612

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
755 changes: 0 additions & 755 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs

This file was deleted.

54 changes: 30 additions & 24 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace Xamarin.Android.Tasks;

/// <summary>
/// Takes a list of files and adds them to an APK archive. If the APK archive already
/// exists, files are only added if they were changed.
/// exists, files are only added if they were changed. Note *ALL* files to be in the final
/// APK must be passed in via @(FilesToAddToArchive). This task will determine any unchanged files
/// and skip them, as well as remove any existing files in the APK that are no longer required.
/// </summary>
public class BuildArchive : AndroidTask
{
Expand All @@ -34,34 +36,17 @@ public class BuildArchive : AndroidTask

public string? ZipFlushSizeLimit { get; set; }

readonly HashSet<string> uncompressedFileExtensions;
readonly CompressionMethod uncompressedMethod = CompressionMethod.Store;
HashSet<string>? uncompressedFileExtensions;
HashSet<string> UncompressedFileExtensionsSet => uncompressedFileExtensions ??= ParseUncompressedFileExtensions ();

public BuildArchive ()
{
uncompressedFileExtensions = new HashSet<string> (StringComparer.OrdinalIgnoreCase);

foreach (var extension in UncompressedFileExtensions?.Split ([';', ','], StringSplitOptions.RemoveEmptyEntries) ?? []) {
var ext = extension.Trim ();

if (string.IsNullOrEmpty (ext)) {
continue;
}

if (ext [0] != '.') {
ext = $".{ext}";
}

uncompressedFileExtensions.Add (ext);
}
CompressionMethod uncompressedMethod = CompressionMethod.Store;

public override bool RunTask ()
{
// Nothing needs to be compressed with app bundles. BundleConfig.json specifies the final compression mode.
if (string.Compare (AndroidPackageFormat, "aab", true) == 0)
uncompressedMethod = CompressionMethod.Default;
}

public override bool RunTask ()
{
var refresh = true;

// If we have an input apk but no output apk, copy it to the output
Expand Down Expand Up @@ -251,6 +236,27 @@ CompressionMethod GetCompressionMethod (ITaskItem item)
return result;
}

return uncompressedFileExtensions.Contains (Path.GetExtension (item.ItemSpec)) ? uncompressedMethod : CompressionMethod.Default;
return UncompressedFileExtensionsSet.Contains (Path.GetExtension (item.ItemSpec)) ? uncompressedMethod : CompressionMethod.Default;
}

HashSet<string> ParseUncompressedFileExtensions ()
{
var uncompressedFileExtensions = new HashSet<string> (StringComparer.OrdinalIgnoreCase);

foreach (var extension in UncompressedFileExtensions?.Split ([';', ','], StringSplitOptions.RemoveEmptyEntries) ?? []) {
var ext = extension.Trim ();

if (string.IsNullOrEmpty (ext)) {
continue;
}

if (ext [0] != '.') {
ext = $".{ext}";
}

uncompressedFileExtensions.Add (ext);
}

return uncompressedFileExtensions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks;

/// <summary>
/// Collects TypeMap to be added to the final archive.
/// </summary>
public class CollectAssemblyFilesForArchive : AndroidTask
{
const string ArchiveAssembliesPath = "lib";
const string ArchiveLibPath = "lib";

public override string TaskPrefix => "CAF";

[Required]
public string AndroidBinUtilsDirectory { get; set; } = "";

[Required]
public string ApkOutputPath { get; set; } = "";

[Required]
public string AppSharedLibrariesDir { get; set; } = "";

public bool EmbedAssemblies { get; set; }

[Required]
public bool EnableCompression { get; set; }

public bool IncludeDebugSymbols { get; set; }

[Required]
public string IntermediateOutputPath { 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; } = [];

public bool UseAssemblyStore { get; set; }

[Output]
public ITaskItem [] FilesToAddToArchive { get; set; } = [];

public override bool RunTask ()
{
// If we aren't embedding assemblies, we don't need to do anything
if (!EmbedAssemblies)
return !Log.HasLoggedErrors;

var files = new PackageFileListBuilder ();

DSOWrapperGenerator.Config dsoWrapperConfig = DSOWrapperGenerator.GetConfig (Log, AndroidBinUtilsDirectory, IntermediateOutputPath);
bool compress = !IncludeDebugSymbols && EnableCompression;
IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>? compressedAssembliesInfo = null;

if (compress) {
string key = CompressedAssemblyInfo.GetKey (ProjectFullPath);
Log.LogDebugMessage ($"Retrieving assembly compression info with key '{key}'");
compressedAssembliesInfo = BuildEngine4.UnregisterTaskObjectAssemblyLocal<IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>> (key, RegisteredTaskObjectLifetime.Build);
if (compressedAssembliesInfo == null)
throw new InvalidOperationException ($"Assembly compression info not found for key '{key}'. Compression will not be performed.");
}

AddAssemblies (dsoWrapperConfig, files, IncludeDebugSymbols, compress, compressedAssembliesInfo, assemblyStoreApkName: null);

FilesToAddToArchive = files.ToArray ();

return !Log.HasLoggedErrors;
}

void AddAssemblies (DSOWrapperGenerator.Config dsoWrapperConfig, PackageFileListBuilder files, bool debug, bool compress, IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>? compressedAssembliesInfo, string? assemblyStoreApkName)
{
string compressedOutputDir = Path.GetFullPath (Path.Combine (Path.GetDirectoryName (ApkOutputPath), "..", "lz4"));
AssemblyStoreBuilder? storeBuilder = null;

if (UseAssemblyStore) {
storeBuilder = new AssemblyStoreBuilder (Log);
}

// Add user assemblies
AssemblyPackagingHelper.AddAssembliesFromCollection (Log, SupportedAbis, ResolvedUserAssemblies, (TaskLoggingHelper log, AndroidTargetArch arch, ITaskItem assembly) => DoAddAssembliesFromArchCollection (log, arch, assembly, dsoWrapperConfig, files, debug, compress, compressedAssembliesInfo, compressedOutputDir, storeBuilder));

// Add framework assemblies
AssemblyPackagingHelper.AddAssembliesFromCollection (Log, SupportedAbis, ResolvedFrameworkAssemblies, (TaskLoggingHelper log, AndroidTargetArch arch, ITaskItem assembly) => DoAddAssembliesFromArchCollection (log, arch, assembly, dsoWrapperConfig, files, debug, compress, compressedAssembliesInfo, compressedOutputDir, storeBuilder));

if (!UseAssemblyStore) {
return;
}

Dictionary<AndroidTargetArch, string> assemblyStorePaths = storeBuilder!.Generate (AppSharedLibrariesDir);

if (assemblyStorePaths.Count == 0) {
throw new InvalidOperationException ("Assembly store generator did not generate any stores");
}

if (assemblyStorePaths.Count != SupportedAbis.Length) {
throw new InvalidOperationException ("Internal error: assembly store did not generate store for each supported ABI");
}

string inArchivePath;
foreach (var kvp in assemblyStorePaths) {
string abi = MonoAndroidHelper.ArchToAbi (kvp.Key);
inArchivePath = MakeArchiveLibPath (abi, "lib" + Path.GetFileName (kvp.Value));
string wrappedSourcePath = DSOWrapperGenerator.WrapIt (Log, dsoWrapperConfig, kvp.Key, kvp.Value, Path.GetFileName (inArchivePath));
files.AddItem (wrappedSourcePath, inArchivePath);
}
}

void DoAddAssembliesFromArchCollection (TaskLoggingHelper log, AndroidTargetArch arch, ITaskItem assembly, DSOWrapperGenerator.Config dsoWrapperConfig, PackageFileListBuilder files, bool debug, bool compress, IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>? compressedAssembliesInfo, string compressedOutputDir, AssemblyStoreBuilder? storeBuilder)
{
// In the "all assemblies are per-RID" world, assemblies, pdb and config are disguised as shared libraries (that is,
// their names end with the .so extension) so that Android allows us to put them in the `lib/{ARCH}` directory.
// For this reason, they have to be treated just like other .so files, as far as compression rules are concerned.
// Thus, we no longer just store them in the apk but we call the `GetCompressionMethod` method to find out whether
// or not we're supposed to compress .so files.
var sourcePath = CompressAssembly (assembly, compress, compressedAssembliesInfo, compressedOutputDir);
if (UseAssemblyStore) {
storeBuilder!.AddAssembly (sourcePath, assembly, includeDebugSymbols: debug);
return;
}

// Add assembly
(string assemblyPath, string assemblyDirectory) = GetInArchiveAssemblyPath (assembly);
string wrappedSourcePath = DSOWrapperGenerator.WrapIt (Log, dsoWrapperConfig, arch, sourcePath, Path.GetFileName (assemblyPath));
files.AddItem (wrappedSourcePath, assemblyPath);

// Try to add config if exists
var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config");
AddAssemblyConfigEntry (dsoWrapperConfig, files, arch, assemblyDirectory, config);

// Try to add symbols if Debug
if (!debug) {
return;
}

string symbols = Path.ChangeExtension (assembly.ItemSpec, "pdb");
if (!File.Exists (symbols)) {
return;
}

string archiveSymbolsPath = assemblyDirectory + MonoAndroidHelper.MakeDiscreteAssembliesEntryName (Path.GetFileName (symbols));
string wrappedSymbolsPath = DSOWrapperGenerator.WrapIt (Log, dsoWrapperConfig, arch, symbols, Path.GetFileName (archiveSymbolsPath));
files.AddItem (wrappedSymbolsPath, archiveSymbolsPath);
}

/// <summary>
/// Returns the in-archive path for an assembly
/// </summary>
(string assemblyFilePath, string assemblyDirectoryPath) GetInArchiveAssemblyPath (ITaskItem assembly)
{
var parts = new List<string> ();

// The PrepareSatelliteAssemblies task takes care of properly setting `DestinationSubDirectory`, so we can just use it here.
string? subDirectory = assembly.GetMetadata ("DestinationSubDirectory")?.Replace ('\\', '/');
if (string.IsNullOrEmpty (subDirectory)) {
throw new InvalidOperationException ($"Internal error: assembly '{assembly}' lacks the required `DestinationSubDirectory` metadata");
}

string assemblyName = Path.GetFileName (assembly.ItemSpec);
// For discrete assembly entries we need to treat assemblies specially.
// All of the assemblies have their names mangled so that the possibility to clash with "real" shared
// library names is minimized. All of the assembly entries will start with a special character:
//
// `_` - for regular assemblies (e.g. `_Mono.Android.dll.so`)
// `-` - for satellite assemblies (e.g. `-es-Mono.Android.dll.so`)
//
// Second of all, we need to treat satellite assemblies with even more care.
// If we encounter one of them, we will return the culture as part of the path transformed
// so that it forms a `-culture-` assembly file name prefix, not a `culture/` subdirectory.
// This is necessary because Android doesn't allow subdirectories in `lib/{ABI}/`
//
string [] subdirParts = subDirectory!.TrimEnd ('/').Split ('/');
if (subdirParts.Length == 1) {
// Not a satellite assembly
parts.Add (subDirectory);
parts.Add (MonoAndroidHelper.MakeDiscreteAssembliesEntryName (assemblyName));
} else if (subdirParts.Length == 2) {
parts.Add (subdirParts [0]);
parts.Add (MonoAndroidHelper.MakeDiscreteAssembliesEntryName (assemblyName, subdirParts [1]));
} else {
throw new InvalidOperationException ($"Internal error: '{assembly}' `DestinationSubDirectory` metadata has too many components ({parts.Count} instead of 1 or 2)");
}

string assemblyFilePath = MonoAndroidHelper.MakeZipArchivePath (ArchiveAssembliesPath, parts);
return (assemblyFilePath, Path.GetDirectoryName (assemblyFilePath) + "/");
}

void AddAssemblyConfigEntry (DSOWrapperGenerator.Config dsoWrapperConfig, PackageFileListBuilder files, AndroidTargetArch arch, string assemblyPath, string configFile)
{
string inArchivePath = MonoAndroidHelper.MakeDiscreteAssembliesEntryName (assemblyPath + Path.GetFileName (configFile));

if (!File.Exists (configFile)) {
return;
}

string wrappedConfigFile = DSOWrapperGenerator.WrapIt (Log, dsoWrapperConfig, arch, configFile, Path.GetFileName (inArchivePath));

files.AddItem (wrappedConfigFile, inArchivePath);
}

string CompressAssembly (ITaskItem assembly, bool compress, IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>? compressedAssembliesInfo, string compressedOutputDir)
{
if (!compress) {
return assembly.ItemSpec;
}

// NRT: compressedAssembliesInfo is guaranteed to be non-null if compress is true
return AssemblyCompression.Compress (Log, assembly, compressedAssembliesInfo!, compressedOutputDir);
}

static string MakeArchiveLibPath (string abi, string fileName) => MonoAndroidHelper.MakeZipArchivePath (ArchiveLibPath, abi, fileName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#nullable enable

using System;
using System.IO;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;

namespace Xamarin.Android.Tasks;

/// <summary>
/// Collects Dalvik classes to be added to the final archive.
/// </summary>
public class CollectDalvikFilesForArchive : AndroidTask
{
public override string TaskPrefix => "CDF";

public string AndroidPackageFormat { get; set; } = "";

[Required]
public ITaskItem [] DalvikClasses { get; set; } = [];

[Output]
public ITaskItem [] FilesToAddToArchive { get; set; } = [];

public override bool RunTask ()
{
var dalvikPath = AndroidPackageFormat.Equals ("aab", StringComparison.InvariantCultureIgnoreCase) ? "dex/" : "";
var files = new PackageFileListBuilder ();

foreach (var dex in DalvikClasses) {
var apkName = dex.GetMetadata ("ApkName");
var dexPath = string.IsNullOrWhiteSpace (apkName) ? Path.GetFileName (dex.ItemSpec) : apkName;

files.AddItem (dex.ItemSpec, dalvikPath + dexPath);
}

FilesToAddToArchive = files.ToArray ();

return !Log.HasLoggedErrors;
}
}
Loading
Loading