-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LibraryLoader.cs
389 lines (323 loc) · 12.1 KB
/
LibraryLoader.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
386
387
388
389
namespace HexaGen.Runtime
{
using System;
using System.Reflection;
using System.Runtime.InteropServices;
#if !NET5_0_OR_GREATER
using HexaGen.Runtime;
#endif
#if ANDROID
using Android.Content.PM;
#endif
public enum TargetPlatform
{
Unknown = 0,
FreeBSD = 1 << 0,
Linux = 1 << 1,
OSX = 1 << 2,
Windows = 1 << 3,
Android = 1 << 4,
IOS = 1 << 5,
Tizen = 1 << 6,
ChromeOS = 1 << 7,
WebAssembly = 1 << 8,
Solaris = 1 << 9,
WatchOS = 1 << 10,
TVO = 1 << 11,
Any = FreeBSD | Linux | OSX | Windows | Android | IOS | Tizen | ChromeOS | WebAssembly | Solaris | WatchOS | TVO
}
public struct LibraryName
{
public string Name;
public TargetPlatform TargetPlatform;
}
public struct LibraryExtension
{
public string Extension;
public TargetPlatform TargetPlatform;
}
public struct LibraryLoaderHints
{
public LibraryName[] Names;
public LibraryExtension[] Extensions;
public string[] Paths;
public LibraryLoaderHints(LibraryName[] names, LibraryExtension[] extensions, string[] paths)
{
Names = names;
Extensions = extensions;
Paths = paths;
}
public LibraryLoaderHints(LibraryName[] names, LibraryExtension[] extensions)
{
Names = names;
Extensions = extensions;
Paths = [];
}
}
public static class LibraryLoader
{
public static OSPlatform FreeBSD { get; } = OSPlatform.Create("FREEBSD");
public static OSPlatform Linux { get; } = OSPlatform.Create("LINUX");
public static OSPlatform OSX { get; } = OSPlatform.Create("OSX");
public static OSPlatform Windows { get; } = OSPlatform.Create("WINDOWS");
public static OSPlatform Android { get; } = OSPlatform.Create("ANDROID");
public static OSPlatform IOS { get; } = OSPlatform.Create("IOS");
public static OSPlatform Tizen { get; } = OSPlatform.Create("TIZEN");
public static OSPlatform ChromeOS { get; } = OSPlatform.Create("CHROMEOS");
public static OSPlatform WebAssembly { get; } = OSPlatform.Create("WEBASSEMBLY");
public static OSPlatform Solaris { get; } = OSPlatform.Create("SOLARIS");
public static OSPlatform WatchOS { get; } = OSPlatform.Create("WATCHOS");
public static OSPlatform TVOS { get; } = OSPlatform.Create("TVOS");
public static string GetExtension(IEnumerable<NativeLibraryExtensionAttribute> extensionAttributes)
{
foreach (var extensionAttribute in extensionAttributes)
{
if (RuntimeInformation.IsOSPlatform(extensionAttribute.TargetPlatform.Convert()))
{
return extensionAttribute.Extension;
}
}
// Default extension based on platform
if (RuntimeInformation.IsOSPlatform(Windows))
{
return ".dll";
}
else if (RuntimeInformation.IsOSPlatform(OSX))
{
return ".dylib";
}
else if (RuntimeInformation.IsOSPlatform(Linux))
{
return ".so";
}
else if (RuntimeInformation.IsOSPlatform(Android))
{
return ".so";
}
else if (RuntimeInformation.IsOSPlatform(IOS))
{
return ".dylib"; // iOS also uses .dylib for dynamic libraries
}
else if (RuntimeInformation.IsOSPlatform(FreeBSD))
{
return ".so";
}
else if (RuntimeInformation.IsOSPlatform(TVOS))
{
return ".dylib"; // tvOS uses the same dynamic library extension as iOS
}
else if (RuntimeInformation.IsOSPlatform(WatchOS))
{
return ".dylib"; // watchOS also uses .dylib
}
else if (RuntimeInformation.IsOSPlatform(Solaris))
{
return ".so";
}
else if (RuntimeInformation.IsOSPlatform(WebAssembly))
{
return ".wasm";
}
else if (RuntimeInformation.IsOSPlatform(Tizen))
{
return ".so";
}
else if (RuntimeInformation.IsOSPlatform(ChromeOS))
{
return ".so";
}
// Default to .so if no platform matches
return ".so";
}
public static nint LoadLibrary()
{
var libraryAttributes = Assembly.GetCallingAssembly().GetCustomAttributes<NativeLibraryAttribute>();
var extensionAttributes = Assembly.GetCallingAssembly().GetCustomAttributes<NativeLibraryExtensionAttribute>();
var libraryName = GetLibraryName(libraryAttributes);
var extension = GetExtension(extensionAttributes);
if (!libraryName.EndsWith(extension, StringComparison.OrdinalIgnoreCase))
{
libraryName += extension;
}
var osPlatform = GetOSPlatform();
var architecture = GetArchitecture();
var libraryPath = GetNativeAssemblyPath(osPlatform, architecture, libraryName);
nint handle;
handle = NativeLibrary.Load(libraryPath);
if (handle == IntPtr.Zero)
{
throw new DllNotFoundException($"Unable to load library '{libraryName}'.");
}
return handle;
}
private static string GetNativeAssemblyPath(string osPlatform, string architecture, string libraryName)
{
#if ANDROID
// Get the application info
ApplicationInfo appInfo = Application.Context.ApplicationInfo!;
// Get the native library directory path
string assemblyLocation = appInfo.NativeLibraryDir!;
#else
string assemblyLocation = AppContext.BaseDirectory;
#endif
var paths = new[]
{
Path.Combine(assemblyLocation, libraryName),
Path.Combine(assemblyLocation, "runtimes", osPlatform, "native", libraryName),
Path.Combine(assemblyLocation, "runtimes", $"{osPlatform}-{architecture}", "debug", libraryName), // allows debug builds sideload.
Path.Combine(assemblyLocation, "runtimes", $"{osPlatform}-{architecture}", "native", libraryName),
};
foreach (var path in paths)
{
if (File.Exists(path))
{
return path;
}
}
return libraryName;
}
private static string GetArchitecture()
{
return RuntimeInformation.ProcessArchitecture switch
{
Architecture.X86 => "x86",
Architecture.X64 => "x64",
Architecture.Arm => "arm",
Architecture.Arm64 => "arm64",
_ => throw new ArgumentException("Unsupported architecture."),
};
}
private static string GetOSPlatform()
{
if (RuntimeInformation.IsOSPlatform(Windows))
{
return "win";
}
else if (RuntimeInformation.IsOSPlatform(Linux))
{
return "linux";
}
else if (RuntimeInformation.IsOSPlatform(OSX))
{
return "osx";
}
else if (RuntimeInformation.IsOSPlatform(Android))
{
return "android";
}
else if (RuntimeInformation.IsOSPlatform(IOS))
{
return "ios";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
{
return "freebsd";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS")))
{
return "tvos";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("WATCHOS")))
{
return "watchos";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("SOLARIS")))
{
return "solaris";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY")))
{
return "webassembly";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("TIZEN")))
{
return "tizen";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("CHROMEOS")))
{
return "chromeos";
}
throw new ArgumentException("Unsupported OS platform.");
}
private static string GetLibraryName(IEnumerable<NativeLibraryAttribute> nativeLibraries)
{
NativeLibraryAttribute? nativeLibrary = null;
foreach (NativeLibraryAttribute attri in nativeLibraries)
{
if (attri.TargetPlatform == TargetPlatform.Any)
{
nativeLibrary = attri; // Default
continue;
}
if (RuntimeInformation.IsOSPlatform(attri.TargetPlatform.Convert()))
{
nativeLibrary = attri;
break;
}
}
if (nativeLibrary == null)
{
throw new Exception("Dll not specified for this platform");
}
return nativeLibrary.LibraryName;
}
private static OSPlatform Convert(this TargetPlatform targetPlatform)
{
switch (targetPlatform)
{
case TargetPlatform.FreeBSD:
return FreeBSD;
case TargetPlatform.Linux:
return Linux;
case TargetPlatform.OSX:
return OSX;
case TargetPlatform.Windows:
return Windows;
case TargetPlatform.Android:
return Android;
case TargetPlatform.IOS:
return IOS;
case TargetPlatform.Tizen:
return Tizen;
case TargetPlatform.ChromeOS:
return ChromeOS;
case TargetPlatform.WebAssembly:
return WebAssembly;
case TargetPlatform.Solaris:
return Solaris;
case TargetPlatform.WatchOS:
return WatchOS;
case TargetPlatform.TVO:
return TVOS;
default:
throw new PlatformNotSupportedException();
}
}
}
[System.AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
public sealed class NativeLibraryExtensionAttribute : Attribute
{
private readonly string extension;
private readonly TargetPlatform targetPlatform;
public NativeLibraryExtensionAttribute(string extension, TargetPlatform targetPlatform = TargetPlatform.Any)
{
this.extension = extension;
this.targetPlatform = targetPlatform;
}
public string Extension => extension;
public TargetPlatform TargetPlatform => targetPlatform;
}
[System.AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
public sealed class NativeLibraryAttribute : Attribute
{
private readonly string libraryName;
private readonly TargetPlatform targetPlatform;
public NativeLibraryAttribute(string libraryName, TargetPlatform targetPlatform = TargetPlatform.Any)
{
this.libraryName = libraryName;
this.targetPlatform = targetPlatform;
}
public string LibraryName => libraryName;
public TargetPlatform TargetPlatform => targetPlatform;
}
}