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";