Skip to content
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

Add FFI::addLibraryPath utility #264

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ To your `php.ini`.
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

// handy for Windows
Vips\FFI::addLibraryPath("C:/vips-dev-8.16/bin");

// fast thumbnail generator
$image = Vips\Image::thumbnail('somefile.jpg', 128);
$image->writeToFile('tiny.jpg');
Expand Down
105 changes: 63 additions & 42 deletions src/FFI.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class FFI
*/
private static bool $ffi_inited = false;

/**
* A list of paths where libvips might reside.
*
* @internal
*/
private static array $libraryPaths = [
"" // system library
];

/**
* Look up these once.
*
Expand Down Expand Up @@ -169,6 +178,30 @@ public static function atLeast(int $x, int $y, int $z = 0): bool
self::$library_micro >= $z);
}

/**
* 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.
*
* @param string $path The path of the library.
* @return bool `true` if the path was added; otherwise, `false`.
*/
public static function addLibraryPath(string $path): bool
{
// Already initialized.
if (self::$ffi_inited) {
return false;
}

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

return false;
}

/**
* Shut down libvips. Call this just before process exit.
*
Expand Down Expand Up @@ -208,14 +241,16 @@ private static function libraryName(string $name, int $abi): string
}

private static function libraryLoad(
array $libraryPaths,
string $libraryName,
string $interface
): ?\FFI {
Utils::debugLog("trying to open", ["libraryName" => $libraryName]);
foreach ($libraryPaths as $path) {
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 @@ -249,49 +284,32 @@ private static function init(): void
}

$vips_libname = self::libraryName("libvips", 42);
if (PHP_OS_FAMILY === "Windows") {
$glib_libname = self::libraryName("libglib-2.0", 0);
$gobject_libname = self::libraryName("libgobject-2.0", 0);
} else {
$glib_libname = $vips_libname;
$gobject_libname = $vips_libname;
}
$glib_libname = self::libraryName("libglib-2.0", 0);
$gobject_libname = self::libraryName("libgobject-2.0", 0);

Utils::debugLog("init", ["library" => $vips_libname]);

$is_64bits = PHP_INT_SIZE === 8;

$libraryPaths = [
"" // system library
];

$vipshome = getenv("VIPSHOME");
if ($vipshome) {
// lib<qual>/ predicates lib/
$libraryPaths[] = $vipshome . ($is_64bits ? "/lib64/" : "/lib32/");
// lib/ is always searched
$libraryPaths[] = $vipshome . "/lib/";
}

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

$vips = self::libraryLoad($libraryPaths, $vips_libname, <<<'CPP'
$vips = self::libraryLoad($vips_libname, <<<'CPP'
int vips_init (const char *argv0);
const char *vips_error_buffer (void);
int vips_version(int flag);
CPP);

if ($vips === null) {
// drop the "" (system path) member
array_shift($libraryPaths);
array_shift(self::$libraryPaths);
$msg = "Unable to open library '$vips_libname'";
if (!empty($libraryPaths)) {
$msg .= " in any of ['" . implode("', '", $libraryPaths) . "']";
if (!empty(self::$libraryPaths)) {
$msg .= " in any of ['" . implode("', '", self::$libraryPaths) . "']";
}
$msg .= ". Make sure that you've installed libvips and that '$vips_libname'";
$msg .= " is on your system's library search path.";
Expand Down Expand Up @@ -775,21 +793,24 @@ 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
);

/**
* We can sometimes get dependent libraries from libvips -- either the platform
* will open dependencies for us automatically, or the libvips binary has been
* built to includes all main dependencies (common on Windows, can happen
* elsewhere).
*
* We must get GLib functions from libvips if we can, since it will be the
* one that libvips itself is using, and they will share runtime types.
*/
self::$glib =
self::libraryLoad($vips_libname, $glib_decls) ??
self::libraryLoad($glib_libname, $glib_decls);
self::$gobject =
self::libraryLoad($vips_libname, $gobject_decls) ??
self::libraryLoad($gobject_libname, $gobject_decls);

self::$vips = self::libraryLoad($vips_libname, $vips_decls);

# Useful for debugging
# self::$vips->vips_leak_set(1);
Expand Down