|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SPC\store\pkg; |
| 6 | + |
| 7 | +use SPC\store\CurlHook; |
| 8 | +use SPC\store\Downloader; |
| 9 | +use SPC\store\FileSystem; |
| 10 | +use SPC\store\LockFile; |
| 11 | + |
| 12 | +class Zig extends CustomPackage |
| 13 | +{ |
| 14 | + public function getSupportName(): array |
| 15 | + { |
| 16 | + return [ |
| 17 | + 'zig-x86_64-linux', |
| 18 | + 'zig-aarch64-linux', |
| 19 | + 'zig-x86_64-macos', |
| 20 | + 'zig-aarch64-macos', |
| 21 | + 'zig-x86_64-win', |
| 22 | + ]; |
| 23 | + } |
| 24 | + |
| 25 | + public function fetch(string $name, bool $force = false, ?array $config = null): void |
| 26 | + { |
| 27 | + $pkgroot = PKG_ROOT_PATH; |
| 28 | + $zig_exec = match (PHP_OS_FAMILY) { |
| 29 | + 'Windows' => "{$pkgroot}/{$name}/bin/zig.exe", |
| 30 | + default => "{$pkgroot}/{$name}/bin/zig", |
| 31 | + }; |
| 32 | + |
| 33 | + if (file_exists($zig_exec) && !$force) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $parts = explode('-', $name); |
| 38 | + $arch = $parts[1]; |
| 39 | + $os = $parts[2]; |
| 40 | + |
| 41 | + $zig_arch = match ($arch) { |
| 42 | + 'x86_64', 'aarch64' => $arch, |
| 43 | + default => throw new \InvalidArgumentException('Unsupported architecture: ' . $arch), |
| 44 | + }; |
| 45 | + |
| 46 | + $zig_os = match ($os) { |
| 47 | + 'linux' => 'linux', |
| 48 | + 'macos' => 'macos', |
| 49 | + 'win' => 'windows', |
| 50 | + default => throw new \InvalidArgumentException('Unsupported OS: ' . $os), |
| 51 | + }; |
| 52 | + |
| 53 | + $index_json = json_decode(Downloader::curlExec('https://ziglang.org/download/index.json', hooks: [[CurlHook::class, 'setupGithubToken']]), true); |
| 54 | + |
| 55 | + $latest_version = null; |
| 56 | + foreach ($index_json as $version => $data) { |
| 57 | + $latest_version = $version; |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + if (!$latest_version) { |
| 62 | + throw new \RuntimeException('Could not determine latest Zig version'); |
| 63 | + } |
| 64 | + |
| 65 | + logger()->info("Installing Zig version {$latest_version}"); |
| 66 | + |
| 67 | + $platform_key = "{$zig_arch}-{$zig_os}"; |
| 68 | + if (!isset($index_json[$latest_version][$platform_key])) { |
| 69 | + throw new \RuntimeException("No download available for {$platform_key} in Zig version {$latest_version}"); |
| 70 | + } |
| 71 | + |
| 72 | + $download_info = $index_json[$latest_version][$platform_key]; |
| 73 | + $url = $download_info['tarball']; |
| 74 | + $filename = basename($url); |
| 75 | + |
| 76 | + $config = [ |
| 77 | + 'type' => 'url', |
| 78 | + 'url' => $url, |
| 79 | + 'filename' => $filename, |
| 80 | + ]; |
| 81 | + |
| 82 | + Downloader::downloadPackage($name, $config, $force); |
| 83 | + } |
| 84 | + |
| 85 | + public function extract(string $name): void |
| 86 | + { |
| 87 | + $pkgroot = PKG_ROOT_PATH; |
| 88 | + $zig_bin_dir = "{$pkgroot}/{$name}"; |
| 89 | + $zig_exec = match (PHP_OS_FAMILY) { |
| 90 | + 'Windows' => "{$zig_bin_dir}/zig.exe", |
| 91 | + default => "{$zig_bin_dir}/zig", |
| 92 | + }; |
| 93 | + |
| 94 | + if (file_exists($zig_exec)) { |
| 95 | + if (!file_exists("{$zig_bin_dir}/zig-cc")) { |
| 96 | + $this->createZigCcScript($zig_bin_dir); |
| 97 | + return; |
| 98 | + } |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + $lock = json_decode(FileSystem::readFile(LockFile::LOCK_FILE), true); |
| 103 | + $source_type = $lock[$name]['source_type']; |
| 104 | + $filename = DOWNLOAD_PATH . '/' . ($lock[$name]['filename'] ?? $lock[$name]['dirname']); |
| 105 | + $extract = "{$pkgroot}/{$name}"; |
| 106 | + |
| 107 | + |
| 108 | + FileSystem::extractPackage($name, $source_type, $filename, $extract); |
| 109 | + |
| 110 | + $this->createZigCcScript($zig_bin_dir); |
| 111 | + } |
| 112 | + |
| 113 | + private function createZigCcScript(string $bin_dir): void |
| 114 | + { |
| 115 | + $zig_cc_path = "{$bin_dir}/zig-cc"; |
| 116 | + |
| 117 | + $script_content = <<<'EOF' |
| 118 | +#!/usr/bin/env bash |
| 119 | +
|
| 120 | +SPC_TARGET="${SPC_TARGET:-native-native}" |
| 121 | +SPC_LIBC="${SPC_LIBC}" |
| 122 | +SPC_LIBC_VERSION="${SPC_LIBC_VERSION}" |
| 123 | +
|
| 124 | +if [ "$SPC_LIBC" = "glibc" ]; then |
| 125 | + SPC_LIBC="gnu" |
| 126 | +fi |
| 127 | +
|
| 128 | +if [ "$SPC_TARGET" = "native-native" ] && [ -z "$SPC_LIBC" ] && [ -z "$SPC_LIBC_VERSION" ]; then |
| 129 | + exec zig cc "$@" |
| 130 | +elif [ -z "$SPC_LIBC" ] && [ -z "$SPC_LIBC_VERSION" ]; then |
| 131 | + exec zig cc -target ${SPC_TARGET} "$@" |
| 132 | +elif [ -z "$SPC_LIBC_VERSION" ]; then |
| 133 | + exec zig cc -target ${SPC_TARGET}-${SPC_LIBC} -L/usr/lib64 -lstdc++ "$@" |
| 134 | +else |
| 135 | + error_output=$(zig cc -target ${SPC_TARGET}-${SPC_LIBC}.${SPC_LIBC_VERSION} "$@" 2>&1 >/dev/null) |
| 136 | + if echo "$error_output" | grep -q "zig: error: version '.*' in target triple '${SPC_TARGET}-${SPC_LIBC}\..*' is invalid"; then |
| 137 | + exec zig cc -target ${SPC_TARGET}-${SPC_LIBC} -L/usr/lib64 -lstdc++ "$@" |
| 138 | + else |
| 139 | + exec zig cc -target ${SPC_TARGET}-${SPC_LIBC}.${SPC_LIBC_VERSION} -L/usr/lib64 -lstdc++ "$@" |
| 140 | + fi |
| 141 | +fi |
| 142 | + |
| 143 | +EOF; |
| 144 | + |
| 145 | + file_put_contents($zig_cc_path, $script_content); |
| 146 | + chmod($zig_cc_path, 0755); |
| 147 | + } |
| 148 | +} |
0 commit comments