Self-contained embeddable Kotlin compiler with shaded dependencies for integration into applications without classpath conflicts
—
Comprehensive configuration management providing type-safe, thread-safe storage for compiler settings and options. The configuration system uses a key-value approach with strongly-typed keys to ensure type safety and prevent configuration errors.
Central thread-safe configuration container managing all compiler settings and options.
/**
* Thread-safe configuration container for compiler settings
* Uses strongly-typed keys to ensure type safety
*/
class CompilerConfiguration {
/** Get configuration value for the specified key, returns null if not set */
fun <T> get(key: CompilerConfigurationKey<T>): T?
/** Set configuration value for the specified key */
fun <T> put(key: CompilerConfigurationKey<T>, value: T): Unit
/** Get configuration value for the specified key, throws if not set */
fun <T> getNotNull(key: CompilerConfigurationKey<T>): T
/** Get boolean configuration value, returns false if not set */
fun getBoolean(key: CompilerConfigurationKey<Boolean>): Boolean
/** Get list configuration value, returns empty list if not set */
fun <T> getList(key: CompilerConfigurationKey<List<T>>): List<T>
/** Add value to list configuration */
fun <T> add(key: CompilerConfigurationKey<List<T>>, value: T): Unit
/** Add all values to list configuration */
fun <T> addAll(key: CompilerConfigurationKey<List<T>>, values: Collection<T>): Unit
/** Create a copy of this configuration */
fun copy(): CompilerConfiguration
/** Set message collector for this configuration */
var messageCollector: MessageCollector
}Usage Examples:
val configuration = CompilerConfiguration()
// Basic configuration
configuration.put(CommonConfigurationKeys.MODULE_NAME, "my-module")
configuration.put(JVMConfigurationKeys.OUTPUT_DIRECTORY, File("build/classes"))
configuration.put(JVMConfigurationKeys.JVM_TARGET, JvmTarget.JVM_17)
// List configuration
configuration.add(CLIConfigurationKeys.CONTENT_ROOTS, JavaSourceRoot(File("src/main/java")))
configuration.add(CLIConfigurationKeys.CONTENT_ROOTS, KotlinSourceRoot(File("src/main/kotlin")))
// Retrieving values
val moduleName = configuration.getNotNull(CommonConfigurationKeys.MODULE_NAME)
val outputDir = configuration.get(JVMConfigurationKeys.OUTPUT_DIRECTORY)
val sources = configuration.getList(CLIConfigurationKeys.CONTENT_ROOTS)
// Boolean configuration
configuration.put(JVMConfigurationKeys.INCLUDE_RUNTIME, true)
val includeRuntime = configuration.getBoolean(JVMConfigurationKeys.INCLUDE_RUNTIME)JVM-specific compiler configuration arguments providing all options for JVM compilation.
/**
* JVM-specific compiler arguments extending common compiler arguments
* Contains all JVM compilation options and settings
*/
class K2JVMCompilerArguments : CommonCompilerArguments() {
/** Output directory for compiled classes */
var destination: String? = null
/** Classpath for compilation (colon/semicolon separated) */
var classpath: String? = null
/** Include Kotlin runtime in the output JAR */
var includeRuntime: Boolean = false
/** Path to JDK home directory */
var jdkHome: String? = null
/** JVM target version (1.8, 9, 10, 11, 15, 17, 19, 20, 21) */
var jvmTarget: String = JvmTarget.DEFAULT.description
/** Generate Java 8+ parameter metadata */
var javaParameters: Boolean = false
/** Enable/disable Java module support */
var javaModulePath: String? = null
/** Module name for the compiled module */
var moduleName: String? = null
/** Enable/disable assertions in JVM */
var assertionsMode: JVMAssertionsMode = JVMAssertionsMode.DEFAULT
/** Paths to JAR files containing annotation processors */
var pluginClasspaths: Array<String>? = null
/** Options to pass to annotation processors */
var pluginOptions: Array<String>? = null
/** Enable/disable use of type annotations in the JVM bytecode */
var useTypeTable: Boolean = true
/** Enable/disable use of SAM conversions */
var samConversions: String = "class"
/** Enable/disable lambda code generation optimizations */
var lambdas: String = "class"
/** Enable/disable use of old Java 6 targets */
var useOldBackend: Boolean = false
}Base compiler arguments shared across all Kotlin compilation targets.
/**
* Base compiler arguments shared across all platforms
* Contains language version, API version, and common settings
*/
abstract class CommonCompilerArguments {
/** Language version (1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0) */
var languageVersion: String? = null
/** API version (1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0) */
var apiVersion: String? = null
/** Enable verbose output */
var verbose: Boolean = false
/** Treat all warnings as errors */
var allWarningsAsErrors: Boolean = false
/** Report all warnings, even those normally suppressed */
var reportAllWarnings: Boolean = false
/** Suppress all warnings */
var suppressWarnings: Boolean = false
/** Enable experimental language features */
var useExperimental: Array<String>? = null
/** Require explicit API mode */
var explicitApi: String? = null
/** Enable progressive compiler mode */
var progressive: Boolean = false
/** Disable optimization */
var noOptimize: Boolean = false
/** Free-form compiler arguments */
var freeArgs: List<String> = emptyList()
}Strongly-typed keys for accessing configuration values.
/**
* Common configuration keys used across all platforms
*/
object CommonConfigurationKeys {
/** Module name for compilation */
val MODULE_NAME: CompilerConfigurationKey<String>
/** Language version settings */
val LANGUAGE_VERSION_SETTINGS: CompilerConfigurationKey<LanguageVersionSettings>
/** Message collector for diagnostics */
val MESSAGE_COLLECTOR_KEY: CompilerConfigurationKey<MessageCollector>
/** Use K2 compiler frontend */
val USE_FIR: CompilerConfigurationKey<Boolean>
/** Metadata version */
val METADATA_VERSION: CompilerConfigurationKey<BinaryVersion>
/** Disable intrinsics */
val DISABLE_INTRINSICS: CompilerConfigurationKey<Boolean>
/** Phase configuration */
val PHASE_CONFIG: CompilerConfigurationKey<PhaseConfig>
}
/**
* JVM-specific configuration keys
*/
object JVMConfigurationKeys {
/** Output directory for compiled classes */
val OUTPUT_DIRECTORY: CompilerConfigurationKey<File>
/** Output JAR file */
val OUTPUT_JAR: CompilerConfigurationKey<File>
/** JVM target version */
val JVM_TARGET: CompilerConfigurationKey<JvmTarget>
/** Include Kotlin runtime in output */
val INCLUDE_RUNTIME: CompilerConfigurationKey<Boolean>
/** Incremental compilation components */
val INCREMENTAL_COMPILATION_COMPONENTS: CompilerConfigurationKey<IncrementalCompilationComponents>
/** Disable optimizations */
val DISABLE_OPTIMIZATION: CompilerConfigurationKey<Boolean>
/** Use type table in bytecode */
val USE_TYPE_TABLE: CompilerConfigurationKey<Boolean>
/** Assertions mode */
val ASSERTIONS_MODE: CompilerConfigurationKey<JVMAssertionsMode>
}
/**
* CLI-specific configuration keys
*/
object CLIConfigurationKeys {
/** Content roots (source directories) */
val CONTENT_ROOTS: CompilerConfigurationKey<List<ContentRoot>>
/** Original message collector */
val ORIGINAL_MESSAGE_COLLECTOR_KEY: CompilerConfigurationKey<MessageCollector>
/** Performance manager */
val PERF_MANAGER: CompilerConfigurationKey<PerformanceManager>
/** Allow Kotlin sources alongside Java */
val ALLOW_KOTLIN_ALONGSIDE_JAVA_SOURCES: CompilerConfigurationKey<Boolean>
}Advanced Configuration Examples:
// Complex configuration setup
val configuration = CompilerConfiguration().apply {
// Language settings
put(CommonConfigurationKeys.LANGUAGE_VERSION_SETTINGS,
LanguageVersionSettingsImpl(LanguageVersion.KOTLIN_2_0, ApiVersion.KOTLIN_2_0))
// JVM settings
put(JVMConfigurationKeys.JVM_TARGET, JvmTarget.JVM_17)
put(JVMConfigurationKeys.OUTPUT_DIRECTORY, File("build/classes"))
put(JVMConfigurationKeys.INCLUDE_RUNTIME, false)
put(JVMConfigurationKeys.USE_TYPE_TABLE, true)
// Source roots
add(CLIConfigurationKeys.CONTENT_ROOTS,
KotlinSourceRoot("src/main/kotlin", isCommon = false))
add(CLIConfigurationKeys.CONTENT_ROOTS,
JavaSourceRoot(File("src/main/java")))
// Message collection
messageCollector = PrintingMessageCollector(System.err, MessageRenderer.PLAIN, true)
// Performance tracking
put(CLIConfigurationKeys.PERF_MANAGER, ProfilingCompilerPerformanceManager())
}
// Using with incremental compilation
val incrementalComponents = IncrementalCompilationComponents.create()
configuration.put(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS, incrementalComponents)/**
* JVM target versions
*/
enum class JvmTarget(val description: String) {
JVM_1_8("1.8"),
JVM_9("9"),
JVM_10("10"),
JVM_11("11"),
JVM_15("15"),
JVM_17("17"),
JVM_19("19"),
JVM_20("20"),
JVM_21("21");
companion object {
val DEFAULT: JvmTarget = JVM_1_8
}
}
/**
* JVM assertions mode
*/
enum class JVMAssertionsMode {
DEFAULT, ENABLE, DISABLE
}
/**
* Content root types for source directories
*/
sealed class ContentRoot
data class KotlinSourceRoot(
val path: String,
val isCommon: Boolean = false
) : ContentRoot()
data class JavaSourceRoot(
val file: File
) : ContentRoot()Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-compiler-embeddable