From e1bb0574203324b61931475529461389823a5385 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 13 Jan 2026 12:04:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=B7=BB=E5=8A=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=9C=BA=E5=88=B6=E5=92=8C=E5=BC=BA=E5=88=B6=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在Preprocessor类中添加enableCache属性控制缓存功能 - 实现getCppFile和getObjectFile方法用于文件路径处理 - 添加hasCppFileCache方法检查C++文件缓存是否存在 - 在prepare方法中集成缓存检查逻辑跳过已存在的缓存文件 - 在Translator类中添加force参数支持强制编译模式 - 移除原有的hasCache方法并优化convert方法的缓存处理 - 添加hasObjectFileCache方法检查对象文件缓存状态 - 更新compileFile方法集成对象文件缓存检查功能 - 在帮助信息中显示新的force选项说明 --- bin/compiler.php | 5 ---- src/Php/Preprocessor.php | 31 ++++++++++++++++++++++ src/Php/Translator.php | 55 ++++++++++++++++++++++------------------ 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/bin/compiler.php b/bin/compiler.php index 04bb440d..d004edda 100755 --- a/bin/compiler.php +++ b/bin/compiler.php @@ -34,11 +34,6 @@ $objectFiles = []; // 分析 PHP 文件,预处理 foreach ($list as $k => $file) { -// if ($translator->hasCache($file)) { -// echo " skip: " . $file . ", cache exists\n"; -// unset($list[$k]); -// continue; -// } if (FileScanner::isPhpFile($file)) { try { $translator->prepare($file); diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 6aac8b3e..cc2ec5c1 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -8,6 +8,8 @@ use PhpParser\NodeTraverser; class Preprocessor extends CompilerBase { + protected bool $enableCache = false; + public function sortFiles(array &$list): void { foreach ($this->functionCallInFile as $k => $call) { @@ -49,8 +51,37 @@ class Preprocessor extends CompilerBase $this->resetNamespace(); } + public function getCppFile(string $file): string + { + $info = pathinfo($file); + return $this->buildDir . '/' . $this->removeCommonPrefix($this->buildDir, $info['dirname'] . '/' . $info['filename'] . '.cc'); + } + + public function getObjectFile(string $cppFile): string + { + $info = pathinfo($cppFile); + return $info['dirname'] . '/' . $info['filename'] . '.o'; + } + + public function hasCppFileCache(string $file): bool + { + if (!$this->enableCache or $this->climate->arguments->defined('force')) { + return false; + } + $cppFile = $this->getCppFile($file); + if (file_exists($cppFile) and filemtime($cppFile) > filemtime($file)) { + return true; + } + return false; + } + public function prepare(string $file): void { + if ($this->hasCppFileCache($file)) { + $this->climate->darkGray('skip: ' . $file . ', cache exists'); + return; + } + $phpCode = $this->loadFile($file); $this->climate->info('prepare: ' . $this->file); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index ae541cd1..50bd51cc 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -45,6 +45,13 @@ class Translator extends Preprocessor 'required' => false, 'noValue' => true, ], + 'force' => [ + 'prefix' => 'f', + 'longPrefix' => 'force', + 'description' => 'Force compile even if cache exists', + 'required' => false, + 'noValue' => true, + ], ]); $this->preprocessArgvAdvanced(); @@ -77,6 +84,7 @@ class Translator extends Preprocessor $climate->tab()->out('-o, --output Output binary name (default: input basename)'); $climate->tab()->out('-v, --verbose Verbose output'); $climate->tab()->out('-h, --help Show this help message'); + $climate->tab()->out('-f, --force Force compile even if cache exists'); $climate->tab()->out('--no-literal-strings Disable literal strings optimization'); $climate->br(); @@ -88,20 +96,12 @@ class Translator extends Preprocessor $climate->br(); } - public function getCppFile(string $file): string - { - $info = pathinfo($file); - return $this->buildDir . '/' . $this->removeCommonPrefix($this->buildDir, $info['dirname'] . '/' . $info['filename'] . '.cc'); - } - - public function getObjectFile(string $cppFile): string - { - $info = pathinfo($cppFile); - return $info['dirname'] . '/' . $info['filename'] . '.o'; - } - public function convert(string $file): string { + if ($this->hasCppFileCache($file)) { + $this->climate->darkGray('skip: ' . $file . ', cache exists'); + return $this->getCppFile($file); + } $phpCode = $this->loadFile($file); $this->stubFileIncluded = false; $this->localHeaders = []; @@ -122,17 +122,6 @@ class Translator extends Preprocessor return self::PREFIX . 'register_class_' . $name; } - public function hasCache(string $file): bool - { - $cppFile = $this->getCppFile($file); - $objectFile = $this->getObjectFile($cppFile); - if ((file_exists($cppFile) and filemtime($cppFile) > filemtime($file)) - and (file_exists($objectFile) and filemtime($objectFile) > filemtime($cppFile))) { - return true; - } - return false; - } - protected function getClassCe(ClassDef $classDef): string { return self::PREFIX . 'class_entry_' . $classDef->getNamespacedName(); @@ -246,9 +235,25 @@ class Translator extends Preprocessor $this->formatCppCode($file); } - public function compileFile(string $file, string $objectFile): void + public function hasObjectFileCache(string $cppFile): bool { - $cmd = $this->cppCompiler . ' -c ' . $file . ' -o ' . $objectFile; + if (!$this->enableCache or $this->climate->arguments->defined('force')) { + return false; + } + $objectFile = $this->getObjectFile($cppFile); + if (file_exists($objectFile) and filemtime($objectFile) > filemtime($cppFile)) { + return true; + } + return false; + } + + public function compileFile(string $cppFile, string $objectFile): void + { + if ($this->hasObjectFileCache($cppFile)) { + $this->climate->darkGray('skip: ' . $cppFile . ', cache exists'); + return; + } + $cmd = $this->cppCompiler . ' -c ' . $cppFile . ' -o ' . $objectFile; $this->addCompilationOption($cmd); $this->climate->comment($cmd); shell_exec($cmd);