Client library for communicating with the Kotlin compilation daemon, enabling remote compilation services and incremental compilation workflows.
—
Compilation execution through the Kotlin daemon with support for different target platforms, compiler modes, and incremental compilation workflows. Provides comprehensive compilation control and result handling.
Performs compilation through the daemon with full control over compilation parameters.
/**
* Compile Kotlin source code through the daemon
* @param compilerService The daemon service to use
* @param sessionId Session ID (use CompileService.NO_SESSION for no session)
* @param targetPlatform Target platform (JVM, JS, Native, etc.)
* @param args Compiler arguments including source files and options
* @param messageCollector Handles compiler messages and diagnostics
* @param outputsCollector Optional callback for tracking output files
* @param compilerMode Compilation mode (incremental, non-incremental)
* @param reportSeverity Minimum severity level for reporting
* @param port Port for service communication
* @param profiler Profiling interface for performance measurement
* @return Compilation exit code (0 = success)
*/
fun compile(
compilerService: CompileService,
sessionId: Int,
targetPlatform: CompileService.TargetPlatform,
args: Array<out String>,
messageCollector: MessageCollector,
outputsCollector: ((File, List<File>) -> Unit)? = null,
compilerMode: CompilerMode = CompilerMode.NON_INCREMENTAL_COMPILER,
reportSeverity: ReportSeverity = ReportSeverity.INFO,
port: Int = SOCKET_ANY_FREE_PORT,
profiler: Profiler = DummyProfiler()
): IntUsage Example:
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
val compileService = KotlinCompilerClient.connectToCompileService(...)
if (compileService != null) {
val messageCollector = PrintingMessageCollector(System.out, null, false)
val outputsCollector = { outputFile: File, sourceFiles: List<File> ->
println("Generated $outputFile from sources: ${sourceFiles.joinToString()}")
}
val exitCode = KotlinCompilerClient.compile(
compilerService = compileService,
sessionId = CompileService.NO_SESSION,
targetPlatform = CompileService.TargetPlatform.JVM,
args = arrayOf(
"-classpath", "/path/to/libs/*",
"-d", "/path/to/output",
"src/main/kotlin/MyClass.kt"
),
messageCollector = messageCollector,
outputsCollector = outputsCollector,
compilerMode = CompilerMode.INCREMENTAL_COMPILER
)
if (exitCode == 0) {
println("Compilation successful")
} else {
println("Compilation failed with exit code: $exitCode")
}
}Container for compilation-related services and components used during compilation.
/**
* Container for compilation services and components
* @param incrementalCompilationComponents Components for incremental compilation
* @param lookupTracker Tracks symbol lookups for incremental compilation
* @param compilationCanceledStatus Allows checking if compilation was canceled
*/
data class CompilationServices(
val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
val lookupTracker: LookupTracker? = null,
val compilationCanceledStatus: CompilationCanceledStatus? = null
)The compilation service supports multiple target platforms:
// JVM target platform
CompileService.TargetPlatform.JVM
// JavaScript target platform
CompileService.TargetPlatform.JS
// Metadata target platform (for multiplatform common modules)
CompileService.TargetPlatform.METADATAAvailable compilation modes for different compilation strategies:
// Non-incremental compilation (clean build)
CompilerMode.NON_INCREMENTAL_COMPILER
// Incremental compilation (only recompile changed files)
CompilerMode.INCREMENTAL_COMPILER
// JPS (JetBrains Project System) compiler mode
CompilerMode.JPS_COMPILERControl the minimum severity level for compilation reporting:
// Debug level (most verbose)
ReportSeverity.DEBUG
// Informational messages
ReportSeverity.INFO
// Warnings
ReportSeverity.WARNING
// Errors only
ReportSeverity.ERRORval exitCode = KotlinCompilerClient.compile(
compilerService = service,
sessionId = CompileService.NO_SESSION,
targetPlatform = CompileService.TargetPlatform.JVM,
args = arrayOf("MyClass.kt"),
messageCollector = PrintingMessageCollector(System.out, null, false)
)val services = CompilationServices(
incrementalCompilationComponents = myIncrementalComponents,
lookupTracker = myLookupTracker
)
val outputCollector = { outputFile: File, sourceFiles: List<File> ->
// Track generated outputs
println("$outputFile <- ${sourceFiles.joinToString { it.name }}")
}
val exitCode = KotlinCompilerClient.compile(
compilerService = service,
sessionId = sessionId,
targetPlatform = CompileService.TargetPlatform.JVM,
args = arrayOf(
"-classpath", classpathString,
"-d", outputDir.absolutePath,
*sourceFiles.map { it.absolutePath }.toTypedArray()
),
messageCollector = myMessageCollector,
outputsCollector = outputCollector,
compilerMode = CompilerMode.INCREMENTAL_COMPILER
)val exitCode = KotlinCompilerClient.compile(
compilerService = service,
sessionId = CompileService.NO_SESSION,
targetPlatform = CompileService.TargetPlatform.JS,
args = arrayOf(
"-output", "output.js",
"-module-kind", "commonjs",
"src/main/kotlin/App.kt"
),
messageCollector = messageCollector
)val exitCode = KotlinCompilerClient.compile(
compilerService = service,
sessionId = CompileService.NO_SESSION,
targetPlatform = CompileService.TargetPlatform.METADATA,
args = arrayOf(
"-d", "build/classes/kotlin/metadata",
"-module-name", "common",
"src/commonMain/kotlin/Common.kt"
),
messageCollector = messageCollector
)class CustomMessageCollector : MessageCollector {
private val errors = mutableListOf<String>()
private val warnings = mutableListOf<String>()
override fun report(
severity: CompilerMessageSeverity,
message: String,
location: CompilerMessageSourceLocation?
) {
val locationStr = location?.let { "${it.path}:${it.line}" } ?: "unknown"
when (severity) {
CompilerMessageSeverity.ERROR -> errors.add("$locationStr: $message")
CompilerMessageSeverity.WARNING -> warnings.add("$locationStr: $message")
else -> println("$severity: $locationStr: $message")
}
}
override fun hasErrors(): Boolean = errors.isNotEmpty()
override fun clear() {
errors.clear()
warnings.clear()
}
fun getErrors(): List<String> = errors.toList()
fun getWarnings(): List<String> = warnings.toList()
}
val messageCollector = CustomMessageCollector()
val exitCode = KotlinCompilerClient.compile(
compilerService = service,
sessionId = CompileService.NO_SESSION,
targetPlatform = CompileService.TargetPlatform.JVM,
args = compilationArgs,
messageCollector = messageCollector
)
if (messageCollector.hasErrors()) {
println("Compilation errors:")
messageCollector.getErrors().forEach(::println)
}Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-daemon-client