summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md130
1 files changed, 93 insertions, 37 deletions
diff --git a/README.md b/README.md
index 2c14965..0a6e7d5 100644
--- a/README.md
+++ b/README.md
@@ -9,23 +9,38 @@ compile inside the stub. **Pure replacement: no passthrough, no fallback.**
```
kotlin-proxy/
+├── Makefile # builds everything (see below)
├── tools/gen_class.py # bytecode stub generator (self-contained)
+├── tools/gen_jar_lists.py # regenerates the jars/*.txt lists from the Gradle cache
+├── docs/opcodes.html # JVM bytecode opcode reference (for gen_class.py work)
├── stubs/
│ ├── src/kotlin/proxy/KotlinProxyLog.java # the logger the stubs call
│ ├── src/kotlin/proxy/KotlinProxyOverrides.java # per-method custom return values
│ ├── src/kotlin/proxy/ProxyPerformanceManager.java # override support class
│ └── classes_min/ # generated .class files (build output)
├── jars/
-│ ├── defs.txt # 28 kotlin-2.2.10 jars (definition lookup)
-│ ├── scan.txt # 6 AGP-9.4.1 jars (which kotlin classes AGP touches)
-│ └── fallback.txt # newer KGP variants (definition lookup only)
+│ ├── defs.txt # all kotlin-<version> jars, sorted (definition lookup)
+│ ├── scan.txt # AGP core jars (which kotlin classes AGP touches)
+│ ├── fallback.txt # other cached KGP versions (definition lookup only)
+│ └── .stamp # versions the lists were generated for (drift detection)
├── dist/kotlin-proxy-compiler-stubs.jar # the artifact
└── test_project/ # AGP test app that consumes the jar
```
## Building `dist/kotlin-proxy-compiler-stubs.jar`
-Three steps, from the repo root. Requires JDK 11+ and Python 3.
+```bash
+make
+```
+
+from the repo root (requires JDK 11+ and Python 3). The Makefile regenerates
+`jars/*.txt` via `tools/gen_jar_lists.py` whenever the pinned versions (its
+`KOTLIN_VERSION` / `AGP_VERSION` vars — keep in sync with
+`test_project/gradle/libs.versions.toml`) drift from `jars/.stamp`, then runs
+the three steps below. The Gradle cache must already hold the pinned versions
+— run `test_project` once first (even a red build resolves everything).
+
+The three steps, manually:
### 1. Generate the stub classes with `gen_class.py`
@@ -74,13 +89,14 @@ classpath so your overrides may import compiler types (`CommonCompilerArguments`
jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min .
```
-Result: 6 class files + manifest
+Result: 7 class files + manifest
```
$ jar tf dist/kotlin-proxy-compiler-stubs.jar | grep -v '/$'
META-INF/MANIFEST.MF
kotlin/proxy/KotlinProxyLog.class
kotlin/proxy/KotlinProxyOverrides.class
+kotlin/proxy/KotlinProxyOverrides$Compiler.class
kotlin/proxy/ProxyPerformanceManager.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class
@@ -111,9 +127,13 @@ Bodies are straight-line (no branches), so no `StackMapTable` frames are needed.
The list files contain one jar path per line; `#` starts a comment.
- `--def-jars` / `--fallback-jars` — the definition source. Every `.class` in
- these jars is parsed into a `name → info` map (first jar wins). `defs.txt`
- holds the full 2.2.10 set (KGP first for priority); `fallback.txt` adds newer
- KGP variants for definitions not present in 2.2.10.
+ these jars is parsed into a `name → info` map (first jar wins). Both files
+ are auto-generated by `tools/gen_jar_lists.py`: `defs.txt` holds every
+ `org.jetbrains.kotlin` jar at the pinned Kotlin version, sorted by jar name —
+ the sort keeps `kotlin-compiler-*` ahead of `kotlin-gradle-plugin`, which
+ bundles its own byte-different copies of some compiler classes (the compiler
+ jar must win, it's what the daemon loads); `fallback.txt` adds other cached
+ KGP versions, newest first, for definitions missing from the pinned set.
- `--scan-jars` — AGP jars. Only used when **not** in `--seed-only` mode: their
Kotlin class references form the seed set S0.
@@ -147,7 +167,11 @@ The list files contain one jar path per line; `#` starts a comment.
### What each generated member does
- **`<clinit>`** (classes only): calls `KotlinProxyLog.loaded(dotname)` — proves
- the stub shadowed the real class.
+ the stub shadowed the real class — and, when the class has a Kotlin companion
+ object, mirrors the real `<clinit>`'s companion init (`new Companion; dup;
+ aconst_null; invokespecial; putstatic` — Kotlin builds the companion in the
+ enclosing class's static initializer; leaving the static null makes real code
+ touching `<This>.Companion` NPE before any stub method is ever called).
- **Constructors:** `super()` (or `Enum.<init>("stub", 0)` for enums), then log.
A public no-arg ctor is synthesized if the real class lacks one, so child
stubs can `invokespecial super()`.
@@ -224,32 +248,48 @@ the jars from `jars/defs.txt` on `javac`'s classpath if you import them.
## Using the jar in a build
-The daemon is a separate JVM. Its compiler classpath comes from the Gradle
-configuration `kotlinCompilerClasspath`, which KGP seeds via hidden
-`defaultDependencies` (0 declared deps → 7 resolved jars). Adding **any**
-regular dependency suppresses those defaults, so re-declare the real compiler
-modules as regular dependencies **with the stub jar first** — the daemon then
-loads `K2JVMCompiler` from the stub:
+The daemon is a separate JVM. Since **KGP ≥ 2.3.20** it runs JVM compilation
+through the **Build Tools API (BTA) by default**, and the daemon's JVM
+classpath is the resolved **`kotlinBuildToolsApiClasspath`** configuration
+(default deps: `kotlin-build-tools-compat` + `kotlin-build-tools-impl`, whose
+transitive closure *is* the compiler — 15 jars at 2.4.21). Like any
+`defaultDependencies`-seeded configuration, adding **any** regular dependency
+suppresses the defaults, so re-declare the same modules as regular
+dependencies **with the stub jar first** — the daemon then loads
+`K2JVMCompiler` from the stub:
```kotlin
// app/build.gradle.kts
val proxyStub = file("/home/the_jk/source/kotlin_proxy/dist/kotlin-proxy-compiler-stubs.jar")
-val realCompilerJars = listOf(
- "org.jetbrains.kotlin:kotlin-compiler-embeddable:2.2.10",
- "org.jetbrains.kotlin:kotlin-stdlib:2.2.10",
- "org.jetbrains.kotlin:kotlin-script-runtime:2.2.10",
- "org.jetbrains.kotlin:kotlin-reflect:1.6.10",
- "org.jetbrains.kotlin:kotlin-daemon-embeddable:2.2.10",
- "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.0",
- "org.jetbrains:annotations:13.0",
-)
+val kotlinVersion = "2.4.21" // from libs.versions.toml in the test project
afterEvaluate {
- val cfg = configurations.getByName("kotlinCompilerClasspath")
- cfg.dependencies.add(dependencies.create(files(proxyStub)))
- realCompilerJars.forEach { cfg.dependencies.add(dependencies.create(it)) }
+ // BTA path (the default since KGP 2.3.20): the daemon classpath
+ val bta = configurations.getByName("kotlinBuildToolsApiClasspath")
+ bta.dependencies.add(dependencies.create(files(proxyStub)))
+ bta.dependencies.add(dependencies.create(
+ "org.jetbrains.kotlin:kotlin-build-tools-compat:$kotlinVersion"))
+ bta.dependencies.add(dependencies.create(
+ "org.jetbrains.kotlin:kotlin-build-tools-impl:$kotlinVersion"))
+ // legacy path (kotlin.compiler.runViaBuildToolsApi=false; deprecated,
+ // removed in KGP 2.5.0) — shadow it the same way, with the full
+ // kotlinCompilerClasspath default closure re-declared
}
```
+The legacy `kotlinCompilerClasspath` (what the daemon used before 2.3.20)
+still exists; its closure is the classic `kotlin-compiler-embeddable` +
+transitives set (`kotlin-stdlib`, `kotlin-script-runtime`,
+`kotlin-reflect:1.6.10`, `kotlin-daemon-embeddable`,
+`kotlinx-coroutines-core-jvm:1.8.0`, `org.jetbrains:annotations:13.0`) and the
+test project shadows it too.
+
+Two more pins, both in the root `build.gradle.kts` buildscript: the stub jar
+is declared **before** AGP/KGP on the buildscript classpath, and KGP is
+declared explicitly (`org.jetbrains.kotlin:kotlin-gradle-plugin:<version>`)
+because **AGP 9.x's built-in Kotlin** applies its own pinned KGP version — an
+explicit buildscript classpath dep upgrades it (AGP 9.0 release notes,
+"Runtime dependency on Kotlin Gradle plugin").
+
and in `gradle.properties`:
```properties
@@ -258,10 +298,15 @@ kotlin.compiler.execution.strategy=daemon
kotlin.daemon.useFallbackStrategy=false
```
-Expected outcome: `:app:compileDebugKotlin` **fails** (the stub returns
-defaults/null, so the real `CLICompiler` machinery NPEs after calling into the
-stub) — that's the point. The stub is the only compiler; the build goes green
-only once the stub implements its own compile.
+Expected outcome: `:app:compileDebugKotlin` **fails** (under BTA: "Internal
+compiler error" — the stub returns defaults/null, so the real compiler
+machinery fails after calling into the stub) — that's the point. The stub is
+the only compiler; the build goes green only once the stub implements its own
+compile. (A *green* build means the stub is not being loaded — that's a
+failure state, not success.) The daemon's full argument dump ("Starting
+compilation with args: …") is in `/tmp/kotlin-daemon.*.log`; stale daemons hold
+stale classpaths, so kill them (`pkill -f 'KotlinCompile[D]aemon'`) and
+`rm /tmp/kotlin-proxy-marker.txt` when re-verifying.
## Reading the log
@@ -280,11 +325,18 @@ daemon's working directory or missing `-Dkotlin.proxy.log`:
```
RMI TCP Connection(2)-127.0.0.1 | LOAD org.jetbrains.kotlin.cli.jvm.K2JVMCompiler <- file:…/dist/kotlin-proxy-compiler-stubs.jar
+RMI TCP Connection(2)-127.0.0.1 | LOAD org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion <- file:…/dist/kotlin-proxy-compiler-stubs.jar
+RMI TCP Connection(2)-127.0.0.1 | CALL org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.<init>
RMI TCP Connection(2)-127.0.0.1 | CALL org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.<init>
-RMI TCP Connection(2)-127.0.0.1 | CALL org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createArguments
-RMI TCP Connection(2)-127.0.0.1 | CALL org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createPerformanceManager arg=K2JVMCompilerArguments{destination=… classpath=… freeArgs=[…/Hello.kt] …}
```
+(Under the BTA flow the daemon drives compilation through its own
+`CompileServiceImpl` with arguments it builds itself, so the stub's
+`createArguments`/`execute` may never be *called* — the crash happens in the
+real incremental machinery after construction. Under the legacy
+`runViaBuildToolsApi=false` path you additionally see `CALL …createArguments`
+and friends, with the boundary args dumped.)
+
- `LOAD <name> <- <codesource>` — the stub class was initialized, from our jar.
- `CALL <name>.<member>` — the stub member was invoked.
- `CALL <name>.<member> arg=<dump>` — boundary argument: compiler-argument
@@ -302,8 +354,12 @@ that method returned `null` — expected diagnostic noise, not an error.
## Regenerating after changes
-- Changed the seeded class set or a stub's behavior → re-run steps 1–3.
-- Changed `KotlinProxyLog.java` → re-run steps 2–3 (or all three; step 1 is
+- Changed the seeded class set or a stub's behavior → `make` (steps 1–3).
+- Changed `KotlinProxyLog.java` → steps 2–3 (or all three; step 1 is
idempotent).
-- Changed a kotlin version → update the jars in `jars/defs.txt` (and the
- `realCompilerJars` versions in the build script).
+- Bumped the Kotlin/AGP version → update `test_project/gradle/libs.versions.toml`
+ (what the build uses) and the Makefile's `KOTLIN_VERSION` / `AGP_VERSION`
+ (what the jar lists are generated for), run the test_project once so Gradle
+ primes the cache with the new jars (even a red build), then `make` — it
+ detects the drift against `jars/.stamp` and regenerates `jars/*.txt` via
+ `tools/gen_jar_lists.py` before regenerating the stubs.