Skip to content

Add FFI::addLibraryPath utility #264

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 2 commits into from
Apr 4, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Incorporate review comment
  • Loading branch information
kleisauke committed Apr 4, 2025
commit deaf4e15d030a8ca923b1e89681d74861a1257a8
28 changes: 17 additions & 11 deletions src/FFI.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ public static function atLeast(int $x, int $y, int $z = 0): bool
/**
* Adds a directory to the search path for shared libraries.
*
* This method has no effect if FFI handles are already initialized or
* if the specified path is already included.
* This method has no effect if FFI handles are already initialized,
* if the specified path is non-existent, or if the path is already
* included.
*
* @param string $path The path of the library.
* @return bool `true` if the path was added; otherwise, `false`.
Expand All @@ -194,12 +195,20 @@ public static function addLibraryPath(string $path): bool
return false;
}

if (!in_array($path, self::$libraryPaths)) {
self::$libraryPaths[] = $path;
return true;
$path = realpath($path);
if ($path === false) {
return false;
}

$path .= DIRECTORY_SEPARATOR;

if (in_array($path, self::$libraryPaths)) {
return false;
}

return false;
self::$libraryPaths[] = $path;

return true;
}

/**
Expand Down Expand Up @@ -248,9 +257,6 @@ private static function libraryLoad(
foreach (self::$libraryPaths as $path) {
Utils::debugLog("trying path", ["path" => $path]);
try {
if ($path !== '') {
$path .= '/';
}
$library = \FFI::cdef($interface, $path . $libraryName);
Utils::debugLog("success", []);
return $library;
Expand Down Expand Up @@ -289,9 +295,9 @@ private static function init(): void

if (PHP_OS_FAMILY === "OSX" || PHP_OS_FAMILY === "Darwin") {
// Homebrew on Apple Silicon
self::$libraryPaths[] = "/opt/homebrew/lib";
self::addLibraryPath("/opt/homebrew/lib");
// See https://github.com/Homebrew/brew/issues/13481#issuecomment-1207203483
self::$libraryPaths[] = "/usr/local/lib";
self::addLibraryPath("/usr/local/lib");
}

$vips = self::libraryLoad($vips_libname, <<<'CPP'
Expand Down