#!/usr/bin/env php 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]); } 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 文件 $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);