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