From 5e025f380652832780548a571515807d30688229 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 29 Jun 2023 20:45:04 +0100 Subject: [PATCH 1/2] 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 https://github.com/libvips/php-vips/pull/198 Tested on macOS 13.4 --- src/FFI.php | 61 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/FFI.php b/src/FFI.php index 4f9d726..0714498 100644 --- a/src/FFI.php +++ b/src/FFI.php @@ -192,6 +192,28 @@ private static function libraryName(string $name, int $abi): string // most *nix return "$name.so.$abi"; } + + return null; + } + + private static function libraryLoad(array $libraryPaths, + string $libraryName, + string $interface): \FFI + { + Utils::debugLog("trying to open", ["libraryName" => $libraryName]); + foreach ($libraryPaths as $path) { + Utils::debugLog("trying path", ["path" => $path]); + try { + $library = \FFI::cdef($interface, $path . $libraryName); + Utils::debugLog("success", []); + return $library; + } catch (\FFI\Exception $e) { + Utils::debugLog("init", [ + "msg" => "library load failed", + "exception" => $e->getMessage() + ]); + } + } } private static function init(): void @@ -239,30 +261,15 @@ private static function init(): void $libraryPaths[] = "/opt/homebrew/lib/"; // Homebrew on Apple Silicon } - // attempt to open libraries using the system library search method - // (no prefix) and a couple of fallback paths, if any - $vips = null; - foreach ($libraryPaths as $path) { - Utils::debugLog("init", ["path" => $path]); - - try { - $vips = \FFI::cdef(<< "library load failed", - "exception" => $e->getMessage() - ]); - } - } + $vips = self::libraryLoad($libraryPaths, $vips_libname, <<vips_leak_set(1); From 7c273048abc3ae4e0e20c36c251d529abdffee95 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 29 Jun 2023 20:52:37 +0100 Subject: [PATCH 2/2] fix formatting --- src/FFI.php | 37 ++++++++++++++++++++++--------------- src/Image.php | 3 ++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/FFI.php b/src/FFI.php index 0714498..1dc3d63 100644 --- a/src/FFI.php +++ b/src/FFI.php @@ -196,10 +196,11 @@ private static function libraryName(string $name, int $abi): string return null; } - private static function libraryLoad(array $libraryPaths, - string $libraryName, - string $interface): \FFI - { + private static function libraryLoad( + array $libraryPaths, + string $libraryName, + string $interface + ): \FFI { Utils::debugLog("trying to open", ["libraryName" => $libraryName]); foreach ($libraryPaths as $path) { Utils::debugLog("trying path", ["path" => $path]); @@ -209,7 +210,7 @@ private static function libraryLoad(array $libraryPaths, return $library; } catch (\FFI\Exception $e) { Utils::debugLog("init", [ - "msg" => "library load failed", + "msg" => "library load failed", "exception" => $e->getMessage() ]); } @@ -558,7 +559,7 @@ private static function init(): void unsigned int offset; } VipsArgumentClass; -int vips_object_get_argument (VipsObject* object, const char *name, +int vips_object_get_argument (VipsObject* object, const char *name, GParamSpec** pspec, VipsArgumentClass** argument_class, VipsArgumentInstance** argument_instance); @@ -743,15 +744,21 @@ private static function init(): void } Utils::debugLog("init", ["binding ..."]); - self::$glib = self::libraryLoad($libraryPaths, - $glib_libname, - $glib_decls); - self::$gobject = self::libraryLoad($libraryPaths, - $gobject_libname, - $gobject_decls); - self::$vips = self::libraryLoad($libraryPaths, - $vips_libname, - $vips_decls); + self::$glib = self::libraryLoad( + $libraryPaths, + $glib_libname, + $glib_decls + ); + self::$gobject = self::libraryLoad( + $libraryPaths, + $gobject_libname, + $gobject_decls + ); + self::$vips = self::libraryLoad( + $libraryPaths, + $vips_libname, + $vips_decls + ); # Useful for debugging # self::$vips->vips_leak_set(1); diff --git a/src/Image.php b/src/Image.php index ab2c2b4..39646f1 100644 --- a/src/Image.php +++ b/src/Image.php @@ -1889,7 +1889,8 @@ public function bandrank($other, array $options = []): Image 'bandrank', null, [array_merge([$this], $other)], - $options); + $options + ); } /**