From c87d750ef369404bebbe43d80b7dd05c73a4704c Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 28 Apr 2026 19:11:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84PHP=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81macOS=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提取php-config路径获取为公共方法getPhpDir() - 移除Translator类中的重复代码 - 为macOS平台添加rpath绝对路径配置 - 修复macOS环境下ldconfig缺失导致的链接问题 --- src/Php/CompilerBase.php | 14 ++++++++++++++ src/Php/Translator.php | 7 +------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 517f6c10..d92d0678 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -290,6 +290,15 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->rootPath . '/vendor/swoole/phpx'; } + public function getPhpDir(): string + { + $phpDir = shell_exec('php-config --prefix'); + if (empty($phpDir)) { + $this->error('The `php-config` is not found'); + } + return trim($phpDir); + } + public function save(string $code, string $file): void { $this->writeFile($file, $code); @@ -2107,6 +2116,11 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->ldflags) { $cmd .= ' ' . $this->ldflags; } + // macos 没有 ldconfig ,只能配置 rpath 绝对路径 + if ($this->isMacos()) { + $cmd .= ' -Wl,-rpath,' . $this->getPhpDir() . '/lib'; + $cmd .= ' -Wl,-rpath,' . $this->getPhpxDir() . '/lib'; + } } else { if ($this->cxxflags) { $cmd .= ' ' . $this->cxxflags; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 47e87121..372f6d6f 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -213,13 +213,8 @@ class Translator extends Preprocessor public function prepare(string $path): array { - $phpDir = shell_exec('php-config --prefix'); - if (empty($phpDir)) { - $this->error('The `php-config` is not found'); - } - $phpDir = trim($phpDir); $ext = $this->isMacos() ? 'dylib' : 'so'; - if (!is_file($phpDir . '/lib/libphp.' . $ext)) { + if (!is_file($this->getPhpDir() . '/lib/libphp.' . $ext)) { $this->error("The `libphp.{$ext}` is not found, please run `make` to build it"); } if (!is_file($this->getPhpxDir() . '/lib/libphpx.' . $ext)) {