[kotlin gradle plugin] enable to work with embedded k/n compiler.

This commit is contained in:
Vasily Levchenko
2021-07-09 14:39:46 +02:00
committed by Space
parent 1eb951749d
commit 2baf344f5f
8 changed files with 38 additions and 5 deletions
+5
View File
@@ -246,6 +246,7 @@ task shadowJar(type: ShadowJar) {
task distCompiler(type: Copy) { task distCompiler(type: Copy) {
dependsOn ":kotlin-native:dependencies:update" dependsOn ":kotlin-native:dependencies:update"
dependsOn ':kotlin-native:shadowJar' dependsOn ':kotlin-native:shadowJar'
dependsOn ":kotlin-native-compiler-embeddable:kotlin-native-compiler-embeddable"
destinationDir distDir destinationDir distDir
@@ -257,6 +258,10 @@ task distCompiler(type: Copy) {
into('konan/lib') into('konan/lib')
} }
from(project(":kotlin-native-compiler-embeddable").file("build/libs")) {
into("konan/lib")
}
from(project(':kotlin-native:Interop').file('Indexer/build/nativelibs')) { from(project(':kotlin-native:Interop').file('Indexer/build/nativelibs')) {
into('konan/nativelib') into('konan/nativelib')
} }
@@ -27,8 +27,8 @@ object KonanHomeProvider {
val jarPath = PathUtil.getResourcePathForClass(this::class.java) val jarPath = PathUtil.getResourcePathForClass(this::class.java)
// Check that the path obtained really points to the distribution. // Check that the path obtained really points to the distribution.
val expectedRelativeJarPath = Paths.get("konan/lib/kotlin-native.jar") check(jarPath.toPath().endsWith(Paths.get("konan/lib/kotlin-native.jar")) ||
check(jarPath.toPath().endsWith(expectedRelativeJarPath)) { jarPath.toPath().endsWith(Paths.get("konan/lib/kotlin-native-compiler-embeddable.jar"))) {
val classesPath = if (jarPath.extension == "jar") jarPath else jarPath.parentFile val classesPath = if (jarPath.extension == "jar") jarPath else jarPath.parentFile
""" """
Cannot determine a compiler distribution directory. Cannot determine a compiler distribution directory.
@@ -124,6 +124,7 @@ interface KotlinCompilerPluginSupportPlugin : Plugin<Project> {
fun getCompilerPluginId(): String fun getCompilerPluginId(): String
fun getPluginArtifact(): SubpluginArtifact fun getPluginArtifact(): SubpluginArtifact
fun getPluginArtifactForNative(): SubpluginArtifact? = null fun getPluginArtifactForNative(): SubpluginArtifact? = null
fun getPluginArtifactForNative(project: Project): SubpluginArtifact? = getPluginArtifactForNative()
} }
open class SubpluginArtifact(val groupId: String, val artifactId: String, val version: String? = null) open class SubpluginArtifact(val groupId: String, val artifactId: String, val version: String? = null)
@@ -116,6 +116,7 @@ internal abstract class KotlinToolRunner(
project.logger.info( project.logger.info(
"""|Run in-process tool "$displayName" """|Run in-process tool "$displayName"
|Entry point method = $mainClass.$daemonEntryPoint |Entry point method = $mainClass.$daemonEntryPoint
|Classpath = ${classpath.joinToString(File.pathSeparator) { it.absolutePath }}
|Arguments = ${args.toPrettyString()} |Arguments = ${args.toPrettyString()}
|Transformed arguments = ${if (transformedArgs == args) "same as arguments" else transformedArgs.toPrettyString()} |Transformed arguments = ${if (transformedArgs == args) "same as arguments" else transformedArgs.toPrettyString()}
""".trimMargin() """.trimMargin()
@@ -77,9 +77,22 @@ internal abstract class KotlinNativeToolRunner(
} }
final override val classpath by lazy { final override val classpath by lazy {
project.fileTree("${project.konanHome}/konan/lib/").apply { include("*.jar") }.toSet() project.files(
project.kotlinNativeCompilerJar,
"${project.konanHome}/konan/lib/trove4j.jar"
).files
} }
private val Project.kotlinNativeCompilerJar: String
get() = if (isNativeEmbedded)
"$konanHome/konan/lib/kotlin-native-compiler-embeddable.jar"
else
"$konanHome/konan/lib/kotlin-native.jar"
//TODO: find better place (see org.jetbrains.kotlinx.serialization.gradle.SerializationGradleSubplugin.isNativeEmbedded)
private val Project.isNativeEmbedded: Boolean
get() = findProperty("kotlin.native.shaded")?.toString()?.toBoolean() == true
final override fun checkClasspath() = final override fun checkClasspath() =
check(classpath.isNotEmpty()) { check(classpath.isNotEmpty()) {
""" """
@@ -72,7 +72,7 @@ class SubpluginEnvironment(
if (kotlinCompilation is AbstractKotlinNativeCompilation) { if (kotlinCompilation is AbstractKotlinNativeCompilation) {
subplugin.getPluginArtifactForNative()?.let { artifact -> subplugin.getPluginArtifactForNative(project)?.let { artifact ->
project.addMavenDependency(kotlinCompilation.pluginConfigurationName, artifact) project.addMavenDependency(kotlinCompilation.pluginConfigurationName, artifact)
} }
} else { } else {
@@ -167,7 +167,9 @@ class NativeCompilerDownloader(
} }
fun downloadIfNeeded() { fun downloadIfNeeded() {
if (KotlinNativeCompilerRunner(project).classpath.isEmpty()) {
val classpath = KotlinNativeCompilerRunner(project).classpath
if (classpath.isEmpty() || classpath.any { !it.exists() }) {
downloadAndExtract() downloadAndExtract()
} }
} }
@@ -50,6 +50,12 @@ class SerializationGradleSubplugin :
override fun getPluginArtifactForNative(): SubpluginArtifact? = override fun getPluginArtifactForNative(): SubpluginArtifact? =
SubpluginArtifact(SERIALIZATION_GROUP_NAME, SERIALIZATION_ARTIFACT_UNSHADED_NAME) SubpluginArtifact(SERIALIZATION_GROUP_NAME, SERIALIZATION_ARTIFACT_UNSHADED_NAME)
override fun getPluginArtifactForNative(project: Project): SubpluginArtifact? =
if (project.isNativeEmbedded)
getPluginArtifact()
else getPluginArtifactForNative()
override fun getCompilerPluginId() = "org.jetbrains.kotlinx.serialization" override fun getCompilerPluginId() = "org.jetbrains.kotlinx.serialization"
//region Stub implementation for legacy API, KT-39809 //region Stub implementation for legacy API, KT-39809
@@ -63,4 +69,9 @@ class SerializationGradleSubplugin :
"Please use an older version of kotlin-serialization or upgrade the Kotlin Gradle plugin version to make them match." "Please use an older version of kotlin-serialization or upgrade the Kotlin Gradle plugin version to make them match."
) )
//endregion //endregion
// TODO: find better place (see org.jetbrains.kotlin.compilerRunner.KotlinNativeToolRunner.isNativeEmbedded)
private val Project.isNativeEmbedded: Boolean
get() = findProperty("kotlin.native.shaded")?.toString()?.toBoolean() == true
} }