refactor(compiler): 优化构建目录处理和代码生成流程

- 将干运行输出改为使用translator的output方法并添加颜色样式
- 在设置构建目录时添加路径解析和验证,确保目录存在且可访问
- 移除重复的头文件和扩展源文件生成代码
- 重构代码生成逻辑,将头文件生成移到正确的执行位置
- 添加对构建路径解析失败的异常处理
pull/3/head
韩天峰 2 months ago
parent 3801158b41
commit a6a62c8e81
  1. 10
      src/Php/CompilerBase.php
  2. 14
      src/Php/Translator.php
  3. 2
      src/compiler.php

@ -5090,10 +5090,14 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function setBuildDir(string $string): void
{
$this->buildDir = $string;
if (!is_dir($this->buildDir)) {
mkdir($this->buildDir, 0777, true);
if (!is_dir($string)) {
mkdir($string, 0777, true);
}
$resolved = realpath($string);
if ($resolved === false) {
throw new \RuntimeException('Failed to resolve build path: ' . $string);
}
$this->buildDir = $resolved;
}
protected function isStubFile(string $file): bool

@ -546,6 +546,13 @@ class Translator extends Preprocessor
if (empty($sourceFiles)) {
$this->stop('No valid source file found');
}
// 生成头文件:函数声明、全局变量声明
$this->genFunctionDeclaration($this->getIncludeDir() . "/php_{$this->targetName}_func_decl.h");
$this->genExternGlobalVars($this->getIncludeDir() . "/php_{$this->targetName}_global_var_decl.h");
// 生成扩展模块源文件
$sourceFiles[] = $this->genExtension();
return $sourceFiles;
}
@ -1052,13 +1059,6 @@ CODE;
{
$job = $this->maxJob;
// 生成所有函数声明、全局变量声明的头文件
$this->genFunctionDeclaration($this->getIncludeDir() . "/php_{$this->targetName}_func_decl.h");
$this->genExternGlobalVars($this->getIncludeDir() . "/php_{$this->targetName}_global_var_decl.h");
// 生成扩展模块的源文件
$sourceFiles[] = $this->genExtension();
// embed 需要 main 函数,以及 cli 的内置函数定义
if ($this->isBuildModeBin()) {
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/main.cc';

@ -27,7 +27,7 @@ function main(int $argc, array $argv): void
if ($translator->isDryRun()) {
$buildDir = $translator->getBuildDir();
$count = count($sourceFiles);
echo "Dry run completed: {$count} C++ source file(s) generated in {$buildDir}\n";
$translator->output("Dry run completed: {$count} C++ source file(s) generated in {$buildDir}", 'lightBlue');
return;
}

Loading…
Cancel
Save