From 991e4bc93118532727731a1040518a12a5ca33c6 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 26 Jun 2026 18:39:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(backend):=20=E5=B0=86=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E6=9E=84=E5=BB=BA=E6=96=B9=E6=B3=95=E4=BB=8E=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=94=B9=E4=B8=BA=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=A0=87=E5=BF=97=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 Clang 和 GccLikeBackend 中的 addPICFlag 方法重构为 getPICFlag - 将 addPlatformLinkFlags 方法重构为 getPlatformLinkFlags - 将 addPlatformFullLinkFlags 方法重构为 getPlatformFullLinkFlags - 修改调用方式从传引用改为直接拼接返回的标志字符串 - 统一 Windows 平台的特殊处理逻辑 - 保持原有功能不变但改进代码结构 --- src/Php/Backend/Clang.php | 35 +++++++++++++++------------ src/Php/Backend/GccLikeBackend.php | 39 ++++++++++++++++++------------ 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/Php/Backend/Clang.php b/src/Php/Backend/Clang.php index dbd8c0c3..b3cb8fb2 100644 --- a/src/Php/Backend/Clang.php +++ b/src/Php/Backend/Clang.php @@ -85,49 +85,52 @@ class Clang extends GccLikeBackend return '-fsanitize=' . $sanitizer; } - protected function addPICFlag(array $config, string &$cmd): void + protected function getPICFlag(array $config): string { if ($this->platform instanceof Windows) { - return; + return ''; } if ((!empty($config['build_mode']) && $config['build_mode'] === 'ext') || !empty($config['pic'])) { - $cmd .= ' -fPIC'; + return ' -fPIC'; } + return ''; } - protected function addPlatformLinkFlags(array $config, string &$cmd): void + protected function getPlatformLinkFlags(array $config): string { if ($this->platform instanceof Windows) { + $flags = ''; if (!empty($config['debug'])) { - $cmd .= ' /DEBUG'; + $flags .= ' /DEBUG'; } if (!empty($config['no_console'])) { - $cmd .= ' ' . $this->platform->getSubsystemOptions(true); + $flags .= ' ' . $this->platform->getSubsystemOptions(true); } - $cmd .= ' ' . $this->platform->getCrtConfig(); + $flags .= ' ' . $this->platform->getCrtConfig(); if (!empty($config['build_mode']) && $config['build_mode'] === 'ext') { - $cmd .= ' /DLL'; + $flags .= ' /DLL'; } - return; + return $flags; } - parent::addPlatformLinkFlags($config, $cmd); + return parent::getPlatformLinkFlags($config); } - protected function addPlatformFullLinkFlags(array $options, string &$cmd): void + protected function getPlatformFullLinkFlags(array $options): string { if ($this->platform instanceof Windows) { + $flags = ''; if (!empty($options['debug'])) { - $cmd .= ' /DEBUG'; + $flags .= ' /DEBUG'; } if (!empty($options['no_console'])) { - $cmd .= ' ' . $this->platform->getSubsystemOptions(true); + $flags .= ' ' . $this->platform->getSubsystemOptions(true); } - $cmd .= ' ' . $this->platform->getCrtConfig(); - return; + $flags .= ' ' . $this->platform->getCrtConfig(); + return $flags; } - parent::addPlatformFullLinkFlags($options, $cmd); + return parent::getPlatformFullLinkFlags($options); } } diff --git a/src/Php/Backend/GccLikeBackend.php b/src/Php/Backend/GccLikeBackend.php index 27e6eabe..c31cf242 100644 --- a/src/Php/Backend/GccLikeBackend.php +++ b/src/Php/Backend/GccLikeBackend.php @@ -50,42 +50,51 @@ abstract class GccLikeBackend extends CompilerBackend }; } - /** 添加 PIC 标志 */ - protected function addPICFlag(array $config, string &$cmd): void + /** 获取 PIC 标志 */ + protected function getPICFlag(array $config): string { if ((!empty($config['build_mode']) && $config['build_mode'] === 'ext') || !empty($config['pic'])) { - $cmd .= ' -fPIC'; + return ' -fPIC'; } + return ''; } - /** 添加平台特定的链接选项 */ - protected function addPlatformLinkFlags(array $config, string &$cmd): void + /** 获取平台特定的链接选项 */ + protected function getPlatformLinkFlags(array $config): string { + $flags = ''; + if ((!empty($config['build_mode']) && $config['build_mode'] === 'ext') || !empty($config['shared'])) { - $cmd .= ' ' . $this->platform->getSharedLinkFlag(); + $flags .= ' ' . $this->platform->getSharedLinkFlag(); if ($this->platform instanceof \PhpAot\Php\Platform\Macos && !empty($config['install_name'])) { - $cmd .= ' ' . $this->platform->getCurrentInstallNameOption($config['install_name']); + $flags .= ' ' . $this->platform->getCurrentInstallNameOption($config['install_name']); } } if (!empty($config['rpath'])) { foreach ($config['rpath'] as $path) { - $cmd .= ' -Wl,-rpath,' . escapeshellarg($path); + $flags .= ' -Wl,-rpath,' . escapeshellarg($path); } } + + return $flags; } - /** 添加平台特定的完整链接选项 */ - protected function addPlatformFullLinkFlags(array $options, string &$cmd): void + /** 获取平台特定的完整链接选项 */ + protected function getPlatformFullLinkFlags(array $options): string { + $flags = ''; + if (!empty($options['shared'])) { - $cmd .= ' ' . $this->platform->getSharedLinkFlag(); + $flags .= ' ' . $this->platform->getSharedLinkFlag(); } if (!empty($options['rpath'])) { - $cmd .= ' ' . $this->platform->getRpathOptions($options['rpath']); + $flags .= ' ' . $this->platform->getRpathOptions($options['rpath']); } + + return $flags; } // ──── 抽象方法实现 ──── @@ -259,7 +268,7 @@ abstract class GccLikeBackend extends CompilerBackend $cmd .= ' --target=' . $config['target_platform']; } - $this->addPICFlag($config, $cmd); + $cmd .= $this->getPICFlag($config); if (!empty($config['enable_profiler'])) { $cmd .= ' -DPPROF_ON=1'; @@ -289,7 +298,7 @@ abstract class GccLikeBackend extends CompilerBackend { $cmd = ''; - $this->addPlatformLinkFlags($config, $cmd); + $cmd .= $this->getPlatformLinkFlags($config); if (!empty($config['sanitize'])) { $cmd .= ' ' . $this->formatSanitizerFlag($config['sanitize']); @@ -344,7 +353,7 @@ abstract class GccLikeBackend extends CompilerBackend { $cmd = ''; - $this->addPlatformFullLinkFlags($options, $cmd); + $cmd .= $this->getPlatformFullLinkFlags($options); if (!empty($options['sanitize'])) { $cmd .= ' -fsanitize=' . $options['sanitize'];