Move zig-toolchain-only things to zig toolchain class

This commit is contained in:
crazywhalecc 2025-07-22 13:16:26 +08:00
parent 3350888af6
commit f69f8d1e4a
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
5 changed files with 34 additions and 51 deletions

View File

@ -9,9 +9,8 @@ use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException; use SPC\exception\WrongUsageException;
use SPC\store\Config; use SPC\store\Config;
use SPC\store\FileSystem; use SPC\store\FileSystem;
use SPC\toolchain\ToolchainManager;
use SPC\toolchain\ZigToolchain;
use SPC\util\SPCConfigUtil; use SPC\util\SPCConfigUtil;
use SPC\util\SPCTarget;
class Extension class Extension
{ {
@ -187,16 +186,11 @@ class Extension
*/ */
public function patchBeforeMake(): bool public function patchBeforeMake(): bool
{ {
if ( if (SPCTarget::getTargetOS() === 'Linux' && $this->isBuildShared() && ($objs = getenv('SPC_EXTRA_RUNTIME_OBJECTS'))) {
PHP_OS_FAMILY === 'Linux' &&
$this->isBuildShared() &&
ToolchainManager::getToolchainClass() === ZigToolchain::class &&
($extra = (new ZigToolchain())->getExtraRuntimeObjects())
) {
FileSystem::replaceFileRegex( FileSystem::replaceFileRegex(
SOURCE_PATH . '/php-src/Makefile', SOURCE_PATH . '/php-src/Makefile',
"/^(shared_objects_{$this->getName()}\\s*=.*)$/m", "/^(shared_objects_{$this->getName()}\\s*=.*)$/m",
"$1 {$extra}", "$1 {$objs}",
); );
return true; return true;
} }
@ -230,15 +224,11 @@ class Extension
*/ */
public function patchBeforeSharedMake(): bool public function patchBeforeSharedMake(): bool
{ {
if ( if (SPCTarget::getTargetOS() === 'Linux' && ($objs = getenv('SPC_EXTRA_RUNTIME_OBJECTS'))) {
PHP_OS_FAMILY === 'Linux' &&
ToolchainManager::getToolchainClass() === ZigToolchain::class &&
($extra = (new ZigToolchain())->getExtraRuntimeObjects())
) {
FileSystem::replaceFileRegex( FileSystem::replaceFileRegex(
$this->source_dir . '/Makefile', $this->source_dir . '/Makefile',
"/^(shared_objects_{$this->getName()}\\s*=.*)$/m", "/^(shared_objects_{$this->getName()}\\s*=.*)$/m",
"$1 {$extra}", "$1 {$objs}",
); );
return true; return true;
} }

View File

@ -10,8 +10,6 @@ use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException; use SPC\exception\WrongUsageException;
use SPC\store\FileSystem; use SPC\store\FileSystem;
use SPC\store\SourcePatcher; use SPC\store\SourcePatcher;
use SPC\toolchain\ToolchainManager;
use SPC\toolchain\ZigToolchain;
use SPC\util\GlobalEnvManager; use SPC\util\GlobalEnvManager;
use SPC\util\SPCTarget; use SPC\util\SPCTarget;
@ -62,7 +60,6 @@ class LinuxBuilder extends UnixBuilderBase
} }
// add libstdc++, some extensions or libraries need it // add libstdc++, some extensions or libraries need it
$extra_libs .= (empty($extra_libs) ? '' : ' ') . ($this->hasCpp() ? '-lstdc++ ' : ''); $extra_libs .= (empty($extra_libs) ? '' : ' ') . ($this->hasCpp() ? '-lstdc++ ' : '');
$extra_libs .= (ToolchainManager::getToolchainClass() === ZigToolchain::class ? ' -lunwind' : '');
f_putenv('SPC_EXTRA_LIBS=' . $extra_libs); f_putenv('SPC_EXTRA_LIBS=' . $extra_libs);
$cflags = $this->arch_c_flags; $cflags = $this->arch_c_flags;
f_putenv('CFLAGS=' . $cflags); f_putenv('CFLAGS=' . $cflags);

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace SPC\store\pkg; namespace SPC\store\pkg;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\store\CurlHook; use SPC\store\CurlHook;
use SPC\store\Downloader; use SPC\store\Downloader;
use SPC\store\FileSystem; use SPC\store\FileSystem;
@ -50,14 +52,14 @@ class Zig extends CustomPackage
$zig_arch = match ($arch) { $zig_arch = match ($arch) {
'x86_64', 'aarch64' => $arch, 'x86_64', 'aarch64' => $arch,
default => throw new \InvalidArgumentException('Unsupported architecture: ' . $arch), default => throw new WrongUsageException('Unsupported architecture: ' . $arch),
}; };
$zig_os = match ($os) { $zig_os = match ($os) {
'linux' => 'linux', 'linux' => 'linux',
'macos' => 'macos', 'macos' => 'macos',
'win' => 'windows', 'win' => 'windows',
default => throw new \InvalidArgumentException('Unsupported OS: ' . $os), default => throw new WrongUsageException('Unsupported OS: ' . $os),
}; };
$index_json = json_decode(Downloader::curlExec('https://ziglang.org/download/index.json', hooks: [[CurlHook::class, 'setupGithubToken']]), true); $index_json = json_decode(Downloader::curlExec('https://ziglang.org/download/index.json', hooks: [[CurlHook::class, 'setupGithubToken']]), true);
@ -69,14 +71,14 @@ class Zig extends CustomPackage
} }
if (!$latest_version) { if (!$latest_version) {
throw new \RuntimeException('Could not determine latest Zig version'); throw new RuntimeException('Could not determine latest Zig version');
} }
logger()->info("Installing Zig version {$latest_version}"); logger()->info("Installing Zig version {$latest_version}");
$platform_key = "{$zig_arch}-{$zig_os}"; $platform_key = "{$zig_arch}-{$zig_os}";
if (!isset($index_json[$latest_version][$platform_key])) { if (!isset($index_json[$latest_version][$platform_key])) {
throw new \RuntimeException("No download available for {$platform_key} in Zig version {$latest_version}"); throw new RuntimeException("No download available for {$platform_key} in Zig version {$latest_version}");
} }
$download_info = $index_json[$latest_version][$platform_key]; $download_info = $index_json[$latest_version][$platform_key];
@ -119,7 +121,6 @@ class Zig extends CustomPackage
{ {
$arch = arch2gnu(php_uname('m')); $arch = arch2gnu(php_uname('m'));
$os = match (PHP_OS_FAMILY) { $os = match (PHP_OS_FAMILY) {
'Linux' => 'linux',
'Windows' => 'win', 'Windows' => 'win',
'Darwin' => 'macos', 'Darwin' => 'macos',
'BSD' => 'freebsd', 'BSD' => 'freebsd',
@ -134,11 +135,13 @@ class Zig extends CustomPackage
]; ];
} }
/**
* @throws WrongUsageException
*/
private static function getPath(): string private static function getPath(): string
{ {
$arch = arch2gnu(php_uname('m')); $arch = arch2gnu(php_uname('m'));
$os = match (PHP_OS_FAMILY) { $os = match (PHP_OS_FAMILY) {
'Linux' => 'linux',
'Windows' => 'win', 'Windows' => 'win',
'Darwin' => 'macos', 'Darwin' => 'macos',
'BSD' => 'freebsd', 'BSD' => 'freebsd',

View File

@ -17,27 +17,8 @@ class ZigToolchain implements ToolchainInterface
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_CXX=zig-c++'); GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_CXX=zig-c++');
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_AR=ar'); GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_AR=ar');
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_LD=ld'); GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_LD=ld');
}
public function afterInit(): void
{
if (!is_dir(Zig::getEnvironment()['PATH'])) {
throw new WrongUsageException('You are building with zig, but zig is not installed, please install zig first. (You can use `doctor` command to install it)');
}
GlobalEnvManager::addPathIfNotExists(Zig::getEnvironment()['PATH']);
}
/**
* Get the extra runtime objects needed for zig toolchain.
* This method searches for `crtbeginS.o` and `crtendS.o` in common GCC library paths.
*/
public function getExtraRuntimeObjects(): string
{
static $cache = null;
if ($cache !== null) {
return $cache;
}
// Generate additional object needed for zig toolchain
$paths = ['/usr/lib/gcc', '/usr/local/lib/gcc']; $paths = ['/usr/lib/gcc', '/usr/local/lib/gcc'];
$objects = ['crtbeginS.o', 'crtendS.o']; $objects = ['crtbeginS.o', 'crtendS.o'];
$found = []; $found = [];
@ -56,8 +37,24 @@ class ZigToolchain implements ToolchainInterface
$found[] = $located; $found[] = $located;
} }
} }
GlobalEnvManager::putenv('SPC_EXTRA_RUNTIME_OBJECTS=' . implode(' ', $found));
$cache = implode(' ', $found); $extra_libs = getenv('SPC_EXTRA_LIBS') ?: '';
return $cache; if (!str_contains($extra_libs, '-lunwind')) {
// Add unwind library if not already present
$extra_libs = trim($extra_libs . ' -lunwind');
GlobalEnvManager::putenv("SPC_EXTRA_LIBS={$extra_libs}");
}
}
/**
* @throws WrongUsageException
*/
public function afterInit(): void
{
if (!is_dir(Zig::getEnvironment()['PATH'])) {
throw new WrongUsageException('You are building with zig, but zig is not installed, please install zig first. (You can use `doctor` command to install it)');
}
GlobalEnvManager::addPathIfNotExists(Zig::getEnvironment()['PATH']);
} }
} }

View File

@ -11,8 +11,6 @@ use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException; use SPC\exception\WrongUsageException;
use SPC\store\Config; use SPC\store\Config;
use SPC\toolchain\ToolchainManager;
use SPC\toolchain\ZigToolchain;
use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\ArgvInput;
class SPCConfigUtil class SPCConfigUtil
@ -71,9 +69,7 @@ class SPCConfigUtil
if ($this->builder->hasCpp()) { if ($this->builder->hasCpp()) {
$libs .= $this->builder instanceof MacOSBuilder ? ' -lc++' : ' -lstdc++'; $libs .= $this->builder instanceof MacOSBuilder ? ' -lc++' : ' -lstdc++';
} }
if (ToolchainManager::getToolchainClass() === ZigToolchain::class) { $libs .= ' ' . (getenv('SPC_EXTRA_LIBS') ?: '');
$libs .= ' -lunwind';
}
// mimalloc must come first // mimalloc must come first
if (str_contains($libs, BUILD_LIB_PATH . '/mimalloc.o')) { if (str_contains($libs, BUILD_LIB_PATH . '/mimalloc.o')) {
$libs = BUILD_LIB_PATH . '/mimalloc.o ' . str_replace(BUILD_LIB_PATH . '/mimalloc.o', '', $libs); $libs = BUILD_LIB_PATH . '/mimalloc.o ' . str_replace(BUILD_LIB_PATH . '/mimalloc.o', '', $libs);