or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

compilation-services.mddaemon-connection.mdindex.mdrepl-client.mdservice-facades.mdstream-servers.md

compilation-services.mddocs/

0

# Compilation Services

1

2

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.

3

4

## Capabilities

5

6

### Compile Function

7

8

Performs compilation through the daemon with full control over compilation parameters.

9

10

```kotlin { .api }

11

/**

12

* Compile Kotlin source code through the daemon

13

* @param compilerService The daemon service to use

14

* @param sessionId Session ID (use CompileService.NO_SESSION for no session)

15

* @param targetPlatform Target platform (JVM, JS, Native, etc.)

16

* @param args Compiler arguments including source files and options

17

* @param messageCollector Handles compiler messages and diagnostics

18

* @param outputsCollector Optional callback for tracking output files

19

* @param compilerMode Compilation mode (incremental, non-incremental)

20

* @param reportSeverity Minimum severity level for reporting

21

* @param port Port for service communication

22

* @param profiler Profiling interface for performance measurement

23

* @return Compilation exit code (0 = success)

24

*/

25

fun compile(

26

compilerService: CompileService,

27

sessionId: Int,

28

targetPlatform: CompileService.TargetPlatform,

29

args: Array<out String>,

30

messageCollector: MessageCollector,

31

outputsCollector: ((File, List<File>) -> Unit)? = null,

32

compilerMode: CompilerMode = CompilerMode.NON_INCREMENTAL_COMPILER,

33

reportSeverity: ReportSeverity = ReportSeverity.INFO,

34

port: Int = SOCKET_ANY_FREE_PORT,

35

profiler: Profiler = DummyProfiler()

36

): Int

37

```

38

39

**Usage Example:**

40

41

```kotlin

42

import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient

43

import org.jetbrains.kotlin.daemon.common.*

44

import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector

45

46

val compileService = KotlinCompilerClient.connectToCompileService(...)

47

48

if (compileService != null) {

49

val messageCollector = PrintingMessageCollector(System.out, null, false)

50

51

val outputsCollector = { outputFile: File, sourceFiles: List<File> ->

52

println("Generated $outputFile from sources: ${sourceFiles.joinToString()}")

53

}

54

55

val exitCode = KotlinCompilerClient.compile(

56

compilerService = compileService,

57

sessionId = CompileService.NO_SESSION,

58

targetPlatform = CompileService.TargetPlatform.JVM,

59

args = arrayOf(

60

"-classpath", "/path/to/libs/*",

61

"-d", "/path/to/output",

62

"src/main/kotlin/MyClass.kt"

63

),

64

messageCollector = messageCollector,

65

outputsCollector = outputsCollector,

66

compilerMode = CompilerMode.INCREMENTAL_COMPILER

67

)

68

69

if (exitCode == 0) {

70

println("Compilation successful")

71

} else {

72

println("Compilation failed with exit code: $exitCode")

73

}

74

}

75

```

76

77

## Types

78

79

### CompilationServices

80

81

Container for compilation-related services and components used during compilation.

82

83

```kotlin { .api }

84

/**

85

* Container for compilation services and components

86

* @param incrementalCompilationComponents Components for incremental compilation

87

* @param lookupTracker Tracks symbol lookups for incremental compilation

88

* @param compilationCanceledStatus Allows checking if compilation was canceled

89

*/

90

data class CompilationServices(

91

val incrementalCompilationComponents: IncrementalCompilationComponents? = null,

92

val lookupTracker: LookupTracker? = null,

93

val compilationCanceledStatus: CompilationCanceledStatus? = null

94

)

95

```

96

97

## Target Platforms

98

99

The compilation service supports multiple target platforms:

100

101

```kotlin

102

// JVM target platform

103

CompileService.TargetPlatform.JVM

104

105

// JavaScript target platform

106

CompileService.TargetPlatform.JS

107

108

// Metadata target platform (for multiplatform common modules)

109

CompileService.TargetPlatform.METADATA

110

```

111

112

## Compiler Modes

113

114

Available compilation modes for different compilation strategies:

115

116

```kotlin

117

// Non-incremental compilation (clean build)

118

CompilerMode.NON_INCREMENTAL_COMPILER

119

120

// Incremental compilation (only recompile changed files)

121

CompilerMode.INCREMENTAL_COMPILER

122

123

// JPS (JetBrains Project System) compiler mode

124

CompilerMode.JPS_COMPILER

125

```

126

127

## Report Severity Levels

128

129

Control the minimum severity level for compilation reporting:

130

131

```kotlin

132

// Debug level (most verbose)

133

ReportSeverity.DEBUG

134

135

// Informational messages

136

ReportSeverity.INFO

137

138

// Warnings

139

ReportSeverity.WARNING

140

141

// Errors only

142

ReportSeverity.ERROR

143

```

144

145

## Usage Patterns

146

147

### Basic JVM Compilation

148

149

```kotlin

150

val exitCode = KotlinCompilerClient.compile(

151

compilerService = service,

152

sessionId = CompileService.NO_SESSION,

153

targetPlatform = CompileService.TargetPlatform.JVM,

154

args = arrayOf("MyClass.kt"),

155

messageCollector = PrintingMessageCollector(System.out, null, false)

156

)

157

```

158

159

### Incremental Compilation with Output Tracking

160

161

```kotlin

162

val services = CompilationServices(

163

incrementalCompilationComponents = myIncrementalComponents,

164

lookupTracker = myLookupTracker

165

)

166

167

val outputCollector = { outputFile: File, sourceFiles: List<File> ->

168

// Track generated outputs

169

println("$outputFile <- ${sourceFiles.joinToString { it.name }}")

170

}

171

172

val exitCode = KotlinCompilerClient.compile(

173

compilerService = service,

174

sessionId = sessionId,

175

targetPlatform = CompileService.TargetPlatform.JVM,

176

args = arrayOf(

177

"-classpath", classpathString,

178

"-d", outputDir.absolutePath,

179

*sourceFiles.map { it.absolutePath }.toTypedArray()

180

),

181

messageCollector = myMessageCollector,

182

outputsCollector = outputCollector,

183

compilerMode = CompilerMode.INCREMENTAL_COMPILER

184

)

185

```

186

187

### JavaScript Compilation

188

189

```kotlin

190

val exitCode = KotlinCompilerClient.compile(

191

compilerService = service,

192

sessionId = CompileService.NO_SESSION,

193

targetPlatform = CompileService.TargetPlatform.JS,

194

args = arrayOf(

195

"-output", "output.js",

196

"-module-kind", "commonjs",

197

"src/main/kotlin/App.kt"

198

),

199

messageCollector = messageCollector

200

)

201

```

202

203

### Metadata Compilation (Multiplatform Common)

204

205

```kotlin

206

val exitCode = KotlinCompilerClient.compile(

207

compilerService = service,

208

sessionId = CompileService.NO_SESSION,

209

targetPlatform = CompileService.TargetPlatform.METADATA,

210

args = arrayOf(

211

"-d", "build/classes/kotlin/metadata",

212

"-module-name", "common",

213

"src/commonMain/kotlin/Common.kt"

214

),

215

messageCollector = messageCollector

216

)

217

```

218

219

### Compilation with Custom Message Collector

220

221

```kotlin

222

class CustomMessageCollector : MessageCollector {

223

private val errors = mutableListOf<String>()

224

private val warnings = mutableListOf<String>()

225

226

override fun report(

227

severity: CompilerMessageSeverity,

228

message: String,

229

location: CompilerMessageSourceLocation?

230

) {

231

val locationStr = location?.let { "${it.path}:${it.line}" } ?: "unknown"

232

when (severity) {

233

CompilerMessageSeverity.ERROR -> errors.add("$locationStr: $message")

234

CompilerMessageSeverity.WARNING -> warnings.add("$locationStr: $message")

235

else -> println("$severity: $locationStr: $message")

236

}

237

}

238

239

override fun hasErrors(): Boolean = errors.isNotEmpty()

240

override fun clear() {

241

errors.clear()

242

warnings.clear()

243

}

244

245

fun getErrors(): List<String> = errors.toList()

246

fun getWarnings(): List<String> = warnings.toList()

247

}

248

249

val messageCollector = CustomMessageCollector()

250

val exitCode = KotlinCompilerClient.compile(

251

compilerService = service,

252

sessionId = CompileService.NO_SESSION,

253

targetPlatform = CompileService.TargetPlatform.JVM,

254

args = compilationArgs,

255

messageCollector = messageCollector

256

)

257

258

if (messageCollector.hasErrors()) {

259

println("Compilation errors:")

260

messageCollector.getErrors().forEach(::println)

261

}

262

```