2025-06-26 12:29:58 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\store\pkg;
|
|
|
|
|
|
2025-08-06 20:43:23 +08:00
|
|
|
use SPC\exception\DownloaderException;
|
2025-07-22 13:16:26 +08:00
|
|
|
use SPC\exception\WrongUsageException;
|
2025-06-26 12:29:58 +07:00
|
|
|
use SPC\store\CurlHook;
|
|
|
|
|
use SPC\store\Downloader;
|
|
|
|
|
use SPC\store\FileSystem;
|
|
|
|
|
use SPC\store\LockFile;
|
|
|
|
|
|
|
|
|
|
class Zig extends CustomPackage
|
|
|
|
|
{
|
2025-07-01 14:01:48 +07:00
|
|
|
public static function isInstalled(): bool
|
|
|
|
|
{
|
|
|
|
|
$path = self::getPath();
|
2025-07-27 01:07:54 +07:00
|
|
|
$files = ['zig', 'zig-cc', 'zig-c++', 'zig-ar', 'zig-ld.lld', 'zig-ranlib', 'zig-objcopy'];
|
2025-07-26 23:14:29 +07:00
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if (!file_exists("{$path}/{$file}")) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2025-07-01 14:01:48 +07:00
|
|
|
}
|
|
|
|
|
|
2025-06-26 12:29:58 +07:00
|
|
|
public function getSupportName(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'zig-x86_64-linux',
|
|
|
|
|
'zig-aarch64-linux',
|
|
|
|
|
'zig-x86_64-macos',
|
|
|
|
|
'zig-aarch64-macos',
|
|
|
|
|
'zig-x86_64-win',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function fetch(string $name, bool $force = false, ?array $config = null): void
|
|
|
|
|
{
|
|
|
|
|
$pkgroot = PKG_ROOT_PATH;
|
|
|
|
|
$zig_exec = match (PHP_OS_FAMILY) {
|
2025-06-29 00:49:39 +07:00
|
|
|
'Windows' => "{$pkgroot}/{$name}/zig.exe",
|
|
|
|
|
default => "{$pkgroot}/{$name}/zig",
|
2025-06-26 12:29:58 +07:00
|
|
|
};
|
|
|
|
|
|
2025-06-28 22:55:30 +07:00
|
|
|
if ($force) {
|
2025-06-29 00:49:39 +07:00
|
|
|
FileSystem::removeDir("{$pkgroot}/{$name}");
|
2025-06-28 22:55:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (file_exists($zig_exec)) {
|
2025-06-26 12:29:58 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parts = explode('-', $name);
|
|
|
|
|
$arch = $parts[1];
|
|
|
|
|
$os = $parts[2];
|
|
|
|
|
|
|
|
|
|
$zig_arch = match ($arch) {
|
|
|
|
|
'x86_64', 'aarch64' => $arch,
|
2025-07-22 13:16:26 +08:00
|
|
|
default => throw new WrongUsageException('Unsupported architecture: ' . $arch),
|
2025-06-26 12:29:58 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$zig_os = match ($os) {
|
|
|
|
|
'linux' => 'linux',
|
|
|
|
|
'macos' => 'macos',
|
|
|
|
|
'win' => 'windows',
|
2025-07-22 13:16:26 +08:00
|
|
|
default => throw new WrongUsageException('Unsupported OS: ' . $os),
|
2025-06-26 12:29:58 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$index_json = json_decode(Downloader::curlExec('https://ziglang.org/download/index.json', hooks: [[CurlHook::class, 'setupGithubToken']]), true);
|
|
|
|
|
|
|
|
|
|
$latest_version = null;
|
|
|
|
|
foreach ($index_json as $version => $data) {
|
2025-12-29 22:16:53 +01:00
|
|
|
// Skip the master branch, get the latest stable release
|
|
|
|
|
if ($version !== 'master') {
|
|
|
|
|
$latest_version = $version;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-06-26 12:29:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$latest_version) {
|
2025-08-06 20:43:23 +08:00
|
|
|
throw new DownloaderException('Could not determine latest Zig version');
|
2025-06-26 12:29:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger()->info("Installing Zig version {$latest_version}");
|
|
|
|
|
|
|
|
|
|
$platform_key = "{$zig_arch}-{$zig_os}";
|
|
|
|
|
if (!isset($index_json[$latest_version][$platform_key])) {
|
2025-08-06 20:43:23 +08:00
|
|
|
throw new DownloaderException("No download available for {$platform_key} in Zig version {$latest_version}");
|
2025-06-26 12:29:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$download_info = $index_json[$latest_version][$platform_key];
|
|
|
|
|
$url = $download_info['tarball'];
|
|
|
|
|
$filename = basename($url);
|
|
|
|
|
|
2025-06-28 22:55:30 +07:00
|
|
|
$pkg = [
|
2025-06-26 12:29:58 +07:00
|
|
|
'type' => 'url',
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'filename' => $filename,
|
|
|
|
|
];
|
|
|
|
|
|
2025-06-28 22:55:30 +07:00
|
|
|
Downloader::downloadPackage($name, $pkg, $force);
|
2025-06-26 12:29:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function extract(string $name): void
|
|
|
|
|
{
|
|
|
|
|
$pkgroot = PKG_ROOT_PATH;
|
2025-09-08 14:18:03 +07:00
|
|
|
$zig_bin_dir = "{$pkgroot}/zig";
|
2025-06-26 12:29:58 +07:00
|
|
|
|
2025-07-27 01:22:11 +07:00
|
|
|
$files = ['zig', 'zig-cc', 'zig-c++', 'zig-ar', 'zig-ld.lld', 'zig-ranlib', 'zig-objcopy'];
|
|
|
|
|
$all_exist = true;
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if (!file_exists("{$zig_bin_dir}/{$file}")) {
|
|
|
|
|
$all_exist = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($all_exist) {
|
2025-06-26 12:29:58 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$lock = json_decode(FileSystem::readFile(LockFile::LOCK_FILE), true);
|
|
|
|
|
$source_type = $lock[$name]['source_type'];
|
|
|
|
|
$filename = DOWNLOAD_PATH . '/' . ($lock[$name]['filename'] ?? $lock[$name]['dirname']);
|
2025-09-08 14:18:03 +07:00
|
|
|
$extract = "{$pkgroot}/zig";
|
2025-06-26 12:29:58 +07:00
|
|
|
|
|
|
|
|
FileSystem::extractPackage($name, $source_type, $filename, $extract);
|
|
|
|
|
|
|
|
|
|
$this->createZigCcScript($zig_bin_dir);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 14:29:00 +07:00
|
|
|
public static function getEnvironment(): array
|
|
|
|
|
{
|
2025-09-09 12:10:06 +07:00
|
|
|
return [];
|
2025-06-26 14:29:00 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 12:10:06 +07:00
|
|
|
public static function getPath(): ?string
|
2025-07-01 14:02:10 +07:00
|
|
|
{
|
2025-09-08 14:18:03 +07:00
|
|
|
return PKG_ROOT_PATH . '/zig';
|
2025-07-01 14:02:10 +07:00
|
|
|
}
|
|
|
|
|
|
2025-06-26 12:29:58 +07:00
|
|
|
private function createZigCcScript(string $bin_dir): void
|
|
|
|
|
{
|
2025-06-26 16:03:57 +07:00
|
|
|
$script_path = __DIR__ . '/../scripts/zig-cc.sh';
|
|
|
|
|
$script_content = file_get_contents($script_path);
|
2025-06-26 12:29:58 +07:00
|
|
|
|
2025-06-26 14:24:00 +07:00
|
|
|
file_put_contents("{$bin_dir}/zig-cc", $script_content);
|
|
|
|
|
chmod("{$bin_dir}/zig-cc", 0755);
|
|
|
|
|
|
|
|
|
|
$script_content = str_replace('zig cc', 'zig c++', $script_content);
|
|
|
|
|
file_put_contents("{$bin_dir}/zig-c++", $script_content);
|
2025-07-26 21:44:01 +07:00
|
|
|
file_put_contents("{$bin_dir}/zig-ar", "#!/usr/bin/env bash\nexec zig ar $@");
|
2025-07-27 01:07:54 +07:00
|
|
|
file_put_contents("{$bin_dir}/zig-ld.lld", "#!/usr/bin/env bash\nexec zig ld.lld $@");
|
2025-07-26 21:44:01 +07:00
|
|
|
file_put_contents("{$bin_dir}/zig-ranlib", "#!/usr/bin/env bash\nexec zig ranlib $@");
|
|
|
|
|
file_put_contents("{$bin_dir}/zig-objcopy", "#!/usr/bin/env bash\nexec zig objcopy $@");
|
2025-06-26 14:24:00 +07:00
|
|
|
chmod("{$bin_dir}/zig-c++", 0755);
|
2025-07-26 21:44:01 +07:00
|
|
|
chmod("{$bin_dir}/zig-ar", 0755);
|
2025-07-27 01:08:34 +07:00
|
|
|
chmod("{$bin_dir}/zig-ld.lld", 0755);
|
2025-07-26 21:44:01 +07:00
|
|
|
chmod("{$bin_dir}/zig-ranlib", 0755);
|
|
|
|
|
chmod("{$bin_dir}/zig-objcopy", 0755);
|
2025-06-26 14:24:00 +07:00
|
|
|
}
|
2025-06-26 12:29:58 +07:00
|
|
|
}
|