Angelo Salvade
11/25/2024, 8:39 PMHi
I'm generating code with KSP. The generated code will be used in common/intermediate code.
I understand that this is not allowed.
See <https://slack-chats.kotlinlang.org/t/16366233/i-m-trying-out-kotlin-2-0-beta-3-and-it-looks-like-generated>:
CCommon/intermediate (= none-platform) code cannot reference generated code in the compilation of platform code.
Generated codes are treated as platform code (you'll have to use expect/actual).
I therefore implemented the proposed expect/actual solution.
Everything works fine (see <https://github.com/softappeal/yass2/blob/main/yass2-generate/src/jvmMain/kotlin/ksp/Generate.kt>)
For finding out if a KSDeclaration is in platform specific code I use the following code:
private val Platforms = setOf(
"jvm", // JVM
"js", // JavaScript
"wasmJs", "wasmWasi",// WebAssembly
"macosX64", "macosArm64", // macOS
"iosArm64", "iosX64", "iosSimulatorArm64", // iOS
"linuxX64", "linuxArm64", // Linux
"watchosArm64", "watchosX64", "watchosSimulatorArm64", "watchosDeviceArm64", // watchOS
"tvosArm64", "tvosX64", "tvosSimulatorArm64", // tvOS
"mingwX64", // Windows
)
private fun KSDeclaration.isPlatformCode(): Boolean { // TODO: is there a better solution?
val filePath = containingFile!!.filePath
Platforms.forEach { platform ->
if (filePath.contains("/${platform}Main/") || filePath.contains("/${platform}Test/")) return true
}
return false
}
internal fun KSDeclaration.actual() = if (isPlatformCode()) "" else "actual "
I'm looking for a more elegant solution for isPlatformCode().
Any ideas?