diff options
Diffstat (limited to 'CLAUDE.md')
| -rw-r--r-- | CLAUDE.md | 137 |
1 files changed, 82 insertions, 55 deletions
@@ -21,43 +21,48 @@ table). Read it before changing generation behavior. - `tools/gen_class.py` — self-contained Python stub generator (the core). Emits `.class` files **directly** (not Java source) by copying the real class's erased structure verbatim. +- `tools/gen_jar_lists.py` — regenerates the `jars/*.txt` lists from the local + Gradle cache (see Building). +- `docs/opcodes.html` — JVM bytecode opcode reference. **Consult it whenever + hand-rolling bytecode in `gen_class.py`** — opcodes like `putstatic` (0xb3) + and stack peaks are easy to get wrong, and the JVM only complains with an + inscrutable `VerifyError`. - `stubs/src/kotlin/proxy/KotlinProxyLog.java` — the logger every generated stub calls into. - `stubs/src/kotlin/proxy/KotlinProxyOverrides.java` — per-method custom return values: the extension point for actually *implementing* stub methods. - `stubs/src/kotlin/proxy/ProxyPerformanceManager.java` — override support class (per-compiler-instance perf manager) returned by the overrides. -- `jars/defs.txt`, `jars/scan.txt`, `jars/fallback.txt` — jar list files feeding - the generator (one jar path per line, `#` = comment). +- `jars/defs.txt`, `jars/scan.txt`, `jars/fallback.txt`, `jars/.stamp` — + auto-generated jar list files feeding the generator (one jar path per line, + `#` = comment); `.stamp` records the versions the lists were generated for. - `dist/kotlin-proxy-compiler-stubs.jar` — the build artifact (gitignored). -- `test_project/` — a minimal AGP app (AGP 9.4.1, Gradle 9.8.1, single file - `Hello.kt`) that consumes the jar to prove the stub runs in the daemon. +- `test_project/` — a minimal AGP app (AGP 9.4.1, Gradle 9.8.1, Kotlin 2.4.21, + single file `Hello.kt`) that consumes the jar to prove the stub runs in the + daemon. -## Building (3 steps, from repo root; JDK 11+ and Python 3) +## Building (from repo root; JDK 11+ and Python 3) ```bash -# 1. Generate the stub classes (WIPES --out first — put nothing you need there) -python3 tools/gen_class.py \ - --scan-jars jars/scan.txt --def-jars jars/defs.txt --fallback-jars jars/fallback.txt \ - --seed-only --no-closure \ - --seed 'org/jetbrains/kotlin/cli/jvm/K2JVMCompiler,org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion,org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt' \ - --out stubs/classes_min +make +``` -# 2. Compile the real classes into the SAME tree (MUST run after step 1, which -# wiped it). The -cp lets KotlinProxyOverrides import real compiler types. -javac -nowarn -proc:none -d stubs/classes_min \ - -cp "$(grep -v '^[[:space:]]*#' jars/defs.txt | tr '\n' ':')" \ - stubs/src/kotlin/proxy/KotlinProxyLog.java \ - stubs/src/kotlin/proxy/KotlinProxyOverrides.java \ - stubs/src/kotlin/proxy/ProxyPerformanceManager.java +The Makefile runs the 3 real steps in order: -# 3. Package -jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min . -``` +1. Regenerate `jars/*.txt` via `tools/gen_jar_lists.py` when the pinned + versions (Makefile vars `KOTLIN_VERSION` / `AGP_VERSION`, kept in sync with + `test_project/gradle/libs.versions.toml`) drift from `jars/.stamp`. The + Gradle cache must already hold those versions — run the test_project once + first (even a red build resolves everything). +2. Generate the stub classes (`tools/gen_class.py`, **wipes** `stubs/classes_min` + first — put nothing you need there): + `--seed-only --no-closure --seed '…K2JVMCompiler,…K2JVMCompiler$Companion,…K2JVMCompilerKt'`. +3. Compile the `kotlin.proxy.*` classes into the same tree (the `-cp` lets + `KotlinProxyOverrides` import real compiler types), then package the jar. -The result is 6 class files + manifest: the three seeded stubs plus -`kotlin/proxy/KotlinProxyLog`, `kotlin/proxy/KotlinProxyOverrides`, and -`kotlin/proxy/ProxyPerformanceManager`. The +The result is 7 class files + manifest: the three seeded stubs plus +`kotlin/proxy/KotlinProxyLog`, `kotlin/proxy/KotlinProxyOverrides` +(+ inner `$Compiler`), and `kotlin/proxy/ProxyPerformanceManager`. The seed trio are **the only** classes stubbed (the "trio" mode); their superclasses/interfaces (`CLICompiler`, `K2JVMCompilerArguments`, …) are *not* stubbed and resolve from the real jars on the daemon classpath. @@ -70,19 +75,31 @@ The "test" is the test project. From `test_project/`: ./gradlew :app:compileDebugKotlin ``` -Expected outcome: **red** (the stub returns defaults, so the real `CLICompiler` -machinery NPEs after calling into the stub). That is the intended state. To -confirm the stub actually ran *inside the out-of-process daemon*, read the marker -file `/tmp/kotlin-proxy-marker.txt` — a daemon run shows thread names -`RMI TCP Connection(N)` and `LOAD …/dist/kotlin-proxy-compiler-stubs.jar` lines. -An `OVERRIDE <owner>.<member> inst=0x… -> <dump>` line shows a +Expected outcome: **red** (`compileDebugKotlin FAILED`, "Internal compiler +error" — the stub returns defaults, so the real compiler machinery fails +after calling into the stub). That is the intended state. To confirm the stub +actually ran *inside the out-of-process daemon*, read the marker file +`/tmp/kotlin-proxy-marker.txt` — a daemon run shows thread names +`RMI TCP Connection(N)` and `LOAD …/dist/kotlin-proxy-compiler-stubs.jar` +lines (the trio's `<clinit>`s also `LOAD` `…$Companion`). An +`OVERRIDE <owner>.<member> inst=0x… -> <dump>` line shows a `KotlinProxyOverrides.invoke` implementation supplied the return value (`inst` is the receiver instance the method was called on — omitted for -statics). -The marker path is fixed regardless of the daemon's working dir or missing -`-Dkotlin.proxy.log`, so it is the reliable proof. +statics). The marker path is fixed regardless of the daemon's working dir, +so it is the reliable proof. The daemon's full argument dump ("Starting +compilation with args: …") lands in `/tmp/kotlin-daemon.*.log`. + +Sanity-check generated bytecode with +`javap -c stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class`. +Verify every hand-rolled opcode number and the declared `max_stack` against +`docs/opcodes.html` — the JVM only complains with an inscrutable +`VerifyError`. A small `java -cp dist/…jar:<defs jars>` main that does +`Class.forName` on the trio and constructs it catches verification/linkage +failures in seconds, without a full Gradle build. -Sanity-check generated bytecode with `javap -c stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class`. +Stale daemons hold stale classpaths: `pkill -f 'KotlinCompile[D]aemon'` +(bracket trick so the pattern doesn't match your own shell) before +re-verifying, and `rm /tmp/kotlin-proxy-marker.txt` for a fresh marker. ## Architecture notes (why it works) @@ -94,8 +111,12 @@ Sanity-check generated bytecode with `javap -c stubs/classes_min/org/jetbrains/k the `Signature` attribute entirely (no generics, no clashes). The JVM links by name + erased descriptor, both copied verbatim, so the stub is ABI-identical. - **Straight-line bodies only:** method bodies have no branches, so no - `StackMapTable` frames are needed. `<clinit>` calls `KotlinProxyLog.loaded`; - constructors call `super()` (enums call `Enum.<init>("stub",0)`); concrete + `StackMapTable` frames are needed. `<clinit>` calls `KotlinProxyLog.loaded` + and — when the class has a Kotlin companion object — mirrors the real + `<clinit>`'s `new Companion; dup; aconst_null; invokespecial; putstatic` + (leaving the static null makes real code touching `<This>.Companion` NPE + before any stub method is ever called). Constructors call `super()` (enums + call `Enum.<init>("stub",0)`); concrete methods log and then return a default — but reference-class returns (except `String`) build an `Object[]` of the call's arguments (primitives boxed; `null` when param-less) and ask `KotlinProxyLog.returnValue`, which @@ -108,31 +129,37 @@ Sanity-check generated bytecode with `javap -c stubs/classes_min/org/jetbrains/k yields a real `K2JVMCompilerArguments`). Param-less erasure bridges forward to their concrete sibling instead of logging+null (the daemon dispatches through bridges). -- **Classpath shadowing (the subtle part):** KGP seeds the daemon classpath from - the `kotlinCompilerClasspath` configuration via hidden `defaultDependencies`. - Adding *any* regular dependency to that configuration suppresses those - defaults. So `test_project/app/build.gradle.kts` re-declares the real compiler - modules as regular deps **with the stub jar first**, making the daemon load - `K2JVMCompiler` from the stub. The root `build.gradle.kts` also puts the stub - first on the *buildscript* classpath. Both are intentional. +- **Classpath shadowing (the subtle part):** KGP ≥ 2.3.20 runs JVM compilation + through the **Build Tools API** 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). Like any `defaultDependencies`-seeded + configuration, adding *any* regular dependency suppresses the defaults — so + `test_project/app/build.gradle.kts` re-declares the stub jar **first** plus + those two modules as regular deps, and the daemon loads `K2JVMCompiler` + from the stub. The older `kotlinCompilerClasspath` (legacy + `runViaBuildToolsApi=false` path, deprecated, removed in KGP 2.5) is shadowed + the same way for completeness. The root `build.gradle.kts` also puts the stub + first on the *buildscript* classpath, and pins KGP explicitly — AGP 9.x's + built-in Kotlin would otherwise apply its own pinned KGP version (AGP 9.0 + release notes, "Runtime dependency on Kotlin Gradle plugin"). + All intentional. ## Gotchas -- **Machine-specific absolute paths.** `jars/defs.txt`, `jars/scan.txt`, - `jars/fallback.txt` point into this machine's Gradle cache - (`/home/the_jk/.gradle/caches/…`), and `test_project/app/build.gradle.kts:51` - hardcodes `/home/the_jk/source/kotlin_proxy/dist/…`. On another machine or a - fresh checkout you must regenerate the jar lists and fix that path, or the - build will find no jars / no stub. -- **Version coupling.** The Kotlin version is pinned in three places and must - all match: `jars/defs.txt` (the 2.2.10 set), `jars/fallback.txt` (newer KGP - variants, def-lookup only), and the `realCompilerJars` list in - `test_project/app/build.gradle.kts`. Bumping the version means updating all - three (and re-seeding the daemon classpath list). +- **Machine-specific absolute paths.** The `jars/*.txt` lists point into this + machine's Gradle cache — regenerate them with `tools/gen_jar_lists.py` + (or `make jars`) on another machine or fresh checkout, and fix the hardcoded + `/home/the_jk/source/kotlin_proxy/dist/…` in `test_project/app/build.gradle.kts`. +- **Version coupling.** The Kotlin/AGP versions live in + `test_project/gradle/libs.versions.toml` (what the build uses) and + `KOTLIN_VERSION` / `AGP_VERSION` in the Makefile (what the jar lists are + generated for) — keep them in sync. The lists themselves are generated, and + `jars/.stamp` lets the Makefile detect drift and regenerate. - **`--seed-only` + `--no-closure`** produces exactly the seed set (the daemon jar). The default mode is a full transitive closure from the AGP scan jars + KGP plugin entry points — a much larger jar; only use it when you actually want to stub a broader surface. - **Build artifacts are gitignored:** `dist/` (whole dir) and `stubs/classes_min/` are not tracked. If you need the jar and it's missing, - run the 3 build steps above. + run `make`. |
