diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 3257b200..51b7ec89 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.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; diff --git a/src/Php/Constants.php b/src/Php/Constants.php index cd0a505c..f2f4e23a 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -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, + ], ]; /** diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 823ce576..2806b9c7 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -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 Enable sanitizers (address, undefined, etc.)'); + $climate->tab()->out('--build-dir Specify build directory for generated C++ code (default: /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) { diff --git a/src/compiler.php b/src/compiler.php index 2fd129c7..63366913 100644 --- a/src/compiler.php +++ b/src/compiler.php @@ -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); // 连接所有目标文件,生成可执行文件