Building from Source¶
Complete guide to building the Onion compiler from source.
Prerequisites¶
Required Software¶
- Java Development Kit (JDK) 17 or later
Check version:
Install:
- macOS: brew install openjdk@17
- Ubuntu/Debian: sudo apt install openjdk-17-jdk
- Windows: Download from Adoptium
- SBT (Scala Build Tool)
Check version:
Install:
- macOS: brew install sbt
- Ubuntu/Debian: Follow official guide
- Windows: Download MSI installer
- Git
Cloning the Repository¶
Building¶
Basic Compilation¶
Compile the entire project:
This will: 1. Download dependencies (first time only) 2. Generate parser from JavaCC grammar 3. Compile Scala and Java source files
Expected output:
[info] compiling ... Scala sources and ... Java sources to target/scala-3.3.7/classes ...
[success] Total time: 45 s
Clean Build¶
Remove all generated files and rebuild:
Incremental Compilation¶
SBT automatically performs incremental compilation. Only changed files are recompiled:
Testing¶
Run All Tests¶
Run Specific Test Suite¶
Run Tests Matching Pattern¶
Creating Distributions¶
Standalone JAR¶
Create a fat JAR with all dependencies:
Output: target/scala-3.3.7/onion.jar
The JAR's default main class is the compiler (onionc), so java -jar compiles
a source file to .class files:
To run a script instead, invoke the ScriptRunner main class (this is what the
onion launcher does):
Distribution Package¶
Create a complete distribution ZIP:
Output: target/onion-dist.zip
Contents:
onion-dist/
├── onion.jar # Main compiler JAR
├── lib/ # Dependencies
│ ├── asm-9.8.jar
│ ├── scala-library-3.3.7.jar
│ └── ...
├── bin/ # Executables
│ ├── onionc
│ ├── onion
│ └── onion-repl
├── run/ # Example programs
│ ├── Hello.on
│ ├── Array.on
│ └── ...
└── README.md
Extract and use:
Development Build¶
Continuous Compilation¶
Watch for changes and recompile automatically:
Run from SBT¶
Execute without creating a JAR:
Run script runner:
Run the promoted REPL:
Run the benchmark suite:
Emit compile profiles:
Interactive SBT¶
Start SBT shell:
Then run commands:
Parser Development¶
Modifying the Grammar¶
-
Edit the JavaCC grammar:
-
Regenerate parser:
The parser is automatically regenerated when:
- Grammar file is newer than generated parser
- Running sbt clean
Parser Location¶
Generated parser:
target/scala-3.3.7/src_managed/main/java/onion/compiler/parser/
├── JJOnionParser.java
├── Token.java
├── TokenManager.java
└── ...
IDE Setup¶
IntelliJ IDEA¶
- Install Scala plugin
- Open project:
File > Open > select build.sbt - Wait for indexing to complete
- Build:
Build > Build Project
Run configuration:
- Main class: onion.tools.CompilerFrontend
- Program arguments: path/to/source.on
- Working directory: $PROJECT_DIR$
Visual Studio Code¶
- Install Metals extension
- Open project folder
- Wait for import to complete
- Build via command palette:
Metals: Compile workspace
Troubleshooting¶
Parser Generation Fails¶
Out of Memory¶
Increase SBT memory:
Or edit .sbtopts:
Dependency Issues¶
Clear Ivy cache:
Compilation Errors¶
Check Scala and Java versions:
Ensure they match requirements: - Scala 3.3.7 - Java 17+
Build Configuration¶
build.sbt¶
Key settings:
// Version is derived from git tags via sbt-dynver
scalaVersion := "3.3.7"
name := "onion"
organization := "org.onion_lang"
// Dependencies
libraryDependencies ++= Seq(
"org.ow2.asm" % "asm" % "9.8",
"net.java.dev.javacc" % "javacc" % "5.0",
"org.scalatest" %% "scalatest" % "3.2.19" % "test"
)
// Main class
mainClass := Some("onion.tools.CompilerFrontend")
Compiler Options¶
Scala compiler options:
Java compiler options:
Performance Tips¶
Faster Builds¶
- Use incremental compilation (default)
- Don't clean unless necessary
- Increase JVM memory
- Use SBT shell for multiple commands
Parallel Compilation¶
SBT compiles in parallel by default. Adjust thread count:
Platform-Specific Notes¶
macOS¶
May need to set JAVA_HOME:
Linux¶
Ensure sufficient memory:
Windows¶
Use PowerShell or Git Bash. Paths use backslashes:
sbt assembly
# compile a source file:
java -jar target\scala-3.3.7\onion.jar Hello.on
# or run a script:
java -cp target\scala-3.3.7\onion.jar onion.tools.ScriptRunner Hello.on
Next Steps¶
- Development Guide - Contributing to Onion
- Compiler Architecture - Internals
- Running Tests - Testing guide