-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLibraryLoader.php
More file actions
175 lines (146 loc) · 4.83 KB
/
LibraryLoader.php
File metadata and controls
175 lines (146 loc) · 4.83 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
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
<?php
declare(strict_types=1);
namespace Codewithkyrian\Whisper;
use Codewithkyrian\PlatformPackageInstaller\Platform;
use FFI;
use RuntimeException;
class LibraryLoader
{
private const LIBRARIES = [
'whisper' => [
'name' => 'whisper',
'header' => 'whisper.h',
'version' => '1.7.2',
],
'sndfile' => [
'name' => 'sndfile',
'header' => 'sndfile.h',
'version' => '1.2.2',
],
'samplerate' => [
'name' => 'samplerate',
'header' => 'samplerate.h',
'version' => '0.2.2',
],
];
private const PLATFORMS = [
'linux-x86_64' => [
'directory' => 'linux-x86_64',
'path' => 'lib{name}.so.{version}',
],
'linux-arm64' => [
'directory' => 'linux-arm64',
'path' => 'lib{name}.so.{version}',
],
'darwin-x86_64' => [
'directory' => 'darwin-x86_64',
'path' => 'lib{name}.{version}.dylib',
],
'darwin-arm64' => [
'directory' => 'darwin-arm64',
'path' => 'lib{name}.{version}.dylib',
],
'windows-x86_64' => [
'directory' => 'windows-x86_64',
'path' => '{name}-{version}.dll',
],
];
private static array $instances = [];
private ?FFI $kernel32 = null;
public function __construct()
{
$this->addDllDirectory();
}
public function __destruct()
{
$this->resetDllDirectory();
}
/**
* Gets the FFI instance for the specified library
*/
public function get(string $library): FFI
{
if (!isset(self::$instances[$library])) {
self::$instances[$library] = $this->load($library);
}
return self::$instances[$library];
}
/**
* Loads a new FFI instance for the specified library
*/
private function load(string $library): FFI
{
$config = self::LIBRARIES[$library] ?? null;
if ($config === null) {
throw new RuntimeException("Unsupported library: {$library}");
}
$platform = Platform::findBestMatch(self::PLATFORMS);
if (!$platform) {
throw new RuntimeException("Unsupported platform: ".json_encode(Platform::current()));
}
$headerPath = $this->getHeaderPath($config['header']);
$libraryPath = $this->getLibraryPath($config['name'],$config['version'],$platform);
return FFI::cdef(file_get_contents($headerPath), $libraryPath);
}
private static function getHeaderPath(string $headerFile): string
{
return self::joinPaths(dirname(__DIR__), 'include', $headerFile);
}
/**
* Get path to library file
*/
private function getLibraryPath(string $libName, string $version, array $platform): string
{
$libraryDir = self::getLibraryDirectory($platform['directory']);
$template = $platform['path'];
$filename = str_replace(['{name}', '{version}'], [$libName, $version], $template);
$candidate = self::joinPaths($libraryDir, $filename);
if (file_exists($candidate)) {
return $candidate;
}
throw new RuntimeException("Unable to locate library file for {$libName} in {$libraryDir}");
}
private static function getLibraryDirectory(string $platformDir): string
{
return self::joinPaths(dirname(__DIR__), 'lib', $platformDir);
}
/**
* Add DLL directory to search path for Windows
*/
private function addDllDirectory(): void
{
if (!Platform::isWindows()) return;
$platform = Platform::findBestMatch(self::PLATFORMS);
$libraryDir = self::getLibraryDirectory($platform['directory']);
$this->kernel32 ??= FFI::cdef("
int SetDllDirectoryA(const char* lpPathName);
int SetDefaultDllDirectories(unsigned long DirectoryFlags);
", 'kernel32.dll');
$this->kernel32?->{'SetDllDirectoryA'}($libraryDir);
}
/**
* Reset DLL directory search path
*/
private function resetDllDirectory(): void
{
if ($this->kernel32 !== null) {
$this->kernel32?->{'SetDllDirectoryA'}(null);
}
}
private static function joinPaths(string ...$args): string
{
$paths = [];
foreach ($args as $key => $path) {
if ($path === '') {
continue;
} elseif ($key === 0) {
$paths[$key] = rtrim($path, DIRECTORY_SEPARATOR);
} elseif ($key === count($paths) - 1) {
$paths[$key] = ltrim($path, DIRECTORY_SEPARATOR);
} else {
$paths[$key] = trim($path, DIRECTORY_SEPARATOR);
}
}
return preg_replace('#(?<!:)//+#', '/', implode(DIRECTORY_SEPARATOR, $paths));
}
}