diff --git a/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt b/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt index 5d751b901eb..8489487d4b6 100644 --- a/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt +++ b/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt @@ -21,6 +21,9 @@ 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.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 kotlin.reflect.KClass 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 { val platformNames = getAttributeValue("allPlatforms")?.split('/')?.toSet() - if (platformNames != null) { - return TargetPlatform(CommonPlatforms.allSimplePlatforms - .flatMap { it.componentPlatforms } - .filter { platformNames.contains(it.serializeToString()) } - .toSet()) + if (platformNames != null && platformNames.isNotEmpty()) { + val knownSimplePlatforms = HashMap() // serialization presentation to simple platform + + // first, collect serialization presentations for every known simple platform + 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") - // this code could be simplified using union after fixing the equals method in SimplePlatform - val allPlatforms = ArrayList(CommonPlatforms.allSimplePlatforms).also { it.add(CommonPlatforms.defaultCommonPlatform) } - return allPlatforms.firstOrNull { it.oldFashionedDescription == platformName }.orDefault() + + // failed to read list of all platforms. Fallback to legacy algorithm + val platformName = getAttributeValue("platform") as String + + 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 { @@ -298,9 +322,14 @@ private fun buildChildElement(element: Element, tag: String, bean: Any, filter: private fun KotlinFacetSettings.writeLatestConfig(element: Element) { val filter = SkipDefaultsSerializationFilter() - targetPlatform?.let { - element.setAttribute("platform", it.oldFashionedDescription) - element.setAttribute("allPlatforms", it.componentPlatforms.map { it.serializeToString() }.sorted().joinToString(separator = "/")) + // 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. + targetPlatform?.let { targetPlatform -> + element.setAttribute("platform", targetPlatform.oldFashionedDescription) + element.setAttribute( + "allPlatforms", + targetPlatform.componentPlatforms.map { it.serializeToString() }.sorted().joinToString(separator = "/") + ) } if (!useProjectSettings) { element.setAttribute("useProjectSettings", useProjectSettings.toString())