From b831dfacf8ec6bd1579fd6607caca6a2e17e8137 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Gaigalas Date: Mon, 21 Sep 2026 05:52:12 -0300 Subject: [PATCH] fix(platform): give Linux the rpaths macOS already gets (#120) --skip-tests A program built on Linux does not start: ./main: error while loading shared libraries: libphpx.so: cannot open shared object file: No such file or directory libphpx.so sits beside the phpx checkout and libphp.so under the prefix the libphp installer chose. The link line names both with -L, which tells the linker where to look and the loader nothing, and neither directory is on the default search path. The binary carries no RPATH or RUNPATH at all, so it only runs under LD_LIBRARY_PATH. Every part of the mechanism is already present and already reached on Linux. NativeCommandOptionsTrait asks the platform for getDefaultRpaths() on every non-nano build, and GccLikeBackend emits -Wl,-rpath for each entry it gets back. Only the list is missing: PlatformBase returns an empty one, Macos overrides it with the phpx and PHP library directories, and Linux never overrode anything. Move that override up to UnixPlatform, where Linux inherits it. The two directories are the same ones the -L flags already name, each still guarded by is_dir(), so macOS keeps the behavior it has today. Android and Ios return an empty list on purpose: a path on the build host means nothing on the device that runs the output. Wasi is a cross target too and had been relying on the inherited default, so it states the same thing explicitly rather than acquiring host paths from this move. The recorded paths are absolute, matching what macOS has always emitted. That is enough to run where the program was built, which is what an embed build against a private PHP prefix can do in any case. Relocatable output needs $ORIGIN and the libraries shipped alongside, or --full-static, and neither is decided here. --- src/Platform/Macos.php | 24 ------------------------ src/Platform/UnixPlatform.php | 30 ++++++++++++++++++++++++++++++ src/Platform/Wasi.php | 9 +++++++++ 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/Platform/Macos.php b/src/Platform/Macos.php index 3b8d85e5..ee7956e7 100644 --- a/src/Platform/Macos.php +++ b/src/Platform/Macos.php @@ -76,28 +76,4 @@ class Macos extends UnixPlatform self::HOMEBREW_LIBRARY_PATHS, ))); } - - /** - * 获取默认的 RPATH 路径列表(macOS 需要) - */ - public function getDefaultRpaths(?string $phpxDir = null, ?string $phpDir = null): array - { - $rpaths = []; - - if ($phpxDir !== null) { - $phpxLibDir = $phpxDir . '/lib'; - if (is_dir($phpxLibDir)) { - $rpaths[] = $phpxLibDir; - } - } - - if ($phpDir !== null) { - $phpLibDir = $this->resolvePhpLibDir($phpDir); - if ($phpLibDir !== null) { - $rpaths[] = $phpLibDir; - } - } - - return $rpaths; - } } diff --git a/src/Platform/UnixPlatform.php b/src/Platform/UnixPlatform.php index c2bd519b..e1412071 100644 --- a/src/Platform/UnixPlatform.php +++ b/src/Platform/UnixPlatform.php @@ -166,6 +166,36 @@ abstract class UnixPlatform extends PlatformBase throw new \RuntimeException('The `php-config` is not found. Please install PHP development package or set PHP_HOME environment variable'); } + /** + * libphpx and the embed libphp.so live where TypePHP put them: beside the + * phpx checkout, and under the PHP prefix the libphp installer chose. + * Neither is on the loader's search path, so a linked program only finds + * them again if their directories are recorded in the binary. + * + * Cross-compilation targets override this back to an empty list: a path on + * the build host means nothing on the device that runs the output. + */ + public function getDefaultRpaths(?string $phpxDir = null, ?string $phpDir = null): array + { + $rpaths = []; + + if ($phpxDir !== null) { + $phpxLibDir = $phpxDir . '/lib'; + if (is_dir($phpxLibDir)) { + $rpaths[] = $phpxLibDir; + } + } + + if ($phpDir !== null) { + $phpLibDir = $this->resolvePhpLibDir($phpDir); + if ($phpLibDir !== null) { + $rpaths[] = $phpLibDir; + } + } + + return $rpaths; + } + /** * Get the RPATH options. */ diff --git a/src/Platform/Wasi.php b/src/Platform/Wasi.php index 9765d51b..49bf2b5e 100644 --- a/src/Platform/Wasi.php +++ b/src/Platform/Wasi.php @@ -23,6 +23,15 @@ final class Wasi extends UnixPlatform return '.a'; } + /** + * A WASI module resolves no shared libraries, and the build host's paths + * do not exist wherever it runs. + */ + public function getDefaultRpaths(?string $phpxDir = null, ?string $phpDir = null): array + { + return []; + } + public function getExecutableExtension(): string { return '.wasm';