feat(compiler): 添加缓存机制和强制编译选项

- 在Preprocessor类中添加enableCache属性控制缓存功能
- 实现getCppFile和getObjectFile方法用于文件路径处理
- 添加hasCppFileCache方法检查C++文件缓存是否存在
- 在prepare方法中集成缓存检查逻辑跳过已存在的缓存文件
- 在Translator类中添加force参数支持强制编译模式
- 移除原有的hasCache方法并优化convert方法的缓存处理
- 添加hasObjectFileCache方法检查对象文件缓存状态
- 更新compileFile方法集成对象文件缓存检查功能
- 在帮助信息中显示新的force选项说明
pull/1/head
韩天峰 7 months ago
parent f3944a5259
commit e1bb057420
  1. 5
      bin/compiler.php
  2. 31
      src/Php/Preprocessor.php
  3. 55
      src/Php/Translator.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);

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

@ -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 <file> 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
{
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
{
$cmd = $this->cppCompiler . ' -c ' . $file . ' -o ' . $objectFile;
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);

Loading…
Cancel
Save