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.
 
 

86 lines
2.3 KiB

#!/usr/bin/env php
<?php
require __DIR__ . '/bootstrap.php';
use PhpAot\Php\Exception\SyntaxError;
use PhpAot\Php\Exception\Unsupported;
use PhpAot\Php\FileScanner;
use PhpAot\Php\Translator;
if (empty($argv[1])) {
die("php compiler.php [file]\n");
}
$path = $argv[1];
$translator = new Translator(ROOT_PATH);
$translator->setIndent(' ');
$list = $translator->getFiles($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]);
} catch (SyntaxError $e) {
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');
// 生成所有全局变量源文件
$extensionSourceFile = $translator->getBuildDir() . '/extension.cc';
$translator->genExtension($extensionSourceFile);
$sourceFiles[] = $extensionSourceFile;
// 添加 main.cc 文件
if ($translator->getBuildMode() == 'bin') {
$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->build($objectFiles);