Skip to content

Commit a38b72c

Browse files
authoredJun 30, 2023
Improve library finding (#206)
* improve library finding This PR adds a libraryLoad() function which searches a path for a library that supports an API, then uses that to load libvips, libglib and libgobject. This fixes #178 and #201 See also #198 Tested on macOS 13.4 * fix formatting
1 parent cbd5b05 commit a38b72c

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed
 

‎src/FFI.php

+45-25
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,29 @@ private static function libraryName(string $name, int $abi): string
204204
// most *nix
205205
return "$name.so.$abi";
206206
}
207+
208+
return null;
209+
}
210+
211+
private static function libraryLoad(
212+
array $libraryPaths,
213+
string $libraryName,
214+
string $interface
215+
): \FFI {
216+
Utils::debugLog("trying to open", ["libraryName" => $libraryName]);
217+
foreach ($libraryPaths as $path) {
218+
Utils::debugLog("trying path", ["path" => $path]);
219+
try {
220+
$library = \FFI::cdef($interface, $path . $libraryName);
221+
Utils::debugLog("success", []);
222+
return $library;
223+
} catch (\FFI\Exception $e) {
224+
Utils::debugLog("init", [
225+
"msg" => "library load failed",
226+
"exception" => $e->getMessage()
227+
]);
228+
}
229+
}
207230
}
208231

209232
private static function init(): void
@@ -251,30 +274,15 @@ private static function init(): void
251274
$libraryPaths[] = "/opt/homebrew/lib/"; // Homebrew on Apple Silicon
252275
}
253276

254-
// attempt to open libraries using the system library search method
255-
// (no prefix) and a couple of fallback paths, if any
256-
$vips = null;
257-
foreach ($libraryPaths as $path) {
258-
Utils::debugLog("init", ["path" => $path]);
259-
260-
try {
261-
$vips = \FFI::cdef(<<<EOS
262-
int vips_init (const char *argv0);
263-
const char *vips_error_buffer (void);
264-
int vips_version(int flag);
265-
EOS, $path . $vips_libname);
266-
break;
267-
} catch (\FFI\Exception $e) {
268-
Utils::debugLog("init", [
269-
"msg" => "library load failed",
270-
"exception" => $e->getMessage()
271-
]);
272-
}
273-
}
277+
$vips = self::libraryLoad($libraryPaths, $vips_libname, <<<EOS
278+
int vips_init (const char *argv0);
279+
const char *vips_error_buffer (void);
280+
int vips_version(int flag);
281+
EOS);
274282

275283
if ($vips === null) {
284+
// drop the "" (system path) member
276285
array_shift($libraryPaths);
277-
278286
$msg = "Unable to open library '$vips_libname'";
279287
if (!empty($libraryPaths)) {
280288
$msg .= " in any of ['" . implode("', '", $libraryPaths) . "']";
@@ -579,7 +587,7 @@ private static function init(): void
579587
unsigned int offset;
580588
} VipsArgumentClass;
581589
582-
int vips_object_get_argument (VipsObject* object, const char *name,
590+
int vips_object_get_argument (VipsObject* object, const char *name,
583591
GParamSpec** pspec,
584592
VipsArgumentClass** argument_class,
585593
VipsArgumentInstance** argument_instance);
@@ -758,9 +766,21 @@ private static function init(): void
758766
}
759767

760768
Utils::debugLog("init", ["binding ..."]);
761-
self::$glib = \FFI::cdef($glib_decls, $glib_libname);
762-
self::$gobject = \FFI::cdef($gobject_decls, $gobject_libname);
763-
self::$vips = \FFI::cdef($vips_decls, $vips_libname);
769+
self::$glib = self::libraryLoad(
770+
$libraryPaths,
771+
$glib_libname,
772+
$glib_decls
773+
);
774+
self::$gobject = self::libraryLoad(
775+
$libraryPaths,
776+
$gobject_libname,
777+
$gobject_decls
778+
);
779+
self::$vips = self::libraryLoad(
780+
$libraryPaths,
781+
$vips_libname,
782+
$vips_decls
783+
);
764784

765785
# Useful for debugging
766786
# self::$vips->vips_leak_set(1);

0 commit comments

Comments
 (0)
Failed to load comments.