refactor(php): 重构PHP目录获取逻辑以支持macOS编译

- 提取php-config路径获取为公共方法getPhpDir()
- 移除Translator类中的重复代码
- 为macOS平台添加rpath绝对路径配置
- 修复macOS环境下ldconfig缺失导致的链接问题
pull/1/head
韩天峰 4 months ago
parent 10ab3d44a7
commit c87d750ef3
  1. 14
      src/Php/CompilerBase.php
  2. 7
      src/Php/Translator.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;

@ -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)) {

Loading…
Cancel
Save