feat(php): 添加 C++ 编译器配置支持并优化 PHP 路径查找逻辑

- 新增 setCppCompiler 方法用于设置 C++ 编译器
- 支持从配置文件读取 cpp-compiler 配置项
- 优化 Unix/Linux/macOS 系统 PHP 路径查找逻辑,增加环境变量 PHP_HOME 支持
- 改进共享库扩展名处理,统一处理带点和不带点的情况
- 添加多个常见 PHP 8.4 安装路径的自动检测
pull/1/head
韩天峰 4 months ago
parent cbf4280fa0
commit 054560c0f9
  1. 64
      src/Php/CompilerBase.php
  2. 6
      src/Php/Translator.php
  3. 16
      test_phpx_dir.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 {

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

@ -1,16 +0,0 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use PhpAot\Php\CompilerBase;
use League\CLImate\CLImate;
$climate = new CLImate();
$compiler = new CompilerBase($climate);
$ref = new ReflectionMethod($compiler, 'getPhpxDir');
$ref->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";
Loading…
Cancel
Save