Skip to content

Embed some data files in libxamarin-app.so #9367

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

Draft
wants to merge 60 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f3dfe90
Embed some data files in `libxamarin-app.so`
grendello Oct 3, 2024
58197ba
Use llvm-mc to embed the binary, this way we have full control
grendello Oct 7, 2024
d01e4e1
Runtime config binary blob is now fully embedded
grendello Oct 8, 2024
514a28a
Remove debug spam
grendello Oct 8, 2024
0081c36
New target to build embedded assembly store
grendello Oct 9, 2024
5d91471
New task to build embedded assembly store
grendello Oct 9, 2024
0c225e1
Move some of assembly packaging code to a helper class
grendello Oct 9, 2024
297097b
Move more assembly packaging code to the helper class
grendello Oct 9, 2024
45ad89a
Almost there, TBC
grendello Oct 9, 2024
2652e5e
Assembly store embedding works
grendello Oct 10, 2024
c188d66
Actually use the embedded assembly store
grendello Oct 10, 2024
12afaf7
A handful of bugfixes and performance tweaks
grendello Oct 10, 2024
15a5bf4
Fix DSO count when building
grendello Oct 11, 2024
9391224
Don't allocate memory for ZIP Central Directory data
grendello Oct 11, 2024
955da11
Experimenting with zip scanning performance
grendello Oct 11, 2024
dee133a
Post rebase fixups
grendello Oct 15, 2024
a27f435
More post rebase fixups
grendello Oct 15, 2024
963d760
seek and ye shall read
grendello Oct 15, 2024
460dc65
Details, details...
grendello Oct 16, 2024
001cfd8
Cleanup
grendello Oct 16, 2024
0907f4d
Teach assembly store explorer about embedded stores
grendello Oct 16, 2024
94d1a6a
Teach store v2 reader to look in `libxamarin-app.so`, too
grendello Oct 16, 2024
fd91482
Nicer
grendello Oct 16, 2024
7a063b3
The runtime configuration blob is now part of `libxamarin-app.so`
grendello Oct 17, 2024
f9f5556
Optionally produce "empty" embed.*.s files
grendello Oct 17, 2024
b04f22e
Don't embed assembly store when fastdev is used
grendello Oct 17, 2024
d53f711
Let's see if designer tests pass now
grendello Oct 17, 2024
1070f1c
Expect the unexpected, DTB doesn't specify any ABIs
grendello Oct 18, 2024
91f740f
[WIP] Builds but doesn't work
grendello Oct 18, 2024
525dd96
Let's see how it works now
grendello Oct 21, 2024
f95cc1f
Always generate embedded store sources in CreateEmbeddedAssemblyStore
grendello Oct 22, 2024
b3b644d
Let's see...
grendello Oct 22, 2024
23e2b2f
Don't assume assemblies exist
grendello Oct 23, 2024
85d9a16
better
grendello Oct 23, 2024
bdc737e
Update apkdesc files
grendello Oct 23, 2024
5acd749
Load embedded assembly store also in filesystem mode
grendello Oct 23, 2024
7ce1f65
Log it when an assembly is missing in ManifestDocument
grendello Oct 23, 2024
b1adce3
Don't throw
grendello Oct 23, 2024
9f85883
Always create the destination directory
grendello Oct 24, 2024
0cfed0e
Post-rebase fixups
grendello Oct 28, 2024
fe9d6cd
Let's see what's in the override dir listing
grendello Oct 28, 2024
d580269
Let's see
grendello Oct 28, 2024
76295b6
Cleanup
grendello Oct 29, 2024
1aebcf1
Address feedback
grendello Oct 31, 2024
0f868e9
Let's see what breaks
grendello Nov 4, 2024
98514e5
Revert "Let's see what breaks"
grendello Nov 5, 2024
1d9ad15
Restore [Require]
grendello Nov 6, 2024
3e5ca7d
Remove comment, no longer valid
grendello Nov 6, 2024
5fd11d3
Fix after rebase
grendello May 27, 2025
5a34466
Post-rebase fixes
grendello May 27, 2025
c73b422
A handful of NRT fixes + reuse shared code
grendello May 28, 2025
0af0848
MSBuild updates + cleanup
grendello May 28, 2025
f1b5ccc
Embed runtime config for MonoVM
grendello May 28, 2025
a32cd9f
Fix DSO counts
grendello May 28, 2025
6421670
Fix MonoVM diagnostic messages
grendello May 28, 2025
3714379
Add support for CoreCLR
grendello May 29, 2025
97751d6
It's gone
grendello May 29, 2025
67cf8be
Remove unnecessary app config field
grendello May 29, 2025
ac64906
Update apkdesc files
grendello May 29, 2025
9b79c81
Add a couple of tests
grendello May 30, 2025
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 @@ -29,4 +29,5 @@ This file is imported *after* the Microsoft.NET.Sdk/Sdk.targets.
<Import Project="Microsoft.Android.Sdk.Publish.targets" />
<Import Project="Microsoft.Android.Sdk.RuntimeConfig.targets" />
<Import Project="Microsoft.Android.Sdk.Tooling.targets" />
<Import Project="Microsoft.Android.Sdk.AssemblyStores.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="Xamarin.Android.Tasks.CreateEmbeddedAssemblyStore" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.PrepareAbiItems" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />

<Target Name="_PrepareCreateEmbeddedAssemblyStoreOutputItems">
<PrepareAbiItems
BuildTargetAbis="@(_BuildTargetAbis)"
NativeSourcesDir="$(_NativeAssemblySourceDir)"
Mode="EmbeddedAssemblyStore">
<Output TaskParameter="AssemblySources" ItemName="_EmbeddedAssemblyStoreSourceFiles" />
</PrepareAbiItems>

<ItemGroup>
<_CreateEmbeddedAssemblyStoreAssembly Include="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths);@(_ShrunkFrameworkAssemblies)"/>
<_CreateEmbeddedAssemblyStoreAssembly Condition=" '@(_CreateEmbeddedAssemblyStoreAssembly->Count())' == '0' " Include="@(_ResolvedUserAssemblies);@(_ResolvedFrameworkAssemblies);@(_AndroidResolvedSatellitePaths)" />
</ItemGroup>
</Target>

<Target Name="_CreateEmbeddedAssemblyStore"
DependsOnTargets="_PrepareCreateEmbeddedAssemblyStoreOutputItems"
Inputs="@(_CreateEmbeddedAssemblyStoreAssembly)"
Outputs="@(_EmbeddedAssemblyStoreSourceFiles)">
<CreateEmbeddedAssemblyStore
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
AppSharedLibrariesDir="$(_AndroidApplicationSharedLibraryPath)"
AssemblySourcesDir="$(IntermediateOutputPath)android"
AssemblyStoreEmbeddedInRuntime="$(_EmbedAssemblyStoreInRuntime)"
IncludeDebugSymbols="$(AndroidIncludeDebugSymbols)"
ResolvedUserAssemblies="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths)"
ResolvedFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)"
SupportedAbis="@(_BuildTargetAbis)" />

<ItemGroup>
<FileWrites Include="@(_EmbeddedAssemblyStoreSourceFiles)" />
</ItemGroup>
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ properties that determine build ordering.
<_PrepareBuildApkDependsOnTargets>
_SetLatestTargetFrameworkVersion;
_GetLibraryImports;
_RemoveRegisterAttribute;
Copy link
Member

Choose a reason for hiding this comment

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

Is this change needed? I didn't see anything in here that is trying to reorder MSBuild targets?

Maybe it's from before, trying to workaround the bug in #9452?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's possible, I don't remember exactly which test was affected by this change. I'll revert this bit and we'll see what (if anything) breaks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did this revert work?

_ResolveAssemblies;
_ResolveSatellitePaths;
_CreatePackageWorkspace;
Expand Down

This file was deleted.

117 changes: 19 additions & 98 deletions src/Xamarin.Android.Build.Tasks/Tasks/CompileNativeAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

using Xamarin.Android.Tools;
using Microsoft.Android.Build.Tasks;

namespace Xamarin.Android.Tasks
Expand Down Expand Up @@ -37,109 +33,34 @@ sealed class Config

public override System.Threading.Tasks.Task RunTaskAsync ()
{
return this.WhenAll (GetAssemblerConfigs (), RunAssembler);
var context = new NativeAssemblerCompilation.AssemblerRunContext (
Log,
Path.GetFullPath (WorkingDirectory),
registerForCancellation: RegisterForCancellation,
cancel: Cancel
);

return this.WhenAll (
GetAssemblerConfigs (),
(NativeAssemblerCompilation.AssemblerConfig config) => NativeAssemblerCompilation.RunAssembler (context, config)
);
}

void RunAssembler (Config config)
void RegisterForCancellation (Process proc)
{
var stdout_completed = new ManualResetEvent (false);
var stderr_completed = new ManualResetEvent (false);
var psi = new ProcessStartInfo () {
FileName = config.AssemblerPath,
Arguments = config.AssemblerOptions,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = WorkingDirectory,
};

string assemblerName = Path.GetFileName (config.AssemblerPath);
LogDebugMessage ($"[LLVM llc] {psi.FileName} {psi.Arguments}");

var stdoutLines = new List<string> ();
var stderrLines = new List<string> ();

using (var proc = new Process ()) {
proc.OutputDataReceived += (s, e) => {
if (e.Data != null) {
OnOutputData (assemblerName, s, e);
stdoutLines.Add (e.Data);
} else
stdout_completed.Set ();
};

proc.ErrorDataReceived += (s, e) => {
if (e.Data != null) {
OnErrorData (assemblerName, s, e);
stderrLines.Add (e.Data);
} else
stderr_completed.Set ();
};

proc.StartInfo = psi;
proc.Start ();
proc.BeginOutputReadLine ();
proc.BeginErrorReadLine ();
CancellationToken.Register (() => { try { proc.Kill (); } catch (Exception) { } });
proc.WaitForExit ();

if (psi.RedirectStandardError)
stderr_completed.WaitOne (TimeSpan.FromSeconds (30));

if (psi.RedirectStandardOutput)
stdout_completed.WaitOne (TimeSpan.FromSeconds (30));

if (proc.ExitCode != 0) {
var sb = MonoAndroidHelper.MergeStdoutAndStderrMessages (stdoutLines, stderrLines);
LogCodedError ("XA3006", Properties.Resources.XA3006, Path.GetFileName (config.InputSource), sb.ToString ());
Cancel ();
CancellationToken.Register (() => {
try {
proc.Kill ();
} catch (Exception) {
}
}
});
}

IEnumerable<Config> GetAssemblerConfigs ()
IEnumerable<NativeAssemblerCompilation.AssemblerConfig> GetAssemblerConfigs ()
{
const string assemblerOptions =
"-O2 " +
"--debugger-tune=lldb " + // NDK uses lldb now
"--debugify-level=location+variables " +
"--fatal-warnings " +
"--filetype=obj " +
"--relocation-model=pic";
string llcPath = Path.Combine (AndroidBinUtilsDirectory, "llc");

foreach (ITaskItem item in Sources) {
// We don't need the directory since our WorkingDirectory is where all the sources are
string sourceFile = Path.GetFileName (item.ItemSpec);
string outputFile = QuoteFileName (sourceFile.Replace (".ll", ".o"));
string executableDir = Path.GetDirectoryName (llcPath);
string executableName = MonoAndroidHelper.GetExecutablePath (executableDir, Path.GetFileName (llcPath));

yield return new Config {
InputSource = item.ItemSpec,
AssemblerPath = Path.Combine (executableDir, executableName),
AssemblerOptions = $"{assemblerOptions} -o={outputFile} {QuoteFileName (sourceFile)}",
};
yield return NativeAssemblerCompilation.GetAssemblerConfig (AndroidBinUtilsDirectory, item, stripFilePaths: true);
}
}

void OnOutputData (string assemblerName, object sender, DataReceivedEventArgs e)
{
LogDebugMessage ($"[{assemblerName} stdout] {e.Data}");
}

void OnErrorData (string assemblerName, object sender, DataReceivedEventArgs e)
{
LogMessage ($"[{assemblerName} stderr] {e.Data}", MessageImportance.High);
}

static string QuoteFileName (string fileName)
{
var builder = new CommandLineBuilder ();
builder.AppendFileNameIfNotNull (fileName);
return builder.ToString ();
}
}
}
39 changes: 3 additions & 36 deletions src/Xamarin.Android.Build.Tasks/Tasks/CreateAssemblyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,16 @@ public class CreateAssemblyStore : AndroidTask
public override bool RunTask ()
{
// Get all the user and framework assemblies we may need to package
var assemblies = ResolvedFrameworkAssemblies.Concat (ResolvedUserAssemblies).Where (asm => !(ShouldSkipAssembly (asm))).ToArray ();
var assemblies = ResolvedFrameworkAssemblies.Concat (ResolvedUserAssemblies).Where (asm => !(AssemblyPackagingHelper.ShouldSkipAssembly (Log, asm)));

if (!UseAssemblyStore) {
AssembliesToAddToArchive = assemblies;
AssembliesToAddToArchive = assemblies.ToArray ();
return !Log.HasLoggedErrors;
}

var store_builder = new AssemblyStoreBuilder (Log);
var per_arch_assemblies = MonoAndroidHelper.GetPerArchAssemblies (assemblies, SupportedAbis, true);

foreach (var kvp in per_arch_assemblies) {
Log.LogDebugMessage ($"Adding assemblies for architecture '{kvp.Key}'");

foreach (var assembly in kvp.Value.Values) {
var sourcePath = assembly.GetMetadataOrDefault ("CompressedAssembly", assembly.ItemSpec);
store_builder.AddAssembly (sourcePath, assembly, includeDebugSymbols: IncludeDebugSymbols);

Log.LogDebugMessage ($"Added '{sourcePath}' to assembly store.");
}
}

var assembly_store_paths = store_builder.Generate (AppSharedLibrariesDir);

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

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

var assembly_store_paths = AssemblyPackagingHelper.CreateAssemblyStore (Log, assemblies, AppSharedLibrariesDir, SupportedAbis, IncludeDebugSymbols);
AssembliesToAddToArchive = assembly_store_paths.Select (kvp => new TaskItem (kvp.Value, new Dictionary<string, string> { { "Abi", MonoAndroidHelper.ArchToAbi (kvp.Key) } })).ToArray ();

return !Log.HasLoggedErrors;
}

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks;

public class CreateEmbeddedAssemblyStore : AndroidTask
{
public override string TaskPrefix => "CEAS";

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

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

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

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

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

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

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

[Required]
public string [] SupportedAbis { get; set; } = [];

public override bool RunTask ()
{
if (AssemblyStoreEmbeddedInRuntime) {
return EmbedAssemblyStore ();
}

// Generate sources to satisfy libmonodroid's ABI requirements
foreach (string abi in SupportedAbis) {
ELFEmbeddingHelper.EmbedBinary (
Log,
abi,
AndroidBinUtilsDirectory,
inputFile: null,
ELFEmbeddingHelper.KnownEmbedItems.AssemblyStore,
AssemblySourcesDir,
missingContentOK: true
);
}

return !Log.HasLoggedErrors;
}

bool EmbedAssemblyStore ()
{
var assemblies = ResolvedFrameworkAssemblies.Concat (ResolvedUserAssemblies).Where (asm => !(AssemblyPackagingHelper.ShouldSkipAssembly (Log, asm)));
var assemblyStorePaths = AssemblyPackagingHelper.CreateAssemblyStore (
Log, assemblies,
Path.Combine (AppSharedLibrariesDir, "embedded"),
SupportedAbis,
IncludeDebugSymbols
);

foreach (var kvp in assemblyStorePaths) {
string abi = MonoAndroidHelper.ArchToAbi (kvp.Key);
string inputFile = kvp.Value;

ELFEmbeddingHelper.EmbedBinary (
Log,
abi,
AndroidBinUtilsDirectory,
inputFile,
ELFEmbeddingHelper.KnownEmbedItems.AssemblyStore,
AssemblySourcesDir,
missingContentOK: false
);
}

return !Log.HasLoggedErrors;
}
}
Loading
Loading