From a6a62c8e81c7183a9b4338e8de3fb36c3927eee5 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 18 Jun 2026 11:42:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(compiler):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E7=9B=AE=E5=BD=95=E5=A4=84=E7=90=86=E5=92=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将干运行输出改为使用translator的output方法并添加颜色样式 - 在设置构建目录时添加路径解析和验证,确保目录存在且可访问 - 移除重复的头文件和扩展源文件生成代码 - 重构代码生成逻辑,将头文件生成移到正确的执行位置 - 添加对构建路径解析失败的异常处理 --- src/Php/CompilerBase.php | 10 +++++++--- src/Php/Translator.php | 14 +++++++------- src/compiler.php | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 51b7ec89..6ce94775 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.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 diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 2806b9c7..196eeebd 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -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'; diff --git a/src/compiler.php b/src/compiler.php index 63366913..5dcf9843 100644 --- a/src/compiler.php +++ b/src/compiler.php @@ -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; }