Welcome! This project provides a toolchain for compiling Rust code into Java bytecode, allowing it to run on the JVM.
The toolchain follows these steps:
-
Parsing & MIR Generation
- Rust code is first parsed and converted into MIR (Mid-level Intermediate Representation) using
rustc.
- Rust code is first parsed and converted into MIR (Mid-level Intermediate Representation) using
-
MIR to Java Bytecode
- The MIR for each crate is translated into a Java Classfile (containing JVM bytecode) via
rustc_codegen_jvm, which is the core of this repository. - Source code for this component is located in the
srcfolder.
- The MIR for each crate is translated into a Java Classfile (containing JVM bytecode) via
-
Linking &
.jarGeneration- Java Classfiles for all crates used in a library or executable are linked into a single
.jarfile, making it ready to run on the JVM. - This step is handled by
java-linker-rs, a custom-built linker in this repository (found in thejava-linkerfolder).
- Java Classfiles for all crates used in a library or executable are linked into a single
- ✅ Compiling a minimal
no_std&no_coreRust program with an emptymainfunction. - ✅ Simple mathematical operations on
i32s: addition, subtraction, and returning()or ani32.
🚧 Full support for the core crate is in progress!
- Ensure you're on the latest nightly build of Rust.
- Install necessary Rust components:
rustup component add rust-src rustc-dev llvm-tools-preview
- Run the setup script:
chmod +x setup.sh && ./setup.sh
To use the toolchain for compiling your Rust project:
```
- Update the Target JSON
- Modify
jvm-unknown-unknown.json, replacing the../../..(relative path to the project root from a test folder) part of thelinkeranddefault-codegen-backendlines to the absolute or relative path of where you cloned this repo.
- Modify
- Build Your Project
cargo build --target /path/to/rustc_codegen_jvm/jvm-unknown-unknown.json
- Find & Run the Generated
.jar- The compiled
.jarfile will be in:target/jvm-unknown-unknown/debug/[cratename].jar - If it's a binary (not a library), run it using:
java -jar target/jvm-unknown-unknown/debug/[cratename].jar
- The compiled
- If you modified the target JSON file, revert the changes before running tests.
- Execute the test script:
python3 Tester.py
- Look for a success message 🎉