refactor(backend): 将命令构建方法从修改字符串改为返回标志字符串

- 将 Clang 和 GccLikeBackend 中的 addPICFlag 方法重构为 getPICFlag
- 将 addPlatformLinkFlags 方法重构为 getPlatformLinkFlags
- 将 addPlatformFullLinkFlags 方法重构为 getPlatformFullLinkFlags
- 修改调用方式从传引用改为直接拼接返回的标志字符串
- 统一 Windows 平台的特殊处理逻辑
- 保持原有功能不变但改进代码结构
pull/3/head
韩天峰 2 months ago
parent 73e4754a49
commit 991e4bc931
  1. 35
      src/Php/Backend/Clang.php
  2. 39
      src/Php/Backend/GccLikeBackend.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);
}
}

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

Loading…
Cancel
Save