|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Package\Artifact; |
| 6 | + |
| 7 | +use StaticPHP\Artifact\ArtifactDownloader; |
| 8 | +use StaticPHP\Artifact\Downloader\DownloadResult; |
| 9 | +use StaticPHP\Attribute\Artifact\AfterBinaryExtract; |
| 10 | +use StaticPHP\Attribute\Artifact\CustomBinary; |
| 11 | +use StaticPHP\Exception\DownloaderException; |
| 12 | +use StaticPHP\Runtime\SystemTarget; |
| 13 | + |
| 14 | +class zig |
| 15 | +{ |
| 16 | + #[CustomBinary('zig', [ |
| 17 | + 'linux-x86_64', |
| 18 | + 'linux-aarch64', |
| 19 | + 'macos-x86_64', |
| 20 | + 'macos-aarch64', |
| 21 | + ])] |
| 22 | + public function downBinary(ArtifactDownloader $downloader): DownloadResult |
| 23 | + { |
| 24 | + $index_json = default_shell()->executeCurl('https://ziglang.org/download/index.json', retries: $downloader->getRetry()); |
| 25 | + $index_json = json_decode($index_json ?: '', true); |
| 26 | + $latest_version = null; |
| 27 | + foreach ($index_json as $version => $data) { |
| 28 | + $latest_version = $version; |
| 29 | + break; |
| 30 | + } |
| 31 | + |
| 32 | + if (!$latest_version) { |
| 33 | + throw new DownloaderException('Could not determine latest Zig version'); |
| 34 | + } |
| 35 | + $zig_arch = SystemTarget::getTargetArch(); |
| 36 | + $zig_os = match (SystemTarget::getTargetOS()) { |
| 37 | + 'Windows' => 'win', |
| 38 | + 'Darwin' => 'macos', |
| 39 | + 'Linux' => 'linux', |
| 40 | + default => throw new DownloaderException('Unsupported OS for Zig: ' . SystemTarget::getTargetOS()), |
| 41 | + }; |
| 42 | + $platform_key = "{$zig_arch}-{$zig_os}"; |
| 43 | + if (!isset($index_json[$latest_version][$platform_key])) { |
| 44 | + throw new DownloaderException("No download available for {$platform_key} in Zig version {$latest_version}"); |
| 45 | + } |
| 46 | + $download_info = $index_json[$latest_version][$platform_key]; |
| 47 | + $url = $download_info['tarball']; |
| 48 | + $sha256 = $download_info['shasum']; |
| 49 | + $filename = basename($url); |
| 50 | + $path = DOWNLOAD_PATH . DIRECTORY_SEPARATOR . $filename; |
| 51 | + default_shell()->executeCurlDownload($url, $path, retries: $downloader->getRetry()); |
| 52 | + // verify hash |
| 53 | + $file_hash = hash_file('sha256', $path); |
| 54 | + if ($file_hash !== $sha256) { |
| 55 | + throw new DownloaderException("Hash mismatch for downloaded Zig binary. Expected {$sha256}, got {$file_hash}"); |
| 56 | + } |
| 57 | + return DownloadResult::archive(basename($path), ['url' => $url, 'version' => $latest_version], extract: PKG_ROOT_PATH . '/zig', verified: true, version: $latest_version); |
| 58 | + } |
| 59 | + |
| 60 | + #[AfterBinaryExtract('zig', [ |
| 61 | + 'linux-x86_64', |
| 62 | + 'linux-aarch64', |
| 63 | + 'macos-x86_64', |
| 64 | + 'macos-aarch64', |
| 65 | + ])] |
| 66 | + public function postExtractZig(string $target_path): void |
| 67 | + { |
| 68 | + $files = ['zig', 'zig-cc', 'zig-c++', 'zig-ar', 'zig-ld.lld', 'zig-ranlib', 'zig-objcopy']; |
| 69 | + $all_exist = true; |
| 70 | + foreach ($files as $file) { |
| 71 | + if (!file_exists("{$target_path}/{$file}")) { |
| 72 | + $all_exist = false; |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + if ($all_exist) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + $script_path = ROOT_DIR . '/src/globals/scripts/zig-cc.sh'; |
| 81 | + $script_content = file_get_contents($script_path); |
| 82 | + |
| 83 | + file_put_contents("{$target_path}/zig-cc", $script_content); |
| 84 | + chmod("{$target_path}/zig-cc", 0755); |
| 85 | + |
| 86 | + $script_content = str_replace('zig cc', 'zig c++', $script_content); |
| 87 | + file_put_contents("{$target_path}/zig-c++", $script_content); |
| 88 | + file_put_contents("{$target_path}/zig-ar", "#!/usr/bin/env bash\nexec zig ar $@"); |
| 89 | + file_put_contents("{$target_path}/zig-ld.lld", "#!/usr/bin/env bash\nexec zig ld.lld $@"); |
| 90 | + file_put_contents("{$target_path}/zig-ranlib", "#!/usr/bin/env bash\nexec zig ranlib $@"); |
| 91 | + file_put_contents("{$target_path}/zig-objcopy", "#!/usr/bin/env bash\nexec zig objcopy $@"); |
| 92 | + chmod("{$target_path}/zig-c++", 0755); |
| 93 | + chmod("{$target_path}/zig-ar", 0755); |
| 94 | + chmod("{$target_path}/zig-ld.lld", 0755); |
| 95 | + chmod("{$target_path}/zig-ranlib", 0755); |
| 96 | + chmod("{$target_path}/zig-objcopy", 0755); |
| 97 | + } |
| 98 | +} |
0 commit comments