[Platform API] Remove IdePlatform, use TargetPlatform instead

Original commit: 2d528c6396
This commit is contained in:
Dmitry Savvinov
2019-03-25 18:38:10 +03:00
parent 7deb18da0b
commit 8112f3be71
14 changed files with 128 additions and 137 deletions
@@ -22,9 +22,9 @@ import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.copyBean
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatformVersion
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.utils.DescriptionAware
@Deprecated("Use IdePlatformKind instead.", level = DeprecationLevel.ERROR)
@@ -188,7 +188,7 @@ class KotlinFacetSettings {
compilerArguments!!.apiVersion = value?.versionString
}
val platform: IdePlatform<*, *>?
val platform: TargetPlatform?
get() {
val compilerArguments = this.compilerArguments ?: return null
return IdePlatformKind.platformByCompilerArguments(compilerArguments)
@@ -25,10 +25,17 @@ import org.jdom.Element
import org.jdom.Text
import org.jetbrains.kotlin.cli.common.arguments.*
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.impl.FakeK2NativeCompilerArguments
import org.jetbrains.kotlin.platform.impl.JvmIdePlatformKind
import org.jetbrains.kotlin.platform.js.JsPlatform
import org.jetbrains.kotlin.platform.jvm.JdkPlatform
import org.jetbrains.kotlin.platform.jvm.JvmPlatform
import org.jetbrains.kotlin.platform.konan.KonanPlatform
import org.jetbrains.kotlin.platform.oldFashionedDescription
import org.jetbrains.kotlin.platform.orDefault
import org.jetbrains.kotlin.resolve.*
import java.lang.reflect.Modifier
import kotlin.reflect.KClass
import kotlin.reflect.full.superclasses
@@ -39,6 +46,20 @@ private fun Element.getOptionValue(name: String) = getOption(name)?.getAttribute
private fun Element.getOptionBody(name: String) = getOption(name)?.children?.firstOrNull()
fun TargetPlatform.createArguments(init: (CommonCompilerArguments).() -> Unit = {}): CommonCompilerArguments {
return when (val singlePlatform = singleOrNull()) {
null -> K2MetadataCompilerArguments().apply { init() }
is JvmPlatform -> K2JVMCompilerArguments().apply {
init()
// TODO(dsavvinov): review this
jvmTarget = (singlePlatform as? JdkPlatform)?.targetVersion?.description ?: JvmTarget.DEFAULT.description
}
is JsPlatform -> K2JSCompilerArguments().apply { init() }
is KonanPlatform -> FakeK2NativeCompilerArguments().apply { init() }
else -> error("Unknown platform $singlePlatform")
}
}
private fun readV1Config(element: Element): KotlinFacetSettings {
return KotlinFacetSettings().apply {
val useProjectSettings = element.getOptionValue("useProjectSettings")?.toBoolean()
@@ -48,8 +69,8 @@ private fun readV1Config(element: Element): KotlinFacetSettings {
val languageLevel = versionInfoElement?.getOptionValue("languageLevel")
val apiLevel = versionInfoElement?.getOptionValue("apiLevel")
val targetPlatform = IdePlatformKind.All_PLATFORMS
.firstOrNull { it.description == targetPlatformName }
?: JvmIdePlatformKind.defaultPlatform
.firstOrNull { it.oldFashionedDescription == targetPlatformName }
?: JvmIdePlatformKind.defaultPlatform // FIXME(dsavvinov): choose proper default
val compilerInfoElement = element.getOptionBody("compilerInfo")
@@ -98,10 +119,10 @@ private fun readV1Config(element: Element): KotlinFacetSettings {
}
}
fun Element.getFacetPlatformByConfigurationElement(): IdePlatform<*, *> {
fun Element.getFacetPlatformByConfigurationElement(): TargetPlatform {
val platformName = getAttributeValue("platform")
return IdePlatformKind.All_PLATFORMS
.firstOrNull { it.description == platformName }
.firstOrNull { it.oldFashionedDescription == platformName }
.orDefault()
}
@@ -268,7 +289,7 @@ private fun KotlinFacetSettings.writeLatestConfig(element: Element) {
val filter = SkipDefaultsSerializationFilter()
platform?.let {
element.setAttribute("platform", it.description)
element.setAttribute("platform", it.oldFashionedDescription)
}
if (!useProjectSettings) {
element.setAttribute("useProjectSettings", useProjectSettings.toString())
@@ -7,23 +7,29 @@ package org.jetbrains.kotlin.platform
import com.intellij.openapi.components.ServiceManager
import org.jetbrains.kotlin.config.isJps
import org.jetbrains.kotlin.platform.impl.JvmIdePlatformKind
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import org.jetbrains.kotlin.platform.TargetPlatform
interface DefaultIdeTargetPlatformKindProvider {
val defaultPlatform: IdePlatform<*, *>
val defaultPlatform: TargetPlatform
companion object {
val defaultPlatform: IdePlatform<*, *>
val defaultPlatform: TargetPlatform
get() {
if (isJps) {
// TODO support passing custom platforms in JPS
return JvmIdePlatformKind.defaultPlatform
return JvmPlatforms.defaultJvmPlatform
}
return ServiceManager.getService(DefaultIdeTargetPlatformKindProvider::class.java).defaultPlatform
}
val defaultCompilerPlatform: TargetPlatform
get() = defaultPlatform.kind.compilerPlatform
}
}
fun TargetPlatform?.orDefault(): TargetPlatform {
return this ?: DefaultIdeTargetPlatformKindProvider.defaultPlatform
}
fun IdePlatformKind<*>?.orDefault(): IdePlatformKind<*> {
return this ?: DefaultIdeTargetPlatformKindProvider.defaultPlatform.idePlatformKind
}
@@ -1,21 +0,0 @@
/*
* Copyright 2010-2018 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.platform
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.utils.DescriptionAware
abstract class IdePlatform<Kind : IdePlatformKind<Kind>, out Arguments : CommonCompilerArguments> : DescriptionAware {
abstract val kind: Kind
abstract val version: TargetPlatformVersion
abstract fun createArguments(init: Arguments.() -> Unit = {}): Arguments
override val description
get() = kind.name + " " + version.description
override fun toString() = description
}
@@ -13,20 +13,22 @@ import org.jetbrains.kotlin.platform.impl.CommonIdePlatformKind
import org.jetbrains.kotlin.platform.impl.JsIdePlatformKind
import org.jetbrains.kotlin.platform.impl.JvmIdePlatformKind
import org.jetbrains.kotlin.platform.impl.NativeIdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
abstract class IdePlatformKind<Kind : IdePlatformKind<Kind>> {
abstract val compilerPlatform: TargetPlatform
abstract val platforms: List<IdePlatform<Kind, *>>
abstract val platforms: List<TargetPlatform>
abstract val defaultPlatform: IdePlatform<Kind, *>
abstract val defaultPlatform: TargetPlatform
abstract fun platformByCompilerArguments(arguments: CommonCompilerArguments): IdePlatform<Kind, CommonCompilerArguments>?
abstract fun platformByCompilerArguments(arguments: CommonCompilerArguments): TargetPlatform?
abstract val argumentsClass: Class<out CommonCompilerArguments>
abstract val name: String
abstract fun createArguments(): CommonCompilerArguments
override fun equals(other: Any?): Boolean = javaClass == other?.javaClass
override fun hashCode(): Int = javaClass.hashCode()
@@ -56,21 +58,17 @@ abstract class IdePlatformKind<Kind : IdePlatformKind<Kind>> {
val All_PLATFORMS by lazy { ALL_KINDS.flatMap { it.platforms } }
val IDE_PLATFORMS_BY_COMPILER_PLATFORMS by lazy { ALL_KINDS.map { it.compilerPlatform to it }.toMap() }
val IDE_PLATFORMS_BY_COMPILER_PLATFORMS by lazy {
ALL_KINDS.flatMap { idePlatformKind ->
idePlatformKind.platforms.map { compilerPlatform -> compilerPlatform to idePlatformKind }
}.toMap()
}
fun <Args : CommonCompilerArguments> platformByCompilerArguments(arguments: Args): IdePlatform<*, *>? =
fun <Args : CommonCompilerArguments> platformByCompilerArguments(arguments: Args): TargetPlatform? =
ALL_KINDS.firstNotNullResult { it.platformByCompilerArguments(arguments) }
}
}
val TargetPlatform.idePlatformKind: IdePlatformKind<*>
get() = IdePlatformKind.IDE_PLATFORMS_BY_COMPILER_PLATFORMS[this] ?: error("Unknown platform $this")
fun IdePlatformKind<*>?.orDefault(): IdePlatformKind<*> {
return this ?: DefaultIdeTargetPlatformKindProvider.defaultPlatform.kind
}
fun IdePlatform<*, *>?.orDefault(): IdePlatform<*, *> {
return this ?: DefaultIdeTargetPlatformKindProvider.defaultPlatform
}
get() = IdePlatformKind.IDE_PLATFORMS_BY_COMPILER_PLATFORMS[this] ?: error("Unknown platform $this")
@@ -9,35 +9,29 @@ package org.jetbrains.kotlin.platform.impl
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
import org.jetbrains.kotlin.platform.CommonPlatforms
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatformVersion
import org.jetbrains.kotlin.platform.TargetPlatform
object CommonIdePlatformKind : IdePlatformKind<CommonIdePlatformKind>() {
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): IdePlatform<CommonIdePlatformKind, CommonCompilerArguments>? {
return if (arguments is K2MetadataCompilerArguments) Platform
else null
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): TargetPlatform? {
return if (arguments is K2MetadataCompilerArguments)
CommonPlatforms.defaultCommonPlatform
else
null
}
override val compilerPlatform get() = CommonPlatforms.defaultCommonPlatform
override fun createArguments(): CommonCompilerArguments {
return K2MetadataCompilerArguments() // TODO(dsavvinov): review that, as now MPP !== K2Metadata
}
override val platforms get() = listOf(Platform)
override val defaultPlatform get() = Platform
override val platforms get() = listOf(CommonPlatforms.defaultCommonPlatform)
override val defaultPlatform get() = CommonPlatforms.defaultCommonPlatform
override val argumentsClass get() = K2MetadataCompilerArguments::class.java
override val name get() = "Common (experimental)"
object Platform : IdePlatform<CommonIdePlatformKind, CommonCompilerArguments>() {
override val kind get() = CommonIdePlatformKind
override val version get() = TargetPlatformVersion.NoVersion
override fun createArguments(init: CommonCompilerArguments.() -> Unit) = K2MetadataCompilerArguments().apply(init)
}
}
val IdePlatformKind<*>?.isCommon
get() = this is CommonIdePlatformKind
val IdePlatform<*, *>?.isCommon
get() = this is CommonIdePlatformKind.Platform
get() = this is CommonIdePlatformKind
@@ -8,36 +8,30 @@ package org.jetbrains.kotlin.platform.impl
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.platform.TargetPlatformVersion
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.js.JsPlatforms
object JsIdePlatformKind : IdePlatformKind<JsIdePlatformKind>() {
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): IdePlatform<JsIdePlatformKind, CommonCompilerArguments>? {
return if (arguments is K2JSCompilerArguments) Platform
else null
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): TargetPlatform? {
return if (arguments is K2JSCompilerArguments)
JsPlatforms.defaultJsPlatform
else
null
}
override val compilerPlatform get() = JsPlatforms.defaultJsPlatform
override val platforms get() = listOf(JsPlatforms.defaultJsPlatform)
override val defaultPlatform get() = JsPlatforms.defaultJsPlatform
override val platforms get() = listOf(Platform)
override val defaultPlatform get() = Platform
override fun createArguments(): CommonCompilerArguments {
return K2JSCompilerArguments()
}
override val argumentsClass get() = K2JSCompilerArguments::class.java
override val name get() = "JavaScript"
object Platform : IdePlatform<JsIdePlatformKind, K2JSCompilerArguments>() {
override val kind get() = JsIdePlatformKind
override val version get() = TargetPlatformVersion.NoVersion
override fun createArguments(init: K2JSCompilerArguments.() -> Unit) = K2JSCompilerArguments().apply(init)
}
}
val IdePlatformKind<*>?.isJavaScript
get() = this is JsIdePlatformKind
val IdePlatform<*, *>?.isJavaScript
get() = this is JsIdePlatformKind.Platform
@@ -11,41 +11,36 @@ import com.intellij.util.text.VersionComparatorUtil
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
object JvmIdePlatformKind : IdePlatformKind<JvmIdePlatformKind>() {
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): IdePlatform<JvmIdePlatformKind, CommonCompilerArguments>? {
return if (arguments is K2JVMCompilerArguments) {
val jvmTarget = arguments.jvmTarget ?: JvmTarget.DEFAULT.description
platforms.firstOrNull { platform ->
VersionComparatorUtil.COMPARATOR.compare(platform.version.description, jvmTarget) >= 0
}
} else null
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): TargetPlatform? {
if (arguments !is K2JVMCompilerArguments) return null
val jvmTargetDescription = arguments.jvmTarget
?: return JvmPlatforms.defaultJvmPlatform
val jvmTarget = JvmTarget.values()
.firstOrNull { VersionComparatorUtil.COMPARATOR.compare(it.description, jvmTargetDescription) >= 0 }
?: return JvmPlatforms.defaultJvmPlatform
return JvmPlatforms.jvmPlatformByTargetVersion(jvmTarget)
}
override val compilerPlatform get() = JvmPlatforms.defaultJvmPlatform
override fun createArguments(): CommonCompilerArguments {
return K2JVMCompilerArguments()
}
override val platforms = JvmTarget.values().map { ver -> Platform(ver) }
override val defaultPlatform get() = Platform(JvmTarget.DEFAULT)
override val platforms = JvmTarget.values().map { ver -> JvmPlatforms.jvmPlatformByTargetVersion(ver) }
override val defaultPlatform get() = JvmPlatforms.defaultJvmPlatform
override val argumentsClass get() = K2JVMCompilerArguments::class.java
override val name get() = "JVM"
data class Platform(override val version: JvmTarget) : IdePlatform<JvmIdePlatformKind, K2JVMCompilerArguments>() {
override val kind get() = JvmIdePlatformKind
override fun createArguments(init: K2JVMCompilerArguments.() -> Unit) = K2JVMCompilerArguments()
.apply(init)
.apply { jvmTarget = this@Platform.version.description }
}
}
val IdePlatformKind<*>?.isJvm
get() = this is JvmIdePlatformKind
val IdePlatform<*, *>?.isJvm
get() = this is JvmIdePlatformKind.Platform
@@ -7,32 +7,34 @@
package org.jetbrains.kotlin.platform.impl
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.platform.TargetPlatformVersion
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.konan.KonanPlatforms
object NativeIdePlatformKind : IdePlatformKind<NativeIdePlatformKind>() {
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): IdePlatform<NativeIdePlatformKind, CommonCompilerArguments>? {
return if (arguments is FakeK2NativeCompilerArguments) Platform
else null
override fun platformByCompilerArguments(arguments: CommonCompilerArguments): TargetPlatform? {
return if (arguments is FakeK2NativeCompilerArguments)
KonanPlatforms.defaultKonanPlatform
else
null
}
override val compilerPlatform get() = KonanPlatforms.defaultKonanPlatform
override val platforms get() = listOf(Platform)
override val defaultPlatform get() = Platform
override val argumentsClass get() = FakeK2NativeCompilerArguments::class.java
override val name get() = "Native"
object Platform : IdePlatform<NativeIdePlatformKind, FakeK2NativeCompilerArguments>() {
override val kind get() = NativeIdePlatformKind
override val version get() = TargetPlatformVersion.NoVersion
override fun createArguments(init: FakeK2NativeCompilerArguments.() -> Unit) = FakeK2NativeCompilerArguments().apply(init)
override fun createArguments(): CommonCompilerArguments {
return FakeK2NativeCompilerArguments()
}
override val defaultPlatform: TargetPlatform
get() = KonanPlatforms.defaultKonanPlatform
override val platforms
get() = listOf(KonanPlatforms.defaultKonanPlatform)
override val argumentsClass
get() = FakeK2NativeCompilerArguments::class.java
override val name
get() = "Native"
}
// These are fake compiler arguments for Kotlin/Native - only for usage within IDEA plugin:
@@ -40,6 +42,3 @@ class FakeK2NativeCompilerArguments : CommonCompilerArguments()
val IdePlatformKind<*>?.isKotlinNative
get() = this is NativeIdePlatformKind
val IdePlatform<*, *>?.isKotlinNative
get() = this is NativeIdePlatformKind.Platform
@@ -55,6 +55,7 @@ import org.jetbrains.kotlin.jps.incremental.*
import org.jetbrains.kotlin.jps.model.JpsKotlinFacetModuleExtension
import org.jetbrains.kotlin.jps.model.kotlinFacet
import org.jetbrains.kotlin.jps.targets.KotlinModuleBuildTarget
import org.jetbrains.kotlin.platform.idePlatformKind
import org.jetbrains.kotlin.platform.impl.isJavaScript
import org.jetbrains.kotlin.platform.impl.isJvm
import org.jetbrains.kotlin.platform.orDefault
@@ -468,7 +469,7 @@ abstract class AbstractIncrementalJpsTest(
private fun configureRequiredLibraries() {
myProject.modules.forEach { module ->
val platformKind = module.kotlinFacet?.settings?.platform?.kind.orDefault()
val platformKind = module.kotlinFacet?.settings?.platform?.idePlatformKind.orDefault()
when {
platformKind.isJvm -> {
@@ -18,6 +18,8 @@ import org.jetbrains.kotlin.jps.build.dependeciestxt.ModulesTxt.Dependency.Kind.
import org.jetbrains.kotlin.platform.impl.FakeK2NativeCompilerArguments
import org.jetbrains.kotlin.platform.impl.isCommon
import org.jetbrains.kotlin.platform.impl.isJvm
import org.jetbrains.kotlin.platform.isCommon
import org.jetbrains.kotlin.platform.jvm.isJvm
import java.io.File
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.full.findAnnotation
@@ -52,11 +54,11 @@ data class ModulesTxt(
val isCommonModule
get() =
kotlinFacetSettings?.platform.isCommon ||
kotlinFacetSettings?.platform.isCommon() ||
kotlinFacetSettings?.kind == SOURCE_SET_HOLDER
val isJvmModule
get() = kotlinFacetSettings?.platform.isJvm
get() = kotlinFacetSettings?.platform.isJvm()
val expectedBy
get() = dependencies.filter {
@@ -11,12 +11,12 @@ import org.jetbrains.jps.model.java.JpsJavaExtensionService
import org.jetbrains.jps.model.module.JpsModule
import org.jetbrains.kotlin.cli.common.arguments.*
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.TargetPlatform
val JpsModule.kotlinFacet: JpsKotlinFacetModuleExtension?
get() = container.getChild(JpsKotlinFacetModuleExtension.KIND)
val JpsModule.platform: IdePlatform<*, *>?
val JpsModule.platform: TargetPlatform?
get() = kotlinFacet?.settings?.platform
val JpsModule.kotlinKind: KotlinModuleKind
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.jps.build.ModuleBuildTarget
import org.jetbrains.kotlin.jps.model.platform
import org.jetbrains.kotlin.platform.DefaultIdeTargetPlatformKindProvider
import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.idePlatformKind
import org.jetbrains.kotlin.platform.impl.*
import org.jetbrains.kotlin.utils.LibraryUtils
import kotlin.system.measureTimeMillis
@@ -134,7 +135,7 @@ internal class KotlinTargetsIndexBuilder internal constructor(
private fun ensureLoaded(target: ModuleBuildTarget): KotlinModuleBuildTarget<*> {
return byJpsModuleBuildTarget.computeIfAbsent(target) {
val platform = target.module.platform?.kind ?: detectTargetPlatform(target)
val platform = target.module.platform?.idePlatformKind ?: detectTargetPlatform(target)
when {
platform.isCommon -> KotlinCommonModuleBuildTarget(uninitializedContext, target)
@@ -152,7 +153,7 @@ internal class KotlinTargetsIndexBuilder internal constructor(
private fun detectTargetPlatform(target: ModuleBuildTarget): IdePlatformKind<*> {
if (hasJsStdLib(target)) return JsIdePlatformKind
return DefaultIdeTargetPlatformKindProvider.defaultPlatform.kind
return DefaultIdeTargetPlatformKindProvider.defaultPlatform.idePlatformKind
}
private fun hasJsStdLib(target: ModuleBuildTarget): Boolean {
@@ -15,12 +15,13 @@ import org.jetbrains.kotlin.jps.build.KotlinCompileContext
import org.jetbrains.kotlin.jps.build.KotlinDirtySourceFilesHolder
import org.jetbrains.kotlin.jps.incremental.JpsIncrementalCache
import org.jetbrains.kotlin.jps.model.platform
import org.jetbrains.kotlin.platform.idePlatformKind
class KotlinUnsupportedModuleBuildTarget(
kotlinContext: KotlinCompileContext,
jpsModuleBuildTarget: ModuleBuildTarget
) : KotlinModuleBuildTarget<BuildMetaInfo>(kotlinContext, jpsModuleBuildTarget) {
val kind = module.platform?.kind?.name
val kind = module.platform?.idePlatformKind?.name
private fun shouldNotBeCalled(): Nothing = error("Should not be called")