From 054560c0f96588793368a3f869032863c85dac8e Mon Sep 17 00:00:00 2001 From: rango Date: Fri, 8 May 2026 12:22:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=20C++=20?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=99=A8=E9=85=8D=E7=BD=AE=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=20PHP=20=E8=B7=AF=E5=BE=84=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 setCppCompiler 方法用于设置 C++ 编译器 - 支持从配置文件读取 cpp-compiler 配置项 - 优化 Unix/Linux/macOS 系统 PHP 路径查找逻辑,增加环境变量 PHP_HOME 支持 - 改进共享库扩展名处理,统一处理带点和不带点的情况 - 添加多个常见 PHP 8.4 安装路径的自动检测 --- src/Php/CompilerBase.php | 64 ++++++++++++++++++++++++++++++++-------- src/Php/Translator.php | 6 ++++ test_phpx_dir.php | 16 ---------- 3 files changed, 57 insertions(+), 29 deletions(-) delete mode 100644 test_phpx_dir.php diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index c4678dbd..13ff542f 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -335,7 +335,7 @@ class CompilerBase extends \PhpAot\Core\Translator // 初始化新的 Platform 和 Backend 抽象层 $this->initializeNewArchitecture(); } else { - // Unix/Linux/macOS 使用 g++ + // Unix/Linux/macOS 默认使用 g++ $this->cppCompiler = 'g++'; // 初始化新的 Platform 和 Backend 抽象层 @@ -343,6 +343,18 @@ class CompilerBase extends \PhpAot\Core\Translator } } + /** + * 设置 C++ 编译器(从配置文件读取) + */ + public function setCppCompiler(string $compiler): void + { + $this->cppCompiler = $compiler; + $this->climate->info("Using compiler from config: {$this->cppCompiler}"); + + // 重新初始化 Backend + $this->initializeNewArchitecture(); + } + /** * 初始化新的 Platform 和 Backend 抽象层 * 这是一个渐进式迁移,保持向后兼容 @@ -569,20 +581,43 @@ class CompilerBase extends \PhpAot\Core\Translator // 默认路径 return 'C:\php'; } else { - // Unix/Linux/macOS 下使用 php-config 获取 PHP 路径 + // Unix/Linux/macOS 下获取 PHP 路径 + // 优先级:环境变量 PHP_HOME > /opt/php-8.4 > php-config > which php + + // 1. 尝试环境变量 PHP_HOME + $phpDir = getenv('PHP_HOME'); + if ($phpDir && is_dir($phpDir)) { + return rtrim($phpDir, '\/'); + } + + // 2. 尝试常见的 PHP 8.4 安装路径 + $commonPaths = [ + '/opt/php-8.4', + '/usr/local/php-8.4', + '/usr/local/php84', + ]; + foreach ($commonPaths as $path) { + if (is_dir($path) && is_executable($path . '/bin/php')) { + return $path; + } + } + + // 3. 使用 php-config 获取 PHP 路径 $phpDir = shell_exec('php-config --prefix 2>/dev/null'); - if (empty($phpDir)) { - // 如果 php-config 不可用,尝试从 which php 推断 - $phpExe = trim(shell_exec('which php 2>/dev/null')); - if ($phpExe && file_exists($phpExe)) { - $phpDir = dirname(dirname($phpExe)); - if (is_dir($phpDir)) { - return $phpDir; - } + if (!empty($phpDir)) { + return trim($phpDir); + } + + // 4. 如果 php-config 不可用,尝试从 which php 推断 + $phpExe = trim(shell_exec('which php 2>/dev/null')); + if ($phpExe && file_exists($phpExe)) { + $phpDir = dirname(dirname($phpExe)); + if (is_dir($phpDir)) { + return $phpDir; } - $this->error('The `php-config` is not found. Please install PHP development package or set PHP_HOME environment variable'); } - return trim($phpDir); + + $this->error('The `php-config` is not found. Please install PHP development package or set PHP_HOME environment variable'); } } @@ -2622,7 +2657,10 @@ class CompilerBase extends \PhpAot\Core\Translator } } else { // Linux/macOS: libphpx.so 或 libphpx.a - $phpxLibPath = $this->getPhpxDir() . '/lib/libphpx.' . $this->platform->getSharedLibraryExtension(); + $sharedLibExt = $this->platform->getSharedLibraryExtension(); + // getSharedLibraryExtension() 返回的值可能带点或不带点,需要统一处理 + $extWithoutDot = ltrim($sharedLibExt, '.'); + $phpxLibPath = $this->getPhpxDir() . '/lib/libphpx.' . $extWithoutDot; if (file_exists($phpxLibPath)) { $libraries[] = $phpxLibPath; } else { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 82fa9239..57755615 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1292,6 +1292,12 @@ CODE; $this->setTargetName($cfg['name']); } + // 读取 cpp-compiler(支持中横线和下划线) + $cppCompiler = $cfg['cpp-compiler'] ?? $cfg['cpp_compiler'] ?? null; + if (!empty($cppCompiler)) { + $this->setCppCompiler($cppCompiler); + } + // 读取 type/build-mode(支持中横线和下划线) $buildMode = $cfg['build-mode'] ?? $cfg['type'] ?? null; if (!empty($buildMode)) { diff --git a/test_phpx_dir.php b/test_phpx_dir.php deleted file mode 100644 index 150d9b85..00000000 --- a/test_phpx_dir.php +++ /dev/null @@ -1,16 +0,0 @@ -setAccessible(true); - -echo "Testing getPhpxDir()...\n"; -echo "PHPX_HOME env: " . (getenv('PHPX_HOME') ?: '(not set)') . "\n"; -echo "Result: " . $ref->invoke($compiler) . "\n"; -echo "Directory exists: " . (is_dir($ref->invoke($compiler)) ? 'YES' : 'NO') . "\n";