mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-18 12:54:52 +08:00
Add PHP_BUILD_COMPILER for different toolchain
This commit is contained in:
parent
34edb6e329
commit
8b9b72958e
@ -7,6 +7,7 @@ namespace SPC\toolchain;
|
|||||||
use SPC\builder\freebsd\SystemUtil as FreeBSDSystemUtil;
|
use SPC\builder\freebsd\SystemUtil as FreeBSDSystemUtil;
|
||||||
use SPC\builder\linux\SystemUtil as LinuxSystemUtil;
|
use SPC\builder\linux\SystemUtil as LinuxSystemUtil;
|
||||||
use SPC\builder\macos\SystemUtil as MacOSSystemUtil;
|
use SPC\builder\macos\SystemUtil as MacOSSystemUtil;
|
||||||
|
use SPC\exception\RuntimeException;
|
||||||
use SPC\exception\WrongUsageException;
|
use SPC\exception\WrongUsageException;
|
||||||
use SPC\util\GlobalEnvManager;
|
use SPC\util\GlobalEnvManager;
|
||||||
|
|
||||||
@ -37,8 +38,19 @@ class ClangNativeToolchain implements ToolchainInterface
|
|||||||
'Linux' => LinuxSystemUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
'Linux' => LinuxSystemUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
||||||
'Darwin' => MacOSSystemUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
'Darwin' => MacOSSystemUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
||||||
'BSD' => FreeBSDSystemUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
'BSD' => FreeBSDSystemUtil::findCommand($command) ?? throw new WrongUsageException("{$command} not found, please install it or set {$env} to a valid path."),
|
||||||
default => throw new \RuntimeException(__CLASS__ . ' is not supported on ' . PHP_OS_FAMILY . '.'),
|
default => throw new RuntimeException(__CLASS__ . ' is not supported on ' . PHP_OS_FAMILY . '.'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCompilerInfo(): ?string
|
||||||
|
{
|
||||||
|
$compiler = getenv('CC') ?: 'clang';
|
||||||
|
$version = shell(false)->execWithResult("{$compiler} --version", false);
|
||||||
|
$head = pathinfo($compiler, PATHINFO_BASENAME);
|
||||||
|
if ($version[0] === 0 && preg_match('/clang version (\d+.\d+.\d+)/', $version[1][0], $match)) {
|
||||||
|
return "{$head} {$match[1]}";
|
||||||
|
}
|
||||||
|
return $head;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,4 +35,15 @@ class GccNativeToolchain implements ToolchainInterface
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,4 +9,9 @@ class MSVCToolchain implements ToolchainInterface
|
|||||||
public function initEnv(): void {}
|
public function initEnv(): void {}
|
||||||
|
|
||||||
public function afterInit(): void {}
|
public function afterInit(): void {}
|
||||||
|
|
||||||
|
public function getCompilerInfo(): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,4 +36,15 @@ class MuslToolchain implements ToolchainInterface
|
|||||||
throw new WrongUsageException('You are building with musl-libc target in glibc distro, but musl-toolchain is not installed, please install musl-toolchain first. (You can use `doctor` command to install it)');
|
throw new WrongUsageException('You are building with musl-libc target in glibc distro, but musl-toolchain is not installed, please install musl-toolchain first. (You can use `doctor` command to install it)');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCompilerInfo(): ?string
|
||||||
|
{
|
||||||
|
$compiler = getenv('CC') ?: getenv('SPC_LINUX_DEFAULT_CC');
|
||||||
|
$version = shell(false)->execWithResult("{$compiler} --version", false);
|
||||||
|
$head = pathinfo($compiler, PATHINFO_BASENAME);
|
||||||
|
if ($version[0] === 0 && preg_match('/linux-musl-cc.*(\d+.\d+.\d+)/', $version[1][0], $match)) {
|
||||||
|
return "{$head} {$match[1]}";
|
||||||
|
}
|
||||||
|
return $head;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,4 +27,12 @@ interface ToolchainInterface
|
|||||||
* post-initialization setup or validation.
|
* post-initialization setup or validation.
|
||||||
*/
|
*/
|
||||||
public function afterInit(): void;
|
public function afterInit(): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the compiler name and version for toolchains.
|
||||||
|
*
|
||||||
|
* If the toolchain does not support compiler information,
|
||||||
|
* this method can return null.
|
||||||
|
*/
|
||||||
|
public function getCompilerInfo(): ?string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -61,6 +61,10 @@ class ToolchainManager
|
|||||||
}
|
}
|
||||||
$toolchain = getenv('SPC_TOOLCHAIN');
|
$toolchain = getenv('SPC_TOOLCHAIN');
|
||||||
/* @var ToolchainInterface $toolchain */
|
/* @var ToolchainInterface $toolchain */
|
||||||
(new $toolchain())->afterInit();
|
$instance = new $toolchain();
|
||||||
|
$instance->afterInit();
|
||||||
|
if (getenv('PHP_BUILD_COMPILER') === false && ($compiler_info = $instance->getCompilerInfo())) {
|
||||||
|
GlobalEnvManager::putenv("PHP_BUILD_COMPILER={$compiler_info}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,4 +68,10 @@ class ZigToolchain implements ToolchainInterface
|
|||||||
GlobalEnvManager::putenv("SPC_EXTRA_LIBS={$extra_libs}");
|
GlobalEnvManager::putenv("SPC_EXTRA_LIBS={$extra_libs}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCompilerInfo(): ?string
|
||||||
|
{
|
||||||
|
$version = shell(false)->execWithResult('zig version', false)[1][0] ?? '';
|
||||||
|
return trim("zig {$version}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user