mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-19 13:24:51 +08:00
70 lines
2.7 KiB
PHP
70 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace SPC\toolchain;
|
|
|
|
use SPC\exception\WrongUsageException;
|
|
use SPC\store\pkg\Zig;
|
|
use SPC\util\GlobalEnvManager;
|
|
|
|
class ZigToolchain implements ToolchainInterface
|
|
{
|
|
public function initEnv(): void
|
|
{
|
|
// Set environment variables for zig toolchain
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_CC=zig-cc');
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_CXX=zig-c++');
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_AR=ar');
|
|
GlobalEnvManager::putenv('SPC_LINUX_DEFAULT_LD=ld');
|
|
|
|
// Generate additional object needed for zig toolchain
|
|
$paths = ['/usr/lib/gcc', '/usr/local/lib/gcc'];
|
|
$objects = ['crtbeginS.o', 'crtendS.o'];
|
|
$found = [];
|
|
|
|
foreach ($objects as $obj) {
|
|
$located = null;
|
|
foreach ($paths as $base) {
|
|
$output = shell_exec("find {$base} -name {$obj} 2>/dev/null | grep -v '/32/' | head -n 1");
|
|
$line = trim((string) $output);
|
|
if ($line !== '') {
|
|
$located = $line;
|
|
break;
|
|
}
|
|
}
|
|
if ($located) {
|
|
$found[] = $located;
|
|
}
|
|
}
|
|
GlobalEnvManager::putenv('SPC_EXTRA_RUNTIME_OBJECTS=' . implode(' ', $found));
|
|
}
|
|
|
|
/**
|
|
* @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']);
|
|
exec('ulimit -n 2048'); // zig opens extra file descriptors, so when a lot of extensions are built statically, 1024 is not enough
|
|
$cflags = getenv('SPC_DEFAULT_C_FLAGS') ?: '';
|
|
$cxxflags = getenv('SPC_DEFAULT_CXX_FLAGS') ?: '';
|
|
$extraCflags = getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') ?: '';
|
|
$cflags = trim($cflags . ' -Wno-date-time');
|
|
$cxxflags = trim($cxxflags . ' -Wno-date-time');
|
|
$extraCflags = trim($extraCflags . ' -Wno-date-time');
|
|
GlobalEnvManager::putenv("SPC_DEFAULT_C_FLAGS={$cflags}");
|
|
GlobalEnvManager::putenv("SPC_DEFAULT_CXX_FLAGS={$cxxflags}");
|
|
GlobalEnvManager::putenv("SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS={$extraCflags}");
|
|
$extra_libs = getenv('SPC_EXTRA_LIBS') ?: '';
|
|
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}");
|
|
}
|
|
}
|
|
}
|