diff --git a/bin/compiler.php b/bin/compiler.php index e9faa681..08c3f00a 100755 --- a/bin/compiler.php +++ b/bin/compiler.php @@ -58,22 +58,6 @@ 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'; - $sourceFiles[] = ROOT_PATH . '/src/cpp/php_cli_process_title.c'; - $sourceFiles[] = ROOT_PATH . '/src/cpp/ps_title.c'; -} - // 编译所有 C++ 文件 $objectFiles = $translator->compile($sourceFiles); diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 5bd33994..0a5b9841 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -572,7 +572,7 @@ class Preprocessor extends CompilerBase $this->checkRequiredArgNum($name, $this->methodDef, $v); $this->classDef->addMethod($this->methodDef); } else { - if (!($class->flags & Modifiers::ABSTRACT)) { + if (isset($class->flags) and !($class->flags & Modifiers::ABSTRACT)) { $this->fatalError($v, "Class {$this->class} cannot override non-abstract method {$v->name}"); } if ($this->method === '__construct') { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 23330978..b196a6cb 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -241,7 +241,7 @@ class Translator extends Preprocessor $this->writeFile($file, $code); } - public function genExtension(string $file): void + public function genExtension(): string { if ($this->buildMode == 'bin') { if (!$this->hasFunction('main')) { @@ -249,12 +249,14 @@ class Translator extends Preprocessor exit(1); } } + $file = $this->getBuildDir() . '/extension-' . $this->getModuleName() . '.cc'; $this->localHeaders = $this->argInfoHeaderFiles; $this->genClassCeList(); $code = $this->render('extension.cc.php'); $this->writeFile($file, $code); $this->formatCppCode($file); $this->localHeaders = []; + return $file; } public function getModuleName(): string @@ -317,17 +319,18 @@ class Translator extends Preprocessor $objectFiles = []; $job = $this->maxJob; - // 如果只有一个文件或 job 为 1,则串行编译 - if (count($sourceFiles) <= 1 || $job <= 1) { - foreach ($sourceFiles as $cppFile) { - $objectFile = $this->getObjectFile($cppFile); - $this->compileFile($cppFile, $objectFile); - if (!is_file($objectFile)) { - throw new \Exception('compile error'); - } - $objectFiles[] = $objectFile; - } - return $objectFiles; + // 生成所有函数声明、全局变量声明的头文件 + $this->genFunctionDeclaration($this->getIncludeDir() . '/php_func_decl.h'); + $this->genExternGlobalVars($this->getIncludeDir() . '/php_global_var_decl.h'); + + // 生成扩展模块的源文件 + $sourceFiles[] =$this->genExtension(); + + // embed 需要 main 函数,以及 cli 的内置函数定义 + if ($this->getBuildMode() == 'bin') { + $sourceFiles[] = __DIR__ . '/../cpp/main.cc'; + $sourceFiles[] = __DIR__ . '/../cpp/php_cli_process_title.c'; + $sourceFiles[] = __DIR__ . '/../cpp/ps_title.c'; } // 并行编译 @@ -338,7 +341,7 @@ class Translator extends Preprocessor $compiledCount = 0; $failedFiles = []; - $this->climate->blue("Starting parallel compilation with {$job} jobs for {$totalFiles} files"); + $this->climate->lightBlue("Starting parallel compilation with {$job} jobs for {$totalFiles} files"); while ($compiledCount < $totalFiles) { // 启动新进程,直到达到最大并发数