TypePHP 编译器 https://swoole.com/aot/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

95 lines
2.5 KiB

#!/usr/bin/env php
<?php
require __DIR__ . '/bootstrap.php';
use PhpAot\Php\FileScanner;
use PhpAot\Php\Translator;
use PhpAot\Php\Unsupported;
if (empty($argv[1])) {
die("php compiler.php [file]\n");
}
$path = $argv[1];
$translator = new Translator(ROOT_PATH);
$translator->setIndent(' ');
$realpath = realpath($path);
if ($realpath === false) {
die("path not exists: $path\n");
}
$path = $realpath;
if (is_dir($path)) {
$scanner = new FileScanner($path);
$list = $scanner->scan();
$targetFile = basename($path);
} else {
$list = [$path];
$targetFile = FileScanner::getFileName($path);
}
$sourceFiles = [];
$objectFiles = [];
// 分析 PHP 文件,预处理
foreach ($list as $k => $file) {
if (FileScanner::isPhpFile($file)) {
try {
$translator->prepare($file);
} catch (Unsupported $e) {
echo " unsupported syntax: " . $e->getMessage() . "\n";
echo " skip: " . $file . "\n";
unset($list[$k]);
}
}
}
$translator->sortFiles($list);
// 生成 C++ 文件
foreach ($list as $k => $file) {
try {
if (FileScanner::isPhpFile($file)) {
$cppFile = $translator->convert($file);
} elseif (FileScanner::isCppFile($file)) {
$cppFile = $file;
} else {
continue;
}
$sourceFiles[] = $cppFile;
} catch (Unsupported $e) {
echo " unsupported syntax: " . $e->getMessage() . "\n";
echo " skip: " . $file . "\n";
unset($list[$k]);
}
}
if (empty($sourceFiles)) {
$translator->stop("No valid source file found");
}
// 生成所有函数声明、全局变量声明
$translator->genFunctionDeclaration($translator->getIncludeDir() . '/php_func_decl.h');
$translator->genExternGlobalVars($translator->getIncludeDir() . '/php_global_var_decl.h');
// 生成所有全局变量源文件
$globalVarsSourceFile = $translator->getBuildDir() . '/global_vars.cc';
$translator->genGlobalVars($globalVarsSourceFile);
$sourceFiles[] = $globalVarsSourceFile;
// 添加 main.cc 文件
$sourceFiles[] = ROOT_PATH . '/src/cpp/main.cc';
// 编译所有 C++ 文件
foreach ($sourceFiles as $cppFile) {
$objectFile = $translator->getObjectFile($cppFile);
$translator->compileFile($cppFile, $objectFile);
if (!is_file($objectFile)) {
throw new Exception("compile error");
}
$objectFiles[] = $objectFile;
}
// 连接所有目标文件,生成可执行文件
$translator->compileBinary($targetFile, $objectFiles);