2025-11-30 15:35:04 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace StaticPHP\Toolchain;
|
|
|
|
|
|
|
|
|
|
use StaticPHP\Exception\EnvironmentException;
|
|
|
|
|
use StaticPHP\Exception\WrongUsageException;
|
|
|
|
|
use StaticPHP\Toolchain\Interface\UnixToolchainInterface;
|
|
|
|
|
use StaticPHP\Util\GlobalEnvManager;
|
|
|
|
|
use StaticPHP\Util\System\LinuxUtil;
|
|
|
|
|
use StaticPHP\Util\System\MacOSUtil;
|
|
|
|
|
|
|
|
|
|
class GccNativeToolchain implements UnixToolchainInterface
|
|
|
|
|
{
|
|
|
|
|
public function initEnv(): void
|
|
|
|
|
{
|
2026-02-03 14:01:41 +08:00
|
|
|
GlobalEnvManager::putenv('SPC_DEFAULT_CC=gcc');
|
|
|
|
|
GlobalEnvManager::putenv('SPC_DEFAULT_CXX=g++');
|
|
|
|
|
GlobalEnvManager::putenv('SPC_DEFAULT_AR=ar');
|
|
|
|
|
GlobalEnvManager::putenv('SPC_DEFAULT_LD=ld');
|
2025-11-30 15:35:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function afterInit(): void
|
|
|
|
|
{
|
|
|
|
|
foreach (['CC', 'CXX', 'AR', 'LD'] as $env) {
|
|
|
|
|
$command = getenv($env);
|
|
|
|
|
if (!$command || is_file($command)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
match (PHP_OS_FAMILY) {
|
|
|
|
|
'Linux' => LinuxUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
|
|
|
|
'Darwin' => MacOSUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
|
|
|
|
default => throw new EnvironmentException(__CLASS__ . ' is not supported on ' . PHP_OS_FAMILY . '.'),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCompilerInfo(): ?string
|
|
|
|
|
{
|
|
|
|
|
$compiler = getenv('CC') ?: 'gcc';
|
|
|
|
|
$version = shell(false)->execWithResult("{$compiler} --version", false);
|
|
|
|
|
$head = pathinfo($compiler, PATHINFO_BASENAME);
|
|
|
|
|
if ($version[0] === 0 && preg_match('/gcc.*?(\d+\.\d+\.\d+)/', $version[1][0], $match)) {
|
|
|
|
|
return "{$head} {$match[1]}";
|
|
|
|
|
}
|
|
|
|
|
return $head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isStatic(): bool
|
|
|
|
|
{
|
2026-02-02 09:59:10 +08:00
|
|
|
return PHP_OS_FAMILY === 'Linux' && LinuxUtil::isMuslDist() && !getenv('SPC_MUSL_DYNAMIC');
|
2025-11-30 15:35:04 +08:00
|
|
|
}
|
|
|
|
|
}
|