From 8267ae2bf4be427064cd7c8ea513777dfae39ecc Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 18 Jun 2026 15:10:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(build):=20=E6=B7=BB=E5=8A=A0=E4=BA=A4?= =?UTF-8?q?=E5=8F=89=E7=BC=96=E8=AF=91=E7=9B=AE=E6=A0=87=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 Clang 和 GCC 编译器后端中添加 --target 参数支持 - 添加 target-platform 配置选项的常量定义 - 在命令行参数解析中添加对 target-platform 的支持 - 将 target-platform 配置传递给编译器命令生成逻辑 - 更新帮助文档显示新的交叉编译选项 - 支持指定交叉编译目标三元组 (如 aarch64-linux-gnu) --- src/Php/Backend/Clang.php | 10 ++++++++++ src/Php/Backend/Gcc.php | 10 ++++++++++ src/Php/Constants.php | 6 ++++++ src/Php/Translator.php | 9 +++++++++ 4 files changed, 35 insertions(+) diff --git a/src/Php/Backend/Clang.php b/src/Php/Backend/Clang.php index 7e1d6c8c..23ed1f2b 100644 --- a/src/Php/Backend/Clang.php +++ b/src/Php/Backend/Clang.php @@ -408,6 +408,11 @@ class Clang extends CompilerBackend $cmd .= ' -march=' . $config['march']; } + // 交叉编译目标平台 (--target=) + if (!empty($config['target_platform'])) { + $cmd .= ' --target=' . $config['target_platform']; + } + // PIC (Position Independent Code) if ((!empty($config['build_mode']) && $config['build_mode'] === 'ext') || !empty($config['pic'])) { if ($this->platform instanceof \PhpAot\Php\Platform\Windows) { @@ -495,6 +500,11 @@ class Clang extends CompilerBackend $cmd .= ' -fsanitize=' . $config['sanitize']; } + // 交叉编译目标平台 (--target=) + if (!empty($config['target_platform'])) { + $cmd .= ' --target=' . $config['target_platform']; + } + // LTO(链接时优化) if (!empty($config['lto'])) { $cmd .= ' -flto'; diff --git a/src/Php/Backend/Gcc.php b/src/Php/Backend/Gcc.php index 40c5c643..bfb5a6f9 100644 --- a/src/Php/Backend/Gcc.php +++ b/src/Php/Backend/Gcc.php @@ -294,6 +294,11 @@ class Gcc extends CompilerBackend $cmd .= ' -march=' . $config['march']; } + // 交叉编译目标平台 (--target=) + if (!empty($config['target_platform'])) { + $cmd .= ' --target=' . $config['target_platform']; + } + // PIC (Position Independent Code) if ((!empty($config['build_mode']) && $config['build_mode'] === 'ext') || !empty($config['pic'])) { $cmd .= ' -fPIC'; @@ -359,6 +364,11 @@ class Gcc extends CompilerBackend } } + // 交叉编译目标平台 (--target=) + if (!empty($config['target_platform'])) { + $cmd .= ' --target=' . $config['target_platform']; + } + // LTO(链接时优化) if (!empty($config['lto'])) { $cmd .= ' -flto'; diff --git a/src/Php/Constants.php b/src/Php/Constants.php index 937d50cd..c4601188 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -169,6 +169,12 @@ class Constants 'required' => false, 'defaultValue' => '', ], + 'target-platform' => [ + 'longPrefix' => 'target-platform', + 'description' => 'Cross-compilation target triple (e.g. aarch64-linux-gnu, x86_64-w64-mingw32)', + 'required' => false, + 'defaultValue' => '', + ], 'no-color' => [ 'longPrefix' => 'no-color', 'description' => 'Disable ANSI color output', diff --git a/src/Php/Translator.php b/src/Php/Translator.php index b1c9c3c4..92e8c1c5 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -232,6 +232,7 @@ class Translator extends Preprocessor $climate->tab()->out('-j, --job Number of parallel compilation jobs (default: 4)'); $climate->tab()->out('--cxx-std C++ standard version (c++17, c++20, etc., default: c++17)'); $climate->tab()->out('--march Target CPU instruction set (e.g. native, x86-64-v3, armv8-a)'); + $climate->tab()->out('--target-platform Cross-compilation target triple (e.g. aarch64-linux-gnu)'); $climate->tab()->out('--lto Enable Link Time Optimization (-flto)'); $climate->tab()->out('--no-literal-strings Disable literal strings optimization'); $climate->tab()->out('--no-console Hide console window (Windows only, GUI application)'); @@ -311,6 +312,11 @@ class Translator extends Preprocessor $this->march = $this->climate->arguments->get('march'); } + // 交叉编译目标平台 + if ($this->climate->arguments->defined('target-platform')) { + $this->targetPlatform = $this->climate->arguments->get('target-platform'); + } + // 构建目录 if ($this->climate->arguments->defined('build-dir')) { $buildDir = $this->climate->arguments->get('build-dir'); @@ -1403,6 +1409,7 @@ CODE; 'sanitize' => $this->sanitize, 'cpp_std' => $this->cxxStd, 'march' => $this->march, + 'target_platform' => $this->targetPlatform, 'is_zts' => $this->isPhpZts, 'build_mode' => $this->buildMode, 'enable_profiler' => $this->enableProfiler, @@ -1442,6 +1449,7 @@ CODE; 'enable_profiler' => $this->enableProfiler, 'suppressed_warnings' => Constants::MSVC_SUPPRESSED_WARNINGS ?? [], 'march' => $this->march, + 'target_platform' => $this->targetPlatform, ]; } @@ -1472,6 +1480,7 @@ CODE; 'build_mode' => $this->buildMode, 'sanitize' => $this->sanitize, 'lto' => $this->enableLto, + 'target_platform' => $this->targetPlatform, ]; $rpaths = $this->getPlatform()->getDefaultRpaths(