CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-compiler

The Kotlin compiler infrastructure providing JVM, JavaScript, and metadata compilation capabilities

Pending
Overview
Eval results
Files

Kotlin Compiler Infrastructure

The 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.

Package Information

  • Package Name: org.jetbrains.kotlin:kotlin-compiler
  • Package Type: maven
  • Language: Kotlin/Java
  • Installation: Add to build.gradle.kts: implementation("org.jetbrains.kotlin:kotlin-compiler:2.2.20-RC2")

Core Imports

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.TargetPlatform

External Dependencies: The Kotlin compiler depends on IntelliJ Platform components:

import com.intellij.openapi.Disposable
import com.intellij.openapi.project.Project
import com.intellij.mock.MockProject

Basic Usage

import 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")
}

Architecture

The Kotlin compiler infrastructure is organized around several key architectural components:

  • CLICompiler Base Class: Abstract foundation providing common compilation workflows and argument processing
  • Platform-Specific Compilers: Concrete implementations for JVM, JavaScript, and metadata compilation targets
  • Configuration System: Type-safe configuration management through strongly-typed keys and services
  • Plugin Architecture: Modern K2-compatible plugin system with CompilerPluginRegistrar interface
  • Pipeline System: Phase-based compilation pipeline with configurable execution stages
  • Environment Management: Comprehensive project environment setup and dependency resolution

Capabilities

JVM Compilation

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
}

JVM Compilation

JavaScript and WebAssembly Compilation

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

Plugin Development

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
    )
}

Plugin Development

Configuration Management

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>
}

Configuration Management

Environment and Project Setup

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)
}

Environment Management

Message Handling and Diagnostics

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
}

Message Handling

Exit Codes

enum class ExitCode(val code: Int) {
    OK(0),
    COMPILATION_ERROR(1),
    INTERNAL_ERROR(2),
    SCRIPT_EXECUTION_ERROR(3),
    OOM_ERROR(137)
}

Core Types

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?
}

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-compiler
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.jetbrains.kotlin/kotlin-compiler@2.2.x