2025-06-28 16:36:05 +08:00
< ? php
declare ( strict_types = 1 );
2025-06-29 16:00:17 +08:00
namespace SPC\toolchain ;
2025-06-28 16:36:05 +08:00
use SPC\builder\freebsd\SystemUtil as FreeBSDSystemUtil ;
use SPC\builder\linux\SystemUtil as LinuxSystemUtil ;
use SPC\builder\macos\SystemUtil as MacOSSystemUtil ;
2025-08-01 12:48:48 +08:00
use SPC\exception\RuntimeException ;
2025-06-28 16:36:05 +08:00
use SPC\exception\WrongUsageException ;
use SPC\util\GlobalEnvManager ;
2025-07-22 13:16:34 +08:00
/**
* Toolchain implementation for system clang compiler .
*/
2025-06-28 16:36:05 +08:00
class ClangNativeToolchain implements ToolchainInterface
{
2025-06-29 16:00:17 +08:00
public function initEnv () : void
2025-06-28 16:36:05 +08:00
{
GlobalEnvManager :: putenv ( 'SPC_LINUX_DEFAULT_CC=clang' );
GlobalEnvManager :: putenv ( 'SPC_LINUX_DEFAULT_CXX=clang++' );
GlobalEnvManager :: putenv ( 'SPC_LINUX_DEFAULT_AR=ar' );
GlobalEnvManager :: putenv ( 'SPC_LINUX_DEFAULT_LD=ld' );
}
2025-06-29 16:00:17 +08:00
public function afterInit () : void
2025-06-28 16:36:05 +08:00
{
2025-07-01 17:53:35 +07:00
foreach ([ 'CC' , 'CXX' , 'AR' , 'LD' ] as $env ) {
$command = getenv ( $env );
2025-07-01 17:57:36 +07:00
if ( ! $command || is_file ( $command )) {
2025-07-01 17:53:35 +07:00
continue ;
}
match ( PHP_OS_FAMILY ) {
'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. " ),
'BSD' => FreeBSDSystemUtil :: findCommand ( $command ) ? ? throw new WrongUsageException ( " { $command } not found, please install it or set { $env } to a valid path. " ),
2025-08-01 12:48:48 +08:00
default => throw new RuntimeException ( __CLASS__ . ' is not supported on ' . PHP_OS_FAMILY . '.' ),
2025-07-01 17:53:35 +07:00
};
}
2025-06-28 16:36:05 +08:00
}
2025-08-01 12:48:48 +08:00
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 ;
}
2025-06-28 16:36:05 +08:00
}