The Kotlin compiler infrastructure providing JVM, JavaScript, and metadata compilation capabilities
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-compiler@2.2.0The Kotlin compiler infrastructure provides comprehensive compilation services for Kotlin source code targeting JVM, JavaScript, and metadata-only outputs. It offers both command-line interfaces and programmatic APIs for embedding Kotlin compilation into build tools, IDEs, and other development infrastructure.
implementation("org.jetbrains.kotlin:kotlin-compiler:2.2.20-RC2")import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import org.jetbrains.kotlin.cli.metadata.KotlinMetadataCompiler
import org.jetbrains.kotlin.cli.common.arguments.*
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.platform.TargetPlatformExternal Dependencies: The Kotlin compiler depends on IntelliJ Platform components:
import com.intellij.openapi.Disposable
import com.intellij.openapi.project.Project
import com.intellij.mock.MockProjectimport org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.config.Services
// Create compiler instance
val compiler = K2JVMCompiler()
// Configure compilation arguments
val args = K2JVMCompilerArguments().apply {
freeArgs = listOf("src/main/kotlin/Main.kt")
destination = "build/classes"
classpath = "lib/kotlin-stdlib.jar"
noStdlib = false
}
// Execute compilation
val exitCode = compiler.exec(System.err, Services.EMPTY, args)
if (exitCode == ExitCode.OK) {
println("Compilation successful")
}The Kotlin compiler infrastructure is organized around several key architectural components:
Complete JVM bytecode compilation with support for all Kotlin language features, Java interop, and advanced optimization passes.
class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
override val platform: TargetPlatform // JvmPlatforms.defaultJvmPlatform
override fun createArguments(): K2JVMCompilerArguments
override fun doExecute(
arguments: K2JVMCompilerArguments,
configuration: CompilerConfiguration,
rootDisposable: Disposable,
paths: KotlinPaths?
): ExitCode
override fun doExecutePhased(
arguments: K2JVMCompilerArguments,
services: Services,
messageCollector: MessageCollector
): ExitCode
}
class K2JVMCompilerArguments : CommonCompilerArguments() {
var destination: String?
var classpath: String?
var moduleName: String?
var jdkHome: String?
var jvmTarget: String?
var includeRuntime: Boolean
}JavaScript ES5/ES6 and WebAssembly compilation with module system support and source map generation.
class K2JSCompiler : CLICompiler<K2JSCompilerArguments>() {
override val platform: TargetPlatform // JsPlatforms.defaultJsPlatform
override fun createArguments(): K2JSCompilerArguments
override fun doExecute(
arguments: K2JSCompilerArguments,
configuration: CompilerConfiguration,
rootDisposable: Disposable,
paths: KotlinPaths?
): ExitCode
override fun doExecutePhased(
arguments: K2JSCompilerArguments,
services: Services,
messageCollector: MessageCollector
): ExitCode?
}
class K2JSCompilerArguments : K2WasmCompilerArguments() {
var outputFile: String?
var moduleKind: String?
var sourceMap: Boolean
var sourceMapPrefix: String?
var libraries: String?
}JavaScript and WebAssembly Compilation
Modern K2 plugin architecture enabling custom compilation phases, code generation, and IDE integration.
@ExperimentalCompilerApi
abstract class CompilerPluginRegistrar {
abstract val pluginId: String
abstract val supportsK2: Boolean
abstract fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration)
class ExtensionStorage {
fun <T : Any> ProjectExtensionDescriptor<T>.registerExtension(extension: T)
fun registerDisposable(disposable: PluginDisposable)
}
}
@ExperimentalCompilerApi
interface CommandLineProcessor {
val pluginId: String
val pluginOptions: Collection<AbstractCliOption>
fun processOption(
option: AbstractCliOption,
value: String,
configuration: CompilerConfiguration
)
}Type-safe configuration system with hierarchical key organization and service dependency injection.
class CompilerConfiguration {
companion object {
val EMPTY: CompilerConfiguration
}
fun <T> put(key: CompilerConfigurationKey<T>, value: T)
fun <T> putIfNotNull(key: CompilerConfigurationKey<T>, value: T?)
fun <T> get(key: CompilerConfigurationKey<T>): T?
fun <T> getNotNull(key: CompilerConfigurationKey<T>): T
fun <T> getList(key: CompilerConfigurationKey<List<T>>): List<T>
fun copy(): CompilerConfiguration
}
object CommonConfigurationKeys {
val MODULE_NAME: CompilerConfigurationKey<String>
val LANGUAGE_VERSION_SETTINGS: CompilerConfigurationKey<LanguageVersionSettings>
val MESSAGE_COLLECTOR_KEY: CompilerConfigurationKey<MessageCollector>
}Comprehensive environment management for compilation projects including dependency resolution and file system setup.
class KotlinCoreEnvironment private constructor(
val projectEnvironment: ProjectEnvironment,
val configuration: CompilerConfiguration,
configFiles: EnvironmentConfigFiles
) {
companion object {
fun createForProduction(
parentDisposable: Disposable,
configuration: CompilerConfiguration,
configFiles: EnvironmentConfigFiles
): KotlinCoreEnvironment
}
val project: Project
class ProjectEnvironment(
disposable: Disposable,
applicationEnvironment: KotlinCoreApplicationEnvironment,
configuration: CompilerConfiguration
) : KotlinCoreProjectEnvironment(disposable, applicationEnvironment)
}Comprehensive diagnostic and message reporting system with multiple output formats and severity levels.
interface MessageCollector {
fun report(
severity: CompilerMessageSeverity,
message: String,
location: CompilerMessageSourceLocation? = null
)
}
enum class CompilerMessageSeverity {
EXCEPTION, ERROR, STRONG_WARNING, FIXED_WARNING, WARNING, INFO, LOGGING, OUTPUT
}enum class ExitCode(val code: Int) {
OK(0),
COMPILATION_ERROR(1),
INTERNAL_ERROR(2),
SCRIPT_EXECUTION_ERROR(3),
OOM_ERROR(137)
}abstract class CLICompiler<A : CommonCompilerArguments> {
abstract val platform: TargetPlatform
abstract fun createArguments(): A
abstract fun doExecute(
arguments: A,
configuration: CompilerConfiguration,
rootDisposable: Disposable,
paths: KotlinPaths?
): ExitCode
open fun doExecutePhased(
arguments: A,
services: Services,
messageCollector: MessageCollector
): ExitCode?
fun exec(errStream: PrintStream, services: Services, vararg args: String): ExitCode
}
open class CommonCompilerArguments : CommonToolArguments() {
var languageVersion: String?
var apiVersion: String?
var suppressWarnings: Boolean
var verbose: Boolean
var freeArgs: List<String>
}
abstract class K2WasmCompilerArguments : CommonCompilerArguments() {
// Base class for WebAssembly-related compilation arguments
}
class Services {
companion object {
val EMPTY: Services
}
fun <T> get(request: Class<T>): T?
}