mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-19 21:34:53 +08:00
55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
|
|
<?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
|
||
|
|
{
|
||
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_CC=gcc');
|
||
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_CXX=g++');
|
||
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_AR=ar');
|
||
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_LD=ld');
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
{
|
||
|
|
return PHP_OS_FAMILY === 'Linux' && LinuxUtil::isMuslDist();
|
||
|
|
}
|
||
|
|
}
|