fix(cli): 修复命令行参数处理和调试选项配置

- 将命令行参数验证移到路径设置之前
- 添加 translator 的 showUsage() 方法调用
- 默认关闭 debugInfo、formatCode 和 printBacktraceOnError
- 在编译命令中条件性添加 -g 参数
- 仅在启用格式化时运行 clang-format
- 更新帮助信息显示动态命令名称
- 添加 -d, --debug-info 选项支持
- 将 -v 选项从 verbose 改为显示版本
- 改进错误处理使用 translator 的 error 方法
pull/1/head
韩天峰 4 months ago
parent 0e319c48de
commit ccfef0f2b6
  1. 14
      src/Php/CompilerBase.php
  2. 22
      src/Php/Translator.php
  3. 11
      src/compiler-build.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);

@ -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 <file/dir> [options]');
$climate->tab()->out($cmd . ' <file/dir/project.yml> [options]');
$climate->br();
$climate->bold('ARGUMENTS:');
$climate->tab()->out('<file> Input PHP file/directory to compile');
$climate->tab()->out('<file> Input PHP file/directory/project.yml to compile');
$climate->br();
$climate->bold('OPTIONS:');
$climate->tab()->out('-O <level> 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 <file> 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 <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;

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

Loading…
Cancel
Save