Compiling and Running kotlinc from source

I am interested in doing some minor experimenting with the Kotlin compiler. I would like to try compiling a very basic kt file using a version of kotlinc that I can edit.

I followed the instructions in the README (GitHub - JetBrains/kotlin: The Kotlin Programming Language.) and was able to get a local copy to compile. But then I’m stuck. The kotlinc script in compiler/cli/bin requires jar files to be in specific named locations, so it isn’t a convenient way to iterate on minor changes to the compiler.

What’s the best way to compile a local version of the kotlin compiler, and then run it on a basic kt file?

There’s a Gradle task in our project called dist which builds compiler and libraries and puts them into dist/kotlinc/lib, exactly where wrapper scripts, which are also copied to dist/kotlinc/bin, expect them to be. So if you change some code in the project and then run:

./gradlew dist

(or run the corresponding task from the Gradle tool window in IntelliJ), the project will be incrementally compiled (therefore it won’t take a lot of time), and the updated Kotlin compiler will be placed in the dist directory, which you can then invoke with

dist/kotlinc/bin/kotlinc source.kt -d output.jar

You can also define a shell alias kotlinc to point to that executable and simplify the command line further.

1 Like