feat(compiler): 添加干运行模式和构建目录选项

- 实现了 --dry 参数支持,允许仅生成 C++ 代码而不进行编译链接
- 添加了 --build-dir 参数用于指定生成 C++ 代码的构建目录
- 在翻译器中增加了 dryRun 属性和对应的判断方法
- 更新了命令行帮助信息,显示新的参数选项
- 在干运行模式下显示生成的源文件数量和构建目录路径
- 添加了相关常量定义和参数解析逻辑
pull/3/head
韩天峰 2 months ago
parent 6543dfccce
commit 3801158b41
  1. 1
      src/Php/CompilerBase.php
  2. 16
      src/Php/Constants.php
  3. 22
      src/Php/Translator.php
  4. 9
      src/compiler.php

@ -249,6 +249,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected bool $noLiteralStrings = false;
protected bool $noConsole = false; // Windows: hide console window
protected string $sanitize = ''; // Sanitizer type (address, undefined, etc.)
protected bool $dryRun = false; // Dry run: only generate C++ code, skip compile & link
protected string $file;
protected string $dir;

@ -158,11 +158,23 @@ class Constants
'defaultValue' => '',
],
'cxx-std' => [
'longPrefix' => 'cxx-std',
'longPrefix' => 'cxx-std',
'description' => 'C++ standard version (c++17, c++20, etc.)',
'required' => false,
'required' => false,
'defaultValue' => 'c++17',
],
'build-dir' => [
'longPrefix' => 'build-dir',
'description' => 'Specify the build directory for generated C++ code',
'required' => false,
'defaultValue' => '',
],
'dry' => [
'longPrefix' => 'dry',
'description' => 'Dry run: only generate C++ code, do not compile or link',
'required' => false,
'noValue' => true,
],
];
/**

@ -221,6 +221,8 @@ class Translator extends Preprocessor
$climate->tab()->out('--no-literal-strings Disable literal strings optimization');
$climate->tab()->out('--no-console Hide console window (Windows only, GUI application)');
$climate->tab()->out('--sanitize <type> Enable sanitizers (address, undefined, etc.)');
$climate->tab()->out('--build-dir <dir> Specify build directory for generated C++ code (default: <root>/build)');
$climate->tab()->out('--dry Dry run: only generate C++ code, skip compilation and linking');
$climate->br();
$climate->bold('EXAMPLES:');
@ -234,6 +236,8 @@ class Translator extends Preprocessor
$climate->tab()->out($cmd . ' app.php --cxx-std=c++17 (Use C++17 standard)');
$climate->tab()->out($cmd . ' app.php --no-literal-strings (Disable string optimization)');
$climate->tab()->out($cmd . ' app.php -r -O2 -- --flag1 value1');
$climate->tab()->out($cmd . ' hello.php --dry (only generate C++ code, skip compilation)');
$climate->tab()->out($cmd . ' app.php --build-dir /tmp/mybuild -O2 (specify build directory)');
$climate->br();
}
@ -295,6 +299,19 @@ class Translator extends Preprocessor
if ($this->climate->arguments->defined('cxx-std')) {
$this->cxxStd = $this->climate->arguments->get('cxx-std');
}
// 构建目录
if ($this->climate->arguments->defined('build-dir')) {
$buildDir = $this->climate->arguments->get('build-dir');
if (!empty($buildDir)) {
$this->setBuildDir($buildDir);
}
}
// 干运行模式
if ($this->climate->arguments->defined('dry')) {
$this->dryRun = true;
}
}
private function showVersion(): void
@ -1418,6 +1435,11 @@ CODE;
return $this->climate->arguments->defined('run');
}
public function isDryRun(): bool
{
return $this->dryRun;
}
public function run(string $targetFile): never
{
if ($this->buildMode !== self::BUILD_MODE_BIN) {

@ -22,6 +22,15 @@ function main(int $argc, array $argv): void
$files = $translator->prepare($translator->parseArgv($argv));
// 生成 C++ 文件
$sourceFiles = $translator->convert($files);
// --dry 模式:仅生成 C++ 代码,不执行编译
if ($translator->isDryRun()) {
$buildDir = $translator->getBuildDir();
$count = count($sourceFiles);
echo "Dry run completed: {$count} C++ source file(s) generated in {$buildDir}\n";
return;
}
// 编译所有 C++ 文件
$objectFiles = $translator->compile($sourceFiles);
// 连接所有目标文件,生成可执行文件

Loading…
Cancel
Save