|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package firtoolresolver |
| 4 | + |
| 5 | +import java.io.{FileNotFoundException, IOException, InputStream} |
| 6 | +import java.net.{HttpURLConnection, URL} |
| 7 | +import java.nio.file.{Files, Path, StandardCopyOption} |
| 8 | +import java.security.{DigestInputStream, MessageDigest} |
| 9 | +import java.util.Locale |
| 10 | +import java.util.zip.GZIPInputStream |
| 11 | +import scala.io.Source |
| 12 | +import org.apache.commons.compress.archivers.ArchiveInputStream |
| 13 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream |
| 14 | +import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream |
| 15 | + |
| 16 | +/** Download CIRCT release assets and extract just the firtool executable. */ |
| 17 | +private[firtoolresolver] object GitHub { |
| 18 | + private class NotFound(url: URL) extends IOException(s"Not found: $url") |
| 19 | + |
| 20 | + /** A published release asset for one platform, with its expected SHA-256. */ |
| 21 | + final case class Release(version: String, platform: String, url: URL, sha256: String) |
| 22 | + |
| 23 | + private def open(url: URL): InputStream = { |
| 24 | + val connection = url.openConnection() |
| 25 | + connection.setConnectTimeout(10000) |
| 26 | + connection.setReadTimeout(60000) |
| 27 | + connection.setRequestProperty("User-Agent", "firtool-resolver") |
| 28 | + // HttpURLConnection follows same-protocol redirects (to release-assets.githubusercontent.com). |
| 29 | + connection match { |
| 30 | + case http: HttpURLConnection if http.getResponseCode == 404 => throw new NotFound(url) |
| 31 | + case http: HttpURLConnection if http.getResponseCode != 200 => |
| 32 | + throw new IOException(s"HTTP ${http.getResponseCode} downloading $url") |
| 33 | + case _ => |
| 34 | + } |
| 35 | + try connection.getInputStream |
| 36 | + catch { case _: FileNotFoundException => throw new NotFound(url) } // file: URLs |
| 37 | + } |
| 38 | + |
| 39 | + /** Look up the release asset and its checksum. None means the asset is not published for this platform. |
| 40 | + * |
| 41 | + * The checksum comes from the `<asset>.sha256` file published next to the asset, not from the `digest` field |
| 42 | + * of the GitHub releases API, because: |
| 43 | + * - `chisel.firtool.url` mirrors are plain file trees and do not implement the API |
| 44 | + * - releases up to firtool-1.119.0 have `"digest": null`, while the .sha256 files exist for all versions |
| 45 | + * - the API is rate limited (60 requests/hour unauthenticated), the download URLs are not |
| 46 | + * - using the API would require parsing JSON |
| 47 | + */ |
| 48 | + def lookup(logger: Logger, version: String, platform: String): Option[Release] = { |
| 49 | + val circtPlatform = if (platform == "macos-aarch64") "macos-arm64" else platform |
| 50 | + val extension = if (platform.startsWith("windows-")) "zip" else "tar.gz" |
| 51 | + val base = sys.props.get("chisel.firtool.url") |
| 52 | + .orElse(sys.env.get("CHISEL_FIRTOOL_URL")) |
| 53 | + .getOrElse("https://github.com/llvm/circt/releases/download") |
| 54 | + val url = new URL(s"${base.stripSuffix("/")}/firtool-$version/firrtl-bin-$circtPlatform.$extension") |
| 55 | + logger.debug(s"Downloading SHA-256 checksum from $url.sha256") |
| 56 | + try { |
| 57 | + val source = Source.fromInputStream(open(new URL(s"$url.sha256")), "UTF-8") |
| 58 | + val hash = try source.mkString.trim.split("\\s+").head.toLowerCase(Locale.ROOT) finally source.close() |
| 59 | + Some(Release(version, platform, url, hash)) |
| 60 | + } catch { |
| 61 | + case _: NotFound => |
| 62 | + logger.debug(s"No GitHub release asset for firtool $version on $platform") |
| 63 | + None |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** Stream the release archive, writing its firtool executable to destination, and verify the checksum. */ |
| 68 | + def extract(logger: Logger, release: Release, destination: Path): Unit = { |
| 69 | + val url = release.url |
| 70 | + val windows = release.platform.startsWith("windows-") |
| 71 | + val name = if (windows) "firtool.exe" else "firtool" |
| 72 | + logger.info(s"Downloading firtool ${release.version} for ${release.platform} from $url") |
| 73 | + val digest = MessageDigest.getInstance("SHA-256") |
| 74 | + val download = new DigestInputStream(open(url), digest) |
| 75 | + val archive: ArchiveInputStream = |
| 76 | + if (windows) new ZipArchiveInputStream(download) else new TarArchiveInputStream(new GZIPInputStream(download)) |
| 77 | + try { |
| 78 | + logger.debug(s"Extracting firtool from $url") |
| 79 | + Iterator.continually(archive.getNextEntry).takeWhile(_ != null) |
| 80 | + .find(e => !e.isDirectory && (e.getName == s"bin/$name" || e.getName.endsWith(s"/bin/$name"))) match { |
| 81 | + case Some(_) => Files.copy(archive, destination, StandardCopyOption.REPLACE_EXISTING) |
| 82 | + case None => throw new IOException(s"$url does not contain bin/$name") |
| 83 | + } |
| 84 | + // Drain the rest of the download so the digest covers the whole archive. |
| 85 | + val buffer = new Array[Byte](64 * 1024) |
| 86 | + while (download.read(buffer) != -1) {} |
| 87 | + } finally archive.close() |
| 88 | + val actual = digest.digest().map(b => f"${b & 0xff}%02x").mkString |
| 89 | + if (actual != release.sha256) { |
| 90 | + throw new IOException(s"SHA-256 checksum mismatch for $url: expected ${release.sha256}, actual $actual") |
| 91 | + } |
| 92 | + logger.debug(s"Verified SHA-256 checksum: $actual") |
| 93 | + } |
| 94 | +} |
0 commit comments