org.jetbrains.kotlin.backend.konan.BinaryOptions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-native-compiler-embeddable Show documentation
Show all versions of kotlin-native-compiler-embeddable Show documentation
Embeddable JAR of Kotlin/Native compiler
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.konan
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlin.konan.target.SanitizerKind
import kotlin.properties.PropertyDelegateProvider
import kotlin.properties.ReadOnlyProperty
// Note: options defined in this class are a part of user interface, including the names:
// users can pass these options using a -Xbinary=name=value compiler argument or corresponding Gradle DSL.
object BinaryOptions : BinaryOptionRegistry() {
val runtimeAssertionsMode by option()
val checkStateAtExternalCalls by booleanOption()
val memoryModel by option()
val freezing by option()
val stripDebugInfoFromNativeLibs by booleanOption()
val sourceInfoType by option()
val androidProgramType by option()
val unitSuspendFunctionObjCExport by option()
val objcExportSuspendFunctionLaunchThreadRestriction by option()
val objcExportDisableSwiftMemberNameMangling by booleanOption()
val objcExportIgnoreInterfaceMethodCollisions by booleanOption()
val objcExportReportNameCollisions by booleanOption()
val objcExportErrorOnNameCollisions by booleanOption()
val objcExportEntryPointsPath by stringOption()
val gc by option(shortcut = { it.shortcut })
val gcSchedulerType by option(hideValue = { it.deprecatedWithReplacement != null })
val gcMarkSingleThreaded by booleanOption()
val concurrentWeakSweep by booleanOption()
val concurrentMarkMaxIterations by uintOption()
val gcMutatorsCooperate by booleanOption()
val auxGCThreads by uintOption()
val linkRuntime by option()
val bundleId by stringOption()
val bundleShortVersionString by stringOption()
val bundleVersion by stringOption()
val appStateTracking by option()
val sanitizer by option()
val mimallocUseDefaultOptions by booleanOption()
val mimallocUseCompaction by booleanOption()
val compileBitcodeWithXcodeLlvm by booleanOption()
val objcDisposeOnMain by booleanOption()
val objcDisposeWithRunLoop by booleanOption()
val disableMmap by booleanOption()
val enableSafepointSignposts by booleanOption()
val packFields by booleanOption()
val cInterfaceMode by option()
val globalDataLazyInit by booleanOption()
val swiftExport by booleanOption()
val genericSafeCasts by booleanOption()
val smallBinary by booleanOption()
}
open class BinaryOption(
val name: String,
val valueParser: ValueParser,
val compilerConfigurationKey: CompilerConfigurationKey = CompilerConfigurationKey.create(name)
) {
interface ValueParser {
fun parse(value: String): T?
val validValuesHint: String?
}
}
open class BinaryOptionRegistry {
private val registeredOptionsByName = mutableMapOf>()
protected fun register(option: BinaryOption<*>) {
val previousOption = registeredOptionsByName[option.name]
if (previousOption != null) {
error("option '${option.name}' is registered twice")
}
registeredOptionsByName[option.name] = option
}
fun getByName(name: String): BinaryOption<*>? = registeredOptionsByName[name]
protected fun booleanOption(): PropertyDelegateProvider>> =
PropertyDelegateProvider { _, property ->
val option = BinaryOption(property.name, BooleanValueParser)
register(option)
ReadOnlyProperty { _, _ ->
option.compilerConfigurationKey
}
}
protected fun uintOption(): PropertyDelegateProvider>> =
PropertyDelegateProvider { _, property ->
val option = BinaryOption(property.name, UIntValueParser)
register(option)
ReadOnlyProperty { _, _ ->
option.compilerConfigurationKey
}
}
protected fun stringOption(): PropertyDelegateProvider>> =
PropertyDelegateProvider { _, property ->
val option = BinaryOption(property.name, StringValueParser)
register(option)
ReadOnlyProperty { _, _ ->
option.compilerConfigurationKey
}
}
protected inline fun > option(noinline shortcut : (T) -> String? = { null }, noinline hideValue: (T) -> Boolean = { false }): PropertyDelegateProvider>> =
PropertyDelegateProvider { _, property ->
val option = BinaryOption(property.name, EnumValueParser(enumValues().toList(), shortcut, hideValue))
register(option)
ReadOnlyProperty { _, _ ->
option.compilerConfigurationKey
}
}
}
private object BooleanValueParser : BinaryOption.ValueParser {
override fun parse(value: String): Boolean? = value.toBooleanStrictOrNull()
override val validValuesHint: String?
get() = "true|false"
}
private object UIntValueParser : BinaryOption.ValueParser {
override fun parse(value: String): UInt? = value.toUIntOrNull()
override val validValuesHint: String?
get() = "non-negative-number"
}
private object StringValueParser : BinaryOption.ValueParser {
override fun parse(value: String) = value
override val validValuesHint: String?
get() = null
}
@PublishedApi
internal class EnumValueParser>(
val values: List,
val shortcut: (T) -> String?,
val hideValue: (T) -> Boolean,
) : BinaryOption.ValueParser {
override fun parse(value: String): T? = values.firstOrNull {
// TODO: should we really ignore case here?
it.name.equals(value, ignoreCase = true) || (shortcut(it)?.equals(value, ignoreCase = true) ?: false)
}
override val validValuesHint: String?
get() = values.filter { !hideValue(it) }.map {
val fullName = "$it".lowercase()
shortcut(it)?.let { short ->
"$fullName (or: $short)"
} ?: fullName
}.joinToString("|")
}