Skip to content

Commit

Permalink
HMPP: Parameterize NativePlatform with KonanTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
ddolovov committed Apr 14, 2020
1 parent 1b281d6 commit 8d2e999
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 38 deletions.
1 change: 1 addition & 0 deletions compiler/cli/cli-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
compile(project(":compiler:config"))
compile(project(":compiler:config.jvm"))
compile(project(":js:js.config"))
compile(project(":native:kotlin-native-utils"))
compileOnly(project(":kotlin-reflect-api"))
compileOnly(intellijCoreDep()) { includeJars("intellij-core") }
compileOnly(intellijDep()) { includeIntellijCoreJarDependencies(project) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.platform.js.JsPlatforms.defaultJsPlatform
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.allJvmPlatforms
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.unspecifiedJvmPlatform
import org.jetbrains.kotlin.platform.konan.NativePlatforms.allNativePlatforms
import org.jetbrains.kotlin.platform.konan.NativePlatforms.defaultNativePlatform
import org.jetbrains.kotlin.platform.konan.NativePlatforms.unspecifiedNativePlatform

@Suppress("DEPRECATION_ERROR")
object CommonPlatforms {
Expand All @@ -23,7 +23,7 @@ object CommonPlatforms {
setOf(
unspecifiedJvmPlatform.single(),
defaultJsPlatform.single(),
defaultNativePlatform.single()
unspecifiedNativePlatform.single()
)
), org.jetbrains.kotlin.analyzer.common.CommonPlatform {
override val platformName: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,62 @@

package org.jetbrains.kotlin.platform.konan

import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.platform.SimplePlatform
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.toTargetPlatform

sealed class NativePlatform : SimplePlatform("Native")

object NativePlatformUnspecifiedTarget : NativePlatform() {
override fun toString() = "$platformName (general)"

override val oldFashionedDescription: String
get() = "Native (general) "
}

data class NativePlatformWithTarget(val target: KonanTarget) : NativePlatform() {
override fun toString() = "$platformName ($target)"

abstract class NativePlatform : SimplePlatform("Native") {
override val oldFashionedDescription: String
get() = "Native "
get() = "Native ($target) "
}

@Suppress("DEPRECATION_ERROR")
object NativePlatforms {
private object DefaultSimpleNativePlatform : NativePlatform()
private val predefinedNativeTargetToSimpleNativePlatform: Map<KonanTarget, NativePlatformWithTarget> =
KonanTarget.predefinedTargets.values.associateWith { NativePlatformWithTarget(it) }

private val predefinedNativeTargetToNativePlatform: Map<KonanTarget, TargetPlatform> =
predefinedNativeTargetToSimpleNativePlatform.mapValues { (_, simplePlatform) -> simplePlatform.toTargetPlatform() }

val unspecifiedNativePlatform: TargetPlatform
get() = CompatNativePlatform

val allNativePlatforms: List<TargetPlatform> = listOf(unspecifiedNativePlatform) + predefinedNativeTargetToNativePlatform.values

fun nativePlatformBySingleTarget(target: KonanTarget): TargetPlatform =
predefinedNativeTargetToNativePlatform[target] ?: unspecifiedNativePlatform

fun nativePlatformByTargets(targets: Collection<KonanTarget>): TargetPlatform {
val simplePlatforms = targets.mapNotNullTo(HashSet()) { predefinedNativeTargetToSimpleNativePlatform[it] }
return when (simplePlatforms.size) {
0 -> unspecifiedNativePlatform
1 -> nativePlatformBySingleTarget(simplePlatforms.first().target)
else -> TargetPlatform(simplePlatforms)
}
}

@Deprecated(
message = "Should be accessed only by compatibility layer, other clients should use 'defaultNativePlatform'",
message = "Should be accessed only by compatibility layer, other clients should use 'unspecifiedNativePlatform'",
level = DeprecationLevel.ERROR
)
object CompatNativePlatform : TargetPlatform(setOf(DefaultSimpleNativePlatform)),
object CompatNativePlatform : TargetPlatform(setOf(NativePlatformUnspecifiedTarget)),
// Needed for backward compatibility, because old code uses INSTANCEOF checks instead of calling extensions
org.jetbrains.kotlin.resolve.konan.platform.KonanPlatform {
override val platformName: String
get() = "Native"
}

val defaultNativePlatform: TargetPlatform
get() = CompatNativePlatform

val allNativePlatforms: List<TargetPlatform> = listOf(defaultNativePlatform)
}

fun TargetPlatform?.isNative(): Boolean = this?.singleOrNull() is NativePlatform
fun TargetPlatform?.isNative(): Boolean = this?.isNotEmpty() == true && all { it is NativePlatform }
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.jetbrains.kotlin.resolve.TargetPlatform

@Deprecated(
message = "This class is deprecated and will be removed soon, use API from 'org.jetbrains.kotlin.platform.*' packages instead",
replaceWith = ReplaceWith("NativePlatforms.defaultNativePlatform", "org.jetbrains.kotlin.platform.konan.NativePlatforms"),
replaceWith = ReplaceWith("NativePlatforms.unspecifiedNativePlatform", "org.jetbrains.kotlin.platform.konan.NativePlatforms"),
level = DeprecationLevel.ERROR
)
interface KonanPlatform : TargetPlatform {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private fun createFakeTopDownAnalyzerForNative(
): LazyTopDownAnalyzer = createContainer("FakeTopDownAnalyzerForNative", NativePlatformAnalyzerServices) {
configureModule(
moduleContext,
NativePlatforms.defaultNativePlatform,
NativePlatforms.unspecifiedNativePlatform,
NativePlatformAnalyzerServices,
bindingTrace,
languageVersionSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ abstract class BaseDiagnosticsTest : KotlinMultiFileTestWithJava<TestModule, Tes
nameSuffix == "COMMON" -> CommonPlatforms.defaultCommonPlatform
nameSuffix == "JVM" -> JvmPlatforms.unspecifiedJvmPlatform // TODO(dsavvinov): determine JvmTarget precisely
nameSuffix == "JS" -> JsPlatforms.defaultJsPlatform
nameSuffix == "NATIVE" -> NativePlatforms.defaultNativePlatform
nameSuffix == "NATIVE" -> NativePlatforms.unspecifiedNativePlatform
nameSuffix.isEmpty() -> null // TODO(dsavvinov): this leads to 'null'-platform in ModuleDescriptor
else -> throw IllegalStateException("Can't determine platform by name $nameSuffix")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ internal class KlibModuleDescriptorFactoryImpl(val createBuiltIns: (StorageManag
KlibModuleOrigin.CAPABILITY to origin,
ImplicitIntegerCoercion.MODULE_CAPABILITY to origin.isInteropLibrary()
),
platform = NativePlatforms.defaultNativePlatform
// TODO: detect native platform by ???
platform = NativePlatforms.unspecifiedNativePlatform
)

override fun createDescriptorAndNewBuiltIns(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class CompositeResolverForModuleFactory(
yieldAll(getCommonProvidersIfAny(moduleInfo, moduleContext, moduleDescriptor, container)) // todo: module context
yieldAll(getJsProvidersIfAny(moduleInfo, moduleContext, moduleDescriptor, container))
yieldAll(getJvmProvidersIfAny(container))
yieldAll(getKonanProvidersIfAny(moduleInfo, container))
yieldAll(getNativeProvidersIfAny(moduleInfo, container))
}.toList()

return ResolverForModule(CompositePackageFragmentProvider(packageFragmentProviders), container)
Expand Down Expand Up @@ -133,11 +133,11 @@ class CompositeResolverForModuleFactory(
private fun getJvmProvidersIfAny(container: StorageComponentContainer): List<PackageFragmentProvider> =
if (targetPlatform.has<JvmPlatform>()) listOf(container.get<JavaDescriptorResolver>().packageFragmentProvider) else emptyList()

private fun getKonanProvidersIfAny(moduleInfo: ModuleInfo, container: StorageComponentContainer): List<PackageFragmentProvider> {
private fun getNativeProvidersIfAny(moduleInfo: ModuleInfo, container: StorageComponentContainer): List<PackageFragmentProvider> {
if (!targetPlatform.has<NativePlatform>()) return emptyList()

return listOfNotNull(
NativePlatforms.defaultNativePlatform.idePlatformKind.resolution.createKlibPackageFragmentProvider(
NativePlatforms.unspecifiedNativePlatform.idePlatformKind.resolution.createKlibPackageFragmentProvider(
moduleInfo,
container.get<StorageManager>(),
container.get<LanguageVersionSettings>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ open class KotlinNativeGradleConfigurator : KotlinWithGradleConfigurator() {

override val name: String get() = NAME

override val targetPlatform get() = NativePlatforms.defaultNativePlatform
override val targetPlatform get() = NativePlatforms.unspecifiedNativePlatform

@Suppress("DEPRECATION_ERROR")
override fun getTargetPlatform() = NativePlatforms.CompatNativePlatform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ class HierarchicalMultiplatformProjectImportingTest : MultiplePluginVersionGradl
module("my-app")
module("project.my-app.commonMain") {
isHMPP(true)
targetPlatform(JsPlatforms.defaultJsPlatform, JvmPlatforms.jvm16, NativePlatforms.defaultNativePlatform)
targetPlatform(JsPlatforms.defaultJsPlatform, JvmPlatforms.jvm16, NativePlatforms.unspecifiedNativePlatform)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:${gradleKotlinPluginVersion}", DependencyScope.COMPILE)
sourceFolder("src/commonMain/kotlin", SourceKotlinRootType)
sourceFolder("src/commonMain/resources", ResourceKotlinRootType)
}

module("project.my-app.commonTest") {
isHMPP(true)
targetPlatform(JsPlatforms.defaultJsPlatform, JvmPlatforms.jvm16, NativePlatforms.defaultNativePlatform)
targetPlatform(JsPlatforms.defaultJsPlatform, JvmPlatforms.jvm16, NativePlatforms.unspecifiedNativePlatform)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:${gradleKotlinPluginVersion}", DependencyScope.TEST)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-test-annotations-common:${gradleKotlinPluginVersion}", DependencyScope.TEST)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-test-common:${gradleKotlinPluginVersion}", DependencyScope.TEST)
Expand Down Expand Up @@ -165,7 +165,7 @@ class HierarchicalMultiplatformProjectImportingTest : MultiplePluginVersionGradl

module("project.my-app.linuxAndJsMain") {
isHMPP(true)
targetPlatform(JsPlatforms.defaultJsPlatform, NativePlatforms.defaultNativePlatform)
targetPlatform(JsPlatforms.defaultJsPlatform, NativePlatforms.unspecifiedNativePlatform)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:${gradleKotlinPluginVersion}", DependencyScope.COMPILE)
moduleDependency("project.my-app.commonMain", DependencyScope.COMPILE)
sourceFolder("src/linuxAndJsMain/kotlin", SourceKotlinRootType)
Expand All @@ -174,7 +174,7 @@ class HierarchicalMultiplatformProjectImportingTest : MultiplePluginVersionGradl

module("project.my-app.linuxAndJsTest") {
isHMPP(true)
targetPlatform(JsPlatforms.defaultJsPlatform, NativePlatforms.defaultNativePlatform)
targetPlatform(JsPlatforms.defaultJsPlatform, NativePlatforms.unspecifiedNativePlatform)
sourceFolder("src/linuxAndJsTest/kotlin", TestSourceKotlinRootType)
sourceFolder("src/linuxAndJsTest/resources", TestResourceKotlinRootType)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:${gradleKotlinPluginVersion}", DependencyScope.TEST)
Expand All @@ -187,7 +187,7 @@ class HierarchicalMultiplatformProjectImportingTest : MultiplePluginVersionGradl

module("project.my-app.linuxX64Main") {
isHMPP(true)
targetPlatform(NativePlatforms.defaultNativePlatform)
targetPlatform(NativePlatforms.unspecifiedNativePlatform)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:${gradleKotlinPluginVersion}", DependencyScope.COMPILE)
libraryDependency("Kotlin/Native ${gradleKotlinPluginVersion} - builtin [linux_x64]", DependencyScope.PROVIDED)
libraryDependency("Kotlin/Native ${gradleKotlinPluginVersion} - iconv [linux_x64]", DependencyScope.PROVIDED)
Expand All @@ -203,7 +203,7 @@ class HierarchicalMultiplatformProjectImportingTest : MultiplePluginVersionGradl

module("project.my-app.linuxX64Test") {
isHMPP(true)
targetPlatform(NativePlatforms.defaultNativePlatform)
targetPlatform(NativePlatforms.unspecifiedNativePlatform)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:${gradleKotlinPluginVersion}", DependencyScope.TEST)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-test-annotations-common:${gradleKotlinPluginVersion}", DependencyScope.TEST)
libraryDependency("Gradle: org.jetbrains.kotlin:kotlin-test-common:${gradleKotlinPluginVersion}", DependencyScope.TEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun IdePlatform<*, *>.toNewPlatform(): NewPlatform = when (this) {
is CommonIdePlatformKind.Platform -> CommonPlatforms.defaultCommonPlatform
is JvmIdePlatformKind.Platform -> JvmPlatforms.jvmPlatformByTargetVersion(this.version)
is JsIdePlatformKind.Platform -> JsPlatforms.defaultJsPlatform
is NativeIdePlatformKind.Platform -> NativePlatforms.defaultNativePlatform
is NativeIdePlatformKind.Platform -> NativePlatforms.unspecifiedNativePlatform
else -> error("Unknown platform $this")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import org.jetbrains.kotlin.platform.IdePlatformKind
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.TargetPlatformVersion
import org.jetbrains.kotlin.platform.konan.NativePlatforms
import org.jetbrains.kotlin.platform.konan.isNative

object NativeIdePlatformKind : IdePlatformKind<NativeIdePlatformKind>() {
override fun supportsTargetPlatform(platform: TargetPlatform): Boolean = platform == NativePlatforms.defaultNativePlatform
override fun supportsTargetPlatform(platform: TargetPlatform): Boolean = platform.isNative()

override fun platformByCompilerArguments(arguments: CommonCompilerArguments): TargetPlatform? {
return if (arguments is FakeK2NativeCompilerArguments)
NativePlatforms.defaultNativePlatform
NativePlatforms.unspecifiedNativePlatform
else
null
}
Expand All @@ -30,7 +31,7 @@ object NativeIdePlatformKind : IdePlatformKind<NativeIdePlatformKind>() {
}

override val defaultPlatform: TargetPlatform
get() = NativePlatforms.defaultNativePlatform
get() = NativePlatforms.unspecifiedNativePlatform

@Deprecated(
message = "IdePlatform is deprecated and will be removed soon, please, migrate to org.jetbrains.kotlin.platform.TargetPlatform",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class NativeIdePlatformKindTooling : IdePlatformKindTooling() {

object NativeLibraryKind : PersistentLibraryKind<DummyLibraryProperties>("kotlin.native"), KotlinLibraryKind {
override val compilerPlatform: TargetPlatform
get() = NativePlatforms.defaultNativePlatform
// TODO: detect native platform by ???
get() = NativePlatforms.unspecifiedNativePlatform

override fun createDefaultProperties() = DummyLibraryProperties.INSTANCE!!
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object NativeLibraryType : LibraryType<DummyLibraryProperties>(NativeLibraryKind
// However this does not work for libraries that are to be just created during project build, e.g. C-interop Kotlin/Native KLIBs.
// The code below helps to perform postponed detection of Kotlin/Native libraries.
override fun detect(classesRoots: List<VirtualFile>): DummyLibraryProperties? =
if (classesRoots.firstOrNull()?.isKlibLibraryRootForPlatform(NativePlatforms.defaultNativePlatform) == true)
if (classesRoots.firstOrNull()?.isKlibLibraryRootForPlatform(NativePlatforms.unspecifiedNativePlatform) == true)
DummyLibraryProperties.INSTANCE!!
else null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class NativePlatformKindResolution : IdePlatformKindResolution {
}

override fun isLibraryFileForPlatform(virtualFile: VirtualFile): Boolean =
virtualFile.isKlibLibraryRootForPlatform(NativePlatforms.defaultNativePlatform)
virtualFile.isKlibLibraryRootForPlatform(NativePlatforms.unspecifiedNativePlatform)

override fun createResolverForModuleFactory(
settings: PlatformAnalysisParameters,
Expand Down Expand Up @@ -162,5 +162,6 @@ class NativeKlibLibraryInfo(project: Project, library: Library, libraryRoot: Str
}

override val platform: TargetPlatform
get() = NativePlatforms.defaultNativePlatform
// TODO: detect native platform by library
get() = NativePlatforms.unspecifiedNativePlatform
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class NativeResolverForModuleFactory(
moduleContext,
declarationProviderFactory,
CodeAnalyzerInitializer.getInstance(moduleContext.project).createTrace(),
NativePlatforms.defaultNativePlatform,
moduleDescriptor.platform!!,
NativePlatformAnalyzerServices,
targetEnvironment,
languageVersionSettings
Expand All @@ -56,7 +56,7 @@ class NativeResolverForModuleFactory(
var packageFragmentProvider = container.get<ResolveSession>().packageFragmentProvider

val klibPackageFragmentProvider =
NativePlatforms.defaultNativePlatform.idePlatformKind.resolution.createKlibPackageFragmentProvider(
NativePlatforms.unspecifiedNativePlatform.idePlatformKind.resolution.createKlibPackageFragmentProvider(
moduleContent.moduleInfo,
moduleContext.storageManager,
languageVersionSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class ModulesTxtBuilder {
"js" -> settings.compilerArguments =
K2JSCompilerArguments().also { settings.targetPlatform = JsPlatforms.defaultJsPlatform }
"native" -> settings.compilerArguments =
FakeK2NativeCompilerArguments().also { settings.targetPlatform = NativePlatforms.defaultNativePlatform }
FakeK2NativeCompilerArguments().also { settings.targetPlatform = NativePlatforms.unspecifiedNativePlatform }
else -> {
val flagProperty = ModulesTxt.Module.flags[flag]
if (flagProperty != null) flagProperty.set(module, true)
Expand Down

0 comments on commit 8d2e999

Please sign in to comment.