From ccfef0f2b6289c74a762192e58ec44def6836e7d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 23 Apr 2026 13:55:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=E4=BF=AE=E5=A4=8D=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=A1=8C=E5=8F=82=E6=95=B0=E5=A4=84=E7=90=86=E5=92=8C?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E9=80=89=E9=A1=B9=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将命令行参数验证移到路径设置之前 - 添加 translator 的 showUsage() 方法调用 - 默认关闭 debugInfo、formatCode 和 printBacktraceOnError - 在编译命令中条件性添加 -g 参数 - 仅在启用格式化时运行 clang-format - 更新帮助信息显示动态命令名称 - 添加 -d, --debug-info 选项支持 - 将 -v 选项从 verbose 改为显示版本 - 改进错误处理使用 translator 的 error 方法 --- src/Php/CompilerBase.php | 14 +++++++++++--- src/Php/Translator.php | 22 +++++++++++++--------- src/compiler-build.php | 11 ++++++----- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ca6b3e78..b0d0fd56 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -158,8 +158,9 @@ class CompilerBase extends \PhpAot\Core\Translator protected string $cxxflags = ''; protected string $ldflags = ''; protected int $floatPrecision = 17; - protected bool $debugInfo = true; - protected bool $printBacktraceOnError = true; + protected bool $debugInfo = false; + protected bool $formatCode = false; + protected bool $printBacktraceOnError = false; protected bool $noLiteralStrings = false; protected string $file; protected string $dir; @@ -2064,8 +2065,12 @@ class CompilerBase extends \PhpAot\Core\Translator { $cmd .= ' ' . $this->parseIncludes(); $cmd .= ' -O' . $this->optimizeLevel; - $cmd .= ' -g'; + + if ($this->debugInfo) { + $cmd .= ' -g'; + } $cmd .= ' -Wall'; + if ($this->enableProfiler) { $cmd .= ' -lprofiler'; $cmd .= ' -DPPROF_ON=1'; @@ -3628,6 +3633,9 @@ class CompilerBase extends \PhpAot\Core\Translator protected function formatCppCode(string $file): void { + if (!$this->formatCode) { + return; + } $cmd = 'cd ' . $this->rootPath . ' && clang-format -i ' . $file; $this->climate->info('format: ' . $this->getRelativePath($file)); $this->climate->comment($cmd); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index ec9074bb..03e02e3f 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -79,19 +79,23 @@ class Translator extends Preprocessor $this->showVersion(); $climate->br(); + global $argv; + $cmd = $argv[0]; + $climate->bold('USAGE:'); - $climate->tab()->out('./bin/compiler.php [options]'); + $climate->tab()->out($cmd . ' [options]'); $climate->br(); $climate->bold('ARGUMENTS:'); - $climate->tab()->out(' Input PHP file/directory to compile'); + $climate->tab()->out(' Input PHP file/directory/project.yml to compile'); $climate->br(); $climate->bold('OPTIONS:'); $climate->tab()->out('-O Optimization level (0-3, default: 0)'); $climate->tab()->out('-p, --profile Enable performance profiling'); + $climate->tab()->out('-d, --debug-info Enable debug info'); $climate->tab()->out('-o, --output Output binary name (default: input basename)'); - $climate->tab()->out('-v, --verbose Verbose output'); + $climate->tab()->out('-v, --version Show version'); $climate->tab()->out('-h, --help Show this help message'); $climate->tab()->out('-f, --force Force compile even if cache exists'); $climate->tab()->out('-m, --mode Compilation mode, -m bin(binary) or -m ext(extension), default: bin'); @@ -100,11 +104,11 @@ class Translator extends Preprocessor $climate->br(); $climate->bold('EXAMPLES:'); - $climate->tab()->out('./bin/compiler.php examples/hello.php'); - $climate->tab()->out('./bin/compiler.php examples/bench.php -O2'); - $climate->tab()->out('./bin/compiler.php examples/bench.php -O2 '); - $climate->tab()->out('./bin/compiler.php examples/extension -O2 -o myapp -m ext'); - $climate->tab()->out('./bin/compiler.php examples/app.php -O3 -o myapp -v'); + $climate->tab()->out($cmd . ' hello.php'); + $climate->tab()->out($cmd . ' bench.php -O2'); + $climate->tab()->out($cmd . ' project/config.yml -O2'); + $climate->tab()->out($cmd . ' my-ext/ -O2 -o myapp -m ext'); + $climate->tab()->out($cmd . ' app.php -O3 -o myapp -v'); $climate->br(); } @@ -179,7 +183,7 @@ class Translator extends Preprocessor { $realpath = realpath($path); if ($realpath === false) { - exit("path not exists: {$path}\n"); + $this->error("path not exists: {$path}"); } $path = $realpath; diff --git a/src/compiler-build.php b/src/compiler-build.php index 772b951b..0396000d 100644 --- a/src/compiler-build.php +++ b/src/compiler-build.php @@ -8,16 +8,17 @@ function main(int $argc, array $argv): void require ROOT_PATH . '/vendor/autoload.php'; - if (empty($argv[1])) { - die("php compiler.php [file]\n"); - } - global $translator; - $path = $argv[1]; $translator = new Translator(ROOT_PATH); + if (empty($argv[1])) { + $translator->showUsage(); + exit(0); + } + $translator->setIndent(' '); // 扫描所有 PHP 文件,预处理 + $path = $argv[1]; $files = $translator->prepare($path); // 生成 C++ 文件 $sourceFiles = $translator->convert($files);