forked from dotnet/LLVMSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLVM.cs
More file actions
79 lines (64 loc) · 2.55 KB
/
LLVM.cs
File metadata and controls
79 lines (64 loc) · 2.55 KB
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
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: DisableRuntimeMarshalling]
namespace LLVMSharp.Interop;
public static unsafe partial class LLVM
{
public static event DllImportResolver? ResolveLibrary;
static LLVM()
{
if (!Configuration.DisableResolveLibraryHook)
{
NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), OnDllImport);
}
}
private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (TryResolveLibrary(libraryName, assembly, searchPath, out var nativeLibrary))
{
return nativeLibrary;
}
if (libraryName.Equals("libLLVM", StringComparison.Ordinal) && TryResolveLLVM(assembly, searchPath, out nativeLibrary))
{
return nativeLibrary;
}
return IntPtr.Zero;
}
private static bool TryResolveLLVM(Assembly assembly, DllImportSearchPath? searchPath, out IntPtr nativeLibrary)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return NativeLibrary.TryLoad("libLLVM.so.20", assembly, searchPath, out nativeLibrary)
|| NativeLibrary.TryLoad("libLLVM-20", assembly, searchPath, out nativeLibrary)
|| NativeLibrary.TryLoad("libLLVM.so.1", assembly, searchPath, out nativeLibrary);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return NativeLibrary.TryLoad("LLVM-C.dll", assembly, searchPath, out nativeLibrary);
}
nativeLibrary = IntPtr.Zero;
return false;
}
private static bool TryResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr nativeLibrary)
{
var resolveLibrary = ResolveLibrary;
if (resolveLibrary is not null)
{
var resolvers = resolveLibrary.GetInvocationList().Cast<DllImportResolver>();
foreach (DllImportResolver resolver in resolvers)
{
nativeLibrary = resolver(libraryName, assembly, searchPath);
if (nativeLibrary != IntPtr.Zero)
{
return true;
}
}
}
nativeLibrary = IntPtr.Zero;
return false;
}
}