Skip to content

Commit 9b562c0

Browse files
committed
refactor(post processing): Use r8 CLI to both generate stack map frames and optimise - reduces build time by 1-2s.
Also no longer includes the ENTIRE kotlin stdlib in every produced jar file, reducing size by 98%-99%, and removes optimise2 and stackmapadder as both tasks are now handled by the r8 cli. Adds needed vendoring steps to Makefile. Needed refactor of java-linker to support this.
1 parent d1ecb5b commit 9b562c0

File tree

13 files changed

+444
-976
lines changed

13 files changed

+444
-976
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818

1919
# Generated by GenerateFiles.py
2020
/config.toml
21-
jvm-unknown-unknown.json
21+
jvm-unknown-unknown.json
22+
23+
# Vendored files
24+
**/vendor/*

Makefile

+15-41
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# === Phony Targets ===
22
.PHONY: all help clean rust-components rust clean-rust java-linker clean-java-linker \
3-
shim-metadata-gen clean-shim-metadata-gen stackmapadder clean-stackmapadder \
4-
library clean-library gen-files clean-gen-files ci optimise2 clean-optimise2
3+
shim-metadata-gen clean-shim-metadata-gen vendor-r8 clean-vendor-r8 \
4+
library clean-library gen-files clean-gen-files ci
55

66
# === Terminal Colors ===
77
GREEN := \033[1;32m
@@ -11,18 +11,16 @@ RESET := \033[0m
1111
# === Directory Variables ===
1212
JAVA_LINKER_DIR := java-linker
1313
SHIM_METADATA_GEN_DIR := shim-metadata-gen
14-
STACKMAPADDER_DIR := stackmapadder
15-
OPTIMISE2_DIR := optimise2
1614
LIBRARY_DIR := library
1715
LIBRARY_JAR := $(LIBRARY_DIR)/build/libs/library-0.1.0.jar
1816
RUST_SOURCES := $(shell find $(SHIM_METADATA_GEN_DIR)/src -type f -name '*.rs')
1917

2018
# === Default Target ===
2119
ifeq ($(IS_CI),1)
22-
all: rust java-linker stackmapadder optimise2
20+
all: rust java-linker vendor-r8
2321
@echo "$(GREEN)✨ Build complete in CI mode! ✨$(RESET)"
2422
else
25-
all: rust gen-files java-linker stackmapadder optimise2
23+
all: rust gen-files java-linker vendor-r8
2624
@echo "$(GREEN)✨ Build complete! ✨$(RESET)"
2725
endif
2826

@@ -41,7 +39,6 @@ help:
4139
@echo " make rust-components - Install needed Rust components"
4240
@echo " make rust - Build the Rust root project"
4341
@echo " make java-linker - Build the Java Linker subproject"
44-
@echo " make stackmapadder - Build the ASM processor"
4542
@echo " make library - Build the standard library shim"
4643
@echo " make gen-files - Generate necessary files from templates"
4744
@echo " make clean-* - Clean individual components"
@@ -96,39 +93,6 @@ clean-shim-metadata-gen-json-files:
9693
rm -f $(SHIM_METADATA_GEN_DIR)/*.json; \
9794
fi
9895

99-
# === ASM Processors (Gradle) ===
100-
stackmapadder:
101-
@echo "$(CYAN)⚙️ Building Stack map adder...$(RESET)"
102-
ifeq ($(IS_CI),1)
103-
cd $(STACKMAPADDER_DIR) && gradle --no-daemon shadowJar
104-
else
105-
cd $(STACKMAPADDER_DIR) && gradle shadowJar
106-
endif
107-
108-
clean-stackmapadder:
109-
@echo "$(CYAN)🧹 Cleaning ASM processor...$(RESET)"
110-
ifeq ($(IS_CI),1)
111-
cd $(STACKMAPADDER_DIR) && gradle --no-daemon clean
112-
else
113-
cd $(STACKMAPADDER_DIR) && gradle clean
114-
endif
115-
116-
optimise2:
117-
@echo "$(CYAN)⚙️ Building optimise2...$(RESET)"
118-
ifeq ($(IS_CI),1)
119-
cd $(OPTIMISE2_DIR) && gradle --no-daemon shadowJar
120-
else
121-
cd $(OPTIMISE2_DIR) && gradle shadowJar
122-
endif
123-
124-
clean-optimise2:
125-
@echo "$(CYAN)🧹 Cleaning optimise2...$(RESET)
126-
ifeq ($(IS_CI),1)
127-
cd $(OPTIMISE2_DIR) && gradle --no-daemon clean
128-
else
129-
cd $(OPTIMISE2_DIR) && gradle clean
130-
endif
131-
13296
# === Standard Library Shim (Gradle) ===
13397
library: $(LIBRARY_JAR)
13498

@@ -158,6 +122,16 @@ clean-gen-files:
158122
@echo "$(CYAN)🧹 Cleaning template generated files...$(RESET)"
159123
rm -f jvm-unknown-unknown.json config.toml
160124

125+
# === Vendoring of R8 ===
126+
vendor-r8:
127+
@echo "$(CYAN)📦 Vendoring R8...$(RESET)"
128+
mkdir -p ./vendor && curl -L -o ./vendor/r8.jar https://maven.google.com/com/android/tools/r8/8.9.35/r8-8.9.35.jar
129+
@echo "$(CYAN)📦 R8 vendored!$(RESET)"
130+
131+
clean-vendor-r8:
132+
@echo "$(CYAN)🧹 Cleaning vendored R8...$(RESET)"
133+
rm -rf ./vendor/r8.jar
134+
161135
# === Clean All ===
162-
clean: clean-rust clean-java-linker clean-stackmapadder clean-library clean-shim-metadata-gen clean-gen-files
136+
clean: clean-rust clean-java-linker clean-library clean-shim-metadata-gen clean-gen-files clean-vendor-r8
163137
@echo "$(GREEN)🧼 All clean!$(RESET)"

Readme.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ All examples live in `tests/binary` and are compiled to JVM bytecode & run/teste
7373
4. **OOMIR → JVM Classfile**
7474
Translate to `.class` files using `ristretto_classfile`.
7575
_(see `src/lower2.rs`)_
76-
5. **Post‑Process Stack Map Frames**
77-
Kotlin `stackmapadder` (built with ASM library) adds verification frames and applys some further optimisations to the class files.
78-
6. **JAR Optimisation**
79-
`optimise2` (based on `r8`) optimises the final jar file.
76+
5. **R8 pass**
77+
`r8` adds stack map frames (neeeded to run on JVM 8+) and applies some further optimisations.
8078
6. **Link & Package**
8179
`java-linker` bundles `.class` files into a runnable `.jar` with `META-INF/MANIFEST.MF`.
8280

@@ -85,8 +83,7 @@ All examples live in `tests/binary` and are compiled to JVM bytecode & run/teste
8583
## 🛠 Prerequisites
8684

8785
- **Rust Nightly** (`rustup default nightly`)
88-
- **JDK 8+** (`java` in PATH)
89-
- **Gradle** (`gradle` in PATH)
86+
- **JDK 8+** (`java` in PATH, and the `JAVA_HOME` environment variable set)
9087
- **Python 3** (`python3` in PATH)
9188

9289
---
@@ -106,8 +103,6 @@ This will compile:
106103

107104
- `rustc_codegen_jvm` backend library
108105
- `java-linker`
109-
- `stackmapadder`
110-
- `optimise2`
111106
- Kotlin shim for `core` (once core support is reached, this will no longer be needed)
112107
- Generate `config.toml` & `jvm-unknown-unknown.json`
113108

@@ -167,11 +162,10 @@ Look for `✅ All tests passed!` or inspect `.generated` files on failure.
167162
│ ├── lower2.rs # OOMIR → JVM bytecode
168163
│ └── oomir.rs # OOMIR definitions
169164
├── java-linker/ # Bundles .class files into .jar
170-
├── stackmapadder/ # Kotlin StackMapFrame generator
171-
├── optimise2/ # Jar optimiser
172165
├── tests/binary/ # Integration tests
173166
├── library/ # Kotlin shim for Rust core library
174167
├── shim-metadata-gen/ # Generates core.json metadata
168+
├── proguard/ # .pro rules used for r8
175169
├── Makefile # build & gen-files
176170
├── config.toml.template
177171
├── jvm-unknown-unknown.json.template

Tester.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def process_test(test_dir: str, release_mode: bool):
6464
os.path.join(test_dir, "target", "jvm-unknown-unknown", target_dir, f"{test_name}.jar"))
6565

6666
jar_path = os.path.join(test_dir, "target", "jvm-unknown-unknown", target_dir, f"{test_name}.jar")
67-
proc = run_command(["java", "-jar", jar_path])
67+
proc = run_command(["java", "-cp", f"library/build/distributions/library-0.1.0/lib/library-0.1.0.jar:library/build/distributions/library-0.1.0/lib/kotlin-stdlib-2.1.20.jar:{jar_path}", test_name])
6868

6969
expected_returncode_file = os.path.join(test_dir, "java-returncode.expected")
7070
if os.path.exists(expected_returncode_file):

config.toml.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
rustflags = [
33
"-Z", "codegen-backend=../../../target/debug/librustc_codegen_jvm.dylib",
44
"-C", "linker=../../../java-linker/target/debug/java-linker",
5-
"-C", "link-args=../../../library/build/distributions/library-0.1.0/lib/library-0.1.0.jar ../../../library/build/distributions/library-0.1.0/lib/kotlin-stdlib-2.1.20.jar --stackmapadder ../../../stackmapadder/build/libs/stackmapadder-1.0-SNAPSHOT-all.jar --known-good kotlin-stdlib --optimizer ../../../optimise2/build/libs/optimise2-1.0-SNAPSHOT-all.jar --proguard-config ../../../proguard/default.pro"
5+
"-C", "link-args=../../../library/build/distributions/library-0.1.0/lib/library-0.1.0.jar ../../../library/build/distributions/library-0.1.0/lib/kotlin-stdlib-2.1.20.jar ../../../library/build/distributions/library-0.1.0/lib/annotations-13.0.jar --r8-jar ../../../vendor/r8.jar --proguard-config ../../../proguard/default.pro"
66
]
77

88
# Throwing a JVM exception will unwind and give a stack trace, no need for rust to handle unwinding.

0 commit comments

Comments
 (0)