Skip to content

Commit 1c028ee

Browse files
committed
Let's see how it works now
1 parent b2d51b7 commit 1c028ee

File tree

8 files changed

+65
-272
lines changed

8 files changed

+65
-272
lines changed

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyStores.targets

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<UsingTask TaskName="Xamarin.Android.Tasks.CreateEmbeddedAssemblyStore" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
4+
<UsingTask TaskName="Xamarin.Android.Tasks.PrepareAbiItems" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
45

56
<Target Name="_PrepareCreateEmbeddedAssemblyStoreOutputItems">
6-
<!-- NOTE: the `embed_` prefix and `assembly_store` stem must be identical to the values used in ELFEmbeddingHelper -->
7-
<ItemGroup>
8-
<_EmbeddedAssemblyStoreObjectFile Include="$(_NativeAssemblySourceDir)embed_assembly_store.%(_BuildTargetAbis.Identity).o" />
9-
</ItemGroup>
10-
11-
<ItemGroup>
12-
<_EmbeddedAssemblyStoreSourceFiles Include="@(_EmbeddedAssemblyStoreObjectFile->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.s'))')" />
13-
</ItemGroup>
7+
<PrepareAbiItems
8+
BuildTargetAbis="@(_BuildTargetAbis)"
9+
NativeSourcesDir="$(_NativeAssemblySourceDir)"
10+
Mode="EmbeddedAssemblyStore">
11+
<Output TaskParameter="AssemblySources" ItemName="_EmbeddedAssemblyStoreSourceFiles" />
12+
</PrepareAbiItems>
1413
</Target>
1514

1615
<Target Name="_CreateEmbeddedAssemblyStore"
1716
Condition=" '$(_AndroidEmbedAssemblyStoreInRuntime)' == 'True' "
1817
DependsOnTargets="_PrepareCreateEmbeddedAssemblyStoreOutputItems"
1918
Inputs="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths);@(_ShrunkFrameworkAssemblies)"
20-
Outputs="@(_EmbeddedAssemblyStoreObjectFile);@(_EmbeddedAssemblyStoreSourceFiles)">
19+
Outputs="@(_EmbeddedAssemblyStoreSourceFiles)">
2120
<CreateEmbeddedAssemblyStore
2221
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
2322
AppSharedLibrariesDir="$(_AndroidApplicationSharedLibraryPath)"
@@ -28,10 +27,7 @@
2827
ProjectFullPath="$(MSBuildProjectFullPath)"
2928
ResolvedUserAssemblies="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths)"
3029
ResolvedFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)"
31-
SupportedAbis="@(_BuildTargetAbis)">
32-
<Output TaskParameter="EmbeddedObjectFiles" ItemName="_NativeAssemblyTarget" />
33-
<Output TaskParameter="NativeAssemblySources" ItemName="_EmbeddedAssemblyStoreSourceFiles" />
34-
</CreateEmbeddedAssemblyStore>
30+
SupportedAbis="@(_BuildTargetAbis)" />
3531

3632
<ItemGroup>
3733
<FileWrites Include="@(_EmbeddedAssemblyStoreSourceFiles)" />

src/Xamarin.Android.Build.Tasks/Tasks/CompileNativeAssembly.cs

Lines changed: 19 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5-
using System.Threading;
65

76
using Microsoft.Build.Framework;
8-
using Microsoft.Build.Utilities;
9-
10-
using Xamarin.Android.Tools;
117
using Microsoft.Android.Build.Tasks;
128

139
namespace Xamarin.Android.Tasks
@@ -16,13 +12,6 @@ public class CompileNativeAssembly : AsyncTask
1612
{
1713
public override string TaskPrefix => "CNA";
1814

19-
sealed class Config
20-
{
21-
public string AssemblerPath;
22-
public string AssemblerOptions;
23-
public string InputSource;
24-
}
25-
2615
[Required]
2716
public ITaskItem[] Sources { get; set; }
2817

@@ -37,124 +26,34 @@ sealed class Config
3726

3827
public override System.Threading.Tasks.Task RunTaskAsync ()
3928
{
40-
return this.WhenAll (GetAssemblerConfigs (), RunAssembler);
29+
var context = new NativeAssemblerCompilation.AssemblerRunContext (
30+
Log,
31+
Path.GetFullPath (WorkingDirectory),
32+
registerForCancellation: RegisterForCancellation,
33+
cancel: Cancel
34+
);
35+
36+
return this.WhenAll (
37+
GetAssemblerConfigs (),
38+
(NativeAssemblerCompilation.AssemblerConfig config) => NativeAssemblerCompilation.RunAssembler (context, config)
39+
);
4140
}
4241

43-
void RunAssembler (Config config)
42+
void RegisterForCancellation (Process proc)
4443
{
45-
var stdout_completed = new ManualResetEvent (false);
46-
var stderr_completed = new ManualResetEvent (false);
47-
var psi = new ProcessStartInfo () {
48-
FileName = config.AssemblerPath,
49-
Arguments = config.AssemblerOptions,
50-
UseShellExecute = false,
51-
RedirectStandardOutput = true,
52-
RedirectStandardError = true,
53-
CreateNoWindow = true,
54-
WindowStyle = ProcessWindowStyle.Hidden,
55-
WorkingDirectory = WorkingDirectory,
56-
};
57-
58-
string assemblerName = Path.GetFileName (config.AssemblerPath);
59-
LogDebugMessage ($"[LLVM llc] {psi.FileName} {psi.Arguments}");
60-
61-
var stdoutLines = new List<string> ();
62-
var stderrLines = new List<string> ();
63-
64-
using (var proc = new Process ()) {
65-
proc.OutputDataReceived += (s, e) => {
66-
if (e.Data != null) {
67-
OnOutputData (assemblerName, s, e);
68-
stdoutLines.Add (e.Data);
69-
} else
70-
stdout_completed.Set ();
71-
};
72-
73-
proc.ErrorDataReceived += (s, e) => {
74-
if (e.Data != null) {
75-
OnErrorData (assemblerName, s, e);
76-
stderrLines.Add (e.Data);
77-
} else
78-
stderr_completed.Set ();
79-
};
80-
81-
proc.StartInfo = psi;
82-
proc.Start ();
83-
proc.BeginOutputReadLine ();
84-
proc.BeginErrorReadLine ();
85-
CancellationToken.Register (() => { try { proc.Kill (); } catch (Exception) { } });
86-
proc.WaitForExit ();
87-
88-
if (psi.RedirectStandardError)
89-
stderr_completed.WaitOne (TimeSpan.FromSeconds (30));
90-
91-
if (psi.RedirectStandardOutput)
92-
stdout_completed.WaitOne (TimeSpan.FromSeconds (30));
93-
94-
if (proc.ExitCode != 0) {
95-
var sb = MonoAndroidHelper.MergeStdoutAndStderrMessages (stdoutLines, stderrLines);
96-
LogCodedError ("XA3006", Properties.Resources.XA3006, Path.GetFileName (config.InputSource), sb.ToString ());
97-
Cancel ();
44+
CancellationToken.Register (() => {
45+
try {
46+
proc.Kill ();
47+
} catch (Exception) {
9848
}
99-
}
49+
});
10050
}
10151

102-
static readonly List<string> llcArguments = new () {
103-
"-O2",
104-
"--debugger-tune=lldb", // NDK uses lldb now
105-
"--debugify-level=location+variables",
106-
"--fatal-warnings",
107-
"--filetype=obj",
108-
"--relocation-model=pic",
109-
};
110-
111-
static readonly List<string> llvmMcArguments = new () {
112-
"--assemble",
113-
"--filetype=obj",
114-
"-g",
115-
};
116-
117-
IEnumerable<Config> GetAssemblerConfigs ()
52+
IEnumerable<NativeAssemblerCompilation.AssemblerConfig> GetAssemblerConfigs ()
11853
{
119-
const string llcOptions =
120-
"-O2 " +
121-
"--debugger-tune=lldb " + // NDK uses lldb now
122-
"--debugify-level=location+variables " +
123-
"--fatal-warnings " +
124-
"--filetype=obj " +
125-
"--relocation-model=pic";
126-
string llcPath = Path.Combine (AndroidBinUtilsDirectory, "llc");
127-
12854
foreach (ITaskItem item in Sources) {
129-
// We don't need the directory since our WorkingDirectory is where all the sources are
130-
string sourceFile = Path.GetFileName (item.ItemSpec);
131-
string outputFile = QuoteFileName (Path.ChangeExtension (sourceFile, ".o"));
132-
string executableDir = Path.GetDirectoryName (llcPath);
133-
string executableName = MonoAndroidHelper.GetExecutablePath (executableDir, Path.GetFileName (llcPath));
134-
135-
yield return new Config {
136-
InputSource = item.ItemSpec,
137-
AssemblerPath = Path.Combine (executableDir, executableName),
138-
AssemblerOptions = $"{llcOptions} -o={outputFile} {QuoteFileName (sourceFile)}",
139-
};
55+
yield return NativeAssemblerCompilation.GetAssemblerConfig (AndroidBinUtilsDirectory, item, stripFilePaths: true);
14056
}
14157
}
142-
143-
void OnOutputData (string assemblerName, object sender, DataReceivedEventArgs e)
144-
{
145-
LogDebugMessage ($"[{assemblerName} stdout] {e.Data}");
146-
}
147-
148-
void OnErrorData (string assemblerName, object sender, DataReceivedEventArgs e)
149-
{
150-
LogMessage ($"[{assemblerName} stderr] {e.Data}", MessageImportance.High);
151-
}
152-
153-
static string QuoteFileName (string fileName)
154-
{
155-
var builder = new CommandLineBuilder ();
156-
builder.AppendFileNameIfNotNull (fileName);
157-
return builder.ToString ();
158-
}
15958
}
16059
}

src/Xamarin.Android.Build.Tasks/Tasks/CreateEmbeddedAssemblyStore.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ public class CreateEmbeddedAssemblyStore : AndroidTask
4343
[Required]
4444
public string [] SupportedAbis { get; set; }
4545

46-
[Output]
47-
public ITaskItem[] NativeAssemblySources { get; set; }
48-
49-
[Output]
50-
public ITaskItem[] EmbeddedObjectFiles { get; set; }
51-
5246
public override bool RunTask ()
5347
{
5448
bool compress = !Debug && EnableCompression;
@@ -71,14 +65,12 @@ public override bool RunTask ()
7165
// Add framework assemblies
7266
AssemblyPackagingHelper.AddAssembliesFromCollection (Log, SupportedAbis, ResolvedFrameworkAssemblies, DoAddAssembliesFromArchCollection);
7367

74-
var objectFiles = new List<ITaskItem> ();
75-
var sourceFiles = new List<ITaskItem> ();
7668
Dictionary<AndroidTargetArch, string> assemblyStorePaths = storeBuilder.Generate (Path.Combine (AppSharedLibrariesDir, "embedded"));
7769
foreach (var kvp in assemblyStorePaths) {
7870
string abi = MonoAndroidHelper.ArchToAbi (kvp.Key);
7971
string inputFile = kvp.Value;
8072

81-
List<ITaskItem> items = ELFEmbeddingHelper.EmbedBinary (
73+
ELFEmbeddingHelper.EmbedBinary (
8274
Log,
8375
abi,
8476
AndroidBinUtilsDirectory,
@@ -87,24 +79,8 @@ public override bool RunTask ()
8779
AssemblySourcesDir,
8880
missingContentOK: false
8981
);
90-
91-
if (items.Count == 0) {
92-
continue;
93-
}
94-
95-
objectFiles.AddRange (items);
96-
foreach (ITaskItem objectItem in items) {
97-
var sourceItem = new TaskItem (
98-
Path.ChangeExtension (objectItem.ItemSpec, ".s"),
99-
objectItem.CloneCustomMetadata ()
100-
);
101-
sourceFiles.Add (sourceItem);
102-
}
10382
}
10483

105-
NativeAssemblySources = sourceFiles.ToArray ();
106-
EmbeddedObjectFiles = objectFiles.ToArray ();
107-
10884
return !Log.HasLoggedErrors;
10985

11086
void DoAddAssembliesFromArchCollection (TaskLoggingHelper log, AndroidTargetArch arch, ITaskItem assembly)

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ public class GeneratePackageManagerJava : AndroidTask
6868
//[Required]
6969
public bool AssemblyStoreEmbeddedInRuntime { get; set; }
7070

71-
[Output]
72-
public ITaskItem[] EmbeddedObjectFiles { get; set; }
73-
7471
public bool EnableMarshalMethods { get; set; }
7572
public string RuntimeConfigBinFilePath { get; set; }
7673
public string BoundExceptionType { get; set; }
@@ -344,7 +341,7 @@ void AddEnvironment ()
344341
}
345342

346343
bool haveRuntimeConfigBlob = !String.IsNullOrEmpty (RuntimeConfigBinFilePath) && File.Exists (RuntimeConfigBinFilePath);
347-
List<ITaskItem> objectFilePaths = ELFEmbeddingHelper.EmbedBinary (
344+
ELFEmbeddingHelper.EmbedBinary (
348345
Log,
349346
SupportedAbis,
350347
AndroidBinUtilsDirectory,
@@ -354,8 +351,6 @@ void AddEnvironment ()
354351
missingContentOK: !haveRuntimeConfigBlob
355352
);
356353

357-
EmbeddedObjectFiles = objectFilePaths.ToArray ();
358-
359354
var jniRemappingNativeCodeInfo = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<GenerateJniRemappingNativeCode.JniRemappingNativeCodeInfo> (ProjectSpecificTaskObjectKey (GenerateJniRemappingNativeCode.JniRemappingNativeCodeInfoKey), RegisteredTaskObjectLifetime.Build);
360355
var appConfigAsmGen = new ApplicationConfigNativeAssemblyGenerator (environmentVariables, systemProperties, Log) {
361356
UsesMonoAOT = usesMonoAOT,

0 commit comments

Comments
 (0)