Skip to content

Commit b831dfa

Browse files
authored
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.
1 parent 69daefa commit b831dfa

3 files changed

Lines changed: 39 additions & 24 deletions

File tree

src/Platform/Macos.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,4 @@ public function buildPhpLibPaths(string $phpDir): array
7676
self::HOMEBREW_LIBRARY_PATHS,
7777
)));
7878
}
79-
80-
/**
81-
* 获取默认的 RPATH 路径列表(macOS 需要)
82-
*/
83-
public function getDefaultRpaths(?string $phpxDir = null, ?string $phpDir = null): array
84-
{
85-
$rpaths = [];
86-
87-
if ($phpxDir !== null) {
88-
$phpxLibDir = $phpxDir . '/lib';
89-
if (is_dir($phpxLibDir)) {
90-
$rpaths[] = $phpxLibDir;
91-
}
92-
}
93-
94-
if ($phpDir !== null) {
95-
$phpLibDir = $this->resolvePhpLibDir($phpDir);
96-
if ($phpLibDir !== null) {
97-
$rpaths[] = $phpLibDir;
98-
}
99-
}
100-
101-
return $rpaths;
102-
}
10379
}

src/Platform/UnixPlatform.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,36 @@ private function resolvePhpDir(): string
166166
throw new \RuntimeException('The `php-config` is not found. Please install PHP development package or set PHP_HOME environment variable');
167167
}
168168

169+
/**
170+
* libphpx and the embed libphp.so live where TypePHP put them: beside the
171+
* phpx checkout, and under the PHP prefix the libphp installer chose.
172+
* Neither is on the loader's search path, so a linked program only finds
173+
* them again if their directories are recorded in the binary.
174+
*
175+
* Cross-compilation targets override this back to an empty list: a path on
176+
* the build host means nothing on the device that runs the output.
177+
*/
178+
public function getDefaultRpaths(?string $phpxDir = null, ?string $phpDir = null): array
179+
{
180+
$rpaths = [];
181+
182+
if ($phpxDir !== null) {
183+
$phpxLibDir = $phpxDir . '/lib';
184+
if (is_dir($phpxLibDir)) {
185+
$rpaths[] = $phpxLibDir;
186+
}
187+
}
188+
189+
if ($phpDir !== null) {
190+
$phpLibDir = $this->resolvePhpLibDir($phpDir);
191+
if ($phpLibDir !== null) {
192+
$rpaths[] = $phpLibDir;
193+
}
194+
}
195+
196+
return $rpaths;
197+
}
198+
169199
/**
170200
* Get the RPATH options.
171201
*/

src/Platform/Wasi.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public function getSharedLibraryExtension(): string
2323
return '.a';
2424
}
2525

26+
/**
27+
* A WASI module resolves no shared libraries, and the build host's paths
28+
* do not exist wherever it runs.
29+
*/
30+
public function getDefaultRpaths(?string $phpxDir = null, ?string $phpDir = null): array
31+
{
32+
return [];
33+
}
34+
2635
public function getExecutableExtension(): string
2736
{
2837
return '.wasm';

0 commit comments

Comments
 (0)