[native-gradle-plugin] Run konan directly from the gradle process
This commit is contained in:
+61
-15
@@ -27,7 +27,11 @@ import org.jetbrains.kotlin.konan.KonanVersion
|
|||||||
import org.jetbrains.kotlin.konan.target.HostManager
|
import org.jetbrains.kotlin.konan.target.HostManager
|
||||||
import org.jetbrains.kotlin.konan.target.KonanTarget
|
import org.jetbrains.kotlin.konan.target.KonanTarget
|
||||||
import org.jetbrains.kotlin.konan.util.DependencyDirectories
|
import org.jetbrains.kotlin.konan.util.DependencyDirectories
|
||||||
|
import java.io.File
|
||||||
|
import java.lang.reflect.InvocationTargetException
|
||||||
|
import java.net.URLClassLoader
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
/** Copied from Kotlin/Native repository. */
|
/** Copied from Kotlin/Native repository. */
|
||||||
|
|
||||||
@@ -44,6 +48,9 @@ internal val Project.konanVersion: KonanVersion
|
|||||||
KonanVersion.fromString(it.toString())
|
KonanVersion.fromString(it.toString())
|
||||||
} ?: NativeCompilerDownloader.DEFAULT_KONAN_VERSION
|
} ?: NativeCompilerDownloader.DEFAULT_KONAN_VERSION
|
||||||
|
|
||||||
|
internal val Project.disableKonanDaemon: Boolean
|
||||||
|
get() = PropertiesProvider(this).nativeDisableCompilerDaemon == true
|
||||||
|
|
||||||
internal interface KonanToolRunner : Named {
|
internal interface KonanToolRunner : Named {
|
||||||
val mainClass: String
|
val mainClass: String
|
||||||
val classpath: FileCollection
|
val classpath: FileCollection
|
||||||
@@ -123,25 +130,64 @@ internal abstract class KonanCliRunner(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
project.javaexec { spec ->
|
if (project.disableKonanDaemon || toolName == "cinterop") {
|
||||||
spec.main = mainClass
|
project.javaexec { spec ->
|
||||||
spec.classpath = classpath
|
spec.main = mainClass
|
||||||
spec.jvmArgs(jvmArgs)
|
spec.classpath = classpath
|
||||||
spec.systemProperties(
|
spec.jvmArgs(jvmArgs)
|
||||||
System.getProperties().asSequence()
|
spec.systemProperties(
|
||||||
.map { (k, v) -> k.toString() to v.toString() }
|
System.getProperties().asSequence()
|
||||||
.filter { (k, _) -> k !in blacklistProperties }
|
.map { (k, v) -> k.toString() to v.toString() }
|
||||||
.escapeQuotesForWindows()
|
.filter { (k, _) -> k !in blacklistProperties }
|
||||||
.toMap()
|
.escapeQuotesForWindows()
|
||||||
)
|
.toMap()
|
||||||
spec.systemProperties(additionalSystemProperties)
|
)
|
||||||
spec.args(listOf(toolName) + transformArgs(args))
|
spec.systemProperties(additionalSystemProperties)
|
||||||
blacklistEnvironment.forEach { spec.environment.remove(it) }
|
spec.args(listOf(toolName) + transformArgs(args))
|
||||||
spec.environment(environment)
|
blacklistEnvironment.forEach { spec.environment.remove(it) }
|
||||||
|
spec.environment(environment)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val oldProperties = mutableMapOf<String, String?>()
|
||||||
|
additionalSystemProperties.forEach {
|
||||||
|
oldProperties[it.key] = System.getProperty(it.key)
|
||||||
|
}
|
||||||
|
System.getProperties().toList()
|
||||||
|
.map { (k, v) -> k.toString() to v.toString() }
|
||||||
|
.filter { (k, _) -> k in blacklistProperties }
|
||||||
|
.forEach { (k, v) ->
|
||||||
|
oldProperties[k] = v
|
||||||
|
System.clearProperty(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
additionalSystemProperties.forEach { System.setProperty(it.key, it.value) }
|
||||||
|
|
||||||
|
val konanCompilerClassLoader = konanCompilerClassLoadersMap.computeIfAbsent(project.konanHome) {
|
||||||
|
val arrayOfURLs = classpath.map { File(it.absolutePath).toURI().toURL() }.toTypedArray()
|
||||||
|
URLClassLoader(arrayOfURLs, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
val mainClass = konanCompilerClassLoader.loadClass(mainClass)
|
||||||
|
val mainMethod = mainClass.methods.single { it.name == "daemonMain" }
|
||||||
|
|
||||||
|
try {
|
||||||
|
mainMethod.invoke(null, (listOf(toolName) + transformArgs(args)).toTypedArray())
|
||||||
|
} catch (t: InvocationTargetException) {
|
||||||
|
throw t.targetException
|
||||||
|
} finally {
|
||||||
|
oldProperties.forEach {
|
||||||
|
val value = it.value
|
||||||
|
if (value == null)
|
||||||
|
System.clearProperty(it.key)
|
||||||
|
else System.setProperty(it.key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val konanCompilerClassLoadersMap = ConcurrentHashMap<String, ClassLoader>()
|
||||||
|
|
||||||
internal class KonanInteropRunner(project: Project, additionalJvmArgs: List<String> = emptyList()) :
|
internal class KonanInteropRunner(project: Project, additionalJvmArgs: List<String> = emptyList()) :
|
||||||
KonanCliRunner("cinterop", "Kotlin/Native cinterop tool", project, additionalJvmArgs) {
|
KonanCliRunner("cinterop", "Kotlin/Native cinterop tool", project, additionalJvmArgs) {
|
||||||
init {
|
init {
|
||||||
|
|||||||
+6
@@ -140,6 +140,12 @@ internal class PropertiesProvider private constructor(private val project: Proje
|
|||||||
val nativeJvmArgs: String?
|
val nativeJvmArgs: String?
|
||||||
get() = propertyWithDeprecatedVariant("kotlin.native.jvmArgs", "org.jetbrains.kotlin.native.jvmArgs")
|
get() = propertyWithDeprecatedVariant("kotlin.native.jvmArgs", "org.jetbrains.kotlin.native.jvmArgs")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces to run a compilation in a separate JVM.
|
||||||
|
*/
|
||||||
|
val nativeDisableCompilerDaemon: Boolean?
|
||||||
|
get() = booleanProperty("kotlin.native.disableCompilerDaemon")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate kotlin/js external declarations from all .d.ts files found in npm modules
|
* Generate kotlin/js external declarations from all .d.ts files found in npm modules
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user