8000 GitHub - qnox/krusty: Kotlin compiler written in Rust: a drop-in replacement for kotlinc with byte-for-byte identical JVM bytecode · GitHub
[go: up one dir, main page]

Skip to content
qnoxPublic

About

Kotlin compiler written in Rust: a drop-in replacement for kotlinc with byte-for-byte identical JVM bytecode

Topics

Resources

Contributing

Stars

5 stars

Watchers

0 watching

Forks

Latest commit

 

History

5,665 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

krusty, a crab mascot

krusty: a Kotlin compiler written in Rust

CI Latest supported Kotlin version Kotlin conformance: share of applicable codegen/box cases whose box() returns OK JVM byte equality: matching leading .class bytes against kotlinc across the same codegen/box cases Native conformance: share of applicable Native codegen/box cases whose box() returns OK

krusty is built to be a drop-in replacement for kotlinc on the JVM. For the Kotlin subset it supports, it accepts kotlinc's command-line flags and aims to emit .class files byte-for-byte identical to kotlinc's, from a single native binary. Three badges measure it against the applicable cases of Kotlin's own codegen/box test suite. The conformance badge shows how much of that suite it passes today: the share of cases whose box() test returns OK. The JVM byte-equality badge shows how close its output is to kotlinc's on the same cases: the matching leading bytes of each paired .class file over the longer file length. The Native conformance badge independently shows the share of Native-applicable cases that compile against the Kotlin/Native stdlib KLIB, link, run, and return OK.

Website · Download · News: @qnox on X · Sponsor

Install

Download the archive for your platform from the latest release (Linux, macOS and Windows, on x86_64 and arm64), extract krusty, and put it on your PATH.

krusty itself needs no JVM, but it reads the JDK's class library, so a JDK must be installed (JAVA_HOME or -jdk-home <dir>).

Usage

krusty src/ -d out/                          # compile a source tree to a class directory
krusty src/ -d mylib.jar -module-name mylib  # or to a jar
krusty -cp deps.jar:classes/ App.kt -d out/  # with a classpath
krusty -help                                 # all options

The reference kotlinc version krusty targets defaults to the newest supported release; select another with -Xkotlin-reference-version=<version>.

krusty reproduces that release's defects and each platform's own behavior too, so its output is kotlinc's. With -Xfix-kotlinc-defects it fixes the defects instead: a program the release wrongly rejects or compiles to the wrong behavior compiles as the release that fixed the defect does, or as the language rules say when no release has. With -Xjvm-platform-behavior a Kotlin/Native or Kotlin/Wasm program behaves as it does on the JVM where that platform's kotlinc differs, such as the order in which classes initialize. Either flag can name the quirks it fixes (-Xfix-kotlinc-defects=<quirk>,...), and the output then differs from kotlinc's wherever a fixed quirk applies.

Gradle

Download krusty-gradle-plugin-<version>.zip from the latest GitHub release and unpack it under the build's gradle/ directory. It contains a portable local Maven repository; krusty is not published to the Gradle Plugin Portal or Maven Central.

// settings.gradle.kts
pluginManagement {
    repositories {
        maven { url = uri("gradle/krusty-gradle-plugin-<version>/repository") }
        gradlePluginPortal()
        mavenCentral()
    }
}
// build.gradle.kts
plugins {
    kotlin("jvm") version "<kotlin>" // any release listed in kotlin-versions
    id("dev.krustythecompiler") version "<version>"
}

Kotlin/JVM, Kotlin Multiplatform, the Android Gradle plugin (AGP 9's built-in Kotlin, or kotlin("android")), or a combination must be applied before krusty; an Android project's Kotlin compiles are replaced per variant. In a multiplatform project krusty compiles the JVM targets' compilations; each source set of a compilation reaches the compiler as a fragment refining the source sets it depends on, as kotlinc receives them. Other targets (Android included) keep their own compilers. kotlin("plugin.serialization") works in any order: krusty receives the Kotlin Gradle plugin's compiler-plugin classpath and options exactly as kotlinc would. The compiler identifies plugin jars and directories from their registrar service entries; a compile that requests a Kotlin compiler plugin krusty does not implement (assignment, Compose, Parcelize, ...), or a kotlinc argument krusty does not implement yet, is reported and left to kotlinc, whatever the artifact filename; the rest of the build still compiles with krusty. KSP2 (com.google.devtools.ksp) works: it runs processors in its own kspKotlin task and adds no compiler plugin to the compilation, and krusty compiles the Kotlin and Java sources the processors generate. The plugin supports the Gradle releases Gradle still maintains, 8.14 and newer; the validated versions are 8.14.8 and 9.9.2. Projects applying Gradle's kotlin-dsl (such as buildSrc) and Kotlin compile tasks outside any source set stay with kotlinc; the plugin logs each task it leaves. The plugin runs the compiler of its own release: on first use it downloads the archive for the host from that GitHub release into the Gradle user home and checks it against the release's krusty-<version>-SHA256SUMS. It trusts that file only through its signature, krusty-<version>-SHA256SUMS.asc, made by a subkey that the maintainer's OpenPGP key (fingerprint D1A7 76AB F83D BE95 D365 C2A1 B871 3034 DFAE AA99, bundled in the plugin) binds for signing, as published in krusty-<version>-signing-key.asc; a missing or invalid signature fails the build. Every release file also carries GitHub build provenance (gh attestation verify <file> -R qnox/krusty). -Pkrusty.releases=<url> reads releases from a mirror with the same layout instead. -Pkrusty.fixKotlincDefects=true (or krusty.fixKotlincDefects=true in gradle.properties) fixes kotlinc's defects in every compile krusty runs, and -Pkrusty.jvmPlatformBehavior=true gives the JVM's behavior; a list of quirk names instead of true fixes only those. A compile left to kotlinc keeps them, and the plugin says so. To run another compiler binary, use either form:

./gradlew -Pkrusty.binary=/absolute/path/to/krusty krustyCompile
KRUSTY_BIN=/absolute/path/to/krusty ./gradlew krustyCompile

Is krusty for my build?

krustyVerify answers that without changing what the build ships. It compiles every Kotlin/JVM compilation with kotlinc, as the build would without krusty, and compiles each one with krusty beside it:

./gradlew krustyVerify

The report in build/reports/krusty-verify/ says which compilations krusty compiles, which it leaves to kotlinc and why (an unsupported compiler plugin is reported as unsupported, not as a failure), krusty's errors, how many classes are byte-identical to kotlinc's and how the others differ (instructions, line numbers, Kotlin metadata, ...), and kotlinc's and krusty's compile time and krusty's peak memory. kotlinc compiles every source again for the comparison, so its time is a full compile's too.

Please submit the report: its button opens a prefilled GitHub issue, or paste krusty-verify-issue.md. It holds totals, difference kinds and unsupported requests with paths masked, and no compilation, class or member name. krusty's errors are masked by rule (their wording up to the first colon, with quoted text and identifier-like words replaced), which cannot promise to catch every name, so review the issue before you submit it. -Pkrusty.verify.shareNames=true names compilations and keeps errors whole, for an issue you are free to share in full.

Features

  • kotlinc-compatible command line for the supported subset, output to a class directory or a jar.
  • Byte-level fidelity. Every change is diffed against the real kotlinc, and every build runs JetBrains' codegen/box suite.
  • Memory, measured nightly. The metrics page compares krusty's peak memory with kotlinc's on real open-source projects.
  • Build systems and compiler plugins listed below: Gradle, the Kotlin Toolchain, the Kotlin Build Tools API and Bazel; kotlinx.serialization, all-open, no-arg, sam-with-receiver and KSP.
  • Editor support. krusty-lsp language server, with a Zed extension.

Supported build systems and compiler plugins

krusty targets every Kotlin release listed in kotlin-versions; the badge above shows the newest. On the command line, anything not listed here fails the build with an error instead of being skipped; the Gradle plugin reports it and leaves that compile task to kotlinc.

Build systems

Build system Support
Command line kotlinc-compatible flags for the supported subset; output to a class directory or a jar. Compiler plugins come in through -Xplugin/-P/-Xcompiler-plugin as for kotlinc.
Gradle id("dev.krustythecompiler") plugin, shipped as krusty-gradle-plugin-<version>.zip in each release (see Gradle). Replaces Kotlin/JVM compile tasks of source sets and of Android variants (AGP 9's built-in Kotlin or kotlin("android"), with android.jar, Java sources and generated R/BuildConfig taken from the variant); supports Gradle 8.14 and newer, validated with 8.14.8 and 9.9.2. kotlin-dsl builds such as buildSrc and compile tasks outside a source set stay with kotlinc, logged per task.
Kotlin Toolchain (project.yaml/module.yaml) krusty-toolchain, built from source in crates/krusty-toolchain, as a drop-in for JetBrains' kotlin command: show modules, show settings, show dependencies and task :<module>:compileJvm. JVM modules only; a module with Java sources, a compiler setting it does not pass yet or an artifact to download is refused by name. See docs/KOTLIN_TOOLCHAIN.md.
Kotlin Build Tools API krusty-build-tools-<version>-kotlin-<kotlin>.jar in each release, one per supported Kotlin version, stands in for kotlin-build-tools-impl in a build tool that loads the API from a classpath (JetBrains' Kotlin Toolchain among them). JVM toolchain and in-process execution only. See tools/krusty-build-tools.
Bazel krusty_jvm_library rule from the krusty Bazel module each release publishes (krusty-bazel-<version>.tar.gz), which brings the release's compiler as a toolchain per host, and a --persistent_worker speaking intellij-community's jvm-inc-builder vocabulary. The worker accepts no compiler plugins. See tools/krusty-bazel/README.md and tools/krusty-bazel/intellij.md.
Maven Verification only: the krusty-maven-plugin verify goal, shipped as krusty-maven-plugin-<version>.zip in each release, compiles each kotlin-maven-plugin compilation with krusty beside kotlinc's output and reports how krusty fares; kotlinc still builds what Maven ships. See tools/krusty-maven.

Compiler plugins

krusty cannot run kotlinc's FIR/IR plugin jars. It reimplements the plugins below natively and selects them from the same jar and options kotlinc receives, so a build switches compilers without changing its plugin configuration.

Plugin Support
kotlinx.serialization Supported. Conformance is measured against the plugin's own box-test corpus.
all-open (plugin.allopen, plugin.spring, plugin.jpa) Supported, with the spring and jpa presets.
no-arg (plugin.noarg, plugin.jpa) Supported, with the jpa preset; invokeInitializers=true is not implemented.
sam-with-receiver Supported.
KSP (KSP2) Supported through a build system: under Gradle, KSP's own kspKotlin task runs the processors and krusty compiles what they generate; under the Kotlin Toolchain, processors from Maven artifacts run before the module compiles, and krusty compiles the Kotlin sources they generate with the module's own and picks up their generated classes and resources; a processor that generates Java sources fails the module for now. The plain command line has no codegen host and rejects the KSP plugin.
kapt Supported under Gradle: kapt's own tasks generate stubs and run the annotation processors with kotlinc, and krusty compiles the module after them. kapt's jar alone, which the Kotlin Gradle plugin passes to every compile of a kapt project, changes nothing; kapt options on the command line fail with an error.
Compose, Parcelize, assignment and every other FIR/IR plugin Not supported. The command line fails with an error. The Gradle plugin reports the unsupported plugin and leaves that compile task to kotlinc, so the build still succeeds.
Kotlin scripts (.kts) Not supported.

Status

krusty compiles plain Kotlin/JVM code today, through the build systems and with the compiler plugins listed above.

Roadmap

  • 100% JVM conformance with kotlinc
  • Kotlin/Native
  • Kotlin/Wasm
  • Compose

Contributing

krusty accepts issues, not code: turning a precise issue into our own implementation is faster than reviewing an outside change. Ask your coding agent to draft one with a minimal reproduction and kotlinc's vs. krusty's result, or submit a krustyVerify report. See CONTRIBUTING.md.

Sponsor

If krusty saves your team build time or CI money, please consider sending part of it back through GitHub Sponsors.

Building from source

cargo build --release -p krusty-cli   # the compiler
cargo build --release -p krusty-lsp   # the language server
./run-tests.sh                        # full test suite against the real kotlinc

With just installed, the test harness downloads the reference kotlinc and test corpus itself. See docs/TEST_HARNESS.md for details, AGENTS.md for contributor rules, and docs/ARCHITECTURE.md for how the compiler is organized.

License

Licensed under the Apache License, Version 2.0. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work shall be licensed as above, without any additional terms or conditions.

0