|
| 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\Artifact\Downloader\Type\CheckUpdateResult; |
| 10 | +use StaticPHP\Artifact\Downloader\Type\GitHubTokenSetupTrait; |
| 11 | +use StaticPHP\Attribute\Artifact\AfterBinaryExtract; |
| 12 | +use StaticPHP\Attribute\Artifact\CustomBinary; |
| 13 | +use StaticPHP\Attribute\Artifact\CustomBinaryCheckUpdate; |
| 14 | +use StaticPHP\DI\ApplicationContext; |
| 15 | +use StaticPHP\Exception\BuildFailureException; |
| 16 | +use StaticPHP\Exception\DownloaderException; |
| 17 | +use StaticPHP\Package\PackageBuilder; |
| 18 | + |
| 19 | +class llvm_tools |
| 20 | +{ |
| 21 | + use GitHubTokenSetupTrait; |
| 22 | + |
| 23 | + public const array TOOLS = ['llvm-objcopy', 'llvm-strip', 'llvm-profdata']; |
| 24 | + |
| 25 | + /** Install prefix for the locally-built llvm-tools. */ |
| 26 | + public static function path(): string |
| 27 | + { |
| 28 | + return PKG_ROOT_PATH . '/llvm-tools'; |
| 29 | + } |
| 30 | + |
| 31 | + /** Path to a binary under llvm-tools/bin (llvm-objcopy, llvm-strip, llvm-profdata, …). */ |
| 32 | + public static function binary(string $name = 'llvm-strip'): string |
| 33 | + { |
| 34 | + return self::path() . '/bin/' . $name; |
| 35 | + } |
| 36 | + |
| 37 | + /** True when every required TOOLS binary is present and executable. */ |
| 38 | + public static function isInstalled(): bool |
| 39 | + { |
| 40 | + foreach (self::TOOLS as $t) { |
| 41 | + $p = self::binary($t); |
| 42 | + if (!is_file($p) || !is_executable($p)) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + #[CustomBinary('llvm-tools', [ |
| 50 | + 'linux-x86_64', |
| 51 | + 'linux-aarch64', |
| 52 | + 'macos-x86_64', |
| 53 | + 'macos-aarch64', |
| 54 | + ])] |
| 55 | + public function downBinary(ArtifactDownloader $downloader): DownloadResult |
| 56 | + { |
| 57 | + $llvmVersion = $this->detectLlvmVersion() |
| 58 | + ?? throw new DownloaderException('Could not detect a clang version on host; install zig or clang first'); |
| 59 | + $tarball = "llvm-project-{$llvmVersion}.src.tar.xz"; |
| 60 | + $url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-{$llvmVersion}/{$tarball}"; |
| 61 | + $tarballPath = DOWNLOAD_PATH . '/' . $tarball; |
| 62 | + default_shell()->executeCurlDownload($url, $tarballPath, headers: $this->getGitHubTokenHeaders(), retries: $downloader->getRetry()); |
| 63 | + return DownloadResult::archive($tarball, ['url' => $url, 'version' => $llvmVersion], extract: '{source_path}/llvm-tools', verified: false, version: $llvmVersion); |
| 64 | + } |
| 65 | + |
| 66 | + #[CustomBinaryCheckUpdate('llvm-tools', [ |
| 67 | + 'linux-x86_64', |
| 68 | + 'linux-aarch64', |
| 69 | + 'macos-x86_64', |
| 70 | + 'macos-aarch64', |
| 71 | + ])] |
| 72 | + public function checkUpdateBinary(?string $old_version, ArtifactDownloader $downloader): CheckUpdateResult |
| 73 | + { |
| 74 | + $llvmVersion = $this->detectLlvmVersion() |
| 75 | + ?? throw new DownloaderException('Could not detect a clang version on host; install zig or clang first'); |
| 76 | + return new CheckUpdateResult( |
| 77 | + old: $old_version, |
| 78 | + new: $llvmVersion, |
| 79 | + needUpdate: $old_version === null || $llvmVersion !== $old_version, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[AfterBinaryExtract('llvm-tools', [ |
| 84 | + 'linux-x86_64', |
| 85 | + 'linux-aarch64', |
| 86 | + 'macos-x86_64', |
| 87 | + 'macos-aarch64', |
| 88 | + ])] |
| 89 | + public function postExtract(string $target_path): void |
| 90 | + { |
| 91 | + $this->buildForHost($target_path); |
| 92 | + } |
| 93 | + |
| 94 | + public function buildForHost(?string $sourceRoot = null): void |
| 95 | + { |
| 96 | + $sourceRoot ??= SOURCE_PATH . '/llvm-tools'; |
| 97 | + if (self::isInstalled()) { |
| 98 | + return; |
| 99 | + } |
| 100 | + $llvmDir = "{$sourceRoot}/llvm"; |
| 101 | + if (!is_dir($llvmDir)) { |
| 102 | + throw new BuildFailureException("llvm-tools: missing source at {$llvmDir} (extraction layout changed?)"); |
| 103 | + } |
| 104 | + $buildDir = "{$sourceRoot}/build"; |
| 105 | + $installDir = self::path(); |
| 106 | + $binDir = self::path() . '/bin'; |
| 107 | + f_mkdir($buildDir, recursive: true); |
| 108 | + f_mkdir($binDir, recursive: true); |
| 109 | + |
| 110 | + $cmakeArgs = implode(' ', array_map('escapeshellarg', [ |
| 111 | + '-S', $llvmDir, |
| 112 | + '-B', $buildDir, |
| 113 | + '-DCMAKE_BUILD_TYPE=Release', |
| 114 | + '-DLLVM_ENABLE_PROJECTS=', |
| 115 | + '-DLLVM_TARGETS_TO_BUILD=', |
| 116 | + '-DLLVM_INCLUDE_BENCHMARKS=OFF', |
| 117 | + '-DLLVM_INCLUDE_TESTS=OFF', |
| 118 | + '-DLLVM_INCLUDE_EXAMPLES=OFF', |
| 119 | + '-DLLVM_INCLUDE_DOCS=OFF', |
| 120 | + '-DLLVM_ENABLE_ZLIB=OFF', |
| 121 | + '-DLLVM_ENABLE_ZSTD=OFF', |
| 122 | + '-DLLVM_ENABLE_LIBXML2=OFF', |
| 123 | + '-DLLVM_ENABLE_TERMINFO=OFF', |
| 124 | + '-DLLVM_ENABLE_LIBEDIT=OFF', |
| 125 | + '-DLLVM_ENABLE_LIBPFM=OFF', |
| 126 | + '-DLLVM_BUILD_LLVM_DYLIB=OFF', |
| 127 | + '-DLLVM_LINK_LLVM_DYLIB=OFF', |
| 128 | + '-DBUILD_SHARED_LIBS=OFF', |
| 129 | + '-DCMAKE_C_COMPILER=' . zig::binary('zig-cc'), |
| 130 | + '-DCMAKE_CXX_COMPILER=' . zig::binary('zig-c++'), |
| 131 | + '-DCMAKE_INSTALL_PREFIX=' . $installDir, |
| 132 | + ])); |
| 133 | + $jobs = ApplicationContext::get(PackageBuilder::class)->concurrency; |
| 134 | + $targetArgs = implode(' ', array_map(fn ($t) => '--target ' . escapeshellarg($t), self::TOOLS)); |
| 135 | + |
| 136 | + shell() |
| 137 | + ->setEnv(['SPC_TARGET' => GNU_ARCH . '-linux-musl']) |
| 138 | + ->exec('cmake ' . $cmakeArgs) |
| 139 | + ->exec('cmake --build ' . escapeshellarg($buildDir) . ' ' . $targetArgs . " -j {$jobs}"); |
| 140 | + |
| 141 | + foreach (self::TOOLS as $t) { |
| 142 | + $built = "{$buildDir}/bin/{$t}"; |
| 143 | + if (!is_file($built)) { |
| 144 | + throw new BuildFailureException("llvm-tools: missing build output {$built}"); |
| 145 | + } |
| 146 | + copy($built, self::binary($t)); |
| 147 | + chmod(self::binary($t), 0755); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private function detectLlvmVersion(): ?string |
| 152 | + { |
| 153 | + if (!zig::isInstalled()) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + [$rc, $out] = shell()->execWithResult(escapeshellarg(zig::binary()) . ' cc --version', false); |
| 157 | + if ($rc !== 0) { |
| 158 | + return null; |
| 159 | + } |
| 160 | + return preg_match('/clang version (\d+\.\d+\.\d+)/', implode("\n", $out), $m) ? $m[1] : null; |
| 161 | + } |
| 162 | +} |
0 commit comments