-
Notifications
You must be signed in to change notification settings - Fork 537
/
Copy pathGetAotArguments.cs
385 lines (321 loc) · 11.1 KB
/
GetAotArguments.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Java.Interop.Tools.Diagnostics;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Xamarin.Android.Tools;
namespace Xamarin.Android.Tasks
{
/// <summary>
/// The <Aot/> task subclasses this in "legacy" Xamarin.Android.
/// Called directly in .NET 5 to populate %(AotArguments) metadata.
/// </summary>
public class GetAotArguments : AndroidAsyncTask
{
public override string TaskPrefix => "GAOT";
[Required]
public string AndroidApiLevel { get; set; }
[Required]
public string AndroidAotMode { get; set; }
[Required]
public string AotOutputDirectory { get; set; }
[Required]
public string AndroidBinUtilsDirectory { get; set; }
/// <summary>
/// Will be blank in .NET 6+
/// </summary>
public string ManifestFile { get; set; }
/// <summary>
/// $(AndroidMinimumSupportedApiLevel) in .NET 6+
/// </summary>
public string MinimumSupportedApiLevel { get; set; }
public string RuntimeIdentifier { get; set; }
public string AndroidNdkDirectory { get; set; }
public bool EnableLLVM { get; set; }
public string AndroidSequencePointsMode { get; set; }
public ITaskItem [] Profiles { get; set; }
public bool UsingAndroidNETSdk { get; set; }
public string AotAdditionalArguments { get; set; }
[Output]
public string Arguments { get; set; }
[Output]
public string OutputDirectory { get; set; }
protected AotMode AotMode;
protected SequencePointsMode SequencePointsMode;
protected string SdkBinDirectory;
public static bool GetAndroidAotMode(string androidAotMode, out AotMode aotMode)
{
aotMode = AotMode.Normal;
switch ((androidAotMode ?? string.Empty).ToLowerInvariant().Trim())
{
case "":
case "none":
aotMode = AotMode.None;
return true;
case "normal":
aotMode = AotMode.Normal;
return true;
case "hybrid":
aotMode = AotMode.Hybrid;
return true;
case "full":
aotMode = AotMode.Full;
return true;
case "interpreter":
// We don't do anything here for this mode, this is just to set the flag for the XA
// runtime to initialize Mono in the interpreter "AOT" mode.
aotMode = AotMode.Interp;
return true;
}
return false;
}
public static bool TryGetSequencePointsMode (string value, out SequencePointsMode mode)
{
mode = SequencePointsMode.None;
switch ((value ?? string.Empty).ToLowerInvariant ().Trim ()) {
case "none":
mode = SequencePointsMode.None;
return true;
case "normal":
mode = SequencePointsMode.Normal;
return true;
case "offline":
mode = SequencePointsMode.Offline;
return true;
}
return false;
}
public override Task RunTaskAsync ()
{
NdkTools? ndk = NdkTools.Create (AndroidNdkDirectory, Log);
if (ndk == null) {
return Task.CompletedTask; // NdkTools.Create will log appropriate error
}
bool hasValidAotMode = GetAndroidAotMode (AndroidAotMode, out AotMode);
if (!hasValidAotMode) {
LogCodedError ("XA3002", Properties.Resources.XA3002, AndroidAotMode);
return Task.CompletedTask;
}
if (AotMode == AotMode.Interp) {
LogDebugMessage ("Interpreter AOT mode enabled");
return Task.CompletedTask;
}
TryGetSequencePointsMode (AndroidSequencePointsMode, out SequencePointsMode);
SdkBinDirectory = MonoAndroidHelper.GetOSBinPath ();
var abi = AndroidRidAbiHelper.RuntimeIdentifierToAbi (RuntimeIdentifier);
if (string.IsNullOrEmpty (abi)) {
Log.LogCodedError ("XA0035", Properties.Resources.XA0035, RuntimeIdentifier);
return Task.CompletedTask;
}
(_, string outdir, string mtriple, AndroidTargetArch arch) = GetAbiSettings (abi);
string toolPrefix = GetToolPrefix (ndk, arch, out int level);
Arguments = string.Join (",", GetAotOptions (ndk, arch, level, outdir, mtriple, toolPrefix));
OutputDirectory = outdir;
return Task.CompletedTask;
}
protected string GetToolPrefix (NdkTools ndk, AndroidTargetArch arch, out int level)
{
level = 0;
return EnableLLVM
? ndk.GetNdkToolPrefixForAOT (arch, level = GetNdkApiLevel (ndk, arch))
: Path.Combine (AndroidBinUtilsDirectory, $"{ndk.GetArchDirName (arch)}-");
}
int GetNdkApiLevel (NdkTools ndk, AndroidTargetArch arch)
{
AndroidAppManifest manifest = null;
if (!string.IsNullOrEmpty (ManifestFile)) {
manifest = AndroidAppManifest.Load (ManifestFile, MonoAndroidHelper.SupportedVersions);
}
int level;
if (manifest?.MinSdkVersion != null) {
level = manifest.MinSdkVersion.Value;
} else if (int.TryParse (MinimumSupportedApiLevel, out level)) {
// level already set
} else if (int.TryParse (AndroidApiLevel, out level)) {
// level already set
} else {
// Probably not ideal!
level = MonoAndroidHelper.SupportedVersions.MaxStableVersion.ApiLevel;
}
// Some Android API levels do not exist on the NDK level. Workaround this my mapping them to the
// most appropriate API level that does exist.
if (level == 6 || level == 7) level = 5;
else if (level == 10) level = 9;
else if (level == 11) level = 12;
else if (level == 20) level = 19;
else if (level == 22) level = 21;
else if (level == 23) level = 21;
// API levels below level 21 do not provide support for 64-bit architectures.
if (ndk.IsNdk64BitArch (arch) && level < 21) {
level = 21;
}
// We perform a downwards API level lookup search since we might not have hardcoded the correct API
// mapping above and we do not want to crash needlessly.
for (; level >= 5; level--) {
try {
ndk.GetDirectoryPath (NdkToolchainDir.PlatformLib, arch, level);
break;
} catch (InvalidOperationException ex) {
// Path not found, continue searching...
continue;
}
}
return level;
}
protected (string aotCompiler, string outdir, string mtriple, AndroidTargetArch arch) GetAbiSettings (string abi)
{
switch (abi) {
case "armeabi-v7a":
return (
Path.Combine (SdkBinDirectory, "cross-arm"),
Path.Combine (AotOutputDirectory, "armeabi-v7a"),
"armv7-linux-gnueabi",
AndroidTargetArch.Arm
);
case "arm64":
case "arm64-v8a":
case "aarch64":
return (
Path.Combine (SdkBinDirectory, "cross-arm64"),
Path.Combine (AotOutputDirectory, "arm64-v8a"),
"aarch64-linux-android",
AndroidTargetArch.Arm64
);
case "x86":
return (
Path.Combine (SdkBinDirectory, "cross-x86"),
Path.Combine (AotOutputDirectory, "x86"),
"i686-linux-android",
AndroidTargetArch.X86
);
case "x86_64":
return (
Path.Combine (SdkBinDirectory, "cross-x86_64"),
Path.Combine (AotOutputDirectory, "x86_64"),
"x86_64-linux-android",
AndroidTargetArch.X86_64
);
// case "mips":
default:
throw new Exception ("Unsupported Android target architecture ABI: " + abi);
}
}
/// <summary>
/// Returns a list of parameters to pass to the --aot switch
/// </summary>
protected List<string> GetAotOptions (NdkTools ndk, AndroidTargetArch arch, int level, string outdir, string mtriple, string toolPrefix)
{
List<string> aotOptions = new List<string> ();
if (Profiles != null && Profiles.Length > 0) {
aotOptions.Add ("profile-only");
foreach (var p in Profiles) {
var fp = Path.GetFullPath (p.ItemSpec);
aotOptions.Add ($"profile={fp}");
}
}
if (!string.IsNullOrEmpty (AotAdditionalArguments))
aotOptions.Add (AotAdditionalArguments);
if (SequencePointsMode == SequencePointsMode.Offline)
aotOptions.Add ($"msym-dir={outdir}");
if (AotMode != AotMode.Normal)
aotOptions.Add (AotMode.ToString ().ToLowerInvariant ());
aotOptions.Add ("asmwriter");
aotOptions.Add ($"mtriple={mtriple}");
aotOptions.Add ($"tool-prefix={toolPrefix}");
string ldName;
if (EnableLLVM) {
ldName = ndk.GetToolPath (NdkToolKind.Linker, arch, level);
if (!string.IsNullOrEmpty (ldName)) {
ldName = Path.GetFileName (ldName);
if (ldName.IndexOf ('-') >= 0) {
ldName = ldName.Substring (ldName.LastIndexOf ("-") + 1);
}
}
} else {
ldName = "ld";
}
string ldFlags = GetLdFlags (ndk, arch, level, toolPrefix);
// MUST be before `ld-flags`, otherwise Mono fails to parse it on Windows
if (!string.IsNullOrEmpty (ldName)) {
aotOptions.Add ($"ld-name={ldName}");
}
if (!string.IsNullOrEmpty (ldFlags)) {
aotOptions.Add ($"ld-flags={ldFlags}");
}
return aotOptions;
}
string GetLdFlags(NdkTools ndk, AndroidTargetArch arch, int level, string toolPrefix)
{
var toolchainPath = toolPrefix.Substring (0, toolPrefix.LastIndexOf (Path.DirectorySeparatorChar));
var ldFlags = string.Empty;
if (EnableLLVM) {
if (string.IsNullOrEmpty (AndroidNdkDirectory)) {
return null;
}
string androidLibPath = string.Empty;
try {
androidLibPath = ndk.GetDirectoryPath (NdkToolchainDir.PlatformLib, arch, level);
} catch (InvalidOperationException ex) {
Diagnostic.Error (5101, ex.Message);
}
string toolchainLibDir;
if (ndk.UsesClang)
toolchainLibDir = GetNdkToolchainLibraryDir (ndk, toolchainPath, arch);
else
toolchainLibDir = GetNdkToolchainLibraryDir (ndk, toolchainPath);
var libs = new List<string> ();
if (ndk.UsesClang) {
libs.Add ($"-L{toolchainLibDir.TrimEnd ('\\')}");
libs.Add ($"-L{androidLibPath.TrimEnd ('\\')}");
if (arch == AndroidTargetArch.Arm) {
// Needed for -lunwind to work
string compilerLibDir = Path.Combine (toolchainPath, "..", "sysroot", "usr", "lib", ndk.GetArchDirName (arch));
libs.Add ($"-L{compilerLibDir.TrimEnd ('\\')}");
}
}
libs.Add (Path.Combine (toolchainLibDir, "libgcc.a"));
libs.Add (Path.Combine (androidLibPath, "libc.so"));
libs.Add (Path.Combine (androidLibPath, "libm.so"));
if (UsingAndroidNETSdk) {
// NOTE: in .NET 6+ use space for the delimiter and escape spaces in paths
var escaped = libs.Select (l => l.Replace (" ", "\\ "));
ldFlags = string.Join (" ", escaped);
} else {
ldFlags = $"\\\"{string.Join ("\\\";\\\"", libs)}\\\"";
}
}
return ldFlags;
}
static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, string archDir = null)
{
var baseDir = Path.GetFullPath (Path.Combine (binDir, ".."));
string libDir = Path.Combine (baseDir, "lib", "gcc");
if (!String.IsNullOrEmpty (archDir))
libDir = Path.Combine (libDir, archDir);
var gccLibDir = Directory.EnumerateDirectories (libDir).ToList ();
gccLibDir.Sort ();
var libPath = gccLibDir.LastOrDefault ();
if (libPath == null) {
goto no_toolchain_error;
}
if (ndk.UsesClang)
return libPath;
gccLibDir = Directory.EnumerateDirectories (libPath).ToList ();
gccLibDir.Sort ();
libPath = gccLibDir.LastOrDefault ();
if (libPath == null) {
goto no_toolchain_error;
}
return libPath;
no_toolchain_error:
throw new Exception ("Could not find a valid NDK compiler toolchain library path");
}
static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, AndroidTargetArch arch)
{
return GetNdkToolchainLibraryDir (ndk, binDir, ndk.GetArchDirName (arch));
}
}
}