Add homebrew llvm version toolchain support

This commit is contained in:
crazywhalecc
2026-03-20 11:20:42 +08:00
parent 00649d816e
commit e9be3a41db
4 changed files with 42 additions and 1 deletions

View File

@@ -134,6 +134,8 @@ OPENSSLDIR=""
; build target: macho or macho (possibly we could support macho-universal in the future)
; Currently we do not support universal and cross-compilation for macOS.
SPC_TARGET=native-macos
; Whether to use brew version of llvm or system version (valid options: 'system', 'brew', default: 'system')
SPC_USE_LLVM=system
; compiler environments (default value is defined by selected toolchain)
CC=${SPC_DEFAULT_CC}
CXX=${SPC_DEFAULT_CXX}

View File

@@ -7,6 +7,7 @@ namespace StaticPHP\Doctor\Item;
use StaticPHP\Attribute\Doctor\CheckItem;
use StaticPHP\Attribute\Doctor\FixItem;
use StaticPHP\Doctor\CheckResult;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Util\System\MacOSUtil;
class MacOSToolCheck
@@ -58,6 +59,20 @@ class MacOSToolCheck
return CheckResult::ok();
}
#[CheckItem('if homebrew llvm are installed', limit_os: 'Darwin')]
public function checkBrewLLVM(): ?CheckResult
{
if (getenv('SPC_USE_LLVM') === 'brew') {
$homebrew_prefix = getenv('HOMEBREW_PREFIX') ?: (SystemTarget::getTargetArch() === 'aarch64' ? '/opt/homebrew' : '/usr/local/homebrew');
if (MacOSUtil::findCommand('clang', ["{$homebrew_prefix}/opt/llvm/bin"]) === null) {
return CheckResult::fail('Homebrew llvm is not installed', 'brew', ['missing' => ['llvm']]);
}
return CheckResult::ok();
}
return null;
}
#[CheckItem('if bison version is 3.0 or later', limit_os: 'Darwin')]
public function checkBisonVersion(array $command_path = []): ?CheckResult
{

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace StaticPHP\Toolchain;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Util\GlobalEnvManager;
class ClangBrewToolchain extends ClangNativeToolchain
{
public function initEnv(): void
{
$homebrew_prefix = getenv('HOMEBREW_PREFIX') ?: (SystemTarget::getTargetArch() === 'aarch64' ? '/opt/homebrew' : '/usr/local/homebrew');
GlobalEnvManager::putenv("SPC_DEFAULT_CC={$homebrew_prefix}/opt/llvm/bin/clang");
GlobalEnvManager::putenv("SPC_DEFAULT_CXX={$homebrew_prefix}/opt/llvm/bin/clang++");
GlobalEnvManager::putenv("SPC_DEFAULT_AR={$homebrew_prefix}/opt/llvm/bin/llvm-ar");
GlobalEnvManager::putenv('SPC_DEFAULT_LD=ld');
GlobalEnvManager::addPathIfNotExists("{$homebrew_prefix}/opt/llvm/bin");
}
}

View File

@@ -39,7 +39,10 @@ class ToolchainManager
return match (PHP_OS_FAMILY) {
'Linux' => ZigToolchain::class,
'Windows' => MSVCToolchain::class,
'Darwin' => ClangNativeToolchain::class,
'Darwin' => match (getenv('SPC_USE_LLVM') ?: 'system') {
'brew' => ClangBrewToolchain::class,
default => ClangNativeToolchain::class,
},
default => throw new WrongUsageException('Unsupported OS family: ' . PHP_OS_FAMILY),
};
}