|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +using Microsoft.Android.Build.Tasks; |
| 6 | +using Microsoft.Build.Framework; |
| 7 | +using Xamarin.Android.Build.Tasks; |
| 8 | +using Xamarin.Android.Tools; |
| 9 | + |
| 10 | +namespace Xamarin.Android.Tasks; |
| 11 | + |
| 12 | +public class GenerateNativeAotLibraryLoadAssemblerSources : AndroidTask |
| 13 | +{ |
| 14 | + static readonly HashSet<string> KnownLibraryExtensions = new (StringComparer.OrdinalIgnoreCase) { |
| 15 | + ".so", |
| 16 | + ".dll", |
| 17 | + ".dylib", |
| 18 | + }; |
| 19 | + |
| 20 | + static readonly HashSet<string> LibraryNamesToIgnore = new (StringComparer.OrdinalIgnoreCase) { |
| 21 | + "*", |
| 22 | + "android", |
| 23 | + "c", |
| 24 | + "libandroid", |
| 25 | + "libandroid.so", |
| 26 | + "libc", |
| 27 | + "libc.dylib", |
| 28 | + "libc.so", |
| 29 | + "liblog", |
| 30 | + "liblog.so", |
| 31 | + "log", |
| 32 | + "xa-internal-api", |
| 33 | + }; |
| 34 | + |
| 35 | + public override string TaskPrefix => "GNALLAS"; |
| 36 | + |
| 37 | + [Required] |
| 38 | + public ITaskItem[] ResolvedAssemblies { get; set; } = []; |
| 39 | + |
| 40 | + [Required] |
| 41 | + public string SourcesOutputDirectory { get; set; } = ""; |
| 42 | + |
| 43 | + // Names of JNI initialization functions in 3rd party libraries. The |
| 44 | + // functions are REQUIRED to use the `JNI_OnLoad(JNIEnv*, void* reserved)` signature. |
| 45 | + // TODO: document it in `Documentation/` |
| 46 | + public ITaskItem[] CustomJniInitFunctions { get; set; } = []; |
| 47 | + |
| 48 | + public override bool RunTask () |
| 49 | + { |
| 50 | + if (ResolvedAssemblies.Length == 0) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + // We run in the inner build, there's going to be just a single RID |
| 55 | + string rid = MonoAndroidHelper.GetAssemblyRid (ResolvedAssemblies[0]); |
| 56 | + AndroidTargetArch targetArch = MonoAndroidHelper.RidToArch (rid); |
| 57 | + |
| 58 | + var assemblies = new Dictionary<string, ITaskItem> (StringComparer.OrdinalIgnoreCase); |
| 59 | + foreach (ITaskItem item in ResolvedAssemblies) { |
| 60 | + string name = MonoAndroidHelper.GetAssemblyNameWithCulture (item); |
| 61 | + if (assemblies.ContainsKey (name)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + assemblies.Add (name, item); |
| 66 | + } |
| 67 | + |
| 68 | + XAAssemblyResolver resolver = MonoAndroidHelper.MakeResolver (Log, useMarshalMethods: false, targetArch, assemblies, loadDebugSymbols: false); |
| 69 | + var pinvokeScanner = new PinvokeScanner (Log, debugLogging: false); |
| 70 | + List<PinvokeScanner.PinvokeEntryInfo> pinfos = pinvokeScanner.Scan (targetArch, resolver, ResolvedAssemblies); |
| 71 | + |
| 72 | + var seen = new HashSet<string> (LibraryNamesToIgnore, StringComparer.OrdinalIgnoreCase); |
| 73 | + var pinvokeLibraries = new HashSet<string> (StringComparer.OrdinalIgnoreCase); |
| 74 | + foreach (PinvokeScanner.PinvokeEntryInfo pinfo in pinfos) { |
| 75 | + if (seen.Contains (pinfo.LibraryName)) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + seen.Add (pinfo.LibraryName); |
| 80 | + |
| 81 | + // All the BCL libraries follow the standard naming pattern of `lib<NAME>`, make sure that's what we have |
| 82 | + pinvokeLibraries.Add (MakeCanonicalLibraryName (pinfo.LibraryName)); |
| 83 | + } |
| 84 | + |
| 85 | + // Take library names, match against NativeRuntimeComponents to see whether a |
| 86 | + // component has an init function associated with it. |
| 87 | + var bclComponents = new NativeRuntimeComponents (monoComponents: null); |
| 88 | + var bclInitFunctions = new List<string> (); |
| 89 | + |
| 90 | + seen = new HashSet<string> (StringComparer.Ordinal); |
| 91 | + foreach (string lib in pinvokeLibraries) { |
| 92 | + foreach (NativeRuntimeComponents.Archive archive in bclComponents.KnownArchives) { |
| 93 | + if (lib != archive.Name) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + if (String.IsNullOrEmpty (archive.JniOnLoadName)) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (seen.Contains (archive.JniOnLoadName!)) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + seen.Add (archive.JniOnLoadName!); |
| 106 | + bclInitFunctions.Add (archive.JniOnLoadName!); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (bclInitFunctions.Count > 0) { |
| 111 | + Log.LogDebugMessage ("Found BCL JNI init functions to call on application init:"); |
| 112 | + foreach (string func in bclInitFunctions) { |
| 113 | + Log.LogDebugMessage ($" {func}"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + List<string>? customInitFunctions = null; |
| 118 | + if (CustomJniInitFunctions != null && CustomJniInitFunctions.Length > 0) { |
| 119 | + customInitFunctions = new List<string> (); |
| 120 | + seen.Clear (); |
| 121 | + Log.LogDebugMessage ("Custom JNI init functions to call on application init:"); |
| 122 | + foreach (ITaskItem func in CustomJniInitFunctions) { |
| 123 | + string name = func.ItemSpec; |
| 124 | + if (seen.Contains (name)) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + seen.Add (name); |
| 128 | + customInitFunctions.Add (name); |
| 129 | + Log.LogDebugMessage ($" {name}"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + var jniInitFuncsLlFilePath = Path.Combine (SourcesOutputDirectory, $"jni_init_funcs.{MonoAndroidHelper.RidToAbi (rid)}.ll"); |
| 134 | + var generator = new NativeAotDsoLoadNativeAssemblyGenerator (Log, bclInitFunctions, customInitFunctions); |
| 135 | + LLVMIR.LlvmIrModule jniInitFuncsModule = generator.Construct (); |
| 136 | + using var jniInitFuncsWriter = MemoryStreamPool.Shared.CreateStreamWriter (); |
| 137 | + bool fileFullyWritten = false; |
| 138 | + try { |
| 139 | + generator.Generate (jniInitFuncsModule, targetArch, jniInitFuncsWriter, jniInitFuncsLlFilePath!); |
| 140 | + jniInitFuncsWriter.Flush (); |
| 141 | + Files.CopyIfStreamChanged (jniInitFuncsWriter.BaseStream, jniInitFuncsLlFilePath!); |
| 142 | + fileFullyWritten = true; |
| 143 | + } finally { |
| 144 | + // Log partial contents for debugging if generation failed |
| 145 | + if (!fileFullyWritten) { |
| 146 | + MonoAndroidHelper.LogTextStreamContents (Log, $"Partial contents of file '{jniInitFuncsLlFilePath}'", jniInitFuncsWriter.BaseStream); |
| 147 | + } |
| 148 | + } |
| 149 | + return !Log.HasLoggedErrors; |
| 150 | + } |
| 151 | + |
| 152 | + static string MakeCanonicalLibraryName (string libName) |
| 153 | + { |
| 154 | + string? ext = Path.GetExtension (libName); |
| 155 | + if (!String.IsNullOrEmpty (ext) && KnownLibraryExtensions.Contains (ext)) { |
| 156 | + libName = Path.GetFileNameWithoutExtension (libName); |
| 157 | + } |
| 158 | + |
| 159 | + if (!libName.StartsWith ("lib", StringComparison.OrdinalIgnoreCase)) { |
| 160 | + libName = $"lib{libName}"; |
| 161 | + } |
| 162 | + |
| 163 | + // We will be matching against list of static archives, add the correct extension |
| 164 | + return $"{libName}.a"; |
| 165 | + } |
| 166 | +} |
0 commit comments