HMPP: Fix serialization of TargetPlatform in Kotlin facet

Original commit: fee6a752e0
This commit is contained in:
Dmitriy Dolovov
2020-04-06 22:13:47 +07:00
parent c2291ab234
commit 11523fc8c4
@@ -21,6 +21,9 @@ import org.jetbrains.kotlin.platform.js.JsPlatform
import org.jetbrains.kotlin.platform.jvm.JdkPlatform import org.jetbrains.kotlin.platform.jvm.JdkPlatform
import org.jetbrains.kotlin.platform.jvm.JvmPlatform import org.jetbrains.kotlin.platform.jvm.JvmPlatform
import org.jetbrains.kotlin.platform.konan.NativePlatform import org.jetbrains.kotlin.platform.konan.NativePlatform
import org.jetbrains.kotlin.platform.konan.NativePlatformUnspecifiedTarget
import org.jetbrains.kotlin.platform.konan.NativePlatforms
import org.jetbrains.kotlin.platform.konan.legacySerializeToString
import java.lang.reflect.Modifier import java.lang.reflect.Modifier
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.full.superclasses import kotlin.reflect.full.superclasses
@@ -104,19 +107,40 @@ private fun readV1Config(element: Element): KotlinFacetSettings {
} }
} }
// TODO: Introduce new version of facet serialization. See https://youtrack.jetbrains.com/issue/KT-38235
// This is necessary to avoid having too much custom logic for platform serialization.
fun Element.getFacetPlatformByConfigurationElement(): TargetPlatform { fun Element.getFacetPlatformByConfigurationElement(): TargetPlatform {
val platformNames = getAttributeValue("allPlatforms")?.split('/')?.toSet() val platformNames = getAttributeValue("allPlatforms")?.split('/')?.toSet()
if (platformNames != null) { if (platformNames != null && platformNames.isNotEmpty()) {
return TargetPlatform(CommonPlatforms.allSimplePlatforms val knownSimplePlatforms = HashMap<String, SimplePlatform>() // serialization presentation to simple platform
.flatMap { it.componentPlatforms }
.filter { platformNames.contains(it.serializeToString()) } // first, collect serialization presentations for every known simple platform
.toSet()) CommonPlatforms.allSimplePlatforms
.flatMap { it.componentPlatforms }
.forEach { knownSimplePlatforms[it.serializeToString()] = it }
// next, add legacy aliases for some of the simple platforms; ex: unspecifiedNativePlatform
NativePlatformUnspecifiedTarget.let { knownSimplePlatforms[it.legacySerializeToString()] = it }
val simplePlatforms = platformNames.mapNotNull(knownSimplePlatforms::get)
if (simplePlatforms.isNotEmpty()) return TargetPlatform(simplePlatforms.toSet())
// empty set of simple platforms is not allowed, in such case fallback to legacy algorithm
} }
// failed to read list of all platforms. Fallback to legacy algorythm
val platformName = getAttributeValue("platform") // failed to read list of all platforms. Fallback to legacy algorithm
// this code could be simplified using union after fixing the equals method in SimplePlatform val platformName = getAttributeValue("platform") as String
val allPlatforms = ArrayList(CommonPlatforms.allSimplePlatforms).also { it.add(CommonPlatforms.defaultCommonPlatform) }
return allPlatforms.firstOrNull { it.oldFashionedDescription == platformName }.orDefault() return CommonPlatforms.allSimplePlatforms.firstOrNull {
// first, look for exact match through all simple platforms
it.oldFashionedDescription == platformName
} ?: CommonPlatforms.defaultCommonPlatform.takeIf {
// then, check exact match for the default common platform
it.oldFashionedDescription == platformName
} ?: NativePlatforms.unspecifiedNativePlatform.takeIf {
// if none of the above succeeded, check if it's an old-style record about native platform (without suffix with target name)
it.oldFashionedDescription.startsWith(platformName)
}.orDefault() // finally, fallback to the default platform
} }
private fun readV2AndLaterConfig(element: Element): KotlinFacetSettings { private fun readV2AndLaterConfig(element: Element): KotlinFacetSettings {
@@ -298,9 +322,14 @@ private fun buildChildElement(element: Element, tag: String, bean: Any, filter:
private fun KotlinFacetSettings.writeLatestConfig(element: Element) { private fun KotlinFacetSettings.writeLatestConfig(element: Element) {
val filter = SkipDefaultsSerializationFilter() val filter = SkipDefaultsSerializationFilter()
targetPlatform?.let { // TODO: Introduce new version of facet serialization. See https://youtrack.jetbrains.com/issue/KT-38235
element.setAttribute("platform", it.oldFashionedDescription) // This is necessary to avoid having too much custom logic for platform serialization.
element.setAttribute("allPlatforms", it.componentPlatforms.map { it.serializeToString() }.sorted().joinToString(separator = "/")) targetPlatform?.let { targetPlatform ->
element.setAttribute("platform", targetPlatform.oldFashionedDescription)
element.setAttribute(
"allPlatforms",
targetPlatform.componentPlatforms.map { it.serializeToString() }.sorted().joinToString(separator = "/")
)
} }
if (!useProjectSettings) { if (!useProjectSettings) {
element.setAttribute("useProjectSettings", useProjectSettings.toString()) element.setAttribute("useProjectSettings", useProjectSettings.toString())