Skip to content

Improve library finding #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 45 additions & 25 deletions src/FFI.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,29 @@ 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
Expand Down Expand Up @@ -239,30 +262,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(<<<EOS
int vips_init (const char *argv0);
const char *vips_error_buffer (void);
int vips_version(int flag);
EOS, $path . $vips_libname);
break;
} catch (\FFI\Exception $e) {
Utils::debugLog("init", [
"msg" => "library load failed",
"exception" => $e->getMessage()
]);
}
}
$vips = self::libraryLoad($libraryPaths, $vips_libname, <<<EOS
int vips_init (const char *argv0);
const char *vips_error_buffer (void);
int vips_version(int flag);
EOS);

if ($vips === null) {
// drop the "" (system path) member
array_shift($libraryPaths);

$msg = "Unable to open library '$vips_libname'";
if (!empty($libraryPaths)) {
$msg .= " in any of ['" . implode("', '", $libraryPaths) . "']";
Expand Down Expand Up @@ -551,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);
Expand Down Expand Up @@ -736,9 +744,21 @@ private static function init(): void
}

Utils::debugLog("init", ["binding ..."]);
self::$glib = \FFI::cdef($glib_decls, $glib_libname);
self::$gobject = \FFI::cdef($gobject_decls, $gobject_libname);
self::$vips = \FFI::cdef($vips_decls, $vips_libname);
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);
Expand Down