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'];