Skip to content

Commit bdb4636

Browse files
authored
Fetch firtool from GitHub instead of Maven Central (#37)
1 parent 40c8d7e commit bdb4636

16 files changed

Lines changed: 636 additions & 89 deletions

‎.github/workflows/cd-llvm-firtool.yml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ name: Continuous Delivery of llvm-firtool
22

33
on:
44
workflow_dispatch:
5-
# Run every 15 min, checking for new releases is cheap.
6-
schedule:
7-
- cron: '*/15 * * * *'
85

96

107
jobs:

‎README.md‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,73 @@
11
# Firtool Resolver and LLVM Firtool Native
22

3-
Firtool Resolver is a Scala library and command-line application for downloading [LLVM Firtool](https://github.com/llvm/circt) as a Maven artifact.
4-
LLVM Firtool Native is a project to distribute LLVM Firtool as Maven artifacts.
3+
Firtool Resolver is a Scala library and command-line application for resolving and downloading [LLVM Firtool](https://github.com/llvm/circt).
4+
It downloads binaries directly from CIRCT's GitHub releases and verifies their SHA-256 checksums.
55

6-
# Firtool Native Dependency Distribution Specification (FNDDS)
6+
LLVM Firtool Native (`org.chipsalliance:llvm-firtool`) and FNDDS are **legacy** distribution mechanisms. The last version published to Maven Central is **1.162.0**; existing artifacts and classpath overrides continue to work.
7+
8+
## Using Firtool Resolver
9+
10+
```scala
11+
import firtoolresolver.Resolve
12+
13+
Resolve("1.163.0") match {
14+
case Right(binary) => println(binary.path)
15+
case Left(error) => sys.error(error)
16+
}
17+
```
18+
19+
The command-line interface is `firtool-resolver [-v] <version>`. For example:
20+
21+
```sh
22+
cs launch --scala 2.13 org.chipsalliance::firtool-resolver:2.2.0 \
23+
--main firtoolresolver.Main -- -v 1.163.0
24+
```
25+
26+
### Resolution order
27+
28+
1. Run `firtool` from the directory specified by `CHISEL_FIRTOOL_PATH`. An invalid path or a binary that cannot run is an unrecoverable error, as before.
29+
2. Choose the effective version: system property `chisel.firtool.version`, then environment variable `CHISEL_FIRTOOL_VERSION`, then the caller's default version. An explicit override skips the classpath lookup and warns if `llvm-firtool` is also on the classpath.
30+
3. If no explicit version is set, honor `llvm-firtool` on the classpath. This legacy mechanism logs a deprecation message at info level.
31+
4. Reuse the cached binary at `<cache>/<effective-version>/bin/firtool`.
32+
5. Download the effective version from GitHub, verify its checksum, and atomically install just the executable in the cache. Downloads are locked per version across processes.
33+
6. As a last resort, fetch `llvm-firtool` with Coursier from Maven. This supports older versions and environments that can reach a Maven proxy but cannot reach GitHub; it only works for already-published artifacts.
34+
35+
### Configuration
36+
37+
| Setting | System property | Environment variable | Default |
38+
|---|---|---|---|
39+
| Executable directory | — | `CHISEL_FIRTOOL_PATH` | Unset |
40+
| Version override | `chisel.firtool.version` | `CHISEL_FIRTOOL_VERSION` | Caller's default version |
41+
| Release base URL / mirror | `chisel.firtool.url` | `CHISEL_FIRTOOL_URL` | `https://github.com/llvm/circt/releases/download` |
42+
| Cache directory | — | `CHISEL_FIRTOOL_CACHE` | OS-specific project cache directory |
43+
44+
Properties take precedence over the corresponding environment variables.
45+
46+
Direct downloads require access to **github.com** and **release-assets.githubusercontent.com**. Standard JVM proxy properties (`https.proxyHost`, `https.proxyPort`, and the corresponding HTTP properties for HTTP mirrors) are honored.
47+
48+
To use an internal web server (HTTP or HTTPS), an Artifactory generic remote, or a local `file:` mirror, set the release base URL. The mirror must provide both the archive and its `.sha256` file at:
49+
50+
```text
51+
<base>/firtool-<version>/firrtl-bin-<platform>.<ext>
52+
<base>/firtool-<version>/firrtl-bin-<platform>.<ext>.sha256
53+
```
54+
55+
| Resolver platform | Release asset |
56+
|---|---|
57+
| `linux-x64` | `firrtl-bin-linux-x64.tar.gz` |
58+
| `macos-x64` | `firrtl-bin-macos-x64.tar.gz` |
59+
| `macos-aarch64` | `firrtl-bin-macos-arm64.tar.gz` |
60+
| `windows-x64` | `firrtl-bin-windows-x64.zip` |
61+
62+
GitHub fetching supports versions starting at **1.45.0**. Native macOS arm64 assets start at **1.144.0**; earlier versions fall back to macOS x64 through Rosetta. Older releases remain available through the legacy Maven path.
63+
64+
### Development and release
65+
66+
Run `./mill -i 'firtool-resolver[_].test'` for the hermetic MUnit resolver tests on both Scala versions, or `./mill -i 'firtool-resolver[2.13].test'` for just Scala 2.13.
67+
68+
The FileCheck integration suite is `./firtool-resolver/test/run_tests.sh <linux|macos|windows> <2.12|2.13>`; it requires Coursier, FileCheck, and network access, along with `LLVM_FIRTOOL_VERSION` and `LLVM_FIRTOOL_PRERELEASE` for the retained legacy `publishLocal` build.
69+
70+
# Firtool Native Dependency Distribution Specification (FNDDS) (legacy)
771

872
This specification follows [Semantic Versioning 2.0.0](https://semver.org).
973

‎build.mill‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ object `llvm-firtool` extends JavaModule with ChipsAlliancePublishModule {
9191

9292
def firtoolVersion = getEnvVariable("LLVM_FIRTOOL_VERSION")
9393
// FNDDS requires that the publish version start with firtool version with optional -<suffix>
94-
def isSnapshot = T { getEnvVariable("LLVM_FIRTOOL_PRERELEASE") != "0" }
94+
def isSnapshot = T.input { getEnvVariable("LLVM_FIRTOOL_PRERELEASE") != "0" }
9595

9696
def publishSuffix = T { if (isSnapshot()) "-SNAPSHOT" else "" }
9797
def publishVersion = T { firtoolVersion + publishSuffix() }
@@ -293,6 +293,7 @@ trait FirtoolResolver extends CrossScalaModule with ChipsAlliancePublishModule {
293293
def ivyDeps = Agg(
294294
ivy"dev.dirs:directories:26",
295295
ivy"io.get-coursier::coursier:2.1.8",
296+
ivy"org.apache.commons:commons-compress:1.24.0",
296297
)
297298

298299
// Modify the classpath to remove things we want to dynamically link (Scala jars).
@@ -334,4 +335,16 @@ trait FirtoolResolver extends CrossScalaModule with ChipsAlliancePublishModule {
334335
Rule.Relocate(s"$p.**", s"firtoolresolver.shaded.$p.@1")
335336
)
336337
}
338+
339+
object test extends ScalaModule with TestModule.Munit {
340+
def scalaVersion = root.scalaVersion
341+
def scalacOptions = root.scalacOptions
342+
def moduleDeps = Seq(core)
343+
def ivyDeps = Agg(ivy"org.scalameta::munit:1.3.6")
344+
// Mill loads tests through a separate classloader. Give subprocess tests
345+
// the complete classpath explicitly rather than the test runner's launcher.
346+
def forkArgs = T {
347+
Seq("-Dfirtoolresolver.test.classpath=" + runClasspath().map(_.path.toString).mkString(java.io.File.pathSeparator))
348+
}
349+
}
337350
}

‎firtool-resolver/src/GitHub.scala‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)